Portable build framework for OpenNTPD
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 lines
3.6 KiB

4 years ago
8 years ago
8 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
8 years ago
8 years ago
4 years ago
8 years ago
4 years ago
4 years ago
8 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. From e809e7f516d3eb2dbd6c7bf250835aadff59b796 Mon Sep 17 00:00:00 2001
  2. From: Brent Cook <busterb@gmail.com>
  3. Date: Wed, 31 Dec 2014 08:26:41 -0600
  4. Subject: [PATCH 07/18] add -p option to create a pid file
  5. This is used in both the Gentoo and Debian ports.
  6. Origin: https://bugs.gentoo.org/show_bug.cgi?id=493082
  7. ---
  8. src/usr.sbin/ntpd/ntpd.8 | 4 ++++
  9. src/usr.sbin/ntpd/ntpd.c | 33 ++++++++++++++++++++++++++++-----
  10. src/usr.sbin/ntpd/ntpd.h | 1 +
  11. 3 files changed, 33 insertions(+), 5 deletions(-)
  12. diff --git a/src/usr.sbin/ntpd/ntpd.8 b/src/usr.sbin/ntpd/ntpd.8
  13. index f5fe1db9d8..98af025991 100644
  14. --- a/src/usr.sbin/ntpd/ntpd.8
  15. +++ b/src/usr.sbin/ntpd/ntpd.8
  16. @@ -25,6 +25,7 @@
  17. .Bk -words
  18. .Op Fl dnv
  19. .Op Fl f Ar file
  20. +.Op Fl p Ar file
  21. .Ek
  22. .Sh DESCRIPTION
  23. The
  24. @@ -67,6 +68,9 @@ configured NTP servers to reply.
  25. This option allows
  26. .Nm
  27. to send DEBUG priority messages to syslog.
  28. +.It Fl p Ar file
  29. +Write pid to
  30. +.Ar file
  31. .El
  32. .Pp
  33. .Nm
  34. diff --git a/src/usr.sbin/ntpd/ntpd.c b/src/usr.sbin/ntpd/ntpd.c
  35. index e015d1d093..a0da39adf7 100644
  36. --- a/src/usr.sbin/ntpd/ntpd.c
  37. +++ b/src/usr.sbin/ntpd/ntpd.c
  38. @@ -90,6 +90,18 @@ sighdlr(int sig)
  39. }
  40. }
  41. +void
  42. +writepid(struct ntpd_conf *lconf)
  43. +{
  44. + if (lconf->pid_file != NULL) {
  45. + FILE *f = fopen(lconf->pid_file, "w");
  46. + if (f == NULL)
  47. + fatal("couldn't open pid file");
  48. + fprintf(f, "%ld\n", (long) getpid());
  49. + fclose(f);
  50. + }
  51. +}
  52. +
  53. __dead void
  54. usage(void)
  55. {
  56. @@ -99,7 +111,7 @@ usage(void)
  57. fprintf(stderr,
  58. "usage: ntpctl -s all | peers | Sensors | status\n");
  59. else
  60. - fprintf(stderr, "usage: %s [-dnv] [-f file]\n",
  61. + fprintf(stderr, "usage: %s [-dnv] [-f file] [-p file]\n",
  62. __progname);
  63. exit(1);
  64. }
  65. @@ -151,7 +163,7 @@ main(int argc, char *argv[])
  66. memset(&lconf, 0, sizeof(lconf));
  67. - while ((ch = getopt(argc, argv, "df:nP:sSv")) != -1) {
  68. + while ((ch = getopt(argc, argv, "df:np:P:sSv")) != -1) {
  69. switch (ch) {
  70. case 'd':
  71. lconf.debug = 1;
  72. @@ -166,6 +178,9 @@ main(int argc, char *argv[])
  73. case 'P':
  74. pname = optarg;
  75. break;
  76. + case 'p':
  77. + lconf.pid_file = optarg;
  78. + break;
  79. case 's':
  80. case 'S':
  81. sopt = ch;
  82. @@ -244,9 +259,11 @@ main(int argc, char *argv[])
  83. logdest = lconf.debug ? LOG_TO_STDERR : LOG_TO_SYSLOG;
  84. if (!lconf.settime) {
  85. log_init(logdest, lconf.verbose, LOG_DAEMON);
  86. - if (!lconf.debug)
  87. + if (!lconf.debug) {
  88. if (daemon(1, 0))
  89. fatal("daemon");
  90. + writepid(&lconf);
  91. + }
  92. } else {
  93. settime_deadline = getmonotime();
  94. timeout = 100;
  95. @@ -330,9 +347,11 @@ main(int argc, char *argv[])
  96. log_init(logdest, lconf.verbose, LOG_DAEMON);
  97. log_warnx("no reply received in time, skipping initial "
  98. "time setting");
  99. - if (!lconf.debug)
  100. + if (!lconf.debug) {
  101. if (daemon(1, 0))
  102. fatal("daemon");
  103. + writepid(&lconf);
  104. + }
  105. }
  106. if (nfds > 0 && (pfd[PFD_PIPE].revents & POLLOUT))
  107. @@ -371,6 +390,8 @@ main(int argc, char *argv[])
  108. msgbuf_clear(&ibuf->w);
  109. free(ibuf);
  110. log_info("Terminating");
  111. + if (lconf.pid_file != NULL)
  112. + unlink(lconf.pid_file);
  113. return (0);
  114. }
  115. @@ -431,9 +452,11 @@ dispatch_imsg(struct ntpd_conf *lconf, int argc, char **argv)
  116. memcpy(&d, imsg.data, sizeof(d));
  117. ntpd_settime(d);
  118. /* daemonize now */
  119. - if (!lconf->debug)
  120. + if (!lconf->debug) {
  121. if (daemon(1, 0))
  122. fatal("daemon");
  123. + writepid(lconf);
  124. + }
  125. lconf->settime = 0;
  126. timeout = INFTIM;
  127. break;
  128. diff --git a/src/usr.sbin/ntpd/ntpd.h b/src/usr.sbin/ntpd/ntpd.h
  129. index b360af9eac..3ea46ac064 100644
  130. --- a/src/usr.sbin/ntpd/ntpd.h
  131. +++ b/src/usr.sbin/ntpd/ntpd.h
  132. @@ -261,6 +261,7 @@ struct ntpd_conf {
  133. u_int8_t *ca;
  134. size_t ca_len;
  135. int tmpfail;
  136. + char *pid_file;
  137. };
  138. struct ctl_show_status {
  139. --
  140. 2.27.0