+/*** 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 ***/