diff options
author | florian <florian@openbsd.org> | 2019-01-14 16:30:21 +0000 |
---|---|---|
committer | florian <florian@openbsd.org> | 2019-01-14 16:30:21 +0000 |
commit | febce3603669e0f85f8c3ea53eb228689e2b1bac (patch) | |
tree | cf4e5dd31143415ba9707a83ff6ddcd54563936f /usr.sbin/ntpd | |
parent | Remove obsolete symbols.sort target. (diff) | |
download | wireguard-openbsd-febce3603669e0f85f8c3ea53eb228689e2b1bac.tar.xz wireguard-openbsd-febce3603669e0f85f8c3ea53eb228689e2b1bac.zip |
Prevent multiple ntpds from tripping over each other.
This brings over the logic from bgpd & ospfd.
Input & OK deraadt
Diffstat (limited to 'usr.sbin/ntpd')
-rw-r--r-- | usr.sbin/ntpd/control.c | 28 | ||||
-rw-r--r-- | usr.sbin/ntpd/ntpd.c | 5 | ||||
-rw-r--r-- | usr.sbin/ntpd/ntpd.h | 3 |
3 files changed, 33 insertions, 3 deletions
diff --git a/usr.sbin/ntpd/control.c b/usr.sbin/ntpd/control.c index aa3b789768c..c47ba0adc5f 100644 --- a/usr.sbin/ntpd/control.c +++ b/usr.sbin/ntpd/control.c @@ -1,4 +1,4 @@ -/* $OpenBSD: control.c,v 1.13 2018/08/04 11:07:14 mestre Exp $ */ +/* $OpenBSD: control.c,v 1.14 2019/01/14 16:30:21 florian Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> @@ -37,6 +37,32 @@ #define square(x) ((x) * (x)) int +control_check(char *path) +{ + struct sockaddr_un sun; + int fd; + + bzero(&sun, sizeof(sun)); + sun.sun_family = AF_UNIX; + strlcpy(sun.sun_path, path, sizeof(sun.sun_path)); + + if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { + log_warn("control_check: socket check"); + return (-1); + } + + if (connect(fd, (struct sockaddr *)&sun, sizeof(sun)) == 0) { + log_warnx("control_check: socket in use"); + close(fd); + return (-1); + } + + close(fd); + + return (0); +} + +int control_init(char *path) { struct sockaddr_un sa; diff --git a/usr.sbin/ntpd/ntpd.c b/usr.sbin/ntpd/ntpd.c index a927be1a749..44c4f72f3fb 100644 --- a/usr.sbin/ntpd/ntpd.c +++ b/usr.sbin/ntpd/ntpd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ntpd.c,v 1.119 2018/11/29 14:25:07 tedu Exp $ */ +/* $OpenBSD: ntpd.c,v 1.120 2019/01/14 16:30:21 florian Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> @@ -202,6 +202,9 @@ main(int argc, char *argv[]) pname); fatalx("%s: process '%s' failed", __func__, pname); + } else { + if ((control_check(CTLSOCKET)) == -1) + fatalx("ntpd already running"); } if (setpriority(PRIO_PROCESS, 0, -20) == -1) diff --git a/usr.sbin/ntpd/ntpd.h b/usr.sbin/ntpd/ntpd.h index 0bb7f1bc573..91d1e0fb9b5 100644 --- a/usr.sbin/ntpd/ntpd.h +++ b/usr.sbin/ntpd/ntpd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: ntpd.h,v 1.137 2018/11/06 20:41:36 jsing Exp $ */ +/* $OpenBSD: ntpd.h,v 1.138 2019/01/14 16:30:21 florian Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> @@ -393,6 +393,7 @@ void sensor_query(struct ntp_sensor *); void ntp_dns(struct ntpd_conf *, struct passwd *); /* control.c */ +int control_check(char *); int control_init(char *); int control_listen(int); void control_shutdown(int); |