Don't require a netmask/prefix.
[util-vserver.git] / src / vspace.c
index 30f0dda..940255f 100644 (file)
@@ -22,7 +22,7 @@
 #endif
 
 #include "util.h"
-#include <lib_internal/sys_unshare.h>
+#include <lib_internal/sys_clone.h>
 
 #include <vserver.h>
 
@@ -56,6 +56,8 @@ CMDLINE_OPTIONS[] = {
   { "ipc",        no_argument,       0, 'I' },
   { "uts",        no_argument,       0, 'U' },
   { "user",       no_argument,       0, 'S' },
+  { "pid",        no_argument,       0, 'P' },
+  { "net",        no_argument,       0, 'N' },
   {0,0,0,0}
 };
 
@@ -83,6 +85,8 @@ showHelp(int fd, char const *cmd, int res)
            "    --ipc             ...  the IPC namespace\n"
            "    --uts             ...  the uts namespace\n"
            "    --user            ...  the user namespace\n"
+           "    --pid             ...  the pid namespace\n"
+           "    --net             ...  the network namespace\n"
            "\n"
            "Please report bugs to " PACKAGE_BUGREPORT "\n");
 
@@ -102,11 +106,30 @@ showVersion()
 }
 
 static void
-newSpaces(uint_least64_t mask)
+newSpaces(uint_least64_t mask, const char *cmd)
 {
-  if (sys_unshare((int) mask) == -1) {
-    perror(ENSC_WRAPPERS_PREFIX "unshare()");
-    exit(wrapper_exit_code);
+  pid_t pid;
+
+  /* optimize default case */
+  if (mask == 0)
+    return;
+
+  signal(SIGCHLD, SIG_DFL);
+
+#ifdef NDEBUG
+  pid = sys_clone((int) mask | CLONE_VFORK|SIGCHLD, 0);
+#else
+  pid = sys_clone((int) mask | SIGCHLD, 0);
+#endif
+
+  switch (pid) {
+    case -1    :
+      perror(ENSC_WRAPPERS_PREFIX "clone()");
+      exit(wrapper_exit_code);
+    case 0     :
+      break;
+    default    :
+      exitLikeProcess(pid, cmd, wrapper_exit_code);
   }
 }
 
@@ -138,7 +161,7 @@ int main(int argc, char *argv[])
   int                  sum        = 0;
   
   while (1) {
-    int                c = getopt_long(argc, argv, "+nsce:m:" "MFIUS", CMDLINE_OPTIONS, 0);
+    int                c = getopt_long(argc, argv, "+nsce:m:" "MFIUSPN", CMDLINE_OPTIONS, 0);
     if (c==-1) break;
 
     switch (c) {
@@ -163,6 +186,8 @@ int main(int argc, char *argv[])
       case 'I'         :  mask |= CLONE_NEWIPC;        break;
       case 'U'         :  mask |= CLONE_NEWUTS;        break;
       case 'S'         :  mask |= CLONE_NEWUSER;       break;
+      case 'P'         :  mask |= CLONE_NEWPID;        break;
+      case 'N'         :  mask |= CLONE_NEWNET;        break;
 
       default          :
        WRITE_MSG(2, "Try '");
@@ -185,7 +210,7 @@ int main(int argc, char *argv[])
   else if (optind==argc && (do_new || do_enter))
     WRITE_MSG(2, "No command specified; try '--help' for more information\n");
   else {
-    if      (do_new)     newSpaces(mask);
+    if      (do_new)     newSpaces(mask, argv[optind]);
     else if (do_set)     setSpaces(VC_SAMECTX, mask);
     else if (do_enter)   enterSpaces(xid, mask);