Skip to content

Commit

Permalink
Prevent multiple ntpds from tripping over each other.
Browse files Browse the repository at this point in the history
This brings over the logic from bgpd & ospfd.
Input & OK deraadt
  • Loading branch information
fobser committed Jan 14, 2019
1 parent 1c6e0f3 commit febce36
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
28 changes: 27 additions & 1 deletion usr.sbin/ntpd/control.c
Original file line number Diff line number Diff line change
@@ -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>
Expand Down Expand Up @@ -36,6 +36,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)
{
Expand Down
5 changes: 4 additions & 1 deletion usr.sbin/ntpd/ntpd.c
Original file line number Diff line number Diff line change
@@ -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>
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion usr.sbin/ntpd/ntpd.h
Original file line number Diff line number Diff line change
@@ -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>
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit febce36

Please sign in to comment.