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

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. From 409921b7cbfab4690911dae29c75a4120d0deab2 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/13] 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 dcfb6d2..1b885a1 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 dnSsv
  19. .Op Fl f Ar file
  20. +.Op Fl p Ar file
  21. .Ek
  22. .Sh DESCRIPTION
  23. The
  24. @@ -59,6 +60,9 @@ instead of the default
  25. .It Fl n
  26. Configtest mode.
  27. Only check the configuration file for validity.
  28. +.It Fl p Ar file
  29. +Write pid to
  30. +.Ar file
  31. .It Fl S
  32. Do not set the time immediately at startup.
  33. This is the default.
  34. diff --git a/src/usr.sbin/ntpd/ntpd.c b/src/usr.sbin/ntpd/ntpd.c
  35. index aac5e23..d1599e2 100644
  36. --- a/src/usr.sbin/ntpd/ntpd.c
  37. +++ b/src/usr.sbin/ntpd/ntpd.c
  38. @@ -87,6 +87,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. @@ -96,7 +108,7 @@ usage(void)
  57. fprintf(stderr,
  58. "usage: ntpctl -s all | peers | Sensors | status\n");
  59. else
  60. - fprintf(stderr, "usage: %s [-dnSsv] [-f file]\n",
  61. + fprintf(stderr, "usage: %s [-dnSsv] [-f file] [-p file]\n",
  62. __progname);
  63. exit(1);
  64. }
  65. @@ -135,7 +147,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:nPp:sSv")) != -1) {
  69. switch (ch) {
  70. case 'd':
  71. lconf.debug = 2;
  72. @@ -150,6 +162,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. lconf.settime = 1;
  81. break;
  82. @@ -217,9 +232,11 @@ main(int argc, char *argv[])
  83. if (!lconf.settime) {
  84. log_init(lconf.debug, LOG_DAEMON);
  85. log_verbose(lconf.verbose);
  86. - if (!lconf.debug)
  87. + if (!lconf.debug) {
  88. if (daemon(1, 0))
  89. fatal("daemon");
  90. + writepid(&lconf);
  91. + }
  92. } else
  93. timeout = SETTIME_TIMEOUT * 1000;
  94. @@ -294,9 +311,11 @@ main(int argc, char *argv[])
  95. log_verbose(lconf.verbose);
  96. log_warnx("no reply received in time, skipping initial "
  97. "time setting");
  98. - if (!lconf.debug)
  99. + if (!lconf.debug) {
  100. if (daemon(1, 0))
  101. fatal("daemon");
  102. + writepid(&lconf);
  103. + }
  104. }
  105. if (nfds > 0 && (pfd[PFD_PIPE].revents & POLLOUT))
  106. @@ -335,6 +354,8 @@ main(int argc, char *argv[])
  107. msgbuf_clear(&ibuf->w);
  108. free(ibuf);
  109. log_info("Terminating");
  110. + if (lconf.pid_file != NULL)
  111. + unlink(lconf.pid_file);
  112. return (0);
  113. }
  114. @@ -395,9 +416,11 @@ dispatch_imsg(struct ntpd_conf *lconf, int argc, char **argv)
  115. memcpy(&d, imsg.data, sizeof(d));
  116. ntpd_settime(d);
  117. /* daemonize now */
  118. - if (!lconf->debug)
  119. + if (!lconf->debug) {
  120. if (daemon(1, 0))
  121. fatal("daemon");
  122. + writepid(lconf);
  123. + }
  124. lconf->settime = 0;
  125. timeout = INFTIM;
  126. break;
  127. diff --git a/src/usr.sbin/ntpd/ntpd.h b/src/usr.sbin/ntpd/ntpd.h
  128. index f80e23c..fcca519 100644
  129. --- a/src/usr.sbin/ntpd/ntpd.h
  130. +++ b/src/usr.sbin/ntpd/ntpd.h
  131. @@ -249,6 +249,7 @@ struct ntpd_conf {
  132. u_int constraint_errors;
  133. u_int8_t *ca;
  134. size_t ca_len;
  135. + char *pid_file;
  136. };
  137. struct ctl_show_status {
  138. --
  139. 2.10.1