]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
basic/user-util: convert prefix_roota→path_join and use _cleanup_ more
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 31 Jan 2023 15:17:12 +0000 (16:17 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 1 Feb 2023 10:38:35 +0000 (11:38 +0100)
src/basic/user-util.c

index 3daca626b8f05a3fd95b6832850a82cd43804078..734122c2a3c0b12dce1fb6c39ae209d97b5f2f67 100644 (file)
@@ -670,7 +670,6 @@ int reset_uid_gid(void) {
 }
 
 int take_etc_passwd_lock(const char *root) {
-
         struct flock flock = {
                 .l_type = F_WRLCK,
                 .l_whence = SEEK_SET,
@@ -678,9 +677,6 @@ int take_etc_passwd_lock(const char *root) {
                 .l_len = 0,
         };
 
-        const char *path;
-        int fd, 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.
          *
@@ -688,22 +684,18 @@ int take_etc_passwd_lock(const char *root) {
          * given that they are redundant: they invoke lckpwdf() first and keep it during everything they do.
          * The per-database locks are awfully racy, and thus we just won't do them. */
 
-        if (root)
-                path = prefix_roota(root, ETC_PASSWD_LOCK_PATH);
-        else
-                path = ETC_PASSWD_LOCK_PATH;
+        _cleanup_free_ char *path = path_join(root, ETC_PASSWD_LOCK_PATH);
+        if (!path)
+                return log_oom_debug();
 
-        fd = open(path, O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, 0600);
+        _cleanup_close_ int fd = open(path, O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, 0600);
         if (fd < 0)
                 return log_debug_errno(errno, "Cannot open %s: %m", path);
 
-        r = fcntl(fd, F_SETLKW, &flock);
-        if (r < 0) {
-                safe_close(fd);
+        if (fcntl(fd, F_SETLKW, &flock) < 0)
                 return log_debug_errno(errno, "Locking %s failed: %m", path);
-        }
 
-        return fd;
+        return TAKE_FD(fd);
 }
 
 bool valid_user_group_name(const char *u, ValidUserFlags flags) {