summaryrefslogtreecommitdiffstats
path: root/sys-utils/nsenter.c
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek2013-01-21 07:38:01 +0100
committerKarel Zak2013-01-25 15:13:39 +0100
commit984e1b7ce90a924ae342169ab8d850d0095f14ab (patch)
treed2462de89ba0f52b33ed96f429ed0dadfaaffd8c /sys-utils/nsenter.c
parenttextual: use UTIL_LINUX_VERSION everywhere (diff)
downloadkernel-qcow2-util-linux-984e1b7ce90a924ae342169ab8d850d0095f14ab.tar.gz
kernel-qcow2-util-linux-984e1b7ce90a924ae342169ab8d850d0095f14ab.tar.xz
kernel-qcow2-util-linux-984e1b7ce90a924ae342169ab8d850d0095f14ab.zip
nsenter: allow arguments to be specified in any order
Allows 'nsenter -mt $PID', which would fail previously. [kzak@redhat.com: - fix open_target_fd() arguments] Signed-off-by: Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'sys-utils/nsenter.c')
-rw-r--r--sys-utils/nsenter.c55
1 files changed, 46 insertions, 9 deletions
diff --git a/sys-utils/nsenter.c b/sys-utils/nsenter.c
index ce2c64d6b..f377f2b84 100644
--- a/sys-utils/nsenter.c
+++ b/sys-utils/nsenter.c
@@ -25,6 +25,7 @@
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
+#include <stdbool.h>
#include <unistd.h>
#include "strutils.h"
@@ -173,7 +174,8 @@ int main(int argc, char *argv[])
struct namespace_file *nsfile;
int do_fork = 0;
- int c;
+ int c, namespaces = 0;
+ bool do_rd = false, do_wd = false;
setlocale(LC_MESSAGES, "");
bindtextdomain(PACKAGE, LOCALEDIR);
@@ -191,32 +193,56 @@ int main(int argc, char *argv[])
namespace_target_pid = strtoul_or_err(optarg, _("failed to parse pid"));
break;
case 'm':
- open_namespace_fd(CLONE_NEWNS, optarg);
+ if (optarg)
+ open_namespace_fd(CLONE_NEWNS, optarg);
+ else
+ namespaces |= CLONE_NEWNS;
break;
case 'u':
- open_namespace_fd(CLONE_NEWUTS, optarg);
+ if (optarg)
+ open_namespace_fd(CLONE_NEWUTS, optarg);
+ else
+ namespaces |= CLONE_NEWUTS;
break;
case 'i':
- open_namespace_fd(CLONE_NEWIPC, optarg);
+ if (optarg)
+ open_namespace_fd(CLONE_NEWIPC, optarg);
+ else
+ namespaces |= CLONE_NEWIPC;
break;
case 'n':
- open_namespace_fd(CLONE_NEWNET, optarg);
+ if (optarg)
+ open_namespace_fd(CLONE_NEWNET, optarg);
+ else
+ namespaces |= CLONE_NEWNET;
break;
case 'p':
do_fork = 1;
- open_namespace_fd(CLONE_NEWPID, optarg);
+ if (optarg)
+ open_namespace_fd(CLONE_NEWPID, optarg);
+ else
+ namespaces |= CLONE_NEWPID;
break;
case 'U':
- open_namespace_fd(CLONE_NEWUSER, optarg);
+ if (optarg)
+ open_namespace_fd(CLONE_NEWUSER, optarg);
+ else
+ namespaces |= CLONE_NEWUSER;
break;
case 'e':
do_fork = 0;
break;
case 'r':
- open_target_fd(&root_fd, "root", optarg);
+ if (optarg)
+ open_target_fd(&root_fd, "root", optarg);
+ else
+ do_rd = true;
break;
case 'w':
- open_target_fd(&wd_fd, "cwd", optarg);
+ if (optarg)
+ open_target_fd(&wd_fd, "cwd", optarg);
+ else
+ do_wd = true;
break;
default:
usage(EXIT_FAILURE);
@@ -227,6 +253,17 @@ int main(int argc, char *argv[])
usage(EXIT_FAILURE);
/*
+ * Open remaining namespace and directory descriptors.
+ */
+ for (nsfile = namespace_files; nsfile->nstype; nsfile++)
+ if (nsfile->nstype & namespaces)
+ open_namespace_fd(nsfile->nstype, NULL);
+ if (do_rd)
+ open_target_fd(&root_fd, "root", NULL);
+ if (do_wd)
+ open_target_fd(&wd_fd, "cwd", NULL);
+
+ /*
* Now that we know which namespaces we want to enter, enter them.
*/
for (nsfile = namespace_files; nsfile->nstype; nsfile++) {