From 460accdf3d681d9a5ccf4fc6356e73b6b323e72c Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Thu, 9 Mar 2023 12:28:31 +0100 Subject: [PATCH] user-util: Use unposix_lock() in take_etc_passwd_lock() --- src/basic/user-util.c | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/basic/user-util.c b/src/basic/user-util.c index 6c5041230a..0a47947d34 100644 --- a/src/basic/user-util.c +++ b/src/basic/user-util.c @@ -18,6 +18,7 @@ #include "fd-util.h" #include "fileio.h" #include "format-util.h" +#include "lock-util.h" #include "macro.h" #include "mkdir.h" #include "parse-util.h" @@ -671,12 +672,7 @@ int reset_uid_gid(void) { } int take_etc_passwd_lock(const char *root) { - struct flock flock = { - .l_type = F_WRLCK, - .l_whence = SEEK_SET, - .l_start = 0, - .l_len = 0, - }; + int r; /* This is roughly the same as lckpwdf(), but not as awful. We don't want to use alarm() and signals, * hence we implement our own trivial version of this. @@ -695,8 +691,9 @@ int take_etc_passwd_lock(const char *root) { if (fd < 0) return log_debug_errno(errno, "Cannot open %s: %m", path); - if (fcntl(fd, F_SETLKW, &flock) < 0) - return log_debug_errno(errno, "Locking %s failed: %m", path); + r = unposix_lock(fd, LOCK_EX); + if (r < 0) + return log_debug_errno(r, "Locking %s failed: %m", path); return TAKE_FD(fd); } -- 2.25.1