From 933fd946afb55fdeaae822b84b0674ee114ad497 Mon Sep 17 00:00:00 2001 From: Brent Cook Date: Wed, 31 Dec 2014 08:26:41 -0600 Subject: [PATCH 07/13] 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 689d269c6..fbcfa85b3 100644 --- a/src/usr.sbin/ntpd/ntpd.8 +++ b/src/usr.sbin/ntpd/ntpd.8 @@ -25,6 +25,7 @@ .Bk -words .Op Fl dnSsv .Op Fl f Ar file +.Op Fl p Ar file .Ek .Sh DESCRIPTION The @@ -59,6 +60,9 @@ instead of the default .It Fl n Configtest mode. Only check the configuration file for validity. +.It Fl p Ar file +Write pid to +.Ar file .It Fl S Do not set the time immediately at startup. This is the default. diff --git a/src/usr.sbin/ntpd/ntpd.c b/src/usr.sbin/ntpd/ntpd.c index 0ec3ede03..3d9e089b3 100644 --- a/src/usr.sbin/ntpd/ntpd.c +++ b/src/usr.sbin/ntpd/ntpd.c @@ -87,6 +87,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) { @@ -96,7 +108,7 @@ usage(void) fprintf(stderr, "usage: ntpctl -s all | peers | Sensors | status\n"); else - fprintf(stderr, "usage: %s [-dnSsv] [-f file]\n", + fprintf(stderr, "usage: %s [-dnSsv] [-f file] [-p file]\n", __progname); exit(1); } @@ -132,7 +144,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 = 2; @@ -147,6 +159,9 @@ main(int argc, char *argv[]) case 'P': pname = optarg; break; + case 'p': + lconf.pid_file = optarg; + break; case 's': lconf.settime = 1; break; @@ -210,9 +225,11 @@ main(int argc, char *argv[]) if (!lconf.settime) { log_init(lconf.debug, LOG_DAEMON); log_setverbose(lconf.verbose); - if (!lconf.debug) + if (!lconf.debug) { if (daemon(1, 0)) fatal("daemon"); + writepid(&lconf); + } } else timeout = SETTIME_TIMEOUT * 1000; @@ -287,9 +304,11 @@ main(int argc, char *argv[]) log_setverbose(lconf.verbose); 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)) @@ -328,6 +347,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); } @@ -388,9 +409,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 b7ddfee0a..39b8e7bf4 100644 --- a/src/usr.sbin/ntpd/ntpd.h +++ b/src/usr.sbin/ntpd/ntpd.h @@ -253,6 +253,7 @@ struct ntpd_conf { u_int constraint_errors; u_int8_t *ca; size_t ca_len; + char *pid_file; }; struct ctl_show_status { -- 2.13.0