+/*** 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);
+ }
+ }
+}
+