added vcopy
[util-vserver.git] / util-vserver / src / new-namespace.c
index 5684f1e..43862fc 100644 (file)
 #endif
 
 #include "util.h"
+#include "stack-start.h"
+#include "sys_clone.h"
 
+#include <sched.h>
 #include <stdio.h>
 #include <unistd.h>
 #include <stdlib.h>
 #include <signal.h>
-#include <sched.h>
 #include <string.h>
 #include <sys/types.h>
 #include <sys/wait.h>
-#include <sched.h>
+#include <libgen.h>
+
+#define ENSC_WRAPPERS_WAIT     1
+#include <wrappers.h>
 
 #ifndef CLONE_NEWNS
 #  define CLONE_NEWNS 0x00020000
 #endif
 
-static int
-childFunc(void *argv_v)
-{
-  char **      argv = argv_v;
-  
-  execvp(argv[0], argv);
-  perror("execvp()");
-  exit(255);
-}
+int    wrapper_exit_code = 255;
 
 static void
-usage(int fd, char const *cmd, int exit_code)
+showHelp(int fd, char const *cmd, int exit_code)
 {
+  VSERVER_DECLARE_CMD(cmd);
+  
   WRITE_MSG(fd,        "Usage:  ");
   WRITE_STR(fd, cmd);
   WRITE_MSG(fd,
@@ -69,35 +68,37 @@ static void
 showVersion()
 {
   WRITE_MSG(1,
-           "new-namespace 0.1\n"
+           "new-namespace " VERSION " -- executes programs in a new namespace\n"
+           "This program is part of " PACKAGE_STRING "\n"
            "Copyright (C) 2003 Enrico Scholz\n"
            VERSION_COPYRIGHT_DISCLAIMER);
   exit(0);
 }
-  
+
 int main(int argc, char *argv[])
 {
-  char                 buf[128];
-  int                  status;
-  pid_t                        pid, p;
+  pid_t                        pid;
 
-  if (argc==1) usage(2, argv[0], 255);
-  if (!strcmp(argv[1], "--help"))    usage(1, argv[0], 0);
+  if (argc==1) showHelp(2, argv[0], 255);
+  if (!strcmp(argv[1], "--help"))    showHelp(1, argv[0], 0);
   if (!strcmp(argv[1], "--version")) showVersion();
   if (!strcmp(argv[1], "--"))        ++argv;
 
-  pid = clone(childFunc, buf+sizeof(buf)/2, CLONE_NEWNS|CLONE_VFORK|SIGCHLD, argv+1);
-  if (pid==-1) {
-    perror("clone()");
-    exit(255);
-  }
-
-  p   = wait4(pid, &status, 0,0);
-  if (p==-1) {
-    perror("waitpid()");
-    exit(255);
+  
+#ifdef NDEBUG    
+  pid = sys_clone(CLONE_NEWNS|CLONE_VFORK|SIGCHLD, 0);
+#else
+  pid = sys_clone(CLONE_NEWNS|SIGCHLD, 0);
+#endif
+  switch (pid) {
+    case -1    :
+      perror("clone()");
+      exit(wrapper_exit_code);
+    case 0     :
+      execvp(argv[1], argv+1);
+      perror("execvp()");
+      exit(wrapper_exit_code);
+    default    :
+      exitLikeProcess(pid);
   }
-
-  if (WIFEXITED(status)) return WEXITSTATUS(status);
-  else                   return 255;
 }