+/*** Directory 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,
+ DIR_FLAG_DEV = 16,
+};
+
+static const char * const dir_flag_names[] = { "rw", "noexec", "fs", "maybe", "dev" };
+
+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)
+ {
+ 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)
+{
+ for (unsigned int i = 0; i < ARRAY_SIZE(dir_flag_names); i++)
+ if (!strcmp(opt, dir_flag_names[i]))
+ return 1U << i;
+ 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)
+ {
+ *colon++ = 0;
+ char *next = strchr(colon, ':');
+ if (next)
+ *next = 0;
+ flags |= parse_dir_option(colon);
+ 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: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[0] == '/' ? path+1 : 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_DEV))
+ mount_flags |= MS_NODEV;
+
+ if (r->flags & DIR_FLAG_FS)
+ {
+ msg("Mounting %s on %s (flags %lx)\n", out, in, mount_flags);
+ if (mount("none", root_in, out, mount_flags, "") < 0)
+ die("Cannot mount %s on %s: %m", out, in);
+ }
+ else
+ {
+ mount_flags |= MS_BIND | MS_NOSUID;
+ msg("Binding %s on %s (flags %lx)\n", out, in, mount_flags);
+ // Most mount flags need remount to work
+ if (mount(out, root_in, "none", mount_flags, "") < 0 ||
+ mount(out, root_in, "none", MS_REMOUNT | mount_flags, "") < 0)
+ die("Cannot mount %s on %s: %m", out, in);
+ }
+ }
+}
+