diff options
Diffstat (limited to 'src/init/s6-linux-init.c')
-rw-r--r-- | src/init/s6-linux-init.c | 150 |
1 files changed, 117 insertions, 33 deletions
diff --git a/src/init/s6-linux-init.c b/src/init/s6-linux-init.c index 78cf3d0..530093e 100644 --- a/src/init/s6-linux-init.c +++ b/src/init/s6-linux-init.c @@ -23,20 +23,53 @@ #include "defaults.h" #include "initctl.h" -#define USAGE "s6-linux-init [ -c basedir ] [ -p initpath ] [ -s envdumpdir ] [ -m umask ] [ -d devtmpfs ] [ -D initdefault ] [ -n | -N ]" +#define USAGE "s6-linux-init [ -c basedir ] [ -p initpath ] [ -s envdumpdir ] [ -m umask ] [ -d devtmpfs ] [ -D initdefault ] [ -n | -N ] [ -C ] [ -B ]" #define dieusage() strerr_dieusage(100, USAGE) #define BANNER "\n s6-linux-init version " S6_LINUX_INIT_VERSION "\n\n" +static int inns = 0 ; +static int nologger = 0 ; +static int notifpipe[2] ; + static inline char const *scan_cmdline (char const *initdefault, char const *const *argv, unsigned int argc) { - static char const *valid[] = { "default", "2", "3", "4", "5", 0 } ; - for (unsigned int i = 0 ; i < argc ; i++) - for (char const *const *p = valid ; *p ; p++) - if (!strcmp(argv[i], *p)) return argv[i] ; + if (!inns) + { + static char const *valid[] = { "default", "2", "3", "4", "5", 0 } ; + for (unsigned int i = 0 ; i < argc ; i++) + for (char const *const *p = valid ; *p ; p++) + if (!strcmp(argv[i], *p)) return argv[i] ; + } return initdefault ; } +static inline void wait_for_notif (int fd) +{ + char buf[16] ; + for (;;) + { + ssize_t r = read(fd, buf, 16) ; + if (r < 0) strerr_diefu1sys(111, "read from notification pipe") ; + if (!r) + { + strerr_warnw1x("s6-svscan failed to send a notification byte!") ; + break ; + } + if (memchr(buf, '\n', r)) break ; + } + close(fd) ; +} + +static void disablecad (void) +{ + if (!inns) + { + if (reboot(RB_DISABLE_CAD) == -1) + strerr_warnwu1sys("trap ctrl-alt-del") ; + } +} + static inline void run_stage2 (char const *basedir, char const **argv, unsigned int argc, char const *const *envp, size_t envlen, char const *modifs, size_t modiflen, char const *initdefault) { size_t dirlen = strlen(basedir) ; @@ -51,11 +84,20 @@ static inline void run_stage2 (char const *basedir, char const **argv, unsigned childargv[i+2] = argv[i] ; childargv[argc + 2] = 0 ; setsid() ; - fd_close(1) ; - if (open(LOGFIFO, O_WRONLY) != 1) /* blocks until catch-all logger is up */ - strerr_diefu1sys(111, "open " LOGFIFO " for writing") ; - if (fd_copy(2, 1) == -1) - strerr_diefu1sys(111, "fd_copy stdout to stderr") ; + if (nologger) + { + close(notifpipe[1]) ; + wait_for_notif(notifpipe[0]) ; + } + else + { + /* block on opening the log fifo until the catch-all logger is up */ + close(1) ; + if (open(LOGFIFO, O_WRONLY) != 1) + strerr_diefu1sys(111, "open " LOGFIFO " for writing") ; + if (fd_copy(2, 1) == -1) + strerr_diefu1sys(111, "fd_copy stdout to stderr") ; + } xpathexec_r(childargv, envp, envlen, modifs, modiflen) ; } @@ -74,15 +116,14 @@ int main (int argc, char const **argv, char const *const *envp) if (getpid() != 1) { argv[0] = S6_LINUX_INIT_BINPREFIX "s6-linux-init-telinit" ; - pathexec_run(argv[0], argv, envp) ; - strerr_dieexec(111, argv[0]) ; + xpathexec_run(argv[0], argv, envp) ; } { subgetopt_t l = SUBGETOPT_ZERO ; for (;;) { - int opt = subgetopt_r(argc, argv, "c:p:s:m:d:D:nN", &l) ; + int opt = subgetopt_r(argc, argv, "c:p:s:m:d:D:nNCB", &l) ; if (opt == -1) break ; switch (opt) { @@ -94,35 +135,60 @@ int main (int argc, char const **argv, char const *const *envp) case 'D' : initdefault = l.arg ; break ; case 'n' : mounttype = 2 ; break ; case 'N' : mounttype = 0 ; break ; + case 'C' : inns = 1 ; break ; + case 'B' : nologger = 1 ; break ; default : dieusage() ; } } argc -= l.ind ; argv += l.ind ; } - allwrite(1, BANNER, sizeof(BANNER) - 1) ; + if (inns) + { /* If there's a Docker synchronization pipe, wait on it */ + char c ; + ssize_t r = read(3, &c, 1) ; + if (r < 0) + { + if (errno != EBADF) strerr_diefu1sys(111, "read from fd 3") ; + } + else + { + if (r) strerr_warnw1x("parent wrote to fd 3!") ; + close(3) ; + } + } + else allwrite(1, BANNER, sizeof(BANNER) - 1) ; if (chdir("/") == -1) strerr_diefu1sys(111, "chdir to /") ; umask(mask) ; setpgid(0, 0) ; - fd_close(0) ; + close(0) ; + if (slashdev) { - fd_close(1) ; - fd_close(2) ; - if (mount("dev", slashdev, "devtmpfs", MS_NOSUID | MS_NOEXEC, "") == -1) + int nope, e ; + close(1) ; + close(2) ; + /* at this point we're totally in the dark, hoping /dev/console will work */ + nope = mount("dev", slashdev, "devtmpfs", MS_NOSUID | MS_NOEXEC, "") < 0 ; + e = errno ; + if (open("/dev/console", O_WRONLY) + || fd_move(2, 0) < 0 + || fd_copy(1, 2) < 0) return 111 ; + if (nope) { - int e = errno ; - open("/dev/null", O_RDONLY) ; - open("/dev/console", O_WRONLY) ; - fd_copy(2, 1) ; errno = e ; - strerr_diefu2sys(111, "mount ", slashdev) ; + strerr_diefu1sys(111, "mount a devtmpfs on /dev") ; } - if (open("/dev/console", O_WRONLY) - || fd_copy(1, 0) == -1 - || fd_move(2, 0) == -1) return 111 ; } - if (open("/dev/null", O_RDONLY)) strerr_diefu1sys(111, "open /dev/null") ; + + if (open("/dev/null", O_RDONLY)) + { /* ghetto /dev/null to the rescue */ + int p[2] ; + strerr_warnwu1sys("open /dev/null") ; + if (pipe(p) < 0) strerr_diefu1sys(111, "pipe") ; + close(p[1]) ; + if (fd_move(0, p[0]) < 0) strerr_diefu1sys(111, "fd_move to stdin") ; + } if (mounttype) { @@ -157,6 +223,8 @@ int main (int argc, char const **argv, char const *const *envp) } if (envdumpdir && !env_dump(envdumpdir, 0700, envp)) strerr_warnwu2sys("dump kernel environment to ", envdumpdir) ; + + if (!nologger) { int fdr = open_read(LOGFIFO) ; if (fdr == -1) strerr_diefu1sys(111, "open " LOGFIFO) ; @@ -164,11 +232,13 @@ int main (int argc, char const **argv, char const *const *envp) if (open(LOGFIFO, O_WRONLY) != 1) strerr_diefu1sys(111, "open " LOGFIFO) ; fd_close(fdr) ; } + { - static char const *const newargv[5] = { S6_EXTBINPREFIX "s6-svscan", "-st0", "--", S6_LINUX_INIT_TMPFS "/" SCANDIR, 0 } ; char const *newenvp[2] = { 0, 0 } ; - pid_t pid ; size_t pathlen = path ? strlen(path) : 0 ; + pid_t pid ; + char fmtfd[2 + UINT_FMT] = "-" ; + char const *newargv[6] = { S6_EXTBINPREFIX "s6-svscan", "-st0", fmtfd, "--", S6_LINUX_INIT_TMPFS "/" SCANDIR, 0 } ; char pathvar[6 + pathlen] ; if (path) { @@ -178,13 +248,27 @@ int main (int argc, char const **argv, char const *const *envp) memcpy(pathvar + 5, path, pathlen + 1) ; newenvp[0] = pathvar ; } + if (nologger && pipe(notifpipe) < 0) strerr_diefu1sys(111, "pipe") ; pid = fork() ; if (pid == -1) strerr_diefu1sys(111, "fork") ; if (!pid) run_stage2(basedir, argv, argc, newenvp, !!path, envmodifs.s, envmodifs.len, initdefault) ; - if (reboot(RB_DISABLE_CAD) == -1) - strerr_warnwu1sys("trap ctrl-alt-del") ; - if (fd_copy(2, 1) == -1) - strerr_diefu1sys(111, "redirect output file descriptor") ; + if (nologger) + { + close(notifpipe[0]) ; + fmtfd[1] = 'd' ; + fmtfd[2 + uint_fmt(fmtfd + 2, notifpipe[1])] = 0 ; + disablecad() ; + } + else + { + int fd = dup(2) ; + if (fd < 0) strerr_diefu1sys(111, "dup stderr") ; + fmtfd[1] = 'X' ; + fmtfd[2 + uint_fmt(fmtfd + 2, (unsigned int)fd)] = 0 ; + disablecad() ; + if (fd_copy(2, 1) == -1) + strerr_diefu1sys(111, "redirect output file descriptor") ; + } xpathexec_r(newargv, newenvp, !!path, envmodifs.s, envmodifs.len) ; } } |