use unshare(CLONE_NEWNS) instead of a complicated 'clone(NEWNS) ... waitpid()' operation
[util-vserver.git] / src / vnamespace.c
index 366b967..076b4e4 100644 (file)
@@ -21,7 +21,7 @@
 #endif
 
 #include "util.h"
-#include <lib_internal/sys_clone.h>
+#include <lib_internal/sys_unshare.h>
 
 #include <vserver.h>
 
 #define ENSC_WRAPPERS_VSERVER  1
 #include <wrappers.h>
 
-#ifndef CLONE_NEWUTS
-#  define CLONE_NEWUTS         0x04000000
-#endif
-#ifndef CLONE_NEWIPC
-#  define CLONE_NEWIPC         0x08000000
-#endif
-
 #define CMD_HELP               0x1000
 #define CMD_VERSION            0x1001
 
@@ -95,42 +88,30 @@ showVersion()
 }
 
 static void
-newNamespace(char const *cmd)
+newNamespace(void)
 {
-  pid_t                pid;
-
-  signal(SIGCHLD, SIG_DFL);
-  
-#ifdef NDEBUG    
-  pid = sys_clone(CLONE_NEWNS|CLONE_NEWUTS|CLONE_NEWIPC|CLONE_VFORK|SIGCHLD, 0);
-#else
-  pid = sys_clone(CLONE_NEWNS|CLONE_NEWUTS|CLONE_NEWIPC|SIGCHLD, 0);
-#endif
+  int          rc;
 
-  switch (pid) {
-    case -1    :
-      perror("vnamespace: clone()");
-      exit(wrapper_exit_code);
-    case 0     :
-      break;
-    default    :
-      exitLikeProcess(pid, cmd, wrapper_exit_code);
+  rc = sys_unshare(CLONE_NEWNS);
+  if (rc!=0) {
+    perror("vnamespace: unshare()");
+    exit(wrapper_exit_code);
   }
 }
 
 static void
-enterNamespace(xid_t xid)
+enterNamespace(xid_t xid, uint_least64_t mask)
 {
-  if (vc_enter_namespace(xid)==-1) {
+  if (vc_enter_namespace(xid, mask)==-1) {
     perror("vnamespace: vc_enter_namespace()");
     exit(255);
   }
 }
 
 static void
-setNamespace()
+setNamespace(xid_t xid, uint_least64_t mask)
 {
-  if (vc_set_namespace()==-1) {
+  if (vc_set_namespace(xid, mask)==-1) {
     perror("vnamespace: vc_set_namespace()");
     exit(255);
   }
@@ -188,10 +169,10 @@ 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)     newNamespace(argv[optind]);
-    else if (do_set)     setNamespace();
+    if      (do_new)     newNamespace();
+    else if (do_set)     setNamespace(VC_SAMECTX, CLONE_NEWNS|CLONE_FS);
     else if (do_cleanup) cleanupNamespace();
-    else if (do_enter)   enterNamespace(xid);
+    else if (do_enter)   enterNamespace(xid, CLONE_NEWNS|CLONE_FS);
 
     if (optind<argc)
       EexecvpD(argv[optind], argv+optind);