merged from UV_NAMESPACE_AFTER_CHROOT branch
[util-vserver.git] / util-vserver / src / vcontext.c
index 05eee72..0d979cf 100644 (file)
@@ -24,8 +24,6 @@
 #include "lib/internal.h"
 #include "lib_internal/jail.h"
 
-#include "linuxcaps.h"
-
 #include <vserver.h>
 #include <getopt.h>
 #include <fcntl.h>
@@ -33,6 +31,7 @@
 #include <sys/socket.h>
 #include <sys/un.h>
 #include <assert.h>
+#include <signal.h>
 
 #define ENSC_WRAPPERS_PREFIX   "vcontext: "
 #define ENSC_WRAPPERS_UNISTD   1
@@ -47,7 +46,7 @@
 #define CMD_XID                        0x4000
 #define CMD_CREATE             0x4001
 #define CMD_MIGRATE            0x4003
-#define CMD_FAKEINIT           0x4002
+#define CMD_INITPID            0x4002
 #define CMD_DISCONNECT         0x4004
 #define CMD_UID                        0x4005
 #define CMD_CHROOT             0x4006
@@ -57,6 +56,7 @@
 #define CMD_MIGRATESELF                0x400a
 #define CMD_ENDSETUP           0x400b
 #define CMD_SILENTEXIST                0x400c
+#define CMD_NAMESPACE          0x400d
 
 
 struct option const
@@ -68,15 +68,19 @@ CMDLINE_OPTIONS[] = {
   { "create",     no_argument,       0, CMD_CREATE },
   { "migrate",    no_argument,       0, CMD_MIGRATE },
   { "migrate-self", no_argument,               0, CMD_MIGRATESELF },
-  { "fakeinit",     no_argument,               0, CMD_FAKEINIT },
+  { "initpid",      no_argument,               0, CMD_INITPID },
   { "endsetup",     no_argument,        0, CMD_ENDSETUP },
   { "disconnect",   no_argument,       0, CMD_DISCONNECT },
   { "silent",       no_argument,               0, CMD_SILENT },
   { "silentexist",  no_argument,               0, CMD_SILENTEXIST },
   { "uid",          required_argument,  0, CMD_UID },
   { "chroot",       no_argument,               0, CMD_CHROOT },
+  { "namespace",    no_argument,               0, CMD_NAMESPACE },
   { "syncsock",     required_argument,         0, CMD_SYNCSOCK },
   { "syncmsg",      required_argument,         0, CMD_SYNCMSG },
+#if 1  
+  { "fakeinit",     no_argument,               0, CMD_INITPID },       // compatibility
+#endif  
   { 0,0,0,0 },
 };
 
@@ -86,8 +90,9 @@ struct Arguments {
     bool               do_migrateself;
     bool               do_disconnect;
     bool               do_endsetup;
-    bool               is_fakeinit;
+    bool               is_initpid;
     bool               is_silentexist;
+    bool               set_namespace;
     int                        verbosity;
     bool               do_chroot;
     uid_t              uid;
@@ -111,8 +116,9 @@ showHelp(int fd, char const *cmd, int res)
            "\n"
            "<opts> can be:\n"
            "    --chroot        ...  chroot into current directory\n"
+           "    --namespace     ...  execute namespace management operations\n"
            "    --uid <uid>     ...  change uid\n"
-           "    --fakeinit      ...  set current process as general process reaper\n"
+           "    --initpid       ...  set current process as general process reaper\n"
            "                         for ctx (possible for --migrate only)\n"
            "    --endsetup      ...  clear the setup flag; usefully for migrate only\n"
            "    --disconnect    ...  start program in background\n"
@@ -183,7 +189,7 @@ setFlags(struct Arguments const *args, xid_t xid)
 {
   struct vc_ctx_flags  flags = { 0,0 };
 
-  if (args->is_fakeinit)
+  if (args->is_initpid)
     flags.mask |=  VC_VXF_STATE_INIT;
 
   if (args->do_endsetup)
@@ -243,8 +249,13 @@ doit(struct Arguments const *args, char *argv[])
     else
       xid = args->xid;
 
-    if (args->do_chroot)
+    if (args->do_chroot) {
       Echroot(".");
+      if (args->set_namespace) {
+       if (args->do_migrateself)  Evc_set_namespace();
+       else if (args->do_migrate) Evc_enter_namespace(xid);
+      }
+    }
 
     setFlags(args, xid);
 
@@ -283,8 +294,9 @@ int main (int argc, char *argv[])
     .do_migrateself = false,
     .do_disconnect  = false,
     .do_endsetup    = false,
-    .is_fakeinit    = false,
+    .is_initpid     = false,
     .is_silentexist = false,
+    .set_namespace  = false,
     .verbosity      = 1,
     .uid            = -1,
     .xid            = VC_DYNAMIC_XID,
@@ -302,8 +314,9 @@ int main (int argc, char *argv[])
       case CMD_MIGRATE         :  args.do_migrate     = true;   break;
       case CMD_DISCONNECT      :  args.do_disconnect  = true;   break;
       case CMD_ENDSETUP                :  args.do_endsetup    = true;   break;
-      case CMD_FAKEINIT                :  args.is_fakeinit    = true;   break;
+      case CMD_INITPID         :  args.is_initpid     = true;   break;
       case CMD_CHROOT          :  args.do_chroot      = true;   break;
+      case CMD_NAMESPACE       :  args.set_namespace  = true;   break;
       case CMD_SILENTEXIST     :  args.is_silentexist = true;   break;
       case CMD_SYNCSOCK                :  args.sync_sock      = optarg; break;
       case CMD_SYNCMSG         :  args.sync_msg       = optarg; break;
@@ -324,6 +337,8 @@ int main (int argc, char *argv[])
     }
   }
 
+  signal(SIGCHLD, SIG_DFL);
+  
   if (args.do_migrateself)
     args.xid = Evc_get_task_xid(0);
   
@@ -331,8 +346,8 @@ int main (int argc, char *argv[])
     WRITE_MSG(2, "Neither '--create' nor '--migrate specified; try '--help' for more information\n");
   else if (args.do_create  &&  args.do_migrate)
     WRITE_MSG(2, "Can not specify '--create' and '--migrate' at the same time; try '--help' for more information\n");
-  else if (!args.do_migrate && args.is_fakeinit)
-    WRITE_MSG(2, "'--fakeinit' is possible in combination with '--migrate' only\n");
+  else if (!args.do_migrate && args.is_initpid)
+    WRITE_MSG(2, "'--initpid' is possible in combination with '--migrate' only\n");
   else if (!args.do_create && args.xid==VC_DYNAMIC_XID)
     WRITE_MSG(2, ENSC_WRAPPERS_PREFIX "Can not migrate to an unknown context\n");
   else if (optind>=argc)