use unshare(CLONE_NEWNS) instead of a complicated 'clone(NEWNS) ... waitpid()' operation
[util-vserver.git] / src / vspace.c
index 940255f..4819138 100644 (file)
@@ -22,7 +22,7 @@
 #endif
 
 #include "util.h"
-#include <lib_internal/sys_clone.h>
+#include <lib_internal/sys_unshare.h>
 
 #include <vserver.h>
 
@@ -106,30 +106,14 @@ showVersion()
 }
 
 static void
-newSpaces(uint_least64_t mask, const char *cmd)
+newSpaces(uint_least64_t mask)
 {
-  pid_t pid;
+  int rc;
 
-  /* 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);
+  rc = sys_unshare(mask);
+  if (rc) {
+         perror(ENSC_WRAPPERS_PREFIX "unshare()");
+         exit(wrapper_exit_code);
   }
 }
 
@@ -173,14 +157,17 @@ int main(int argc, char *argv[])
        do_enter = true;
        xid      = Evc_xidopt2xid(optarg,true);
        break;
-      case 'm'         :
-       if (!isNumberUnsigned(optarg, &mask, true)) {
+      case 'm'         :  {
+       unsigned long   mask_l;
+       if (!isNumberUnsigned(optarg, &mask_l, true)) {
          WRITE_MSG(2, "Invalid mask '");
          WRITE_STR(2, optarg);
          WRITE_MSG(2, "'; try '--help' for more information\n");
          return wrapper_exit_code;
        }
+       mask = mask_l;
        break;
+      }
       case 'M'         :  mask |= CLONE_NEWNS;         break;
       case 'F'         :  mask |= CLONE_FS;            break;
       case 'I'         :  mask |= CLONE_NEWIPC;        break;
@@ -205,12 +192,10 @@ int main(int argc, char *argv[])
     WRITE_MSG(2, "No operation was specified; try '--help' for more information\n");
   else if (sum>1)
     WRITE_MSG(2, "Can not specify multiple operations; try '--help' for more information\n");
-  else if (mask==0)
-    WRITE_MSG(2, "Must specify at least one space; try '--help' for more information\n");
   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, argv[optind]);
+    if      (do_new)     newSpaces(mask);
     else if (do_set)     setSpaces(VC_SAMECTX, mask);
     else if (do_enter)   enterSpaces(xid, mask);