mount: allow PID as --namespace argument

[[kzak@redhat.com: - update code]

Signed-off-by: Karel Zak <kzak@redhat.com>
Signed-off-by: Vaclav Dolezal <vdolezal@redhat.com>
This commit is contained in:
Vaclav Dolezal 2018-04-20 17:57:39 +02:00 committed by Karel Zak
parent d45e8ef999
commit d59766a648
2 changed files with 26 additions and 3 deletions

View 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

View 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;