use unshare(CLONE_NEWNS) instead of a complicated 'clone(NEWNS) ... waitpid()' operation
[util-vserver.git] / src / rpm-fake.c
index 1689e2d..c751dc8 100644 (file)
@@ -25,7 +25,7 @@
 
 #include <lib/vserver.h>
 #include <lib/internal.h>
-#include <lib_internal/sys_clone.h>
+#include <lib_internal/sys_unshare.h>
 
 #include <sys/socket.h>
 #include <dlfcn.h>
@@ -49,7 +49,7 @@
 #include <fcntl.h>
 #include <pwd.h>
 #include <grp.h>
-
+#include <sched.h>
 
   // from selinux.h
   // FIXME: add configure autodetection and include <selinux.h> directly
@@ -96,6 +96,8 @@ static unsigned int   debug_level = 0;
 
 static bool            is_initialized = false;
 
+static bool            ctx_created = false;
+
   //DECLARE(rpm_execcon);
   //DECLARE(execv);
 DECLARE(getpwnam);
@@ -245,7 +247,7 @@ setupContext(xid_t xid, char const **xid_str)
     xid_t      rc=VC_NOCTX;
 
     if ((xid==VC_DYNAMIC_XID || !vc_is_dynamic_xid(xid)) &&
-       (rc=vc_ctx_create(xid))==VC_NOCTX &&
+       (rc=vc_ctx_create(xid, NULL))==VC_NOCTX &&
        errno!=EEXIST) {
       perror(ENSC_WRAPPERS_PREFIX "vc_ctx_create()");
       exit(255);
@@ -255,6 +257,7 @@ setupContext(xid_t xid, char const **xid_str)
       char                     buf[sizeof(xid_t)*3 + 128];
       size_t                   l;
       struct vc_ctx_caps       caps;
+      struct vc_ctx_flags      flags;
       
       strcpy(buf, "rpm-fake.so #");
       l = utilvserver_fmt_uint(buf+sizeof("rpm-fake.so #")-1, getppid());
@@ -265,11 +268,16 @@ setupContext(xid_t xid, char const **xid_str)
       caps.bcaps = ~vc_get_insecurebcaps();
       caps.bmask = ~0ull;
       Evc_set_ccaps(rc, &caps);
+
+      flags.flagword = 0;
+      flags.mask = VC_VXF_SC_HELPER;
+      Evc_set_cflags(rc, &flags);
       
        // context will be activated later...
 
       xid = rc;
       res = true;
+      ctx_created = true;
     }
   }
 
@@ -510,6 +518,15 @@ exitRPMFake()
     uint8_t    c;
     if (read(sync_sock, &c, 1)!=1) { /*...*/ }
     if (write(pw_sock, "Q", 1)!=1) { /*...*/ }
+    if (ctx_created) {
+      if (vc_isSupported(vcFEATURE_VWAIT)) {
+       if (vc_wait_exit(ctx)==-1) { /*...*/ }
+      }
+      else {
+       /* this can race */
+       if (read(sync_sock, &c, 1)!=0) { /*...*/}
+      }
+    }
   }
 }
 
@@ -612,20 +629,12 @@ execvWorker(char const *path, char * const argv[], char * const envp[])
   return res;
 }
 
-struct ExecvParams
-{
-    char const *       path;
-    char * const *     argv;
-    char * const *     envp;
-    char const *       mnts;
-};
-
 static int
-removeNamespaceMountsChild(struct ExecvParams const *params)
+removeNamespaceMounts(char const *mnts)
 {
-  char                 buf[strlen(params->mnts)+1], *ptr;
+  char                 buf[strlen(mnts)+1], *ptr;
 
-  strcpy(buf, params->mnts);
+  strcpy(buf, mnts);
   ptr = strtok(buf, ":");
   while (ptr) {
     if (umount2(ptr, 0)==-1) {
@@ -639,53 +648,27 @@ removeNamespaceMountsChild(struct ExecvParams const *params)
     ptr = strtok(0, ":");
   }
 
-  return execvWorker(params->path, params->argv, params->envp);
+  return 0;
 }
 
 static int
-removeNamespaceMounts(char const *path,
-                     char * const argv[], char * const envp[])
+execv_main(char const *path, char * const argv[], char * const envp[])
 {
-  if (mnts==0) return execvWorker(path, argv, envp);
-
-  {
-    int                                status;
-    pid_t                      p, pid;
-    struct ExecvParams         params;
-
-    params.path = path;
-    params.argv = argv;
-    params.envp = envp;
-    params.mnts = mnts;
-
-      // the rpmlib signal-handler is still active; use the default one to
-      // make wait4() working...
-    signal(SIGCHLD, SIG_DFL);
-
-#ifdef NDEBUG
-    pid = sys_clone(CLONE_NEWNS|SIGCHLD|CLONE_VFORK, 0);
-#else
-    pid = sys_clone(CLONE_NEWNS|SIGCHLD, 0);
-#endif
-
-    switch (pid) {
-      case -1  :  return -1;
-      case 0   :  _exit(removeNamespaceMountsChild(&params));
-      default  :  break;
-    }
-       
-    while ((p=wait4(pid, &status, 0,0))==-1 &&
-          (errno==EINTR || errno==EAGAIN)) ;
-
-    if (p==-1)   return -1;
-
-    if (WIFEXITED(status))   _exit(WEXITSTATUS(status));
-    if (WIFSIGNALED(status)) kill(getpid(), WTERMSIG(status));
+  int          rc = 0;
 
+  if (sys_unshare(CLONE_NEWNS)==-1) {
+    perror("unshare()");
     return -1;
   }
-}
 
+  if (mnts)
+    rc = removeNamespaceMounts(mnts);
+
+  if (rc!=0)
+    return rc;
+  
+  return execvWorker(path, argv, envp);
+}
 
 int
 execv(char const *path, char * const argv[])
@@ -698,7 +681,7 @@ execv(char const *path, char * const argv[])
     WRITE_MSG(2, "', ...)\n");
   }
 
-  return removeNamespaceMounts(path, argv, environ);
+  return execv_main(path, argv, environ);
 }
 
 int
@@ -712,7 +695,7 @@ rpm_execcon(unsigned int UNUSED verified,
     WRITE_MSG(2, "', ...)\n");
   }
 
-  return removeNamespaceMounts(filename, argv, envp);
+  return execv_main(filename, argv, envp);
 }
 
 int