From owner-svn-src-user@freebsd.org Mon Nov 16 22:40:50 2015 Return-Path: Delivered-To: svn-src-user@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 96012A31CD2 for ; Mon, 16 Nov 2015 22:40:50 +0000 (UTC) (envelope-from ngie@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 69F9C192D; Mon, 16 Nov 2015 22:40:50 +0000 (UTC) (envelope-from ngie@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id tAGMenpn090861; Mon, 16 Nov 2015 22:40:49 GMT (envelope-from ngie@FreeBSD.org) Received: (from ngie@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id tAGMenR0090857; Mon, 16 Nov 2015 22:40:49 GMT (envelope-from ngie@FreeBSD.org) Message-Id: <201511162240.tAGMenR0090857@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: ngie set sender to ngie@FreeBSD.org using -f From: Garrett Cooper Date: Mon, 16 Nov 2015 22:40:49 +0000 (UTC) To: src-committers@freebsd.org, svn-src-user@freebsd.org Subject: svn commit: r290955 - user/ngie/more-tests2/sbin/ggate/ggated X-SVN-Group: user MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-user@freebsd.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: "SVN commit messages for the experimental " user" src tree" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 16 Nov 2015 22:40:50 -0000 Author: ngie Date: Mon Nov 16 22:40:49 2015 New Revision: 290955 URL: https://svnweb.freebsd.org/changeset/base/290955 Log: Add pidfile support to ggated Modified: user/ngie/more-tests2/sbin/ggate/ggated/Makefile user/ngie/more-tests2/sbin/ggate/ggated/ggated.8 user/ngie/more-tests2/sbin/ggate/ggated/ggated.c Modified: user/ngie/more-tests2/sbin/ggate/ggated/Makefile ============================================================================== --- user/ngie/more-tests2/sbin/ggate/ggated/Makefile Mon Nov 16 22:37:28 2015 (r290954) +++ user/ngie/more-tests2/sbin/ggate/ggated/Makefile Mon Nov 16 22:40:49 2015 (r290955) @@ -6,7 +6,7 @@ PROG= ggated MAN= ggated.8 SRCS= ggated.c ggate.c -LIBADD= pthread +LIBADD= pthread util CFLAGS+= -I${.CURDIR}/../shared Modified: user/ngie/more-tests2/sbin/ggate/ggated/ggated.8 ============================================================================== --- user/ngie/more-tests2/sbin/ggate/ggated/ggated.8 Mon Nov 16 22:37:28 2015 (r290954) +++ user/ngie/more-tests2/sbin/ggate/ggated/ggated.8 Mon Nov 16 22:40:49 2015 (r290955) @@ -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 foll 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, Modified: user/ngie/more-tests2/sbin/ggate/ggated/ggated.c ============================================================================== --- user/ngie/more-tests2/sbin/ggate/ggated/ggated.c Mon Nov 16 22:37:28 2015 (r290954) +++ user/ngie/more-tests2/sbin/ggate/ggated/ggated.c Mon Nov 16 22:40:49 2015 (r290955) @@ -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); }