2 * Copyright (c) 2004 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 #include <sys/param.h>
44 #include <sys/ioctl.h>
45 #include <sys/socket.h>
46 #include <sys/sysctl.h>
47 #include <sys/syslog.h>
50 #include <netinet/in.h>
51 #include <netinet/tcp.h>
52 #include <arpa/inet.h>
54 #include <geom/gate/g_gate.h>
58 enum { UNSET, CREATE, DESTROY, LIST, RESCUE } action = UNSET;
60 static const char *path = NULL;
61 static const char *host = NULL;
63 static unsigned flags = 0;
65 static unsigned queue_size = G_GATE_QUEUE_SIZE;
66 static unsigned port = G_GATE_PORT;
67 static off_t mediasize;
68 static unsigned sectorsize = 0;
69 static unsigned timeout = G_GATE_TIMEOUT;
70 static int sendfd, recvfd;
71 static uint32_t token;
72 static pthread_t sendtd, recvtd;
79 fprintf(stderr, "usage: %s create [-nv] [-o <ro|wo|rw>] [-p port] "
80 "[-q queue_size] [-R rcvbuf] [-S sndbuf] [-s sectorsize] "
81 "[-t timeout] [-u unit] <host> <path>\n", getprogname());
82 fprintf(stderr, " %s rescue [-nv] [-o <ro|wo|rw>] [-p port] "
83 "[-R rcvbuf] [-S sndbuf] <-u unit> <host> <path>\n", getprogname());
84 fprintf(stderr, " %s destroy [-f] <-u unit>\n", getprogname());
85 fprintf(stderr, " %s list [-v] [-u unit]\n", getprogname());
90 send_thread(void *arg __unused)
92 struct g_gate_ctl_io ggio;
93 struct g_gate_hdr hdr;
98 g_gate_log(LOG_NOTICE, "%s: started!", __func__);
100 ggio.gctl_version = G_GATE_VERSION;
101 ggio.gctl_unit = unit;
102 ggio.gctl_data = buf;
105 ggio.gctl_length = sizeof(buf);
107 g_gate_ioctl(G_GATE_CMD_START, &ggio);
108 error = ggio.gctl_error;
115 /* Exit gracefully. */
116 g_gate_close_device();
120 /* Buffer too small. */
121 ggio.gctl_data = realloc(ggio.gctl_data,
123 if (ggio.gctl_data != NULL) {
124 bsize = ggio.gctl_length;
131 g_gate_xlog("ioctl(/dev/%s): %s.", G_GATE_CTL_NAME,
138 switch (ggio.gctl_cmd) {
140 hdr.gh_cmd = GGATE_CMD_READ;
143 hdr.gh_cmd = GGATE_CMD_WRITE;
146 hdr.gh_seq = ggio.gctl_seq;
147 hdr.gh_offset = ggio.gctl_offset;
148 hdr.gh_length = ggio.gctl_length;
150 g_gate_swap2n_hdr(&hdr);
152 data = g_gate_send(sendfd, &hdr, sizeof(hdr), MSG_NOSIGNAL);
153 g_gate_log(LOG_DEBUG, "Sent hdr packet.");
154 g_gate_swap2h_hdr(&hdr);
157 if (data != sizeof(hdr)) {
158 g_gate_log(LOG_ERR, "Lost connection 1.");
160 pthread_kill(recvtd, SIGUSR1);
164 if (hdr.gh_cmd == GGATE_CMD_WRITE) {
165 data = g_gate_send(sendfd, ggio.gctl_data,
166 ggio.gctl_length, MSG_NOSIGNAL);
169 if (data != ggio.gctl_length) {
170 g_gate_log(LOG_ERR, "Lost connection 2 (%zd != %zd).", data, (ssize_t)ggio.gctl_length);
172 pthread_kill(recvtd, SIGUSR1);
175 g_gate_log(LOG_DEBUG, "Sent %zd bytes (offset=%llu, "
176 "size=%u).", data, hdr.gh_offset, hdr.gh_length);
179 g_gate_log(LOG_DEBUG, "%s: Died.", __func__);
184 recv_thread(void *arg __unused)
186 struct g_gate_ctl_io ggio;
187 struct g_gate_hdr hdr;
191 g_gate_log(LOG_NOTICE, "%s: started!", __func__);
193 ggio.gctl_version = G_GATE_VERSION;
194 ggio.gctl_unit = unit;
195 ggio.gctl_data = buf;
198 data = g_gate_recv(recvfd, &hdr, sizeof(hdr), MSG_WAITALL);
201 g_gate_swap2h_hdr(&hdr);
202 if (data != sizeof(hdr)) {
203 if (data == -1 && errno == EAGAIN)
205 g_gate_log(LOG_ERR, "Lost connection 3.");
207 pthread_kill(sendtd, SIGUSR1);
210 g_gate_log(LOG_DEBUG, "Received hdr packet.");
212 ggio.gctl_seq = hdr.gh_seq;
213 ggio.gctl_cmd = hdr.gh_cmd;
214 ggio.gctl_offset = hdr.gh_offset;
215 ggio.gctl_length = hdr.gh_length;
216 ggio.gctl_error = hdr.gh_error;
218 if (ggio.gctl_error == 0 && ggio.gctl_cmd == GGATE_CMD_READ) {
219 data = g_gate_recv(recvfd, ggio.gctl_data,
220 ggio.gctl_length, MSG_WAITALL);
223 g_gate_log(LOG_DEBUG, "Received data packet.");
224 if (data != ggio.gctl_length) {
225 g_gate_log(LOG_ERR, "Lost connection 4.");
227 pthread_kill(sendtd, SIGUSR1);
230 g_gate_log(LOG_DEBUG, "Received %d bytes (offset=%ju, "
231 "size=%zu).", data, (uintmax_t)hdr.gh_offset,
232 (size_t)hdr.gh_length);
235 g_gate_ioctl(G_GATE_CMD_DONE, &ggio);
237 g_gate_log(LOG_DEBUG, "%s: Died.", __func__);
244 struct g_gate_version ver;
245 struct g_gate_cinit cinit;
246 struct g_gate_sinit sinit;
247 struct sockaddr_in serv;
251 * Do the network stuff.
253 bzero(&serv, sizeof(serv));
254 serv.sin_family = AF_INET;
255 serv.sin_addr.s_addr = g_gate_str2ip(host);
256 if (serv.sin_addr.s_addr == INADDR_NONE) {
257 g_gate_log(LOG_DEBUG, "Invalid IP/host name: %s.", host);
260 serv.sin_port = htons(port);
261 sfd = socket(AF_INET, SOCK_STREAM, 0);
263 g_gate_log(LOG_DEBUG, "Cannot open socket: %s.",
268 g_gate_socket_settings(sfd);
270 if (connect(sfd, (struct sockaddr *)&serv, sizeof(serv)) == -1) {
271 g_gate_log(LOG_DEBUG, "Cannot connect to server: %s.",
277 g_gate_log(LOG_INFO, "Connected to the server: %s:%d.", host, port);
280 * Create and send version packet.
282 g_gate_log(LOG_DEBUG, "Sending version packet.");
283 assert(strlen(GGATE_MAGIC) == sizeof(ver.gv_magic));
284 bcopy(GGATE_MAGIC, ver.gv_magic, sizeof(ver.gv_magic));
285 ver.gv_version = GGATE_VERSION;
287 g_gate_swap2n_version(&ver);
288 if (g_gate_send(sfd, &ver, sizeof(ver), MSG_NOSIGNAL) == -1) {
289 g_gate_log(LOG_DEBUG, "Error while sending version packet: %s.",
294 bzero(&ver, sizeof(ver));
295 if (g_gate_recv(sfd, &ver, sizeof(ver), MSG_WAITALL) == -1) {
296 g_gate_log(LOG_DEBUG, "Error while receiving data: %s.",
301 if (ver.gv_error != 0) {
302 g_gate_log(LOG_DEBUG, "Version verification problem: %s.",
309 * Create and send initial packet.
311 g_gate_log(LOG_DEBUG, "Sending initial packet.");
312 if (strlcpy(cinit.gc_path, path, sizeof(cinit.gc_path)) >=
313 sizeof(cinit.gc_path)) {
314 g_gate_log(LOG_DEBUG, "Path name too long.");
318 cinit.gc_flags = flags | dir;
319 cinit.gc_token = token;
321 g_gate_swap2n_cinit(&cinit);
322 if (g_gate_send(sfd, &cinit, sizeof(cinit), MSG_NOSIGNAL) == -1) {
323 g_gate_log(LOG_DEBUG, "Error while sending initial packet: %s.",
328 g_gate_swap2h_cinit(&cinit);
331 * Receiving initial packet from server.
333 g_gate_log(LOG_DEBUG, "Receiving initial packet.");
334 if (g_gate_recv(sfd, &sinit, sizeof(sinit), MSG_WAITALL) == -1) {
335 g_gate_log(LOG_DEBUG, "Error while receiving data: %s.",
340 g_gate_swap2h_sinit(&sinit);
341 if (sinit.gs_error != 0) {
342 g_gate_log(LOG_DEBUG, "Error from server: %s.",
343 strerror(sinit.gs_error));
347 g_gate_log(LOG_DEBUG, "Received initial packet.");
349 mediasize = sinit.gs_mediasize;
351 sectorsize = sinit.gs_sectorsize;
360 if (g_gate_verbose > 0)
362 if (daemon(0, 0) == 0)
364 if (action == CREATE)
365 g_gate_destroy(unit, 1);
366 err(EXIT_FAILURE, "Cannot daemonize");
370 g_gatec_connect(void)
373 token = arc4random();
375 * Our receive descriptor is connected to the send descriptor on the
378 recvfd = handshake(GGATE_FLAG_SEND);
382 * Our send descriptor is connected to the receive descriptor on the
385 sendfd = handshake(GGATE_FLAG_RECV);
397 error = pthread_create(&recvtd, NULL, recv_thread, NULL);
399 g_gate_destroy(unit, 1);
400 g_gate_xlog("pthread_create(recv_thread): %s.",
403 sendtd = pthread_self();
411 signop(int sig __unused)
420 struct g_gate_ctl_cancel ggioc;
422 signal(SIGUSR1, signop);
425 g_gate_log(LOG_NOTICE, "Disconnected [%s %s]. Connecting...",
427 while (!g_gatec_connect()) {
429 g_gate_log(LOG_NOTICE, "Connecting [%s %s]...", host,
432 ggioc.gctl_version = G_GATE_VERSION;
433 ggioc.gctl_unit = unit;
435 g_gate_ioctl(G_GATE_CMD_CANCEL, &ggioc);
442 struct g_gate_ctl_create ggioc;
444 if (!g_gatec_connect())
445 g_gate_xlog("Cannot connect: %s.", strerror(errno));
448 * Ok, got both sockets, time to create provider.
450 ggioc.gctl_version = G_GATE_VERSION;
451 ggioc.gctl_mediasize = mediasize;
452 ggioc.gctl_sectorsize = sectorsize;
453 ggioc.gctl_flags = flags;
454 ggioc.gctl_maxcount = queue_size;
455 ggioc.gctl_timeout = timeout;
456 ggioc.gctl_unit = unit;
457 snprintf(ggioc.gctl_info, sizeof(ggioc.gctl_info), "%s:%u %s", host,
459 g_gate_ioctl(G_GATE_CMD_CREATE, &ggioc);
461 printf("%s%u\n", G_GATE_PROVIDER_NAME, ggioc.gctl_unit);
464 unit = ggioc.gctl_unit;
473 struct g_gate_ctl_cancel ggioc;
475 if (!g_gatec_connect())
476 g_gate_xlog("Cannot connect: %s.", strerror(errno));
478 ggioc.gctl_version = G_GATE_VERSION;
479 ggioc.gctl_unit = unit;
481 g_gate_ioctl(G_GATE_CMD_CANCEL, &ggioc);
488 main(int argc, char *argv[])
493 if (strcasecmp(argv[1], "create") == 0)
495 else if (strcasecmp(argv[1], "destroy") == 0)
497 else if (strcasecmp(argv[1], "list") == 0)
499 else if (strcasecmp(argv[1], "rescue") == 0)
508 ch = getopt(argc, argv, "fno:p:q:R:S:s:t:u:v");
513 if (action != DESTROY)
518 if (action != CREATE && action != RESCUE)
523 if (action != CREATE && action != RESCUE)
525 if (strcasecmp("ro", optarg) == 0)
526 flags = G_GATE_FLAG_READONLY;
527 else if (strcasecmp("wo", optarg) == 0)
528 flags = G_GATE_FLAG_WRITEONLY;
529 else if (strcasecmp("rw", optarg) == 0)
533 "Invalid argument for '-o' option.");
537 if (action != CREATE && action != RESCUE)
540 port = strtoul(optarg, NULL, 10);
541 if (port == 0 && errno != 0)
542 errx(EXIT_FAILURE, "Invalid port.");
545 if (action != CREATE)
548 queue_size = strtoul(optarg, NULL, 10);
549 if (queue_size == 0 && errno != 0)
550 errx(EXIT_FAILURE, "Invalid queue_size.");
553 if (action != CREATE && action != RESCUE)
556 rcvbuf = strtoul(optarg, NULL, 10);
557 if (rcvbuf == 0 && errno != 0)
558 errx(EXIT_FAILURE, "Invalid rcvbuf.");
561 if (action != CREATE && action != RESCUE)
564 sndbuf = strtoul(optarg, NULL, 10);
565 if (sndbuf == 0 && errno != 0)
566 errx(EXIT_FAILURE, "Invalid sndbuf.");
569 if (action != CREATE)
572 sectorsize = strtoul(optarg, NULL, 10);
573 if (sectorsize == 0 && errno != 0)
574 errx(EXIT_FAILURE, "Invalid sectorsize.");
577 if (action != CREATE)
580 timeout = strtoul(optarg, NULL, 10);
581 if (timeout == 0 && errno != 0)
582 errx(EXIT_FAILURE, "Invalid timeout.");
586 unit = strtol(optarg, NULL, 10);
587 if (unit == 0 && errno != 0)
588 errx(EXIT_FAILURE, "Invalid unit number.");
591 if (action == DESTROY)
606 g_gate_load_module();
607 g_gate_open_device();
614 fprintf(stderr, "Required unit number.\n");
618 g_gate_open_device();
619 g_gate_destroy(unit, force);
622 g_gate_list(unit, g_gate_verbose);
628 fprintf(stderr, "Required unit number.\n");
631 g_gate_open_device();
640 g_gate_close_device();