+/*** Mount rules ***/
+
+struct dir_rule {
+ char *inside; // A relative path
+ char *outside; // This can be:
+ // - an absolute path
+ // - a relative path starting with "./"
+ // - one of the above prefixed with "?" to mean "only if exists"
+ // - "procfs"
+ struct dir_rule *next;
+};
+
+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)
+{
+ // Make sure that "in" is relative
+ while (in[0] == '/')
+ in++;
+ if (!*in)
+ return 0;
+
+ // Check "out"
+ if (out)
+ {
+ char *o = out;
+ if (*o == '?')
+ o++;
+ if (!(o[0] == '/' ||
+ !strncmp(o, "./", 2) ||
+ !strcmp(o, "procfs")))
+ return 0;
+ }
+
+ // Override an existing rule
+ for (struct dir_rule *r = first_dir_rule; r; r=r->next)
+ if (!strcmp(r->inside, in))
+ {
+ r->outside = out;
+ return 1;
+ }
+
+ // Add a new rule
+ struct dir_rule *r = xmalloc(sizeof(*r));
+ r->inside = in;
+ r->outside = out;
+ *last_dir_rule = r;
+ last_dir_rule = &r->next;
+ r->next = NULL;
+
+ return 1;
+}
+
+static int set_dir_action(char *arg)
+{
+ arg = xstrdup(arg);
+ char *sep = strchr(arg, '=');
+
+ if (sep)
+ {
+ *sep++ = 0;
+ return add_dir_rule(arg, (*sep ? sep : NULL));
+ }
+ else
+ {
+ char *out = xmalloc(1 + strlen(arg) + 1);
+ sprintf(out, "/%s", arg);
+ return add_dir_rule(arg, out);
+ }
+}
+
+static void init_dir_rules(void)
+{
+ set_dir_action("box=./box");
+ set_dir_action("bin");
+ set_dir_action("dev");
+ set_dir_action("lib");
+ set_dir_action("lib64=?/lib64");
+ set_dir_action("proc=procfs");
+ 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))
+ {
+ if (sep)
+ *sep = '/';
+ return;
+ }
+
+ if (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 (out[0] == '?')
+ {
+ out++;
+ if (!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);
+
+ if (!strcmp(out, "procfs"))
+ {
+ msg("Mounting procfs on %s\n", in);
+ if (mount("none", root_in, "proc", 0, "") < 0)
+ die("Cannot mount proc on %s: %m", in);
+ }
+ else
+ {
+ msg("Binding %s on %s\n", out, in);
+ if (mount(out, root_in, "none", MS_BIND | MS_NOSUID | MS_NODEV, "") < 0)
+ die("Cannot bind %s on %s: %m", out, in);
+ }
+ }
+}
+