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.

891 lines
20 KiB

9 years ago
9 years ago
18 years ago
18 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. /* $OpenBSD: ntpd.c,v 1.120 2019/01/14 16:30:21 florian Exp $ */
  2. /*
  3. * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
  4. * Copyright (c) 2012 Mike Miller <mmiller@mgm51.com>
  5. *
  6. * Permission to use, copy, modify, and distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. #include <sys/types.h>
  19. #include <sys/resource.h>
  20. #include <sys/socket.h>
  21. #include <sys/wait.h>
  22. #include <sys/un.h>
  23. #include <netinet/in.h>
  24. #include <errno.h>
  25. #include <poll.h>
  26. #include <pwd.h>
  27. #include <signal.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <syslog.h>
  32. #include <tls.h>
  33. #include <time.h>
  34. #include <unistd.h>
  35. #include <fcntl.h>
  36. #include <err.h>
  37. #include "ntpd.h"
  38. void sighdlr(int);
  39. __dead void usage(void);
  40. int main(int, char *[]);
  41. void check_child(void);
  42. int dispatch_imsg(struct ntpd_conf *, int, char **);
  43. int dispatch_imsg_ctl(struct ntpd_conf *);
  44. void reset_adjtime(void);
  45. int ntpd_adjtime(double);
  46. void ntpd_adjfreq(double, int);
  47. void ntpd_settime(double);
  48. void readfreq(void);
  49. int writefreq(double);
  50. void ctl_main(int, char*[]);
  51. const char *ctl_lookup_option(char *, const char **);
  52. void show_status_msg(struct imsg *);
  53. void show_peer_msg(struct imsg *, int);
  54. void show_sensor_msg(struct imsg *, int);
  55. volatile sig_atomic_t quit = 0;
  56. volatile sig_atomic_t reconfig = 0;
  57. volatile sig_atomic_t sigchld = 0;
  58. struct imsgbuf *ibuf;
  59. int timeout = INFTIM;
  60. extern u_int constraint_cnt;
  61. const char *showopt;
  62. static const char *ctl_showopt_list[] = {
  63. "peers", "Sensors", "status", "all", NULL
  64. };
  65. void
  66. sighdlr(int sig)
  67. {
  68. switch (sig) {
  69. case SIGTERM:
  70. case SIGINT:
  71. quit = 1;
  72. break;
  73. case SIGCHLD:
  74. sigchld = 1;
  75. break;
  76. case SIGHUP:
  77. reconfig = 1;
  78. break;
  79. }
  80. }
  81. __dead void
  82. usage(void)
  83. {
  84. extern char *__progname;
  85. if (strcmp(__progname, "ntpctl") == 0)
  86. fprintf(stderr,
  87. "usage: ntpctl -s all | peers | Sensors | status\n");
  88. else
  89. fprintf(stderr, "usage: %s [-dnSsv] [-f file]\n",
  90. __progname);
  91. exit(1);
  92. }
  93. #define POLL_MAX 8
  94. #define PFD_PIPE 0
  95. #define PFD_MAX 1
  96. int
  97. main(int argc, char *argv[])
  98. {
  99. struct ntpd_conf lconf;
  100. struct pollfd *pfd = NULL;
  101. pid_t pid;
  102. const char *conffile;
  103. int ch, nfds, i, j;
  104. int pipe_chld[2];
  105. extern char *__progname;
  106. u_int pfd_elms = 0, new_cnt;
  107. struct constraint *cstr;
  108. struct passwd *pw;
  109. void *newp;
  110. int argc0 = argc;
  111. char **argv0 = argv;
  112. char *pname = NULL;
  113. if (strcmp(__progname, "ntpctl") == 0) {
  114. ctl_main(argc, argv);
  115. /* NOTREACHED */
  116. }
  117. conffile = CONFFILE;
  118. memset(&lconf, 0, sizeof(lconf));
  119. while ((ch = getopt(argc, argv, "df:nP:sSv")) != -1) {
  120. switch (ch) {
  121. case 'd':
  122. lconf.debug = 2;
  123. break;
  124. case 'f':
  125. conffile = optarg;
  126. break;
  127. case 'n':
  128. lconf.debug = 2;
  129. lconf.noaction = 1;
  130. break;
  131. case 'P':
  132. pname = optarg;
  133. break;
  134. case 's':
  135. lconf.settime = 1;
  136. break;
  137. case 'S':
  138. lconf.settime = 0;
  139. break;
  140. case 'v':
  141. lconf.verbose++;
  142. break;
  143. default:
  144. usage();
  145. /* NOTREACHED */
  146. }
  147. }
  148. /* log to stderr until daemonized */
  149. log_init(lconf.debug ? lconf.debug : 1, LOG_DAEMON);
  150. argc -= optind;
  151. argv += optind;
  152. if (argc > 0)
  153. usage();
  154. if (parse_config(conffile, &lconf))
  155. exit(1);
  156. if (lconf.noaction) {
  157. fprintf(stderr, "configuration OK\n");
  158. exit(0);
  159. }
  160. if (geteuid())
  161. errx(1, "need root privileges");
  162. if ((pw = getpwnam(NTPD_USER)) == NULL)
  163. errx(1, "unknown user %s", NTPD_USER);
  164. if (pname != NULL) {
  165. /* Remove our proc arguments, so child doesn't need to. */
  166. if (sanitize_argv(&argc0, &argv0) == -1)
  167. fatalx("sanitize_argv");
  168. if (strcmp(NTP_PROC_NAME, pname) == 0)
  169. ntp_main(&lconf, pw, argc0, argv0);
  170. else if (strcmp(NTPDNS_PROC_NAME, pname) == 0)
  171. ntp_dns(&lconf, pw);
  172. else if (strcmp(CONSTRAINT_PROC_NAME, pname) == 0)
  173. priv_constraint_child(pw->pw_dir, pw->pw_uid,
  174. pw->pw_gid);
  175. else
  176. fatalx("%s: invalid process name '%s'", __func__,
  177. pname);
  178. fatalx("%s: process '%s' failed", __func__, pname);
  179. } else {
  180. if ((control_check(CTLSOCKET)) == -1)
  181. fatalx("ntpd already running");
  182. }
  183. if (setpriority(PRIO_PROCESS, 0, -20) == -1)
  184. warn("can't set priority");
  185. reset_adjtime();
  186. if (!lconf.settime) {
  187. log_init(lconf.debug, LOG_DAEMON);
  188. log_setverbose(lconf.verbose);
  189. if (!lconf.debug)
  190. if (daemon(1, 0))
  191. fatal("daemon");
  192. } else
  193. timeout = SETTIME_TIMEOUT * 1000;
  194. if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, PF_UNSPEC,
  195. pipe_chld) == -1)
  196. fatal("socketpair");
  197. if (chdir("/") == -1)
  198. fatal("chdir(\"/\")");
  199. signal(SIGCHLD, sighdlr);
  200. /* fork child process */
  201. start_child(NTP_PROC_NAME, pipe_chld[1], argc0, argv0);
  202. log_procinit("[priv]");
  203. readfreq();
  204. signal(SIGTERM, sighdlr);
  205. signal(SIGINT, sighdlr);
  206. signal(SIGHUP, sighdlr);
  207. constraint_purge();
  208. if ((ibuf = malloc(sizeof(struct imsgbuf))) == NULL)
  209. fatal(NULL);
  210. imsg_init(ibuf, pipe_chld[0]);
  211. constraint_cnt = 0;
  212. /*
  213. * Constraint processes are forked with certificates in memory,
  214. * then privdrop into chroot before speaking to the outside world.
  215. */
  216. if (unveil(tls_default_ca_cert_file(), "r") == -1)
  217. err(1, "unveil");
  218. if (unveil("/usr/sbin/ntpd", "x") == -1)
  219. err(1, "unveil");
  220. if (pledge("stdio rpath inet settime proc exec id", NULL) == -1)
  221. err(1, "pledge");
  222. while (quit == 0) {
  223. new_cnt = PFD_MAX + constraint_cnt;
  224. if (new_cnt > pfd_elms) {
  225. if ((newp = reallocarray(pfd, new_cnt,
  226. sizeof(*pfd))) == NULL) {
  227. /* panic for now */
  228. log_warn("could not resize pfd from %u -> "
  229. "%u entries", pfd_elms, new_cnt);
  230. fatalx("exiting");
  231. }
  232. pfd = newp;
  233. pfd_elms = new_cnt;
  234. }
  235. memset(pfd, 0, sizeof(*pfd) * pfd_elms);
  236. pfd[PFD_PIPE].fd = ibuf->fd;
  237. pfd[PFD_PIPE].events = POLLIN;
  238. if (ibuf->w.queued)
  239. pfd[PFD_PIPE].events |= POLLOUT;
  240. i = PFD_MAX;
  241. TAILQ_FOREACH(cstr, &conf->constraints, entry) {
  242. pfd[i].fd = cstr->fd;
  243. pfd[i].events = POLLIN;
  244. i++;
  245. }
  246. if ((nfds = poll(pfd, i, timeout)) == -1)
  247. if (errno != EINTR) {
  248. log_warn("poll error");
  249. quit = 1;
  250. }
  251. if (nfds == 0 && lconf.settime) {
  252. lconf.settime = 0;
  253. timeout = INFTIM;
  254. log_init(lconf.debug, LOG_DAEMON);
  255. log_setverbose(lconf.verbose);
  256. log_warnx("no reply received in time, skipping initial "
  257. "time setting");
  258. if (!lconf.debug)
  259. if (daemon(1, 0))
  260. fatal("daemon");
  261. }
  262. if (nfds > 0 && (pfd[PFD_PIPE].revents & POLLOUT))
  263. if (msgbuf_write(&ibuf->w) <= 0 && errno != EAGAIN) {
  264. log_warn("pipe write error (to child)");
  265. quit = 1;
  266. }
  267. if (nfds > 0 && pfd[PFD_PIPE].revents & POLLIN) {
  268. nfds--;
  269. if (dispatch_imsg(&lconf, argc0, argv0) == -1)
  270. quit = 1;
  271. }
  272. for (j = PFD_MAX; nfds > 0 && j < i; j++) {
  273. nfds -= priv_constraint_dispatch(&pfd[j]);
  274. }
  275. if (sigchld) {
  276. check_child();
  277. sigchld = 0;
  278. }
  279. }
  280. signal(SIGCHLD, SIG_DFL);
  281. /* Close socket and start shutdown. */
  282. close(ibuf->fd);
  283. do {
  284. if ((pid = wait(NULL)) == -1 &&
  285. errno != EINTR && errno != ECHILD)
  286. fatal("wait");
  287. } while (pid != -1 || (pid == -1 && errno == EINTR));
  288. msgbuf_clear(&ibuf->w);
  289. free(ibuf);
  290. log_info("Terminating");
  291. return (0);
  292. }
  293. void
  294. check_child(void)
  295. {
  296. int status;
  297. pid_t pid;
  298. do {
  299. pid = waitpid(WAIT_ANY, &status, WNOHANG);
  300. if (pid <= 0)
  301. continue;
  302. priv_constraint_check_child(pid, status);
  303. } while (pid > 0 || (pid == -1 && errno == EINTR));
  304. }
  305. int
  306. dispatch_imsg(struct ntpd_conf *lconf, int argc, char **argv)
  307. {
  308. struct imsg imsg;
  309. int n;
  310. double d;
  311. if (((n = imsg_read(ibuf)) == -1 && errno != EAGAIN) || n == 0)
  312. return (-1);
  313. for (;;) {
  314. if ((n = imsg_get(ibuf, &imsg)) == -1)
  315. return (-1);
  316. if (n == 0)
  317. break;
  318. switch (imsg.hdr.type) {
  319. case IMSG_ADJTIME:
  320. if (imsg.hdr.len != IMSG_HEADER_SIZE + sizeof(d))
  321. fatalx("invalid IMSG_ADJTIME received");
  322. memcpy(&d, imsg.data, sizeof(d));
  323. n = ntpd_adjtime(d);
  324. imsg_compose(ibuf, IMSG_ADJTIME, 0, 0, -1,
  325. &n, sizeof(n));
  326. break;
  327. case IMSG_ADJFREQ:
  328. if (imsg.hdr.len != IMSG_HEADER_SIZE + sizeof(d))
  329. fatalx("invalid IMSG_ADJFREQ received");
  330. memcpy(&d, imsg.data, sizeof(d));
  331. ntpd_adjfreq(d, 1);
  332. break;
  333. case IMSG_SETTIME:
  334. if (imsg.hdr.len != IMSG_HEADER_SIZE + sizeof(d))
  335. fatalx("invalid IMSG_SETTIME received");
  336. if (!lconf->settime)
  337. break;
  338. log_init(lconf->debug, LOG_DAEMON);
  339. log_setverbose(lconf->verbose);
  340. memcpy(&d, imsg.data, sizeof(d));
  341. ntpd_settime(d);
  342. /* daemonize now */
  343. if (!lconf->debug)
  344. if (daemon(1, 0))
  345. fatal("daemon");
  346. lconf->settime = 0;
  347. timeout = INFTIM;
  348. break;
  349. case IMSG_CONSTRAINT_QUERY:
  350. priv_constraint_msg(imsg.hdr.peerid,
  351. imsg.data, imsg.hdr.len - IMSG_HEADER_SIZE,
  352. argc, argv);
  353. break;
  354. case IMSG_CONSTRAINT_KILL:
  355. priv_constraint_kill(imsg.hdr.peerid);
  356. break;
  357. default:
  358. break;
  359. }
  360. imsg_free(&imsg);
  361. }
  362. return (0);
  363. }
  364. void
  365. reset_adjtime(void)
  366. {
  367. struct timeval tv;
  368. timerclear(&tv);
  369. if (adjtime(&tv, NULL) == -1)
  370. log_warn("reset adjtime failed");
  371. }
  372. int
  373. ntpd_adjtime(double d)
  374. {
  375. struct timeval tv, olddelta;
  376. int synced = 0;
  377. static int firstadj = 1;
  378. d += getoffset();
  379. if (d >= (double)LOG_NEGLIGIBLE_ADJTIME / 1000 ||
  380. d <= -1 * (double)LOG_NEGLIGIBLE_ADJTIME / 1000)
  381. log_info("adjusting local clock by %fs", d);
  382. else
  383. log_debug("adjusting local clock by %fs", d);
  384. d_to_tv(d, &tv);
  385. if (adjtime(&tv, &olddelta) == -1)
  386. log_warn("adjtime failed");
  387. else if (!firstadj && olddelta.tv_sec == 0 && olddelta.tv_usec == 0)
  388. synced = 1;
  389. firstadj = 0;
  390. return (synced);
  391. }
  392. void
  393. ntpd_adjfreq(double relfreq, int wrlog)
  394. {
  395. int64_t curfreq;
  396. double ppmfreq;
  397. int r;
  398. if (adjfreq(NULL, &curfreq) == -1) {
  399. log_warn("adjfreq failed");
  400. return;
  401. }
  402. /*
  403. * adjfreq's unit is ns/s shifted left 32; convert relfreq to
  404. * that unit before adding. We log values in part per million.
  405. */
  406. curfreq += relfreq * 1e9 * (1LL << 32);
  407. r = writefreq(curfreq / 1e9 / (1LL << 32));
  408. ppmfreq = relfreq * 1e6;
  409. if (wrlog) {
  410. if (ppmfreq >= LOG_NEGLIGIBLE_ADJFREQ ||
  411. ppmfreq <= -LOG_NEGLIGIBLE_ADJFREQ)
  412. log_info("adjusting clock frequency by %f to %fppm%s",
  413. ppmfreq, curfreq / 1e3 / (1LL << 32),
  414. r ? "" : " (no drift file)");
  415. else
  416. log_debug("adjusting clock frequency by %f to %fppm%s",
  417. ppmfreq, curfreq / 1e3 / (1LL << 32),
  418. r ? "" : " (no drift file)");
  419. }
  420. if (adjfreq(&curfreq, NULL) == -1)
  421. log_warn("adjfreq failed");
  422. }
  423. void
  424. ntpd_settime(double d)
  425. {
  426. struct timeval tv, curtime;
  427. char buf[80];
  428. time_t tval;
  429. if (gettimeofday(&curtime, NULL) == -1) {
  430. log_warn("gettimeofday");
  431. return;
  432. }
  433. d_to_tv(d, &tv);
  434. curtime.tv_usec += tv.tv_usec + 1000000;
  435. curtime.tv_sec += tv.tv_sec - 1 + (curtime.tv_usec / 1000000);
  436. curtime.tv_usec %= 1000000;
  437. if (settimeofday(&curtime, NULL) == -1) {
  438. log_warn("settimeofday");
  439. return;
  440. }
  441. tval = curtime.tv_sec;
  442. strftime(buf, sizeof(buf), "%a %b %e %H:%M:%S %Z %Y",
  443. localtime(&tval));
  444. log_info("set local clock to %s (offset %fs)", buf, d);
  445. }
  446. static FILE *freqfp;
  447. void
  448. readfreq(void)
  449. {
  450. int64_t current;
  451. int fd;
  452. double d;
  453. fd = open(DRIFTFILE, O_RDWR);
  454. if (fd == -1) {
  455. log_warnx("creating new %s", DRIFTFILE);
  456. current = 0;
  457. if (adjfreq(&current, NULL) == -1)
  458. log_warn("adjfreq reset failed");
  459. freqfp = fopen(DRIFTFILE, "w");
  460. return;
  461. }
  462. freqfp = fdopen(fd, "r+");
  463. /* if we're adjusting frequency already, don't override */
  464. if (adjfreq(NULL, &current) == -1)
  465. log_warn("adjfreq failed");
  466. else if (current == 0 && freqfp) {
  467. if (fscanf(freqfp, "%lf", &d) == 1) {
  468. d /= 1e6; /* scale from ppm */
  469. ntpd_adjfreq(d, 0);
  470. } else
  471. log_warnx("%s is empty", DRIFTFILE);
  472. }
  473. }
  474. int
  475. writefreq(double d)
  476. {
  477. int r;
  478. static int warnonce = 1;
  479. if (freqfp == NULL)
  480. return 0;
  481. rewind(freqfp);
  482. r = fprintf(freqfp, "%.3f\n", d * 1e6); /* scale to ppm */
  483. if (r < 0 || fflush(freqfp) != 0) {
  484. if (warnonce) {
  485. log_warnx("can't write %s", DRIFTFILE);
  486. warnonce = 0;
  487. }
  488. clearerr(freqfp);
  489. return 0;
  490. }
  491. ftruncate(fileno(freqfp), ftello(freqfp));
  492. fsync(fileno(freqfp));
  493. return 1;
  494. }
  495. void
  496. ctl_main(int argc, char *argv[])
  497. {
  498. struct sockaddr_un sa;
  499. struct imsg imsg;
  500. struct imsgbuf *ibuf_ctl;
  501. int fd, n, done, ch, action;
  502. char *sockname;
  503. sockname = CTLSOCKET;
  504. if (argc < 2) {
  505. usage();
  506. /* NOTREACHED */
  507. }
  508. while ((ch = getopt(argc, argv, "s:")) != -1) {
  509. switch (ch) {
  510. case 's':
  511. showopt = ctl_lookup_option(optarg, ctl_showopt_list);
  512. if (showopt == NULL) {
  513. warnx("Unknown show modifier '%s'", optarg);
  514. usage();
  515. }
  516. break;
  517. default:
  518. usage();
  519. /* NOTREACHED */
  520. }
  521. }
  522. action = -1;
  523. if (showopt != NULL) {
  524. switch (*showopt) {
  525. case 'p':
  526. action = CTL_SHOW_PEERS;
  527. break;
  528. case 's':
  529. action = CTL_SHOW_STATUS;
  530. break;
  531. case 'S':
  532. action = CTL_SHOW_SENSORS;
  533. break;
  534. case 'a':
  535. action = CTL_SHOW_ALL;
  536. break;
  537. }
  538. }
  539. if (action == -1)
  540. usage();
  541. /* NOTREACHED */
  542. if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
  543. err(1, "ntpctl: socket");
  544. memset(&sa, 0, sizeof(sa));
  545. sa.sun_family = AF_UNIX;
  546. if (strlcpy(sa.sun_path, sockname, sizeof(sa.sun_path)) >=
  547. sizeof(sa.sun_path))
  548. errx(1, "ctl socket name too long");
  549. if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1)
  550. err(1, "connect: %s", sockname);
  551. if (pledge("stdio", NULL) == -1)
  552. err(1, "pledge");
  553. if ((ibuf_ctl = malloc(sizeof(struct imsgbuf))) == NULL)
  554. err(1, NULL);
  555. imsg_init(ibuf_ctl, fd);
  556. switch (action) {
  557. case CTL_SHOW_STATUS:
  558. imsg_compose(ibuf_ctl, IMSG_CTL_SHOW_STATUS,
  559. 0, 0, -1, NULL, 0);
  560. break;
  561. case CTL_SHOW_PEERS:
  562. imsg_compose(ibuf_ctl, IMSG_CTL_SHOW_PEERS,
  563. 0, 0, -1, NULL, 0);
  564. break;
  565. case CTL_SHOW_SENSORS:
  566. imsg_compose(ibuf_ctl, IMSG_CTL_SHOW_SENSORS,
  567. 0, 0, -1, NULL, 0);
  568. break;
  569. case CTL_SHOW_ALL:
  570. imsg_compose(ibuf_ctl, IMSG_CTL_SHOW_ALL,
  571. 0, 0, -1, NULL, 0);
  572. break;
  573. default:
  574. errx(1, "invalid action");
  575. break; /* NOTREACHED */
  576. }
  577. while (ibuf_ctl->w.queued)
  578. if (msgbuf_write(&ibuf_ctl->w) <= 0 && errno != EAGAIN)
  579. err(1, "ibuf_ctl: msgbuf_write error");
  580. done = 0;
  581. while (!done) {
  582. if ((n = imsg_read(ibuf_ctl)) == -1 && errno != EAGAIN)
  583. err(1, "ibuf_ctl: imsg_read error");
  584. if (n == 0)
  585. errx(1, "ntpctl: pipe closed");
  586. while (!done) {
  587. if ((n = imsg_get(ibuf_ctl, &imsg)) == -1)
  588. err(1, "ibuf_ctl: imsg_get error");
  589. if (n == 0)
  590. break;
  591. switch (action) {
  592. case CTL_SHOW_STATUS:
  593. show_status_msg(&imsg);
  594. done = 1;
  595. break;
  596. case CTL_SHOW_PEERS:
  597. show_peer_msg(&imsg, 0);
  598. if (imsg.hdr.type ==
  599. IMSG_CTL_SHOW_PEERS_END)
  600. done = 1;
  601. break;
  602. case CTL_SHOW_SENSORS:
  603. show_sensor_msg(&imsg, 0);
  604. if (imsg.hdr.type ==
  605. IMSG_CTL_SHOW_SENSORS_END)
  606. done = 1;
  607. break;
  608. case CTL_SHOW_ALL:
  609. switch (imsg.hdr.type) {
  610. case IMSG_CTL_SHOW_STATUS:
  611. show_status_msg(&imsg);
  612. break;
  613. case IMSG_CTL_SHOW_PEERS:
  614. show_peer_msg(&imsg, 1);
  615. break;
  616. case IMSG_CTL_SHOW_SENSORS:
  617. show_sensor_msg(&imsg, 1);
  618. break;
  619. case IMSG_CTL_SHOW_PEERS_END:
  620. case IMSG_CTL_SHOW_SENSORS_END:
  621. /* do nothing */
  622. break;
  623. case IMSG_CTL_SHOW_ALL_END:
  624. done=1;
  625. break;
  626. default:
  627. /* no action taken */
  628. break;
  629. }
  630. default:
  631. /* no action taken */
  632. break;
  633. }
  634. imsg_free(&imsg);
  635. }
  636. }
  637. close(fd);
  638. free(ibuf_ctl);
  639. exit(0);
  640. }
  641. const char *
  642. ctl_lookup_option(char *cmd, const char **list)
  643. {
  644. const char *item = NULL;
  645. if (cmd != NULL && *cmd)
  646. for (; *list; list++)
  647. if (!strncmp(cmd, *list, strlen(cmd))) {
  648. if (item == NULL)
  649. item = *list;
  650. else
  651. errx(1, "%s is ambiguous", cmd);
  652. }
  653. return (item);
  654. }
  655. void
  656. show_status_msg(struct imsg *imsg)
  657. {
  658. struct ctl_show_status *cstatus;
  659. double clock_offset;
  660. struct timeval tv;
  661. if (imsg->hdr.len != IMSG_HEADER_SIZE + sizeof(struct ctl_show_status))
  662. fatalx("invalid IMSG_CTL_SHOW_STATUS received");
  663. cstatus = (struct ctl_show_status *)imsg->data;
  664. if (cstatus->peercnt > 0)
  665. printf("%d/%d peers valid, ",
  666. cstatus->valid_peers, cstatus->peercnt);
  667. if (cstatus->sensorcnt > 0)
  668. printf("%d/%d sensors valid, ",
  669. cstatus->valid_sensors, cstatus->sensorcnt);
  670. if (cstatus->constraint_median) {
  671. tv.tv_sec = cstatus->constraint_median +
  672. (getmonotime() - cstatus->constraint_last);
  673. tv.tv_usec = 0;
  674. d_to_tv(gettime_from_timeval(&tv) - gettime(), &tv);
  675. printf("constraint offset %llds", (long long)tv.tv_sec);
  676. if (cstatus->constraint_errors)
  677. printf(" (%d errors)",
  678. cstatus->constraint_errors);
  679. printf(", ");
  680. }
  681. if (cstatus->peercnt + cstatus->sensorcnt == 0)
  682. printf("no peers and no sensors configured\n");
  683. if (cstatus->synced == 1)
  684. printf("clock synced, stratum %u\n", cstatus->stratum);
  685. else {
  686. printf("clock unsynced");
  687. clock_offset = cstatus->clock_offset < 0 ?
  688. -1.0 * cstatus->clock_offset : cstatus->clock_offset;
  689. if (clock_offset > 5e-7)
  690. printf(", clock offset is %.3fms\n",
  691. cstatus->clock_offset);
  692. else
  693. printf("\n");
  694. }
  695. }
  696. void
  697. show_peer_msg(struct imsg *imsg, int calledfromshowall)
  698. {
  699. struct ctl_show_peer *cpeer;
  700. int cnt;
  701. char stratum[3];
  702. static int firsttime = 1;
  703. if (imsg->hdr.type == IMSG_CTL_SHOW_PEERS_END) {
  704. if (imsg->hdr.len != IMSG_HEADER_SIZE + sizeof(cnt))
  705. fatalx("invalid IMSG_CTL_SHOW_PEERS_END received");
  706. memcpy(&cnt, imsg->data, sizeof(cnt));
  707. if (cnt == 0)
  708. printf("no peers configured\n");
  709. return;
  710. }
  711. if (imsg->hdr.len != IMSG_HEADER_SIZE + sizeof(struct ctl_show_peer))
  712. fatalx("invalid IMSG_CTL_SHOW_PEERS received");
  713. cpeer = (struct ctl_show_peer *)imsg->data;
  714. if (strlen(cpeer->peer_desc) > MAX_DISPLAY_WIDTH - 1)
  715. fatalx("peer_desc is too long");
  716. if (firsttime) {
  717. firsttime = 0;
  718. if (calledfromshowall)
  719. printf("\n");
  720. printf("peer\n wt tl st next poll "
  721. "offset delay jitter\n");
  722. }
  723. if (cpeer->stratum > 0)
  724. snprintf(stratum, sizeof(stratum), "%2u", cpeer->stratum);
  725. else
  726. strlcpy(stratum, " -", sizeof (stratum));
  727. printf("%s\n %1s %2u %2u %2s %4llds %4llds",
  728. cpeer->peer_desc, cpeer->syncedto == 1 ? "*" : " ",
  729. cpeer->weight, cpeer->trustlevel, stratum,
  730. (long long)cpeer->next, (long long)cpeer->poll);
  731. if (cpeer->trustlevel >= TRUSTLEVEL_BADPEER)
  732. printf(" %12.3fms %9.3fms %8.3fms\n", cpeer->offset,
  733. cpeer->delay, cpeer->jitter);
  734. else
  735. printf(" ---- peer not valid ----\n");
  736. }
  737. void
  738. show_sensor_msg(struct imsg *imsg, int calledfromshowall)
  739. {
  740. struct ctl_show_sensor *csensor;
  741. int cnt;
  742. static int firsttime = 1;
  743. if (imsg->hdr.type == IMSG_CTL_SHOW_SENSORS_END) {
  744. if (imsg->hdr.len != IMSG_HEADER_SIZE + sizeof(cnt))
  745. fatalx("invalid IMSG_CTL_SHOW_SENSORS_END received");
  746. memcpy(&cnt, imsg->data, sizeof(cnt));
  747. if (cnt == 0)
  748. printf("no sensors configured\n");
  749. return;
  750. }
  751. if (imsg->hdr.len != IMSG_HEADER_SIZE + sizeof(struct ctl_show_sensor))
  752. fatalx("invalid IMSG_CTL_SHOW_SENSORS received");
  753. csensor = (struct ctl_show_sensor *)imsg->data;
  754. if (strlen(csensor->sensor_desc) > MAX_DISPLAY_WIDTH - 1)
  755. fatalx("sensor_desc is too long");
  756. if (firsttime) {
  757. firsttime = 0;
  758. if (calledfromshowall)
  759. printf("\n");
  760. printf("sensor\n wt gd st next poll "
  761. "offset correction\n");
  762. }
  763. printf("%s\n %1s %2u %2u %2u %4llds %4llds",
  764. csensor->sensor_desc, csensor->syncedto == 1 ? "*" : " ",
  765. csensor->weight, csensor->good, csensor->stratum,
  766. (long long)csensor->next, (long long)csensor->poll);
  767. if (csensor->good == 1)
  768. printf(" %11.3fms %9.3fms\n",
  769. csensor->offset, csensor->correction);
  770. else
  771. printf(" - sensor not valid -\n");
  772. }