diff options
Diffstat (limited to 'src/init')
-rw-r--r-- | src/init/deps-exe/s6-linux-init (renamed from src/init/deps-exe/s6-halt) | 0 | ||||
-rw-r--r-- | src/init/deps-exe/s6-poweroff | 1 | ||||
-rw-r--r-- | src/init/deps-exe/s6-reboot | 1 | ||||
-rw-r--r-- | src/init/hpr.c | 107 | ||||
-rw-r--r-- | src/init/s6-halt.c | 7 | ||||
-rw-r--r-- | src/init/s6-linux-init-maker.c | 444 | ||||
-rw-r--r-- | src/init/s6-linux-init.c | 168 | ||||
-rw-r--r-- | src/init/s6-poweroff.c | 7 | ||||
-rw-r--r-- | src/init/s6-reboot.c | 7 |
9 files changed, 387 insertions, 355 deletions
diff --git a/src/init/deps-exe/s6-halt b/src/init/deps-exe/s6-linux-init index e7187fe..e7187fe 100644 --- a/src/init/deps-exe/s6-halt +++ b/src/init/deps-exe/s6-linux-init diff --git a/src/init/deps-exe/s6-poweroff b/src/init/deps-exe/s6-poweroff deleted file mode 100644 index e7187fe..0000000 --- a/src/init/deps-exe/s6-poweroff +++ /dev/null @@ -1 +0,0 @@ --lskarnet diff --git a/src/init/deps-exe/s6-reboot b/src/init/deps-exe/s6-reboot deleted file mode 100644 index e7187fe..0000000 --- a/src/init/deps-exe/s6-reboot +++ /dev/null @@ -1 +0,0 @@ --lskarnet diff --git a/src/init/hpr.c b/src/init/hpr.c deleted file mode 100644 index db80ba2..0000000 --- a/src/init/hpr.c +++ /dev/null @@ -1,107 +0,0 @@ -/* ISC license. */ - -#include <skalibs/sysdeps.h> -#include <skalibs/nonposix.h> -#include <unistd.h> -#include <signal.h> -#include <errno.h> -#include <sys/reboot.h> -#include <skalibs/strerr2.h> -#include <skalibs/sgetopt.h> -#include <skalibs/sig.h> -#include <skalibs/djbunix.h> - -#define USAGE PROGNAME " [ -h | -p | -r ] [ -f ]" - -#ifdef SKALIBS_HASNSGETPARENT - -#include <sys/ioctl.h> -#include <linux/nsfs.h> - -static int test_in_namespace (void) -{ - int r ; - int fd = open_read("/proc/1/ns/pid") ; - if (fd < 0) return 0 ; - r = ioctl(fd, NS_GET_PARENT) ; - close(fd) ; - return r >= 0 ; -} - -#else - - /* - When in doubt, always trap signals. This incurs a small race: - if ctrl-alt-del is pressed at the wrong time, the process will - exit and cause a kernel panic. But the alternatives are WAY - more hackish than this. - */ - -static int test_in_namespace (void) -{ - return 1 ; -} - -#endif - -static void sigint_handler (int sig) -{ - (void)sig ; - _exit(1) ; -} - -static void sighup_handler (int sig) -{ - (void)sig ; - _exit(0) ; -} - -int main (int argc, char const *const *argv) -{ - int what = WHATDEFAULT ; - int force = 0 ; - PROG = PROGNAME ; - - { - subgetopt_t l = SUBGETOPT_ZERO ; - for (;;) - { - int opt = subgetopt_r(argc, argv, "hprf", &l) ; - if (opt == -1) break ; - switch (opt) - { - case 'h' : what = 1 ; break ; - case 'p' : what = 2 ; break ; - case 'r' : what = 3 ; break ; - case 'f' : force = 1 ; break ; - default : strerr_dieusage(100, USAGE) ; - } - } - argc -= l.ind ; argv += l.ind ; - } - - if (geteuid()) - { - errno = EPERM ; - strerr_dief1sys(100, "nice try, peon") ; - } - - if (force) - { - sync() ; - if (getpid() == 1) - { - if (test_in_namespace()) - { - if (sig_catch(SIGINT, &sigint_handler) < 0 - || sig_catch(SIGHUP, &sighup_handler) < 0) - strerr_diefu1sys(111, "catch signals") ; - } - } - reboot(what == 3 ? RB_AUTOBOOT : what == 2 ? RB_POWER_OFF : RB_HALT_SYSTEM) ; - strerr_diefu1sys(111, "reboot()") ; - } - else if (kill(1, what == 3 ? SIGINT : what == 2 ? SIGUSR1 : SIGUSR2) < 0) - strerr_diefu1sys(111, "signal process 1") ; - return 0 ; -} diff --git a/src/init/s6-halt.c b/src/init/s6-halt.c deleted file mode 100644 index d758030..0000000 --- a/src/init/s6-halt.c +++ /dev/null @@ -1,7 +0,0 @@ -/* ISC license. */ - -#undef PROGNAME -#define PROGNAME "s6-halt" -#undef WHATDEFAULT -#define WHATDEFAULT 1 -#include "hpr.c" diff --git a/src/init/s6-linux-init-maker.c b/src/init/s6-linux-init-maker.c index 30866fe..48c95c6 100644 --- a/src/init/s6-linux-init-maker.c +++ b/src/init/s6-linux-init-maker.c @@ -16,11 +16,17 @@ #include <skalibs/sgetopt.h> #include <skalibs/skamisc.h> -#define USAGE "s6-linux-init-maker [ -c basedir ] [ -l tmpfsdir ] [ -b execline_bindir ] [ -u log_uid -g log_gid | -U ] [ -G early_getty_cmd ] [ -2 stage2_script ] [ -r ] [ -Z shutdownscript ] [ -p initial_path ] [ -m initial_umask ] [ -t timestamp_style ] [ -d dev_style ] [ -s env_store ] [ -e initial_envvar ... ] [ -n ] [ -q final_sleep_time ] dir" +#include <s6/config.h> + +#include <s6-linux-init/config.h> +#include "defaults.h" +#include "initctl.h" + +#define USAGE "s6-linux-init-maker [ -c basedir ] [ -b execline_bindir ] [ -u log_uid -g log_gid | -U ] [ -G early_getty_cmd ] [ -r ] [ -L ] [ -p initial_path ] [ -m initial_umask ] [ -t timestamp_style ] [ -d slashdev ] [ -s env_store ] [ -e initial_envvar ... ] [ -q default_grace_time ] dir" #define dieusage() strerr_dieusage(100, USAGE) #define dienomem() strerr_diefu1sys(111, "stralloc_catb") ; -#define BANNER "\n init created by s6-linux-init-maker\n see http://skarnet.org/software/s6-linux-init/\n\n" +#define UNCAUGHT_DIR "uncaught-logs" #define EXITCODENAME "file\\ created\\ by\\ s6-linux-init,\\ storing\\ a\\ container's\\ exit\\ code" #define CRASH_SCRIPT \ @@ -28,248 +34,177 @@ "redirfd -w 1 /dev/console\n" \ "fdmove -c 2 1\n" \ "foreground { s6-echo -- " \ -"\"s6-svscan crashed. Dropping to an interactive shell.\" }\n" \ +"\"s6-svscan finished. Dropping to an interactive shell.\" }\n" \ "/bin/sh -i\n" -static char const *slashrun = "/run" ; -static char const *robase = "/etc/s6-linux-init" ; -static char const *init_script = "/etc/rc.init" ; -static char const *tini_script = "/etc/rc.shutdown" ; -static char const *bindir = "/bin" ; -static char const *initial_path = "/usr/bin:/usr/sbin:/bin:/sbin" ; +static char const *robase = BASEDIR ; +static char const *bindir = BINDIR ; +static char const *initial_path = INITPATH ; static char const *env_store = 0 ; static char const *early_getty = 0 ; +static char const *slashdev = 0 ; static uid_t uncaught_logs_uid = 0 ; static gid_t uncaught_logs_gid = 0 ; -static unsigned int initial_umask = 022 ; +static unsigned int initial_umask = 0022 ; static unsigned int timestamp_style = 1 ; -static unsigned int slashdev_style = 2 ; -static unsigned int finalsleep = 2000 ; +static unsigned int finalsleep = 3000 ; static int redirect_stage2 = 0 ; -static int in_namespace = 0 ; +static int logouthookd = 0 ; -typedef int writetobuf_func_t (buffer *) ; +typedef int writetobuf_func_t (buffer *, void *) ; typedef writetobuf_func_t *writetobuf_func_t_ref ; -static int put_shebang (buffer *b) +#define put_shebang(b) put_shebang_options((b), "-P") + +static int put_shebang_options (buffer *b, void *data) { + char *options = data ; return buffer_puts(b, "#!") >= 0 && buffer_puts(b, bindir) >= 0 - && buffer_puts(b, "/execlineb -P\n\n") >= 0 ; + && buffer_puts(b, "/execlineb") >= 0 + && (!options || !options[0] || buffer_puts(b, " ") >= 0) + && buffer_puts(b, options) >= 0 + && buffer_puts(b, "\n\n") >= 0 ; } -static int early_getty_script (buffer *b) +static int line_script (buffer *b, void *data) { + char *line = data ; return put_shebang(b) - && buffer_puts(b, early_getty) >= 0 + && buffer_puts(b, line) >= 0 && buffer_put(b, "\n", 1) >= 0 ; } -static int crash_script (buffer *b) +static int linewithargs_script (buffer *b, void *data) +{ + char *line = data ; + return put_shebang_options(b, "-S0") + && buffer_puts(b, line) >= 0 + && buffer_puts(b, " $@\n") >= 0 ; +} + +static int death_script (buffer *b, void *data) { + char *s = data ; return put_shebang(b) - && buffer_puts(b, CRASH_SCRIPT) >= 0 ; + && buffer_puts(b, + "redirfd -r 0 /dev/console\n" + "redirfd -w 1 /dev/console\n" + "fdmove -c 2 1\n" + "foreground { s6-echo -- \"s6-svscan ") >= 0 + && buffer_puts(b, s) >= 0 + && buffer_puts(b, + ". Dropping to an interactive shell.\" }\n" + "/bin/sh -i\n") >= 0 } -static int s6_svscan_log_script (buffer *b) +static int s6_svscan_log_script (buffer *b, void *data) { - size_t sabase = satmp.len ; char fmt[UINT64_FMT] ; - if (!put_shebang(b) - || buffer_puts(b, + (void)data ; + return put_shebang(b) + && buffer_puts(b, "redirfd -w 2 /dev/console\n" "redirfd -w 1 /dev/null\n" - "redirfd -rnb 0 fifo\n" - "s6-applyuidgid -u ") < 0 - || buffer_put(b, fmt, uid_fmt(fmt, uncaught_logs_uid)) < 0 - || buffer_puts(b, " -g ") < 0 - || buffer_put(b, fmt, gid_fmt(fmt, uncaught_logs_gid)) < 0 - || buffer_puts(b, " --\ns6-log -bp -- ") < 0 - || buffer_puts(b, timestamp_style & 1 ? "t " : "") < 0 - || buffer_puts(b, timestamp_style & 2 ? "T " : "") < 0) return 0 ; - if (!string_quote(&satmp, slashrun, strlen(slashrun))) return 0 ; - if (buffer_put(b, satmp.s + sabase, satmp.len - sabase) < 0) - { - satmp.len = sabase ; - return 0 ; - } - satmp.len = sabase ; - if (buffer_puts(b, "/uncaught-logs\n") < 0) return 0 ; - return 1 ; + "redirfd -rnb 0 " LOGGER_FIFO "\n" + "s6-applyuidgid -u ") >= 0 + && buffer_put(b, fmt, uid_fmt(fmt, uncaught_logs_uid)) >= 0 + && buffer_puts(b, " -g ") >= 0 + && buffer_put(b, fmt, gid_fmt(fmt, uncaught_logs_gid)) >= 0 + && buffer_puts(b, " --\ns6-log -bpd3 -- ") >= 0 + && buffer_puts(b, timestamp_style & 1 ? "t " : "") >= 0 + && buffer_puts(b, timestamp_style & 2 ? "T " : "") >= 0 + && buffer_puts(b, S6_LINUX_INIT_TMPFS "/" UNCAUGHT_DIR "\n") >= 0 ; } -static int finish_script (buffer *b) +static int logouthookd_script (buffer *b, void *data) { - size_t sabase = satmp.len ; - char fmt[UINT_FMT] ; - fmt[uint_fmt(fmt, finalsleep)] = 0 ; - if (buffer_puts(b, "#!") < 0 - || buffer_puts(b, bindir) < 0 - || buffer_puts(b, "/execlineb -S0\n\n") < 0 - || !string_quote(&satmp, slashrun, strlen(slashrun))) return 0 ; - - if (in_namespace) - { - if (buffer_puts(b, "ifelse { redirfd -w 2 /dev/null redirfd -r 0 ") < 0 - || buffer_put(b, satmp.s + sabase, satmp.len - sabase) < 0 - || buffer_puts(b, "/" EXITCODENAME " exit 0 }\n{ redirfd -r 0 ") < 0 - || buffer_put(b, satmp.s + sabase, satmp.len - sabase) < 0 - || buffer_puts(b, "/" EXITCODENAME " withstdinas -in CODE foreground { s6-rmrf ") < 0 - || buffer_put(b, satmp.s + sabase, satmp.len - sabase) < 0 - || buffer_puts(b, "/" EXITCODENAME " } importas -ui CODE CODE exit ${CODE} }\n") < 0) goto err ; - } - - if (buffer_puts(b, "cd /\nredirfd -w 2 /dev/console\nfdmove -c 1 2\nforeground { s6-svc -X -- ") < 0 - || buffer_put(b, satmp.s + sabase, satmp.len - sabase) < 0) goto err ; - satmp.len = sabase ; - if (buffer_puts(b, "/service/s6-svscan-log }\n" - "unexport ?\nwait -r -- { }\n" - "foreground { s6-echo \"Syncing disks.\" }\n" - "foreground { s6-sync }\n" - "foreground { s6-echo \"Sending all processes the TERM signal.\" }\n" - "foreground { s6-nuke -th }\n" - "s6-sleep -m -- ") < 0 - || buffer_puts(b, fmt) < 0 - || buffer_puts(b, "\nforeground { s6-echo \"Sending all processes the KILL signal.\" }\n" - "foreground { s6-nuke -k }\n" - "wait { }\n" - "foreground { s6-echo \"Unmounting disks.\" }\n" - "foreground { s6-umount -a }\n" - "foreground { s6-mount -ro remount /dev/root / }\n" - "s6-${1} -f\n") < 0) return 0 ; - return 1 ; - err: - satmp.len = sabase ; - return 0 ; + (void)data ; + return put_shebang(b) + && buffer_puts(b, + S6_EXTBINPREFIX "s6-ipcserver -1 -l0 -- " LOGOUTHOOKD_SOCKET "\n" + S6_LINUX_INIT_BINPREFIX "s6-linux-init-logouthookd\n") >= 0 ; } -static int sig_script (buffer *b, char c) +static int shutdownd_script (buffer *b, void *data) { size_t sabase = satmp.len ; + char fmt[UINT_FMT] ; if (!put_shebang(b) - || buffer_puts(b, "foreground { ") < 0 - || !string_quote(&satmp, tini_script, strlen(tini_script))) return 0 ; - if (buffer_put(b, satmp.s + sabase, satmp.len - sabase) < 0) goto err ; + || buffer_puts(b, S6_LINUX_INIT_BINPREFIX "s6-linux-init-shutdownd -b ") < 0) + || !string_quote(&satmp, bindir, strlen(bindir))) return 0 ; + if (buffer_puts(b, satmp.s + sabase) < 0) goto err ; satmp.len = sabase ; - if (buffer_puts(b, " }\ns6-svscanctl -") < 0 - || buffer_put(b, &c, 1) < 0 - || buffer_puts(b, " -- ") < 0 - || !string_quote(&satmp, slashrun, strlen(slashrun))) return 0 ; - if (buffer_put(b, satmp.s + sabase, satmp.len - sabase) < 0) goto err ; + if (buffer_puts(b, " -c ") < 0 + || !string_quote(&satmp, robase, strlen(robase))) return 0 ; + if (buffer_puts(b, satmp.s + sabase) < 0) goto err ; satmp.len = sabase ; - if (buffer_puts(b, "/service\n") < 0) return 0 ; + if (buffer_puts(b, " -g ") < 0 + || buffer_puts(b, fmt, uint_fmt(fmt, finalsleep)) < 0 + || buffer_puts(b, "\n") < 0) return 0 ; + (void)data ; return 1 ; - err: - satmp.len = sabase ; - return 0 ; -} -static int onlyexit (buffer *b, char c) -{ - size_t sabase = satmp.len ; - if (!put_shebang(b) - || buffer_puts(b, "foreground { redirfd -w 1 ") < 0 - || !string_quote(&satmp, slashrun, strlen(slashrun)) - || buffer_put(b, satmp.s + sabase, satmp.len - sabase) < 0 - || buffer_puts(b, "/" EXITCODENAME " s6-echo -- ") < 0 - || buffer_put(b, &c, 1) < 0 - || buffer_puts(b, " }\ns6-svscanctl -b ") < 0 - || buffer_put(b, satmp.s + sabase, satmp.len - sabase) < 0 - || buffer_puts(b, "/service\n") < 0) goto err ; - satmp.len = sabase ; - return 1 ; err: satmp.len = sabase ; - return 0 ; -} - -static int sigterm_script (buffer *b) -{ - return sig_script(b, 't') ; -} - -static int sighup_script (buffer *b) -{ - return in_namespace ? onlyexit(b, '0') : sig_script(b, 'h') ; -} - -static int sigquit_script (buffer *b) -{ - return sig_script(b, 'q') ; + return 0 ; } -static int sigint_script (buffer *b) +static int sig_script (buffer *b, void *data) { - return in_namespace ? onlyexit(b, '1') : sig_script(b, '6') ; -} - -static int sigusr1_script (buffer *b) -{ - return sig_script(b, '7') ; + char *option = data ; + return put_shebang(b) + && buffer_puts(b, S6_LINUX_INIT_BINPREFIX "s6-linux-init-shutdown -a ") >= 0 + && buffer_puts(b, option) >= 0 + && buffer_puts(" -- now\n") >= 0 ; } -static int sigusr2_script (buffer *b) +static int stage4_script (buffer *b, void *data) { - return sig_script(b, '0') ; + (void)data ; } static inline int stage1_script (buffer *b) { - size_t sabase = satmp.len, pos, pos2 ; - char fmt[UINT_OFMT] ; - if (!put_shebang(b) - || buffer_puts(b, bindir) < 0 - || buffer_puts(b, "/export PATH ") < 0 - || !string_quote(&satmp, initial_path, strlen(initial_path))) return 0 ; - if (buffer_put(b, satmp.s + sabase, satmp.len - sabase) < 0) goto err ; - satmp.len = sabase ; - if (buffer_put(b, "\n", 1) < 0 - || buffer_puts(b, bindir) < 0 - || buffer_puts(b, "/cd /\ns6-setsid -qb --\numask 0") < 0 - || buffer_put(b, fmt, uint_ofmt(fmt, initial_umask)) < 0 - || buffer_puts(b, "\nif { s6-echo -- ") < 0 - || !string_quote(&satmp, BANNER, sizeof(BANNER) - 1)) return 0 ; - if (buffer_put(b, satmp.s, satmp.len) < 0) goto err ; - satmp.len = sabase ; - if (buffer_puts(b, " }\nif { s6-mount -nwt tmpfs -o mode=0755 tmpfs ") < 0 - || !string_quote(&satmp, slashrun, strlen(slashrun))) return 0 ; - pos = satmp.len ; - if (buffer_put(b, satmp.s + sabase, pos - sabase) < 0 - || buffer_puts(b, " }\nif { s6-hiercopy ") < 0 + size_t sabase = satmp.len ; + if (!put_shebang_options(b, "-S0") + || buffer_puts(b, S6_LINUX_INIT_EXTBINPREFIX "s6-linux-init -c ") < 0 || !string_quote(&satmp, robase, strlen(robase))) return 0 ; - pos2 = satmp.len ; - if (buffer_put(b, satmp.s + pos, pos2 - pos) < 0 - || buffer_puts(b, "/run-image ") < 0 - || buffer_put(b, satmp.s + sabase, pos - sabase) < 0 - || buffer_puts(b, " }\n") < 0) goto err ; - if (slashdev_style == 1) + if (buffer_puts(b, satmp.s + sabase) < 0) goto err ; + satmp.len = sabase ; + { + char fmt[UINT_OFMT] ; + if (buffer_puts(b, " -m 00") < 0 + || buffer_put(b, fmt, uint_ofmt(fmt, initial_umask)) < 0) return 0 ; + } + if (redirect_stage2) { - if (buffer_puts(b, "if { s6-mount -nt devtmpfs dev /dev }\n") < 0) goto err ; + if (buffer_puts(b, " -r") < 0) return 0 ; + } + if (initial_path) + { + if (buffer_puts(b, " -p ") < 0 + || !string_quote(&satmp, initial_path, strlen(initial_path))) return 0 ; + if (buffer_puts(b, satmp.s + sabase) < 0) goto err ; + satmp.len = sabase ; } if (env_store) { - size_t base = satmp.len ; - if (!string_quote(&satmp, env_store, strlen(env_store))) return 0 ; - if (buffer_puts(b, "if { unexport PATH s6-dumpenv -- ") < 0 - || buffer_put(b, satmp.s + base, satmp.len - base) < 0 - || buffer_puts(b, " }\n") < 0) goto err ; - satmp.len = base ; + if (buffer_puts(b, " -s ") < 0 + || !string_quote(&satmp, env_store, strlen(env_store))) return 0 ; + if (buffer_puts(b, satmp.s + sabase) < 0) goto err ; + satmp.len = sabase ; + } + if (slashdev) + { + if (buffer_puts(b, " -d ") < 0 + || !string_quote(&satmp, slashdev, strlen(slashdev))) return 0 ; + if (buffer_puts(b, satmp.s + sabase) < 0) goto err ; + satmp.len = sabase ; } - if (buffer_puts(b, "emptyenv -p\ns6-envdir -I -- ") < 0 - || buffer_put(b, satmp.s + pos, pos2 - pos) < 0 - || buffer_puts(b, "/env\nredirfd -r 0 /dev/null\nredirfd -wnb 1 ") < 0 - || buffer_put(b, satmp.s + sabase, pos - sabase) < 0 - || buffer_puts(b, "/service/s6-svscan-log/fifo\nbackground\n{\n s6-setsid --\n redirfd -w 1 ") < 0 - || buffer_put(b, satmp.s + sabase, pos - sabase) < 0 - || buffer_puts(b, "/service/s6-svscan-log/fifo\n fdmove -c ") < 0 - || buffer_puts(b, redirect_stage2 ? "2 1" : "1 2") < 0 - || buffer_puts(b, "\n ") < 0 - || !string_quote(&satmp, init_script, strlen(init_script)) - || buffer_put(b, satmp.s + pos2, satmp.len - pos2) < 0 - || buffer_puts(b, "\n}\nunexport !\ncd ") < 0 - || buffer_put(b, satmp.s + sabase, pos - sabase) < 0 - || buffer_puts(b, "/service\nfdmove -c 2 1\ns6-svscan -st0\n") < 0) goto err ; + if (buffer_puts(b, "\n") < 0) return 0 ; return 1 ; + err: satmp.len = sabase ; return 0 ; @@ -307,13 +242,25 @@ static void auto_file (char const *base, char const *file, char const *s, unsign fn[clen] = '/' ; memcpy(fn + clen + 1, file, flen + 1) ; if (!openwritenclose_unsafe(fn, s, n) - || (executable && chmod(fn, 0755) < 0)) + || chmod(fn, executable ? 0755 : 0644) == -1)) { cleanup(base) ; strerr_diefu2sys(111, "write to ", fn) ; } } +static void auto_symlink (char const *base, char const *name, char const *target) +{ + size_t clen = strlen(base) ; + size_t dlen = strlen(name) ; + char fn[clen + dlen + 2] ; + memcpy(fn, base, clen) ; + fn[clen] = '/' ; + memcpy(fn + clen + 1, name, dlen + 1) ; + if (symlink(target, fn) == -1) + strerr_diefu4sys(111, "make a symlink named ", fn, " pointing to ", target) ; +} + static void auto_fifo (char const *base, char const *fifo) { size_t baselen = strlen(base) ; @@ -329,7 +276,7 @@ static void auto_fifo (char const *base, char const *fifo) } } -static void auto_script (char const *base, char const *file, writetobuf_func_t_ref scriptf) +static void auto_script (char const *base, char const *file, writetobuf_func_t_ref scriptf, void *data) { char buf[4096] ; buffer b ; @@ -347,25 +294,40 @@ static void auto_script (char const *base, char const *file, writetobuf_func_t_r strerr_diefu3sys(111, "open ", fn, " for script writing") ; } buffer_init(&b, &fd_writev, fd, buf, 4096) ; - if (!(*scriptf)(&b) || !buffer_flush(&b)) + if (!(*scriptf)(&b, data) || !buffer_flush(&b)) { cleanup(base) ; strerr_diefu2sys(111, "write to ", fn) ; } - close(fd) ; + fd_close(fd) ; } -static inline void make_env (char const *base, char const *modif, size_t modiflen) +static void auto_exec (char const *base, char const *name, char const *target) { - auto_dir(base, "env", 0, 0, 0755) ; + if (S6_LINUX_INIT_BINPREFIX[0] == '/') + { + size_t len = strlen(target) ; + char fn[sizeof(S6_LINUX_INIT_BINPREFIX) + len] = S6_LINUX_INIT_BINPREFIX ; + memcpy(fn + sizeof(S6_LINUX_INIT_BINPREFIX - 1, len + 1, target)) ; + auto_symlink(base, name, fn) ; + } + else + auto_script(base, name, &linewithargs_script, target) ; +} + +static void make_env (char const *base, char const *envname, char const *modif, size_t modiflen) +{ + size_t envnamelen = strlen(envname) ; + auto_dir(base, envname, 0, 0, 0755) ; while (modiflen) { size_t len = strlen(modif) ; size_t pos = byte_chr(modif, len, '=') ; - char fn[5 + pos] ; - memcpy(fn, "env/", 4) ; - memcpy(fn + 4, modif, pos) ; - fn[4 + pos] = 0 ; + char fn[envnamelen + pos + 2] ; + memcpy(fn, envname, envnamelen) ; + fn[envnamelen] = '/' ; + memcpy(fn + envnamelen + 1, modif, pos) ; + fn[envnamelen + 1 + pos] = 0 ; if (pos + 1 < len) auto_file(base, fn, modif + pos + 1, len - pos - 1, 0) ; else if (pos + 1 == len) auto_file(base, fn, "\n", 1, 0) ; @@ -377,41 +339,71 @@ static inline void make_env (char const *base, char const *modif, size_t modifle static inline void make_image (char const *base) { auto_dir(base, "run-image", 0, 0, 0755) ; - auto_dir(base, "run-image/uncaught-logs", uncaught_logs_uid, uncaught_logs_gid, 02700) ; - auto_dir(base, "run-image/service", 0, 0, 0755) ; - auto_dir(base, "run-image/service/.s6-svscan", 0, 0, 0755) ; - auto_script(base, "run-image/service/.s6-svscan/crash", &crash_script) ; - auto_script(base, "run-image/service/.s6-svscan/finish", &finish_script) ; - auto_script(base, "run-image/service/.s6-svscan/SIGTERM", &sigterm_script) ; - auto_script(base, "run-image/service/.s6-svscan/SIGHUP", &sighup_script) ; - auto_script(base, "run-image/service/.s6-svscan/SIGQUIT", &sigquit_script) ; - auto_script(base, "run-image/service/.s6-svscan/SIGINT", &sigint_script) ; - auto_script(base, "run-image/service/.s6-svscan/SIGUSR1", &sigusr1_script) ; - auto_script(base, "run-image/service/.s6-svscan/SIGUSR2", &sigusr2_script) ; - auto_dir(base, "run-image/service/s6-svscan-log", 0, 0, 0755) ; - auto_fifo(base, "run-image/service/s6-svscan-log/fifo") ; - auto_script(base, "run-image/service/s6-svscan-log/run", &s6_svscan_log_script) ; + auto_dir(base, "run-image/" UNCAUGHT_DIR, uncaught_logs_uid, uncaught_logs_gid, 02700) ; + auto_dir(base, "run-image/" SCANDIR, 0, 0, 0755) ; + auto_dir(base, "run-image/" SCANDIR "/.s6-svscan", 0, 0, 0755) ; + auto_script(base, "run-image/" SCANDIR "/.s6-svscan/crash", &death_script, "crashed") ; + auto_script(base, "run-image/" SCANDIR "/.s6-svscan/finish", &death_script, "exited") ; + auto_script(base, "run-image/" SCANDIR "/.s6-svscan/SIGTERM", &put_shebang_options, 0) ; + auto_script(base, "run-image/" SCANDIR "/.s6-svscan/SIGHUP", &put_shebang_options, 0) ; + auto_script(base, "run-image/" SCANDIR "/.s6-svscan/SIGQUIT", &put_shebang_options, 0) ; + auto_script(base, "run-image/" SCANDIR "/.s6-svscan/SIGINT", &sig_script, "-r") ; + auto_script(base, "run-image/" SCANDIR "/.s6-svscan/SIGUSR1", &sig_script, "-p") ; + auto_script(base, "run-image/" SCANDIR "/.s6-svscan/SIGUSR2", &sig_script, "-h") ; + auto_dir(base, "run-image/" SCANDIR "/" LOGGER_SERVICEDIR, 0, 0, 0755) ; + auto_fifo(base, "run-image/" SCANDIR "/" LOGGER_SERVICEDIR "/" LOGGER_FIFO) ; + auto_file(base, "run-image/" SCANDIR "/" LOGGER_SERVICEDIR "/notification-fd", "3\n", 2, 0) ; + auto_script(base, "run-image/" SCANDIR "/" LOGGER_SERVICEDIR "/run, &s6_svscan_log_script, 0) ; + auto_dir(base, "run-image/" SCANDIR "/" SHUTDOWND_SERVICEDIR, 0, 0, 0755) ; + auto_fifo(base, "run-image/" SCANDIR "/" SHUTDOWND_SERVICEDIR "/" SHUTDOWND_FIFO) ; + auto_script(base, "run-image/" SCANDIR "/" SHUTDOWND_SERVICEDIR "/run", &shutdownd_script, 0) ; + if (logouthookd) + { + auto_dir(base, "run-image/" SCANDIR "/" LOGOUTHOOKD_SERVICEDIR, 0, 0, 0755) ; + auto_file(base, "run-image/" SCANDIR "/" LOGOUTHOOKD_SERVICEDIR "/notification-fd", "1\n", 2, 0) ; + auto_script(base, "run-image/" SCANDIR "/" LOGOUTHOOKD_SERVICEDIR "/run", &logouthookd_script, 0) ; + } if (early_getty) { - auto_dir(base, "run-image/service/s6-linux-init-early-getty", 0, 0, 0755) ; - auto_script(base, "run-image/service/s6-linux-init-early-getty/run", &early_getty_script) ; + auto_dir(base, "run-image/" SCANDIR "/" EARLYGETTY_SERVICEDIR, 0, 0, 0755) ; + auto_script(base, "run-image/" SCANDIR "/" EARLYGETTY_SERVICEDIR "/run", &line_script, early_getty) ; } - auto_script(base, "init", &stage1_script) ; +} + +static inline void make_scripts (char const *base) +{ + auto_dir(base, "scripts", 0, 0, 0755) ; + auto_script(base, "scripts/runlevel", &put_shebang_options, 0) ; + auto_script(base, "scripts/" STAGE2, &put_shebang_options, 0) ; + auto_script(base, "scripts/" STAGE3, &put_shebang_options, 0) ; +} + +static inline void make_bins (char const *base) +{ + auto_dir(base, "bin", 0, 0, 0755) ; + auto_exec(base, "bin/halt", "s6-linux-init-halt") ; + auto_exec(base, "bin/reboot", "s6-linux-init-reboot") ; + auto_exec(base, "bin/poweroff", "s6-linux-init-poweroff") ; + auto_exec(base, "bin/shutdown", "s6-linux-init-shutdown") ; + auto_exec(base, "bin/telinit", "s6-linux-init-telinit") ; + auto_script(base, "bin/init", &stage1_script, 0) ; + auto_script(base, "bin/" STAGE4, &stage4_script, 0) ; } int main (int argc, char const *const *argv, char const *const *envp) { + stralloc saenv1 = STRALLOC_ZERO ; + stralloc saenv2 = STRALLOC_ZERO ; PROG = "s6-linux-init-maker" ; { subgetopt_t l = SUBGETOPT_ZERO ; for (;;) { - int opt = subgetopt_r(argc, argv, "c:l:b:u:g:UG:2:rZ:p:m:t:d:s:e:nq:", &l) ; + int opt = subgetopt_r(argc, argv, "c:b:u:g:UG:rLp:m:t:d:s:e:E:q:", &l) ; if (opt == -1) break ; switch (opt) { case 'c' : robase = l.arg ; break ; - case 'l' : slashrun = l.arg ; break ; case 'b' : bindir = l.arg ; break ; case 'u' : if (!uint0_scan(l.arg, &uncaught_logs_uid)) dieusage() ; break ; case 'g' : if (!uint0_scan(l.arg, &uncaught_logs_gid)) dieusage() ; break ; @@ -425,16 +417,15 @@ int main (int argc, char const *const *argv, char const *const *envp) if (!uint0_scan(x, &uncaught_logs_gid)) strerr_dieinvalid(100, "GID") ; } case 'G' : early_getty = l.arg ; break ; - case '2' : init_script = l.arg ; break ; case 'r' : redirect_stage2 = 1 ; break ; - case 'Z' : tini_script = l.arg ; break ; + case 'L' : logouthookd = 1 ; break ; case 'p' : initial_path = l.arg ; break ; case 'm' : if (!uint0_oscan(l.arg, &initial_umask)) dieusage() ; break ; case 't' : if (!uint0_scan(l.arg, ×tamp_style)) dieusage() ; break ; - case 'd' : if (!uint0_scan(l.arg, &slashdev_style)) dieusage() ; break ; + case 'd' : slashdev = l.arg ; break ; case 's' : env_store = l.arg ; break ; - case 'e' : if (!stralloc_catb(&satmp, l.arg, strlen(l.arg) + 1)) dienomem() ; break ; - case 'n' : in_namespace = 1 ; break ; + case 'e' : if (!stralloc_catb(&saenv1, l.arg, strlen(l.arg) + 1)) dienomem() ; break ; + case 'E' : if (!stralloc_catb(&saenv2, l.arg, strlen(l.arg) + 1)) dienomem() ; break ; case 'q' : if (!uint0_scan(l.arg, &finalsleep)) dieusage() ; break ; default : dieusage() ; } @@ -445,24 +436,27 @@ int main (int argc, char const *const *argv, char const *const *envp) if (robase[0] != '/') strerr_dief3x(100, "base directory ", robase, " is not absolute") ; - if (slashrun[0] != '/') - strerr_dief3x(100, "tmpfs directory ", slashrun, " is not absolute") ; if (bindir[0] != '/') strerr_dief3x(100, "initial location for binaries ", bindir, " is not absolute") ; if (init_script[0] != '/') strerr_dief3x(100, "stage 2 script location ", init_script, " is not absolute") ; if (tini_script[0] != '/') strerr_dief3x(100, "shutdown script location ", tini_script, " is not absolute") ; + if (slashdev && slashdev[0] != '/') + strerr_dief3x(100, "devtmpfs mounting location ", slashdev, " is not absolute") ; if (timestamp_style > 3) strerr_dief1x(100, "-t timestamp_style must be 0, 1, 2 or 3") ; - if (slashdev_style > 2) - strerr_dief1x(100, "-d dev_style must be 0, 1 or 2") ; + umask(0) ; if (mkdir(argv[0], 0755) < 0) strerr_diefu2sys(111, "mkdir ", argv[0]) ; - make_env(argv[0], satmp.s, satmp.len) ; - satmp.len = 0 ; - make_image(argv[0]) ; + make_env(argv[0], ENVSTAGE2, saenv2.s, saenv2.len) ; + stralloc_free(&saenv2) ; + make_env(argv[0], ENVSTAGE1, saenv1.s, saenv1.len) ; + saenv1.len = 0 ; + make_image(argv[0], &saenv1) ; + make_scripts(argv[0]) ; + make_bins(argv[0]) ; return 0 ; } diff --git a/src/init/s6-linux-init.c b/src/init/s6-linux-init.c new file mode 100644 index 0000000..6758682 --- /dev/null +++ b/src/init/s6-linux-init.c @@ -0,0 +1,168 @@ +/* ISC license. */ + +#include <fcntl.h> +#include <string.h> +#include <unistd.h> +#include <stdlib.h> +#include <errno.h> +#include <sys/stat.h> +#include <sys/mount.h> + +#include <skalibs/types.h> +#include <skalibs/allreadwrite.h> +#include <skalibs/sgetopt.h> +#include <skalibs/strerr2.h> +#include <skalibs/stralloc.h> +#include <skalibs/env.h> +#include <skalibs/djbunix.h> + +#include <s6/config.h> + +#include <s6-linux-init/config.h> + +#include "defaults.h" +#include "initctl.h" + +#define USAGE "s6-linux-init [ -r ] [ -c basedir ] [ -p initpath ] [ -s envdumpdir ] [ -m umask ] [ -d devtmpfs ]" +#define dieusage() strerr_dieusage(100, USAGE) + +#define BANNER "\n s6-linux-init version " S6_LINUX_INIT_VERSION "\n\n" + +static inline void run_stage2 (char const *basedir, char const **argv, unsigned int argc, char const *const *envp, size_t envlen, stralloc *envmodifs, int redirect) +{ + size_t dirlen = strlen(basedir) ; + char const *childargv[argc + 2] ; + char fn[dirlen + 1 + (sizeof(ENVSTAGE2) > sizeof(STAGE2) ? sizeof(ENVSTAGE2) : sizeof(STAGE2))] ; + PROG = "s6-linux-init (child)" ; + argv[0] = PROG ; + memcpy(fn, basedir, dirlen) ; + fn[dirlen] = '/' ; + memcpy(fn + dirlen + 1, ENVSTAGE2, sizeof(ENVSTAGE2)) ; + if (envdir(fn, envmodifs) == -1) + strerr_warnwu2sys("envdir ", fn) ; + memcpy(fn + dirlen + 1, STAGE2, sizeof(STAGE2)) ; + childargv[0] = fn ; + for (unsigned int i = 0 ; i < argc ; i++) + childargv[i+1] = argv[i] ; + childargv[argc + 1] = 0 ; + setsid() ; + fd_close(1) ; + if (open(LOGFIFO, O_WRONLY) != 1) /* blocks */ + strerr_diefu1sys(111, "open " LOGFIFO " for writing") ; + if (fd_copy(1 + redirect, 2 - redirect) == -1) + strerr_diefu1sys(111, "redirect output file descriptor") ; + xpathexec_r(childargv, envp, envlen, envmodifs->s, envmodifs->len) ; +} + +int main (int argc, char const **argv, char const *const *envp) +{ + mode_t mask = 0022 ; + char const *basedir = BASEDIR ; + char const *path = INITPATH ; + char const *slashdev = 0 ; + char const *envdumpdir = 0 ; + stralloc envmodifs = STRALLOC_ZERO ; + int redirect = 0 ; + PROG = "s6-linux-init" ; + + if (getpid() != 1) + { + argv[0] = S6_LINUX_INIT_BINPREFIX "/s6-linux-init-telinit" ; + pathexec_run(argv[0], argv, envp) ; + strerr_dieexec(111, argv[0]) ; + } + + { + subgetopt_t l = SUBGETOPT_ZERO ; + for (;;) + { + int opt = subgetopt_r(argc, argv, "rc:p:s:m:d:", &l) ; + if (opt == -1) break ; + switch (opt) + { + case 'r' : redirect = 1 ; break ; + case 'c' : basedir = l.arg ; break ; + case 'p' : path = l.arg ; break ; + case 's' : envdumpdir = l.arg ; break ; + case 'm' : if (!uint0_oscan(l.arg, &mask)) dieusage() ; break ; + case 'd' : slashdev = l.arg ; break ; + default : dieusage() ; + } + } + argc -= l.ind ; argv += l.ind ; + } + + allwrite(1, BANNER, sizeof(BANNER) - 1) ; + if (chdir("/") == -1) strerr_diefu1sys(111, "chdir to /") ; + umask(mask) ; + setpgid(0, 0) ; + fd_close(0) ; + if (slashdev) + { + fd_close(1) ; + fd_close(2) ; + if (mount("dev", slashdev, "devtmpfs", MS_NOSUID | MS_NOEXEC, "") == -1) + { + int e = errno ; + open("/dev/null", O_RDONLY) ; + open("/dev/console", O_WRONLY) ; + fd_copy(2, 1) ; + errno = e ; + strerr_diefu2sys(111, "mount ", slashdev) ; + } + 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 (umount(S6_LINUX_INIT_TMPFS) == -1) + { + if (errno != EINVAL) + strerr_warnwu1sys("umount " S6_LINUX_INIT_TMPFS) ; + } + if (mount("tmpfs", S6_LINUX_INIT_TMPFS, "tmpfs", MS_NODEV | MS_NOSUID, "mode=0755") == -1) + strerr_diefu1sys(111, "mount tmpfs on " S6_LINUX_INIT_TMPFS) ; + { + size_t dirlen = strlen(basedir) ; + char fn[dirlen + 1 + (sizeof(RUNIMAGE) > sizeof(ENVSTAGE1) ? sizeof(RUNIMAGE) : sizeof(ENVSTAGE1))] ; + memcpy(fn, basedir, dirlen) ; + fn[dirlen] = '/' ; + memcpy(fn + dirlen + 1, RUNIMAGE, sizeof(RUNIMAGE)) ; + if (!hiercopy(fn, S6_LINUX_INIT_TMPFS)) + strerr_diefu3sys(111, "copy ", fn, " to " S6_LINUX_INIT_TMPFS) ; + memcpy(fn + dirlen + 1, ENVSTAGE1, sizeof(ENVSTAGE1)) ; + if (envdir(fn, &envmodifs) == -1) + strerr_warnwu2sys("envdir ", fn) ; + } + if (envdumpdir && !env_dump(envdumpdir, 0700, envp)) + strerr_warnwu2sys("dump kernel environment to ", envdumpdir) ; + { + int fdr = open_read(LOGFIFO) ; + if (fdr == -1) strerr_diefu1sys(111, "open " LOGFIFO) ; + fd_close(1) ; + 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 ; + char pathvar[6 + pathlen] ; + if (path) + { + if (setenv("PATH", path, 1) == -1) + strerr_diefu1sys(111, "set initial PATH") ; + memcpy(pathvar, "PATH=", 5) ; + memcpy(pathvar + 5, path, pathlen + 1) ; + newenvp[0] = pathvar ; + } + pid = fork() ; + if (pid == -1) strerr_diefu1sys(111, "fork") ; + if (!pid) run_stage2(basedir, argv, argc, newenvp, !!path, &envmodifs, redirect) ; + if (fd_copy(2, 1) == -1) + strerr_diefu1sys(111, "redirect output file descriptor") ; + xpathexec_r(newargv, newenvp, !!path, envmodifs.s, envmodifs.len) ; + } +} diff --git a/src/init/s6-poweroff.c b/src/init/s6-poweroff.c deleted file mode 100644 index f9f7747..0000000 --- a/src/init/s6-poweroff.c +++ /dev/null @@ -1,7 +0,0 @@ -/* ISC license. */ - -#undef PROGNAME -#define PROGNAME "s6-poweroff" -#undef WHATDEFAULT -#define WHATDEFAULT 2 -#include "hpr.c" diff --git a/src/init/s6-reboot.c b/src/init/s6-reboot.c deleted file mode 100644 index 0bf8785..0000000 --- a/src/init/s6-reboot.c +++ /dev/null @@ -1,7 +0,0 @@ -/* ISC license. */ - -#undef PROGNAME -#define PROGNAME "s6-reboot" -#undef WHATDEFAULT -#define WHATDEFAULT 3 -#include "hpr.c" |