summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2023-01-31 16:17:12 +0100
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2023-02-01 11:38:35 +0100
commite5b30f7232acfc2e99bce50ac1e683deaec57140 (patch)
treed5c2ca744c256ee34aa17dfaee7b796e6c9aef2c
parentfddb524d8a9fcfbf943c88b5c7d13eba7ac0083d (diff)
downloadsystemd-e5b30f7232acfc2e99bce50ac1e683deaec57140.tar.gz
basic/user-util: convert prefix_roota→path_join and use _cleanup_ more
-rw-r--r--src/basic/user-util.c20
1 files changed, 6 insertions, 14 deletions
diff --git a/src/basic/user-util.c b/src/basic/user-util.c
index 3daca626b8..734122c2a3 100644
--- a/src/basic/user-util.c
+++ b/src/basic/user-util.c
@@ -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) {