Source code pulled from OpenBSD for OpenNTPD. The place to contribute to this code is via the OpenBSD CVS tree.
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.

364 lines
7.7 KiB

  1. /* $OpenBSD: ntpd.c,v 1.39 2005/07/11 08:08:06 dtucker Exp $ */
  2. /*
  3. * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
  14. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  15. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include <sys/types.h>
  18. #include <sys/socket.h>
  19. #include <sys/wait.h>
  20. #include <netinet/in.h>
  21. #include <errno.h>
  22. #include <poll.h>
  23. #include <pwd.h>
  24. #include <resolv.h>
  25. #include <signal.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <unistd.h>
  30. #include "ntpd.h"
  31. void sighdlr(int);
  32. __dead void usage(void);
  33. int main(int, char *[]);
  34. int check_child(pid_t, const char *);
  35. int dispatch_imsg(struct ntpd_conf *);
  36. int ntpd_adjtime(double);
  37. void ntpd_settime(double);
  38. volatile sig_atomic_t quit = 0;
  39. volatile sig_atomic_t reconfig = 0;
  40. volatile sig_atomic_t sigchld = 0;
  41. struct imsgbuf *ibuf;
  42. void
  43. sighdlr(int sig)
  44. {
  45. switch (sig) {
  46. case SIGTERM:
  47. case SIGINT:
  48. quit = 1;
  49. break;
  50. case SIGCHLD:
  51. sigchld = 1;
  52. break;
  53. case SIGHUP:
  54. reconfig = 1;
  55. break;
  56. }
  57. }
  58. __dead void
  59. usage(void)
  60. {
  61. extern char *__progname;
  62. fprintf(stderr, "usage: %s [-dSs] [-f file]\n", __progname);
  63. exit(1);
  64. }
  65. #define POLL_MAX 8
  66. #define PFD_PIPE 0
  67. int
  68. main(int argc, char *argv[])
  69. {
  70. struct ntpd_conf conf;
  71. struct pollfd pfd[POLL_MAX];
  72. pid_t chld_pid = 0, pid;
  73. const char *conffile;
  74. int ch, nfds, timeout = INFTIM;
  75. int pipe_chld[2];
  76. conffile = CONFFILE;
  77. bzero(&conf, sizeof(conf));
  78. log_init(1); /* log to stderr until daemonized */
  79. res_init(); /* XXX */
  80. while ((ch = getopt(argc, argv, "df:sS")) != -1) {
  81. switch (ch) {
  82. case 'd':
  83. conf.debug = 1;
  84. break;
  85. case 'f':
  86. conffile = optarg;
  87. break;
  88. case 's':
  89. conf.settime = 1;
  90. break;
  91. case 'S':
  92. conf.settime = 0;
  93. break;
  94. default:
  95. usage();
  96. /* NOTREACHED */
  97. }
  98. }
  99. if (parse_config(conffile, &conf))
  100. exit(1);
  101. if (geteuid()) {
  102. fprintf(stderr, "ntpd: need root privileges\n");
  103. exit(1);
  104. }
  105. if (getpwnam(NTPD_USER) == NULL) {
  106. fprintf(stderr, "ntpd: unknown user %s\n", NTPD_USER);
  107. exit(1);
  108. }
  109. endpwent();
  110. if (!conf.settime) {
  111. log_init(conf.debug);
  112. if (!conf.debug)
  113. if (daemon(1, 0))
  114. fatal("daemon");
  115. } else
  116. timeout = SETTIME_TIMEOUT * 1000;
  117. if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_chld) == -1)
  118. fatal("socketpair");
  119. /* fork child process */
  120. chld_pid = ntp_main(pipe_chld, &conf);
  121. setproctitle("[priv]");
  122. signal(SIGTERM, sighdlr);
  123. signal(SIGINT, sighdlr);
  124. signal(SIGCHLD, sighdlr);
  125. signal(SIGHUP, sighdlr);
  126. close(pipe_chld[1]);
  127. if ((ibuf = malloc(sizeof(struct imsgbuf))) == NULL)
  128. fatal(NULL);
  129. imsg_init(ibuf, pipe_chld[0]);
  130. while (quit == 0) {
  131. pfd[PFD_PIPE].fd = ibuf->fd;
  132. pfd[PFD_PIPE].events = POLLIN;
  133. if (ibuf->w.queued)
  134. pfd[PFD_PIPE].events |= POLLOUT;
  135. if ((nfds = poll(pfd, 1, timeout)) == -1)
  136. if (errno != EINTR) {
  137. log_warn("poll error");
  138. quit = 1;
  139. }
  140. if (nfds == 0 && conf.settime) {
  141. conf.settime = 0;
  142. timeout = INFTIM;
  143. log_init(conf.debug);
  144. log_debug("no reply received in time, skipping initial "
  145. "time setting");
  146. if (!conf.debug)
  147. if (daemon(1, 0))
  148. fatal("daemon");
  149. }
  150. if (nfds > 0 && (pfd[PFD_PIPE].revents & POLLOUT))
  151. if (msgbuf_write(&ibuf->w) < 0) {
  152. log_warn("pipe write error (to child)");
  153. quit = 1;
  154. }
  155. if (nfds > 0 && pfd[PFD_PIPE].revents & POLLIN) {
  156. nfds--;
  157. if (dispatch_imsg(&conf) == -1)
  158. quit = 1;
  159. }
  160. if (sigchld) {
  161. if (check_child(chld_pid, "child")) {
  162. quit = 1;
  163. chld_pid = 0;
  164. }
  165. sigchld = 0;
  166. }
  167. }
  168. signal(SIGCHLD, SIG_DFL);
  169. if (chld_pid)
  170. kill(chld_pid, SIGTERM);
  171. do {
  172. if ((pid = wait(NULL)) == -1 &&
  173. errno != EINTR && errno != ECHILD)
  174. fatal("wait");
  175. } while (pid != -1 || (pid == -1 && errno == EINTR));
  176. msgbuf_clear(&ibuf->w);
  177. free(ibuf);
  178. log_info("Terminating");
  179. return (0);
  180. }
  181. int
  182. check_child(pid_t pid, const char *pname)
  183. {
  184. int status, sig;
  185. char *signame;
  186. if (waitpid(pid, &status, WNOHANG) > 0) {
  187. if (WIFEXITED(status)) {
  188. log_warnx("Lost child: %s exited", pname);
  189. return (1);
  190. }
  191. if (WIFSIGNALED(status)) {
  192. sig = WTERMSIG(status);
  193. signame = strsignal(sig) ? strsignal(sig) : "unknown";
  194. log_warnx("Lost child: %s terminated; signal %d (%s)",
  195. pname, sig, signame);
  196. return (1);
  197. }
  198. }
  199. return (0);
  200. }
  201. int
  202. dispatch_imsg(struct ntpd_conf *conf)
  203. {
  204. struct imsg imsg;
  205. int n, cnt;
  206. double d;
  207. char *name;
  208. struct ntp_addr *h, *hn;
  209. struct buf *buf;
  210. if ((n = imsg_read(ibuf)) == -1)
  211. return (-1);
  212. if (n == 0) { /* connection closed */
  213. log_warnx("dispatch_imsg in main: pipe closed");
  214. return (-1);
  215. }
  216. for (;;) {
  217. if ((n = imsg_get(ibuf, &imsg)) == -1)
  218. return (-1);
  219. if (n == 0)
  220. break;
  221. switch (imsg.hdr.type) {
  222. case IMSG_ADJTIME:
  223. if (imsg.hdr.len != IMSG_HEADER_SIZE + sizeof(d))
  224. fatalx("invalid IMSG_ADJTIME received");
  225. memcpy(&d, imsg.data, sizeof(d));
  226. n = ntpd_adjtime(d);
  227. imsg_compose(ibuf, IMSG_ADJTIME, 0, 0, &n, sizeof(n));
  228. break;
  229. case IMSG_SETTIME:
  230. if (imsg.hdr.len != IMSG_HEADER_SIZE + sizeof(d))
  231. fatalx("invalid IMSG_SETTIME received");
  232. if (!conf->settime)
  233. break;
  234. memcpy(&d, imsg.data, sizeof(d));
  235. ntpd_settime(d);
  236. /* daemonize now */
  237. log_init(conf->debug);
  238. if (!conf->debug)
  239. if (daemon(1, 0))
  240. fatal("daemon");
  241. conf->settime = 0;
  242. break;
  243. case IMSG_HOST_DNS:
  244. name = imsg.data;
  245. if (imsg.hdr.len < 1 + IMSG_HEADER_SIZE)
  246. fatalx("invalid IMSG_HOST_DNS received");
  247. imsg.hdr.len -= 1 + IMSG_HEADER_SIZE;
  248. if (name[imsg.hdr.len] != '\0' ||
  249. strlen(name) != imsg.hdr.len)
  250. fatalx("invalid IMSG_HOST_DNS received");
  251. cnt = host_dns(name, &hn);
  252. buf = imsg_create(ibuf, IMSG_HOST_DNS,
  253. imsg.hdr.peerid, 0,
  254. cnt * sizeof(struct sockaddr_storage));
  255. if (buf == NULL)
  256. break;
  257. if (cnt > 0)
  258. for (h = hn; h != NULL; h = h->next)
  259. imsg_add(buf, &h->ss, sizeof(h->ss));
  260. imsg_close(ibuf, buf);
  261. break;
  262. default:
  263. break;
  264. }
  265. imsg_free(&imsg);
  266. }
  267. return (0);
  268. }
  269. int
  270. ntpd_adjtime(double d)
  271. {
  272. struct timeval tv, olddelta;
  273. int synced = 0;
  274. static int firstadj = 1;
  275. if (d >= (double)LOG_NEGLIGEE / 1000 ||
  276. d <= -1 * (double)LOG_NEGLIGEE / 1000)
  277. log_info("adjusting local clock by %fs", d);
  278. else
  279. log_debug("adjusting local clock by %fs", d);
  280. d_to_tv(d, &tv);
  281. if (adjtime(&tv, &olddelta) == -1)
  282. log_warn("adjtime failed");
  283. else if (!firstadj && olddelta.tv_sec == 0 && olddelta.tv_usec == 0)
  284. synced = 1;
  285. firstadj = 0;
  286. return (synced);
  287. }
  288. void
  289. ntpd_settime(double d)
  290. {
  291. struct timeval tv, curtime;
  292. char buf[80];
  293. time_t tval;
  294. /* if the offset is small, don't call settimeofday */
  295. if (d < SETTIME_MIN_OFFSET && d > -SETTIME_MIN_OFFSET)
  296. return;
  297. if (gettimeofday(&curtime, NULL) == -1) {
  298. log_warn("gettimeofday");
  299. return;
  300. }
  301. d_to_tv(d, &tv);
  302. curtime.tv_usec += tv.tv_usec + 1000000;
  303. curtime.tv_sec += tv.tv_sec - 1 + (curtime.tv_usec / 1000000);
  304. curtime.tv_usec %= 1000000;
  305. if (settimeofday(&curtime, NULL) == -1) {
  306. log_warn("settimeofday");
  307. return;
  308. }
  309. tval = curtime.tv_sec;
  310. strftime(buf, sizeof(buf), "%a %b %e %H:%M:%S %Z %Y",
  311. localtime(&tval));
  312. log_info("set local clock to %s (offset %fs)", buf, d);
  313. }