diff options
author | jmg <jmg@FreeBSD.org> | 2013-09-13 16:57:28 +0000 |
---|---|---|
committer | jmg <jmg@FreeBSD.org> | 2013-09-13 16:57:28 +0000 |
commit | 1b9dc8f60c0b501e17ab3dcdceb4fb2797485692 (patch) | |
tree | c909ff0892366b5acc4a1abbc7b61b53bd564c5f /usr.sbin/daemon/daemon.c | |
parent | 33a03d15e5bf925c6298af751be6ce613f664830 (diff) | |
download | FreeBSD-src-1b9dc8f60c0b501e17ab3dcdceb4fb2797485692.zip FreeBSD-src-1b9dc8f60c0b501e17ab3dcdceb4fb2797485692.tar.gz |
add support for writing the pid of the daemon program to a pid file so
that daemon can be used w/ rc.subr and ports can use the additional
functionality, such as keeping the ldap daemon up and running, and have
the proper program to signal to exit..
PR: bin/181341
Submitted by: feld
Approved by: re (glebius)
Diffstat (limited to 'usr.sbin/daemon/daemon.c')
-rw-r--r-- | usr.sbin/daemon/daemon.c | 35 |
1 files changed, 28 insertions, 7 deletions
diff --git a/usr.sbin/daemon/daemon.c b/usr.sbin/daemon/daemon.c index b012c88..5ae4fff 100644 --- a/usr.sbin/daemon/daemon.c +++ b/usr.sbin/daemon/daemon.c @@ -53,16 +53,17 @@ static void usage(void); int main(int argc, char *argv[]) { - struct pidfh *pfh = NULL; + struct pidfh *ppfh, *pfh; sigset_t mask, oldmask; int ch, nochdir, noclose, restart; - const char *pidfile, *user; + const char *pidfile, *ppidfile, *user; pid_t otherpid, pid; nochdir = noclose = 1; restart = 0; - pidfile = user = NULL; - while ((ch = getopt(argc, argv, "cfp:ru:")) != -1) { + ppfh = pfh = NULL; + ppidfile = pidfile = user = NULL; + while ((ch = getopt(argc, argv, "cfp:P:ru:")) != -1) { switch (ch) { case 'c': nochdir = 0; @@ -73,6 +74,9 @@ main(int argc, char *argv[]) case 'p': pidfile = optarg; break; + case 'P': + ppidfile = optarg; + break; case 'r': restart = 1; break; @@ -89,7 +93,7 @@ main(int argc, char *argv[]) if (argc == 0) usage(); - pfh = NULL; + ppfh = pfh = NULL; /* * Try to open the pidfile before calling daemon(3), * to be able to report the error intelligently @@ -104,6 +108,18 @@ main(int argc, char *argv[]) err(2, "pidfile ``%s''", pidfile); } } + + /* do same for actual daemon process */ + if (ppidfile != NULL) { + ppfh = pidfile_open(ppidfile, 0600, &otherpid); + if (ppfh == NULL) { + if (errno == EEXIST) { + errx(3, "process already running, pid: %d", + otherpid); + } + err(2, "ppidfile ``%s''", ppidfile); + } + } if (daemon(nochdir, noclose) == -1) err(1, NULL); @@ -176,12 +192,17 @@ restart: */ err(1, "%s", argv[0]); } + /* write out parent pidfile if needed */ + if (ppidfile != NULL) + pidfile_write(ppfh); + setproctitle("%s[%d]", argv[0], pid); if (wait_child(pid, &mask) == 0 && restart) { sleep(1); goto restart; } pidfile_remove(pfh); + pidfile_remove(ppfh); exit(0); /* Exit status does not matter. */ } @@ -240,7 +261,7 @@ static void usage(void) { (void)fprintf(stderr, - "usage: daemon [-cfr] [-p pidfile] [-u user] command " - "arguments ...\n"); + "usage: daemon [-cfr] [-p child_pidfile] [-P supervisor_pidfile] " + "[-u user]\n command arguments ...\n"); exit(1); } |