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
31 #include <sys/param.h>
34 #include <sys/endian.h>
35 #include <sys/ioctl.h>
36 #include <sys/queue.h>
37 #include <sys/socket.h>
40 #include <arpa/inet.h>
41 #include <netinet/in.h>
42 #include <netinet/tcp.h>
63 #define GGATED_EXPORT_FILE "/etc/gg.exports"
65 struct ggd_connection {
67 unsigned c_sectorsize;
68 unsigned c_flags; /* flags (RO/RW) */
76 LIST_ENTRY(ggd_connection) c_next;
80 struct g_gate_hdr r_hdr;
82 TAILQ_ENTRY(ggd_request) r_next;
84 #define r_cmd r_hdr.gh_cmd
85 #define r_offset r_hdr.gh_offset
86 #define r_length r_hdr.gh_length
87 #define r_error r_hdr.gh_error
90 char *e_path; /* path to device/file */
91 in_addr_t e_ip; /* remote IP address */
92 in_addr_t e_mask; /* IP mask */
93 unsigned e_flags; /* flags (RO/RW) */
94 SLIST_ENTRY(ggd_export) e_next;
97 static const char *exports_file = GGATED_EXPORT_FILE;
98 static int got_sighup = 0;
99 static in_addr_t bindaddr;
101 static TAILQ_HEAD(, ggd_request) inqueue = TAILQ_HEAD_INITIALIZER(inqueue);
102 static TAILQ_HEAD(, ggd_request) outqueue = TAILQ_HEAD_INITIALIZER(outqueue);
103 static pthread_mutex_t inqueue_mtx, outqueue_mtx;
104 static pthread_cond_t inqueue_cond, outqueue_cond;
106 static SLIST_HEAD(, ggd_export) exports = SLIST_HEAD_INITIALIZER(exports);
107 static LIST_HEAD(, ggd_connection) connections = LIST_HEAD_INITIALIZER(connections);
109 static void *recv_thread(void *arg);
110 static void *disk_thread(void *arg);
111 static void *send_thread(void *arg);
117 fprintf(stderr, "usage: %s [-nv] [-a address] [-F pidfile] [-p port] "
118 "[-R rcvbuf] [-S sndbuf] [exports file]\n", getprogname());
127 snprintf(sip, sizeof(sip), "%u.%u.%u.%u",
136 countmask(unsigned m)
143 mask = 1 << (32 - m);
151 line_parse(char *line, unsigned lineno)
153 struct ggd_export *ex;
154 char *word, *path, *sflags;
155 unsigned flags, i, vmask;
158 ip = mask = flags = vmask = 0;
162 for (i = 0, word = strtok(line, " \t"); word != NULL;
163 i++, word = strtok(NULL, " \t")) {
165 case 0: /* IP address or host name */
166 ip = g_gate_str2ip(strsep(&word, "/"));
167 if (ip == INADDR_NONE) {
168 g_gate_xlog("Invalid IP/host name at line %u.",
176 vmask = strtoul(word, NULL, 10);
177 if (vmask == 0 && errno != 0) {
178 g_gate_xlog("Invalid IP mask value at "
181 if ((unsigned)vmask > 32) {
182 g_gate_xlog("Invalid IP mask value at line %u.",
186 mask = countmask(vmask);
189 if (strcasecmp("rd", word) == 0 ||
190 strcasecmp("ro", word) == 0) {
192 } else if (strcasecmp("wo", word) == 0) {
194 } else if (strcasecmp("rw", word) == 0) {
197 g_gate_xlog("Invalid value in flags field at "
203 if (strlen(word) >= MAXPATHLEN) {
204 g_gate_xlog("Path too long at line %u. ",
210 g_gate_xlog("Too many arguments at line %u. ", lineno);
214 g_gate_xlog("Too few arguments at line %u.", lineno);
216 ex = malloc(sizeof(*ex));
218 g_gate_xlog("Not enough memory.");
219 ex->e_path = strdup(path);
220 if (ex->e_path == NULL)
221 g_gate_xlog("Not enough memory.");
223 /* Made 'and' here. */
224 ex->e_ip = (ip & mask);
228 SLIST_INSERT_HEAD(&exports, ex, e_next);
230 g_gate_log(LOG_DEBUG, "Added %s/%u %s %s to exports list.",
231 ip2str(ex->e_ip), vmask, path, sflags);
237 struct ggd_export *ex;
239 while (!SLIST_EMPTY(&exports)) {
240 ex = SLIST_FIRST(&exports);
241 SLIST_REMOVE_HEAD(&exports, e_next);
246 #define EXPORTS_LINE_SIZE 2048
250 char buf[EXPORTS_LINE_SIZE], *line;
251 unsigned lineno = 0, objs = 0, len;
256 fd = fopen(exports_file, "r");
258 g_gate_xlog("Cannot open exports file (%s): %s.", exports_file,
262 g_gate_log(LOG_INFO, "Reading exports file (%s).", exports_file);
265 if (fgets(buf, sizeof(buf), fd) == NULL) {
269 g_gate_xlog("Error while reading exports file: %s.",
273 /* Increase line count. */
276 /* Skip spaces and tabs. */
277 for (line = buf; *line == ' ' || *line == '\t'; ++line)
280 /* Empty line, comment or empty line at the end of file. */
281 if (*line == '\n' || *line == '#' || *line == '\0')
285 if (line[len - 1] == '\n') {
286 /* Remove new line char. */
287 line[len - 1] = '\0';
290 g_gate_xlog("Line %u too long.", lineno);
293 line_parse(line, lineno);
300 g_gate_xlog("There are no objects to export.");
302 g_gate_log(LOG_INFO, "Exporting %u object(s).", objs);
306 exports_check(struct ggd_export *ex, struct g_gate_cinit *cinit,
307 struct ggd_connection *conn)
309 char ipmask[32]; /* 32 == strlen("xxx.xxx.xxx.xxx/xxx.xxx.xxx.xxx")+1 */
310 int error = 0, flags;
312 strlcpy(ipmask, ip2str(ex->e_ip), sizeof(ipmask));
313 strlcat(ipmask, "/", sizeof(ipmask));
314 strlcat(ipmask, ip2str(ex->e_mask), sizeof(ipmask));
315 if ((cinit->gc_flags & GGATE_FLAG_RDONLY) != 0) {
316 if (ex->e_flags == O_WRONLY) {
317 g_gate_log(LOG_WARNING, "Read-only access requested, "
318 "but %s (%s) is exported write-only.", ex->e_path,
322 conn->c_flags |= GGATE_FLAG_RDONLY;
324 } else if ((cinit->gc_flags & GGATE_FLAG_WRONLY) != 0) {
325 if (ex->e_flags == O_RDONLY) {
326 g_gate_log(LOG_WARNING, "Write-only access requested, "
327 "but %s (%s) is exported read-only.", ex->e_path,
331 conn->c_flags |= GGATE_FLAG_WRONLY;
334 if (ex->e_flags == O_RDONLY) {
335 g_gate_log(LOG_WARNING, "Read-write access requested, "
336 "but %s (%s) is exported read-only.", ex->e_path,
339 } else if (ex->e_flags == O_WRONLY) {
340 g_gate_log(LOG_WARNING, "Read-write access requested, "
341 "but %s (%s) is exported write-only.", ex->e_path,
346 if ((conn->c_flags & GGATE_FLAG_RDONLY) != 0)
348 else if ((conn->c_flags & GGATE_FLAG_WRONLY) != 0)
352 conn->c_diskfd = open(ex->e_path, flags);
353 if (conn->c_diskfd == -1) {
355 g_gate_log(LOG_ERR, "Cannot open %s: %s.", ex->e_path,
362 static struct ggd_export *
363 exports_find(struct sockaddr *s, struct g_gate_cinit *cinit,
364 struct ggd_connection *conn)
366 struct ggd_export *ex;
370 ip = htonl(((struct sockaddr_in *)(void *)s)->sin_addr.s_addr);
371 SLIST_FOREACH(ex, &exports, e_next) {
372 if ((ip & ex->e_mask) != ex->e_ip) {
373 g_gate_log(LOG_DEBUG, "exports[%s]: IP mismatch.",
377 if (strcmp(cinit->gc_path, ex->e_path) != 0) {
378 g_gate_log(LOG_DEBUG, "exports[%s]: Path mismatch.",
382 error = exports_check(ex, cinit, conn);
390 g_gate_log(LOG_WARNING, "Unauthorized connection from: %s.",
397 * Remove timed out connections.
400 connection_cleanups(void)
402 struct ggd_connection *conn, *tconn;
406 LIST_FOREACH_SAFE(conn, &connections, c_next, tconn) {
407 if (now - conn->c_birthtime > 10) {
408 LIST_REMOVE(conn, c_next);
409 g_gate_log(LOG_NOTICE,
410 "Connection from %s [%s] removed.",
411 ip2str(conn->c_srcip), conn->c_path);
412 close(conn->c_diskfd);
413 close(conn->c_sendfd);
414 close(conn->c_recvfd);
421 static struct ggd_connection *
422 connection_find(struct g_gate_cinit *cinit)
424 struct ggd_connection *conn;
426 LIST_FOREACH(conn, &connections, c_next) {
427 if (conn->c_token == cinit->gc_token)
433 static struct ggd_connection *
434 connection_new(struct g_gate_cinit *cinit, struct sockaddr *s, int sfd)
436 struct ggd_connection *conn;
440 * First, look for old connections.
441 * We probably should do it every X seconds, but what for?
442 * It is only dangerous if an attacker wants to overload connections
443 * queue, so here is a good place to do the cleanups.
445 connection_cleanups();
447 conn = malloc(sizeof(*conn));
450 conn->c_path = strdup(cinit->gc_path);
451 if (conn->c_path == NULL) {
455 conn->c_token = cinit->gc_token;
456 ip = htonl(((struct sockaddr_in *)(void *)s)->sin_addr.s_addr);
458 conn->c_sendfd = conn->c_recvfd = -1;
459 if ((cinit->gc_flags & GGATE_FLAG_SEND) != 0)
460 conn->c_sendfd = sfd;
462 conn->c_recvfd = sfd;
463 conn->c_mediasize = 0;
464 conn->c_sectorsize = 0;
465 time(&conn->c_birthtime);
466 conn->c_flags = cinit->gc_flags;
467 LIST_INSERT_HEAD(&connections, conn, c_next);
468 g_gate_log(LOG_DEBUG, "Connection created [%s, %s].", ip2str(ip),
474 connection_add(struct ggd_connection *conn, struct g_gate_cinit *cinit,
475 struct sockaddr *s, int sfd)
479 ip = htonl(((struct sockaddr_in *)(void *)s)->sin_addr.s_addr);
480 if ((cinit->gc_flags & GGATE_FLAG_SEND) != 0) {
481 if (conn->c_sendfd != -1) {
482 g_gate_log(LOG_WARNING,
483 "Send socket already exists [%s, %s].", ip2str(ip),
487 conn->c_sendfd = sfd;
489 if (conn->c_recvfd != -1) {
490 g_gate_log(LOG_WARNING,
491 "Receive socket already exists [%s, %s].",
492 ip2str(ip), conn->c_path);
495 conn->c_recvfd = sfd;
497 g_gate_log(LOG_DEBUG, "Connection added [%s, %s].", ip2str(ip),
503 * Remove one socket from the given connection or the whole
504 * connection if sfd == -1.
507 connection_remove(struct ggd_connection *conn)
510 LIST_REMOVE(conn, c_next);
511 g_gate_log(LOG_DEBUG, "Connection removed [%s %s].",
512 ip2str(conn->c_srcip), conn->c_path);
513 if (conn->c_sendfd != -1)
514 close(conn->c_sendfd);
515 if (conn->c_recvfd != -1)
516 close(conn->c_recvfd);
522 connection_ready(struct ggd_connection *conn)
525 return (conn->c_sendfd != -1 && conn->c_recvfd != -1);
529 connection_launch(struct ggd_connection *conn)
537 else if (pid == -1) {
538 g_gate_log(LOG_ERR, "Cannot fork: %s.", strerror(errno));
541 g_gate_log(LOG_DEBUG, "Process created [%s].", conn->c_path);
544 * Create condition variables and mutexes for in-queue and out-queue
547 error = pthread_mutex_init(&inqueue_mtx, NULL);
549 g_gate_xlog("pthread_mutex_init(inqueue_mtx): %s.",
552 error = pthread_cond_init(&inqueue_cond, NULL);
554 g_gate_xlog("pthread_cond_init(inqueue_cond): %s.",
557 error = pthread_mutex_init(&outqueue_mtx, NULL);
559 g_gate_xlog("pthread_mutex_init(outqueue_mtx): %s.",
562 error = pthread_cond_init(&outqueue_cond, NULL);
564 g_gate_xlog("pthread_cond_init(outqueue_cond): %s.",
570 * recvtd - thread for receiving I/O request
571 * diskio - thread for doing I/O request
572 * sendtd - thread for sending I/O requests back
574 error = pthread_create(&td, NULL, send_thread, conn);
576 g_gate_xlog("pthread_create(send_thread): %s.",
579 error = pthread_create(&td, NULL, recv_thread, conn);
581 g_gate_xlog("pthread_create(recv_thread): %s.",
588 sendfail(int sfd, int error, const char *fmt, ...)
590 struct g_gate_sinit sinit;
594 memset(&sinit, 0, sizeof(sinit));
595 sinit.gs_error = error;
596 g_gate_swap2n_sinit(&sinit);
597 data = g_gate_send(sfd, &sinit, sizeof(sinit), 0);
598 g_gate_swap2h_sinit(&sinit);
599 if (data != sizeof(sinit)) {
600 g_gate_log(LOG_WARNING, "Cannot send initial packet: %s.",
606 g_gate_vlog(LOG_WARNING, fmt, ap);
612 malloc_waitok(size_t size)
616 while ((p = malloc(size)) == NULL) {
617 g_gate_log(LOG_DEBUG, "Cannot allocate %zu bytes.", size);
624 recv_thread(void *arg)
626 struct ggd_connection *conn;
627 struct ggd_request *req;
632 g_gate_log(LOG_NOTICE, "%s: started [%s]!", __func__, conn->c_path);
638 req = malloc_waitok(sizeof(*req));
639 data = g_gate_recv(fd, &req->r_hdr, sizeof(req->r_hdr),
642 g_gate_log(LOG_DEBUG, "Process %u exiting.", getpid());
644 } else if (data == -1) {
645 g_gate_xlog("Error while receiving hdr packet: %s.",
647 } else if (data != sizeof(req->r_hdr)) {
648 g_gate_xlog("Malformed hdr packet received.");
650 g_gate_log(LOG_DEBUG, "Received hdr packet.");
651 g_gate_swap2h_hdr(&req->r_hdr);
653 g_gate_log(LOG_DEBUG, "%s: offset=%jd length=%u", __func__,
654 (intmax_t)req->r_offset, (unsigned)req->r_length);
657 * Allocate memory for data.
659 req->r_data = malloc_waitok(req->r_length);
662 * Receive data to write for WRITE request.
664 if (req->r_cmd == GGATE_CMD_WRITE) {
665 g_gate_log(LOG_DEBUG, "Waiting for %u bytes of data...",
667 data = g_gate_recv(fd, req->r_data, req->r_length,
670 g_gate_xlog("Error while receiving data: %s.",
676 * Put the request onto the incoming queue.
678 error = pthread_mutex_lock(&inqueue_mtx);
680 TAILQ_INSERT_TAIL(&inqueue, req, r_next);
681 error = pthread_cond_signal(&inqueue_cond);
683 error = pthread_mutex_unlock(&inqueue_mtx);
689 disk_thread(void *arg)
691 struct ggd_connection *conn;
692 struct ggd_request *req;
697 g_gate_log(LOG_NOTICE, "%s: started [%s]!", __func__, conn->c_path);
701 * Get a request from the incoming queue.
703 error = pthread_mutex_lock(&inqueue_mtx);
705 while ((req = TAILQ_FIRST(&inqueue)) == NULL) {
706 error = pthread_cond_wait(&inqueue_cond, &inqueue_mtx);
709 TAILQ_REMOVE(&inqueue, req, r_next);
710 error = pthread_mutex_unlock(&inqueue_mtx);
716 assert(req->r_cmd == GGATE_CMD_READ || req->r_cmd == GGATE_CMD_WRITE);
717 assert(req->r_offset + req->r_length <= (uintmax_t)conn->c_mediasize);
718 assert((req->r_offset % conn->c_sectorsize) == 0);
719 assert((req->r_length % conn->c_sectorsize) == 0);
721 g_gate_log(LOG_DEBUG, "%s: offset=%jd length=%u", __func__,
722 (intmax_t)req->r_offset, (unsigned)req->r_length);
728 switch (req->r_cmd) {
730 data = pread(fd, req->r_data, req->r_length,
733 case GGATE_CMD_WRITE:
734 data = pwrite(fd, req->r_data, req->r_length,
736 /* Free data memory here - better sooner. */
741 if (data != (ssize_t)req->r_length) {
742 /* Report short reads/writes as I/O errors. */
745 g_gate_log(LOG_ERR, "Disk error: %s", strerror(errno));
746 req->r_error = errno;
747 if (req->r_data != NULL) {
754 * Put the request onto the outgoing queue.
756 error = pthread_mutex_lock(&outqueue_mtx);
758 TAILQ_INSERT_TAIL(&outqueue, req, r_next);
759 error = pthread_cond_signal(&outqueue_cond);
761 error = pthread_mutex_unlock(&outqueue_mtx);
770 send_thread(void *arg)
772 struct ggd_connection *conn;
773 struct ggd_request *req;
778 g_gate_log(LOG_NOTICE, "%s: started [%s]!", __func__, conn->c_path);
782 * Get a request from the outgoing queue.
784 error = pthread_mutex_lock(&outqueue_mtx);
786 while ((req = TAILQ_FIRST(&outqueue)) == NULL) {
787 error = pthread_cond_wait(&outqueue_cond,
791 TAILQ_REMOVE(&outqueue, req, r_next);
792 error = pthread_mutex_unlock(&outqueue_mtx);
795 g_gate_log(LOG_DEBUG, "%s: offset=%jd length=%u", __func__,
796 (intmax_t)req->r_offset, (unsigned)req->r_length);
801 g_gate_swap2n_hdr(&req->r_hdr);
802 if (g_gate_send(fd, &req->r_hdr, sizeof(req->r_hdr), 0) == -1) {
803 g_gate_xlog("Error while sending hdr packet: %s.",
806 g_gate_log(LOG_DEBUG, "Sent hdr packet.");
807 g_gate_swap2h_hdr(&req->r_hdr);
808 if (req->r_data != NULL) {
809 data = g_gate_send(fd, req->r_data, req->r_length, 0);
810 if (data != (ssize_t)req->r_length) {
811 g_gate_xlog("Error while sending data: %s.",
814 g_gate_log(LOG_DEBUG,
815 "Sent %zd bytes (offset=%ju, size=%zu).", data,
816 (uintmax_t)req->r_offset, (size_t)req->r_length);
827 log_connection(struct sockaddr *from)
831 ip = htonl(((struct sockaddr_in *)(void *)from)->sin_addr.s_addr);
832 g_gate_log(LOG_INFO, "Connection from: %s.", ip2str(ip));
836 handshake(struct sockaddr *from, int sfd)
838 struct g_gate_version ver;
839 struct g_gate_cinit cinit;
840 struct g_gate_sinit sinit;
841 struct ggd_connection *conn;
842 struct ggd_export *ex;
845 log_connection(from);
847 * Phase 1: Version verification.
849 g_gate_log(LOG_DEBUG, "Receiving version packet.");
850 data = g_gate_recv(sfd, &ver, sizeof(ver), MSG_WAITALL);
851 g_gate_swap2h_version(&ver);
852 if (data != sizeof(ver)) {
853 g_gate_log(LOG_WARNING, "Malformed version packet.");
856 g_gate_log(LOG_DEBUG, "Version packet received.");
857 if (memcmp(ver.gv_magic, GGATE_MAGIC, strlen(GGATE_MAGIC)) != 0) {
858 g_gate_log(LOG_WARNING, "Invalid magic field.");
861 if (ver.gv_version != GGATE_VERSION) {
862 g_gate_log(LOG_WARNING, "Version %u is not supported.",
867 g_gate_swap2n_version(&ver);
868 data = g_gate_send(sfd, &ver, sizeof(ver), 0);
869 g_gate_swap2h_version(&ver);
871 sendfail(sfd, errno, "Error while sending version packet: %s.",
877 * Phase 2: Request verification.
879 g_gate_log(LOG_DEBUG, "Receiving initial packet.");
880 data = g_gate_recv(sfd, &cinit, sizeof(cinit), MSG_WAITALL);
881 g_gate_swap2h_cinit(&cinit);
882 if (data != sizeof(cinit)) {
883 g_gate_log(LOG_WARNING, "Malformed initial packet.");
886 g_gate_log(LOG_DEBUG, "Initial packet received.");
887 conn = connection_find(&cinit);
890 * Connection should already exists.
892 g_gate_log(LOG_DEBUG, "Found existing connection (token=%lu).",
893 (unsigned long)conn->c_token);
894 if (connection_add(conn, &cinit, from, sfd) == -1) {
895 connection_remove(conn);
900 * New connection, allocate space.
902 conn = connection_new(&cinit, from, sfd);
904 sendfail(sfd, ENOMEM,
905 "Cannot allocate new connection.");
908 g_gate_log(LOG_DEBUG, "New connection created (token=%lu).",
909 (unsigned long)conn->c_token);
912 ex = exports_find(from, &cinit, conn);
914 sendfail(sfd, errno, NULL);
915 connection_remove(conn);
918 if (conn->c_mediasize == 0) {
919 conn->c_mediasize = g_gate_mediasize(conn->c_diskfd);
920 conn->c_sectorsize = g_gate_sectorsize(conn->c_diskfd);
922 sinit.gs_mediasize = conn->c_mediasize;
923 sinit.gs_sectorsize = conn->c_sectorsize;
926 g_gate_log(LOG_DEBUG, "Sending initial packet.");
928 g_gate_swap2n_sinit(&sinit);
929 data = g_gate_send(sfd, &sinit, sizeof(sinit), 0);
930 g_gate_swap2h_sinit(&sinit);
932 sendfail(sfd, errno, "Error while sending initial packet: %s.",
937 if (connection_ready(conn)) {
938 connection_launch(conn);
939 connection_remove(conn);
945 huphandler(int sig __unused)
952 main(int argc, char *argv[])
954 const char *ggated_pidfile = _PATH_VARRUN "/ggated.pid";
956 struct sockaddr_in serv;
957 struct sockaddr from;
963 bindaddr = htonl(INADDR_ANY);
965 while ((ch = getopt(argc, argv, "a:hnp:F:R:S:v")) != -1) {
968 bindaddr = g_gate_str2ip(optarg);
969 if (bindaddr == INADDR_NONE) {
971 "Invalid IP/host name to bind to.");
975 ggated_pidfile = optarg;
982 port = strtoul(optarg, NULL, 10);
983 if (port == 0 && errno != 0)
984 errx(EXIT_FAILURE, "Invalid port.");
988 rcvbuf = strtoul(optarg, NULL, 10);
989 if (rcvbuf == 0 && errno != 0)
990 errx(EXIT_FAILURE, "Invalid rcvbuf.");
994 sndbuf = strtoul(optarg, NULL, 10);
995 if (sndbuf == 0 && errno != 0)
996 errx(EXIT_FAILURE, "Invalid sndbuf.");
1009 if (argv[0] != NULL)
1010 exports_file = argv[0];
1013 pfh = pidfile_open(ggated_pidfile, 0600, &otherpid);
1015 if (errno == EEXIST) {
1016 errx(EXIT_FAILURE, "Daemon already running, pid: %jd.",
1017 (intmax_t)otherpid);
1019 err(EXIT_FAILURE, "Cannot open/create pidfile");
1022 if (!g_gate_verbose) {
1023 /* Run in daemon mode. */
1024 if (daemon(0, 0) == -1)
1025 g_gate_xlog("Cannot daemonize: %s", strerror(errno));
1030 signal(SIGCHLD, SIG_IGN);
1032 sfd = socket(AF_INET, SOCK_STREAM, 0);
1034 g_gate_xlog("Cannot open stream socket: %s.", strerror(errno));
1035 bzero(&serv, sizeof(serv));
1036 serv.sin_family = AF_INET;
1037 serv.sin_addr.s_addr = bindaddr;
1038 serv.sin_port = htons(port);
1040 g_gate_socket_settings(sfd);
1042 if (bind(sfd, (struct sockaddr *)&serv, sizeof(serv)) == -1)
1043 g_gate_xlog("bind(): %s.", strerror(errno));
1044 if (listen(sfd, 5) == -1)
1045 g_gate_xlog("listen(): %s.", strerror(errno));
1047 g_gate_log(LOG_INFO, "Listen on port: %d.", port);
1049 signal(SIGHUP, huphandler);
1052 fromlen = sizeof(from);
1053 tmpsfd = accept(sfd, &from, &fromlen);
1055 g_gate_xlog("accept(): %s.", strerror(errno));
1062 if (!handshake(&from, tmpsfd))
1066 pidfile_remove(pfh);