From a16255189687df4eec8a8e73b599dd04be544b67 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Fri, 13 Nov 2026 17:02:04 +0100 Subject: [PATCH] service: bind the systemd notify socket before fork A notify:systemd service that calls sd_notify() early could lose its READY=1 and never become ready. Seen with udevd in an initramfs. The socket address contained the PID of the service, so it was bound in the parent after fork(), racing the service. Let the kernel pick a unique abstract address instead, bound before fork(), and pass it in NOTIFY_SOCKET. Fixes #505 Signed-off-by: Joachim Wiberg --- doc/ChangeLog.md | 3 +++ src/service.c | 54 +++++++++++++++++++++++++++--------------------- 2 files changed, 34 insertions(+), 23 deletions(-) diff --git a/doc/ChangeLog.md b/doc/ChangeLog.md index 4abad8eb..a0250772 100644 --- a/doc/ChangeLog.md +++ b/doc/ChangeLog.md @@ -166,6 +166,9 @@ All relevant changes are documented in this file. - Fix #503: keventd did not touch its PID file after SIGHUP, so with a daemon-owned PID file Finit never saw it reassert readiness after a reload. Reported by Aaron Andersen +- Fix #505: a `notify:systemd` service could send `READY=1` before Finit + had bound the notify socket, losing it, so the service never became + ready. Seen with udevd in an initramfs. Reported by Aaron Andersen - Fix a command whose name starts like a service option, e.g. `logger` or `pidof`. It was taken as the `log` or `pid` option and the next argument ran as the command, which also broke the `logger` example in finit.conf(5) diff --git a/src/service.c b/src/service.c index af57948c..5bda279f 100644 --- a/src/service.c +++ b/src/service.c @@ -65,8 +65,6 @@ #include "utmp-api.h" #include "schedule.h" -#define NOTIFY_PATH "@run/finit/notify/%d" - int logfile_size_max = 200000; /* 200 kB */ int logfile_count_max = 5; @@ -895,6 +893,26 @@ static int pam_refused(svc_t *svc) return 1; } +/* + * Bind the systemd notify socket before fork since the daemon may + * notify right away, see issue #505. The kernel picks a unique abstract + * address, which NOTIFY_SOCKET spells with a leading '@'. + */ +static int notify_bind(int sd, char *path, size_t len) +{ + struct sockaddr_un sun = { .sun_family = AF_UNIX }; + socklen_t slen = sizeof(sun); + + if (bind(sd, (struct sockaddr *)&sun, sizeof(sa_family_t)) || + getsockname(sd, (struct sockaddr *)&sun, &slen)) + return -1; + + slen -= offsetof(struct sockaddr_un, sun_path) + 1; + snprintf(path, len, "@%.*s", (int)slen, &sun.sun_path[1]); + + return 0; +} + /** * service_start - Start service * @svc: Service to start @@ -904,6 +922,7 @@ static int pam_refused(svc_t *svc) */ static int service_start(svc_t *svc) { + char notify[sizeof(struct sockaddr_un)]; int result = 0, do_progress = 1; char cmdline[CMD_SIZE] = ""; sigset_t nmask, omask; @@ -987,6 +1006,12 @@ static int service_start(svc_t *svc) svc_missing(svc); return 1; } + if (notify_bind(sd, notify, sizeof(notify))) { + err(1, "%s: failed binding to notify socket", svc_ident(svc, NULL, 0)); + close(sd); + svc_missing(svc); + return 1; + } break; default: break; @@ -1025,31 +1050,16 @@ static int service_start(svc_t *svc) result = -1; goto fail; } else if (pid > 0) { - struct sockaddr_un sun; - size_t len; - dbg("Starting %s as PID %d", svc_ident(svc, NULL, 0), pid); svc->pid = pid; svc->start_time = jiffies(); + if (fd != -1) + close(fd); /* client-end of pipefd for s6 notify */ + switch (svc->notify) { case SVC_NOTIFY_SYSTEMD: - memset(&sun, 0, sizeof(sun)); - sun.sun_family = AF_UNIX; - snprintf(sun.sun_path, sizeof(sun.sun_path), NOTIFY_PATH, pid); - len = strlen(sun.sun_path); - sun.sun_path[0] = 0; - result = bind(sd, (struct sockaddr *)&sun, offsetof(struct sockaddr_un, sun_path) + len); - if (result == -1) { - err(1, "%s: failed binding to notify socket", svc_ident(svc, NULL, 0)); - close(sd); - break; - } - /* fallthrough */ case SVC_NOTIFY_S6: - if (svc->notify == SVC_NOTIFY_S6) - close(fd); /* client-end of pipefd for s6 notify */ - result = uev_io_init(ctx, &svc->notify_watcher, service_notify_cb, svc, sd, UEV_READ); if (result < 0) { err(1, "%s: failed setting up notify callback", svc_ident(svc, NULL, 0)); @@ -1060,7 +1070,6 @@ static int service_start(svc_t *svc) break; } } else { /* pid == 0 */ - char str[strlen(NOTIFY_PATH) + 32]; char *args[MAX_NUM_SVC_ARGS + 3]; int status; @@ -1069,8 +1078,7 @@ static int service_start(svc_t *svc) switch (svc->notify) { case SVC_NOTIFY_SYSTEMD: - snprintf(str, sizeof(str), NOTIFY_PATH, getpid()); - setenv("NOTIFY_SOCKET", str, 1); + setenv("NOTIFY_SOCKET", notify, 1); /* fallthrough */ case SVC_NOTIFY_S6: close(sd);