namespace-util: introduce helper for combining unshare() + MS_SLAVE remount

We have multiple places we do these two non-trivial operations together,
let's introduce a unified helper for doing both at once.
This commit is contained in:
Lennart Poettering
2020-07-07 11:20:42 +02:00
parent cda667722c
commit e2ec9c4d3a
5 changed files with 27 additions and 16 deletions
+14
View File
@@ -2,6 +2,7 @@
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mount.h>
#include "fd-util.h"
#include "missing_fs.h"
@@ -169,3 +170,16 @@ int fd_is_network_ns(int fd) {
return r == CLONE_NEWNET;
}
int detach_mount_namespace(void) {
/* Detaches the mount namespace, disabling propagation from our namespace to the host */
if (unshare(CLONE_NEWNS) < 0)
return -errno;
if (mount(NULL, "/", NULL, MS_SLAVE | MS_REC, NULL) < 0)
return -errno;
return 0;
}
+2
View File
@@ -7,3 +7,5 @@ int namespace_open(pid_t pid, int *pidns_fd, int *mntns_fd, int *netns_fd, int *
int namespace_enter(int pidns_fd, int mntns_fd, int netns_fd, int userns_fd, int root_fd);
int fd_is_network_ns(int fd);
int detach_mount_namespace(void);