2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2004 Pawel Jakub Dawidek <pjd@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
45 #include <sys/param.h>
46 #include <sys/ioctl.h>
47 #include <sys/socket.h>
48 #include <sys/sysctl.h>
49 #include <sys/syslog.h>
52 #include <netinet/in.h>
53 #include <netinet/tcp.h>
54 #include <arpa/inet.h>
56 #include <geom/gate/g_gate.h>
60 static enum { UNSET, CREATE, DESTROY, LIST, RESCUE } action = UNSET;
62 static const char *path = NULL;
63 static const char *host = NULL;
64 static int unit = G_GATE_UNIT_AUTO;
65 static unsigned flags = 0;
67 static unsigned queue_size = G_GATE_QUEUE_SIZE;
68 static unsigned port = G_GATE_PORT;
69 static off_t mediasize;
70 static unsigned sectorsize = 0;
71 static unsigned timeout = G_GATE_TIMEOUT;
72 static int sendfd, recvfd;
73 static uint32_t token;
74 static pthread_t sendtd, recvtd;
81 fprintf(stderr, "usage: %s create [-nv] [-o <ro|wo|rw>] [-p port] "
82 "[-q queue_size] [-R rcvbuf] [-S sndbuf] [-s sectorsize] "
83 "[-t timeout] [-u unit] <host> <path>\n", getprogname());
84 fprintf(stderr, " %s rescue [-nv] [-o <ro|wo|rw>] [-p port] "
85 "[-R rcvbuf] [-S sndbuf] <-u unit> <host> <path>\n", getprogname());
86 fprintf(stderr, " %s destroy [-f] <-u unit>\n", getprogname());
87 fprintf(stderr, " %s list [-v] [-u unit]\n", getprogname());
92 send_thread(void *arg __unused)
94 struct g_gate_ctl_io ggio;
95 struct g_gate_hdr hdr;
100 g_gate_log(LOG_NOTICE, "%s: started!", __func__);
102 ggio.gctl_version = G_GATE_VERSION;
103 ggio.gctl_unit = unit;
104 ggio.gctl_data = buf;
107 ggio.gctl_length = sizeof(buf);
109 g_gate_ioctl(G_GATE_CMD_START, &ggio);
110 error = ggio.gctl_error;
117 /* Exit gracefully. */
118 g_gate_close_device();
122 /* Buffer too small. */
123 ggio.gctl_data = realloc(ggio.gctl_data,
125 if (ggio.gctl_data != NULL) {
126 bsize = ggio.gctl_length;
133 g_gate_xlog("ioctl(/dev/%s): %s.", G_GATE_CTL_NAME,
140 switch (ggio.gctl_cmd) {
142 hdr.gh_cmd = GGATE_CMD_READ;
145 hdr.gh_cmd = GGATE_CMD_WRITE;
148 hdr.gh_seq = ggio.gctl_seq;
149 hdr.gh_offset = ggio.gctl_offset;
150 hdr.gh_length = ggio.gctl_length;
152 g_gate_swap2n_hdr(&hdr);
154 data = g_gate_send(sendfd, &hdr, sizeof(hdr), MSG_NOSIGNAL);
155 g_gate_log(LOG_DEBUG, "Sent hdr packet.");
156 g_gate_swap2h_hdr(&hdr);
159 if (data != sizeof(hdr)) {
160 g_gate_log(LOG_ERR, "Lost connection 1.");
162 pthread_kill(recvtd, SIGUSR1);
166 if (hdr.gh_cmd == GGATE_CMD_WRITE) {
167 data = g_gate_send(sendfd, ggio.gctl_data,
168 ggio.gctl_length, MSG_NOSIGNAL);
171 if (data != ggio.gctl_length) {
172 g_gate_log(LOG_ERR, "Lost connection 2 (%zd != %zd).", data, (ssize_t)ggio.gctl_length);
174 pthread_kill(recvtd, SIGUSR1);
177 g_gate_log(LOG_DEBUG, "Sent %zd bytes (offset=%llu, "
178 "size=%u).", data, hdr.gh_offset, hdr.gh_length);
181 g_gate_log(LOG_DEBUG, "%s: Died.", __func__);
186 recv_thread(void *arg __unused)
188 struct g_gate_ctl_io ggio;
189 struct g_gate_hdr hdr;
193 g_gate_log(LOG_NOTICE, "%s: started!", __func__);
195 ggio.gctl_version = G_GATE_VERSION;
196 ggio.gctl_unit = unit;
197 ggio.gctl_data = buf;
200 data = g_gate_recv(recvfd, &hdr, sizeof(hdr), MSG_WAITALL);
203 g_gate_swap2h_hdr(&hdr);
204 if (data != sizeof(hdr)) {
205 if (data == -1 && errno == EAGAIN)
207 g_gate_log(LOG_ERR, "Lost connection 3.");
209 pthread_kill(sendtd, SIGUSR1);
212 g_gate_log(LOG_DEBUG, "Received hdr packet.");
214 ggio.gctl_seq = hdr.gh_seq;
215 ggio.gctl_cmd = hdr.gh_cmd;
216 ggio.gctl_offset = hdr.gh_offset;
217 ggio.gctl_length = hdr.gh_length;
218 ggio.gctl_error = hdr.gh_error;
220 if (ggio.gctl_error == 0 && ggio.gctl_cmd == GGATE_CMD_READ) {
221 data = g_gate_recv(recvfd, ggio.gctl_data,
222 ggio.gctl_length, MSG_WAITALL);
225 g_gate_log(LOG_DEBUG, "Received data packet.");
226 if (data != ggio.gctl_length) {
227 g_gate_log(LOG_ERR, "Lost connection 4.");
229 pthread_kill(sendtd, SIGUSR1);
232 g_gate_log(LOG_DEBUG, "Received %d bytes (offset=%ju, "
233 "size=%zu).", data, (uintmax_t)hdr.gh_offset,
234 (size_t)hdr.gh_length);
237 g_gate_ioctl(G_GATE_CMD_DONE, &ggio);
239 g_gate_log(LOG_DEBUG, "%s: Died.", __func__);
246 struct g_gate_version ver;
247 struct g_gate_cinit cinit;
248 struct g_gate_sinit sinit;
249 struct sockaddr_in serv;
253 * Do the network stuff.
255 bzero(&serv, sizeof(serv));
256 serv.sin_family = AF_INET;
257 serv.sin_addr.s_addr = g_gate_str2ip(host);
258 if (serv.sin_addr.s_addr == INADDR_NONE) {
259 g_gate_log(LOG_DEBUG, "Invalid IP/host name: %s.", host);
262 serv.sin_port = htons(port);
263 sfd = socket(AF_INET, SOCK_STREAM, 0);
265 g_gate_log(LOG_DEBUG, "Cannot open socket: %s.",
270 g_gate_socket_settings(sfd);
272 if (connect(sfd, (struct sockaddr *)&serv, sizeof(serv)) == -1) {
273 g_gate_log(LOG_DEBUG, "Cannot connect to server: %s.",
279 g_gate_log(LOG_INFO, "Connected to the server: %s:%d.", host, port);
282 * Create and send version packet.
284 g_gate_log(LOG_DEBUG, "Sending version packet.");
285 assert(strlen(GGATE_MAGIC) == sizeof(ver.gv_magic));
286 bcopy(GGATE_MAGIC, ver.gv_magic, sizeof(ver.gv_magic));
287 ver.gv_version = GGATE_VERSION;
289 g_gate_swap2n_version(&ver);
290 if (g_gate_send(sfd, &ver, sizeof(ver), MSG_NOSIGNAL) == -1) {
291 g_gate_log(LOG_DEBUG, "Error while sending version packet: %s.",
296 bzero(&ver, sizeof(ver));
297 if (g_gate_recv(sfd, &ver, sizeof(ver), MSG_WAITALL) == -1) {
298 g_gate_log(LOG_DEBUG, "Error while receiving data: %s.",
303 if (ver.gv_error != 0) {
304 g_gate_log(LOG_DEBUG, "Version verification problem: %s.",
311 * Create and send initial packet.
313 g_gate_log(LOG_DEBUG, "Sending initial packet.");
314 if (strlcpy(cinit.gc_path, path, sizeof(cinit.gc_path)) >=
315 sizeof(cinit.gc_path)) {
316 g_gate_log(LOG_DEBUG, "Path name too long.");
320 cinit.gc_flags = flags | dir;
321 cinit.gc_token = token;
323 g_gate_swap2n_cinit(&cinit);
324 if (g_gate_send(sfd, &cinit, sizeof(cinit), MSG_NOSIGNAL) == -1) {
325 g_gate_log(LOG_DEBUG, "Error while sending initial packet: %s.",
330 g_gate_swap2h_cinit(&cinit);
333 * Receiving initial packet from server.
335 g_gate_log(LOG_DEBUG, "Receiving initial packet.");
336 if (g_gate_recv(sfd, &sinit, sizeof(sinit), MSG_WAITALL) == -1) {
337 g_gate_log(LOG_DEBUG, "Error while receiving data: %s.",
342 g_gate_swap2h_sinit(&sinit);
343 if (sinit.gs_error != 0) {
344 g_gate_log(LOG_DEBUG, "Error from server: %s.",
345 strerror(sinit.gs_error));
349 g_gate_log(LOG_DEBUG, "Received initial packet.");
351 mediasize = sinit.gs_mediasize;
353 sectorsize = sinit.gs_sectorsize;
362 if (g_gate_verbose > 0)
364 if (daemon(0, 0) == 0)
366 if (action == CREATE)
367 g_gate_destroy(unit, 1);
368 err(EXIT_FAILURE, "Cannot daemonize");
372 g_gatec_connect(void)
375 token = arc4random();
377 * Our receive descriptor is connected to the send descriptor on the
380 recvfd = handshake(GGATE_FLAG_SEND);
384 * Our send descriptor is connected to the receive descriptor on the
387 sendfd = handshake(GGATE_FLAG_RECV);
399 error = pthread_create(&recvtd, NULL, recv_thread, NULL);
401 g_gate_destroy(unit, 1);
402 g_gate_xlog("pthread_create(recv_thread): %s.",
405 sendtd = pthread_self();
413 signop(int sig __unused)
422 struct g_gate_ctl_cancel ggioc;
424 signal(SIGUSR1, signop);
427 g_gate_log(LOG_NOTICE, "Disconnected [%s %s]. Connecting...",
429 while (!g_gatec_connect()) {
431 g_gate_log(LOG_NOTICE, "Connecting [%s %s]...", host,
434 ggioc.gctl_version = G_GATE_VERSION;
435 ggioc.gctl_unit = unit;
437 g_gate_ioctl(G_GATE_CMD_CANCEL, &ggioc);
444 struct g_gate_ctl_create ggioc;
446 if (!g_gatec_connect())
447 g_gate_xlog("Cannot connect: %s.", strerror(errno));
450 * Ok, got both sockets, time to create provider.
452 memset(&ggioc, 0, sizeof(ggioc));
453 ggioc.gctl_version = G_GATE_VERSION;
454 ggioc.gctl_mediasize = mediasize;
455 ggioc.gctl_sectorsize = sectorsize;
456 ggioc.gctl_flags = flags;
457 ggioc.gctl_maxcount = queue_size;
458 ggioc.gctl_timeout = timeout;
459 ggioc.gctl_unit = unit;
460 snprintf(ggioc.gctl_info, sizeof(ggioc.gctl_info), "%s:%u %s", host,
462 g_gate_ioctl(G_GATE_CMD_CREATE, &ggioc);
464 printf("%s%u\n", G_GATE_PROVIDER_NAME, ggioc.gctl_unit);
467 unit = ggioc.gctl_unit;
476 struct g_gate_ctl_cancel ggioc;
478 if (!g_gatec_connect())
479 g_gate_xlog("Cannot connect: %s.", strerror(errno));
481 ggioc.gctl_version = G_GATE_VERSION;
482 ggioc.gctl_unit = unit;
484 g_gate_ioctl(G_GATE_CMD_CANCEL, &ggioc);
491 main(int argc, char *argv[])
496 if (strcasecmp(argv[1], "create") == 0)
498 else if (strcasecmp(argv[1], "destroy") == 0)
500 else if (strcasecmp(argv[1], "list") == 0)
502 else if (strcasecmp(argv[1], "rescue") == 0)
511 ch = getopt(argc, argv, "fno:p:q:R:S:s:t:u:v");
516 if (action != DESTROY)
521 if (action != CREATE && action != RESCUE)
526 if (action != CREATE && action != RESCUE)
528 if (strcasecmp("ro", optarg) == 0)
529 flags = G_GATE_FLAG_READONLY;
530 else if (strcasecmp("wo", optarg) == 0)
531 flags = G_GATE_FLAG_WRITEONLY;
532 else if (strcasecmp("rw", optarg) == 0)
536 "Invalid argument for '-o' option.");
540 if (action != CREATE && action != RESCUE)
543 port = strtoul(optarg, NULL, 10);
544 if (port == 0 && errno != 0)
545 errx(EXIT_FAILURE, "Invalid port.");
548 if (action != CREATE)
551 queue_size = strtoul(optarg, NULL, 10);
552 if (queue_size == 0 && errno != 0)
553 errx(EXIT_FAILURE, "Invalid queue_size.");
556 if (action != CREATE && action != RESCUE)
559 rcvbuf = strtoul(optarg, NULL, 10);
560 if (rcvbuf == 0 && errno != 0)
561 errx(EXIT_FAILURE, "Invalid rcvbuf.");
564 if (action != CREATE && action != RESCUE)
567 sndbuf = strtoul(optarg, NULL, 10);
568 if (sndbuf == 0 && errno != 0)
569 errx(EXIT_FAILURE, "Invalid sndbuf.");
572 if (action != CREATE)
575 sectorsize = strtoul(optarg, NULL, 10);
576 if (sectorsize == 0 && errno != 0)
577 errx(EXIT_FAILURE, "Invalid sectorsize.");
580 if (action != CREATE)
583 timeout = strtoul(optarg, NULL, 10);
584 if (timeout == 0 && errno != 0)
585 errx(EXIT_FAILURE, "Invalid timeout.");
589 unit = strtol(optarg, NULL, 10);
590 if (unit == 0 && errno != 0)
591 errx(EXIT_FAILURE, "Invalid unit number.");
594 if (action == DESTROY)
609 g_gate_load_module();
610 g_gate_open_device();
617 fprintf(stderr, "Required unit number.\n");
621 g_gate_open_device();
622 g_gate_destroy(unit, force);
625 g_gate_list(unit, g_gate_verbose);
631 fprintf(stderr, "Required unit number.\n");
634 g_gate_open_device();
643 g_gate_close_device();