]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
mount_utils: add mount_fd()
authorChristian Brauner <christian.brauner@ubuntu.com>
Fri, 30 Jul 2021 07:15:14 +0000 (09:15 +0200)
committerChristian Brauner <christian.brauner@ubuntu.com>
Fri, 30 Jul 2021 07:19:32 +0000 (09:19 +0200)
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/lxc/mount_utils.c
src/lxc/mount_utils.h

index 1cf71dadd61c3a4211b0304c78faeaaa8def3097..77498bbf30fc5bd41aca60a4640cbd822e46bc03 100644 (file)
@@ -635,3 +635,37 @@ int mount_beneath_fd(int fd, const char *source, const char *target,
        TRACE("Mounted \"%s\" to \"%s\"", source, buf_target);
        return 0;
 }
+
+int mount_fd(int fd_source, int fd_target, const char *fs_name,
+            unsigned int flags, const void *data)
+{
+       int ret;
+       char buf_source[LXC_PROC_PID_FD_LEN], buf_target[LXC_PROC_PID_FD_LEN];
+       char *source = buf_source, *target = buf_target;
+
+       if (fd_source < 0) {
+               source = NULL;
+       } else {
+               ret = strnprintf(buf_source, sizeof(buf_source),
+                                "/proc/self/fd/%d", fd_source);
+               if (ret < 0)
+                       return ret;
+       }
+
+       if (fd_target < 0) {
+               target = NULL;
+       } else {
+               ret = strnprintf(buf_target, sizeof(buf_target),
+                                "/proc/self/fd/%d", fd_target);
+               if (ret < 0)
+                       return ret;
+       }
+
+       ret = mount(source, target, "none", MS_BIND, 0);
+       if (ret < 0)
+               return syserror("Failed to mount \"%s\" to \"%s\"",
+                               maybe_empty(source), maybe_empty(target));
+
+       TRACE("Mounted \"%s\" to \"%s\"", maybe_empty(source), maybe_empty(target));
+       return 0;
+}
index 17ff4698f9453425671af00dfb079b4dd7d60a2b..4caf34b2fbaf041737fff9e261675fc55856a026 100644 (file)
@@ -224,5 +224,7 @@ __hidden extern bool can_use_bind_mounts(void);
 __hidden extern int mount_beneath_fd(int fd, const char *source,
                                     const char *target, const char *fs_name,
                                     unsigned int flags, const void *data);
+__hidden extern int mount_fd(int fd_source, int fd_target, const char *fs_name,
+                            unsigned int flags, const void *data);
 
 #endif /* __LXC_MOUNT_UTILS_H */