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
29 #include <sys/param.h>
32 #include <sys/endian.h>
33 #include <sys/ioctl.h>
34 #include <sys/queue.h>
35 #include <sys/socket.h>
38 #include <arpa/inet.h>
39 #include <netinet/in.h>
40 #include <netinet/tcp.h>
61 #define GGATED_EXPORT_FILE "/etc/gg.exports"
63 struct ggd_connection {
65 unsigned c_sectorsize;
66 unsigned c_flags; /* flags (RO/RW) */
74 LIST_ENTRY(ggd_connection) c_next;
78 struct g_gate_hdr r_hdr;
80 TAILQ_ENTRY(ggd_request) r_next;
82 #define r_cmd r_hdr.gh_cmd
83 #define r_offset r_hdr.gh_offset
84 #define r_length r_hdr.gh_length
85 #define r_error r_hdr.gh_error
88 char *e_path; /* path to device/file */
89 in_addr_t e_ip; /* remote IP address */
90 in_addr_t e_mask; /* IP mask */
91 unsigned e_flags; /* flags (RO/RW) */
92 SLIST_ENTRY(ggd_export) e_next;
95 static const char *exports_file = GGATED_EXPORT_FILE;
96 static int got_sighup = 0;
97 static in_addr_t bindaddr;
99 static TAILQ_HEAD(, ggd_request) inqueue = TAILQ_HEAD_INITIALIZER(inqueue);
100 static TAILQ_HEAD(, ggd_request) outqueue = TAILQ_HEAD_INITIALIZER(outqueue);
101 static pthread_mutex_t inqueue_mtx, outqueue_mtx;
102 static pthread_cond_t inqueue_cond, outqueue_cond;
104 static SLIST_HEAD(, ggd_export) exports = SLIST_HEAD_INITIALIZER(exports);
105 static LIST_HEAD(, ggd_connection) connections = LIST_HEAD_INITIALIZER(connections);
107 static void *recv_thread(void *arg);
108 static void *disk_thread(void *arg);
109 static void *send_thread(void *arg);
115 fprintf(stderr, "usage: %s [-nv] [-a address] [-F pidfile] [-p port] "
116 "[-R rcvbuf] [-S sndbuf] [exports file]\n", getprogname());
125 snprintf(sip, sizeof(sip), "%u.%u.%u.%u",
134 countmask(unsigned m)
141 mask = 1 << (32 - m);
149 line_parse(char *line, unsigned lineno)
151 struct ggd_export *ex;
152 char *word, *path, *sflags;
153 unsigned flags, i, vmask;
156 ip = mask = flags = vmask = 0;
160 for (i = 0, word = strtok(line, " \t"); word != NULL;
161 i++, word = strtok(NULL, " \t")) {
163 case 0: /* IP address or host name */
164 ip = g_gate_str2ip(strsep(&word, "/"));
165 if (ip == INADDR_NONE) {
166 g_gate_xlog("Invalid IP/host name at line %u.",
174 vmask = strtoul(word, NULL, 10);
175 if (vmask == 0 && errno != 0) {
176 g_gate_xlog("Invalid IP mask value at "
179 if ((unsigned)vmask > 32) {
180 g_gate_xlog("Invalid IP mask value at line %u.",
184 mask = countmask(vmask);
187 if (strcasecmp("rd", word) == 0 ||
188 strcasecmp("ro", word) == 0) {
190 } else if (strcasecmp("wo", word) == 0) {
192 } else if (strcasecmp("rw", word) == 0) {
195 g_gate_xlog("Invalid value in flags field at "
201 if (strlen(word) >= MAXPATHLEN) {
202 g_gate_xlog("Path too long at line %u. ",
208 g_gate_xlog("Too many arguments at line %u. ", lineno);
212 g_gate_xlog("Too few arguments at line %u.", lineno);
214 ex = malloc(sizeof(*ex));
216 g_gate_xlog("Not enough memory.");
217 ex->e_path = strdup(path);
218 if (ex->e_path == NULL)
219 g_gate_xlog("Not enough memory.");
221 /* Made 'and' here. */
222 ex->e_ip = (ip & mask);
226 SLIST_INSERT_HEAD(&exports, ex, e_next);
228 g_gate_log(LOG_DEBUG, "Added %s/%u %s %s to exports list.",
229 ip2str(ex->e_ip), vmask, path, sflags);
235 struct ggd_export *ex;
237 while (!SLIST_EMPTY(&exports)) {
238 ex = SLIST_FIRST(&exports);
239 SLIST_REMOVE_HEAD(&exports, e_next);
244 #define EXPORTS_LINE_SIZE 2048
248 char buf[EXPORTS_LINE_SIZE], *line;
249 unsigned lineno = 0, objs = 0, len;
254 fd = fopen(exports_file, "r");
256 g_gate_xlog("Cannot open exports file (%s): %s.", exports_file,
260 g_gate_log(LOG_INFO, "Reading exports file (%s).", exports_file);
263 if (fgets(buf, sizeof(buf), fd) == NULL) {
267 g_gate_xlog("Error while reading exports file: %s.",
271 /* Increase line count. */
274 /* Skip spaces and tabs. */
275 for (line = buf; *line == ' ' || *line == '\t'; ++line)
278 /* Empty line, comment or empty line at the end of file. */
279 if (*line == '\n' || *line == '#' || *line == '\0')
283 if (line[len - 1] == '\n') {
284 /* Remove new line char. */
285 line[len - 1] = '\0';
288 g_gate_xlog("Line %u too long.", lineno);
291 line_parse(line, lineno);
298 g_gate_xlog("There are no objects to export.");
300 g_gate_log(LOG_INFO, "Exporting %u object(s).", objs);
304 exports_check(struct ggd_export *ex, struct g_gate_cinit *cinit,
305 struct ggd_connection *conn)
307 char ipmask[32]; /* 32 == strlen("xxx.xxx.xxx.xxx/xxx.xxx.xxx.xxx")+1 */
308 int error = 0, flags;
310 strlcpy(ipmask, ip2str(ex->e_ip), sizeof(ipmask));
311 strlcat(ipmask, "/", sizeof(ipmask));
312 strlcat(ipmask, ip2str(ex->e_mask), sizeof(ipmask));
313 if ((cinit->gc_flags & GGATE_FLAG_RDONLY) != 0) {
314 if (ex->e_flags == O_WRONLY) {
315 g_gate_log(LOG_WARNING, "Read-only access requested, "
316 "but %s (%s) is exported write-only.", ex->e_path,
320 conn->c_flags |= GGATE_FLAG_RDONLY;
322 } else if ((cinit->gc_flags & GGATE_FLAG_WRONLY) != 0) {
323 if (ex->e_flags == O_RDONLY) {
324 g_gate_log(LOG_WARNING, "Write-only access requested, "
325 "but %s (%s) is exported read-only.", ex->e_path,
329 conn->c_flags |= GGATE_FLAG_WRONLY;
332 if (ex->e_flags == O_RDONLY) {
333 g_gate_log(LOG_WARNING, "Read-write access requested, "
334 "but %s (%s) is exported read-only.", ex->e_path,
337 } else if (ex->e_flags == O_WRONLY) {
338 g_gate_log(LOG_WARNING, "Read-write access requested, "
339 "but %s (%s) is exported write-only.", ex->e_path,
344 if ((conn->c_flags & GGATE_FLAG_RDONLY) != 0)
346 else if ((conn->c_flags & GGATE_FLAG_WRONLY) != 0)
350 conn->c_diskfd = open(ex->e_path, flags);
351 if (conn->c_diskfd == -1) {
353 g_gate_log(LOG_ERR, "Cannot open %s: %s.", ex->e_path,
360 static struct ggd_export *
361 exports_find(struct sockaddr *s, struct g_gate_cinit *cinit,
362 struct ggd_connection *conn)
364 struct ggd_export *ex;
368 ip = htonl(((struct sockaddr_in *)(void *)s)->sin_addr.s_addr);
369 SLIST_FOREACH(ex, &exports, e_next) {
370 if ((ip & ex->e_mask) != ex->e_ip) {
371 g_gate_log(LOG_DEBUG, "exports[%s]: IP mismatch.",
375 if (strcmp(cinit->gc_path, ex->e_path) != 0) {
376 g_gate_log(LOG_DEBUG, "exports[%s]: Path mismatch.",
380 error = exports_check(ex, cinit, conn);
388 g_gate_log(LOG_WARNING, "Unauthorized connection from: %s.",
395 * Remove timed out connections.
398 connection_cleanups(void)
400 struct ggd_connection *conn, *tconn;
404 LIST_FOREACH_SAFE(conn, &connections, c_next, tconn) {
405 if (now - conn->c_birthtime > 10) {
406 LIST_REMOVE(conn, c_next);
407 g_gate_log(LOG_NOTICE,
408 "Connection from %s [%s] removed.",
409 ip2str(conn->c_srcip), conn->c_path);
410 close(conn->c_diskfd);
411 close(conn->c_sendfd);
412 close(conn->c_recvfd);
419 static struct ggd_connection *
420 connection_find(struct g_gate_cinit *cinit)
422 struct ggd_connection *conn;
424 LIST_FOREACH(conn, &connections, c_next) {
425 if (conn->c_token == cinit->gc_token)
431 static struct ggd_connection *
432 connection_new(struct g_gate_cinit *cinit, struct sockaddr *s, int sfd)
434 struct ggd_connection *conn;
438 * First, look for old connections.
439 * We probably should do it every X seconds, but what for?
440 * It is only dangerous if an attacker wants to overload connections
441 * queue, so here is a good place to do the cleanups.
443 connection_cleanups();
445 conn = malloc(sizeof(*conn));
448 conn->c_path = strdup(cinit->gc_path);
449 if (conn->c_path == NULL) {
453 conn->c_token = cinit->gc_token;
454 ip = htonl(((struct sockaddr_in *)(void *)s)->sin_addr.s_addr);
456 conn->c_sendfd = conn->c_recvfd = -1;
457 if ((cinit->gc_flags & GGATE_FLAG_SEND) != 0)
458 conn->c_sendfd = sfd;
460 conn->c_recvfd = sfd;
461 conn->c_mediasize = 0;
462 conn->c_sectorsize = 0;
463 time(&conn->c_birthtime);
464 conn->c_flags = cinit->gc_flags;
465 LIST_INSERT_HEAD(&connections, conn, c_next);
466 g_gate_log(LOG_DEBUG, "Connection created [%s, %s].", ip2str(ip),
472 connection_add(struct ggd_connection *conn, struct g_gate_cinit *cinit,
473 struct sockaddr *s, int sfd)
477 ip = htonl(((struct sockaddr_in *)(void *)s)->sin_addr.s_addr);
478 if ((cinit->gc_flags & GGATE_FLAG_SEND) != 0) {
479 if (conn->c_sendfd != -1) {
480 g_gate_log(LOG_WARNING,
481 "Send socket already exists [%s, %s].", ip2str(ip),
485 conn->c_sendfd = sfd;
487 if (conn->c_recvfd != -1) {
488 g_gate_log(LOG_WARNING,
489 "Receive socket already exists [%s, %s].",
490 ip2str(ip), conn->c_path);
493 conn->c_recvfd = sfd;
495 g_gate_log(LOG_DEBUG, "Connection added [%s, %s].", ip2str(ip),
501 * Remove one socket from the given connection or the whole
502 * connection if sfd == -1.
505 connection_remove(struct ggd_connection *conn)
508 LIST_REMOVE(conn, c_next);
509 g_gate_log(LOG_DEBUG, "Connection removed [%s %s].",
510 ip2str(conn->c_srcip), conn->c_path);
511 if (conn->c_sendfd != -1)
512 close(conn->c_sendfd);
513 if (conn->c_recvfd != -1)
514 close(conn->c_recvfd);
520 connection_ready(struct ggd_connection *conn)
523 return (conn->c_sendfd != -1 && conn->c_recvfd != -1);
527 connection_launch(struct ggd_connection *conn)
535 else if (pid == -1) {
536 g_gate_log(LOG_ERR, "Cannot fork: %s.", strerror(errno));
539 g_gate_log(LOG_DEBUG, "Process created [%s].", conn->c_path);
542 * Create condition variables and mutexes for in-queue and out-queue
545 error = pthread_mutex_init(&inqueue_mtx, NULL);
547 g_gate_xlog("pthread_mutex_init(inqueue_mtx): %s.",
550 error = pthread_cond_init(&inqueue_cond, NULL);
552 g_gate_xlog("pthread_cond_init(inqueue_cond): %s.",
555 error = pthread_mutex_init(&outqueue_mtx, NULL);
557 g_gate_xlog("pthread_mutex_init(outqueue_mtx): %s.",
560 error = pthread_cond_init(&outqueue_cond, NULL);
562 g_gate_xlog("pthread_cond_init(outqueue_cond): %s.",
568 * recvtd - thread for receiving I/O request
569 * diskio - thread for doing I/O request
570 * sendtd - thread for sending I/O requests back
572 error = pthread_create(&td, NULL, send_thread, conn);
574 g_gate_xlog("pthread_create(send_thread): %s.",
577 error = pthread_create(&td, NULL, recv_thread, conn);
579 g_gate_xlog("pthread_create(recv_thread): %s.",
586 sendfail(int sfd, int error, const char *fmt, ...)
588 struct g_gate_sinit sinit;
592 sinit.gs_error = error;
593 g_gate_swap2n_sinit(&sinit);
594 data = g_gate_send(sfd, &sinit, sizeof(sinit), 0);
595 g_gate_swap2h_sinit(&sinit);
596 if (data != sizeof(sinit)) {
597 g_gate_log(LOG_WARNING, "Cannot send initial packet: %s.",
603 g_gate_vlog(LOG_WARNING, fmt, ap);
609 malloc_waitok(size_t size)
613 while ((p = malloc(size)) == NULL) {
614 g_gate_log(LOG_DEBUG, "Cannot allocate %zu bytes.", size);
621 recv_thread(void *arg)
623 struct ggd_connection *conn;
624 struct ggd_request *req;
629 g_gate_log(LOG_NOTICE, "%s: started [%s]!", __func__, conn->c_path);
635 req = malloc_waitok(sizeof(*req));
636 data = g_gate_recv(fd, &req->r_hdr, sizeof(req->r_hdr),
639 g_gate_log(LOG_DEBUG, "Process %u exiting.", getpid());
641 } else if (data == -1) {
642 g_gate_xlog("Error while receiving hdr packet: %s.",
644 } else if (data != sizeof(req->r_hdr)) {
645 g_gate_xlog("Malformed hdr packet received.");
647 g_gate_log(LOG_DEBUG, "Received hdr packet.");
648 g_gate_swap2h_hdr(&req->r_hdr);
650 g_gate_log(LOG_DEBUG, "%s: offset=%jd length=%u", __func__,
651 (intmax_t)req->r_offset, (unsigned)req->r_length);
654 * Allocate memory for data.
656 req->r_data = malloc_waitok(req->r_length);
659 * Receive data to write for WRITE request.
661 if (req->r_cmd == GGATE_CMD_WRITE) {
662 g_gate_log(LOG_DEBUG, "Waiting for %u bytes of data...",
664 data = g_gate_recv(fd, req->r_data, req->r_length,
667 g_gate_xlog("Error while receiving data: %s.",
673 * Put the request onto the incoming queue.
675 error = pthread_mutex_lock(&inqueue_mtx);
677 TAILQ_INSERT_TAIL(&inqueue, req, r_next);
678 error = pthread_cond_signal(&inqueue_cond);
680 error = pthread_mutex_unlock(&inqueue_mtx);
686 disk_thread(void *arg)
688 struct ggd_connection *conn;
689 struct ggd_request *req;
694 g_gate_log(LOG_NOTICE, "%s: started [%s]!", __func__, conn->c_path);
698 * Get a request from the incoming queue.
700 error = pthread_mutex_lock(&inqueue_mtx);
702 while ((req = TAILQ_FIRST(&inqueue)) == NULL) {
703 error = pthread_cond_wait(&inqueue_cond, &inqueue_mtx);
706 TAILQ_REMOVE(&inqueue, req, r_next);
707 error = pthread_mutex_unlock(&inqueue_mtx);
713 assert(req->r_cmd == GGATE_CMD_READ || req->r_cmd == GGATE_CMD_WRITE);
714 assert(req->r_offset + req->r_length <= (uintmax_t)conn->c_mediasize);
715 assert((req->r_offset % conn->c_sectorsize) == 0);
716 assert((req->r_length % conn->c_sectorsize) == 0);
718 g_gate_log(LOG_DEBUG, "%s: offset=%jd length=%u", __func__,
719 (intmax_t)req->r_offset, (unsigned)req->r_length);
725 switch (req->r_cmd) {
727 data = pread(fd, req->r_data, req->r_length,
730 case GGATE_CMD_WRITE:
731 data = pwrite(fd, req->r_data, req->r_length,
733 /* Free data memory here - better sooner. */
738 if (data != (ssize_t)req->r_length) {
739 /* Report short reads/writes as I/O errors. */
742 g_gate_log(LOG_ERR, "Disk error: %s", strerror(errno));
743 req->r_error = errno;
744 if (req->r_data != NULL) {
751 * Put the request onto the outgoing queue.
753 error = pthread_mutex_lock(&outqueue_mtx);
755 TAILQ_INSERT_TAIL(&outqueue, req, r_next);
756 error = pthread_cond_signal(&outqueue_cond);
758 error = pthread_mutex_unlock(&outqueue_mtx);
767 send_thread(void *arg)
769 struct ggd_connection *conn;
770 struct ggd_request *req;
775 g_gate_log(LOG_NOTICE, "%s: started [%s]!", __func__, conn->c_path);
779 * Get a request from the outgoing queue.
781 error = pthread_mutex_lock(&outqueue_mtx);
783 while ((req = TAILQ_FIRST(&outqueue)) == NULL) {
784 error = pthread_cond_wait(&outqueue_cond,
788 TAILQ_REMOVE(&outqueue, req, r_next);
789 error = pthread_mutex_unlock(&outqueue_mtx);
792 g_gate_log(LOG_DEBUG, "%s: offset=%jd length=%u", __func__,
793 (intmax_t)req->r_offset, (unsigned)req->r_length);
798 g_gate_swap2n_hdr(&req->r_hdr);
799 if (g_gate_send(fd, &req->r_hdr, sizeof(req->r_hdr), 0) == -1) {
800 g_gate_xlog("Error while sending hdr packet: %s.",
803 g_gate_log(LOG_DEBUG, "Sent hdr packet.");
804 g_gate_swap2h_hdr(&req->r_hdr);
805 if (req->r_data != NULL) {
806 data = g_gate_send(fd, req->r_data, req->r_length, 0);
807 if (data != (ssize_t)req->r_length) {
808 g_gate_xlog("Error while sending data: %s.",
811 g_gate_log(LOG_DEBUG,
812 "Sent %zd bytes (offset=%ju, size=%zu).", data,
813 (uintmax_t)req->r_offset, (size_t)req->r_length);
824 log_connection(struct sockaddr *from)
828 ip = htonl(((struct sockaddr_in *)(void *)from)->sin_addr.s_addr);
829 g_gate_log(LOG_INFO, "Connection from: %s.", ip2str(ip));
833 handshake(struct sockaddr *from, int sfd)
835 struct g_gate_version ver;
836 struct g_gate_cinit cinit;
837 struct g_gate_sinit sinit;
838 struct ggd_connection *conn;
839 struct ggd_export *ex;
842 log_connection(from);
844 * Phase 1: Version verification.
846 g_gate_log(LOG_DEBUG, "Receiving version packet.");
847 data = g_gate_recv(sfd, &ver, sizeof(ver), MSG_WAITALL);
848 g_gate_swap2h_version(&ver);
849 if (data != sizeof(ver)) {
850 g_gate_log(LOG_WARNING, "Malformed version packet.");
853 g_gate_log(LOG_DEBUG, "Version packet received.");
854 if (memcmp(ver.gv_magic, GGATE_MAGIC, strlen(GGATE_MAGIC)) != 0) {
855 g_gate_log(LOG_WARNING, "Invalid magic field.");
858 if (ver.gv_version != GGATE_VERSION) {
859 g_gate_log(LOG_WARNING, "Version %u is not supported.",
864 g_gate_swap2n_version(&ver);
865 data = g_gate_send(sfd, &ver, sizeof(ver), 0);
866 g_gate_swap2h_version(&ver);
868 sendfail(sfd, errno, "Error while sending version packet: %s.",
874 * Phase 2: Request verification.
876 g_gate_log(LOG_DEBUG, "Receiving initial packet.");
877 data = g_gate_recv(sfd, &cinit, sizeof(cinit), MSG_WAITALL);
878 g_gate_swap2h_cinit(&cinit);
879 if (data != sizeof(cinit)) {
880 g_gate_log(LOG_WARNING, "Malformed initial packet.");
883 g_gate_log(LOG_DEBUG, "Initial packet received.");
884 conn = connection_find(&cinit);
887 * Connection should already exists.
889 g_gate_log(LOG_DEBUG, "Found existing connection (token=%lu).",
890 (unsigned long)conn->c_token);
891 if (connection_add(conn, &cinit, from, sfd) == -1) {
892 connection_remove(conn);
897 * New connection, allocate space.
899 conn = connection_new(&cinit, from, sfd);
901 sendfail(sfd, ENOMEM,
902 "Cannot allocate new connection.");
905 g_gate_log(LOG_DEBUG, "New connection created (token=%lu).",
906 (unsigned long)conn->c_token);
909 ex = exports_find(from, &cinit, conn);
911 sendfail(sfd, errno, NULL);
912 connection_remove(conn);
915 if (conn->c_mediasize == 0) {
916 conn->c_mediasize = g_gate_mediasize(conn->c_diskfd);
917 conn->c_sectorsize = g_gate_sectorsize(conn->c_diskfd);
919 sinit.gs_mediasize = conn->c_mediasize;
920 sinit.gs_sectorsize = conn->c_sectorsize;
923 g_gate_log(LOG_DEBUG, "Sending initial packet.");
925 g_gate_swap2n_sinit(&sinit);
926 data = g_gate_send(sfd, &sinit, sizeof(sinit), 0);
927 g_gate_swap2h_sinit(&sinit);
929 sendfail(sfd, errno, "Error while sending initial packet: %s.",
934 if (connection_ready(conn)) {
935 connection_launch(conn);
936 connection_remove(conn);
942 huphandler(int sig __unused)
949 main(int argc, char *argv[])
951 const char *ggated_pidfile = _PATH_VARRUN "/ggated.pid";
953 struct sockaddr_in serv;
954 struct sockaddr from;
960 bindaddr = htonl(INADDR_ANY);
962 while ((ch = getopt(argc, argv, "a:hnp:F:R:S:v")) != -1) {
965 bindaddr = g_gate_str2ip(optarg);
966 if (bindaddr == INADDR_NONE) {
968 "Invalid IP/host name to bind to.");
972 ggated_pidfile = optarg;
979 port = strtoul(optarg, NULL, 10);
980 if (port == 0 && errno != 0)
981 errx(EXIT_FAILURE, "Invalid port.");
985 rcvbuf = strtoul(optarg, NULL, 10);
986 if (rcvbuf == 0 && errno != 0)
987 errx(EXIT_FAILURE, "Invalid rcvbuf.");
991 sndbuf = strtoul(optarg, NULL, 10);
992 if (sndbuf == 0 && errno != 0)
993 errx(EXIT_FAILURE, "Invalid sndbuf.");
1006 if (argv[0] != NULL)
1007 exports_file = argv[0];
1010 pfh = pidfile_open(ggated_pidfile, 0600, &otherpid);
1012 if (errno == EEXIST) {
1013 errx(EXIT_FAILURE, "Daemon already running, pid: %jd.",
1014 (intmax_t)otherpid);
1016 err(EXIT_FAILURE, "Cannot open/create pidfile");
1019 if (!g_gate_verbose) {
1020 /* Run in daemon mode. */
1021 if (daemon(0, 0) == -1)
1022 g_gate_xlog("Cannot daemonize: %s", strerror(errno));
1027 signal(SIGCHLD, SIG_IGN);
1029 sfd = socket(AF_INET, SOCK_STREAM, 0);
1031 g_gate_xlog("Cannot open stream socket: %s.", strerror(errno));
1032 bzero(&serv, sizeof(serv));
1033 serv.sin_family = AF_INET;
1034 serv.sin_addr.s_addr = bindaddr;
1035 serv.sin_port = htons(port);
1037 g_gate_socket_settings(sfd);
1039 if (bind(sfd, (struct sockaddr *)&serv, sizeof(serv)) == -1)
1040 g_gate_xlog("bind(): %s.", strerror(errno));
1041 if (listen(sfd, 5) == -1)
1042 g_gate_xlog("listen(): %s.", strerror(errno));
1044 g_gate_log(LOG_INFO, "Listen on port: %d.", port);
1046 signal(SIGHUP, huphandler);
1049 fromlen = sizeof(from);
1050 tmpsfd = accept(sfd, &from, &fromlen);
1052 g_gate_xlog("accept(): %s.", strerror(errno));
1059 if (!handshake(&from, tmpsfd))
1063 pidfile_remove(pfh);