diff --git a/src/usr.sbin/ntpd/ntp.c b/src/usr.sbin/ntpd/ntp.c index ae7418d0..a4ef5edc 100644 --- a/src/usr.sbin/ntpd/ntp.c +++ b/src/usr.sbin/ntpd/ntp.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ntp.c,v 1.72 2006/05/27 18:32:00 henning Exp $ */ +/* $OpenBSD: ntp.c,v 1.73 2006/05/27 21:27:34 henning Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -35,7 +35,8 @@ #include "ntp.h" #define PFD_PIPE_MAIN 0 -#define PFD_MAX 1 +#define PFD_HOTPLUG 1 +#define PFD_MAX 2 volatile sig_atomic_t ntp_quit = 0; struct imsgbuf *ibuf_main; @@ -62,7 +63,8 @@ ntp_sighdlr(int sig) pid_t ntp_main(int pipe_prnt[2], struct ntpd_conf *nconf) { - int a, b, nfds, i, j, idx_peers, timeout, nullfd; + int a, b, nfds, i, j, idx_peers, timeout; + int hotplugfd, nullfd; u_int pfd_elms = 0, idx2peer_elms = 0; u_int listener_cnt, new_cnt, sent_cnt, trial_cnt; u_int sensors_cnt = 0; @@ -97,6 +99,7 @@ ntp_main(int pipe_prnt[2], struct ntpd_conf *nconf) if ((nullfd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) fatal(NULL); + hotplugfd = sensor_hotplugfd(); if (stat(pw->pw_dir, &stb) == -1) fatal("stat"); @@ -187,6 +190,8 @@ ntp_main(int pipe_prnt[2], struct ntpd_conf *nconf) nextaction = time(NULL) + 3600; pfd[PFD_PIPE_MAIN].fd = ibuf_main->fd; pfd[PFD_PIPE_MAIN].events = POLLIN; + pfd[PFD_HOTPLUG].fd = hotplugfd; + pfd[PFD_HOTPLUG].events = POLLIN; i = PFD_MAX; TAILQ_FOREACH(la, &conf->listen_addrs, entry) { @@ -272,6 +277,11 @@ ntp_main(int pipe_prnt[2], struct ntpd_conf *nconf) ntp_quit = 1; } + if (nfds > 0 && pfd[PFD_HOTPLUG].revents & (POLLIN|POLLERR)) { + nfds--; + sensor_hotplugevent(hotplugfd); + } + for (j = 1; nfds > 0 && j < idx_peers; j++) if (pfd[j].revents & (POLLIN|POLLERR)) { nfds--; diff --git a/src/usr.sbin/ntpd/ntpd.h b/src/usr.sbin/ntpd/ntpd.h index 5d3086ad..8b041053 100644 --- a/src/usr.sbin/ntpd/ntpd.h +++ b/src/usr.sbin/ntpd/ntpd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: ntpd.h,v 1.65 2006/05/27 18:32:00 henning Exp $ */ +/* $OpenBSD: ntpd.h,v 1.66 2006/05/27 21:27:34 henning Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -285,3 +285,5 @@ void sensor_init(struct ntpd_conf *); void sensor_scan(struct ntpd_conf *); void sensor_remove(struct ntpd_conf *, struct ntp_sensor *); int sensor_query(struct ntp_sensor *); +int sensor_hotplugfd(void); +void sensor_hotplugevent(int); diff --git a/src/usr.sbin/ntpd/sensors.c b/src/usr.sbin/ntpd/sensors.c index 6213f5ca..59e306f8 100644 --- a/src/usr.sbin/ntpd/sensors.c +++ b/src/usr.sbin/ntpd/sensors.c @@ -1,4 +1,4 @@ -/* $OpenBSD: sensors.c,v 1.2 2006/05/27 17:01:07 henning Exp $ */ +/* $OpenBSD: sensors.c,v 1.3 2006/05/27 21:27:34 henning Exp $ */ /* * Copyright (c) 2006 Henning Brauer @@ -20,6 +20,8 @@ #include #include #include +#include +#include #include #include @@ -30,6 +32,7 @@ #include "ntpd.h" #define SENSORS_MAX 255 +#define _PATH_DEV_HOTPLUG "/dev/hotplug" void sensor_add(struct ntpd_conf *, struct sensor *); @@ -150,3 +153,41 @@ sensor_query(struct ntp_sensor *s) return (0); } + +int +sensor_hotplugfd(void) +{ + int fd, flags; + + if ((fd = open(_PATH_DEV_HOTPLUG, O_RDONLY, 0)) == -1) + fatal(NULL); + + if ((flags = fcntl(fd, F_GETFL, 0)) == -1) + fatal("fnctl F_GETFL"); + flags |= O_NONBLOCK; + if ((flags = fcntl(fd, F_SETFL, flags)) == -1) + fatal("fnctl F_SETFL"); + + return (fd); +} + +void +sensor_hotplugevent(int fd) +{ + struct hotplug_event he; + ssize_t n; + + do { + if ((n = read(fd, &he, sizeof(he))) == -1 && + errno != EINTR && errno != EAGAIN) + fatal("sensor_hotplugevent read"); + + if (n == sizeof(he)) + switch (he.he_type) { + default: /* ignore */ + break; + } + else if (n > 0) + fatal("sensor_hotplugevent: short read"); + } while (n > 0 || (n == -1 && errno == EINTR)); +}