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 */
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 */
}
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;
}
static int hardlink_context_realize(HardlinkContext *c) {
- int r;
-
if (!c)
return 0;
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;
}
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);