From 776dc1b9d5294f217b0d717126dced4993395ee1 Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Sat, 3 Jan 2026 20:26:27 +0100 Subject: [PATCH 1/2] core/dynamic-user: flock() does not return EBUSY --- src/core/dynamic-user.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/dynamic-user.c b/src/core/dynamic-user.c index 9d7d96e4a3..576ce5c8b0 100644 --- a/src/core/dynamic-user.c +++ b/src/core/dynamic-user.c @@ -240,7 +240,7 @@ static int pick_uid(char **suggested_paths, const char *name, uid_t *ret_uid) { r = flock(lock_fd, LOCK_EX|LOCK_NB); /* Try to get a BSD file lock on the UID lock file */ if (r < 0) { - if (IN_SET(errno, EBUSY, EAGAIN)) + if (errno == EAGAIN) goto next; /* already in use */ return -errno; From 252b40c449ce71d21a2434c4c2ef6ccabc3ca037 Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Sat, 3 Jan 2026 20:25:53 +0100 Subject: [PATCH 2/2] core/dynamic-user: use fd_verify_linked() --- src/core/dynamic-user.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/core/dynamic-user.c b/src/core/dynamic-user.c index 576ce5c8b0..921622d92c 100644 --- a/src/core/dynamic-user.c +++ b/src/core/dynamic-user.c @@ -21,6 +21,7 @@ #include "serialize.h" #include "siphash24.h" #include "socket-util.h" +#include "stat-util.h" #include "stdio-util.h" #include "string-util.h" #include "uid-classification.h" @@ -232,8 +233,6 @@ static int pick_uid(char **suggested_paths, const char *name, uid_t *ret_uid) { xsprintf(lock_path, "/run/systemd/dynamic-uid/" UID_FMT, candidate); for (;;) { - struct stat st; - lock_fd = open(lock_path, O_CREAT|O_RDWR|O_NOFOLLOW|O_CLOEXEC|O_NOCTTY, 0600); if (lock_fd < 0) return -errno; @@ -246,10 +245,11 @@ static int pick_uid(char **suggested_paths, const char *name, uid_t *ret_uid) { return -errno; } - if (fstat(lock_fd, &st) < 0) - return -errno; - if (st.st_nlink > 0) + r = fd_verify_linked(lock_fd); + if (r >= 0) break; + if (r != -EIDRM) + return r; /* Oh, bummer, we got the lock, but the file was unlinked between the time we opened it and * got the lock. Close it, and try again. */