From e809e7f516d3eb2dbd6c7bf250835aadff59b796 Mon Sep 17 00:00:00 2001 From: Brent Cook Date: Wed, 31 Dec 2014 08:26:41 -0600 Subject: [PATCH 07/18] add -p option to create a pid file This is used in both the Gentoo and Debian ports. Origin: https://bugs.gentoo.org/show_bug.cgi?id=493082 --- src/usr.sbin/ntpd/ntpd.8 | 4 ++++ src/usr.sbin/ntpd/ntpd.c | 33 ++++++++++++++++++++++++++++----- src/usr.sbin/ntpd/ntpd.h | 1 + 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/usr.sbin/ntpd/ntpd.8 b/src/usr.sbin/ntpd/ntpd.8 index f5fe1db9d8..98af025991 100644 --- a/src/usr.sbin/ntpd/ntpd.8 +++ b/src/usr.sbin/ntpd/ntpd.8 @@ -25,6 +25,7 @@ .Bk -words .Op Fl dnv .Op Fl f Ar file +.Op Fl p Ar file .Ek .Sh DESCRIPTION The @@ -67,6 +68,9 @@ configured NTP servers to reply. This option allows .Nm to send DEBUG priority messages to syslog. +.It Fl p Ar file +Write pid to +.Ar file .El .Pp .Nm diff --git a/src/usr.sbin/ntpd/ntpd.c b/src/usr.sbin/ntpd/ntpd.c index e015d1d093..a0da39adf7 100644 --- a/src/usr.sbin/ntpd/ntpd.c +++ b/src/usr.sbin/ntpd/ntpd.c @@ -90,6 +90,18 @@ sighdlr(int sig) } } +void +writepid(struct ntpd_conf *lconf) +{ + if (lconf->pid_file != NULL) { + FILE *f = fopen(lconf->pid_file, "w"); + if (f == NULL) + fatal("couldn't open pid file"); + fprintf(f, "%ld\n", (long) getpid()); + fclose(f); + } +} + __dead void usage(void) { @@ -99,7 +111,7 @@ usage(void) fprintf(stderr, "usage: ntpctl -s all | peers | Sensors | status\n"); else - fprintf(stderr, "usage: %s [-dnv] [-f file]\n", + fprintf(stderr, "usage: %s [-dnv] [-f file] [-p file]\n", __progname); exit(1); } @@ -151,7 +163,7 @@ main(int argc, char *argv[]) memset(&lconf, 0, sizeof(lconf)); - while ((ch = getopt(argc, argv, "df:nP:sSv")) != -1) { + while ((ch = getopt(argc, argv, "df:np:P:sSv")) != -1) { switch (ch) { case 'd': lconf.debug = 1; @@ -166,6 +178,9 @@ main(int argc, char *argv[]) case 'P': pname = optarg; break; + case 'p': + lconf.pid_file = optarg; + break; case 's': case 'S': sopt = ch; @@ -244,9 +259,11 @@ main(int argc, char *argv[]) logdest = lconf.debug ? LOG_TO_STDERR : LOG_TO_SYSLOG; if (!lconf.settime) { log_init(logdest, lconf.verbose, LOG_DAEMON); - if (!lconf.debug) + if (!lconf.debug) { if (daemon(1, 0)) fatal("daemon"); + writepid(&lconf); + } } else { settime_deadline = getmonotime(); timeout = 100; @@ -330,9 +347,11 @@ main(int argc, char *argv[]) log_init(logdest, lconf.verbose, LOG_DAEMON); log_warnx("no reply received in time, skipping initial " "time setting"); - if (!lconf.debug) + if (!lconf.debug) { if (daemon(1, 0)) fatal("daemon"); + writepid(&lconf); + } } if (nfds > 0 && (pfd[PFD_PIPE].revents & POLLOUT)) @@ -371,6 +390,8 @@ main(int argc, char *argv[]) msgbuf_clear(&ibuf->w); free(ibuf); log_info("Terminating"); + if (lconf.pid_file != NULL) + unlink(lconf.pid_file); return (0); } @@ -431,9 +452,11 @@ dispatch_imsg(struct ntpd_conf *lconf, int argc, char **argv) memcpy(&d, imsg.data, sizeof(d)); ntpd_settime(d); /* daemonize now */ - if (!lconf->debug) + if (!lconf->debug) { if (daemon(1, 0)) fatal("daemon"); + writepid(lconf); + } lconf->settime = 0; timeout = INFTIM; break; diff --git a/src/usr.sbin/ntpd/ntpd.h b/src/usr.sbin/ntpd/ntpd.h index b360af9eac..3ea46ac064 100644 --- a/src/usr.sbin/ntpd/ntpd.h +++ b/src/usr.sbin/ntpd/ntpd.h @@ -261,6 +261,7 @@ struct ntpd_conf { u_int8_t *ca; size_t ca_len; int tmpfail; + char *pid_file; }; struct ctl_show_status { -- 2.27.0