]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tree-wide: port various places over to open_mkdir_at() 21401/head
authorLennart Poettering <lennart@poettering.net>
Tue, 16 Nov 2021 14:24:07 +0000 (15:24 +0100)
committerLennart Poettering <lennart@poettering.net>
Wed, 17 Nov 2021 20:48:29 +0000 (21:48 +0100)
src/core/namespace.c
src/home/homework-cifs.c
src/shared/copy.c
src/shared/creds-util.c

index a84060c68261eaf37a74b9229726d832b9d7cb2b..c8e7e65e2722cd2b5a9a5d8d61dcaefe800899c5 100644 (file)
@@ -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 */
         }
index c76d6a6b13edcdb281993769badf3fcbb29d153a..b49b7b3dcdb9db13db2ad501a910f42953813269 100644 (file)
@@ -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;
index fd83d74265e185fa2fecabe1abc2494c11ad3176..bb3ac8a3f85301fed4296d51ebf2895b27921501 100644 (file)
@@ -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;
 }
index b764198b76cbc277092e6d0825868bd7bf03e324..0c8181bce29b7303d00db5faa1252605c3368441 100644 (file)
@@ -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);