1
/* $OpenBSD: ntpd.c,v 1.40 2005/09/06 21:27:10 wvdputte Exp $ */
4
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
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.
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 MIND, USE, DATA OR PROFITS, WHETHER
15
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21
RCSID("$Release: OpenNTPD "OPENNTPD_VERSION" $");
23
#include <sys/types.h>
24
#include <sys/socket.h>
26
#include <netinet/in.h>
42
__dead void usage(void);
43
int main(int, char *[]);
44
int check_child(pid_t, const char *);
45
int dispatch_imsg(struct ntpd_conf *);
46
int ntpd_adjtime(double);
47
void ntpd_settime(double);
49
volatile sig_atomic_t quit = 0;
50
volatile sig_atomic_t reconfig = 0;
51
volatile sig_atomic_t sigchld = 0;
74
extern char *__progname;
76
fprintf(stderr, "usage: %s [-dSs] [-f file]\n", __progname);
84
main(int argc, char *argv[])
86
struct ntpd_conf conf;
87
struct pollfd pfd[POLL_MAX];
88
pid_t chld_pid = 0, pid;
90
int ch, nfds, timeout = INFTIM;
92
extern char *__progname;
94
__progname = _compat_get_progname(argv[0]);
98
bzero(&conf, sizeof(conf));
100
log_init(1); /* log to stderr until daemonized */
101
res_init(); /* XXX */
103
while ((ch = getopt(argc, argv, "df:sS")) != -1) {
123
if (parse_config(conffile, &conf))
127
fprintf(stderr, "ntpd: need root privileges\n");
131
if (getpwnam(NTPD_USER) == NULL) {
132
fprintf(stderr, "ntpd: unknown user %s\n", NTPD_USER);
137
#ifndef HAVE_ARC4RANDOM
142
log_init(conf.debug);
147
timeout = SETTIME_TIMEOUT * 1000;
149
if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_chld) == -1)
152
/* fork child process */
153
chld_pid = ntp_main(pipe_chld, &conf);
155
setproctitle("[priv]");
157
signal(SIGTERM, sighdlr);
158
signal(SIGINT, sighdlr);
159
signal(SIGCHLD, sighdlr);
160
signal(SIGHUP, sighdlr);
164
if ((ibuf = malloc(sizeof(struct imsgbuf))) == NULL)
166
imsg_init(ibuf, pipe_chld[0]);
169
pfd[PFD_PIPE].fd = ibuf->fd;
170
pfd[PFD_PIPE].events = POLLIN;
172
pfd[PFD_PIPE].events |= POLLOUT;
174
if ((nfds = poll(pfd, 1, timeout)) == -1)
175
if (errno != EINTR) {
176
log_warn("poll error");
180
if (nfds == 0 && conf.settime) {
183
log_init(conf.debug);
184
log_debug("no reply received in time, skipping initial "
191
if (nfds > 0 && (pfd[PFD_PIPE].revents & POLLOUT))
192
if (msgbuf_write(&ibuf->w) < 0) {
193
log_warn("pipe write error (to child)");
197
if (nfds > 0 && pfd[PFD_PIPE].revents & POLLIN) {
199
if (dispatch_imsg(&conf) == -1)
204
if (check_child(chld_pid, "child")) {
213
signal(SIGCHLD, SIG_DFL);
216
kill(chld_pid, SIGTERM);
219
if ((pid = wait(NULL)) == -1 &&
220
errno != EINTR && errno != ECHILD)
222
} while (pid != -1 || (pid == -1 && errno == EINTR));
224
msgbuf_clear(&ibuf->w);
226
log_info("Terminating");
231
check_child(pid_t pid, const char *pname)
236
if (waitpid(pid, &status, WNOHANG) > 0) {
237
if (WIFEXITED(status)) {
238
log_warnx("Lost child: %s exited", pname);
241
if (WIFSIGNALED(status)) {
242
sig = WTERMSIG(status);
243
signame = strsignal(sig) ? strsignal(sig) : "unknown";
244
log_warnx("Lost child: %s terminated; signal %d (%s)",
245
pname, sig, signame);
254
dispatch_imsg(struct ntpd_conf *conf)
260
struct ntp_addr *h, *hn;
263
if ((n = imsg_read(ibuf)) == -1)
266
if (n == 0) { /* connection closed */
267
log_warnx("dispatch_imsg in main: pipe closed");
272
if ((n = imsg_get(ibuf, &imsg)) == -1)
278
switch (imsg.hdr.type) {
280
if (imsg.hdr.len != IMSG_HEADER_SIZE + sizeof(d))
281
fatalx("invalid IMSG_ADJTIME received");
282
memcpy(&d, imsg.data, sizeof(d));
284
imsg_compose(ibuf, IMSG_ADJTIME, 0, 0, &n, sizeof(n));
287
if (imsg.hdr.len != IMSG_HEADER_SIZE + sizeof(d))
288
fatalx("invalid IMSG_SETTIME received");
291
log_init(conf->debug);
292
memcpy(&d, imsg.data, sizeof(d));
302
if (imsg.hdr.len < 1 + IMSG_HEADER_SIZE)
303
fatalx("invalid IMSG_HOST_DNS received");
304
imsg.hdr.len -= 1 + IMSG_HEADER_SIZE;
305
if (name[imsg.hdr.len] != '\0' ||
306
strlen(name) != imsg.hdr.len)
307
fatalx("invalid IMSG_HOST_DNS received");
308
cnt = host_dns(name, &hn);
309
buf = imsg_create(ibuf, IMSG_HOST_DNS,
311
cnt * sizeof(struct sockaddr_storage));
315
for (h = hn; h != NULL; h = h->next)
316
imsg_add(buf, &h->ss, sizeof(h->ss));
318
imsg_close(ibuf, buf);
329
ntpd_adjtime(double d)
331
struct timeval tv, olddelta;
333
static int firstadj = 1;
335
if (d >= (double)LOG_NEGLIGEE / 1000 ||
336
d <= -1 * (double)LOG_NEGLIGEE / 1000)
337
log_info("adjusting local clock by %fs", d);
339
log_debug("adjusting local clock by %fs", d);
341
if (adjtime(&tv, &olddelta) == -1)
342
log_warn("adjtime failed");
343
else if (!firstadj && olddelta.tv_sec == 0 && olddelta.tv_usec == 0)
350
ntpd_settime(double d)
352
struct timeval tv, curtime;
356
/* if the offset is small, don't call settimeofday */
357
if (d < SETTIME_MIN_OFFSET && d > -SETTIME_MIN_OFFSET)
360
if (gettimeofday(&curtime, NULL) == -1) {
361
log_warn("gettimeofday");
365
curtime.tv_usec += tv.tv_usec + 1000000;
366
curtime.tv_sec += tv.tv_sec - 1 + (curtime.tv_usec / 1000000);
367
curtime.tv_usec %= 1000000;
369
if (settimeofday(&curtime, NULL) == -1) {
370
log_warn("settimeofday");
373
tval = curtime.tv_sec;
374
strftime(buf, sizeof(buf), "%a %b %e %H:%M:%S %Z %Y",
376
log_info("set local clock to %s (offset %fs)", buf, d);