Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 143 lines (124 sloc) 3.761 kB
96cc0ed @joshwilsdon OS-858 add experimental support for VM.send, VM.receive, VM.install t…
joshwilsdon authored
1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at http://smartos.org/CDDL
10 *
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file.
16 *
17 * If applicable, add the following below this CDDL HEADER, with the
18 * fields enclosed by brackets "[]" replaced with your own identifying
19 * information: Portions Copyright [yyyy] [name of copyright owner]
20 *
21 * CDDL HEADER END
22 *
23 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
24 *
25 */
26
27 #include <sys/socket.h>
28 #include <sys/types.h>
29 #include <arpa/inet.h>
30 #include <errno.h>
31 #include <netdb.h>
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <strings.h>
35 #include <unistd.h>
36
37 /*
38 * To match /usr/sbin/zfs, the following exit statuses will be used:
39 *
40 * 0 Successful completion.
41 * 1 An error occurred.
42 * 2 Invalid command line options were specified.
43 *
44 * Diagnostic output goes to stderr.
45 * On success it a TCP socket will be opened listening for data from zfs_send.
46 *
47 */
48
49 int main(int argc, char *argv[]) {
50 int conn;
51 struct addrinfo hints;
52 struct addrinfo *res;
53 char *host;
54 char *port;
55 char rhost[NI_MAXHOST + NI_MAXSERV];
56 char rport[NI_MAXSERV];
57 int error;
58 int opt;
59 int sock;
60
61 if (argc < 4) {
62 fprintf(stderr, "Usage: %s <host> <port> ['zfs recv' args ...]\n",
63 argv[0]);
64 exit(2);
65 }
66
67 host = argv[1];
68 port = argv[2];
69
70 bzero((char *)&hints, sizeof (struct addrinfo));
71 hints.ai_family = PF_UNSPEC;
72 hints.ai_socktype = SOCK_STREAM;
73 hints.ai_protocol = IPPROTO_TCP;
74 hints.ai_flags |= AI_NUMERICHOST;
75 hints.ai_flags |= AI_NUMERICSERV;
76
77 if ((error = getaddrinfo(host, port, &hints, &res))) {
78 fprintf(stderr, "zfs_recv: getaddrinfo(): %s\n", gai_strerror(error));
79 exit(1);
80 }
81
82 sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
83 if (sock < 0) {
84 perror("zfs_recv: socket()");
85 exit(1);
86 }
87
88 opt = 1;
89 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
90 (char *)&opt, sizeof (opt)) < 0) {
91
92 perror("zfs_recv: setsockopt()");
93 exit(1);
94 }
95
96 if (bind(sock, (struct sockaddr *)res->ai_addr, res->ai_addrlen) < 0) {
97 perror("zfs_recv: bind()");
98 exit(1);
99 }
100
101 if (listen(sock, 1) < 0) {
102 perror("zfs_recv: listen()");
103 exit(1);
104 }
105
106 error = getnameinfo(res->ai_addr, res->ai_addrlen, rhost, sizeof (rhost),
107 rport, sizeof (rport), NI_NUMERICHOST|NI_NUMERICSERV);
108 if (error != 0) {
109 fprintf(stderr, "zfs_recv: getnameinfo(): %s\n", gai_strerror(error));
110 exit(1);
111 }
112 fprintf(stderr, "Waiting for stream on: {'host': '%s', 'port': '%s'}\n",
113 rhost, rport);
114
115 freeaddrinfo(res);
116
117 if ((conn = accept(sock, NULL, NULL)) < 0) {
118 perror("zfs_recv: accept()");
119 exit(1);
120 }
121
122 /* redir stdout and stderr to the socket, keep stderr (caller can log it) */
123 if (dup2(conn, 0) < 0) {
124 perror("zfs_recv: dup2(stdin)");
125 exit(1);
126 }
127 if (dup2(conn, 1) < 0) {
128 perror("zfs_recv: dup2(stdout)");
129 exit(1);
130 }
131
132 /* run the <program> and its args */
133 argv++;
134 argc--;
135 argv[0] = "/usr/sbin/zfs"; // replace host
136 argv[1] = "recv"; // replace port
137 execvp(*argv, argv); // now 'zfs recv <args>'
138
139 /* if we got here we failed. */
140 perror("zfs_recv: execvp()");
141 exit(1);
142 }
Something went wrong with that request. Please try again.