mirror of
https://github.com/morgan9e/systemd
synced 2026-04-15 00:47:10 +09:00
Merge pull request #7663 from keszybz/mkdir-return-value
util-lib: fix return value in mkdir_parents()
This commit is contained in:
@@ -779,13 +779,11 @@ int cg_create(const char *controller, const char *path) {
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
if (mkdir(fs, 0755) < 0) {
|
||||
|
||||
if (errno == EEXIST)
|
||||
return 0;
|
||||
|
||||
return -errno;
|
||||
}
|
||||
r = mkdir_errno_wrapper(fs, 0755);
|
||||
if (r == -EEXIST)
|
||||
return 0;
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
r = cg_hybrid_unified();
|
||||
if (r < 0)
|
||||
|
||||
@@ -42,26 +42,6 @@ int label_fix(const char *path, bool ignore_enoent, bool ignore_erofs) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int mkdir_label(const char *path, mode_t mode) {
|
||||
int r;
|
||||
|
||||
assert(path);
|
||||
|
||||
r = mac_selinux_create_file_prepare(path, S_IFDIR);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
if (mkdir(path, mode) < 0)
|
||||
r = -errno;
|
||||
|
||||
mac_selinux_create_file_clear();
|
||||
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
return mac_smack_fix(path, false, false);
|
||||
}
|
||||
|
||||
int symlink_label(const char *old_path, const char *new_path) {
|
||||
int r;
|
||||
|
||||
|
||||
@@ -20,11 +20,32 @@
|
||||
***/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "label.h"
|
||||
#include "macro.h"
|
||||
#include "mkdir.h"
|
||||
#include "selinux-util.h"
|
||||
#include "smack-util.h"
|
||||
|
||||
int mkdir_label(const char *path, mode_t mode) {
|
||||
int r;
|
||||
|
||||
assert(path);
|
||||
|
||||
r = mac_selinux_create_file_prepare(path, S_IFDIR);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
r = mkdir_errno_wrapper(path, mode);
|
||||
mac_selinux_create_file_clear();
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
return mac_smack_fix(path, false, false);
|
||||
}
|
||||
|
||||
int mkdir_safe_label(const char *path, mode_t mode, uid_t uid, gid_t gid, bool follow_symlink) {
|
||||
return mkdir_safe_internal(path, mode, uid, gid, follow_symlink, mkdir_label);
|
||||
|
||||
@@ -35,6 +35,8 @@ int mkdir_safe_internal(const char *path, mode_t mode, uid_t uid, gid_t gid, boo
|
||||
struct stat st;
|
||||
int r;
|
||||
|
||||
assert(_mkdir != mkdir);
|
||||
|
||||
if (_mkdir(path, mode) >= 0) {
|
||||
r = chmod_and_chown(path, mode, uid, gid);
|
||||
if (r < 0)
|
||||
@@ -68,8 +70,14 @@ int mkdir_safe_internal(const char *path, mode_t mode, uid_t uid, gid_t gid, boo
|
||||
return 0;
|
||||
}
|
||||
|
||||
int mkdir_errno_wrapper(const char *pathname, mode_t mode) {
|
||||
if (mkdir(pathname, mode) < 0)
|
||||
return -errno;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int mkdir_safe(const char *path, mode_t mode, uid_t uid, gid_t gid, bool follow_symlink) {
|
||||
return mkdir_safe_internal(path, mode, uid, gid, follow_symlink, mkdir);
|
||||
return mkdir_safe_internal(path, mode, uid, gid, follow_symlink, mkdir_errno_wrapper);
|
||||
}
|
||||
|
||||
int mkdir_parents_internal(const char *prefix, const char *path, mode_t mode, mkdir_func_t _mkdir) {
|
||||
@@ -77,6 +85,7 @@ int mkdir_parents_internal(const char *prefix, const char *path, mode_t mode, mk
|
||||
int r;
|
||||
|
||||
assert(path);
|
||||
assert(_mkdir != mkdir);
|
||||
|
||||
if (prefix && !path_startswith(path, prefix))
|
||||
return -ENOTDIR;
|
||||
@@ -104,8 +113,7 @@ int mkdir_parents_internal(const char *prefix, const char *path, mode_t mode, mk
|
||||
e = p + strcspn(p, "/");
|
||||
p = e + strspn(e, "/");
|
||||
|
||||
/* Is this the last component? If so, then we're
|
||||
* done */
|
||||
/* Is this the last component? If so, then we're done */
|
||||
if (*p == 0)
|
||||
return 0;
|
||||
|
||||
@@ -116,13 +124,13 @@ int mkdir_parents_internal(const char *prefix, const char *path, mode_t mode, mk
|
||||
continue;
|
||||
|
||||
r = _mkdir(t, mode);
|
||||
if (r < 0 && errno != EEXIST)
|
||||
return -errno;
|
||||
if (r < 0 && r != -EEXIST)
|
||||
return r;
|
||||
}
|
||||
}
|
||||
|
||||
int mkdir_parents(const char *path, mode_t mode) {
|
||||
return mkdir_parents_internal(NULL, path, mode, mkdir);
|
||||
return mkdir_parents_internal(NULL, path, mode, mkdir_errno_wrapper);
|
||||
}
|
||||
|
||||
int mkdir_p_internal(const char *prefix, const char *path, mode_t mode, mkdir_func_t _mkdir) {
|
||||
@@ -130,17 +138,19 @@ int mkdir_p_internal(const char *prefix, const char *path, mode_t mode, mkdir_fu
|
||||
|
||||
/* Like mkdir -p */
|
||||
|
||||
assert(_mkdir != mkdir);
|
||||
|
||||
r = mkdir_parents_internal(prefix, path, mode, _mkdir);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
r = _mkdir(path, mode);
|
||||
if (r < 0 && (errno != EEXIST || is_dir(path, true) <= 0))
|
||||
return -errno;
|
||||
if (r < 0 && (r != -EEXIST || is_dir(path, true) <= 0))
|
||||
return r;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int mkdir_p(const char *path, mode_t mode) {
|
||||
return mkdir_p_internal(NULL, path, mode, mkdir);
|
||||
return mkdir_p_internal(NULL, path, mode, mkdir_errno_wrapper);
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
int mkdir_errno_wrapper(const char *pathname, mode_t mode);
|
||||
int mkdir_safe(const char *path, mode_t mode, uid_t uid, gid_t gid, bool follow_symlink);
|
||||
int mkdir_parents(const char *path, mode_t mode);
|
||||
int mkdir_p(const char *path, mode_t mode);
|
||||
|
||||
@@ -136,7 +136,7 @@ int mac_smack_apply_pid(pid_t pid, const char *label) {
|
||||
|
||||
int mac_smack_fix(const char *path, bool ignore_enoent, bool ignore_erofs) {
|
||||
struct stat st;
|
||||
int r = 0;
|
||||
int r;
|
||||
|
||||
assert(path);
|
||||
|
||||
|
||||
@@ -931,7 +931,7 @@ int bus_machine_method_bind_mount(sd_bus_message *message, void *userdata, sd_bu
|
||||
/* Second, we mount the source file or directory to a directory inside of our MS_SLAVE playground. */
|
||||
mount_tmp = strjoina(mount_slave, "/mount");
|
||||
if (S_ISDIR(st.st_mode))
|
||||
r = mkdir(mount_tmp, 0700) < 0 ? -errno : 0;
|
||||
r = mkdir_errno_wrapper(mount_tmp, 0700);
|
||||
else
|
||||
r = touch(mount_tmp);
|
||||
if (r < 0) {
|
||||
|
||||
@@ -474,9 +474,9 @@ static int mkdir_userns(const char *path, mode_t mode, MountSettingsMask mask, u
|
||||
|
||||
assert(path);
|
||||
|
||||
r = mkdir(path, mode);
|
||||
if (r < 0 && errno != EEXIST)
|
||||
return -errno;
|
||||
r = mkdir_errno_wrapper(path, mode);
|
||||
if (r < 0 && r != -EEXIST)
|
||||
return r;
|
||||
|
||||
if ((mask & MOUNT_USE_USERNS) == 0)
|
||||
return 0;
|
||||
@@ -484,8 +484,7 @@ static int mkdir_userns(const char *path, mode_t mode, MountSettingsMask mask, u
|
||||
if (mask & MOUNT_IN_USERNS)
|
||||
return 0;
|
||||
|
||||
r = lchown(path, uid_shift, uid_shift);
|
||||
if (r < 0)
|
||||
if (lchown(path, uid_shift, uid_shift) < 0)
|
||||
return -errno;
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -1313,13 +1313,14 @@ static int userns_lchown(const char *p, uid_t uid, gid_t gid) {
|
||||
|
||||
static int userns_mkdir(const char *root, const char *path, mode_t mode, uid_t uid, gid_t gid) {
|
||||
const char *q;
|
||||
int r;
|
||||
|
||||
q = prefix_roota(root, path);
|
||||
if (mkdir(q, mode) < 0) {
|
||||
if (errno == EEXIST)
|
||||
return 0;
|
||||
return -errno;
|
||||
}
|
||||
r = mkdir_errno_wrapper(q, mode);
|
||||
if (r == -EEXIST)
|
||||
return 0;
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
return userns_lchown(q, uid, gid);
|
||||
}
|
||||
@@ -1599,8 +1600,10 @@ static int setup_pts(const char *dest) {
|
||||
|
||||
/* Mount /dev/pts itself */
|
||||
p = prefix_roota(dest, "/dev/pts");
|
||||
if (mkdir(p, 0755) < 0)
|
||||
return log_error_errno(errno, "Failed to create /dev/pts: %m");
|
||||
r = mkdir_errno_wrapper(p, 0755);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to create /dev/pts: %m");
|
||||
|
||||
r = mount_verbose(LOG_ERR, "devpts", p, "devpts", MS_NOSUID|MS_NOEXEC, options);
|
||||
if (r < 0)
|
||||
return r;
|
||||
@@ -1846,12 +1849,13 @@ static int setup_journal(const char *directory) {
|
||||
/* don't create parents here — if the host doesn't have
|
||||
* permanent journal set up, don't force it here */
|
||||
|
||||
if (mkdir(p, 0755) < 0 && errno != EEXIST) {
|
||||
r = mkdir_errno_wrapper(p, 0755);
|
||||
if (r < 0 && r != -EEXIST) {
|
||||
if (try) {
|
||||
log_debug_errno(errno, "Failed to create %s, skipping journal setup: %m", p);
|
||||
log_debug_errno(r, "Failed to create %s, skipping journal setup: %m", p);
|
||||
return 0;
|
||||
} else
|
||||
return log_error_errno(errno, "Failed to create %s: %m", p);
|
||||
return log_error_errno(r, "Failed to create %s: %m", p);
|
||||
}
|
||||
|
||||
} else if (access(p, F_OK) < 0)
|
||||
|
||||
@@ -1700,9 +1700,9 @@ int main(int argc, char *argv[]) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
r = mkdir("/run/udev", 0755);
|
||||
if (r < 0 && errno != EEXIST) {
|
||||
r = log_error_errno(errno, "could not create /run/udev: %m");
|
||||
r = mkdir_errno_wrapper("/run/udev", 0755);
|
||||
if (r < 0 && r != -EEXIST) {
|
||||
log_error_errno(r, "could not create /run/udev: %m");
|
||||
goto exit;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user