File: | src/usr.sbin/ntpd/control.c |
Warning: | line 305, column 7 Although the value stored to 'flags' is used in the enclosing expression, the value is never actually read from 'flags' |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* $OpenBSD: control.c,v 1.20 2023/12/20 15:36:36 otto Exp $ */ |
2 | |
3 | /* |
4 | * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> |
5 | * Copyright (c) 2012 Mike Miller <mmiller@mgm51.com> |
6 | * |
7 | * Permission to use, copy, modify, and distribute this software for any |
8 | * purpose with or without fee is hereby granted, provided that the above |
9 | * copyright notice and this permission notice appear in all copies. |
10 | * |
11 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
12 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
13 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
14 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
15 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
16 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
17 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
18 | */ |
19 | |
20 | #include <sys/types.h> |
21 | #include <sys/stat.h> |
22 | #include <sys/socket.h> |
23 | #include <sys/un.h> |
24 | #include <errno(*__errno()).h> |
25 | #include <math.h> |
26 | #include <stdio.h> |
27 | #include <stdlib.h> |
28 | #include <string.h> |
29 | #include <unistd.h> |
30 | #include <fcntl.h> |
31 | #include <err.h> |
32 | |
33 | #include "ntpd.h" |
34 | |
35 | #define CONTROL_BACKLOG5 5 |
36 | |
37 | #define square(x)((x) * (x)) ((x) * (x)) |
38 | |
39 | int |
40 | control_check(char *path) |
41 | { |
42 | struct sockaddr_un sun; |
43 | int fd; |
44 | |
45 | bzero(&sun, sizeof(sun)); |
46 | sun.sun_family = AF_UNIX1; |
47 | strlcpy(sun.sun_path, path, sizeof(sun.sun_path)); |
48 | |
49 | if ((fd = socket(AF_UNIX1, SOCK_STREAM1, 0)) == -1) { |
50 | log_debug("control_check: socket check"); |
51 | return (-1); |
52 | } |
53 | |
54 | if (connect(fd, (struct sockaddr *)&sun, sizeof(sun)) == 0) { |
55 | log_debug("control_check: socket in use"); |
56 | close(fd); |
57 | return (-1); |
58 | } |
59 | |
60 | close(fd); |
61 | |
62 | return (0); |
63 | } |
64 | |
65 | int |
66 | control_init(char *path) |
67 | { |
68 | struct sockaddr_un sa; |
69 | int fd; |
70 | mode_t old_umask; |
71 | |
72 | if ((fd = socket(AF_UNIX1, SOCK_STREAM1 | SOCK_CLOEXEC0x8000, 0)) == -1) { |
73 | log_warn("control_init: socket"); |
74 | return (-1); |
75 | } |
76 | |
77 | memset(&sa, 0, sizeof(sa)); |
78 | sa.sun_family = AF_UNIX1; |
79 | if (strlcpy(sa.sun_path, path, sizeof(sa.sun_path)) >= |
80 | sizeof(sa.sun_path)) |
81 | errx(1, "ctl socket name too long"); |
82 | |
83 | if (unlink(path) == -1) |
84 | if (errno(*__errno()) != ENOENT2) { |
85 | log_warn("control_init: unlink %s", path); |
86 | close(fd); |
87 | return (-1); |
88 | } |
89 | |
90 | old_umask = umask(S_IXUSR0000100|S_IXGRP0000010|S_IWOTH0000002|S_IROTH0000004|S_IXOTH0000001); |
91 | if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) { |
92 | log_warn("control_init: bind: %s", path); |
93 | close(fd); |
94 | umask(old_umask); |
95 | return (-1); |
96 | } |
97 | umask(old_umask); |
98 | |
99 | if (chmod(path, S_IRUSR0000400|S_IWUSR0000200|S_IRGRP0000040|S_IWGRP0000020) == -1) { |
100 | log_warn("control_init: chmod"); |
101 | close(fd); |
102 | (void)unlink(path); |
103 | return (-1); |
104 | } |
105 | |
106 | session_socket_nonblockmode(fd); |
107 | |
108 | return (fd); |
109 | } |
110 | |
111 | int |
112 | control_listen(int fd) |
113 | { |
114 | if (fd != -1 && listen(fd, CONTROL_BACKLOG5) == -1) { |
115 | log_warn("control_listen: listen"); |
116 | return (-1); |
117 | } |
118 | |
119 | return (0); |
120 | } |
121 | |
122 | void |
123 | control_shutdown(int fd) |
124 | { |
125 | close(fd); |
126 | } |
127 | |
128 | int |
129 | control_accept(int listenfd) |
130 | { |
131 | int connfd; |
132 | socklen_t len; |
133 | struct sockaddr_un sa; |
134 | struct ctl_conn *ctl_conn; |
135 | |
136 | len = sizeof(sa); |
137 | if ((connfd = accept(listenfd, |
138 | (struct sockaddr *)&sa, &len)) == -1) { |
139 | if (errno(*__errno()) != EWOULDBLOCK35 && errno(*__errno()) != EINTR4) |
140 | log_warn("control_accept: accept"); |
141 | return (0); |
142 | } |
143 | |
144 | session_socket_nonblockmode(connfd); |
145 | |
146 | if ((ctl_conn = calloc(1, sizeof(struct ctl_conn))) == NULL((void *)0)) { |
147 | log_warn("control_accept"); |
148 | close(connfd); |
149 | return (0); |
150 | } |
151 | |
152 | imsg_init(&ctl_conn->ibuf, connfd); |
153 | |
154 | TAILQ_INSERT_TAIL(&ctl_conns, ctl_conn, entry)do { (ctl_conn)->entry.tqe_next = ((void *)0); (ctl_conn)-> entry.tqe_prev = (&ctl_conns)->tqh_last; *(&ctl_conns )->tqh_last = (ctl_conn); (&ctl_conns)->tqh_last = & (ctl_conn)->entry.tqe_next; } while (0); |
155 | |
156 | return (1); |
157 | } |
158 | |
159 | struct ctl_conn * |
160 | control_connbyfd(int fd) |
161 | { |
162 | struct ctl_conn *c; |
163 | |
164 | TAILQ_FOREACH(c, &ctl_conns, entry)for((c) = ((&ctl_conns)->tqh_first); (c) != ((void *)0 ); (c) = ((c)->entry.tqe_next)) { |
165 | if (c->ibuf.fd == fd) |
166 | break; |
167 | } |
168 | |
169 | return (c); |
170 | } |
171 | |
172 | int |
173 | control_close(int fd) |
174 | { |
175 | struct ctl_conn *c; |
176 | |
177 | if ((c = control_connbyfd(fd)) == NULL((void *)0)) { |
178 | log_warn("control_close: fd %d: not found", fd); |
179 | return (0); |
180 | } |
181 | |
182 | msgbuf_clear(&c->ibuf.w); |
183 | TAILQ_REMOVE(&ctl_conns, c, entry)do { if (((c)->entry.tqe_next) != ((void *)0)) (c)->entry .tqe_next->entry.tqe_prev = (c)->entry.tqe_prev; else ( &ctl_conns)->tqh_last = (c)->entry.tqe_prev; *(c)-> entry.tqe_prev = (c)->entry.tqe_next; ; ; } while (0); |
184 | |
185 | close(c->ibuf.fd); |
186 | free(c); |
187 | |
188 | return (1); |
189 | } |
190 | |
191 | int |
192 | control_dispatch_msg(struct pollfd *pfd, u_int *ctl_cnt) |
193 | { |
194 | struct imsg imsg; |
195 | struct ctl_conn *c; |
196 | struct ntp_peer *p; |
197 | struct ntp_sensor *s; |
198 | struct ctl_show_status c_status; |
199 | struct ctl_show_peer c_peer; |
200 | struct ctl_show_sensor c_sensor; |
201 | int cnt; |
202 | ssize_t n; |
203 | |
204 | if ((c = control_connbyfd(pfd->fd)) == NULL((void *)0)) { |
205 | log_warn("control_dispatch_msg: fd %d: not found", pfd->fd); |
206 | return (0); |
207 | } |
208 | |
209 | if (pfd->revents & POLLOUT0x0004) |
210 | if (msgbuf_write(&c->ibuf.w) <= 0 && errno(*__errno()) != EAGAIN35) { |
211 | *ctl_cnt -= control_close(pfd->fd); |
212 | return (1); |
213 | } |
214 | |
215 | if (!(pfd->revents & POLLIN0x0001)) |
216 | return (0); |
217 | |
218 | if (((n = imsg_read(&c->ibuf)) == -1 && errno(*__errno()) != EAGAIN35) || n == 0) { |
219 | *ctl_cnt -= control_close(pfd->fd); |
220 | return (1); |
221 | } |
222 | |
223 | for (;;) { |
224 | if ((n = imsg_get(&c->ibuf, &imsg)) == -1) { |
225 | *ctl_cnt -= control_close(pfd->fd); |
226 | return (1); |
227 | } |
228 | if (n == 0) |
229 | break; |
230 | |
231 | switch (imsg.hdr.type) { |
232 | case IMSG_CTL_SHOW_STATUS: |
233 | build_show_status(&c_status); |
234 | imsg_compose(&c->ibuf, IMSG_CTL_SHOW_STATUS, 0, 0, -1, |
235 | &c_status, sizeof (c_status)); |
236 | break; |
237 | case IMSG_CTL_SHOW_PEERS: |
238 | cnt = 0; |
239 | TAILQ_FOREACH(p, &conf->ntp_peers, entry)for((p) = ((&conf->ntp_peers)->tqh_first); (p) != ( (void *)0); (p) = ((p)->entry.tqe_next)) { |
240 | build_show_peer(&c_peer, p); |
241 | imsg_compose(&c->ibuf, IMSG_CTL_SHOW_PEERS, |
242 | 0, 0, -1, &c_peer, sizeof(c_peer)); |
243 | cnt++; |
244 | } |
245 | imsg_compose(&c->ibuf, IMSG_CTL_SHOW_PEERS_END, |
246 | 0, 0, -1, &cnt, sizeof(cnt)); |
247 | break; |
248 | case IMSG_CTL_SHOW_SENSORS: |
249 | cnt = 0; |
250 | TAILQ_FOREACH(s, &conf->ntp_sensors, entry)for((s) = ((&conf->ntp_sensors)->tqh_first); (s) != ((void *)0); (s) = ((s)->entry.tqe_next)) { |
251 | build_show_sensor(&c_sensor, s); |
252 | imsg_compose(&c->ibuf, IMSG_CTL_SHOW_SENSORS, |
253 | 0, 0, -1, &c_sensor, sizeof(c_sensor)); |
254 | cnt++; |
255 | } |
256 | imsg_compose(&c->ibuf, IMSG_CTL_SHOW_SENSORS_END, |
257 | 0, 0, -1, &cnt, sizeof(cnt)); |
258 | break; |
259 | case IMSG_CTL_SHOW_ALL: |
260 | build_show_status(&c_status); |
261 | imsg_compose(&c->ibuf, IMSG_CTL_SHOW_STATUS, 0, 0, -1, |
262 | &c_status, sizeof (c_status)); |
263 | |
264 | cnt = 0; |
265 | TAILQ_FOREACH(p, &conf->ntp_peers, entry)for((p) = ((&conf->ntp_peers)->tqh_first); (p) != ( (void *)0); (p) = ((p)->entry.tqe_next)) { |
266 | build_show_peer(&c_peer, p); |
267 | imsg_compose(&c->ibuf, IMSG_CTL_SHOW_PEERS, |
268 | 0, 0, -1, &c_peer, sizeof(c_peer)); |
269 | cnt++; |
270 | } |
271 | imsg_compose(&c->ibuf, IMSG_CTL_SHOW_PEERS_END, |
272 | 0, 0, -1, &cnt, sizeof(cnt)); |
273 | |
274 | cnt = 0; |
275 | TAILQ_FOREACH(s, &conf->ntp_sensors, entry)for((s) = ((&conf->ntp_sensors)->tqh_first); (s) != ((void *)0); (s) = ((s)->entry.tqe_next)) { |
276 | build_show_sensor(&c_sensor, s); |
277 | imsg_compose(&c->ibuf, IMSG_CTL_SHOW_SENSORS, |
278 | 0, 0, -1, &c_sensor, sizeof(c_sensor)); |
279 | cnt++; |
280 | } |
281 | imsg_compose(&c->ibuf, IMSG_CTL_SHOW_SENSORS_END, |
282 | 0, 0, -1, &cnt, sizeof(cnt)); |
283 | |
284 | imsg_compose(&c->ibuf, IMSG_CTL_SHOW_ALL_END, |
285 | 0, 0, -1, NULL((void *)0), 0); |
286 | break; |
287 | default: |
288 | break; |
289 | } |
290 | imsg_free(&imsg); |
291 | } |
292 | return (0); |
293 | } |
294 | |
295 | void |
296 | session_socket_nonblockmode(int fd) |
297 | { |
298 | int flags; |
299 | |
300 | if ((flags = fcntl(fd, F_GETFL3)) == -1) |
301 | fatal("fcntl F_GETFL"); |
302 | |
303 | flags |= O_NONBLOCK0x0004; |
304 | |
305 | if ((flags = fcntl(fd, F_SETFL4, flags)) == -1) |
Although the value stored to 'flags' is used in the enclosing expression, the value is never actually read from 'flags' | |
306 | fatal("fcntl F_SETFL"); |
307 | } |
308 | |
309 | void |
310 | build_show_status(struct ctl_show_status *cs) |
311 | { |
312 | struct ntp_peer *p; |
313 | struct ntp_sensor *s; |
314 | |
315 | cs->peercnt = cs->valid_peers = 0; |
316 | cs->sensorcnt = cs->valid_sensors = 0; |
317 | |
318 | TAILQ_FOREACH(p, &conf->ntp_peers, entry)for((p) = ((&conf->ntp_peers)->tqh_first); (p) != ( (void *)0); (p) = ((p)->entry.tqe_next)) { |
319 | cs->peercnt++; |
320 | if (p->trustlevel >= TRUSTLEVEL_BADPEER6) |
321 | cs->valid_peers++; |
322 | } |
323 | TAILQ_FOREACH(s, &conf->ntp_sensors, entry)for((s) = ((&conf->ntp_sensors)->tqh_first); (s) != ((void *)0); (s) = ((s)->entry.tqe_next)) { |
324 | cs->sensorcnt++; |
325 | if (s->update.good) |
326 | cs->valid_sensors++; |
327 | } |
328 | |
329 | cs->synced = conf->status.synced; |
330 | cs->stratum = conf->status.stratum; |
331 | cs->clock_offset = getoffset() * 1000.0; |
332 | cs->constraints = !TAILQ_EMPTY(&conf->constraints)(((&conf->constraints)->tqh_first) == ((void *)0)); |
333 | cs->constraint_median = conf->constraint_median; |
334 | cs->constraint_last = conf->constraint_last; |
335 | cs->constraint_errors = conf->constraint_errors; |
336 | } |
337 | |
338 | void |
339 | build_show_peer(struct ctl_show_peer *cp, struct ntp_peer *p) |
340 | { |
341 | const char *a = "not resolved"; |
342 | const char *pool = "", *addr_head_name = ""; |
343 | const char *auth = ""; |
344 | int shift, best = -1, validdelaycnt = 0, jittercnt = 0; |
345 | time_t now; |
346 | |
347 | now = getmonotime(); |
348 | |
349 | if (p->addr) { |
350 | a = log_ntp_addr(p->addr); |
351 | if (p->addr->notauth) |
352 | auth = " (non-dnssec lookup)"; |
353 | } |
354 | if (p->addr_head.pool) |
355 | pool = "from pool "; |
356 | |
357 | if (0 != strcmp(a, p->addr_head.name) || p->addr_head.pool) |
358 | addr_head_name = p->addr_head.name; |
359 | |
360 | snprintf(cp->peer_desc, sizeof(cp->peer_desc), |
361 | "%s %s%s%s", a, pool, addr_head_name, auth); |
362 | |
363 | cp->offset = cp->delay = 0.0; |
364 | for (shift = 0; shift < OFFSET_ARRAY_SIZE8; shift++) { |
365 | if (p->reply[shift].delay > 0.0) { |
366 | cp->offset += p->reply[shift].offset; |
367 | cp->delay += p->reply[shift].delay; |
368 | |
369 | if (best == -1 || |
370 | p->reply[shift].delay < p->reply[best].delay) |
371 | best = shift; |
372 | |
373 | validdelaycnt++; |
374 | } |
375 | } |
376 | |
377 | if (validdelaycnt > 1) { |
378 | cp->offset /= validdelaycnt; |
379 | cp->delay /= validdelaycnt; |
380 | } |
381 | |
382 | cp->jitter = 0.0; |
383 | if (best != -1) { |
384 | for (shift = 0; shift < OFFSET_ARRAY_SIZE8; shift++) { |
385 | if (p->reply[shift].delay > 0.0 && shift != best) { |
386 | cp->jitter += square(p->reply[shift].delay -((p->reply[shift].delay - p->reply[best].delay) * (p-> reply[shift].delay - p->reply[best].delay)) |
387 | p->reply[best].delay)((p->reply[shift].delay - p->reply[best].delay) * (p-> reply[shift].delay - p->reply[best].delay)); |
388 | jittercnt++; |
389 | } |
390 | } |
391 | if (jittercnt > 1) |
392 | cp->jitter /= jittercnt; |
393 | cp->jitter = sqrt(cp->jitter); |
394 | } |
395 | |
396 | if (p->shift == 0) |
397 | shift = OFFSET_ARRAY_SIZE8 - 1; |
398 | else |
399 | shift = p->shift - 1; |
400 | |
401 | if (conf->status.synced == 1 && |
402 | p->reply[shift].status.send_refid == conf->status.refid) |
403 | cp->syncedto = 1; |
404 | else |
405 | cp->syncedto = 0; |
406 | |
407 | /* milliseconds to reduce number of leading zeroes */ |
408 | cp->offset *= 1000.0; |
409 | cp->delay *= 1000.0; |
410 | cp->jitter *= 1000.0; |
411 | |
412 | cp->weight = p->weight; |
413 | cp->trustlevel = p->trustlevel; |
414 | cp->stratum = p->reply[shift].status.stratum; |
415 | cp->next = p->next - now < 0 ? 0 : p->next - now; |
416 | cp->poll = p->poll; |
417 | } |
418 | |
419 | void |
420 | build_show_sensor(struct ctl_show_sensor *cs, struct ntp_sensor *s) |
421 | { |
422 | time_t now; |
423 | u_int8_t shift; |
424 | u_int32_t refid; |
425 | |
426 | now = getmonotime(); |
427 | |
428 | memcpy(&refid, SENSOR_DEFAULT_REFID"HARD", sizeof(refid)); |
429 | refid = refid == s->refid ? 0 : s->refid; |
430 | |
431 | snprintf(cs->sensor_desc, sizeof(cs->sensor_desc), |
432 | "%s %.4s", s->device, (char *)&refid); |
433 | |
434 | if (s->shift == 0) |
435 | shift = SENSOR_OFFSETS6 - 1; |
436 | else |
437 | shift = s->shift - 1; |
438 | |
439 | if (conf->status.synced == 1 && |
440 | s->offsets[shift].status.send_refid == conf->status.refid) |
441 | cs->syncedto = 1; |
442 | else |
443 | cs->syncedto = 0; |
444 | |
445 | cs->weight = s->weight; |
446 | cs->good = s->update.good; |
447 | cs->stratum = s->offsets[shift].status.stratum; |
448 | cs->next = s->next - now < 0 ? 0 : s->next - now; |
449 | cs->poll = SENSOR_QUERY_INTERVAL15; |
450 | cs->offset = s->offsets[shift].offset * 1000.0; |
451 | cs->correction = (double)s->correction / 1000.0; |
452 | } |