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.

154 lines
3.8 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
  1. From cb6da547c5b532d6e8d241cb993c831605d0d45f 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, 36 insertions(+), 4 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 b6eb98e..608e6fd 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. @@ -191,10 +206,16 @@ main(int argc, char *argv[])
  83. reset_adjtime();
  84. if (!lconf.settime) {
  85. log_init(lconf.debug, LOG_DAEMON);
  86. +<<<<<<< 00fff5a8c36c3b3574abf8bd4bb1a558d1f8232d
  87. log_verbose(lconf.verbose);
  88. if (!lconf.debug)
  89. +=======
  90. + if (!lconf.debug) {
  91. +>>>>>>> add -p option to create a pid file
  92. if (daemon(1, 0))
  93. fatal("daemon");
  94. + writepid(&lconf);
  95. + }
  96. } else
  97. timeout = SETTIME_TIMEOUT * 1000;
  98. @@ -273,9 +294,11 @@ main(int argc, char *argv[])
  99. log_verbose(lconf.verbose);
  100. log_warnx("no reply received in time, skipping initial "
  101. "time setting");
  102. - if (!lconf.debug)
  103. + if (!lconf.debug) {
  104. if (daemon(1, 0))
  105. fatal("daemon");
  106. + writepid(&lconf);
  107. + }
  108. }
  109. if (nfds > 0 && (pfd[PFD_PIPE].revents & POLLOUT))
  110. @@ -318,6 +341,8 @@ main(int argc, char *argv[])
  111. msgbuf_clear(&ibuf->w);
  112. free(ibuf);
  113. log_info("Terminating");
  114. + if (lconf.pid_file != NULL)
  115. + unlink(lconf.pid_file);
  116. return (0);
  117. }
  118. @@ -401,9 +426,11 @@ dispatch_imsg(struct ntpd_conf *lconf, const char *pw_dir,
  119. memcpy(&d, imsg.data, sizeof(d));
  120. ntpd_settime(d);
  121. /* daemonize now */
  122. - if (!lconf->debug)
  123. + if (!lconf->debug) {
  124. if (daemon(1, 0))
  125. fatal("daemon");
  126. + writepid(lconf);
  127. + }
  128. lconf->settime = 0;
  129. timeout = INFTIM;
  130. break;
  131. diff --git a/src/usr.sbin/ntpd/ntpd.h b/src/usr.sbin/ntpd/ntpd.h
  132. index dfcaf33..24a3222 100644
  133. --- a/src/usr.sbin/ntpd/ntpd.h
  134. +++ b/src/usr.sbin/ntpd/ntpd.h
  135. @@ -243,6 +243,7 @@ struct ntpd_conf {
  136. u_int constraint_errors;
  137. u_int8_t *ca;
  138. size_t ca_len;
  139. + char *pid_file;
  140. };
  141. struct ctl_show_status {
  142. --
  143. 2.7.0