From: Lennart Poettering Date: Tue, 16 Nov 2021 14:24:07 +0000 (+0100) Subject: tree-wide: port various places over to open_mkdir_at() X-Git-Tag: v250-rc1~220^2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F21401%2Fhead;p=thirdparty%2Fsystemd.git tree-wide: port various places over to open_mkdir_at() --- diff --git a/src/core/namespace.c b/src/core/namespace.c index a84060c6826..c8e7e65e272 100644 --- a/src/core/namespace.c +++ b/src/core/namespace.c @@ -2499,6 +2499,7 @@ int temporary_filesystem_add( static int make_tmp_prefix(const char *prefix) { _cleanup_free_ char *t = NULL; + _cleanup_close_ int fd = -1; int r; /* Don't do anything unless we know the dir is actually missing */ @@ -2517,18 +2518,20 @@ static int make_tmp_prefix(const char *prefix) { if (r < 0) return r; - if (mkdir(t, 0777) < 0) /* umask will corrupt this access mode, but that doesn't matter, we need to - * call chmod() anyway for the suid bit, below. */ - return -errno; + /* umask will corrupt this access mode, but that doesn't matter, we need to call chmod() anyway for + * the suid bit, below. */ + fd = open_mkdir_at(AT_FDCWD, t, O_EXCL|O_CLOEXEC, 0777); + if (fd < 0) + return fd; - if (chmod(t, 01777) < 0) { - r = -errno; + r = RET_NERRNO(fchmod(fd, 01777)); + if (r < 0) { (void) rmdir(t); return r; } - if (rename(t, prefix) < 0) { - r = -errno; + r = RET_NERRNO(rename(t, prefix)); + if (r < 0) { (void) rmdir(t); return r == -EEXIST ? 0 : r; /* it's fine if someone else created the dir by now */ } diff --git a/src/home/homework-cifs.c b/src/home/homework-cifs.c index c76d6a6b13e..b49b7b3dcdb 100644 --- a/src/home/homework-cifs.c +++ b/src/home/homework-cifs.c @@ -127,15 +127,17 @@ int home_setup_cifs( return log_oom(); if (FLAGS_SET(flags, HOME_SETUP_CIFS_MKDIR)) { - r = mkdir_p(j, 0700); - if (r < 0) - return log_error_errno(r, "Failed to create CIFS subdirectory: %m"); + setup->root_fd = open_mkdir_at(AT_FDCWD, j, O_CLOEXEC, 0700); + if (setup->root_fd < 0) + return log_error_errno(setup->root_fd, "Failed to create CIFS subdirectory: %m"); } } - setup->root_fd = open(j ?: HOME_RUNTIME_WORK_DIR, O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOFOLLOW); - if (setup->root_fd < 0) - return log_error_errno(errno, "Failed to open home directory: %m"); + if (setup->root_fd < 0) { + setup->root_fd = open(j ?: HOME_RUNTIME_WORK_DIR, O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOFOLLOW); + if (setup->root_fd < 0) + return log_error_errno(errno, "Failed to open home directory: %m"); + } setup->mount_suffix = TAKE_PTR(cdir); return 0; diff --git a/src/shared/copy.c b/src/shared/copy.c index fd83d74265e..bb3ac8a3f85 100644 --- a/src/shared/copy.c +++ b/src/shared/copy.c @@ -485,8 +485,6 @@ static int hardlink_context_setup( } static int hardlink_context_realize(HardlinkContext *c) { - int r; - if (!c) return 0; @@ -498,15 +496,9 @@ static int hardlink_context_realize(HardlinkContext *c) { assert(c->subdir); - if (mkdirat(c->parent_fd, c->subdir, 0700) < 0) - return -errno; - - c->dir_fd = openat(c->parent_fd, c->subdir, O_RDONLY|O_DIRECTORY|O_CLOEXEC); - if (c->dir_fd < 0) { - r = -errno; - (void) unlinkat(c->parent_fd, c->subdir, AT_REMOVEDIR); - return r; - } + c->dir_fd = open_mkdir_at(c->parent_fd, c->subdir, O_EXCL|O_CLOEXEC, 0700); + if (c->dir_fd < 0) + return c->dir_fd; return 1; } diff --git a/src/shared/creds-util.c b/src/shared/creds-util.c index b764198b76c..0c8181bce29 100644 --- a/src/shared/creds-util.c +++ b/src/shared/creds-util.c @@ -215,10 +215,10 @@ int get_credential_host_secret(CredentialSecretFlags flags, void **ret, size_t * fn = "credential.secret"; } - (void) mkdir_p(p, 0755); - dfd = open(p, O_CLOEXEC|O_DIRECTORY|O_RDONLY); + mkdir_parents(p, 0755); + dfd = open_mkdir_at(AT_FDCWD, p, O_CLOEXEC, 0755); if (dfd < 0) - return -errno; + return dfd; if (FLAGS_SET(flags, CREDENTIAL_SECRET_FAIL_ON_TEMPORARY_FS)) { r = fd_is_temporary_fs(dfd);