1 /* $OpenBSD: src/usr.sbin/ntpd/ntpd.c,v 1.25 2004/12/06 20:57:17 mickey 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.
19 #include <sys/types.h>
20 #include <sys/socket.h>
22 #include <netinet/in.h>
23 #include <arpa/inet.h>
38 int main(int, char *[]);
39 int check_child(pid_t, const char *);
40 int dispatch_imsg(struct ntpd_conf *);
41 void ntpd_adjtime(double);
42 void ntpd_settime(double);
44 volatile sig_atomic_t quit = 0;
45 volatile sig_atomic_t reconfig = 0;
46 volatile sig_atomic_t sigchld = 0;
69 extern char *__progname;
71 fprintf(stderr, "usage: %s [-dSs] [-f file]\n", __progname);
79 main(int argc, char *argv[])
81 struct ntpd_conf conf;
82 struct pollfd pfd[POLL_MAX];
83 pid_t chld_pid = 0, pid;
85 int ch, nfds, timeout = INFTIM;
90 bzero(&conf, sizeof(conf));
92 log_init(1); /* log to stderr until daemonized */
94 while ((ch = getopt(argc, argv, "df:sS")) != -1) {
114 if (parse_config(conffile, &conf))
118 fprintf(stderr, "ntpd: need root privileges\n");
122 if (getpwnam(NTPD_USER) == NULL) {
123 fprintf(stderr, "ntpd: unknown user %s\n", NTPD_USER);
129 log_init(conf.debug);
135 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_chld) == -1)
139 chld_pid = ntp_main(pipe_chld, &conf);
141 setproctitle("[priv]");
143 signal(SIGTERM, sighdlr);
144 signal(SIGINT, sighdlr);
145 signal(SIGCHLD, sighdlr);
146 signal(SIGHUP, sighdlr);
150 if ((ibuf = malloc(sizeof(struct imsgbuf))) == NULL)
152 imsg_init(ibuf, pipe_chld[0]);
155 pfd[PFD_PIPE].fd = ibuf->fd;
156 pfd[PFD_PIPE].events = POLLIN;
158 pfd[PFD_PIPE].events |= POLLOUT;
160 if ((nfds = poll(pfd, 1, timeout)) == -1)
161 if (errno != EINTR) {
162 log_warn("poll error");
166 if (nfds == 0 && conf.settime) {
167 log_debug("no reply received, skipping initial time"
171 log_init(conf.debug);
176 if (nfds > 0 && (pfd[PFD_PIPE].revents & POLLOUT))
177 if (msgbuf_write(&ibuf->w) < 0) {
178 log_warn("pipe write error (to child");
182 if (nfds > 0 && pfd[PFD_PIPE].revents & POLLIN) {
184 if (dispatch_imsg(&conf) == -1)
189 if (check_child(chld_pid, "child")) {
198 signal(SIGCHLD, SIG_DFL);
201 kill(chld_pid, SIGTERM);
204 if ((pid = wait(NULL)) == -1 &&
205 errno != EINTR && errno != ECHILD)
207 } while (pid != -1 || (pid == -1 && errno == EINTR));
209 msgbuf_clear(&ibuf->w);
211 log_info("Terminating");
216 check_child(pid_t pid, const char *pname)
220 if (waitpid(pid, &status, WNOHANG) > 0) {
221 if (WIFEXITED(status)) {
222 log_warnx("Lost child: %s exited", pname);
225 if (WIFSIGNALED(status)) {
226 log_warnx("Lost child: %s terminated; signal %d",
227 pname, WTERMSIG(status));
236 dispatch_imsg(struct ntpd_conf *conf)
242 struct ntp_addr *h, *hn;
245 if ((n = imsg_read(ibuf)) == -1)
248 if (n == 0) { /* connection closed */
249 log_warnx("dispatch_imsg in main: pipe closed");
254 if ((n = imsg_get(ibuf, &imsg)) == -1)
260 switch (imsg.hdr.type) {
262 if (imsg.hdr.len != IMSG_HEADER_SIZE + sizeof(d))
263 fatal("invalid IMSG_ADJTIME received");
264 memcpy(&d, imsg.data, sizeof(d));
268 if (imsg.hdr.len != IMSG_HEADER_SIZE + sizeof(d))
269 fatal("invalid IMSG_SETTIME received");
272 memcpy(&d, imsg.data, sizeof(d));
275 log_init(conf->debug);
282 if (imsg.hdr.len != strlen(name) + 1 + IMSG_HEADER_SIZE)
283 fatal("invalid IMSG_HOST_DNS received");
284 if ((cnt = host_dns(name, &hn)) > 0) {
285 buf = imsg_create(ibuf, IMSG_HOST_DNS,
287 cnt * sizeof(struct sockaddr_storage));
290 for (h = hn; h != NULL; h = h->next) {
291 imsg_add(buf, &h->ss, sizeof(h->ss));
293 imsg_close(ibuf, buf);
305 ntpd_adjtime(double d)
309 if (d >= (double)LOG_NEGLIGEE / 1000)
310 log_info("adjusting local clock by %fs", d);
312 log_debug("adjusting local clock by %fs", d);
314 if (adjtime(&tv, NULL) == -1)
315 log_warn("adjtime failed");
319 ntpd_settime(double d)
321 struct timeval tv, curtime;
325 /* if the offset is small, don't call settimeofday */
326 if (d < SETTIME_MIN_OFFSET && d > -SETTIME_MIN_OFFSET)
330 if (gettimeofday(&curtime, NULL) == -1)
331 log_warn("gettimeofday");
332 curtime.tv_sec += tv.tv_sec;
333 curtime.tv_usec += tv.tv_usec;
334 if (curtime.tv_usec > 1000000) {
336 curtime.tv_usec -= 1000000;
338 if (settimeofday(&curtime, NULL) == -1)
339 log_warn("settimeofday");
340 tval = curtime.tv_sec;
341 strftime(buf, sizeof(buf), "%a %b %e %H:%M:%S %Z %Y",
343 log_info("set local clock to %s (offset %fs)", buf, d);