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.

159 lines
3.9 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. From 04896536acf7a0305de2397afe4f86d3bbf66df7 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/12] 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 | 35 +++++++++++++++++++++++++++++------
  10. src/usr.sbin/ntpd/ntpd.h | 1 +
  11. 3 files changed, 34 insertions(+), 6 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 43486f0..f8727f6 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. @@ -132,7 +144,7 @@ main(int argc, char *argv[])
  66. memset(&lconf, 0, sizeof(lconf));
  67. - while ((ch = getopt(argc, argv, "df:nsSv")) != -1) {
  68. + while ((ch = getopt(argc, argv, "df:np:sSv")) != -1) {
  69. switch (ch) {
  70. case 'd':
  71. lconf.debug = 2;
  72. @@ -144,6 +156,9 @@ main(int argc, char *argv[])
  73. lconf.debug = 2;
  74. lconf.noaction = 1;
  75. break;
  76. + case 'p':
  77. + lconf.pid_file = optarg;
  78. + break;
  79. case 's':
  80. lconf.settime = 1;
  81. break;
  82. @@ -192,9 +207,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. + }
  91. + writepid(&lconf);
  92. } else
  93. timeout = SETTIME_TIMEOUT * 1000;
  94. @@ -230,7 +247,7 @@ main(int argc, char *argv[])
  95. * Constraint processes are forked with certificates in memory,
  96. * then privdrop into chroot before speaking to the outside world.
  97. */
  98. -#if 0
  99. +#if 0
  100. if (pledge("stdio rpath inet settime proc id", NULL) == -1)
  101. err(1, "pledge");
  102. #endif
  103. @@ -275,9 +292,11 @@ main(int argc, char *argv[])
  104. log_verbose(lconf.verbose);
  105. log_warnx("no reply received in time, skipping initial "
  106. "time setting");
  107. - if (!lconf.debug)
  108. + if (!lconf.debug) {
  109. if (daemon(1, 0))
  110. fatal("daemon");
  111. + writepid(&lconf);
  112. + }
  113. }
  114. if (nfds > 0 && (pfd[PFD_PIPE].revents & POLLOUT))
  115. @@ -320,6 +339,8 @@ main(int argc, char *argv[])
  116. msgbuf_clear(&ibuf->w);
  117. free(ibuf);
  118. log_info("Terminating");
  119. + if (lconf.pid_file != NULL)
  120. + unlink(lconf.pid_file);
  121. return (0);
  122. }
  123. @@ -403,9 +424,11 @@ dispatch_imsg(struct ntpd_conf *lconf, const char *pw_dir,
  124. memcpy(&d, imsg.data, sizeof(d));
  125. ntpd_settime(d);
  126. /* daemonize now */
  127. - if (!lconf->debug)
  128. + if (!lconf->debug) {
  129. if (daemon(1, 0))
  130. fatal("daemon");
  131. + writepid(lconf);
  132. + }
  133. lconf->settime = 0;
  134. timeout = INFTIM;
  135. break;
  136. diff --git a/src/usr.sbin/ntpd/ntpd.h b/src/usr.sbin/ntpd/ntpd.h
  137. index 9ee2e42..d39628c 100644
  138. --- a/src/usr.sbin/ntpd/ntpd.h
  139. +++ b/src/usr.sbin/ntpd/ntpd.h
  140. @@ -237,6 +237,7 @@ struct ntpd_conf {
  141. u_int constraint_errors;
  142. u_int8_t *ca;
  143. size_t ca_len;
  144. + char *pid_file;
  145. };
  146. struct ctl_show_status {
  147. --
  148. 2.8.1