1 /* $OpenBSD: ntpd.c,v 1.14 2004/08/12 16:33:59 henning 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(void);
41 void ntpd_adjtime(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 [-d] [-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;
91 bzero(&conf, sizeof(conf));
93 log_init(1); /* log to stderr until daemonized */
95 while ((ch = getopt(argc, argv, "df:")) != -1) {
109 if (parse_config(conffile, &conf))
113 fprintf(stderr, "ntpd: need root privileges\n");
117 if (getpwnam(NTPD_USER) == NULL) {
118 fprintf(stderr, "ntpd: unknown user %s\n", NTPD_USER);
128 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_chld) == -1)
132 chld_pid = ntp_main(pipe_chld, &conf);
134 setproctitle("[priv]");
136 signal(SIGTERM, sighdlr);
137 signal(SIGINT, sighdlr);
138 signal(SIGCHLD, sighdlr);
139 signal(SIGHUP, sighdlr);
143 imsg_init(&ibuf, pipe_chld[0]);
146 pfd[PFD_PIPE].fd = ibuf.fd;
147 pfd[PFD_PIPE].events = POLLIN;
149 pfd[PFD_PIPE].events |= POLLOUT;
151 if ((nfds = poll(pfd, 1, INFTIM)) == -1)
152 if (errno != EINTR) {
153 log_warn("poll error");
157 if (nfds > 0 && (pfd[PFD_PIPE].revents & POLLOUT))
158 if (msgbuf_write(&ibuf.w) < 0) {
159 log_warn("pipe write error (to child");
163 if (nfds > 0 && pfd[PFD_PIPE].revents & POLLIN) {
165 if (dispatch_imsg() == -1)
170 if (check_child(chld_pid, "child"))
177 signal(SIGCHLD, SIG_IGN);
180 kill(chld_pid, SIGTERM);
183 if ((pid = wait(NULL)) == -1 &&
184 errno != EINTR && errno != ECHILD)
186 } while (pid != -1 || (pid == -1 && errno == EINTR));
188 log_info("Terminating");
193 check_child(pid_t pid, const char *pname)
197 if (waitpid(pid, &status, WNOHANG) > 0) {
198 if (WIFEXITED(status)) {
199 log_warnx("Lost child: %s exited", pname);
202 if (WIFSIGNALED(status)) {
203 log_warnx("Lost child: %s terminated; signal %d",
204 pname, WTERMSIG(status));
219 struct ntp_addr *h, *hn;
222 if ((n = imsg_read(&ibuf)) == -1)
225 if (n == 0) { /* connection closed */
226 log_warnx("dispatch_imsg in main: pipe closed");
231 if ((n = imsg_get(&ibuf, &imsg)) == -1)
237 switch (imsg.hdr.type) {
239 if (imsg.hdr.len != IMSG_HEADER_SIZE + sizeof(d))
240 fatal("invalid IMSG_ADJTIME received");
241 memcpy(&d, imsg.data, sizeof(d));
246 if (imsg.hdr.len != strlen(name) + 1 + IMSG_HEADER_SIZE)
247 fatal("invalid IMSG_HOST_DNS received");
248 if ((cnt = host_dns(name, &hn)) > 0) {
249 buf = imsg_create(&ibuf, IMSG_HOST_DNS,
251 cnt * sizeof(struct sockaddr_storage));
254 for (h = hn; h != NULL; h = h->next) {
255 imsg_add(buf, &h->ss, sizeof(h->ss));
257 imsg_close(&ibuf, buf);
269 ntpd_adjtime(double d)
274 log_info("adjusting local clock by %fs", d);
275 if (adjtime(&tv, NULL) == -1)
276 log_warn("adjtime failed");