From 52ce820bdc82ac7680828e84b1e2f4217cdead57 Mon Sep 17 00:00:00 2001 From: ngie Date: Mon, 16 Nov 2015 22:21:10 +0000 Subject: [PATCH 1/4] Sort imports --- ggated/ggated.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/ggated/ggated.c b/ggated/ggated.c index a340f01..4e3fd45 100644 --- a/ggated/ggated.c +++ b/ggated/ggated.c @@ -26,32 +26,32 @@ * $FreeBSD$ */ -#include -#include -#include -#include -#include -#include #include -#include +#include +#include #include -#include #include +#include +#include #include #include -#include -#include +#include #include #include -#include -#include #include #include #include -#include +#include #include -#include +#include +#include #include +#include +#include +#include +#include +#include +#include #include "ggate.h" From 401cdeba19cc55d68f3dc280dbc3adae7bfb297a Mon Sep 17 00:00:00 2001 From: ngie Date: Mon, 16 Nov 2015 22:22:17 +0000 Subject: [PATCH 2/4] Use a more common pattern for parsing with getopt(3) --- ggated/ggated.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/ggated/ggated.c b/ggated/ggated.c index 4e3fd45..262550e 100644 --- a/ggated/ggated.c +++ b/ggated/ggated.c @@ -949,17 +949,12 @@ main(int argc, char *argv[]) struct sockaddr_in serv; struct sockaddr from; socklen_t fromlen; - int sfd, tmpsfd; + int ch, sfd, tmpsfd; unsigned port; bindaddr = htonl(INADDR_ANY); port = G_GATE_PORT; - for (;;) { - int ch; - - ch = getopt(argc, argv, "a:hnp:R:S:v"); - if (ch == -1) - break; + while ((ch = getopt(argc, argv, "a:hnp:R:S:v")) != -1) { switch (ch) { case 'a': bindaddr = g_gate_str2ip(optarg); From 7a51dd003c3c49987ad37b00cf9e4ce1b4384992 Mon Sep 17 00:00:00 2001 From: ngie Date: Mon, 16 Nov 2015 22:40:49 +0000 Subject: [PATCH 3/4] Add pidfile support to ggated --- ggated/Makefile | 2 +- ggated/ggated.8 | 13 +++++++++++++ ggated/ggated.c | 22 +++++++++++++++++++++- 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/ggated/Makefile b/ggated/Makefile index af5c9bd..83516c6 100644 --- a/ggated/Makefile +++ b/ggated/Makefile @@ -6,7 +6,7 @@ PROG= ggated MAN= ggated.8 SRCS= ggated.c ggate.c -LIBADD= pthread +LIBADD= pthread util CFLAGS+= -I${.CURDIR}/../shared diff --git a/ggated/ggated.8 b/ggated/ggated.8 index dd6109a..15b1792 100644 --- a/ggated/ggated.8 +++ b/ggated/ggated.8 @@ -37,6 +37,7 @@ .Op Fl v .Op Fl a Ar address .Op Fl p Ar port +.Op Fl F Ar pidfile .Op Fl R Ar rcvbuf .Op Fl S Ar sndbuf .Op Ar "exports file" @@ -67,6 +68,10 @@ Port on which .Nm listens for connections. Default is 3080. +.It Fl F Ar pidfile +PID file that +.Nm +uses. .It Fl R Ar rcvbuf Size of receive buffer to use. Default is 131072 (128kB). @@ -86,6 +91,14 @@ The format of an exports file is as follows: 1.2.3.0/24 RW /tmp/test.img hostname WO /tmp/image .Ed +.El +.Sh FILES +.Bl -tag -width ".Pa /var/run/ggated.pid" -compact +.It Pa /var/run/ggated.pid +The default location of the +.Nm +PID file. +.El .Sh EXIT STATUS Exit status is 0 on success, or 1 if the command fails. To get details about the failure, diff --git a/ggated/ggated.c b/ggated/ggated.c index 262550e..d6a2e47 100644 --- a/ggated/ggated.c +++ b/ggated/ggated.c @@ -43,6 +43,8 @@ #include #include #include +#include +#include #include #include #include @@ -946,15 +948,18 @@ huphandler(int sig __unused) int main(int argc, char *argv[]) { + const char *ggated_pidfile = _PATH_VARRUN "/ggated.pid"; + struct pidfh *pfh; struct sockaddr_in serv; struct sockaddr from; socklen_t fromlen; + pid_t otherpid; int ch, sfd, tmpsfd; unsigned port; bindaddr = htonl(INADDR_ANY); port = G_GATE_PORT; - while ((ch = getopt(argc, argv, "a:hnp:R:S:v")) != -1) { + while ((ch = getopt(argc, argv, "a:hnp:F:R:S:v")) != -1) { switch (ch) { case 'a': bindaddr = g_gate_str2ip(optarg); @@ -963,6 +968,9 @@ main(int argc, char *argv[]) "Invalid IP/host name to bind to."); } break; + case 'F': + ggated_pidfile = optarg; + break; case 'n': nagle = 0; break; @@ -999,12 +1007,23 @@ main(int argc, char *argv[]) exports_file = argv[0]; exports_get(); + pfh = pidfile_open(ggated_pidfile, 0600, &otherpid); + if (pfh == NULL) { + if (errno == EEXIST) { + errx(EXIT_FAILURE, "Daemon already running, pid: %jd.", + (intmax_t)otherpid); + } + err(EXIT_FAILURE, "Cannot open/create pidfile"); + } + if (!g_gate_verbose) { /* Run in daemon mode. */ if (daemon(0, 0) == -1) g_gate_xlog("Cannot daemonize: %s", strerror(errno)); } + pidfile_write(pfh); + signal(SIGCHLD, SIG_IGN); sfd = socket(AF_INET, SOCK_STREAM, 0); @@ -1041,5 +1060,6 @@ main(int argc, char *argv[]) close(tmpsfd); } close(sfd); + pidfile_remove(pfh); exit(EXIT_SUCCESS); } From 63f32471b02f56d919f30a5da7f8363e74afe240 Mon Sep 17 00:00:00 2001 From: ngie Date: Tue, 24 Nov 2015 23:44:06 +0000 Subject: [PATCH 4/4] Add -F pidfile to usage(..) --- ggated/ggated.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ggated/ggated.c b/ggated/ggated.c index d6a2e47..e234cb5 100644 --- a/ggated/ggated.c +++ b/ggated/ggated.c @@ -112,8 +112,8 @@ static void usage(void) { - fprintf(stderr, "usage: %s [-nv] [-a address] [-p port] [-R rcvbuf] " - "[-S sndbuf] [exports file]\n", getprogname()); + fprintf(stderr, "usage: %s [-nv] [-a address] [-F pidfile] [-p port] " + "[-R rcvbuf] [-S sndbuf] [exports file]\n", getprogname()); exit(EXIT_FAILURE); }