Skip to content

Commit 96603ea

Browse files
committed
tree-wide: port various places over to open_mkdir_at()
1 parent c73094f commit 96603ea

File tree

4 files changed

+24
-27
lines changed

4 files changed

+24
-27
lines changed

src/core/namespace.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2499,6 +2499,7 @@ int temporary_filesystem_add(
24992499

25002500
static int make_tmp_prefix(const char *prefix) {
25012501
_cleanup_free_ char *t = NULL;
2502+
_cleanup_close_ int fd = -1;
25022503
int r;
25032504

25042505
/* Don't do anything unless we know the dir is actually missing */
@@ -2517,18 +2518,20 @@ static int make_tmp_prefix(const char *prefix) {
25172518
if (r < 0)
25182519
return r;
25192520

2520-
if (mkdir(t, 0777) < 0) /* umask will corrupt this access mode, but that doesn't matter, we need to
2521-
* call chmod() anyway for the suid bit, below. */
2522-
return -errno;
2521+
/* umask will corrupt this access mode, but that doesn't matter, we need to call chmod() anyway for
2522+
* the suid bit, below. */
2523+
fd = open_mkdir_at(AT_FDCWD, t, O_EXCL|O_CLOEXEC, 0777);
2524+
if (fd < 0)
2525+
return fd;
25232526

2524-
if (chmod(t, 01777) < 0) {
2525-
r = -errno;
2527+
r = RET_NERRNO(fchmod(fd, 01777));
2528+
if (r < 0) {
25262529
(void) rmdir(t);
25272530
return r;
25282531
}
25292532

2530-
if (rename(t, prefix) < 0) {
2531-
r = -errno;
2533+
r = RET_NERRNO(rename(t, prefix));
2534+
if (r < 0) {
25322535
(void) rmdir(t);
25332536
return r == -EEXIST ? 0 : r; /* it's fine if someone else created the dir by now */
25342537
}

src/home/homework-cifs.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,17 @@ int home_setup_cifs(
127127
return log_oom();
128128

129129
if (FLAGS_SET(flags, HOME_SETUP_CIFS_MKDIR)) {
130-
r = mkdir_p(j, 0700);
131-
if (r < 0)
132-
return log_error_errno(r, "Failed to create CIFS subdirectory: %m");
130+
setup->root_fd = open_mkdir_at(AT_FDCWD, j, O_CLOEXEC, 0700);
131+
if (setup->root_fd < 0)
132+
return log_error_errno(setup->root_fd, "Failed to create CIFS subdirectory: %m");
133133
}
134134
}
135135

136-
setup->root_fd = open(j ?: HOME_RUNTIME_WORK_DIR, O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOFOLLOW);
137-
if (setup->root_fd < 0)
138-
return log_error_errno(errno, "Failed to open home directory: %m");
136+
if (setup->root_fd < 0) {
137+
setup->root_fd = open(j ?: HOME_RUNTIME_WORK_DIR, O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOFOLLOW);
138+
if (setup->root_fd < 0)
139+
return log_error_errno(errno, "Failed to open home directory: %m");
140+
}
139141

140142
setup->mount_suffix = TAKE_PTR(cdir);
141143
return 0;

src/shared/copy.c

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -485,8 +485,6 @@ static int hardlink_context_setup(
485485
}
486486

487487
static int hardlink_context_realize(HardlinkContext *c) {
488-
int r;
489-
490488
if (!c)
491489
return 0;
492490

@@ -498,15 +496,9 @@ static int hardlink_context_realize(HardlinkContext *c) {
498496

499497
assert(c->subdir);
500498

501-
if (mkdirat(c->parent_fd, c->subdir, 0700) < 0)
502-
return -errno;
503-
504-
c->dir_fd = openat(c->parent_fd, c->subdir, O_RDONLY|O_DIRECTORY|O_CLOEXEC);
505-
if (c->dir_fd < 0) {
506-
r = -errno;
507-
(void) unlinkat(c->parent_fd, c->subdir, AT_REMOVEDIR);
508-
return r;
509-
}
499+
c->dir_fd = open_mkdir_at(c->parent_fd, c->subdir, O_EXCL|O_CLOEXEC, 0700);
500+
if (c->dir_fd < 0)
501+
return c->dir_fd;
510502

511503
return 1;
512504
}

src/shared/creds-util.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,10 @@ int get_credential_host_secret(CredentialSecretFlags flags, void **ret, size_t *
215215
fn = "credential.secret";
216216
}
217217

218-
(void) mkdir_p(p, 0755);
219-
dfd = open(p, O_CLOEXEC|O_DIRECTORY|O_RDONLY);
218+
mkdir_parents(p, 0755);
219+
dfd = open_mkdir_at(AT_FDCWD, p, O_CLOEXEC, 0755);
220220
if (dfd < 0)
221-
return -errno;
221+
return dfd;
222222

223223
if (FLAGS_SET(flags, CREDENTIAL_SECRET_FAIL_ON_TEMPORARY_FS)) {
224224
r = fd_is_temporary_fs(dfd);

0 commit comments

Comments
 (0)