]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
basic: add make_mount_point_inode helper
authorLuca Boccassi <bluca@debian.org>
Sat, 19 Dec 2020 21:40:47 +0000 (21:40 +0000)
committerLennart Poettering <lennart@poettering.net>
Mon, 21 Dec 2020 20:48:13 +0000 (21:48 +0100)
Creates a file or a directory depending on the source path, useful
for creating mount points.

src/basic/mountpoint-util.c
src/basic/mountpoint-util.h
src/core/namespace.c
src/machine/machine-dbus.c
src/test/test-mountpoint-util.c

index a6602ad53945f0a7ec517cac2d9be221389ddb3d..ed7457f8b76aca89634b203c223696a070c88739 100644 (file)
@@ -8,14 +8,17 @@
 #include "fd-util.h"
 #include "fileio.h"
 #include "fs-util.h"
+#include "label.h"
 #include "missing_stat.h"
 #include "missing_syscall.h"
+#include "mkdir.h"
 #include "mountpoint-util.h"
 #include "parse-util.h"
 #include "path-util.h"
 #include "stat-util.h"
 #include "stdio-util.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
  * any more currently defined value to future-proof things: if the size is increased in the API headers, and our code
@@ -509,3 +512,25 @@ int mount_propagation_flags_from_string(const char *name, unsigned long *ret) {
                 return -EINVAL;
         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);
+}
index aadb2123d91680003cf9dd687c1628c1ddc16083..cebcec5e78852ff15fc1cfd6cae90a70baa8b4db 100644 (file)
@@ -23,3 +23,7 @@ int dev_is_devtmpfs(void);
 
 const char *mount_propagation_flags_to_string(unsigned long flags);
 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);
index 767439b7f609b1f57838e2ed2a2baa174b5e0e93..e32336a7ff0c44ab9cf0256d6c62ebc0ca5ba3ea 100644 (file)
@@ -1184,29 +1184,19 @@ static int apply_mount(
                 bool try_again = false;
 
                 if (r == -ENOENT && make) {
-                        struct stat st;
+                        int q;
 
                         /* Hmm, either the source or the destination are missing. Let's see if we can create
                            the destination, then try again. */
 
-                        if (stat(what, &st) < 0)
-                                log_error_errno(errno, "Mount point source '%s' is not accessible: %m", what);
-                        else {
-                                int q;
+                        (void) mkdir_parents(mount_entry_path(m), 0755);
 
-                                (void) mkdir_parents(mount_entry_path(m), 0755);
-
-                                if (S_ISDIR(st.st_mode))
-                                        q = mkdir(mount_entry_path(m), 0755) < 0 ? -errno : 0;
-                                else
-                                        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;
-                        }
+                        q = make_mount_point_inode_from_path(what, mount_entry_path(m), 0755);
+                        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)
index 46465716685acef675b0521ee11853146ebc92cd..5d8d4276e238cbf22000eb2f23c49700f20b447b 100644 (file)
@@ -32,6 +32,7 @@
 #include "missing_capability.h"
 #include "mkdir.h"
 #include "mount-util.h"
+#include "mountpoint-util.h"
 #include "namespace-util.h"
 #include "os-util.h"
 #include "path-util.h"
@@ -908,10 +909,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_errno_wrapper(mount_tmp, 0700);
-        else
-                r = touch(mount_tmp);
+        r = make_mount_point_inode_from_stat(&st, mount_tmp, 0700);
         if (r < 0) {
                 sd_bus_error_set_errnof(error, r, "Failed to create temporary mount point %s: %m", mount_tmp);
                 goto finish;
@@ -1003,12 +1001,8 @@ int bus_machine_method_bind_mount(sd_bus_message *message, void *userdata, sd_bu
                 }
 
                 if (make_file_or_directory) {
-                        if (S_ISDIR(st.st_mode))
-                                (void) mkdir_p(dest, 0755);
-                        else {
-                                (void) mkdir_parents(dest, 0755);
-                                (void) mknod(dest, S_IFREG|0600, 0);
-                        }
+                        (void) mkdir_parents(dest, 0755);
+                        (void) make_mount_point_inode_from_stat(&st, dest, 0700);
                 }
 
                 mount_inside = strjoina("/run/host/incoming/", basename(mount_outside));
index 125e5ac9fa5bd058fcf4ab55cc4bcac905ae1b2a..128daa6de89e7b6b0e4a67bdd480a11481f486c3 100644 (file)
@@ -8,13 +8,16 @@
 #include "def.h"
 #include "fd-util.h"
 #include "fileio.h"
+#include "fs-util.h"
 #include "hashmap.h"
 #include "log.h"
+#include "mkdir.h"
 #include "mountpoint-util.h"
 #include "path-util.h"
 #include "rm-rf.h"
 #include "string-util.h"
 #include "tests.h"
+#include "tmpfile-util.h"
 
 static void test_mount_propagation_flags(const char *name, int ret, unsigned long expected) {
         long unsigned flags;
@@ -287,6 +290,52 @@ static void test_fd_is_mount_point(void) {
         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[]) {
         test_setup_logging(LOG_DEBUG);
 
@@ -311,6 +360,7 @@ int main(int argc, char *argv[]) {
         test_mnt_id();
         test_path_is_mount_point();
         test_fd_is_mount_point();
+        test_make_mount_point_inode();
 
         return 0;
 }