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.

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