From 984e1b7ce90a924ae342169ab8d850d0095f14ab Mon Sep 17 00:00:00 2001 From: Zbigniew Jędrzejewski-Szmek Date: Mon, 21 Jan 2013 01:38:01 -0500 Subject: 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 Signed-off-by: Karel Zak --- sys-utils/nsenter.c | 55 ++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 46 insertions(+), 9 deletions(-) (limited to 'sys-utils/nsenter.c') 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 #include #include +#include #include #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); @@ -226,6 +252,17 @@ int main(int argc, char *argv[]) if(optind >= argc) 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. */ -- cgit v1.2.3-55-g7522