]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
user-util: Use unposix_lock() in take_etc_passwd_lock()
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Thu, 9 Mar 2023 11:28:31 +0000 (12:28 +0100)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Fri, 10 Mar 2023 10:57:43 +0000 (11:57 +0100)
src/basic/user-util.c

index 6c5041230a1cb38164423c36707ba6dfeaba0f21..0a47947d34853e174227eb01582d0534b7c687a7 100644 (file)
@@ -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);
 }