xref: /freebsd/sbin/ggate/ggatec/ggatec.c (revision 7ef62cebc2f965b0f640263e179276928885e33d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2004 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <stdint.h>
34 #include <fcntl.h>
35 #include <unistd.h>
36 #include <string.h>
37 #include <ctype.h>
38 #include <inttypes.h>
39 #include <libgen.h>
40 #include <pthread.h>
41 #include <signal.h>
42 #include <err.h>
43 #include <errno.h>
44 #include <assert.h>
45 
46 #include <sys/param.h>
47 #include <sys/ioctl.h>
48 #include <sys/socket.h>
49 #include <sys/sysctl.h>
50 #include <sys/syslog.h>
51 #include <sys/time.h>
52 #include <sys/bio.h>
53 #include <netinet/in.h>
54 #include <netinet/tcp.h>
55 #include <arpa/inet.h>
56 
57 #include <geom/gate/g_gate.h>
58 #include "ggate.h"
59 
60 
61 static enum { UNSET, CREATE, DESTROY, LIST, RESCUE } action = UNSET;
62 
63 static const char *path = NULL;
64 static const char *host = NULL;
65 static int unit = G_GATE_UNIT_AUTO;
66 static unsigned flags = 0;
67 static int force = 0;
68 static unsigned queue_size = G_GATE_QUEUE_SIZE;
69 static unsigned port = G_GATE_PORT;
70 static off_t mediasize;
71 static unsigned sectorsize = 0;
72 static unsigned timeout = G_GATE_TIMEOUT;
73 static int sendfd, recvfd;
74 static uint32_t token;
75 static pthread_t sendtd, recvtd;
76 static int reconnect;
77 static int initialbuffersize = 131072;
78 
79 static void
80 usage(void)
81 {
82 
83 	fprintf(stderr, "usage: %s create [-nv] [-o <ro|wo|rw>] [-p port] "
84 	    "[-q queue_size] [-R rcvbuf] [-S sndbuf] [-s sectorsize] "
85 	    "[-t timeout] [-u unit] <host> <path>\n", getprogname());
86 	fprintf(stderr, "       %s rescue [-nv] [-o <ro|wo|rw>] [-p port] "
87 	    "[-R rcvbuf] [-S sndbuf] <-u unit> <host> <path>\n", getprogname());
88 	fprintf(stderr, "       %s destroy [-f] <-u unit>\n", getprogname());
89 	fprintf(stderr, "       %s list [-v] [-u unit]\n", getprogname());
90 	exit(EXIT_FAILURE);
91 }
92 
93 static void *
94 send_thread(void *arg __unused)
95 {
96 	struct g_gate_ctl_io ggio;
97 	struct g_gate_hdr hdr;
98 	size_t buf_capacity;
99 	ssize_t numbytesprocd;
100 	int error;
101 	char *newbuf;
102 
103 	g_gate_log(LOG_NOTICE, "%s: started!", __func__);
104 
105 	buf_capacity = initialbuffersize;
106 
107 	ggio.gctl_version = G_GATE_VERSION;
108 	ggio.gctl_unit = unit;
109 	ggio.gctl_data = malloc(buf_capacity);
110 	if (ggio.gctl_data == NULL) {
111 		g_gate_log(LOG_ERR, "%s: Cannot alloc buffer.", __func__);
112 		pthread_exit(NULL);
113 	}
114 
115 	for (;;) {
116 		ggio.gctl_length = buf_capacity;
117 		ggio.gctl_error = 0;
118 		g_gate_ioctl(G_GATE_CMD_START, &ggio);
119 		error = ggio.gctl_error;
120 		switch (error) {
121 		case 0:
122 			break;
123 		case ECANCELED:
124 			if (reconnect)
125 				break;
126 			/* Exit gracefully. */
127 			g_gate_close_device();
128 			exit(EXIT_SUCCESS);
129 
130 		case ENOMEM:
131 		{
132 			/* Buffer too small. */
133 			g_gate_log(LOG_DEBUG, "buffer too small. new size: %u",
134 			    ggio.gctl_length);
135 			newbuf = malloc(ggio.gctl_length);
136 			if (newbuf != NULL) {
137 				free(ggio.gctl_data);
138 				ggio.gctl_data = newbuf;
139 				buf_capacity = ggio.gctl_length;
140 				continue;
141 			}
142 			/* FALLTHROUGH */
143 		}
144 
145 		case ENXIO:
146 		default:
147 			g_gate_xlog("ioctl(/dev/%s): %s.", G_GATE_CTL_NAME,
148 			    strerror(error));
149 		}
150 
151 		if (reconnect)
152 			break;
153 
154 		switch (ggio.gctl_cmd) {
155 		case BIO_READ:
156 			hdr.gh_cmd = GGATE_CMD_READ;
157 			break;
158 		case BIO_WRITE:
159 			hdr.gh_cmd = GGATE_CMD_WRITE;
160 			break;
161 		case BIO_FLUSH:
162 			hdr.gh_cmd = GGATE_CMD_FLUSH;
163 			break;
164 		default:
165 			g_gate_log(LOG_NOTICE, "Unknown gctl_cmd: %i",
166 			    ggio.gctl_cmd);
167 			ggio.gctl_error = EOPNOTSUPP;
168 			g_gate_ioctl(G_GATE_CMD_DONE, &ggio);
169 			continue;
170 		}
171 
172 		hdr.gh_seq = ggio.gctl_seq;
173 		hdr.gh_offset = ggio.gctl_offset;
174 		hdr.gh_length = ggio.gctl_length;
175 		hdr.gh_error = 0;
176 		g_gate_swap2n_hdr(&hdr);
177 
178 		numbytesprocd = g_gate_send(sendfd, &hdr, sizeof(hdr), MSG_NOSIGNAL);
179 		g_gate_log(LOG_DEBUG, "Sent hdr packet.");
180 		g_gate_swap2h_hdr(&hdr);
181 		if (reconnect)
182 			break;
183 		if (numbytesprocd != sizeof(hdr)) {
184 			g_gate_log(LOG_ERR, "Lost connection 1.");
185 			reconnect = 1;
186 			pthread_kill(recvtd, SIGUSR1);
187 			break;
188 		}
189 
190 		if (hdr.gh_cmd == GGATE_CMD_WRITE) {
191 			numbytesprocd = g_gate_send(sendfd, ggio.gctl_data,
192 			    ggio.gctl_length, MSG_NOSIGNAL);
193 			if (reconnect)
194 				break;
195 			if (numbytesprocd != ggio.gctl_length) {
196 				g_gate_log(LOG_ERR, "Lost connection 2 (%zd != %zd).",
197 				    numbytesprocd, (ssize_t)ggio.gctl_length);
198 				reconnect = 1;
199 				pthread_kill(recvtd, SIGUSR1);
200 				break;
201 			}
202 			g_gate_log(LOG_DEBUG, "Sent %zd bytes (offset=%"
203 			    PRIu64 ", length=%" PRIu32 ").", numbytesprocd,
204 			    hdr.gh_offset, hdr.gh_length);
205 		}
206 	}
207 	g_gate_log(LOG_DEBUG, "%s: Died.", __func__);
208 	return (NULL);
209 }
210 
211 static void *
212 recv_thread(void *arg __unused)
213 {
214 	struct g_gate_ctl_io ggio;
215 	struct g_gate_hdr hdr;
216 	ssize_t buf_capacity;
217 	ssize_t numbytesprocd;
218 	char *newbuf;
219 
220 	g_gate_log(LOG_NOTICE, "%s: started!", __func__);
221 
222 	buf_capacity = initialbuffersize;
223 
224 	ggio.gctl_version = G_GATE_VERSION;
225 	ggio.gctl_unit = unit;
226 	ggio.gctl_data = malloc(buf_capacity);
227 	if (ggio.gctl_data == NULL) {
228 		g_gate_log(LOG_ERR, "%s: Cannot alloc buffer.", __func__);
229 		pthread_exit(NULL);
230 	}
231 
232 	for (;;) {
233 		numbytesprocd = g_gate_recv(recvfd, &hdr, sizeof(hdr), MSG_WAITALL);
234 		if (reconnect)
235 			break;
236 		g_gate_swap2h_hdr(&hdr);
237 		if (numbytesprocd != sizeof(hdr)) {
238 			if (numbytesprocd == -1 && errno == EAGAIN)
239 				continue;
240 			g_gate_log(LOG_ERR, "Lost connection 3.");
241 			reconnect = 1;
242 			pthread_kill(sendtd, SIGUSR1);
243 			break;
244 		}
245 		g_gate_log(LOG_DEBUG, "Received hdr packet.");
246 
247 		ggio.gctl_seq = hdr.gh_seq;
248 		ggio.gctl_cmd = hdr.gh_cmd;
249 		ggio.gctl_offset = hdr.gh_offset;
250 		ggio.gctl_length = hdr.gh_length;
251 		ggio.gctl_error = hdr.gh_error;
252 
253 		if (ggio.gctl_length > buf_capacity) {
254 			newbuf = malloc(ggio.gctl_length);
255 			if (newbuf != NULL) {
256 				free(ggio.gctl_data);
257 				ggio.gctl_data = newbuf;
258 				buf_capacity = ggio.gctl_length;
259 			} else {
260 				g_gate_log(LOG_ERR, "Received too big response: %zd",
261 				    ggio.gctl_length);
262 				break;
263 			}
264 		}
265 
266 		if (ggio.gctl_error == 0 && ggio.gctl_cmd == GGATE_CMD_READ) {
267 			numbytesprocd = g_gate_recv(recvfd, ggio.gctl_data,
268 			    ggio.gctl_length, MSG_WAITALL);
269 			if (reconnect)
270 				break;
271 			g_gate_log(LOG_DEBUG, "Received data packet.");
272 			if (numbytesprocd != ggio.gctl_length) {
273 				g_gate_log(LOG_ERR, "Lost connection 4.");
274 				reconnect = 1;
275 				pthread_kill(sendtd, SIGUSR1);
276 				break;
277 			}
278 			g_gate_log(LOG_DEBUG, "Received %d bytes (offset=%"
279 			    PRIu64 ", length=%" PRIu32 ").", numbytesprocd,
280 			    hdr.gh_offset, hdr.gh_length);
281 		}
282 
283 		g_gate_ioctl(G_GATE_CMD_DONE, &ggio);
284 	}
285 	g_gate_log(LOG_DEBUG, "%s: Died.", __func__);
286 	pthread_exit(NULL);
287 }
288 
289 static int
290 handshake(int dir)
291 {
292 	struct g_gate_version ver;
293 	struct g_gate_cinit cinit;
294 	struct g_gate_sinit sinit;
295 	struct sockaddr_in serv;
296 	int sfd;
297 
298 	/*
299 	 * Do the network stuff.
300 	 */
301 	bzero(&serv, sizeof(serv));
302 	serv.sin_family = AF_INET;
303 	serv.sin_addr.s_addr = g_gate_str2ip(host);
304 	if (serv.sin_addr.s_addr == INADDR_NONE) {
305 		g_gate_log(LOG_DEBUG, "Invalid IP/host name: %s.", host);
306 		return (-1);
307 	}
308 	serv.sin_port = htons(port);
309 	sfd = socket(AF_INET, SOCK_STREAM, 0);
310 	if (sfd == -1) {
311 		g_gate_log(LOG_DEBUG, "Cannot open socket: %s.",
312 		    strerror(errno));
313 		return (-1);
314 	}
315 
316 	g_gate_socket_settings(sfd);
317 
318 	if (connect(sfd, (struct sockaddr *)&serv, sizeof(serv)) == -1) {
319 		g_gate_log(LOG_DEBUG, "Cannot connect to server: %s.",
320 		    strerror(errno));
321 		close(sfd);
322 		return (-1);
323 	}
324 
325 	g_gate_log(LOG_INFO, "Connected to the server: %s:%d.", host, port);
326 
327 	/*
328 	 * Create and send version packet.
329 	 */
330 	g_gate_log(LOG_DEBUG, "Sending version packet.");
331 	assert(strlen(GGATE_MAGIC) == sizeof(ver.gv_magic));
332 	bcopy(GGATE_MAGIC, ver.gv_magic, sizeof(ver.gv_magic));
333 	ver.gv_version = GGATE_VERSION;
334 	ver.gv_error = 0;
335 	g_gate_swap2n_version(&ver);
336 	if (g_gate_send(sfd, &ver, sizeof(ver), MSG_NOSIGNAL) == -1) {
337 		g_gate_log(LOG_DEBUG, "Error while sending version packet: %s.",
338 		    strerror(errno));
339 		close(sfd);
340 		return (-1);
341 	}
342 	bzero(&ver, sizeof(ver));
343 	if (g_gate_recv(sfd, &ver, sizeof(ver), MSG_WAITALL) == -1) {
344 		g_gate_log(LOG_DEBUG, "Error while receiving data: %s.",
345 		    strerror(errno));
346 		close(sfd);
347 		return (-1);
348 	}
349 	if (ver.gv_error != 0) {
350 		g_gate_log(LOG_DEBUG, "Version verification problem: %s.",
351 		    strerror(errno));
352 		close(sfd);
353 		return (-1);
354 	}
355 
356 	/*
357 	 * Create and send initial packet.
358 	 */
359 	g_gate_log(LOG_DEBUG, "Sending initial packet.");
360 	if (strlcpy(cinit.gc_path, path, sizeof(cinit.gc_path)) >=
361 	    sizeof(cinit.gc_path)) {
362 		g_gate_log(LOG_DEBUG, "Path name too long.");
363 		close(sfd);
364 		return (-1);
365 	}
366 	cinit.gc_flags = flags | dir;
367 	cinit.gc_token = token;
368 	cinit.gc_nconn = 2;
369 	g_gate_swap2n_cinit(&cinit);
370 	if (g_gate_send(sfd, &cinit, sizeof(cinit), MSG_NOSIGNAL) == -1) {
371 	        g_gate_log(LOG_DEBUG, "Error while sending initial packet: %s.",
372 		    strerror(errno));
373 		close(sfd);
374 		return (-1);
375 	}
376 	g_gate_swap2h_cinit(&cinit);
377 
378 	/*
379 	 * Receiving initial packet from server.
380 	 */
381 	g_gate_log(LOG_DEBUG, "Receiving initial packet.");
382 	if (g_gate_recv(sfd, &sinit, sizeof(sinit), MSG_WAITALL) == -1) {
383 		g_gate_log(LOG_DEBUG, "Error while receiving data: %s.",
384 		    strerror(errno));
385 		close(sfd);
386 		return (-1);
387 	}
388 	g_gate_swap2h_sinit(&sinit);
389 	if (sinit.gs_error != 0) {
390 	        g_gate_log(LOG_DEBUG, "Error from server: %s.",
391 		    strerror(sinit.gs_error));
392 		close(sfd);
393 		return (-1);
394 	}
395 	g_gate_log(LOG_DEBUG, "Received initial packet.");
396 
397 	mediasize = sinit.gs_mediasize;
398 	if (sectorsize == 0)
399 		sectorsize = sinit.gs_sectorsize;
400 
401 	return (sfd);
402 }
403 
404 static void
405 mydaemon(void)
406 {
407 
408 	if (g_gate_verbose > 0)
409 		return;
410 	if (daemon(0, 0) == 0)
411 		return;
412 	if (action == CREATE)
413 		g_gate_destroy(unit, 1);
414 	err(EXIT_FAILURE, "Cannot daemonize");
415 }
416 
417 static int
418 g_gatec_connect(void)
419 {
420 
421 	token = arc4random();
422 	/*
423 	 * Our receive descriptor is connected to the send descriptor on the
424 	 * server side.
425 	 */
426 	recvfd = handshake(GGATE_FLAG_SEND);
427 	if (recvfd == -1)
428 		return (0);
429 	/*
430 	 * Our send descriptor is connected to the receive descriptor on the
431 	 * server side.
432 	 */
433 	sendfd = handshake(GGATE_FLAG_RECV);
434 	if (sendfd == -1)
435 		return (0);
436 	return (1);
437 }
438 
439 static void
440 g_gatec_start(void)
441 {
442 	int error;
443 
444 	reconnect = 0;
445 	error = pthread_create(&recvtd, NULL, recv_thread, NULL);
446 	if (error != 0) {
447 		g_gate_destroy(unit, 1);
448 		g_gate_xlog("pthread_create(recv_thread): %s.",
449 		    strerror(error));
450 	}
451 	sendtd = pthread_self();
452 	send_thread(NULL);
453 	/* Disconnected. */
454 	close(sendfd);
455 	close(recvfd);
456 }
457 
458 static void
459 signop(int sig __unused)
460 {
461 
462 	/* Do nothing. */
463 }
464 
465 static void
466 g_gatec_loop(void)
467 {
468 	struct g_gate_ctl_cancel ggioc;
469 
470 	signal(SIGUSR1, signop);
471 	for (;;) {
472 		g_gatec_start();
473 		g_gate_log(LOG_NOTICE, "Disconnected [%s %s]. Connecting...",
474 		    host, path);
475 		while (!g_gatec_connect()) {
476 			sleep(2);
477 			g_gate_log(LOG_NOTICE, "Connecting [%s %s]...", host,
478 			    path);
479 		}
480 		ggioc.gctl_version = G_GATE_VERSION;
481 		ggioc.gctl_unit = unit;
482 		ggioc.gctl_seq = 0;
483 		g_gate_ioctl(G_GATE_CMD_CANCEL, &ggioc);
484 	}
485 }
486 
487 static void
488 g_gatec_create(void)
489 {
490 	struct g_gate_ctl_create ggioc;
491 
492 	if (!g_gatec_connect())
493 		g_gate_xlog("Cannot connect: %s.", strerror(errno));
494 
495 	/*
496 	 * Ok, got both sockets, time to create provider.
497 	 */
498 	memset(&ggioc, 0, sizeof(ggioc));
499 	ggioc.gctl_version = G_GATE_VERSION;
500 	ggioc.gctl_mediasize = mediasize;
501 	ggioc.gctl_sectorsize = sectorsize;
502 	ggioc.gctl_flags = flags;
503 	ggioc.gctl_maxcount = queue_size;
504 	ggioc.gctl_timeout = timeout;
505 	ggioc.gctl_unit = unit;
506 	snprintf(ggioc.gctl_info, sizeof(ggioc.gctl_info), "%s:%u %s", host,
507 	    port, path);
508 	g_gate_ioctl(G_GATE_CMD_CREATE, &ggioc);
509 	if (unit == -1) {
510 		printf("%s%u\n", G_GATE_PROVIDER_NAME, ggioc.gctl_unit);
511 		fflush(stdout);
512 	}
513 	unit = ggioc.gctl_unit;
514 
515 	mydaemon();
516 	g_gatec_loop();
517 }
518 
519 static void
520 g_gatec_rescue(void)
521 {
522 	struct g_gate_ctl_cancel ggioc;
523 
524 	if (!g_gatec_connect())
525 		g_gate_xlog("Cannot connect: %s.", strerror(errno));
526 
527 	ggioc.gctl_version = G_GATE_VERSION;
528 	ggioc.gctl_unit = unit;
529 	ggioc.gctl_seq = 0;
530 	g_gate_ioctl(G_GATE_CMD_CANCEL, &ggioc);
531 
532 	mydaemon();
533 	g_gatec_loop();
534 }
535 
536 static void
537 init_initial_buffer_size(void)
538 {
539 	int value;
540 	size_t intsize;
541 	intsize = sizeof(initialbuffersize);
542 	if (sysctlbyname("kern.maxphys", &value, &intsize, NULL, 0) == 0)
543 		initialbuffersize = value;
544 }
545 
546 int
547 main(int argc, char *argv[])
548 {
549 
550 	if (argc < 2)
551 		usage();
552 	if (strcasecmp(argv[1], "create") == 0)
553 		action = CREATE;
554 	else if (strcasecmp(argv[1], "destroy") == 0)
555 		action = DESTROY;
556 	else if (strcasecmp(argv[1], "list") == 0)
557 		action = LIST;
558 	else if (strcasecmp(argv[1], "rescue") == 0)
559 		action = RESCUE;
560 	else
561 		usage();
562 	argc -= 1;
563 	argv += 1;
564 	for (;;) {
565 		int ch;
566 
567 		ch = getopt(argc, argv, "fno:p:q:R:S:s:t:u:v");
568 		if (ch == -1)
569 			break;
570 		switch (ch) {
571 		case 'f':
572 			if (action != DESTROY)
573 				usage();
574 			force = 1;
575 			break;
576 		case 'n':
577 			if (action != CREATE && action != RESCUE)
578 				usage();
579 			nagle = 0;
580 			break;
581 		case 'o':
582 			if (action != CREATE && action != RESCUE)
583 				usage();
584 			if (strcasecmp("ro", optarg) == 0)
585 				flags = G_GATE_FLAG_READONLY;
586 			else if (strcasecmp("wo", optarg) == 0)
587 				flags = G_GATE_FLAG_WRITEONLY;
588 			else if (strcasecmp("rw", optarg) == 0)
589 				flags = 0;
590 			else {
591 				errx(EXIT_FAILURE,
592 				    "Invalid argument for '-o' option.");
593 			}
594 			break;
595 		case 'p':
596 			if (action != CREATE && action != RESCUE)
597 				usage();
598 			errno = 0;
599 			port = strtoul(optarg, NULL, 10);
600 			if (port == 0 && errno != 0)
601 				errx(EXIT_FAILURE, "Invalid port.");
602 			break;
603 		case 'q':
604 			if (action != CREATE)
605 				usage();
606 			errno = 0;
607 			queue_size = strtoul(optarg, NULL, 10);
608 			if (queue_size == 0 && errno != 0)
609 				errx(EXIT_FAILURE, "Invalid queue_size.");
610 			break;
611 		case 'R':
612 			if (action != CREATE && action != RESCUE)
613 				usage();
614 			errno = 0;
615 			rcvbuf = strtoul(optarg, NULL, 10);
616 			if (rcvbuf == 0 && errno != 0)
617 				errx(EXIT_FAILURE, "Invalid rcvbuf.");
618 			break;
619 		case 'S':
620 			if (action != CREATE && action != RESCUE)
621 				usage();
622 			errno = 0;
623 			sndbuf = strtoul(optarg, NULL, 10);
624 			if (sndbuf == 0 && errno != 0)
625 				errx(EXIT_FAILURE, "Invalid sndbuf.");
626 			break;
627 		case 's':
628 			if (action != CREATE)
629 				usage();
630 			errno = 0;
631 			sectorsize = strtoul(optarg, NULL, 10);
632 			if (sectorsize == 0 && errno != 0)
633 				errx(EXIT_FAILURE, "Invalid sectorsize.");
634 			break;
635 		case 't':
636 			if (action != CREATE)
637 				usage();
638 			errno = 0;
639 			timeout = strtoul(optarg, NULL, 10);
640 			if (timeout == 0 && errno != 0)
641 				errx(EXIT_FAILURE, "Invalid timeout.");
642 			break;
643 		case 'u':
644 			errno = 0;
645 			unit = strtol(optarg, NULL, 10);
646 			if (unit == 0 && errno != 0)
647 				errx(EXIT_FAILURE, "Invalid unit number.");
648 			break;
649 		case 'v':
650 			if (action == DESTROY)
651 				usage();
652 			g_gate_verbose++;
653 			break;
654 		default:
655 			usage();
656 		}
657 	}
658 	argc -= optind;
659 	argv += optind;
660 
661 	init_initial_buffer_size();
662 
663 	switch (action) {
664 	case CREATE:
665 		if (argc != 2)
666 			usage();
667 		g_gate_load_module();
668 		g_gate_open_device();
669 		host = argv[0];
670 		path = argv[1];
671 		g_gatec_create();
672 		break;
673 	case DESTROY:
674 		if (unit == -1) {
675 			fprintf(stderr, "Required unit number.\n");
676 			usage();
677 		}
678 		g_gate_verbose = 1;
679 		g_gate_open_device();
680 		g_gate_destroy(unit, force);
681 		break;
682 	case LIST:
683 		g_gate_list(unit, g_gate_verbose);
684 		break;
685 	case RESCUE:
686 		if (argc != 2)
687 			usage();
688 		if (unit == -1) {
689 			fprintf(stderr, "Required unit number.\n");
690 			usage();
691 		}
692 		g_gate_open_device();
693 		host = argv[0];
694 		path = argv[1];
695 		g_gatec_rescue();
696 		break;
697 	case UNSET:
698 	default:
699 		usage();
700 	}
701 	g_gate_close_device();
702 	exit(EXIT_SUCCESS);
703 }
704