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

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
10 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. From 39b0d8bc55f07cc2d15c7a60ee77c41e00b8c9b1 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 08/10] 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 18b12e8..9eb1fee 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. @@ -56,6 +57,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 a02d9f5..304e050 100644
  36. --- a/src/usr.sbin/ntpd/ntpd.c
  37. +++ b/src/usr.sbin/ntpd/ntpd.c
  38. @@ -83,6 +83,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. @@ -92,7 +104,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. @@ -123,7 +135,7 @@ main(int argc, char *argv[])
  66. log_init(1); /* log to stderr until daemonized */
  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 = 1;
  72. @@ -135,6 +147,9 @@ main(int argc, char *argv[])
  73. case 'n':
  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. @@ -175,9 +190,11 @@ main(int argc, char *argv[])
  83. reset_adjtime();
  84. if (!lconf.settime) {
  85. log_init(lconf.debug);
  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. @@ -224,9 +241,11 @@ main(int argc, char *argv[])
  95. log_init(lconf.debug);
  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. @@ -265,6 +284,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. @@ -337,9 +358,11 @@ dispatch_imsg(struct ntpd_conf *lconf)
  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 cfbd603..8f13b00 100644
  129. --- a/src/usr.sbin/ntpd/ntpd.h
  130. +++ b/src/usr.sbin/ntpd/ntpd.h
  131. @@ -237,6 +237,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. 1.9.1