From bc9eb55c873f0ed3d1900026f88b3e4592a2071f Mon Sep 17 00:00:00 2001 From: kn Date: Wed, 21 Jul 2021 03:53:50 +0000 Subject: [PATCH] Use exclusive lock under /dev/, silence expected errors in installer resolvd(8), slaacd(8) and dhcpleased(8) are different from other daemons in that there must only be a single instance. resolvd already does this, adjust slaacd and dhcpleased accordingly while moving the lockfile paths under /dev/ such that they work early on boot and don't run into races should /var be (un)mounted between daemon starts. Locking is especially required in the installer where all three daemons are started every time the "(I)nstall, (U)pgrade, (A)utoinstall or (S)hell? " prompt is entered, i.e. restarting installation or dropping into a shell and back into the prompt again would start multiple instances. To avoid expected lockfile error messages in between installer prompts, discard standard error when starting the autoconf daemons; none of them has other potential failure cases in installer mode before daemon(3)izing. Input sthen deraadt OK deraadt --- distrib/miniroot/dot.profile | 9 +++++---- sbin/dhcpleased/dhcpleased.c | 8 ++++++-- sbin/dhcpleased/dhcpleased.h | 3 ++- sbin/resolvd/resolvd.c | 6 +++--- sbin/slaacd/slaacd.c | 8 ++++++-- sbin/slaacd/slaacd.h | 3 ++- 6 files changed, 24 insertions(+), 13 deletions(-) diff --git a/distrib/miniroot/dot.profile b/distrib/miniroot/dot.profile index 0baba160733..93e6f5837cf 100644 --- a/distrib/miniroot/dot.profile +++ b/distrib/miniroot/dot.profile @@ -1,4 +1,4 @@ -# $OpenBSD: dot.profile,v 1.45 2021/07/16 15:24:44 florian Exp $ +# $OpenBSD: dot.profile,v 1.46 2021/07/21 03:53:50 kn Exp $ # $NetBSD: dot.profile,v 1.1 1995/12/18 22:54:43 pk Exp $ # # Copyright (c) 2009 Kenneth R. Westerback @@ -58,9 +58,10 @@ if [[ -z $DONEPROFILE ]]; then mkdir -m u=rwx,go=rx -p /tmp/{ai,i} # Start autoconfiguration daemons. - [[ -x /sbin/resolvd ]] && /sbin/resolvd - [[ -x /sbin/dhcpleased ]] && /sbin/dhcpleased - [[ -x /sbin/slaacd ]] && /sbin/slaacd + # Hide legit "already running" errors when reentering the installer. + [[ -x /sbin/resolvd ]] && /sbin/resolvd 2>/dev/null + [[ -x /sbin/dhcpleased ]] && /sbin/dhcpleased 2>/dev/null + [[ -x /sbin/slaacd ]] && /sbin/slaacd 2>/dev/null # Set up some sane tty defaults. echo 'erase ^?, werase ^W, kill ^U, intr ^C, status ^T' diff --git a/sbin/dhcpleased/dhcpleased.c b/sbin/dhcpleased/dhcpleased.c index 2157406e71a..8fc60693f10 100644 --- a/sbin/dhcpleased/dhcpleased.c +++ b/sbin/dhcpleased/dhcpleased.c @@ -1,4 +1,4 @@ -/* $OpenBSD: dhcpleased.c,v 1.14 2021/07/12 15:09:18 beck Exp $ */ +/* $OpenBSD: dhcpleased.c,v 1.15 2021/07/21 03:53:50 kn Exp $ */ /* * Copyright (c) 2017, 2021 Florian Obser @@ -131,7 +131,7 @@ main(int argc, char *argv[]) char *saved_argv0; int pipe_main2frontend[2]; int pipe_main2engine[2]; - int frontend_routesock, rtfilter; + int frontend_routesock, rtfilter, lockfd; int rtable_any = RTABLE_ANY; char *csock = _PATH_DHCPLEASED_SOCKET; #ifndef SMALL @@ -181,6 +181,10 @@ main(int argc, char *argv[]) if (geteuid()) errx(1, "need root privileges"); + lockfd = open(_PATH_LOCKFILE, O_CREAT|O_RDWR|O_EXLOCK|O_NONBLOCK, 0600); + if (lockfd == -1) + errx(1, "already running"); + /* Check for assigned daemon user */ if (getpwnam(DHCPLEASED_USER) == NULL) errx(1, "unknown user %s", DHCPLEASED_USER); diff --git a/sbin/dhcpleased/dhcpleased.h b/sbin/dhcpleased/dhcpleased.h index 2654293dcb5..ae81baa7bb0 100644 --- a/sbin/dhcpleased/dhcpleased.h +++ b/sbin/dhcpleased/dhcpleased.h @@ -1,4 +1,4 @@ -/* $OpenBSD: dhcpleased.h,v 1.6 2021/06/20 08:31:45 florian Exp $ */ +/* $OpenBSD: dhcpleased.h,v 1.7 2021/07/21 03:53:50 kn Exp $ */ /* * Copyright (c) 2017, 2021 Florian Obser @@ -18,6 +18,7 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#define _PATH_LOCKFILE "/dev/dhcpleased.lock" #define _PATH_DHCPLEASED_SOCKET "/dev/dhcpleased.sock" #define DHCPLEASED_USER "_dhcp" #define DHCPLEASED_RTA_LABEL "dhcpleased" diff --git a/sbin/resolvd/resolvd.c b/sbin/resolvd/resolvd.c index a7d6bf2e365..915a8a52649 100644 --- a/sbin/resolvd/resolvd.c +++ b/sbin/resolvd/resolvd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: resolvd.c,v 1.15 2021/07/18 15:18:49 deraadt Exp $ */ +/* $OpenBSD: resolvd.c,v 1.16 2021/07/21 03:53:50 kn Exp $ */ /* * Copyright (c) 2021 Florian Obser * Copyright (c) 2021 Theo de Raadt @@ -42,10 +42,10 @@ #define ROUTE_SOCKET_BUF_SIZE 16384 #define ASR_MAXNS 10 +#define _PATH_LOCKFILE "/dev/resolvd.lock" #define _PATH_UNWIND_SOCKET "/dev/unwind.sock" #define _PATH_RESCONF "/etc/resolv.conf" #define _PATH_RESCONF_NEW "/etc/resolv.conf.new" -#define _PATH_LOCKFILE "/var/run/resolvd.lock" #ifndef nitems #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0])) @@ -193,7 +193,7 @@ main(int argc, char *argv[]) lockfd = open(_PATH_LOCKFILE, O_CREAT|O_RDWR|O_EXLOCK|O_NONBLOCK, 0600); if (lockfd == -1) - errx(1, "already running, " _PATH_LOCKFILE); + errx(1, "already running"); if (!debug) daemon(0, 0); diff --git a/sbin/slaacd/slaacd.c b/sbin/slaacd/slaacd.c index 44a754b421c..dbd06d6ad79 100644 --- a/sbin/slaacd/slaacd.c +++ b/sbin/slaacd/slaacd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: slaacd.c,v 1.60 2021/05/01 11:53:24 florian Exp $ */ +/* $OpenBSD: slaacd.c,v 1.61 2021/07/21 03:53:50 kn Exp $ */ /* * Copyright (c) 2017 Florian Obser @@ -129,7 +129,7 @@ main(int argc, char *argv[]) char *saved_argv0; int pipe_main2frontend[2]; int pipe_main2engine[2]; - int frontend_routesock, rtfilter; + int frontend_routesock, rtfilter, lockfd; int rtable_any = RTABLE_ANY; char *csock = _PATH_SLAACD_SOCKET; #ifndef SMALL @@ -180,6 +180,10 @@ main(int argc, char *argv[]) if (geteuid()) errx(1, "need root privileges"); + lockfd = open(_PATH_LOCKFILE, O_CREAT|O_RDWR|O_EXLOCK|O_NONBLOCK, 0600); + if (lockfd == -1) + errx(1, "already running"); + /* Check for assigned daemon user */ if (getpwnam(SLAACD_USER) == NULL) errx(1, "unknown user %s", SLAACD_USER); diff --git a/sbin/slaacd/slaacd.h b/sbin/slaacd/slaacd.h index 0bad1163846..7a53a29b160 100644 --- a/sbin/slaacd/slaacd.h +++ b/sbin/slaacd/slaacd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: slaacd.h,v 1.33 2021/03/21 18:25:24 florian Exp $ */ +/* $OpenBSD: slaacd.h,v 1.34 2021/07/21 03:53:50 kn Exp $ */ /* * Copyright (c) 2017 Florian Obser @@ -18,6 +18,7 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#define _PATH_LOCKFILE "/dev/slaacd.lock" #define _PATH_SLAACD_SOCKET "/dev/slaacd.sock" #define SLAACD_USER "_slaacd" #define SLAACD_RTA_LABEL "slaacd" -- 2.20.1