Merge pull request #21326 from poettering/mkdir-tweaks
various tweaks to mkdir code
This commit is contained in:
@@ -33,7 +33,7 @@
|
||||
#include "macro.h"
|
||||
#include "memory-util.h"
|
||||
#include "missing_syscall.h"
|
||||
#include "mkdir.h"
|
||||
#include "mkdir-label.h"
|
||||
#include "process-util.h"
|
||||
#include "random-util.h"
|
||||
#include "signal-util.h"
|
||||
|
||||
@@ -292,7 +292,7 @@ int cg_create(const char *controller, const char *path) {
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
r = mkdir_errno_wrapper(fs, 0755);
|
||||
r = RET_NERRNO(mkdir(fs, 0755));
|
||||
if (r == -EEXIST)
|
||||
return 0;
|
||||
if (r < 0)
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
#include "io-util.h"
|
||||
#include "macro.h"
|
||||
#include "missing_syscall.h"
|
||||
#include "mkdir-label.h"
|
||||
#include "mountpoint-util.h"
|
||||
#include "nulstr-util.h"
|
||||
#include "rm-rf.h"
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include "dev-setup.h"
|
||||
#include "label.h"
|
||||
#include "log.h"
|
||||
#include "mkdir-label.h"
|
||||
#include "nulstr-util.h"
|
||||
#include "path-util.h"
|
||||
#include "umask-util.h"
|
||||
|
||||
@@ -46,7 +46,7 @@
|
||||
#include "hostname-setup.h"
|
||||
#include "id128-util.h"
|
||||
#include "import-util.h"
|
||||
#include "mkdir.h"
|
||||
#include "mkdir-label.h"
|
||||
#include "mount-util.h"
|
||||
#include "mountpoint-util.h"
|
||||
#include "namespace-util.h"
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
#include "generator.h"
|
||||
#include "log.h"
|
||||
#include "macro.h"
|
||||
#include "mkdir.h"
|
||||
#include "mkdir-label.h"
|
||||
#include "path-util.h"
|
||||
#include "special.h"
|
||||
#include "specifier.h"
|
||||
@@ -63,7 +63,7 @@ int generator_add_symlink(const char *dir, const char *dst, const char *dep_type
|
||||
from = path_is_absolute(src) ? src : strjoina("../", src);
|
||||
to = strjoina(dir, "/", dst, ".", dep_type, "/", basename(src));
|
||||
|
||||
mkdir_parents_label(to, 0755);
|
||||
(void) mkdir_parents_label(to, 0755);
|
||||
if (symlink(from, to) < 0)
|
||||
if (errno != EEXIST)
|
||||
return log_error_errno(errno, "Failed to create symlink \"%s\": %m", to);
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
#include "hwdb-internal.h"
|
||||
#include "hwdb-util.h"
|
||||
#include "label.h"
|
||||
#include "mkdir.h"
|
||||
#include "mkdir-label.h"
|
||||
#include "nulstr-util.h"
|
||||
#include "path-util.h"
|
||||
#include "sort-util.h"
|
||||
|
||||
@@ -26,7 +26,7 @@
|
||||
#include "locale-util.h"
|
||||
#include "log.h"
|
||||
#include "macro.h"
|
||||
#include "mkdir.h"
|
||||
#include "mkdir-label.h"
|
||||
#include "path-lookup.h"
|
||||
#include "path-util.h"
|
||||
#include "rm-rf.h"
|
||||
@@ -461,7 +461,7 @@ static int create_symlink(
|
||||
* the right place, or negative on error.
|
||||
*/
|
||||
|
||||
mkdir_parents_label(new_path, 0755);
|
||||
(void) mkdir_parents_label(new_path, 0755);
|
||||
|
||||
if (symlink(old_path, new_path) >= 0) {
|
||||
unit_file_changes_add(changes, n_changes, UNIT_FILE_SYMLINK, new_path, old_path);
|
||||
|
||||
+1
-2
@@ -1,6 +1,7 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
#pragma once
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <stdbool.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
@@ -14,8 +15,6 @@ static inline int label_fix(const char *path, LabelFixFlags flags) {
|
||||
return label_fix_container(path, path, flags);
|
||||
}
|
||||
|
||||
int mkdir_label(const char *path, mode_t mode);
|
||||
int mkdirat_label(int dirfd, const char *path, mode_t mode);
|
||||
int symlink_label(const char *old_path, const char *new_path);
|
||||
int symlink_atomic_label(const char *from, const char *to);
|
||||
int mknod_label(const char *pathname, mode_t mode, dev_t dev);
|
||||
|
||||
@@ -209,6 +209,7 @@ shared_sources = files('''
|
||||
macvlan-util.h
|
||||
main-func.h
|
||||
mkdir-label.c
|
||||
mkdir-label.h
|
||||
mkfs-util.c
|
||||
mkfs-util.h
|
||||
module-util.h
|
||||
|
||||
@@ -1,34 +1,10 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "label.h"
|
||||
#include "macro.h"
|
||||
#include "mkdir.h"
|
||||
#include "mkdir-label.h"
|
||||
#include "selinux-util.h"
|
||||
#include "smack-util.h"
|
||||
#include "user-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, 0);
|
||||
}
|
||||
|
||||
int mkdirat_label(int dirfd, const char *path, mode_t mode) {
|
||||
int r;
|
||||
|
||||
@@ -47,13 +23,13 @@ int mkdirat_label(int dirfd, const char *path, mode_t mode) {
|
||||
}
|
||||
|
||||
int mkdir_safe_label(const char *path, mode_t mode, uid_t uid, gid_t gid, MkdirFlags flags) {
|
||||
return mkdir_safe_internal(path, mode, uid, gid, flags, mkdir_label);
|
||||
return mkdir_safe_internal(path, mode, uid, gid, flags, mkdirat_label);
|
||||
}
|
||||
|
||||
int mkdir_parents_label(const char *path, mode_t mode) {
|
||||
return mkdir_parents_internal(NULL, path, mode, UID_INVALID, UID_INVALID, 0, mkdir_label);
|
||||
return mkdir_parents_internal(NULL, path, mode, UID_INVALID, UID_INVALID, 0, mkdirat_label);
|
||||
}
|
||||
|
||||
int mkdir_p_label(const char *path, mode_t mode) {
|
||||
return mkdir_p_internal(NULL, path, mode, UID_INVALID, UID_INVALID, 0, mkdir_label);
|
||||
return mkdir_p_internal(NULL, path, mode, UID_INVALID, UID_INVALID, 0, mkdirat_label);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
#pragma once
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include "mkdir.h"
|
||||
|
||||
int mkdirat_label(int dirfd, const char *path, mode_t mode);
|
||||
|
||||
static inline int mkdir_label(const char *path, mode_t mode) {
|
||||
return mkdirat_label(AT_FDCWD, path, mode);
|
||||
}
|
||||
|
||||
int mkdir_safe_label(const char *path, mode_t mode, uid_t uid, gid_t gid, MkdirFlags flags);
|
||||
int mkdir_parents_label(const char *path, mode_t mod);
|
||||
int mkdir_p_label(const char *path, mode_t mode);
|
||||
@@ -20,7 +20,7 @@
|
||||
#include "label.h"
|
||||
#include "log.h"
|
||||
#include "macro.h"
|
||||
#include "mkdir.h"
|
||||
#include "mkdir-label.h"
|
||||
#include "mount-setup.h"
|
||||
#include "mount-util.h"
|
||||
#include "mountpoint-util.h"
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
#include "libmount-util.h"
|
||||
#include "missing_mount.h"
|
||||
#include "missing_syscall.h"
|
||||
#include "mkdir.h"
|
||||
#include "mkdir-label.h"
|
||||
#include "mount-util.h"
|
||||
#include "mountpoint-util.h"
|
||||
#include "namespace-util.h"
|
||||
|
||||
+11
-27
@@ -497,25 +497,30 @@ static int selinux_create_file_prepare_abspath(const char *abspath, mode_t mode)
|
||||
}
|
||||
#endif
|
||||
|
||||
int mac_selinux_create_file_prepare_at(int dirfd, const char *path, mode_t mode) {
|
||||
int mac_selinux_create_file_prepare_at(
|
||||
int dir_fd,
|
||||
const char *path,
|
||||
mode_t mode) {
|
||||
|
||||
#if HAVE_SELINUX
|
||||
_cleanup_free_ char *abspath = NULL;
|
||||
int r;
|
||||
|
||||
assert(path);
|
||||
if (dir_fd < 0 && dir_fd != AT_FDCWD)
|
||||
return -EBADF;
|
||||
|
||||
if (!label_hnd)
|
||||
return 0;
|
||||
|
||||
if (!path_is_absolute(path)) {
|
||||
if (dirfd == AT_FDCWD)
|
||||
if (isempty(path) || !path_is_absolute(path)) {
|
||||
if (dir_fd == AT_FDCWD)
|
||||
r = safe_getcwd(&abspath);
|
||||
else
|
||||
r = fd_get_path(dirfd, &abspath);
|
||||
r = fd_get_path(dir_fd, &abspath);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
if (!path_extend(&abspath, path))
|
||||
if (!isempty(path) && !path_extend(&abspath, path))
|
||||
return -ENOMEM;
|
||||
|
||||
path = abspath;
|
||||
@@ -527,27 +532,6 @@ int mac_selinux_create_file_prepare_at(int dirfd, const char *path, mode_t mode)
|
||||
#endif
|
||||
}
|
||||
|
||||
int mac_selinux_create_file_prepare(const char *path, mode_t mode) {
|
||||
#if HAVE_SELINUX
|
||||
int r;
|
||||
|
||||
_cleanup_free_ char *abspath = NULL;
|
||||
|
||||
assert(path);
|
||||
|
||||
if (!label_hnd)
|
||||
return 0;
|
||||
|
||||
r = path_make_absolute_cwd(path, &abspath);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
return selinux_create_file_prepare_abspath(abspath, mode);
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
int mac_selinux_create_file_prepare_label(const char *path, const char *label) {
|
||||
#if HAVE_SELINUX
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
#pragma once
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <stdbool.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
@@ -41,8 +42,10 @@ int mac_selinux_get_our_label(char **label);
|
||||
int mac_selinux_get_child_mls_label(int socket_fd, const char *exe, const char *exec_label, char **label);
|
||||
char* mac_selinux_free(char *label);
|
||||
|
||||
int mac_selinux_create_file_prepare(const char *path, mode_t mode);
|
||||
int mac_selinux_create_file_prepare_at(int dirfd, const char *path, mode_t mode);
|
||||
static inline int mac_selinux_create_file_prepare(const char *path, mode_t mode) {
|
||||
return mac_selinux_create_file_prepare_at(AT_FDCWD, path, mode);
|
||||
}
|
||||
int mac_selinux_create_file_prepare_label(const char *path, const char *label);
|
||||
void mac_selinux_create_file_clear(void);
|
||||
|
||||
|
||||
@@ -176,7 +176,7 @@ static int smack_fix_fd(int fd, const char *abspath, LabelFixFlags flags) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int mac_smack_fix_at(int dirfd, const char *path, LabelFixFlags flags) {
|
||||
int mac_smack_fix_at(int dir_fd, const char *path, LabelFixFlags flags) {
|
||||
_cleanup_free_ char *p = NULL;
|
||||
_cleanup_close_ int fd = -1;
|
||||
int r;
|
||||
@@ -186,7 +186,14 @@ int mac_smack_fix_at(int dirfd, const char *path, LabelFixFlags flags) {
|
||||
if (!mac_smack_use())
|
||||
return 0;
|
||||
|
||||
fd = openat(dirfd, path, O_NOFOLLOW|O_CLOEXEC|O_PATH);
|
||||
if (dir_fd < 0) {
|
||||
if (dir_fd != AT_FDCWD)
|
||||
return -EBADF;
|
||||
|
||||
return mac_smack_fix(path, flags);
|
||||
}
|
||||
|
||||
fd = openat(dir_fd, path, O_NOFOLLOW|O_CLOEXEC|O_PATH);
|
||||
if (fd < 0) {
|
||||
if ((flags & LABEL_IGNORE_ENOENT) && errno == ENOENT)
|
||||
return 0;
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
#include "log.h"
|
||||
#include "macro.h"
|
||||
#include "missing_socket.h"
|
||||
#include "mkdir.h"
|
||||
#include "mkdir-label.h"
|
||||
#include "selinux-util.h"
|
||||
#include "socket-util.h"
|
||||
#include "umask-util.h"
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
#include "fd-util.h"
|
||||
#include "log.h"
|
||||
#include "missing_syscall.h"
|
||||
#include "mkdir.h"
|
||||
#include "mkdir-label.h"
|
||||
#include "mount-util.h"
|
||||
#include "mountpoint-util.h"
|
||||
#include "path-util.h"
|
||||
|
||||
Reference in New Issue
Block a user