From: Christian Brauner Date: Thu, 4 Feb 2021 14:23:55 +0000 (+0100) Subject: utils: add mount_from_at() X-Git-Tag: lxc-5.0.0~301^2~8 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=977687db1c288e3315d9aea56fc9ae10bdd1badf;p=thirdparty%2Flxc.git utils: add mount_from_at() Signed-off-by: Christian Brauner --- diff --git a/src/lxc/utils.c b/src/lxc/utils.c index e664e6276..60d35ed92 100644 --- a/src/lxc/utils.c +++ b/src/lxc/utils.c @@ -1263,6 +1263,53 @@ int mount_at(int dfd, return ret; } +int mount_from_at(int dfd_from, const char *path_from, + __u64 o_flags_from, + __u64 resolve_flags_from, + int dfd_to, const char *path_to, + __u64 o_flags_to, + __u64 resolve_flags_to, + const char *fstype, unsigned int mnt_flags, const void *data) +{ + __do_close int fd_from = -EBADF, fd_to = -EBADF; + struct lxc_open_how how = {}; + int ret; + char src_buf[LXC_PROC_PID_FD_LEN], dst_buf[LXC_PROC_PID_FD_LEN]; + + if (is_empty_string(path_from)) { + ret = snprintf(src_buf, sizeof(src_buf), "/proc/self/fd/%d", dfd_from); + } else { + how.flags = o_flags_from; + how.resolve = resolve_flags_from; + fd_from = openat2(dfd_from, path_from, &how, sizeof(how)); + if (fd_from < 0) + return -errno; + + ret = snprintf(src_buf, sizeof(src_buf), "/proc/self/fd/%d", fd_from); + } + if (ret < 0 || ret >= sizeof(src_buf)) + return -EIO; + + if (is_empty_string(path_to)) { + ret = snprintf(dst_buf, sizeof(dst_buf), "/proc/self/fd/%d", dfd_to); + } else { + how.flags = o_flags_to; + how.resolve = resolve_flags_to; + fd_to = openat2(dfd_to, path_to, &how, sizeof(how)); + if (fd_to < 0) + return -errno; + + ret = snprintf(dst_buf, sizeof(dst_buf), "/proc/self/fd/%d", fd_to); + } + + if (is_empty_string(src_buf)) + ret = mount(NULL, dst_buf, fstype, mnt_flags, data); + else + ret = mount(src_buf, dst_buf, fstype, mnt_flags, data); + + return ret; +} + int open_devnull(void) { int fd = open("/dev/null", O_RDWR); diff --git a/src/lxc/utils.h b/src/lxc/utils.h index dc22394fc..ffc235a1b 100644 --- a/src/lxc/utils.h +++ b/src/lxc/utils.h @@ -247,5 +247,11 @@ __hidden extern int mount_at(int dfd, const char *src_under_dfd, const char *dst_under_dfd, __u64 o_flags, __u64 resolve_flags, const char *fstype, unsigned int mnt_flags, const void *data); +__hidden extern int mount_from_at(int dfd_from, const char *path_from, + __u64 o_flags_from, __u64 resolve_flags_from, + int dfd_to, const char *path_to, + __u64 o_flags_to, __u64 resolve_flags_to, + const char *fstype, unsigned int mnt_flags, + const void *data); #endif /* __LXC_UTILS_H */