jail: add some remaining OCI features
[project/procd.git] / jail / jail.c
index df990f1bac94a8902a3574e51073f937029b2b9c..ee909f3483ef74c3875eb5d1d3484e40d317d0f5 100644 (file)
@@ -20,6 +20,7 @@
 #include <sys/time.h>
 #include <sys/resource.h>
 #include <sys/stat.h>
+#include <sys/sysmacros.h>
 
 /* musl only defined 15 limit types, make sure all 16 are supported */
 #ifndef RLIMIT_RTTIME
@@ -38,8 +39,9 @@
 #include <string.h>
 #include <fcntl.h>
 #include <sched.h>
-#include <linux/limits.h>
 #include <linux/filter.h>
+#include <linux/limits.h>
+#include <linux/nsfs.h>
 #include <signal.h>
 #include <inttypes.h>
 
@@ -63,7 +65,9 @@
 #endif
 
 #define STACK_SIZE     (1024 * 1024)
-#define OPT_ARGS       "S:C:n:h:r:w:d:psulocU:G:NR:fFO:T:EyJ:"
+#define OPT_ARGS       "S:C:n:h:r:w:d:psulocU:G:NR:fFO:T:EyJ:i"
+
+#define OCI_VERSION_STRING "1.0.2"
 
 struct hook_execvpe {
        char *file;
@@ -77,6 +81,14 @@ struct sysctl_val {
        char *value;
 };
 
+struct mknod_args {
+       char *path;
+       mode_t mode;
+       dev_t dev;
+       uid_t uid;
+       gid_t gid;
+};
+
 static struct {
        char *name;
        char *hostname;
@@ -97,6 +109,18 @@ static struct {
        struct sysctl_val **sysctl;
        int no_new_privs;
        int namespace;
+       struct {
+               int pid;
+               int net;
+               int ns;
+               int ipc;
+               int uts;
+               int user;
+               int cgroup;
+#ifdef CLONE_NEWTIME
+               int time;
+#endif
+       } setns;
        int procfs;
        int ronly;
        int sysfs;
@@ -117,8 +141,42 @@ static struct {
                struct hook_execvpe **poststop;
        } hooks;
        struct rlimit *rlimits[RLIM_NLIMITS];
+       int oom_score_adj;
+       bool set_oom_score_adj;
+       struct mknod_args **devices;
+       char *ocibundle;
+       bool immediately;
+       struct blob_attr *annotations;
 } opts;
 
+static struct blob_buf ocibuf;
+
+extern int pivot_root(const char *new_root, const char *put_old);
+
+int debug = 0;
+
+static char child_stack[STACK_SIZE];
+
+static struct ubus_context *parent_ctx;
+
+int console_fd;
+
+
+static inline bool has_namespaces(void)
+{
+return ((opts.setns.pid != -1) ||
+       (opts.setns.net != -1) ||
+       (opts.setns.ns != -1) ||
+       (opts.setns.ipc != -1) ||
+       (opts.setns.uts != -1) ||
+       (opts.setns.user != -1) ||
+       (opts.setns.cgroup != -1) ||
+#ifdef CLONE_NEWTIME
+       (opts.setns.time != -1) ||
+#endif
+       opts.namespace);
+}
+
 static void free_hooklist(struct hook_execvpe **hooklist)
 {
        struct hook_execvpe *cur;
@@ -158,6 +216,21 @@ static void free_sysctl(void) {
        free(opts.sysctl);
 }
 
+static void free_devices(void) {
+       struct mknod_args **cur;
+
+       if (!opts.devices)
+               return;
+
+       cur = opts.devices;
+
+       while (*cur) {
+               free((*cur)->path);
+               free(*(cur++));
+       }
+       free(opts.devices);
+}
+
 static void free_rlimits(void) {
        int type;
 
@@ -190,28 +263,19 @@ static void free_opts(bool child) {
 
        free_rlimits();
        free_sysctl();
+       free_devices();
        free(opts.hostname);
        free(opts.cwd);
        free(opts.extroot);
        free(opts.uidmap);
        free(opts.gidmap);
+       free(opts.annotations);
        free_hooklist(opts.hooks.createRuntime);
        free_hooklist(opts.hooks.createContainer);
        free_hooklist(opts.hooks.startContainer);
        free_hooklist(opts.hooks.poststart);
        free_hooklist(opts.hooks.poststop);
 }
-
-static struct blob_buf ocibuf;
-
-extern int pivot_root(const char *new_root, const char *put_old);
-
-int debug = 0;
-
-static char child_stack[STACK_SIZE];
-
-int console_fd;
-
 static int mount_overlay(char *jail_root, char *overlaydir) {
        char *upperdir, *workdir, *optsstr, *upperetc, *upperresolvconf;
        const char mountoptsformat[] = "lowerdir=%s,upperdir=%s,workdir=%s";
@@ -277,24 +341,24 @@ out:
 
 static void pass_console(int console_fd)
 {
-       struct ubus_context *ctx = ubus_connect(NULL);
+       struct ubus_context *child_ctx = ubus_connect(NULL);
        static struct blob_buf req;
        uint32_t id;
 
-       if (!ctx)
+       if (!child_ctx)
                return;
 
        blob_buf_init(&req, 0);
        blobmsg_add_string(&req, "name", opts.name);
 
-       if (ubus_lookup_id(ctx, "container", &id) ||
-           ubus_invoke_fd(ctx, id, "console_set", req.head, NULL, NULL, 3000, console_fd))
+       if (ubus_lookup_id(child_ctx, "container", &id) ||
+           ubus_invoke_fd(child_ctx, id, "console_set", req.head, NULL, NULL, 3000, console_fd))
                INFO("ubus request failed\n");
        else
                close(console_fd);
 
        blob_buf_free(&req);
-       ubus_free(ctx);
+       ubus_free(child_ctx);
 }
 
 static int create_dev_console(const char *jail_root)
@@ -344,24 +408,34 @@ no_console:
 
 static int hook_running = 0;
 static int hook_return_code = 0;
+static struct hook_execvpe **current_hook = NULL;
+typedef void (*hook_return_handler)(void);
+static hook_return_handler hook_return_cb = NULL;
 
 static void hook_process_timeout_cb(struct uloop_timeout *t);
 static struct uloop_timeout hook_process_timeout = {
        .cb = hook_process_timeout_cb,
 };
 
+static void run_hooklist(void);
 static void hook_process_handler(struct uloop_process *c, int ret)
 {
        uloop_timeout_cancel(&hook_process_timeout);
+
        if (WIFEXITED(ret)) {
                hook_return_code = WEXITSTATUS(ret);
-               DEBUG("hook (%d) exited with exit: %d\n", c->pid, hook_return_code);
+               if (hook_return_code)
+                       ERROR("hook (%d) exited with exit: %d\n", c->pid, hook_return_code);
+               else
+                       DEBUG("hook (%d) exited with exit: %d\n", c->pid, hook_return_code);
+
        } else {
                hook_return_code = WTERMSIG(ret);
-               DEBUG("hook (%d) exited with signal: %d\n", c->pid, hook_return_code);
+               ERROR("hook (%d) exited with signal: %d\n", c->pid, hook_return_code);
        }
        hook_running = 0;
-       uloop_end();
+       ++current_hook;
+       run_hooklist();
 }
 
 static struct uloop_process hook_process = {
@@ -374,77 +448,63 @@ static void hook_process_timeout_cb(struct uloop_timeout *t)
        kill(hook_process.pid, SIGKILL);
 }
 
-static int run_hook(struct hook_execvpe *hook)
+static void run_hooklist(void)
 {
+       struct hook_execvpe *hook = *current_hook;
        struct stat s;
 
+       if (!hook)
+               hook_return_cb();
+
        DEBUG("executing hook %s\n", hook->file);
 
        if (stat(hook->file, &s))
-               return ENOENT;
+               hook_process_handler(&hook_process, ENOENT);
 
        if (!((unsigned long)s.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
-               return EPERM;
+               hook_process_handler(&hook_process, EPERM);
 
        if (!((unsigned long)s.st_mode & (S_IRUSR | S_IRGRP | S_IROTH)))
-               return EPERM;
-
-       uloop_init();
+               hook_process_handler(&hook_process, EPERM);
 
        hook_running = 1;
        hook_process.pid = fork();
-       if (hook_process.pid > 0) {
-               /* parent */
-               uloop_process_add(&hook_process);
-
-               if (hook->timeout > 0)
-                       uloop_timeout_set(&hook_process_timeout, 1000 * hook->timeout);
-
-               uloop_run();
-               if (hook_running) {
-                       DEBUG("uloop interrupted, killing hook process\n");
-                       kill(hook_process.pid, SIGTERM);
-                       uloop_timeout_set(&hook_process_timeout, 1000);
-                       uloop_run();
-               }
-               uloop_done();
-
-               waitpid(hook_process.pid, NULL, WCONTINUED);
-
-               return hook_return_code;
-       } else if (hook_process.pid == 0) {
+       if (hook_process.pid == 0) {
                /* child */
-               execvpe(hook->file, hook->argv, hook->envp);
-               hook_running = 0;
+               execve(hook->file, hook->argv, hook->envp);
+               ERROR("execve error %m\n");
                _exit(errno);
-       } else {
+       } else if (hook_process.pid < 0) {
                /* fork error */
+               ERROR("hook fork error\n");
                hook_running = 0;
-               return errno;
+               hook_process_handler(&hook_process, errno);
        }
-}
 
-static int run_hooks(struct hook_execvpe **hooklist)
-{
-       struct hook_execvpe **cur;
-       int res;
+       /* parent */
+       uloop_process_add(&hook_process);
 
-       if (!hooklist)
-               return 0; /* Nothing to do */
+       if (hook->timeout > 0)
+               uloop_timeout_set(&hook_process_timeout, 1000 * hook->timeout);
 
-       cur = hooklist;
+       uloop_run();
+       if (hook_running) {
+               DEBUG("uloop interrupted, killing jail process\n");
+               kill(hook_process.pid, SIGTERM);
+               uloop_timeout_set(&hook_process_timeout, 1000);
+               uloop_run();
+       }
+}
 
-       while (*cur) {
-               res = run_hook(*cur);
-               if (res)
-                       DEBUG(" error running hook %s\n", (*cur)->file);
-               else
-                       DEBUG(" success running hook %s\n", (*cur)->file);
+static void run_hooks(struct hook_execvpe **hooklist, hook_return_handler return_cb)
+{
+       if (!hooklist)
+               return_cb();
 
-               ++cur;
-       }
+       current_hook = hooklist;
+       hook_return_cb = return_cb;
 
-       return 0;
+       run_hooklist();
 }
 
 static int apply_sysctl(const char *jail_root)
@@ -491,14 +551,83 @@ static int apply_sysctl(const char *jail_root)
        return 0;
 }
 
+/* glibc defines makedev calling a function. make sure it's a pure macro */
+#if defined(__GLIBC__)
+#undef makedev
+/* from musl's sys/sysmacros.h */
+#define makedev(x,y) ( \
+       (((x)&0xfffff000ULL) << 32) | \
+       (((x)&0x00000fffULL) << 8) | \
+       (((y)&0xffffff00ULL) << 12) | \
+       (((y)&0x000000ffULL)) )
+#endif
+
+static struct mknod_args default_devices[] = {
+       { .path = "/dev/null", .mode = (S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH), .dev = makedev(1, 3) },
+       { .path = "/dev/zero", .mode = (S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH), .dev = makedev(1, 5) },
+       { .path = "/dev/full", .mode = (S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH), .dev = makedev(1, 7) },
+       { .path = "/dev/random", .mode = (S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH), .dev = makedev(1, 8) },
+       { .path = "/dev/urandom", .mode = (S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH), .dev = makedev(1, 9) },
+       { .path = "/dev/tty", .mode = (S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP), .dev = makedev(5, 0), .gid = 5 },
+       { 0 },
+};
+
+static int create_devices(void)
+{
+       struct mknod_args **cur, *curdef;
+
+       if (!opts.devices)
+               goto only_default_devices;
+
+       cur = opts.devices;
+
+       while (*cur) {
+               DEBUG("creating %s (mode=%08o)\n", (*cur)->path, (*cur)->mode);
+               if (mknod((*cur)->path, (*cur)->mode, (*cur)->dev))
+                       return errno;
+
+               if (((*cur)->uid || (*cur)->gid) &&
+                   chown((*cur)->path, (*cur)->uid, (*cur)->gid))
+                       return errno;
+
+               ++cur;
+       }
+
+only_default_devices:
+       curdef = default_devices;
+       while(curdef->path) {
+               DEBUG("creating %s (mode=%08o)\n", curdef->path, curdef->mode);
+               if (mknod(curdef->path, curdef->mode, curdef->dev)) {
+                       ++curdef;
+                       continue; /* may already exist, eg. due to a bind-mount */
+               }
+               if ((curdef->uid || curdef->gid) &&
+                   chown(curdef->path, curdef->uid, curdef->gid))
+                       return errno;
+
+               ++curdef;
+       }
+
+       /* Dev symbolic links as defined in OCI spec */
+       symlink("/dev/pts/ptmx", "/dev/ptmx");
+       symlink("/proc/self/fd", "/dev/fd");
+       symlink("/proc/self/fd/0", "/dev/stdin");
+       symlink("/proc/self/fd/1", "/dev/stdout");
+       symlink("/proc/self/fd/2", "/dev/stderr");
+
+       return 0;
+}
+
+static char jail_root[] = "/tmp/ujail-XXXXXX";
+static char tmpovdir[] = "/tmp/ujail-overlay-XXXXXX";
+static mode_t old_umask;
+static void enter_jail_fs(void);
 static int build_jail_fs(void)
 {
-       char jail_root[] = "/tmp/ujail-XXXXXX";
-       char tmpovdir[] = "/tmp/ujail-overlay-XXXXXX";
-       char tmpdevdir[] = "/tmp/ujail-XXXXXX/dev";
-       char tmpdevptsdir[] = "/tmp/ujail-XXXXXX/dev/pts";
        char *overlaydir = NULL;
 
+       old_umask = umask(0);
+
        if (mkdtemp(jail_root) == NULL) {
                ERROR("mkdtemp(%s) failed: %m\n", jail_root);
                return -1;
@@ -555,24 +684,14 @@ static int build_jail_fs(void)
                return -1;
        }
 
-       snprintf(tmpdevdir, sizeof(tmpdevdir), "%s/dev", jail_root);
-       mkdir_p(tmpdevdir, 0755);
-       if (mount(NULL, tmpdevdir, "tmpfs", MS_NOATIME | MS_NOEXEC | MS_NOSUID, "size=1M"))
-               return -1;
-
-       snprintf(tmpdevptsdir, sizeof(tmpdevptsdir), "%s/dev/pts", jail_root);
-       mkdir_p(tmpdevptsdir, 0755);
-       if (mount(NULL, tmpdevptsdir, "devpts", MS_NOATIME | MS_NOEXEC | MS_NOSUID, NULL))
-               return -1;
-
-       if (opts.console)
-               create_dev_console(jail_root);
-
        if (mount_all(jail_root)) {
                ERROR("mount_all() failed\n");
                return -1;
        }
 
+       if (opts.console)
+               create_dev_console(jail_root);
+
        /* make sure /etc/resolv.conf exists if in new network namespace */
        if (opts.namespace & CLONE_NEWNET) {
                char jailetc[PATH_MAX], jaillink[PATH_MAX];
@@ -583,22 +702,29 @@ static int build_jail_fs(void)
                if (overlaydir)
                        unlink(jaillink);
 
-               symlink("../tmp/resolv.conf.d/resolv.conf.auto", jaillink);
+               symlink("../dev/resolv.conf.d/resolv.conf.auto", jaillink);
        }
 
-       run_hooks(opts.hooks.createContainer);
+       run_hooks(opts.hooks.createContainer, enter_jail_fs);
 
+       return 0;
+}
+
+static void post_jail_fs(void);
+static void enter_jail_fs(void)
+{
        char dirbuf[sizeof(jail_root) + 4];
+
        snprintf(dirbuf, sizeof(dirbuf), "%s/old", jail_root);
        mkdir(dirbuf, 0755);
 
        if (pivot_root(jail_root, dirbuf) == -1) {
                ERROR("pivot_root(%s, %s) failed: %m\n", jail_root, dirbuf);
-               return -1;
+               exit(-1);
        }
        if (chdir("/")) {
                ERROR("chdir(/) (after pivot_root) failed: %m\n");
-               return -1;
+               exit(-1);
        }
 
        snprintf(dirbuf, sizeof(dirbuf), "/old%s", jail_root);
@@ -614,31 +740,15 @@ static int build_jail_fs(void)
        umount2("/old", MNT_DETACH);
        rmdir("/old");
 
-       if (opts.procfs) {
-               mkdir("/proc", 0755);
-               mount("proc", "/proc", "proc", MS_NOATIME | MS_NODEV | MS_NOEXEC | MS_NOSUID, 0);
-               /*
-                * make /proc/sys read-only while keeping read-write to
-                * /proc/sys/net if CLONE_NEWNET is set.
-                */
-               if (opts.namespace & CLONE_NEWNET)
-                       mount("/proc/sys/net", "/proc/self/net", NULL, MS_BIND, 0);
-
-               mount("/proc/sys", "/proc/sys", NULL, MS_BIND, 0);
-               mount(NULL, "/proc/sys", NULL, MS_REMOUNT | MS_RDONLY, 0);
-               mount(NULL, "/proc", NULL, MS_REMOUNT, 0);
-
-               if (opts.namespace & CLONE_NEWNET)
-                       mount("/proc/self/net", "/proc/sys/net", NULL, MS_MOVE, 0);
-       }
-       if (opts.sysfs) {
-               mkdir("/sys", 0755);
-               mount("sysfs", "/sys", "sysfs", MS_NOATIME | MS_NODEV | MS_NOEXEC | MS_NOSUID | MS_RDONLY, 0);
+       if (create_devices()) {
+               ERROR("create_devices() failed\n");
+               exit(-1);
        }
        if (opts.ronly)
                mount(NULL, "/", NULL, MS_REMOUNT | MS_BIND | MS_RDONLY, 0);
 
-       return 0;
+       umask(old_umask);
+       post_jail_fs();
 }
 
 static int write_uid_gid_map(pid_t child_pid, bool gidmap, char *mapstr)
@@ -839,7 +949,8 @@ static void usage(void)
        fprintf(stderr, "  -T <size>\tuse tmpfs r/w overlayfs with <size>\n");
        fprintf(stderr, "  -E\t\tfail if jail cannot be setup\n");
        fprintf(stderr, "  -y\t\tprovide jail console\n");
-       fprintf(stderr, "  -J <dir>\tstart OCI bundle\n");
+       fprintf(stderr, "  -J <dir>\tcreate container from OCI bundle\n");
+       fprintf(stderr, "  -j\t\tstart container immediately\n");
        fprintf(stderr, "\nWarning: by default root inside the jail is the same\n\
 and he has the same powers as root outside the jail,\n\
 thus he can escape the jail and/or break stuff.\n\
@@ -849,33 +960,155 @@ ujail will not use namespace/build a jail,\n\
 and will only drop capabilities/apply seccomp filter.\n\n");
 }
 
-static int exec_jail(void *pipes_ptr)
+static int* get_namespace_fd(const unsigned int nstype)
+{
+       switch (nstype) {
+               case CLONE_NEWPID:
+                       return &opts.setns.pid;
+               case CLONE_NEWNET:
+                       return &opts.setns.net;
+               case CLONE_NEWNS:
+                       return &opts.setns.ns;
+               case CLONE_NEWIPC:
+                       return &opts.setns.ipc;
+               case CLONE_NEWUTS:
+                       return &opts.setns.uts;
+               case CLONE_NEWUSER:
+                       return &opts.setns.user;
+               case CLONE_NEWCGROUP:
+                       return &opts.setns.cgroup;
+#ifdef CLONE_NEWTIME
+               case CLONE_NEWTIME:
+                       return &opts.setns.time;
+#endif
+               default:
+                       return NULL;
+       }
+}
+
+static int setns_open(unsigned long nstype)
+{
+       int *fd = get_namespace_fd(nstype);
+
+       if (!*fd)
+               return EFAULT;
+
+       if (*fd == -1)
+               return 0;
+
+       if (setns(*fd, nstype) == -1) {
+               close(*fd);
+               return errno;
+       }
+
+       close(*fd);
+       return 0;
+}
+
+static int jail_running = 0;
+static int jail_return_code = 0;
+
+static void jail_process_timeout_cb(struct uloop_timeout *t);
+static struct uloop_timeout jail_process_timeout = {
+       .cb = jail_process_timeout_cb,
+};
+static void poststop(void);
+static void jail_process_handler(struct uloop_process *c, int ret)
+{
+       uloop_timeout_cancel(&jail_process_timeout);
+       if (WIFEXITED(ret)) {
+               jail_return_code = WEXITSTATUS(ret);
+               INFO("jail (%d) exited with exit: %d\n", c->pid, jail_return_code);
+       } else {
+               jail_return_code = WTERMSIG(ret);
+               INFO("jail (%d) exited with signal: %d\n", c->pid, jail_return_code);
+       }
+       jail_running = 0;
+       poststop();
+}
+
+static struct uloop_process jail_process = {
+       .cb = jail_process_handler,
+};
+
+static void jail_process_timeout_cb(struct uloop_timeout *t)
+{
+       DEBUG("jail process failed to stop, sending SIGKILL\n");
+       kill(jail_process.pid, SIGKILL);
+}
+
+static void jail_handle_signal(int signo)
+{
+       if (hook_running) {
+               DEBUG("forwarding signal %d to the hook process\n", signo);
+               kill(hook_process.pid, signo);
+       }
+
+       if (jail_running) {
+               DEBUG("forwarding signal %d to the jailed process\n", signo);
+               kill(jail_process.pid, signo);
+       }
+}
+
+static void signals_init(void)
+{
+       int i;
+       sigset_t sigmask;
+
+       sigfillset(&sigmask);
+       for (i = 0; i < _NSIG; i++) {
+               struct sigaction s = { 0 };
+
+               if (!sigismember(&sigmask, i))
+                       continue;
+               if ((i == SIGCHLD) || (i == SIGPIPE) || (i == SIGSEGV))
+                       continue;
+
+               s.sa_handler = jail_handle_signal;
+               sigaction(i, &s, NULL);
+       }
+}
+
+static void pre_exec_jail(struct uloop_timeout *t);
+static struct uloop_timeout pre_exec_timeout = {
+       .cb = pre_exec_jail,
+};
+
+int pipes[4];
+static int exec_jail(void *arg)
 {
-       int *pipes = (int*)pipes_ptr;
        char buf[1];
-       int pw_uid, pw_gid, gr_gid;
+
+       uloop_init();
+       signals_init();
 
        close(pipes[0]);
        close(pipes[3]);
 
+       setns_open(CLONE_NEWUSER);
+       setns_open(CLONE_NEWNET);
+       setns_open(CLONE_NEWNS);
+       setns_open(CLONE_NEWIPC);
+       setns_open(CLONE_NEWUTS);
+#ifdef CLONE_NEWTIME
+       setns_open(CLONE_NEWTIME);
+#endif
+
        buf[0] = 'i';
        if (write(pipes[1], buf, 1) < 1) {
                ERROR("can't write to parent\n");
-               exit(EXIT_FAILURE);
+               return EXIT_FAILURE;
        }
+       close(pipes[1]);
        if (read(pipes[2], buf, 1) < 1) {
                ERROR("can't read from parent\n");
-               exit(EXIT_FAILURE);
+               return EXIT_FAILURE;
        }
        if (buf[0] != 'O') {
                ERROR("parent had an error, child exiting\n");
-               exit(EXIT_FAILURE);
+               return EXIT_FAILURE;
        }
-
-       close(pipes[1]);
-       close(pipes[2]);
-
-       if (opts.namespace & CLONE_NEWUSER) {
+       if ((opts.namespace & CLONE_NEWUSER) || (opts.setns.user != -1)) {
                if (setregid(0, 0) < 0) {
                        ERROR("setgid\n");
                        exit(EXIT_FAILURE);
@@ -896,13 +1129,44 @@ static int exec_jail(void *pipes_ptr)
                exit(EXIT_FAILURE);
        }
 
+       uloop_timeout_add(&pre_exec_timeout);
+       uloop_run();
+
+       exit(-1);
+}
+
+static void pre_exec_jail(struct uloop_timeout *t)
+{
        if ((opts.namespace & CLONE_NEWNS) && build_jail_fs()) {
                ERROR("failed to build jail fs\n");
                exit(EXIT_FAILURE);
+       } else {
+               run_hooks(opts.hooks.createContainer, post_jail_fs);
+       }
+}
+
+static void post_start_hook(void);
+static void post_jail_fs(void)
+{
+       char buf[1];
+
+       if (read(pipes[2], buf, 1) < 1) {
+               ERROR("can't read from parent\n");
+               exit(EXIT_FAILURE);
+       }
+       if (buf[0] != '!') {
+               ERROR("parent had an error, child exiting\n");
+               exit(EXIT_FAILURE);
        }
-       run_hooks(opts.hooks.startContainer);
+       close(pipes[2]);
+
+       run_hooks(opts.hooks.startContainer, post_start_hook);
+}
 
-       if (!(opts.namespace & CLONE_NEWUSER)) {
+static void post_start_hook(void)
+{
+       if (!(opts.namespace & CLONE_NEWUSER) && (opts.setns.user == -1)) {
+               int pw_uid, pw_gid, gr_gid;
                get_jail_user(&pw_uid, &pw_gid, &gr_gid);
 
                set_jail_user(opts.pw_uid?:pw_uid, opts.pw_gid?:pw_gid, opts.gr_gid?:gr_gid);
@@ -951,67 +1215,30 @@ static int exec_jail(void *pipes_ptr)
        exit(EXIT_FAILURE);
 }
 
-static int jail_running = 0;
-static int jail_return_code = 0;
-
-static void jail_process_timeout_cb(struct uloop_timeout *t);
-static struct uloop_timeout jail_process_timeout = {
-       .cb = jail_process_timeout_cb,
-};
-
-static void jail_process_handler(struct uloop_process *c, int ret)
-{
-       uloop_timeout_cancel(&jail_process_timeout);
-       if (WIFEXITED(ret)) {
-               jail_return_code = WEXITSTATUS(ret);
-               INFO("jail (%d) exited with exit: %d\n", c->pid, jail_return_code);
-       } else {
-               jail_return_code = WTERMSIG(ret);
-               INFO("jail (%d) exited with signal: %d\n", c->pid, jail_return_code);
-       }
-       jail_running = 0;
-       uloop_end();
-}
-
-static struct uloop_process jail_process = {
-       .cb = jail_process_handler,
-};
-
-static void jail_process_timeout_cb(struct uloop_timeout *t)
+static int netns_open_pid(const pid_t target_ns)
 {
-       DEBUG("jail process failed to stop, sending SIGKILL\n");
-       kill(jail_process.pid, SIGKILL);
-}
+       char pid_net_path[PATH_MAX];
 
-static void jail_handle_signal(int signo)
-{
-       if (hook_running) {
-               DEBUG("forwarding signal %d to the hook process\n", signo);
-               kill(hook_process.pid, signo);
-       }
+       snprintf(pid_net_path, sizeof(pid_net_path), "/proc/%u/ns/net", target_ns);
 
-       if (jail_running) {
-               DEBUG("forwarding signal %d to the jailed process\n", signo);
-               kill(jail_process.pid, signo);
-       }
+       return open(pid_net_path, O_RDONLY);
 }
 
-static int netns_open_pid(const pid_t target_ns)
+static int pidns_open_pid(const pid_t target_ns)
 {
-       char pid_net_path[PATH_MAX];
+       char pid_pid_path[PATH_MAX];
 
-       snprintf(pid_net_path, sizeof(pid_net_path), "/proc/%u/ns/net", target_ns);
+       snprintf(pid_pid_path, sizeof(pid_pid_path), "/proc/%u/ns/pid", target_ns);
 
-       return open(pid_net_path, O_RDONLY);
+       return open(pid_pid_path, O_RDONLY);
 }
 
 static void netns_updown(pid_t pid, bool start)
 {
-       struct ubus_context *ctx = ubus_connect(NULL);
        static struct blob_buf req;
        uint32_t id;
 
-       if (!ctx)
+       if (!parent_ctx)
                return;
 
        blob_buf_init(&req, 0);
@@ -1019,12 +1246,11 @@ static void netns_updown(pid_t pid, bool start)
        blobmsg_add_u32(&req, "pid", pid);
        blobmsg_add_u8(&req, "start", start);
 
-       if (ubus_lookup_id(ctx, "network", &id) ||
-           ubus_invoke(ctx, id, "netns_updown", req.head, NULL, NULL, 3000))
+       if (ubus_lookup_id(parent_ctx, "network", &id) ||
+           ubus_invoke(parent_ctx, id, "netns_updown", req.head, NULL, NULL, 3000))
                INFO("ubus request failed\n");
 
        blob_buf_free(&req);
-       ubus_free(ctx);
 }
 
 static int parseOCIenvarray(struct blob_attr *msg, char ***envp)
@@ -1136,7 +1362,7 @@ static int parseOCIhook(struct hook_execvpe ***hooklist, struct blob_attr *msg)
                        goto errout;
                }
 
-               (*hooklist)[idx] = malloc(sizeof(struct hook_execvpe));
+               (*hooklist)[idx] = calloc(1, sizeof(struct hook_execvpe));
                if (tb[OCI_HOOK_ARGS]) {
                        ret = parseOCIenvarray(tb[OCI_HOOK_ARGS], &((*hooklist)[idx]->argv));
                        if (ret)
@@ -1306,7 +1532,7 @@ static int parseOCIprocessuser(struct blob_attr *msg) {
                        }
                        opts.num_additional_gids = gidcnt;
                }
-               DEBUG("read %lu additional groups\n", gidcnt);
+               DEBUG("read %zu additional groups\n", gidcnt);
        }
 
        if (tb[OCI_PROCESS_USER_UMASK]) {
@@ -1416,6 +1642,7 @@ enum {
        OCI_PROCESS_CAPABILITIES,
        OCI_PROCESS_CWD,
        OCI_PROCESS_ENV,
+       OCI_PROCESS_OOMSCOREADJ,
        OCI_PROCESS_NONEWPRIVILEGES,
        OCI_PROCESS_RLIMITS,
        OCI_PROCESS_TERMINAL,
@@ -1428,6 +1655,7 @@ static const struct blobmsg_policy oci_process_policy[] = {
        [OCI_PROCESS_CAPABILITIES] = { "capabilities", BLOBMSG_TYPE_TABLE },
        [OCI_PROCESS_CWD] = { "cwd", BLOBMSG_TYPE_STRING },
        [OCI_PROCESS_ENV] = { "env", BLOBMSG_TYPE_ARRAY },
+       [OCI_PROCESS_OOMSCOREADJ] = { "oomScoreAdj", BLOBMSG_TYPE_INT32 },
        [OCI_PROCESS_NONEWPRIVILEGES] = { "noNewPrivileges", BLOBMSG_TYPE_BOOL },
        [OCI_PROCESS_RLIMITS] = { "rlimits", BLOBMSG_TYPE_ARRAY },
        [OCI_PROCESS_TERMINAL] = { "terminal", BLOBMSG_TYPE_BOOL },
@@ -1472,6 +1700,11 @@ static int parseOCIprocess(struct blob_attr *msg)
            (res = parseOCIrlimits(tb[OCI_PROCESS_RLIMITS])))
                return res;
 
+       if (tb[OCI_PROCESS_OOMSCOREADJ]) {
+               opts.oom_score_adj = blobmsg_get_u32(tb[OCI_PROCESS_OOMSCOREADJ]);
+               opts.set_oom_score_adj = true;
+       }
+
        return 0;
 }
 
@@ -1486,7 +1719,7 @@ static const struct blobmsg_policy oci_linux_namespace_policy[] = {
        [OCI_LINUX_NAMESPACE_PATH] = { "path", BLOBMSG_TYPE_STRING },
 };
 
-static unsigned int resolve_nstype(char *type) {
+static int resolve_nstype(char *type) {
        if (!strcmp("pid", type))
                return CLONE_NEWPID;
        else if (!strcmp("network", type))
@@ -1501,6 +1734,10 @@ static unsigned int resolve_nstype(char *type) {
                return CLONE_NEWUSER;
        else if (!strcmp("cgroup", type))
                return CLONE_NEWCGROUP;
+#ifdef CLONE_NEWTIME
+       else if (!strcmp("time", type))
+               return CLONE_NEWTIME;
+#endif
        else
                return 0;
 }
@@ -1508,16 +1745,50 @@ static unsigned int resolve_nstype(char *type) {
 static int parseOCIlinuxns(struct blob_attr *msg)
 {
        struct blob_attr *tb[__OCI_LINUX_NAMESPACE_MAX];
+       int nstype;
+       int *setns;
+       int fd;
 
        blobmsg_parse(oci_linux_namespace_policy, __OCI_LINUX_NAMESPACE_MAX, tb, blobmsg_data(msg), blobmsg_len(msg));
 
        if (!tb[OCI_LINUX_NAMESPACE_TYPE])
                return EINVAL;
 
-       if (tb[OCI_LINUX_NAMESPACE_PATH])
-               return ENOTSUP; /* ToDo */
+       nstype = resolve_nstype(blobmsg_get_string(tb[OCI_LINUX_NAMESPACE_TYPE]));
+       if (!nstype)
+               return EINVAL;
+
+       if (opts.namespace & nstype)
+               return ENOTUNIQ;
+
+       setns = get_namespace_fd(nstype);
+
+       if (!setns)
+               return EFAULT;
+
+       if (*setns != -1)
+               return ENOTUNIQ;
+
+       if (tb[OCI_LINUX_NAMESPACE_PATH]) {
+               DEBUG("opening existing %s namespace from path %s\n",
+                       blobmsg_get_string(tb[OCI_LINUX_NAMESPACE_TYPE]),
+                       blobmsg_get_string(tb[OCI_LINUX_NAMESPACE_PATH]));
 
-       opts.namespace |= resolve_nstype(blobmsg_get_string(tb[OCI_LINUX_NAMESPACE_TYPE]));
+               fd = open(blobmsg_get_string(tb[OCI_LINUX_NAMESPACE_PATH]), O_RDONLY);
+               if (fd == -1)
+                       return errno?:ESTALE;
+
+               if (ioctl(fd, NS_GET_NSTYPE) != nstype)
+                       return EINVAL;
+
+               DEBUG("opened existing %s namespace got filehandler %u\n",
+                       blobmsg_get_string(tb[OCI_LINUX_NAMESPACE_TYPE]),
+                       fd);
+
+               *setns = fd;
+       } else {
+               opts.namespace |= nstype;
+       }
 
        return 0;
 };
@@ -1605,11 +1876,112 @@ static int parseOCIuidgidmappings(struct blob_attr *msg, bool is_gidmap)
        return 0;
 }
 
+enum {
+       OCI_DEVICES_TYPE,
+       OCI_DEVICES_PATH,
+       OCI_DEVICES_MAJOR,
+       OCI_DEVICES_MINOR,
+       OCI_DEVICES_FILEMODE,
+       OCI_DEVICES_UID,
+       OCI_DEVICES_GID,
+       __OCI_DEVICES_MAX,
+};
+
+static const struct blobmsg_policy oci_devices_policy[] = {
+       [OCI_DEVICES_TYPE] = { "type", BLOBMSG_TYPE_STRING },
+       [OCI_DEVICES_PATH] = { "path", BLOBMSG_TYPE_STRING },
+       [OCI_DEVICES_MAJOR] = { "major", BLOBMSG_TYPE_INT32 },
+       [OCI_DEVICES_MINOR] = { "minor", BLOBMSG_TYPE_INT32 },
+       [OCI_DEVICES_FILEMODE] = { "fileMode", BLOBMSG_TYPE_INT32 },
+       [OCI_DEVICES_UID] = { "uid", BLOBMSG_TYPE_INT32 },
+       [OCI_DEVICES_GID] = { "uid", BLOBMSG_TYPE_INT32 },
+};
+
+static mode_t resolve_devtype(char *tstr)
+{
+       if (!strcmp("c", tstr) ||
+           !strcmp("u", tstr))
+               return S_IFCHR;
+       else if (!strcmp("b", tstr))
+               return S_IFBLK;
+       else if (!strcmp("p", tstr))
+               return S_IFIFO;
+       else
+               return 0;
+}
+
+static int parseOCIdevices(struct blob_attr *msg)
+{
+       struct blob_attr *tb[__OCI_DEVICES_MAX];
+       struct blob_attr *cur;
+       int rem;
+       size_t cnt = 0;
+       struct mknod_args *tmp;
+
+       blobmsg_for_each_attr(cur, msg, rem)
+               ++cnt;
+
+       opts.devices = calloc(cnt + 1, sizeof(struct mknod_args *));
+
+       cnt = 0;
+       blobmsg_for_each_attr(cur, msg, rem) {
+               blobmsg_parse(oci_devices_policy, __OCI_DEVICES_MAX, tb, blobmsg_data(cur), blobmsg_len(cur));
+               if (!tb[OCI_DEVICES_TYPE] ||
+                   !tb[OCI_DEVICES_PATH])
+                       return ENODATA;
+
+               tmp = calloc(1, sizeof(struct mknod_args));
+               if (!tmp)
+                       return ENOMEM;
+
+               tmp->mode = resolve_devtype(blobmsg_get_string(tb[OCI_DEVICES_TYPE]));
+               if (!tmp->mode)
+                       return EINVAL;
+
+               if (tmp->mode != S_IFIFO) {
+                       if (!tb[OCI_DEVICES_MAJOR] || !tb[OCI_DEVICES_MINOR])
+                               return ENODATA;
+
+                       tmp->dev = makedev(blobmsg_get_u32(tb[OCI_DEVICES_MAJOR]),
+                                          blobmsg_get_u32(tb[OCI_DEVICES_MINOR]));
+               }
+
+               if (tb[OCI_DEVICES_FILEMODE]) {
+                       if (~(S_IRWXU|S_IRWXG|S_IRWXO) & blobmsg_get_u32(tb[OCI_DEVICES_FILEMODE]))
+                               return EINVAL;
+
+                       tmp->mode |= blobmsg_get_u32(tb[OCI_DEVICES_FILEMODE]);
+               } else {
+                       tmp->mode |= (S_IRUSR|S_IWUSR); /* 0600 */
+               }
+
+               tmp->path = strdup(blobmsg_get_string(tb[OCI_DEVICES_PATH]));
+
+               if (tb[OCI_DEVICES_UID])
+                       tmp->uid = blobmsg_get_u32(tb[OCI_DEVICES_UID]);
+               else
+                       tmp->uid = -1;
+
+               if (tb[OCI_DEVICES_GID])
+                       tmp->gid = blobmsg_get_u32(tb[OCI_DEVICES_GID]);
+               else
+                       tmp->gid = -1;
+
+               DEBUG("read device %s (%s)\n", blobmsg_get_string(tb[OCI_DEVICES_PATH]), blobmsg_get_string(tb[OCI_DEVICES_TYPE]));
+               opts.devices[cnt++] = tmp;
+       }
+
+       opts.devices[cnt] = NULL;
+
+       return 0;
+}
+
 enum {
        OCI_LINUX_RESOURCES,
        OCI_LINUX_SECCOMP,
        OCI_LINUX_SYSCTL,
        OCI_LINUX_NAMESPACES,
+       OCI_LINUX_DEVICES,
        OCI_LINUX_UIDMAPPINGS,
        OCI_LINUX_GIDMAPPINGS,
        OCI_LINUX_MASKEDPATHS,
@@ -1623,6 +1995,7 @@ static const struct blobmsg_policy oci_linux_policy[] = {
        [OCI_LINUX_SECCOMP] = { "seccomp", BLOBMSG_TYPE_TABLE },
        [OCI_LINUX_SYSCTL] = { "sysctl", BLOBMSG_TYPE_TABLE },
        [OCI_LINUX_NAMESPACES] = { "namespaces", BLOBMSG_TYPE_ARRAY },
+       [OCI_LINUX_DEVICES] = { "devices", BLOBMSG_TYPE_ARRAY },
        [OCI_LINUX_UIDMAPPINGS] = { "uidMappings", BLOBMSG_TYPE_ARRAY },
        [OCI_LINUX_GIDMAPPINGS] = { "gidMappings", BLOBMSG_TYPE_ARRAY },
        [OCI_LINUX_MASKEDPATHS] = { "maskedPaths", BLOBMSG_TYPE_ARRAY },
@@ -1730,6 +2103,12 @@ static int parseOCIlinux(struct blob_attr *msg)
                        return EINVAL;
        }
 
+       if (tb[OCI_LINUX_DEVICES]) {
+               res = parseOCIdevices(tb[OCI_LINUX_DEVICES]);
+               if (res)
+                       return res;
+       }
+
        return 0;
 }
 
@@ -1741,6 +2120,7 @@ enum {
        OCI_MOUNTS,
        OCI_HOOKS,
        OCI_LINUX,
+       OCI_ANNOTATIONS,
        __OCI_MAX,
 };
 
@@ -1752,6 +2132,7 @@ static const struct blobmsg_policy oci_policy[] = {
        [OCI_MOUNTS] = { "mounts", BLOBMSG_TYPE_ARRAY },
        [OCI_HOOKS] = { "hooks", BLOBMSG_TYPE_TABLE },
        [OCI_LINUX] = { "linux", BLOBMSG_TYPE_TABLE },
+       [OCI_ANNOTATIONS] = { "annotations", BLOBMSG_TYPE_TABLE },
 };
 
 static int parseOCI(const char *jsonfile)
@@ -1803,22 +2184,128 @@ static int parseOCI(const char *jsonfile)
        if (tb[OCI_HOOKS] && (res = parseOCIhooks(tb[OCI_HOOKS])))
                return res;
 
+       if (tb[OCI_ANNOTATIONS])
+               opts.annotations = blob_memdup(tb[OCI_ANNOTATIONS]);
+
        blob_buf_free(&ocibuf);
 
        return 0;
 }
 
+static int set_oom_score_adj(void)
+{
+       int f;
+       char fname[32];
+
+       if (!opts.set_oom_score_adj)
+               return 0;
+
+       snprintf(fname, sizeof(fname), "/proc/%u/oom_score_adj", jail_process.pid);
+       f = open(fname, O_WRONLY | O_TRUNC);
+       if (f == -1)
+               return errno;
+
+       dprintf(f, "%d", opts.oom_score_adj);
+       close(f);
+
+       return 0;
+}
+
+
+enum {
+       OCI_STATE_CREATING,
+       OCI_STATE_CREATED,
+       OCI_STATE_RUNNING,
+       OCI_STATE_STOPPED,
+};
+
+static int jail_oci_state = OCI_STATE_CREATED;
+static void pipe_send_start_container(struct uloop_timeout *t);
+static struct uloop_timeout start_container_timeout = {
+       .cb = pipe_send_start_container,
+};
+
+static int handle_start(struct ubus_context *ctx, struct ubus_object *obj,
+                       struct ubus_request_data *req, const char *method,
+                       struct blob_attr *msg)
+{
+       if (jail_oci_state != OCI_STATE_CREATED)
+               return UBUS_STATUS_INVALID_ARGUMENT;
+
+       uloop_timeout_add(&start_container_timeout);
+
+       return UBUS_STATUS_OK;
+}
+
+static struct blob_buf bb;
+static int handle_state(struct ubus_context *ctx, struct ubus_object *obj,
+                       struct ubus_request_data *req, const char *method,
+                       struct blob_attr *msg)
+{
+       char *statusstr;
+
+       switch (jail_oci_state) {
+               case OCI_STATE_CREATING:
+                       statusstr = "creating";
+                       break;
+               case OCI_STATE_CREATED:
+                       statusstr = "created";
+                       break;
+               case OCI_STATE_RUNNING:
+                       statusstr = "running";
+                       break;
+               case OCI_STATE_STOPPED:
+                       statusstr = "stopped";
+                       break;
+               default:
+                       statusstr = "unknown";
+       }
+
+       blob_buf_init(&bb, 0);
+       blobmsg_add_string(&bb, "ociVersion", OCI_VERSION_STRING);
+       blobmsg_add_string(&bb, "id", opts.name);
+       blobmsg_add_string(&bb, "status", statusstr);
+       if (jail_oci_state == OCI_STATE_CREATED ||
+           jail_oci_state == OCI_STATE_RUNNING)
+               blobmsg_add_u32(&bb, "pid", jail_process.pid);
+
+       blobmsg_add_string(&bb, "bundle", opts.ocibundle);
+
+       if (opts.annotations)
+               blobmsg_add_blob(&bb, opts.annotations);
+
+       ubus_send_reply(ctx, req, bb.head);
+
+       return UBUS_STATUS_OK;
+}
+
+static struct ubus_method container_methods[] = {
+       UBUS_METHOD_NOARG("start", handle_start),
+       UBUS_METHOD_NOARG("state", handle_state),
+};
+
+static struct ubus_object_type container_object_type =
+       UBUS_OBJECT_TYPE("container", container_methods);
+
+static struct ubus_object container_object = {
+       .type = &container_object_type,
+       .methods = container_methods,
+       .n_methods = ARRAY_SIZE(container_methods),
+};
+
+static void post_main(struct uloop_timeout *t);
+static struct uloop_timeout post_main_timeout = {
+       .cb = post_main,
+};
+static int netns_fd;
+static int pidns_fd;
+static void post_create_runtime(void);
 int main(int argc, char **argv)
 {
-       sigset_t sigmask;
        uid_t uid = getuid();
        const char log[] = "/dev/log";
        const char ubus[] = "/var/run/ubus.sock";
-       char *jsonfile = NULL;
-       int i, ch;
-       int pipes[4];
-       char sig_buf[1];
-       int netns_fd;
+       int ch, ret;
 
        if (uid) {
                ERROR("not root, aborting: %m\n");
@@ -1910,16 +2397,34 @@ int main(int argc, char **argv)
                        opts.console = 1;
                        break;
                case 'J':
-                       asprintf(&jsonfile, "%s/config.json", optarg);
+                       opts.ocibundle = strdup(optarg);
+                       break;
+               case 'i':
+                       opts.immediately = true;
                        break;
                }
        }
 
-       if (opts.namespace)
+       if (opts.namespace && !opts.ocibundle)
                opts.namespace |= CLONE_NEWIPC | CLONE_NEWPID;
 
-       if (jsonfile) {
+       /* those are filehandlers, so -1 indicates unused */
+       opts.setns.pid = -1;
+       opts.setns.net = -1;
+       opts.setns.ns = -1;
+       opts.setns.ipc = -1;
+       opts.setns.uts = -1;
+       opts.setns.user = -1;
+       opts.setns.cgroup = -1;
+#ifdef CLONE_NEWTIME
+       opts.setns.time = -1;
+#endif
+
+       if (opts.ocibundle) {
+               char *jsonfile;
                int ocires;
+
+               asprintf(&jsonfile, "%s/config.json", opts.ocibundle);
                ocires = parseOCI(jsonfile);
                free(jsonfile);
                if (ocires) {
@@ -1934,11 +2439,11 @@ int main(int argc, char **argv)
        }
 
        /* no <binary> param found */
-       if (!jsonfile && (argc - optind < 1)) {
+       if (!opts.ocibundle && (argc - optind < 1)) {
                usage();
                return EXIT_FAILURE;
        }
-       if (!(opts.namespace||opts.capabilities||opts.seccomp)) {
+       if (!(opts.ocibundle||opts.namespace||opts.capabilities||opts.seccomp)) {
                ERROR("Not using namespaces, capabilities or seccomp !!!\n\n");
                usage();
                return EXIT_FAILURE;
@@ -1948,7 +2453,27 @@ int main(int argc, char **argv)
                opts.capabilities != 0 || opts.capset.apply,
                opts.seccomp != 0 || opts.ociseccomp != 0);
 
-       if (!jsonfile) {
+       uloop_init();
+       signals_init();
+
+       parent_ctx = ubus_connect(NULL);
+       ubus_add_uloop(parent_ctx);
+
+       if (opts.ocibundle) {
+               char *objname;
+               if (asprintf(&objname, "container.%s", opts.name) < 0)
+                       exit(-ENOMEM);
+
+               container_object.name = objname;
+               ret = ubus_add_object(parent_ctx, &container_object);
+               if (ret) {
+                       ERROR("Failed to add object: %s\n", ubus_strerror(ret));
+                       exit(-1);
+               }
+       }
+
+       /* deliberately not using 'else' on unrelated conditional branches */
+       if (!opts.ocibundle) {
                /* allocate NULL-terminated array for argv */
                opts.jail_argv = calloc(1 + argc - optind, sizeof(char**));
                if (!opts.jail_argv)
@@ -1975,6 +2500,15 @@ int main(int argc, char **argv)
                        return -1;
        }
 
+       uloop_timeout_add(&post_main_timeout);
+       uloop_run();
+
+       /* unreachable */
+       return 0;
+}
+
+static void post_main(struct uloop_timeout *t)
+{
        if (apply_rlimits()) {
                ERROR("error applying resource limits\n");
                exit(EXIT_FAILURE);
@@ -1983,29 +2517,11 @@ int main(int argc, char **argv)
        if (opts.name)
                prctl(PR_SET_NAME, opts.name, NULL, NULL, NULL);
 
-       sigfillset(&sigmask);
-       for (i = 0; i < _NSIG; i++) {
-               struct sigaction s = { 0 };
+       if (pipe(&pipes[0]) < 0 || pipe(&pipes[2]) < 0)
+               exit(-1);
 
-               if (!sigismember(&sigmask, i))
-                       continue;
-               if ((i == SIGCHLD) || (i == SIGPIPE) || (i == SIGSEGV))
-                       continue;
-
-               s.sa_handler = jail_handle_signal;
-               sigaction(i, &s, NULL);
-       }
-
-       if (opts.namespace) {
+       if (has_namespaces()) {
                if (opts.namespace & CLONE_NEWNS) {
-                       add_mount_bind("/dev/full", 0, -1);
-                       add_mount_bind("/dev/null", 0, -1);
-                       add_mount_bind("/dev/random", 0, -1);
-                       add_mount_bind("/dev/urandom", 0, -1);
-                       add_mount_bind("/dev/zero", 0, -1);
-                       add_mount_bind("/dev/ptmx", 0, -1);
-                       add_mount_bind("/dev/tty", 0, -1);
-
                        if (!opts.extroot && (opts.user || opts.group)) {
                                add_mount_bind("/etc/passwd", 0, -1);
                                add_mount_bind("/etc/group", 0, -1);
@@ -2018,39 +2534,103 @@ int main(int argc, char **argv)
 
                        if (!(opts.namespace & CLONE_NEWNET)) {
                                add_mount_bind("/etc/resolv.conf", 0, -1);
-                       } else {
+                       } else if (opts.setns.net == -1) {
                                char hostdir[PATH_MAX];
 
                                snprintf(hostdir, PATH_MAX, "/tmp/resolv.conf-%s.d", opts.name);
                                mkdir_p(hostdir, 0755);
-                               add_mount(hostdir, "/tmp/resolv.conf.d", NULL, MS_BIND | MS_NOEXEC | MS_NOATIME | MS_NOSUID | MS_NODEV | MS_RDONLY, NULL, -1);
+                               add_mount(hostdir, "/dev/resolv.conf.d", NULL, MS_BIND | MS_NOEXEC | MS_NOATIME | MS_NOSUID | MS_NODEV | MS_RDONLY, NULL, -1);
+                       }
+
+                       /* default mounts */
+                       add_mount(NULL, "/dev", "tmpfs", MS_NOATIME | MS_NOEXEC | MS_NOSUID, "size=1M", -1);
+                       add_mount(NULL, "/dev/pts", "devpts", MS_NOATIME | MS_NOEXEC | MS_NOSUID, "newinstance,ptmxmode=0666,mode=0620,gid=5", 0);
+
+                       if (opts.procfs || opts.ocibundle) {
+                               add_mount("proc", "/proc", "proc", MS_NOATIME | MS_NODEV | MS_NOEXEC | MS_NOSUID, NULL, -1);
+
+                               /*
+                                * hack to make /proc/sys/net read-write while the rest of /proc/sys is read-only
+                                * which cannot be expressed with OCI spec, but happends to be very useful.
+                                * Only apply it if '/proc/sys' is not already listed as mount, maskedPath or
+                                * readonlyPath.
+                                * If not running in a new network namespace, only make /proc/sys read-only.
+                                * If running in a new network namespace, temporarily stash (ie. mount-bind)
+                                * /proc/sys/net into (totally unrelated, but surely existing) /proc/self/net.
+                                * Then we mount-bind /proc/sys read-only and then mount-move /proc/self/net into
+                                * /proc/sys/net.
+                                * This works because mounts are executed in incrementing strcmp() order and
+                                * /proc/self/net appears there before /proc/sys/net and hence the operation
+                                * succeeds as the bind-mount of /proc/self/net is performed first and then
+                                * move-mount of /proc/sys/net follows because 'e' preceeds 'y' in the ASCII
+                                * table (and in the alphabet).
+                                */
+                               if (!add_mount(NULL, "/proc/sys", NULL, MS_BIND | MS_RDONLY, NULL, -1))
+                                       if (opts.namespace & CLONE_NEWNET)
+                                               if (!add_mount_inner("/proc/self/net", "/proc/sys/net", NULL, MS_MOVE, NULL, -1))
+                                                       add_mount_inner("/proc/sys/net", "/proc/self/net", NULL, MS_BIND, NULL, -1);
+
                        }
+                       if (opts.sysfs || opts.ocibundle)
+                               add_mount("sysfs", "/sys", "sysfs", MS_NOATIME | MS_NODEV | MS_NOEXEC | MS_NOSUID | MS_RDONLY, NULL, -1);
+
+                       if (opts.ocibundle)
+                               add_mount("shm", "/dev/shm", "tmpfs", MS_NOSUID | MS_NOEXEC | MS_NODEV, "mode=1777", -1);
+
                }
 
-               if (pipe(&pipes[0]) < 0 || pipe(&pipes[2]) < 0)
-                       return -1;
+               if (opts.setns.pid != -1) {
+                       pidns_fd = pidns_open_pid(getpid());
+                       setns_open(CLONE_NEWPID);
+               } else {
+                       pidns_fd = -1;
+               }
 
-               jail_process.pid = clone(exec_jail, child_stack + STACK_SIZE, SIGCHLD | opts.namespace, &pipes);
+               jail_process.pid = clone(exec_jail, child_stack + STACK_SIZE, SIGCHLD | opts.namespace, NULL);
        } else {
                jail_process.pid = fork();
        }
 
        if (jail_process.pid > 0) {
+               /* parent process */
+               char sig_buf[1];
+
+               uloop_process_add(&jail_process);
                jail_running = 1;
                seteuid(0);
-               /* parent process */
+               if (pidns_fd != -1) {
+                       setns(pidns_fd, CLONE_NEWPID);
+                       close(pidns_fd);
+               }
+               if (opts.setns.net != -1)
+                       close(opts.setns.net);
+               if (opts.setns.ns != -1)
+                       close(opts.setns.ns);
+               if (opts.setns.ipc != -1)
+                       close(opts.setns.ipc);
+               if (opts.setns.uts != -1)
+                       close(opts.setns.uts);
+               if (opts.setns.user != -1)
+                       close(opts.setns.user);
+               if (opts.setns.cgroup != -1)
+                       close(opts.setns.cgroup);
+#ifdef CLONE_NEWTIME
+               if (opts.setns.time != -1)
+                       close(opts.setns.time);
+#endif
                close(pipes[1]);
                close(pipes[2]);
-               run_hooks(opts.hooks.createRuntime);
                if (read(pipes[0], sig_buf, 1) < 1) {
                        ERROR("can't read from child\n");
-                       return -1;
+                       exit(-1);
                }
                close(pipes[0]);
+               set_oom_score_adj();
+
                if (opts.namespace & CLONE_NEWUSER) {
                        if (write_setgroups(jail_process.pid, true)) {
                                ERROR("can't write setgroups\n");
-                               return -1;
+                               exit(-1);
                        }
                        if (!opts.uidmap) {
                                bool has_gr = (opts.gr_gid != -1);
@@ -2071,43 +2651,82 @@ int main(int argc, char **argv)
                if (opts.namespace & CLONE_NEWNET) {
                        if (!opts.name) {
                                ERROR("netns needs a named jail\n");
-                               return -1;
+                               exit(-1);
                        }
                        netns_fd = netns_open_pid(jail_process.pid);
                        netns_updown(jail_process.pid, true);
                }
-
-               sig_buf[0] = 'O';
-               if (write(pipes[3], sig_buf, 1) < 0) {
-                       ERROR("can't write to child\n");
-                       return -1;
-               }
-               close(pipes[3]);
-               run_hooks(opts.hooks.poststart);
-
-               uloop_init();
-               uloop_process_add(&jail_process);
-               uloop_run();
-               if (jail_running) {
-                       DEBUG("uloop interrupted, killing jail process\n");
-                       kill(jail_process.pid, SIGTERM);
-                       uloop_timeout_set(&jail_process_timeout, 1000);
-                       uloop_run();
-               }
-               uloop_done();
-               if (opts.namespace & CLONE_NEWNET) {
-                       setns(netns_fd, CLONE_NEWNET);
-                       netns_updown(getpid(), false);
-                       close(netns_fd);
-               }
-               run_hooks(opts.hooks.poststop);
-               free_opts(true);
-               return jail_return_code;
        } else if (jail_process.pid == 0) {
                /* fork child process */
-               return exec_jail(NULL);
+               exit(exec_jail(NULL));
        } else {
                ERROR("failed to clone/fork: %m\n");
-               return EXIT_FAILURE;
+               exit(EXIT_FAILURE);
        }
+       run_hooks(opts.hooks.createRuntime, post_create_runtime);
+}
+
+static void post_poststart(void);
+static void post_create_runtime(void)
+{
+       char sig_buf[1];
+
+       sig_buf[0] = 'O';
+       if (write(pipes[3], sig_buf, 1) < 0) {
+               ERROR("can't write to child\n");
+               exit(-1);
+       }
+
+       jail_oci_state = OCI_STATE_CREATED;
+       if (opts.ocibundle && !opts.immediately)
+               uloop_run(); /* wait for 'start' command via ubus */
+       else
+               pipe_send_start_container(NULL);
+}
+
+static void pipe_send_start_container(struct uloop_timeout *t)
+{
+       char sig_buf[1];
+
+       jail_oci_state = OCI_STATE_RUNNING;
+       sig_buf[0] = '!';
+       if (write(pipes[3], sig_buf, 1) < 0) {
+               ERROR("can't write to child\n");
+               exit(-1);
+       }
+       close(pipes[3]);
+
+       run_hooks(opts.hooks.poststart, post_poststart);
+}
+
+static void post_poststart(void)
+{
+       uloop_run(); /* idle here while jail is running */
+       if (jail_running) {
+               DEBUG("uloop interrupted, killing jail process\n");
+               kill(jail_process.pid, SIGTERM);
+               uloop_timeout_set(&jail_process_timeout, 1000);
+               uloop_run();
+       }
+       uloop_done();
+       poststop();
+}
+
+static void post_poststop(void);
+static void poststop(void) {
+       if (opts.namespace & CLONE_NEWNET) {
+               setns(netns_fd, CLONE_NEWNET);
+               netns_updown(getpid(), false);
+               close(netns_fd);
+       }
+       run_hooks(opts.hooks.poststop, post_poststop);
+}
+
+static void post_poststop(void)
+{
+       free_opts(true);
+       if (parent_ctx)
+               ubus_free(parent_ctx);
+
+       exit(jail_return_code);
 }