1
0
mirror of https://github.com/systemd/systemd synced 2026-03-07 13:44:46 +01:00

Compare commits

..

No commits in common. "8bab8029105e44ce78c5e11bffa203a1135fe201" and "8786d4bbe43b5f6493982bcb5211e010f99deb57" have entirely different histories.

6 changed files with 29 additions and 93 deletions

View File

@ -8,17 +8,14 @@
#include "fd-util.h" #include "fd-util.h"
#include "fileio.h" #include "fileio.h"
#include "fs-util.h" #include "fs-util.h"
#include "label.h"
#include "missing_stat.h" #include "missing_stat.h"
#include "missing_syscall.h" #include "missing_syscall.h"
#include "mkdir.h"
#include "mountpoint-util.h" #include "mountpoint-util.h"
#include "parse-util.h" #include "parse-util.h"
#include "path-util.h" #include "path-util.h"
#include "stat-util.h" #include "stat-util.h"
#include "stdio-util.h" #include "stdio-util.h"
#include "strv.h" #include "strv.h"
#include "user-util.h"
/* This is the original MAX_HANDLE_SZ definition from the kernel, when the API was introduced. We use that in place of /* This is the original MAX_HANDLE_SZ definition from the kernel, when the API was introduced. We use that in place of
* any more currently defined value to future-proof things: if the size is increased in the API headers, and our code * any more currently defined value to future-proof things: if the size is increased in the API headers, and our code
@ -512,25 +509,3 @@ int mount_propagation_flags_from_string(const char *name, unsigned long *ret) {
return -EINVAL; return -EINVAL;
return 0; return 0;
} }
int make_mount_point_inode_from_stat(const struct stat *st, const char *dest, mode_t mode) {
assert(st);
assert(dest);
if (S_ISDIR(st->st_mode))
return mkdir_label(dest, mode);
else
return mknod(dest, S_IFREG|(mode & ~0111), 0);
}
int make_mount_point_inode_from_path(const char *source, const char *dest, mode_t mode) {
struct stat st;
assert(source);
assert(dest);
if (stat(source, &st) < 0)
return -errno;
return make_mount_point_inode_from_stat(&st, dest, mode);
}

View File

@ -23,7 +23,3 @@ int dev_is_devtmpfs(void);
const char *mount_propagation_flags_to_string(unsigned long flags); const char *mount_propagation_flags_to_string(unsigned long flags);
int mount_propagation_flags_from_string(const char *name, unsigned long *ret); int mount_propagation_flags_from_string(const char *name, unsigned long *ret);
/* Creates a mount point (not parents) based on the source path or stat - ie, a file or a directory */
int make_mount_point_inode_from_stat(const struct stat *st, const char *dest, mode_t mode);
int make_mount_point_inode_from_path(const char *source, const char *dest, mode_t mode);

View File

@ -1184,19 +1184,29 @@ static int apply_mount(
bool try_again = false; bool try_again = false;
if (r == -ENOENT && make) { if (r == -ENOENT && make) {
int q; struct stat st;
/* Hmm, either the source or the destination are missing. Let's see if we can create /* Hmm, either the source or the destination are missing. Let's see if we can create
the destination, then try again. */ the destination, then try again. */
(void) mkdir_parents(mount_entry_path(m), 0755); if (stat(what, &st) < 0)
log_error_errno(errno, "Mount point source '%s' is not accessible: %m", what);
else {
int q;
q = make_mount_point_inode_from_path(what, mount_entry_path(m), 0755); (void) mkdir_parents(mount_entry_path(m), 0755);
if (q < 0)
log_error_errno(q, "Failed to create destination mount point node '%s': %m", if (S_ISDIR(st.st_mode))
mount_entry_path(m)); q = mkdir(mount_entry_path(m), 0755) < 0 ? -errno : 0;
else else
try_again = true; q = touch(mount_entry_path(m));
if (q < 0)
log_error_errno(q, "Failed to create destination mount point node '%s': %m",
mount_entry_path(m));
else
try_again = true;
}
} }
if (try_again) if (try_again)

View File

@ -32,7 +32,6 @@
#include "missing_capability.h" #include "missing_capability.h"
#include "mkdir.h" #include "mkdir.h"
#include "mount-util.h" #include "mount-util.h"
#include "mountpoint-util.h"
#include "namespace-util.h" #include "namespace-util.h"
#include "os-util.h" #include "os-util.h"
#include "path-util.h" #include "path-util.h"
@ -909,7 +908,10 @@ 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. */ /* Second, we mount the source file or directory to a directory inside of our MS_SLAVE playground. */
mount_tmp = strjoina(mount_slave, "/mount"); mount_tmp = strjoina(mount_slave, "/mount");
r = make_mount_point_inode_from_stat(&st, mount_tmp, 0700); if (S_ISDIR(st.st_mode))
r = mkdir_errno_wrapper(mount_tmp, 0700);
else
r = touch(mount_tmp);
if (r < 0) { if (r < 0) {
sd_bus_error_set_errnof(error, r, "Failed to create temporary mount point %s: %m", mount_tmp); sd_bus_error_set_errnof(error, r, "Failed to create temporary mount point %s: %m", mount_tmp);
goto finish; goto finish;
@ -1001,8 +1003,12 @@ int bus_machine_method_bind_mount(sd_bus_message *message, void *userdata, sd_bu
} }
if (make_file_or_directory) { if (make_file_or_directory) {
(void) mkdir_parents(dest, 0755); if (S_ISDIR(st.st_mode))
(void) make_mount_point_inode_from_stat(&st, dest, 0700); (void) mkdir_p(dest, 0755);
else {
(void) mkdir_parents(dest, 0755);
(void) mknod(dest, S_IFREG|0600, 0);
}
} }
mount_inside = strjoina("/run/host/incoming/", basename(mount_outside)); mount_inside = strjoina("/run/host/incoming/", basename(mount_outside));

View File

@ -8,16 +8,13 @@
#include "def.h" #include "def.h"
#include "fd-util.h" #include "fd-util.h"
#include "fileio.h" #include "fileio.h"
#include "fs-util.h"
#include "hashmap.h" #include "hashmap.h"
#include "log.h" #include "log.h"
#include "mkdir.h"
#include "mountpoint-util.h" #include "mountpoint-util.h"
#include "path-util.h" #include "path-util.h"
#include "rm-rf.h" #include "rm-rf.h"
#include "string-util.h" #include "string-util.h"
#include "tests.h" #include "tests.h"
#include "tmpfile-util.h"
static void test_mount_propagation_flags(const char *name, int ret, unsigned long expected) { static void test_mount_propagation_flags(const char *name, int ret, unsigned long expected) {
long unsigned flags; long unsigned flags;
@ -290,52 +287,6 @@ static void test_fd_is_mount_point(void) {
assert_se(IN_SET(fd_is_mount_point(fd, "root/", 0), -ENOENT, 0)); assert_se(IN_SET(fd_is_mount_point(fd, "root/", 0), -ENOENT, 0));
} }
static void test_make_mount_point_inode(void) {
_cleanup_(rm_rf_physical_and_freep) char *d = NULL;
const char *src_file, *src_dir, *dst_file, *dst_dir;
struct stat st;
log_info("/* %s */", __func__);
assert_se(mkdtemp_malloc(NULL, &d) >= 0);
src_file = strjoina(d, "/src/file");
src_dir = strjoina(d, "/src/dir");
dst_file = strjoina(d, "/dst/file");
dst_dir = strjoina(d, "/dst/dir");
assert_se(mkdir_p(src_dir, 0755) >= 0);
assert_se(mkdir_parents(dst_file, 0755) >= 0);
assert_se(touch(src_file) >= 0);
assert_se(make_mount_point_inode_from_path(src_file, dst_file, 0755) >= 0);
assert_se(make_mount_point_inode_from_path(src_dir, dst_dir, 0755) >= 0);
assert_se(stat(dst_dir, &st) == 0);
assert_se(S_ISDIR(st.st_mode));
assert_se(stat(dst_file, &st) == 0);
assert_se(S_ISREG(st.st_mode));
assert_se(!(S_IXUSR & st.st_mode));
assert_se(!(S_IXGRP & st.st_mode));
assert_se(!(S_IXOTH & st.st_mode));
assert_se(unlink(dst_file) == 0);
assert_se(rmdir(dst_dir) == 0);
assert_se(stat(src_file, &st) == 0);
assert_se(make_mount_point_inode_from_stat(&st, dst_file, 0755) >= 0);
assert_se(stat(src_dir, &st) == 0);
assert_se(make_mount_point_inode_from_stat(&st, dst_dir, 0755) >= 0);
assert_se(stat(dst_dir, &st) == 0);
assert_se(S_ISDIR(st.st_mode));
assert_se(stat(dst_file, &st) == 0);
assert_se(S_ISREG(st.st_mode));
assert_se(!(S_IXUSR & st.st_mode));
assert_se(!(S_IXGRP & st.st_mode));
assert_se(!(S_IXOTH & st.st_mode));
}
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
test_setup_logging(LOG_DEBUG); test_setup_logging(LOG_DEBUG);
@ -360,7 +311,6 @@ int main(int argc, char *argv[]) {
test_mnt_id(); test_mnt_id();
test_path_is_mount_point(); test_path_is_mount_point();
test_fd_is_mount_point(); test_fd_is_mount_point();
test_make_mount_point_inode();
return 0; return 0;
} }

View File

@ -227,7 +227,7 @@ static size_t escape_path(const char *src, char *dest, size_t size) {
/* manage "stack of names" with possibly specified device priorities */ /* manage "stack of names" with possibly specified device priorities */
static int link_update(sd_device *dev, const char *slink, bool add) { static int link_update(sd_device *dev, const char *slink, bool add) {
_cleanup_free_ char *filename = NULL, *dirname = NULL; _cleanup_free_ char *target = NULL, *filename = NULL, *dirname = NULL;
char name_enc[PATH_MAX]; char name_enc[PATH_MAX];
const char *id_filename; const char *id_filename;
int i, r, retries; int i, r, retries;
@ -270,7 +270,6 @@ static int link_update(sd_device *dev, const char *slink, bool add) {
retries = sd_device_get_is_initialized(dev) > 0 ? LINK_UPDATE_MAX_RETRIES : 1; retries = sd_device_get_is_initialized(dev) > 0 ? LINK_UPDATE_MAX_RETRIES : 1;
for (i = 0; i < retries; i++) { for (i = 0; i < retries; i++) {
_cleanup_free_ char *target = NULL;
struct stat st1 = {}, st2 = {}; struct stat st1 = {}, st2 = {};
r = stat(dirname, &st1); r = stat(dirname, &st1);