]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
mount: allow PID as --namespace argument
authorVaclav Dolezal <vdolezal@redhat.com>
Fri, 20 Apr 2018 15:57:39 +0000 (17:57 +0200)
committerKarel Zak <kzak@redhat.com>
Mon, 11 Jun 2018 14:16:32 +0000 (16:16 +0200)
[[kzak@redhat.com: - update code]

Signed-off-by: Karel Zak <kzak@redhat.com>
Signed-off-by: Vaclav Dolezal <vdolezal@redhat.com>
sys-utils/mount.8
sys-utils/mount.c

index 4884037175fe79bc7048d366c9ceb2b22ea42f01..1a05f71c09f23d152ed33a8ae764d672ff0c6cd0 100644 (file)
@@ -646,7 +646,9 @@ is on a read-only filesystem.
 .TP
 .BR \-N , " \-\-namespace " \fIns
 Perform mount in namespace specified by \fIns\fR.
-\fIns\fR is typically in form \fI/proc/[pid]/ns/mnt\fR.
+\fIns\fR is either PID of process running in that namespace
+or special file representing that namespace.
+See \fBnamespaces\fR(7) for more information.
 .TP
 .BR \-O , " \-\-test\-opts " \fIopts
 Limit the set of filesystems to which the
index a0dba721a8cd31277d2e432eaf3b847e992160fc..5e139e896948e02da6dcc05996953591e1e30ddc 100644 (file)
@@ -512,6 +512,19 @@ static long osrc2mask(const char *str, size_t len)
        return -EINVAL;
 }
 
+static pid_t parse_pid(const char *str)
+{
+       char *end;
+       pid_t ret;
+
+       errno = 0;
+       ret = strtoul(str, &end, 10);
+
+       if (ret < 0 || errno || end == str || (end && *end))
+               return 0;
+       return ret;
+}
+
 int main(int argc, char **argv)
 {
        int c, rc = MNT_EX_SUCCESS, all = 0, show_labels = 0;
@@ -694,9 +707,17 @@ int main(int argc, char **argv)
                        append_option(cxt, "rbind");
                        break;
                case 'N':
-                       if (mnt_context_set_target_ns(cxt, optarg))
-                               err(MNT_EX_SYSERR, _("failed to set target namespace"));
+               {
+                       char path[PATH_MAX];
+                       pid_t pid = parse_pid(optarg);
+
+                       if (pid)
+                               snprintf(path, sizeof(path), "/proc/%i/ns/mnt", pid);
+
+                       if (mnt_context_set_target_ns(cxt, pid ? path : optarg))
+                               err(MNT_EX_SYSERR, _("failed to set target namespace to %s"), pid ? path : optarg);
                        break;
+               }
                case MOUNT_OPT_SHARED:
                        append_option(cxt, "shared");
                        propa = 1;