From e929bee09ab8000e87b7e825ed3a78d73ecdd7f0 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 26 Oct 2015 19:08:09 +0100 Subject: util-lib: move take_password_lock() to user-util.[ch] Also, rename it take_etc_passwd_lock(), in order to make it more expressive. --- src/basic/user-util.c | 41 +++++++++++++++++++++++++++++++++++++++++ src/basic/user-util.h | 2 ++ src/basic/util.c | 40 ---------------------------------------- src/basic/util.h | 2 -- src/firstboot/firstboot.c | 3 ++- src/sysusers/sysusers.c | 2 +- 6 files changed, 46 insertions(+), 44 deletions(-) diff --git a/src/basic/user-util.c b/src/basic/user-util.c index 0b3cf3d733..bdee77a7d2 100644 --- a/src/basic/user-util.c +++ b/src/basic/user-util.c @@ -22,6 +22,7 @@ #include #include +#include "fd-util.h" #include "macro.h" #include "parse-util.h" #include "path-util.h" @@ -428,3 +429,43 @@ int reset_uid_gid(void) { return 0; } + +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, + }; + + 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. + * + * Note that shadow-utils also takes per-database locks in + * addition to lckpwdf(). However, we don't given that they + * are redundant as they 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/.pwd.lock"); + else + path = "/etc/.pwd.lock"; + + fd = open(path, O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, 0600); + if (fd < 0) + return -errno; + + r = fcntl(fd, F_SETLKW, &flock); + if (r < 0) { + safe_close(fd); + return -errno; + } + + return fd; +} diff --git a/src/basic/user-util.h b/src/basic/user-util.h index 7995698f27..4496f58ba3 100644 --- a/src/basic/user-util.h +++ b/src/basic/user-util.h @@ -52,3 +52,5 @@ int get_home_dir(char **ret); int get_shell(char **_ret); int reset_uid_gid(void); + +int take_etc_passwd_lock(const char *root); diff --git a/src/basic/util.c b/src/basic/util.c index 576c6238d6..2ee5de9cd0 100644 --- a/src/basic/util.c +++ b/src/basic/util.c @@ -2096,46 +2096,6 @@ int update_reboot_param_file(const char *param) { return 0; } -int take_password_lock(const char *root) { - - struct flock flock = { - .l_type = F_WRLCK, - .l_whence = SEEK_SET, - .l_start = 0, - .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. - * - * Note that shadow-utils also takes per-database locks in - * addition to lckpwdf(). However, we don't given that they - * are redundant as they 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 = strjoina(root, "/etc/.pwd.lock"); - else - path = "/etc/.pwd.lock"; - - fd = open(path, O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, 0600); - if (fd < 0) - return -errno; - - r = fcntl(fd, F_SETLKW, &flock); - if (r < 0) { - safe_close(fd); - return -errno; - } - - return fd; -} - int is_symlink(const char *path) { struct stat info; diff --git a/src/basic/util.h b/src/basic/util.h index f96b493d9d..76d0784d36 100644 --- a/src/basic/util.h +++ b/src/basic/util.h @@ -519,8 +519,6 @@ union file_handle_union { int update_reboot_param_file(const char *param); -int take_password_lock(const char *root); - int is_symlink(const char *path); int is_dir(const char *path, bool follow); int is_device_node(const char *path); diff --git a/src/firstboot/firstboot.c b/src/firstboot/firstboot.c index abb5e77966..564bd50f9b 100644 --- a/src/firstboot/firstboot.c +++ b/src/firstboot/firstboot.c @@ -38,6 +38,7 @@ #include "strv.h" #include "terminal-util.h" #include "time-util.h" +#include "user-util.h" static char *arg_root = NULL; static char *arg_locale = NULL; /* $LANG */ @@ -536,7 +537,7 @@ static int process_root_password(void) { mkdir_parents(etc_shadow, 0755); - lock = take_password_lock(arg_root); + lock = take_etc_passwd_lock(arg_root); if (lock < 0) return lock; diff --git a/src/sysusers/sysusers.c b/src/sysusers/sysusers.c index 177432bf9f..5d2cbe1225 100644 --- a/src/sysusers/sysusers.c +++ b/src/sysusers/sysusers.c @@ -1859,7 +1859,7 @@ int main(int argc, char *argv[]) { if (r < 0) goto finish; - lock = take_password_lock(arg_root); + lock = take_etc_passwd_lock(arg_root); if (lock < 0) { log_error_errno(lock, "Failed to take lock: %m"); goto finish; -- cgit v1.2.3-54-g00ecf