]> mj.ucw.cz Git - moe.git/blobdiff - isolate/isolate.c
Isolate: Directory rules can have flags
[moe.git] / isolate / isolate.c
index 718724a29b34158b3c36e60abddbb4379f5ed12c..ea14aa9998ba4f6e79dd6a838104ad92b558af7b 100644 (file)
@@ -1,14 +1,14 @@
 /*
- *     A Process Isolator based in Linux Containers
+ *     A Process Isolator based on Linux Containers
  *
  *     (c) 2012 Martin Mares <mj@ucw.cz>
+ *     (c) 2012 Bernard Blackham <bernard@blackham.com.au>
  */
 
 #define _GNU_SOURCE
 
 #include "autoconf.h"
 
-// FIXME: prune
 #include <errno.h>
 #include <stdio.h>
 #include <fcntl.h>
 #include <time.h>
 #include <grp.h>
 #include <sys/wait.h>
-#include <sys/user.h>
 #include <sys/time.h>
-#include <sys/ptrace.h>
 #include <sys/signal.h>
-#include <sys/sysinfo.h>
 #include <sys/resource.h>
 #include <sys/mount.h>
 #include <sys/stat.h>
 #define UNUSED __attribute__((unused))
 #define ARRAY_SIZE(a) (int)(sizeof(a)/sizeof(a[0]))
 
-// FIXME: Make configurable, probably in compile time
-#define BOX_DIR "/tmp/box"
-#define BOX_UID 60000
-#define BOX_GID 60000
+#define BOX_DIR CONFIG_ISOLATE_BOX_DIR
+#define BOX_UID CONFIG_ISOLATE_BOX_UID
+#define BOX_GID CONFIG_ISOLATE_BOX_GID
 
 static int timeout;                    /* milliseconds */
 static int wall_timeout;
@@ -47,20 +43,34 @@ static int pass_environ;
 static int verbose;
 static int memory_limit;
 static int stack_limit;
+static int max_processes = 1;
 static char *redir_stdin, *redir_stdout, *redir_stderr;
-static char *set_cwd;
+
+static int cg_enable;
+static int cg_memory_limit;
+static int cg_timing;
+static char *cg_root = "/sys/fs/cgroup";
+
+static uid_t orig_uid;
+static gid_t orig_gid;
 
 static pid_t box_pid;
-static volatile int timer_tick;
-static struct timeval start_time;
-static int ticks_per_sec;
 static int partial_line;
+static char cleanup_cmd[256];
 
-static int mem_peak_kb;
+static struct timeval start_time;
+static int ticks_per_sec;
 static int total_ms, wall_ms;
+static volatile sig_atomic_t timer_tick;
+
+static int error_pipes[2];
+static int write_errors_to_fd;
+static int read_errors_from_fd;
 
 static void die(char *msg, ...) NONRET;
-static void sample_mem_peak(void);
+static void cg_stats(void);
+static int get_wall_time_ms(void);
+static int get_run_time_ms(struct rusage *rus);
 
 /*** Meta-files ***/
 
@@ -101,26 +111,35 @@ meta_printf(const char *fmt, ...)
 static void
 final_stats(struct rusage *rus)
 {
-  struct timeval total, now, wall;
-  timeradd(&rus->ru_utime, &rus->ru_stime, &total);
-  total_ms = total.tv_sec*1000 + total.tv_usec/1000;
-  gettimeofday(&now, NULL);
-  timersub(&now, &start_time, &wall);
-  wall_ms = wall.tv_sec*1000 + wall.tv_usec/1000;
+  total_ms = get_run_time_ms(rus);
+  wall_ms = get_wall_time_ms();
 
   meta_printf("time:%d.%03d\n", total_ms/1000, total_ms%1000);
   meta_printf("time-wall:%d.%03d\n", wall_ms/1000, wall_ms%1000);
-  meta_printf("mem:%llu\n", (unsigned long long) mem_peak_kb * 1024);
+  meta_printf("max-rss:%ld\n", rus->ru_maxrss);
+  meta_printf("csw-voluntary:%ld\n", rus->ru_nvcsw);
+  meta_printf("csw-forced:%ld\n", rus->ru_nivcsw);
+
+  cg_stats();
 }
 
 /*** Messages and exits ***/
 
+static void
+xsystem(const char *cmd)
+{
+  int ret = system(cmd);
+  if (ret < 0)
+    die("system(\"%s\"): %m", cmd);
+  if (!WIFEXITED(ret) || WEXITSTATUS(ret))
+    die("system(\"%s\"): Exited with status %d", cmd, ret);
+}
+
 static void NONRET
 box_exit(int rc)
 {
   if (box_pid > 0)
     {
-      sample_mem_peak();
       kill(-box_pid, SIGKILL);
       kill(box_pid, SIGKILL);
       meta_printf("killed:1\n");
@@ -135,6 +154,10 @@ box_exit(int rc)
       else
        final_stats(&rus);
     }
+
+  if (rc < 2 && cleanup_cmd[0])
+    xsystem(cleanup_cmd);
+
   meta_close();
   exit(rc);
 }
@@ -153,9 +176,19 @@ die(char *msg, ...)
 {
   va_list args;
   va_start(args, msg);
-  flush_line();
   char buf[1024];
-  vsnprintf(buf, sizeof(buf), msg, args);
+  int n = vsnprintf(buf, sizeof(buf), msg, args);
+
+  if (write_errors_to_fd)
+    {
+      // We are inside the box, have to use error pipe for error reporting.
+      // We hope that the whole error message fits in PIPE_BUF bytes.
+      write(write_errors_to_fd, buf, n);
+      exit(2);
+    }
+
+  // Otherwise, we in the box keeper process, so we report errors normally
+  flush_line();
   meta_printf("status:XX\nmessage:%s\n", buf);
   fputs(buf, stderr);
   fputc('\n', stderr);
@@ -199,6 +232,8 @@ msg(char *msg, ...)
   va_end(args);
 }
 
+/*** Utility functions ***/
+
 static void *
 xmalloc(size_t size)
 {
@@ -208,6 +243,21 @@ xmalloc(size_t size)
   return p;
 }
 
+static char *
+xstrdup(char *str)
+{
+  char *p = strdup(str);
+  if (!p)
+    die("Out of memory");
+  return p;
+}
+
+static int dir_exists(char *path)
+{
+  struct stat st;
+  return (stat(path, &st) >= 0 && S_ISDIR(st.st_mode));
+}
+
 /*** Environment rules ***/
 
 struct env_rule {
@@ -343,7 +393,368 @@ setup_environment(void)
   return env;
 }
 
-/*** FIXME ***/
+/*** Mount rules ***/
+
+struct dir_rule {
+  char *inside;                        // A relative path
+  char *outside;               // This can be an absolute path or a relative path starting with "./"
+  unsigned int flags;          // DIR_FLAG_xxx
+  struct dir_rule *next;
+};
+
+enum dir_rule_flags {
+  DIR_FLAG_RW = 1,
+  DIR_FLAG_NOEXEC = 2,
+  DIR_FLAG_FS = 4,
+  DIR_FLAG_MAYBE = 8,
+};
+
+static struct dir_rule *first_dir_rule;
+static struct dir_rule **last_dir_rule = &first_dir_rule;
+
+static int add_dir_rule(char *in, char *out, unsigned int flags)
+{
+  // Make sure that "in" is relative
+  while (in[0] == '/')
+    in++;
+  if (!*in)
+    return 0;
+
+  // Check "out"
+  if (flags & DIR_FLAG_FS)
+    {
+      if (!out || out[0] == '/')
+       return 0;
+    }
+  else
+    {
+      if (out && out[0] != '/' && strncmp(out, "./", 2))
+       return 0;
+    }
+
+  // Override an existing rule
+  struct dir_rule *r;
+  for (r = first_dir_rule; r; r=r->next)
+    if (!strcmp(r->inside, in))
+      break;
+
+  // Add a new rule
+  if (!r)
+    {
+      struct dir_rule *r = xmalloc(sizeof(*r));
+      r->inside = in;
+      *last_dir_rule = r;
+      last_dir_rule = &r->next;
+      r->next = NULL;
+    }
+  r->outside = out;
+  r->flags = flags;
+  return 1;
+}
+
+static unsigned int parse_dir_option(char *opt)
+{
+  if (!strcmp(opt, "rw"))
+    return DIR_FLAG_RW;
+  if (!strcmp(opt, "noexec"))
+    return DIR_FLAG_NOEXEC;
+  if (!strcmp(opt, "fs"))
+    return DIR_FLAG_FS;
+  if (!strcmp(opt, "maybe"))
+    return DIR_FLAG_MAYBE;
+  die("Unknown directory option %s", opt);
+}
+
+static int set_dir_action(char *arg)
+{
+  arg = xstrdup(arg);
+
+  char *colon = strchr(arg, ':');
+  unsigned int flags = 0;
+  while (colon)
+    {
+      char *opt = colon + 1;
+      char *next = strchr(opt, ':');
+      if (next)
+       *next = 0;
+      flags |= parse_dir_option(opt);
+      colon = next;
+    }
+
+  char *eq = strchr(arg, '=');
+  if (eq)
+    {
+      *eq++ = 0;
+      return add_dir_rule(arg, (*eq ? eq : NULL), flags);
+    }
+  else
+    {
+      char *out = xmalloc(1 + strlen(arg) + 1);
+      sprintf(out, "/%s", arg);
+      return add_dir_rule(arg, out, flags);
+    }
+}
+
+static void init_dir_rules(void)
+{
+  set_dir_action("box=./box:rw");
+  set_dir_action("bin");
+  set_dir_action("dev");
+  set_dir_action("lib");
+  set_dir_action("lib64:maybe");
+  set_dir_action("proc=proc:fs");
+  set_dir_action("usr");
+}
+
+static void make_dir(char *path)
+{
+  char *sep = path;
+  for (;;)
+    {
+      sep = strchr(sep, '/');
+      if (sep)
+       *sep = 0;
+
+      if (!dir_exists(path) && mkdir(path, 0777) < 0)
+       die("Cannot create directory %s: %m\n", path);
+
+      if (!sep)
+       return;
+      *sep++ = '/';
+    }
+}
+
+static void apply_dir_rules(void)
+{
+  for (struct dir_rule *r = first_dir_rule; r; r=r->next)
+    {
+      char *in = r->inside;
+      char *out = r->outside;
+      if (!out)
+       {
+         msg("Not binding anything on %s\n", r->inside);
+         continue;
+       }
+
+      if ((r->flags & DIR_FLAG_MAYBE) && !dir_exists(out))
+       {
+         msg("Not binding %s on %s (does not exist)\n", out, r->inside);
+         continue;
+       }
+
+      char root_in[1024];
+      snprintf(root_in, sizeof(root_in), "root/%s", in);
+      make_dir(root_in);
+
+      unsigned long mount_flags = 0;
+      if (!(r->flags & DIR_FLAG_RW))
+       mount_flags |= MS_RDONLY;
+      if (r->flags & DIR_FLAG_NOEXEC)
+       mount_flags |= MS_NOEXEC;
+
+      if (r->flags & DIR_FLAG_FS)
+       {
+         msg("Mounting %s on %s\n", out, in);
+         if (mount("none", root_in, out, mount_flags, "") < 0)
+           die("Cannot mount %s on %s: %m", out, in);
+       }
+      else
+       {
+         msg("Binding %s on %s\n", out, in);
+         if (mount(out, root_in, "none", MS_BIND | MS_NOSUID | MS_NODEV | mount_flags, "") < 0)
+           die("Cannot bind %s on %s: %m", out, in);
+       }
+    }
+}
+
+/*** Control groups ***/
+
+static char cg_path[256];
+
+#define CG_BUFSIZE 1024
+
+static int
+cg_read(char *attr, char *buf)
+{
+  int maybe = 0;
+  if (attr[0] == '?')
+    {
+      attr++;
+      maybe = 1;
+    }
+
+  char path[256];
+  snprintf(path, sizeof(path), "%s/%s", cg_path, attr);
+
+  int fd = open(path, O_RDONLY);
+  if (fd < 0)
+    {
+      if (maybe)
+       return 0;
+      die("Cannot read %s: %m", path);
+    }
+
+  int n = read(fd, buf, CG_BUFSIZE);
+  if (n < 0)
+    die("Cannot read %s: %m", path);
+  if (n >= CG_BUFSIZE - 1)
+    die("Attribute %s too long", path);
+  if (n > 0 && buf[n-1] == '\n')
+    n--;
+  buf[n] = 0;
+
+  if (verbose > 1)
+    msg("CG: Read %s = %s\n", attr, buf);
+
+  close(fd);
+  return 1;
+}
+
+static void __attribute__((format(printf,2,3)))
+cg_write(char *attr, char *fmt, ...)
+{
+  va_list args;
+  va_start(args, fmt);
+
+  char buf[CG_BUFSIZE];
+  int n = vsnprintf(buf, sizeof(buf), fmt, args);
+  if (n >= CG_BUFSIZE)
+    die("cg_writef: Value for attribute %s is too long", attr);
+
+  if (verbose > 1)
+    msg("CG: Write %s = %s", attr, buf);
+
+  char path[256];
+  snprintf(path, sizeof(path), "%s/%s", cg_path, attr);
+
+  int fd = open(path, O_WRONLY | O_TRUNC);
+  if (fd < 0)
+    die("Cannot write %s: %m", path);
+
+  int written = write(fd, buf, n);
+  if (written < 0)
+    die("Cannot set %s to %s: %m", path, buf);
+  if (written != n)
+    die("Short write to %s (%d out of %d bytes)", path, written, n);
+
+  close(fd);
+  va_end(args);
+}
+
+static void
+cg_init(void)
+{
+  if (!cg_enable)
+    return;
+
+  if (!dir_exists(cg_root))
+    die("Control group filesystem at %s not mounted", cg_root);
+
+  snprintf(cg_path, sizeof(cg_path), "%s/box-%d", cg_root, BOX_UID);
+  msg("Using control group %s\n", cg_path);
+}
+
+static void
+cg_prepare(void)
+{
+  if (!cg_enable)
+    return;
+
+  struct stat st;
+  char buf[CG_BUFSIZE];
+
+  if (stat(cg_path, &st) >= 0 || errno != ENOENT)
+    {
+      msg("Control group %s already exists, trying to empty it.\n", cg_path);
+      if (rmdir(cg_path) < 0)
+       die("Failed to reset control group %s: %m", cg_path);
+    }
+
+  if (mkdir(cg_path, 0777) < 0)
+    die("Failed to create control group %s: %m", cg_path);
+
+  // If cpuset module is enabled, copy allowed cpus and memory nodes from parent group
+  if (cg_read("?../cpuset.cpus", buf))
+    cg_write("cpuset.cpus", "%s", buf);
+  if (cg_read("?../cpuset.mems", buf))
+    cg_write("cpuset.mems", "%s", buf);
+}
+
+static void
+cg_enter(void)
+{
+  if (!cg_enable)
+    return;
+
+  msg("Entering control group %s\n", cg_path);
+
+  struct stat st;
+  if (stat(cg_path, &st) < 0)
+    die("Control group %s does not exist: %m", cg_path);
+
+  if (cg_memory_limit)
+    {
+      cg_write("memory.limit_in_bytes", "%lld\n", (long long) cg_memory_limit << 10);
+      cg_write("memory.memsw.limit_in_bytes", "%lld\n", (long long) cg_memory_limit << 10);
+    }
+
+  if (cg_timing)
+    cg_write("cpuacct.usage", "0\n");
+
+  cg_write("tasks", "%d\n", (int) getpid());
+}
+
+static int
+cg_get_run_time_ms(void)
+{
+  if (!cg_enable)
+    return 0;
+
+  char buf[CG_BUFSIZE];
+  cg_read("cpuacct.usage", buf);
+  unsigned long long ns = atoll(buf);
+  return ns / 1000000;
+}
+
+static void
+cg_stats(void)
+{
+  if (!cg_enable)
+    return;
+
+  char buf[CG_BUFSIZE];
+
+  // Memory usage statistics
+  unsigned long long mem=0, memsw=0;
+  if (cg_read("?memory.max_usage_in_bytes", buf))
+    mem = atoll(buf);
+  if (cg_read("?memory.memsw.max_usage_in_bytes", buf))
+    {
+      memsw = atoll(buf);
+      if (memsw > mem)
+       mem = memsw;
+    }
+  if (mem)
+    meta_printf("cg-mem:%lld\n", mem >> 10);
+}
+
+static void
+cg_remove(void)
+{
+  char buf[CG_BUFSIZE];
+
+  if (!cg_enable)
+    return;
+
+  cg_read("tasks", buf);
+  if (buf[0])
+    die("Some tasks left in control group %s, failed to remove it", cg_path);
+
+  if (rmdir(cg_path) < 0)
+    die("Cannot remove control group %s: %m", cg_path);
+}
+
+/*** The keeper process ***/
 
 static void
 signal_alarm(int unused UNUSED)
@@ -382,16 +793,56 @@ read_proc_file(char *buf, char *name, int *fdp)
   buf[c] = 0;
 }
 
+static int
+get_wall_time_ms(void)
+{
+  struct timeval now, wall;
+  gettimeofday(&now, NULL);
+  timersub(&now, &start_time, &wall);
+  return wall.tv_sec*1000 + wall.tv_usec/1000;
+}
+
+static int
+get_run_time_ms(struct rusage *rus)
+{
+  if (cg_timing)
+    return cg_get_run_time_ms();
+
+  if (rus)
+    {
+      struct timeval total;
+      timeradd(&rus->ru_utime, &rus->ru_stime, &total);
+      return total.tv_sec*1000 + total.tv_usec/1000;
+    }
+
+  char buf[PROC_BUF_SIZE], *x;
+  int utime, stime;
+  static int proc_stat_fd;
+
+  read_proc_file(buf, "stat", &proc_stat_fd);
+  x = buf;
+  while (*x && *x != ' ')
+    x++;
+  while (*x == ' ')
+    x++;
+  if (*x++ != '(')
+    die("proc stat syntax error 1");
+  while (*x && (*x != ')' || x[1] != ' '))
+    x++;
+  while (*x == ')' || *x == ' ')
+    x++;
+  if (sscanf(x, "%*c %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %d %d", &utime, &stime) != 2)
+    die("proc stat syntax error 2");
+
+  return (utime + stime) * 1000 / ticks_per_sec;
+}
+
 static void
 check_timeout(void)
 {
   if (wall_timeout)
     {
-      struct timeval now, wall;
-      int wall_ms;
-      gettimeofday(&now, NULL);
-      timersub(&now, &start_time, &wall);
-      wall_ms = wall.tv_sec*1000 + wall.tv_usec/1000;
+      int wall_ms = get_wall_time_ms();
       if (wall_ms > wall_timeout)
         err("TO: Time limit exceeded (wall clock)");
       if (verbose > 1)
@@ -399,24 +850,7 @@ check_timeout(void)
     }
   if (timeout)
     {
-      char buf[PROC_BUF_SIZE], *x;
-      int utime, stime, ms;
-      static int proc_stat_fd;
-      read_proc_file(buf, "stat", &proc_stat_fd);
-      x = buf;
-      while (*x && *x != ' ')
-       x++;
-      while (*x == ' ')
-       x++;
-      if (*x++ != '(')
-       die("proc stat syntax error 1");
-      while (*x && (*x != ')' || x[1] != ' '))
-       x++;
-      while (*x == ')' || *x == ' ')
-       x++;
-      if (sscanf(x, "%*c %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %d %d", &utime, &stime) != 2)
-       die("proc stat syntax error 2");
-      ms = (utime + stime) * 1000 / ticks_per_sec;
+      int ms = get_run_time_ms(NULL);
       if (verbose > 1)
        fprintf(stderr, "[time check: %d msec]\n", ms);
       if (ms > timeout && ms > extra_timeout)
@@ -424,56 +858,13 @@ check_timeout(void)
     }
 }
 
-static void
-sample_mem_peak(void)
-{
-  /*
-   *  We want to find out the peak memory usage of the process, which is
-   *  maintained by the kernel, but unforunately it gets lost when the
-   *  process exits (it is not reported in struct rusage). Therefore we
-   *  have to sample it whenever we suspect that the process is about
-   *  to exit.
-   */
-  char buf[PROC_BUF_SIZE], *x;
-  static int proc_status_fd;
-  read_proc_file(buf, "status", &proc_status_fd);
-
-  x = buf;
-  while (*x)
-    {
-      char *key = x;
-      while (*x && *x != ':' && *x != '\n')
-       x++;
-      if (!*x || *x == '\n')
-       break;
-      *x++ = 0;
-      while (*x == ' ' || *x == '\t')
-       x++;
-
-      char *val = x;
-      while (*x && *x != '\n')
-       x++;
-      if (!*x)
-       break;
-      *x++ = 0;
-
-      if (!strcmp(key, "VmPeak"))
-       {
-         int peak = atoi(val);
-         if (peak > mem_peak_kb)
-           mem_peak_kb = peak;
-       }
-    }
-
-  if (verbose > 1)
-    msg("[mem-peak: %u KB]\n", mem_peak_kb);
-}
-
 static void
 box_keeper(void)
 {
-  struct sigaction sa;
+  read_errors_from_fd = error_pipes[0];
+  close(error_pipes[1]);
 
+  struct sigaction sa;
   bzero(&sa, sizeof(sa));
   sa.sa_handler = signal_int;
   sigaction(SIGINT, &sa, NULL);
@@ -509,13 +900,22 @@ box_keeper(void)
        }
       if (p != box_pid)
        die("wait4: unknown pid %d exited!", p);
+      box_pid = 0;
+
+      // Check error pipe if there is an internal error passed from inside the box
+      char interr[1024];
+      int n = read(read_errors_from_fd, interr, sizeof(interr) - 1);
+      if (n > 0)
+       {
+         interr[n] = 0;
+         die("%s", interr);
+       }
+
       if (WIFEXITED(stat))
        {
-         box_pid = 0;
          final_stats(&rus);
          if (WEXITSTATUS(stat))
            {
-             // FIXME: Recognize internal errors during setup
              meta_printf("exitcode:%d\n", WEXITSTATUS(stat));
              err("RE: Exited with error status %d", WEXITSTATUS(stat));
            }
@@ -524,22 +924,19 @@ box_keeper(void)
          if (wall_timeout && wall_ms > wall_timeout)
            err("TO: Time limit exceeded (wall clock)");
          flush_line();
-         fprintf(stderr, "OK (%d.%03d sec real, %d.%03d sec wall, %d MB)\n",
+         fprintf(stderr, "OK (%d.%03d sec real, %d.%03d sec wall)\n",
              total_ms/1000, total_ms%1000,
-             wall_ms/1000, wall_ms%1000,
-             (mem_peak_kb + 1023) / 1024);
+             wall_ms/1000, wall_ms%1000);
          box_exit(0);
        }
-      if (WIFSIGNALED(stat))
+      else if (WIFSIGNALED(stat))
        {
-         box_pid = 0;
          meta_printf("exitsig:%d\n", WTERMSIG(stat));
          final_stats(&rus);
          err("SG: Caught fatal signal %d", WTERMSIG(stat));
        }
-      if (WIFSTOPPED(stat))
+      else if (WIFSTOPPED(stat))
        {
-         box_pid = 0;
          meta_printf("exitsig:%d\n", WSTOPSIG(stat));
          final_stats(&rus);
          err("SG: Stopped by signal %d", WSTOPSIG(stat));
@@ -549,38 +946,18 @@ box_keeper(void)
     }
 }
 
+/*** The process running inside the box ***/
+
 static void
 setup_root(void)
 {
-  umask(0022);
-
-  if (mkdir("root", 0777) < 0 && errno != EEXIST)
+  if (mkdir("root", 0750) < 0 && errno != EEXIST)
     die("mkdir('root'): %m");
 
   if (mount("none", "root", "tmpfs", 0, "mode=755") < 0)
     die("Cannot mount root ramdisk: %m");
 
-  // FIXME: Make the list of bind-mounts configurable
-  // FIXME: Virtual dev?
-  // FIXME: Read-only mounts?
-
-  static const char * const dirs[] = { "box", "/bin", "/lib", "/usr", "/dev" };
-  for (int i=0; i < ARRAY_SIZE(dirs); i++)
-    {
-      const char *d = dirs[i];
-      char buf[1024];  // FIXME
-      sprintf(buf, "root/%s", (d[0] == '/' ? d+1 : d));
-      printf("Binding %s on %s\n", d, buf);
-      if (mkdir(buf, 0777) < 0)
-       die("mkdir(%s): %m", buf);
-      if (mount(d, buf, "none", MS_BIND | MS_NOSUID | MS_NODEV, "") < 0)
-       die("Cannot bind %s on %s: %m", d, buf);
-    }
-
-  if (mkdir("root/proc", 0777) < 0)
-    die("Cannot create proc: %m");
-  if (mount("none", "root/proc", "proc", 0, "") < 0)
-    die("Cannot mount proc: %m");
+  apply_dir_rules();
 
   if (chroot("root") < 0)
     die("Chroot failed: %m");
@@ -589,29 +966,21 @@ setup_root(void)
     die("Cannot change current directory: %m");
 }
 
-static int
-box_inside(void *arg)
+static void
+setup_credentials(void)
 {
-  char **argv = arg;
-  int argc = 0;
-  while (argv[argc])
-    argc++;
-
-  struct rlimit rl;
-  char *args[argc+1];
-
-  memcpy(args, argv, argc * sizeof(char *));
-  args[argc] = NULL;
-
-  setup_root();
-
   if (setresgid(BOX_GID, BOX_GID, BOX_GID) < 0)
     die("setresgid: %m");
   if (setgroups(0, NULL) < 0)
     die("setgroups: %m");
   if (setresuid(BOX_UID, BOX_UID, BOX_UID) < 0)
     die("setresuid: %m");
+  setpgrp();
+}
 
+static void
+setup_fds(void)
+{
   if (redir_stdin)
     {
       close(0);
@@ -632,67 +1001,95 @@ box_inside(void *arg)
     }
   else
     dup2(1, 2);
-  setpgrp();
+}
+
+static void
+setup_rlim(const char *res_name, int res, rlim_t limit)
+{
+  struct rlimit rl = { .rlim_cur = limit, .rlim_max = limit };
+  if (setrlimit(res, &rl) < 0)
+    die("setrlimit(%s, %jd)", res_name, (intmax_t) limit);
+}
+
+static void
+setup_rlimits(void)
+{
+#define RLIM(res, val) setup_rlim("RLIMIT_" #res, RLIMIT_##res, val)
 
   if (memory_limit)
-    {
-      rl.rlim_cur = rl.rlim_max = memory_limit * 1024;
-      if (setrlimit(RLIMIT_AS, &rl) < 0)
-       die("setrlimit(RLIMIT_AS): %m");
-    }
+    RLIM(AS, memory_limit * 1024);
 
-  rl.rlim_cur = rl.rlim_max = (stack_limit ? (rlim_t)stack_limit * 1024 : RLIM_INFINITY);
-  if (setrlimit(RLIMIT_STACK, &rl) < 0)
-    die("setrlimit(RLIMIT_STACK): %m");
+  RLIM(STACK, (stack_limit ? (rlim_t)stack_limit * 1024 : RLIM_INFINITY));
+  RLIM(NOFILE, 64);
+  RLIM(MEMLOCK, 0);
 
-  rl.rlim_cur = rl.rlim_max = 64;
-  if (setrlimit(RLIMIT_NOFILE, &rl) < 0)
-    die("setrlimit(RLIMIT_NOFILE): %m");
+  if (max_processes)
+    RLIM(NPROC, max_processes);
 
-  // FIXME: Create multi-process mode
-  rl.rlim_cur = rl.rlim_max = 1;
-  if (setrlimit(RLIMIT_NPROC, &rl) < 0)
-    die("setrlimit(RLIMIT_NPROC): %m");
+#undef RLIM
+}
 
-  rl.rlim_cur = rl.rlim_max = 0;
-  if (setrlimit(RLIMIT_MEMLOCK, &rl) < 0)
-    die("setrlimit(RLIMIT_MEMLOCK): %m");
+static int
+box_inside(void *arg)
+{
+  char **args = arg;
+  write_errors_to_fd = error_pipes[1];
+  close(error_pipes[0]);
 
+  cg_enter();
+  setup_root();
+  setup_credentials();
+  setup_fds();
+  setup_rlimits();
   char **env = setup_environment();
+
   execve(args[0], args, env);
   die("execve(\"%s\"): %m", args[0]);
 }
 
+/*** Commands ***/
+
 static void
-prepare(void)
+init(void)
 {
-  // FIXME: Move chdir to common code?
-  if (chdir(BOX_DIR) < 0)
-    die("chdir(%s): %m", BOX_DIR);
-
-  if (system("./prepare"))
-    die("Prepare hook failed");
+  msg("Preparing sandbox directory\n");
+  xsystem("rm -rf box");
+  if (mkdir("box", 0700) < 0)
+    die("Cannot create box: %m");
+  if (chown("box", orig_uid, orig_gid) < 0)
+    die("Cannot chown box: %m");
+
+  cg_prepare();
 }
 
 static void
 cleanup(void)
 {
-  if (chdir(BOX_DIR) < 0)
-    die("chdir(%s): %m", BOX_DIR);
+  if (!dir_exists("box"))
+    die("Box directory not found, there isn't anything to clean up");
 
-  if (system("./cleanup"))
-    die("Prepare hook failed");
+  msg("Deleting sandbox directory\n");
+  xsystem("rm -rf box");
+  cg_remove();
 }
 
 static void
 run(char **argv)
 {
-  if (chdir(BOX_DIR) < 0)
-    die("chdir(%s): %m", BOX_DIR);
+  if (!dir_exists("box"))
+    die("Box directory not found, did you run `isolate --init'?");
 
-  struct stat st;
-  if (stat("box", &st) < 0 || !S_ISDIR(st.st_mode))
-    die("Box directory not found, did you run `isolate --prepare'?");
+  char cmd[256];
+  snprintf(cmd, sizeof(cmd), "chown -R %d.%d box", BOX_UID, BOX_GID);
+  xsystem(cmd);
+  snprintf(cleanup_cmd, sizeof(cleanup_cmd), "chown -R %d.%d box", orig_uid, orig_gid);
+
+  if (pipe(error_pipes) < 0)
+    die("pipe: %m");
+  for (int i=0; i<2; i++)
+    if (fcntl(error_pipes[i], F_SETFD, fcntl(error_pipes[i], F_GETFD) | FD_CLOEXEC) < 0 ||
+        fcntl(error_pipes[i], F_SETFL, fcntl(error_pipes[i], F_GETFL) | O_NONBLOCK) < 0)
+      die("fcntl on pipe: %m");
 
   box_pid = clone(
     box_inside,                        // Function to execute as the body of the new process
@@ -706,53 +1103,91 @@ run(char **argv)
   box_keeper();
 }
 
-// FIXME: Prune (and also the option list)
+static void
+show_version(void)
+{
+  printf("Process isolator 1.0\n");
+  printf("(c) 2012 Martin Mares and Bernard Blackham\n");
+  printf("\nCompile-time configuration:\n");
+  printf("Sandbox directory: %s\n", BOX_DIR);
+  printf("Sandbox credentials: uid=%u gid=%u\n", BOX_UID, BOX_GID);
+}
+
+/*** Options ***/
+
 static void
 usage(void)
 {
   fprintf(stderr, "Invalid arguments!\n");
   printf("\
-Usage: box [<options>] -- <command> <arguments>\n\
+Usage: isolate [<options>] <command>\n\
 \n\
 Options:\n\
--a <level>\tSet file access level (0=none, 1=cwd, 2=/etc,/lib,..., 3=whole fs, 9=no checks; needs -f)\n\
--c <dir>\tChange directory to <dir> first\n\
--e\t\tInherit full environment of the parent process\n\
--E <var>\tInherit the environment variable <var> from the parent process\n\
--E <var>=<val>\tSet the environment variable <var> to <val>; unset it if <var> is empty\n\
--f\t\tFilter system calls (-ff=very restricted)\n\
--i <file>\tRedirect stdin from <file>\n\
--k <size>\tLimit stack size to <size> KB (default: 0=unlimited)\n\
--m <size>\tLimit address space to <size> KB\n\
--M <file>\tOutput process information to <file> (name:value)\n\
--o <file>\tRedirect stdout to <file>\n\
--p <path>\tPermit access to the specified path (or subtree if it ends with a `/')\n\
--p <path>=<act>\tDefine action for the specified path (<act>=yes/no)\n\
--r <file>\tRedirect stderr to <file>\n\
--s <sys>\tPermit the specified syscall (be careful)\n\
--s <sys>=<act>\tDefine action for the specified syscall (<act>=yes/no/file)\n\
--t <time>\tSet run time limit (seconds, fractions allowed)\n\
--T\t\tAllow syscalls for measuring run time\n\
--v\t\tBe verbose (use multiple times for even more verbosity)\n\
--w <time>\tSet wall clock time limit (seconds, fractions allowed)\n\
--x <time>\tSet extra timeout, before which a timing-out program is not yet killed,\n\
-\t\tso that its real execution time is reported (seconds, fractions allowed)\n\
+-c, --cg[=<parent>]\tPut process in a control group (optionally a sub-group of <parent>)\n\
+    --cg-mem=<size>\tLimit memory usage of the control group to <size> KB\n\
+    --cg-timing\t\tTime limits affects total run time of the control group\n\
+-d, --dir=<dir>\t\tMake a directory <dir> visible inside the sandbox\n\
+    --dir=<in>=<out>\tMake a directory <out> outside visible as <in> inside\n\
+    --dir=<in>=\t\tDelete a previously defined directory rule (even a default one)\n\
+    --dir=...:<opt>\tSpecify options for a rule: rw, noexec, fs, maybe\n\
+-E, --env=<var>\t\tInherit the environment variable <var> from the parent process\n\
+-E, --env=<var>=<val>\tSet the environment variable <var> to <val>; unset it if <var> is empty\n\
+-x, --extra-time=<time>\tSet extra timeout, before which a timing-out program is not yet killed,\n\
+\t\t\tso that its real execution time is reported (seconds, fractions allowed)\n\
+-e, --full-env\t\tInherit full environment of the parent process\n\
+-m, --mem=<size>\tLimit address space to <size> KB\n\
+-M, --meta=<file>\tOutput process information to <file> (name:value)\n\
+-k, --stack=<size>\tLimit stack size to <size> KB (default: 0=unlimited)\n\
+-r, --stderr=<file>\tRedirect stderr to <file>\n\
+-i, --stdin=<file>\tRedirect stdin from <file>\n\
+-o, --stdout=<file>\tRedirect stdout to <file>\n\
+-p, --processes[=<max>]\tEnable multiple processes (at most <max> of them); needs --cg\n\
+-t, --time=<time>\tSet run time limit (seconds, fractions allowed)\n\
+-v, --verbose\t\tBe verbose (use multiple times for even more verbosity)\n\
+-w, --wall-time=<time>\tSet wall clock time limit (seconds, fractions allowed)\n\
+\n\
+Commands:\n\
+    --init\t\tInitialize sandbox (and its control group when --cg is used)\n\
+    --run -- <cmd> ...\tRun given command within sandbox\n\
+    --cleanup\t\tClean up sandbox\n\
+    --version\t\tDisplay program version and configuration\n\
 ");
   exit(2);
 }
 
 enum opt_code {
-  OPT_PREPARE,
+  OPT_INIT = 256,
   OPT_RUN,
   OPT_CLEANUP,
+  OPT_VERSION,
+  OPT_CG_MEM,
+  OPT_CG_TIMING,
 };
 
-static const char short_opts[] = "a:c:eE:fi:k:m:M:o:p:r:s:t:Tvw:x:";
+static const char short_opts[] = "c::d:eE:i:k:m:M:o:p::r:t:vw:x:";
 
 static const struct option long_opts[] = {
-  { "prepare",         0, NULL, OPT_PREPARE },
-  { "run",             0, NULL, OPT_RUN },
+  { "cg",              2, NULL, 'c' },
+  { "cg-mem",          1, NULL, OPT_CG_MEM },
+  { "cg-timing",       0, NULL, OPT_CG_TIMING },
   { "cleanup",         0, NULL, OPT_CLEANUP },
+  { "dir",             1, NULL, 'd' },
+  { "env",             1, NULL, 'E' },
+  { "extra-time",      1, NULL, 'x' },
+  { "full-env",                0, NULL, 'e' },
+  { "init",            0, NULL, OPT_INIT },
+  { "mem",             1, NULL, 'm' },
+  { "meta",            1, NULL, 'M' },
+  { "processes",       2, NULL, 'p' },
+  { "run",             0, NULL, OPT_RUN },
+  { "stack",           1, NULL, 'k' },
+  { "stderr",          1, NULL, 'r' },
+  { "stdin",           1, NULL, 'i' },
+  { "stdout",          1, NULL, 'o' },
+  { "time",            1, NULL, 't' },
+  { "verbose",         0, NULL, 'v' },
+  { "version",         0, NULL, OPT_VERSION },
+  { "wall-time",       1, NULL, 'w' },
   { NULL,              0, NULL, 0 }
 };
 
@@ -762,11 +1197,19 @@ main(int argc, char **argv)
   int c;
   enum opt_code mode = 0;
 
+  init_dir_rules();
+
   while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) >= 0)
     switch (c)
       {
       case 'c':
-       set_cwd = optarg;
+       if (optarg)
+         cg_root = optarg;
+       cg_enable = 1;
+       break;
+      case 'd':
+       if (!set_dir_action(optarg))
+         usage();
        break;
       case 'e':
        pass_environ = 1;
@@ -776,13 +1219,13 @@ main(int argc, char **argv)
          usage();
        break;
       case 'k':
-       stack_limit = atol(optarg);
+       stack_limit = atoi(optarg);
        break;
       case 'i':
        redir_stdin = optarg;
        break;
       case 'm':
-       memory_limit = atol(optarg);
+       memory_limit = atoi(optarg);
        break;
       case 'M':
        meta_open(optarg);
@@ -790,6 +1233,12 @@ main(int argc, char **argv)
       case 'o':
        redir_stdout = optarg;
        break;
+      case 'p':
+       if (optarg)
+         max_processes = atoi(optarg);
+       else
+         max_processes = 0;
+       break;
       case 'r':
        redir_stderr = optarg;
        break;
@@ -805,26 +1254,46 @@ main(int argc, char **argv)
       case 'x':
        extra_timeout = 1000*atof(optarg);
        break;
-      case OPT_PREPARE:
+      case OPT_INIT:
       case OPT_RUN:
       case OPT_CLEANUP:
+      case OPT_VERSION:
        mode = c;
        break;
+      case OPT_CG_MEM:
+       cg_memory_limit = atoi(optarg);
+       break;
+      case OPT_CG_TIMING:
+       cg_timing = 1;
+       break;
       default:
        usage();
       }
 
+  if (!mode)
+    usage();
+  if (mode == OPT_VERSION)
+    {
+      show_version();
+      return 0;
+    }
+
   if (geteuid())
     die("Must be started as root");
+  orig_uid = getuid();
+  orig_gid = getgid();
 
-  // FIXME: Copying of files into the box
+  umask(022);
+  if (chdir(BOX_DIR) < 0)
+    die("chdir(%s): %m", BOX_DIR);
+  cg_init();
 
   switch (mode)
     {
-    case OPT_PREPARE:
+    case OPT_INIT:
       if (optind < argc)
        usage();
-      prepare();
+      init();
       break;
     case OPT_RUN:
       if (optind >= argc)