cleanups
[util-vserver.git] / util-vserver / src / rpm-fake-resolver.c
index 4e5bc30..a89ee30 100644 (file)
 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
 
+// Protocol:
+// 1. startup
+// 2. initialize (setuid, ctx-migrate, chroot, ...)
+// 3. send "." token to fd 3
+// 4. wait one character on fd 1
+// 5. process this character and consume further characters from fd 1 as far
+//    as needed
+// 6. go to 3) (or exit)
+
 #ifdef HAVE_CONFIG_H
 #  include <config.h>
 #endif
 #include "internal.h"
 #include "vserver.h"
 #include "util.h"
-#include "wrappers.h"
-#include "wrappers-vserver.h"
 
 #include <getopt.h>
 #include <stdlib.h>
 #include <stdbool.h>
 #include <grp.h>
 #include <pwd.h>
+#include <fcntl.h>
+#include <errno.h>
+
+#define ENSC_WRAPPERS_PREFIX   "rpm-fake-resolver: "
+#define ENSC_WRAPPERS_VSERVER  1
+#define ENSC_WRAPPERS_UNISTD   1
+#define ENSC_WRAPPERS_FCNTL    1
+#include <wrappers.h>
 
 #define MAX_RQSIZE     0x1000
 
@@ -41,6 +56,7 @@ struct ArgInfo {
     uid_t              uid;
     gid_t              gid;
     bool               do_fork;
+    bool               in_ctx;
     char const *       pid_file;
     char const *       chroot;
 };
@@ -58,7 +74,7 @@ showHelp(int fd, char const *cmd, int res)
   WRITE_MSG(fd, "Usage:  ");
   WRITE_STR(fd, cmd);
   WRITE_MSG(fd,
-           " [-c <ctx>] [-u <uid>] [-g <gid>] [-r <chroot>] [-n]\n"
+           " [-c <ctx>] [-u <uid>] [-g <gid>] [-r <chroot>] [-s] [-n]\n"
            "Please report bugs to " PACKAGE_BUGREPORT "\n");
   exit(res);
 }
@@ -79,7 +95,7 @@ inline static void
 parseArgs(struct ArgInfo *args, int argc, char *argv[])
 {
   while (1) {
-    int                c = getopt_long(argc, argv, "c:u:g:r:n", CMDLINE_OPTIONS, 0);
+    int                c = getopt_long(argc, argv, "c:u:g:r:ns", CMDLINE_OPTIONS, 0);
     if (c==-1) break;
 
     switch (c) {
@@ -91,6 +107,7 @@ parseArgs(struct ArgInfo *args, int argc, char *argv[])
       case 'g'         :  args->gid = atoi(optarg); break;
       case 'r'         :  args->chroot  = optarg;   break;
       case 'n'         :  args->do_fork = false;    break;
+      case 's'         :  args->in_ctx  = true;     break;
       default          :
        WRITE_MSG(2, "Try '");
        WRITE_STR(2, argv[0]);
@@ -182,7 +199,7 @@ run()
   uint8_t      c;
 
   while (true) {
-    Ewrite(2, ".", 1);
+    Ewrite(3, ".", 1);
     Eread (0, &c,  sizeof c);
     switch (c) {
       case 'P' :  do_getpwnam(); break;
@@ -198,7 +215,13 @@ run()
 static void
 daemonize(struct ArgInfo const UNUSED * args, int pid_fd)
 {
-  pid_t                pid = Efork();
+  int          p[2];
+  pid_t                pid;
+  char         c;
+  
+  Epipe(p);
+  pid = Efork();
+  
   if (pid!=0) {
     if (pid_fd!=-1) {
       char     buf[sizeof(id_t)*3 + 2];
@@ -210,29 +233,62 @@ daemonize(struct ArgInfo const UNUSED * args, int pid_fd)
     }
     _exit(0);
   }
+  Eclose(p[1]);
+  read(p[0], &c, 1);
+  Eclose(p[0]);
+}
+
+static void
+activateContext(xid_t xid, bool in_ctx)
+{
+  if (in_ctx) {
+    struct vc_ctx_flags                flags = {
+      .flagword = 0,
+      .mask     = VC_VXF_STATE_SETUP,
+    };
+
+    Evc_set_flags(xid, &flags);
+  }
+  else if (vc_isSupported(vcFEATURE_MIGRATE))
+      Evc_migrate_context(xid);
+  else {
+#ifdef VC_ENABLE_API_COMPAT
+    Evc_new_s_context(xid, 0, S_CTX_INFO_LOCK);
+#else
+    WRITE_MSG(2, ENSC_WRAPPERS_PREFIX "can not change context: migrate kernel feature missing and 'compat' API disabled\n");
+    exit(wrapper_exit_code);
+#endif
+  }
 }
 
 int main(int argc, char * argv[])
 {
   struct ArgInfo       args = {
-    .ctx      = VC_RANDCTX,
+    .ctx      = VC_DYNAMIC_XID,
     .uid      = 99,
     .gid      = 99,
-    .do_fork  = false,
+    .do_fork  = true,
     .pid_file = 0,
-    .chroot   = 0
+    .chroot   = 0,
+    .in_ctx   = false,
   };
   int                  pid_fd = -1;
 
+#ifndef __dietlibc__
+#  warning  *** rpm-fake-resolver is built against glibc; please do not report errors before trying a dietlibc version ***
+  WRITE_MSG(2,
+           "***  rpm-fake-resolver was built with glibc;  please do  ***\n"
+           "***  not report errors before trying a dietlibc version. ***\n");
+#endif
+
   parseArgs(&args, argc, argv);
   if (args.pid_file && args.do_fork)
-    pid_fd = Eopen(args.pid_file, O_CREAT|O_WRONLY, 0644);
+    pid_fd = EopenD(args.pid_file, O_CREAT|O_WRONLY, 0644);
   
   if (args.chroot) Echroot(args.chroot);
   Echdir("/");
 
-  //Evc_new_s_context(args.ctx, ~(VC_CAP_SETGID|VC_CAP_SETUID), S_CTX_INFO_LOCK);
-  Evc_new_s_context(args.ctx, 0, S_CTX_INFO_LOCK);
+  activateContext(args.ctx, args.in_ctx);
   Esetgroups(0, &args.gid);
   Esetgid(args.gid);
   Esetuid(args.uid);