From 73cff35173dd75b0254ae7cf9098c32822859de0 Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Tue, 16 Apr 2019 20:37:01 +0000 Subject: It builds! Tomorrow: skeletons for stage2, stage3 and runlevel scripts. --- src/include-local/defaults.h | 1 - src/include-local/initctl.h | 4 + src/init/deps-exe/s6-linux-init-telinit | 1 + src/init/s6-linux-init-maker.c | 148 ++++++------ src/init/s6-linux-init-telinit.c | 72 ++++++ src/logout-hook/deps-exe/s6-linux-init-logouthookd | 4 - src/logout-hook/s6-linux-init-logouthookd.c | 69 ------ src/misc/deps-exe/s6-linux-init-echo | 1 + src/misc/deps-exe/s6-linux-init-logouthookd | 4 + src/misc/deps-exe/s6-linux-init-umountall | 1 + src/misc/s6-linux-init-echo.c | 39 +++ src/misc/s6-linux-init-logouthookd.c | 69 ++++++ src/misc/s6-linux-init-umountall.c | 70 ++++++ src/shutdown/deps-exe/s6-linux-init-halt | 5 + src/shutdown/deps-exe/s6-linux-init-poweroff | 5 + src/shutdown/deps-exe/s6-linux-init-reboot | 5 + src/shutdown/deps-exe/s6-linux-init-shutdown | 5 + src/shutdown/deps-exe/s6-linux-init-shutdownd | 4 + src/shutdown/deps-lib/hpr | 2 + src/shutdown/hpr.c | 104 ++++++++ src/shutdown/hpr.h | 20 ++ src/shutdown/hpr_shutdown.c | 16 ++ src/shutdown/hpr_wall.c | 47 ++++ src/shutdown/s6-linux-init-halt.c | 7 + src/shutdown/s6-linux-init-poweroff.c | 7 + src/shutdown/s6-linux-init-reboot.c | 7 + src/shutdown/s6-linux-init-shutdown.c | 268 +++++++++++++++++++++ src/shutdown/s6-linux-init-shutdownd.c | 228 ++++++++++++++++++ src/state/deps-exe/s6-linux-init-halt | 5 - src/state/deps-exe/s6-linux-init-poweroff | 5 - src/state/deps-exe/s6-linux-init-reboot | 5 - src/state/deps-exe/s6-linux-init-shutdown | 5 - src/state/deps-exe/s6-linux-init-shutdownd | 4 - src/state/deps-exe/s6-linux-init-single | 1 - src/state/deps-exe/s6-linux-init-telinit | 2 - src/state/deps-lib/hpr | 4 - src/state/hpr.c | 102 -------- src/state/hpr.h | 22 -- src/state/hpr_fmttime.c | 52 ---- src/state/hpr_shutdown.c | 16 -- src/state/hpr_wall.c | 47 ---- src/state/hpr_wall_seconds.c | 12 - src/state/s6-linux-init-halt.c | 7 - src/state/s6-linux-init-poweroff.c | 7 - src/state/s6-linux-init-reboot.c | 7 - src/state/s6-linux-init-shutdown.c | 268 --------------------- src/state/s6-linux-init-shutdownd.c | 241 ------------------ src/state/s6-linux-init-single.c | 54 ----- src/state/s6-linux-init-telinit.c | 42 ---- 49 files changed, 1070 insertions(+), 1051 deletions(-) create mode 100644 src/init/deps-exe/s6-linux-init-telinit create mode 100644 src/init/s6-linux-init-telinit.c delete mode 100644 src/logout-hook/deps-exe/s6-linux-init-logouthookd delete mode 100644 src/logout-hook/s6-linux-init-logouthookd.c create mode 100644 src/misc/deps-exe/s6-linux-init-echo create mode 100644 src/misc/deps-exe/s6-linux-init-logouthookd create mode 100644 src/misc/deps-exe/s6-linux-init-umountall create mode 100644 src/misc/s6-linux-init-echo.c create mode 100644 src/misc/s6-linux-init-logouthookd.c create mode 100644 src/misc/s6-linux-init-umountall.c create mode 100644 src/shutdown/deps-exe/s6-linux-init-halt create mode 100644 src/shutdown/deps-exe/s6-linux-init-poweroff create mode 100644 src/shutdown/deps-exe/s6-linux-init-reboot create mode 100644 src/shutdown/deps-exe/s6-linux-init-shutdown create mode 100644 src/shutdown/deps-exe/s6-linux-init-shutdownd create mode 100644 src/shutdown/deps-lib/hpr create mode 100644 src/shutdown/hpr.c create mode 100644 src/shutdown/hpr.h create mode 100644 src/shutdown/hpr_shutdown.c create mode 100644 src/shutdown/hpr_wall.c create mode 100644 src/shutdown/s6-linux-init-halt.c create mode 100644 src/shutdown/s6-linux-init-poweroff.c create mode 100644 src/shutdown/s6-linux-init-reboot.c create mode 100644 src/shutdown/s6-linux-init-shutdown.c create mode 100644 src/shutdown/s6-linux-init-shutdownd.c delete mode 100644 src/state/deps-exe/s6-linux-init-halt delete mode 100644 src/state/deps-exe/s6-linux-init-poweroff delete mode 100644 src/state/deps-exe/s6-linux-init-reboot delete mode 100644 src/state/deps-exe/s6-linux-init-shutdown delete mode 100644 src/state/deps-exe/s6-linux-init-shutdownd delete mode 100644 src/state/deps-exe/s6-linux-init-single delete mode 100644 src/state/deps-exe/s6-linux-init-telinit delete mode 100644 src/state/deps-lib/hpr delete mode 100644 src/state/hpr.c delete mode 100644 src/state/hpr.h delete mode 100644 src/state/hpr_fmttime.c delete mode 100644 src/state/hpr_shutdown.c delete mode 100644 src/state/hpr_wall.c delete mode 100644 src/state/hpr_wall_seconds.c delete mode 100644 src/state/s6-linux-init-halt.c delete mode 100644 src/state/s6-linux-init-poweroff.c delete mode 100644 src/state/s6-linux-init-reboot.c delete mode 100644 src/state/s6-linux-init-shutdown.c delete mode 100644 src/state/s6-linux-init-shutdownd.c delete mode 100644 src/state/s6-linux-init-single.c delete mode 100644 src/state/s6-linux-init-telinit.c (limited to 'src') diff --git a/src/include-local/defaults.h b/src/include-local/defaults.h index dc36e0f..a22693b 100644 --- a/src/include-local/defaults.h +++ b/src/include-local/defaults.h @@ -3,7 +3,6 @@ #ifndef S6_LINUX_INIT_DEFAULTS_H #define S6_LINUX_INIT_DEFAULTS_H -#define BINDIR "/bin" #define BASEDIR "/etc/s6-linux-init" #define INITPATH "/usr/bin:/bin" diff --git a/src/include-local/initctl.h b/src/include-local/initctl.h index 4102640..6610de8 100644 --- a/src/include-local/initctl.h +++ b/src/include-local/initctl.h @@ -11,6 +11,10 @@ #define SHUTDOWND_FIFO "fifo" #define INITCTL S6_LINUX_INIT_TMPFS "/" SCANDIR "/" SHUTDOWND_SERVICEDIR "/" SHUTDOWND_FIFO +#define RUNLEVELD_SERVICEDIR "s6-linux-init-runleveld" +#define RUNLEVELD_SOCKET "s" +#define RUNLEVELD_PATH S6_LINUX_INIT_TMPFS "/" SCANDIR "/" RUNLEVELD_SERVICEDIR "/" RUNLEVELD_SOCKET + #define LOGOUTHOOKD_SERVICEDIR "s6-linux-init-logouthookd" #define LOGOUTHOOKD_SOCKET "s" #define LOGOUTHOOKD_PATH S6_LINUX_INIT_TMPFS "/" SCANDIR "/" LOGOUTHOOKD_SERVICEDIR "/" LOGOUTHOOKD_SOCKET diff --git a/src/init/deps-exe/s6-linux-init-telinit b/src/init/deps-exe/s6-linux-init-telinit new file mode 100644 index 0000000..e7187fe --- /dev/null +++ b/src/init/deps-exe/s6-linux-init-telinit @@ -0,0 +1 @@ +-lskarnet diff --git a/src/init/s6-linux-init-maker.c b/src/init/s6-linux-init-maker.c index ede9a52..2470c4d 100644 --- a/src/init/s6-linux-init-maker.c +++ b/src/init/s6-linux-init-maker.c @@ -16,29 +16,21 @@ #include #include +#include + #include #include #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 ] [ -1 ] [ -L ] [ -p initial_path ] [ -m initial_umask ] [ -t timestamp_style ] [ -d slashdev ] [ -s env_store ] [ -e initial_envvar ... ] [ -q default_grace_time ] dir" +#define USAGE "s6-linux-init-maker [ -c basedir ] [ -u log_uid -g log_gid | -U ] [ -G early_getty_cmd ] [ -1 ] [ -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 UNCAUGHT_DIR "uncaught-logs" -#define EXITCODENAME "file\\ created\\ by\\ s6-linux-init,\\ storing\\ a\\ container's\\ exit\\ code" - -#define CRASH_SCRIPT \ -"redirfd -r 0 /dev/console\n" \ -"redirfd -w 1 /dev/console\n" \ -"fdmove -c 2 1\n" \ -"foreground { s6-echo -- " \ -"\"s6-svscan finished. Dropping to an interactive shell.\" }\n" \ -"/bin/sh -i\n" 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 ; @@ -51,65 +43,59 @@ static unsigned int finalsleep = 3000 ; static int console = 0 ; static int logouthookd = 0 ; -typedef int writetobuf_func_t (buffer *, void *) ; +typedef int writetobuf_func_t (buffer *, char const *) ; typedef writetobuf_func_t *writetobuf_func_t_ref ; #define put_shebang(b) put_shebang_options((b), "-P") -static int put_shebang_options (buffer *b, void *data) +static int put_shebang_options (buffer *b, char const *options) { - char *options = data ; - return buffer_puts(b, "#!") >= 0 - && buffer_puts(b, bindir) >= 0 - && buffer_puts(b, "/execlineb") >= 0 + return buffer_puts(b, "#!" EXECLINE_SHEBANGPREFIX "/execlineb") >= 0 && (!options || !options[0] || buffer_puts(b, " ") >= 0) && buffer_puts(b, options) >= 0 && buffer_puts(b, "\n\n") >= 0 ; } -static int line_script (buffer *b, void *data) +static int line_script (buffer *b, char const *line) { - char *line = data ; return put_shebang(b) && buffer_puts(b, line) >= 0 && buffer_put(b, "\n", 1) >= 0 ; } -static int linewithargs_script (buffer *b, void *data) +static int linewithargs_script (buffer *b, char const *line) { - 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) +static int death_script (buffer *b, char const *s) { - char *s = data ; return put_shebang(b) && 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 + EXECLINE_EXTBINPREFIX "redirfd -r 0 /dev/console\n" + EXECLINE_EXTBINPREFIX "redirfd -w 1 /dev/console\n" + EXECLINE_EXTBINPREFIX "fdmove -c 2 1\n" + EXECLINE_EXTBINPREFIX "foreground { " + S6_LINUX_INIT_LIBEXECPREFIX "s6-linux-init-echo -- \"s6-svscan ") >= 0 && buffer_puts(b, s) >= 0 && buffer_puts(b, ". Dropping to an interactive shell.\" }\n" - "/bin/sh -i\n") >= 0 + "/bin/sh -i\n") >= 0 ; } -static int s6_svscan_log_script (buffer *b, void *data) +static int s6_svscan_log_script (buffer *b, char const *data) { char fmt[UINT64_FMT] ; - (void)data ; if (!put_shebang(b) || buffer_puts(b, - "redirfd -w 2 /dev/console\n" - "redirfd -w 1 /dev/") < 0 + EXECLINE_EXTBINPREFIX "redirfd -w 2 /dev/console\n" + EXECLINE_EXTBINPREFIX "redirfd -w 1 /dev/") < 0 || buffer_puts(b, console ? "console" : "null") < 0 || buffer_puts(b, "\n" - "redirfd -rnb 0 " LOGGER_FIFO "\n" - "s6-applyuidgid -u ") < 0 + EXECLINE_EXTBINPREFIX "redirfd -rnb 0 " LOGGER_FIFO "\n" + S6_EXTBINPREFIX "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 @@ -126,33 +112,30 @@ static int s6_svscan_log_script (buffer *b, void *data) || timestamp_style & 2 && buffer_puts(b, "T ") < 0 || buffer_puts(b, S6_LINUX_INIT_TMPFS "/" UNCAUGHT_DIR "\n") < 0) return 0 ; + (void)data ; return 1 ; } -static int logouthookd_script (buffer *b, void *data) +static int logouthookd_script (buffer *b, char const *data) { (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 ; + S6_LINUX_INIT_LIBEXECPREFIX "s6-linux-init-logouthookd\n") >= 0 ; } -static int shutdownd_script (buffer *b, void *data) +static int shutdownd_script (buffer *b, char const *data) { size_t sabase = satmp.len ; char fmt[UINT_FMT] ; if (!put_shebang(b) - || 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, " -c ") < 0 + || buffer_puts(b, S6_LINUX_INIT_LIBEXECPREFIX "s6-linux-init-shutdownd -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, " -g ") < 0 - || buffer_puts(b, fmt, uint_fmt(fmt, finalsleep)) < 0 + || buffer_put(b, fmt, uint_fmt(fmt, finalsleep)) < 0 || buffer_puts(b, "\n") < 0) return 0 ; (void)data ; return 1 ; @@ -162,25 +145,42 @@ static int shutdownd_script (buffer *b, void *data) return 0 ; } -static int sig_script (buffer *b, void *data) +static int runleveld_script (buffer *b, char const *data) { - 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 ; + size_t sabase = satmp.len ; + if (!put_shebang(b) + || buffer_puts(b, + EXECLINE_EXTBINPREFIX "fdmove -c 2 1\n" + EXECLINE_EXTBINPREFIX "fdmove 1 3\n" + S6_EXTBINPREFIX "s6-ipcserver-socketbinder -- " RUNLEVELD_SOCKET "\n" + S6_EXTBINPREFIX "s6-ipcserverd -1 -c1 --\n" + S6_EXTBINPREFIX "s6-ipcserver-access -v0 -E -l0 -i data/rules --\n" + S6_EXTBINPREFIX "s6-sudod -0 -1 -2 -t 30000 --\n") < 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, "/scripts/runlevel\n") < 0) return 0 ; + (void)data ; + return 1 ; + + err: + satmp.len = sabase ; + return 0 ; } -static int stage4_script (buffer *b, void *data) +static int sig_script (buffer *b, char const *option) { - (void)data ; + return put_shebang(b) + && buffer_puts(b, S6_LINUX_INIT_LIBEXECPREFIX "s6-linux-init-shutdown -a ") >= 0 + && buffer_puts(b, option) >= 0 + && buffer_puts(b, " -- now\n") >= 0 ; } -static inline int stage1_script (buffer *b) +static inline int stage1_script (buffer *b, char const *data) { size_t sabase = satmp.len ; if (!put_shebang_options(b, "-S0") - || buffer_puts(b, S6_LINUX_INIT_EXTBINPREFIX "s6-linux-init -c ") < 0 + || buffer_puts(b, S6_LINUX_INIT_LIBEXECPREFIX "s6-linux-init -c ") < 0 || !string_quote(&satmp, robase, strlen(robase))) return 0 ; if (buffer_puts(b, satmp.s + sabase) < 0) goto err ; satmp.len = sabase ; @@ -211,6 +211,7 @@ static inline int stage1_script (buffer *b) satmp.len = sabase ; } if (buffer_puts(b, "\n") < 0) return 0 ; + (void)data ; return 1 ; err: @@ -250,7 +251,7 @@ 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) - || chmod(fn, executable ? 0755 : 0644) == -1)) + || chmod(fn, executable ? 0755 : 0644) == -1) { cleanup(base) ; strerr_diefu2sys(111, "write to ", fn) ; @@ -284,7 +285,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, void *data) +static void auto_script (char const *base, char const *file, writetobuf_func_t_ref scriptf, char const *data) { char buf[4096] ; buffer b ; @@ -312,11 +313,12 @@ static void auto_script (char const *base, char const *file, writetobuf_func_t_r static void auto_exec (char const *base, char const *name, char const *target) { - if (S6_LINUX_INIT_BINPREFIX[0] == '/') + if (S6_LINUX_INIT_LIBEXECPREFIX[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)) ; + char fn[sizeof(S6_LINUX_INIT_LIBEXECPREFIX) + len] ; + memcpy(fn, S6_LINUX_INIT_LIBEXECPREFIX, sizeof(S6_LINUX_INIT_LIBEXECPREFIX) - 1) ; + memcpy(fn + sizeof(S6_LINUX_INIT_LIBEXECPREFIX) - 1, target, len + 1) ; auto_symlink(base, name, fn) ; } else @@ -358,19 +360,35 @@ static inline void make_image (char const *base) 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_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) ; + + auto_dir(base, "run-image/" SCANDIR "/" RUNLEVELD_SERVICEDIR, 0, 0, 0755) ; + auto_dir(base, "run-image/" SCANDIR "/" RUNLEVELD_SERVICEDIR "/data", 0, 0, 0755) ; + auto_dir(base, "run-image/" SCANDIR "/" RUNLEVELD_SERVICEDIR "/data/rules", 0, 0, 0755) ; + auto_dir(base, "run-image/" SCANDIR "/" RUNLEVELD_SERVICEDIR "/data/rules/gid", 0, 0, 0755) ; + auto_dir(base, "run-image/" SCANDIR "/" RUNLEVELD_SERVICEDIR "/data/rules/gid/0", 0, 0, 0755) ; + auto_file(base, "run-image/" SCANDIR "/" RUNLEVELD_SERVICEDIR "/data/rules/gid/0/allow", "", 0, 0) ; + auto_dir(base, "run-image/" SCANDIR "/" RUNLEVELD_SERVICEDIR "/data/rules/uid", 0, 0, 0755) ; + auto_dir(base, "run-image/" SCANDIR "/" RUNLEVELD_SERVICEDIR "/data/rules/uid/0", 0, 0, 0755) ; + auto_file(base, "run-image/" SCANDIR "/" RUNLEVELD_SERVICEDIR "/data/rules/uid/0/allow", "", 0, 0) ; + auto_file(base, "run-image/" SCANDIR "/" RUNLEVELD_SERVICEDIR "/notification-fd", "3\n", 2, 0) ; + auto_script(base, "run-image/" SCANDIR "/" RUNLEVELD_SERVICEDIR "/run", &runleveld_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/" SCANDIR "/" EARLYGETTY_SERVICEDIR, 0, 0, 0755) ; @@ -395,7 +413,6 @@ static inline void make_bins (char const *base) 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) @@ -407,12 +424,11 @@ int main (int argc, char const *const *argv, char const *const *envp) subgetopt_t l = SUBGETOPT_ZERO ; for (;;) { - int opt = subgetopt_r(argc, argv, "c:b:u:g:UG:1Lp:m:t:d:s:e:E:q:", &l) ; + int opt = subgetopt_r(argc, argv, "c:u:g:UG:1Lp:m:t:d:s:e:E:q:", &l) ; if (opt == -1) break ; switch (opt) { case 'c' : robase = 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 ; case 'U' : @@ -444,12 +460,6 @@ 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 (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) @@ -463,7 +473,7 @@ int main (int argc, char const *const *argv, char const *const *envp) stralloc_free(&saenv2) ; make_env(argv[0], ENVSTAGE1, saenv1.s, saenv1.len) ; saenv1.len = 0 ; - make_image(argv[0], &saenv1) ; + make_image(argv[0]) ; make_scripts(argv[0]) ; make_bins(argv[0]) ; return 0 ; diff --git a/src/init/s6-linux-init-telinit.c b/src/init/s6-linux-init-telinit.c new file mode 100644 index 0000000..8678709 --- /dev/null +++ b/src/init/s6-linux-init-telinit.c @@ -0,0 +1,72 @@ +/* ISC license. */ + +#include +#include + +#include +#include +#include +#include + +#include + +#include +#include "initctl.h" + +#define USAGE "s6-linux-init-telinit runlevel" +#define dieusage() strerr_dieusage(100, USAGE) + +int main (int argc, char const *const *argv, char const *const *envp) +{ + char const *newargv[8] = { S6_EXTBINPREFIX "s6-sudo", "-e", "-T", "3600000", "--", RUNLEVELD_PATH, 0, 0 } ; + PROG = "s6-linux-init-telinit" ; + { + subgetopt_t l = SUBGETOPT_ZERO ; + for (;;) + { + int opt = subgetopt_r(argc, argv, "c:p:s:m:d:", &l) ; + if (opt == -1) break ; + switch (opt) + { + case 'c' : /* s6-linux-init may be called with these options, don't choke on them */ + case 'p' : + case 's' : + case 'm' : + case 'd' : break ; + default : dieusage() ; + } + } + argc -= l.ind ; argv += l.ind ; + } + + if (!argc) dieusage() ; + newargv[6] = argv[0] ; + + + /* specialcase 0 and 6: fork runlevel call then exec shutdown, instead of execing runlevel call */ + + if (!strcmp(argv[0], "0") || !strcmp(argv[0], "6")) + { + int wstat ; + pid_t pid = child_spawn0(newargv[0], newargv, envp) ; + if (!pid) strerr_diefu2sys(111, "spawn ", newargv[0]) ; + if (wait_pid(pid, &wstat) < 0) strerr_diefu1sys(111, "wait_pid") ; + if (WIFSIGNALED(wstat)) + { + char fmt[UINT_FMT] ; + fmt[uint_fmt(fmt, WTERMSIG(wstat))] = 0 ; + strerr_dief3x(wait_estatus(wstat), newargv[0], " crashed with signal ", fmt) ; + } + else if (WEXITSTATUS(wstat)) + { + char fmt[UINT_FMT] ; + fmt[uint_fmt(fmt, WEXITSTATUS(wstat))] = 0 ; + strerr_dief3x(wait_estatus(wstat), newargv[0], " died with exitcode ", fmt) ; + } + + newargv[0] = argv[0][0] == '6' ? S6_LINUX_INIT_BINPREFIX "reboot" : S6_LINUX_INIT_BINPREFIX "poweroff" ; + newargv[1] = 0 ; + } + + xpathexec_run(newargv[0], newargv, envp) ; +} diff --git a/src/logout-hook/deps-exe/s6-linux-init-logouthookd b/src/logout-hook/deps-exe/s6-linux-init-logouthookd deleted file mode 100644 index 5a7d2fd..0000000 --- a/src/logout-hook/deps-exe/s6-linux-init-logouthookd +++ /dev/null @@ -1,4 +0,0 @@ -${LIBUTMPS} --lskarnet -${TAINNOW_LIB} -${SOCKET_LIB} diff --git a/src/logout-hook/s6-linux-init-logouthookd.c b/src/logout-hook/s6-linux-init-logouthookd.c deleted file mode 100644 index cc542ba..0000000 --- a/src/logout-hook/s6-linux-init-logouthookd.c +++ /dev/null @@ -1,69 +0,0 @@ -/* ISC license. */ - -#include - -#ifndef SKALIBS_HASSOPEERCRED -#error "The SO_PEERCRED option to getsockopt() is required." -#endif - -#include - -#include -#include -#include -#include -#include -#include - -#include -#include - -#ifndef UT_NAMESIZE -#define UT_NAMESIZE 32 -#endif - -#ifndef UT_HOSTSIZE -#define UT_HOSTSIZE 256 -#endif - -int main (void) -{ - struct utmpx *utx ; - struct ucred client ; - socklen_t len = sizeof(client) ; - ssize_t r ; - char c ; - - close(1) ; - if (getsockopt(0, SOL_SOCKET, SO_PEERCRED, &client, &len) == -1) - strerr_diefu1sys(111, "getsockopt") ; - - /* Only take connections from root. */ - if (client.uid) return 1 ; - - /* Wait for the client to die. */ - r = fd_read(0, &c, 1) ; - if (r < 0) strerr_diefu1sys(111, "read from stdin") ; - if (r) strerr_dief1x(2, "client attempted to write") ; - - /* Clean up utmpx record for the client's pid, then exit. */ - for (;;) - { - errno = 0 ; - utx = getutxent() ; - if (!utx) break ; - if (utx->ut_pid == client.pid) goto gotit ; - } - if (errno) strerr_diefu1sys(111, "getutxent") ; - return 0 ; - - gotit: - utx->ut_type = DEAD_PROCESS ; - memset(utx->ut_user, 0, UT_NAMESIZE) ; - memset(utx->ut_host, 0, UT_HOSTSIZE) ; - utx->ut_tv.tv_sec = 0 ; - utx->ut_tv.tv_usec = 0 ; - setutxent() ; - if (!pututxline(utx)) strerr_diefu1sys(111, "pututxline") ; - return 0 ; -} diff --git a/src/misc/deps-exe/s6-linux-init-echo b/src/misc/deps-exe/s6-linux-init-echo new file mode 100644 index 0000000..e7187fe --- /dev/null +++ b/src/misc/deps-exe/s6-linux-init-echo @@ -0,0 +1 @@ +-lskarnet diff --git a/src/misc/deps-exe/s6-linux-init-logouthookd b/src/misc/deps-exe/s6-linux-init-logouthookd new file mode 100644 index 0000000..5a7d2fd --- /dev/null +++ b/src/misc/deps-exe/s6-linux-init-logouthookd @@ -0,0 +1,4 @@ +${LIBUTMPS} +-lskarnet +${TAINNOW_LIB} +${SOCKET_LIB} diff --git a/src/misc/deps-exe/s6-linux-init-umountall b/src/misc/deps-exe/s6-linux-init-umountall new file mode 100644 index 0000000..e7187fe --- /dev/null +++ b/src/misc/deps-exe/s6-linux-init-umountall @@ -0,0 +1 @@ +-lskarnet diff --git a/src/misc/s6-linux-init-echo.c b/src/misc/s6-linux-init-echo.c new file mode 100644 index 0000000..d39e649 --- /dev/null +++ b/src/misc/s6-linux-init-echo.c @@ -0,0 +1,39 @@ +/* ISC license. */ + +#include +#include +#include + +#define USAGE "s6-linux-init-echo [ -n ] [ -s sep ] args..." + +int main (int argc, char const *const *argv) +{ + char sep = ' ' ; + char donl = 1 ; + PROG = "s6-echo" ; + { + subgetopt_t l = SUBGETOPT_ZERO ; + for (;;) + { + int opt = subgetopt_r(argc, argv, "ns:", &l) ; + if (opt == -1) break ; + switch (opt) + { + case 'n': donl = 0 ; break ; + case 's': sep = *l.arg ; break ; + default : strerr_dieusage(100, USAGE) ; + } + } + argc -= l.ind ; argv += l.ind ; + } + for ( ; *argv ; argv++) + if ((buffer_puts(buffer_1small, *argv) < 0) + || (argv[1] && (buffer_put(buffer_1small, &sep, 1) < 0))) + goto err ; + if (donl && (buffer_put(buffer_1small, "\n", 1) < 0)) goto err ; + if (!buffer_flush(buffer_1small)) goto err ; + return 0 ; + + err: + strerr_diefu1sys(111, "write to stdout") ; +} diff --git a/src/misc/s6-linux-init-logouthookd.c b/src/misc/s6-linux-init-logouthookd.c new file mode 100644 index 0000000..cc542ba --- /dev/null +++ b/src/misc/s6-linux-init-logouthookd.c @@ -0,0 +1,69 @@ +/* ISC license. */ + +#include + +#ifndef SKALIBS_HASSOPEERCRED +#error "The SO_PEERCRED option to getsockopt() is required." +#endif + +#include + +#include +#include +#include +#include +#include +#include + +#include +#include + +#ifndef UT_NAMESIZE +#define UT_NAMESIZE 32 +#endif + +#ifndef UT_HOSTSIZE +#define UT_HOSTSIZE 256 +#endif + +int main (void) +{ + struct utmpx *utx ; + struct ucred client ; + socklen_t len = sizeof(client) ; + ssize_t r ; + char c ; + + close(1) ; + if (getsockopt(0, SOL_SOCKET, SO_PEERCRED, &client, &len) == -1) + strerr_diefu1sys(111, "getsockopt") ; + + /* Only take connections from root. */ + if (client.uid) return 1 ; + + /* Wait for the client to die. */ + r = fd_read(0, &c, 1) ; + if (r < 0) strerr_diefu1sys(111, "read from stdin") ; + if (r) strerr_dief1x(2, "client attempted to write") ; + + /* Clean up utmpx record for the client's pid, then exit. */ + for (;;) + { + errno = 0 ; + utx = getutxent() ; + if (!utx) break ; + if (utx->ut_pid == client.pid) goto gotit ; + } + if (errno) strerr_diefu1sys(111, "getutxent") ; + return 0 ; + + gotit: + utx->ut_type = DEAD_PROCESS ; + memset(utx->ut_user, 0, UT_NAMESIZE) ; + memset(utx->ut_host, 0, UT_HOSTSIZE) ; + utx->ut_tv.tv_sec = 0 ; + utx->ut_tv.tv_usec = 0 ; + setutxent() ; + if (!pututxline(utx)) strerr_diefu1sys(111, "pututxline") ; + return 0 ; +} diff --git a/src/misc/s6-linux-init-umountall.c b/src/misc/s6-linux-init-umountall.c new file mode 100644 index 0000000..24b22fc --- /dev/null +++ b/src/misc/s6-linux-init-umountall.c @@ -0,0 +1,70 @@ +/* ISC license. */ + +#include +#include + +#include +#include +#include +#include +#include +#include + +#define BUFSIZE 4096 +#define MAXLINES 512 + +int main (int argc, char const *const *argv) +{ + stralloc mountpoints[MAXLINES] ; + char buf[BUFSIZE] ; + buffer b ; + stralloc sa = STRALLOC_ZERO ; + unsigned int line = 0 ; + int e = 0 ; + int r ; + int fd ; + + PROG = "s6-linux-init-umountall" ; + + + /* + We need to go through /proc/mounts *in reverse order*, because later mounts + may depend on earlier mounts. + The getmntent() family of functions has obviously not been designed for that + use case at all, and it is actually more difficult to use it than to do the + /proc/mounts parsing by hand. So, we do it by hand with skalibs functions. + That's how you can tell a good API from a terrible one. + */ + + fd = open_readb("/proc/mounts") ; + if (fd < 0) strerr_diefu1sys(111, "open /proc/mounts") ; + memset(mountpoints, 0, sizeof(mountpoints)) ; + buffer_init(&b, &buffer_read, fd, buf, BUFSIZE) ; + for (;;) + { + size_t n, p ; + if (line >= MAXLINES) strerr_dief1x(111, "/proc/mounts too big") ; + sa.len = 0 ; + r = skagetln(&b, &sa, '\n') ; + if (r <= 0) break ; + p = byte_chr(sa.s, sa.len, ' ') ; + if (p >= sa.len) strerr_dief1x(111, "bad /proc/mounts format") ; + p++ ; + n = byte_chr(sa.s + p, sa.len - p, ' ') ; + if (n == sa.len - p) strerr_dief1x(111, "bad /proc/mounts format") ; + if (!stralloc_catb(&mountpoints[line], sa.s + p, n) || !stralloc_0(&mountpoints[line])) + strerr_diefu1sys(111, "store mount point") ; + line++ ; + } + fd_close(fd) ; + stralloc_free(&sa) ; + if (r < 0) strerr_diefu1sys(111, "read /proc/mounts") ; + + while (line--) + if (umount(mountpoints[line].s) == -1) + { + e++ ; + strerr_warnwu2sys("umount ", mountpoints[line].s) ; + } + return e ; +} diff --git a/src/shutdown/deps-exe/s6-linux-init-halt b/src/shutdown/deps-exe/s6-linux-init-halt new file mode 100644 index 0000000..0c4376c --- /dev/null +++ b/src/shutdown/deps-exe/s6-linux-init-halt @@ -0,0 +1,5 @@ +libhpr.a.xyzzy +${LIBUTMPS} +-lskarnet +${TAINNOW_LIB} +${SOCKET_LIB} diff --git a/src/shutdown/deps-exe/s6-linux-init-poweroff b/src/shutdown/deps-exe/s6-linux-init-poweroff new file mode 100644 index 0000000..0c4376c --- /dev/null +++ b/src/shutdown/deps-exe/s6-linux-init-poweroff @@ -0,0 +1,5 @@ +libhpr.a.xyzzy +${LIBUTMPS} +-lskarnet +${TAINNOW_LIB} +${SOCKET_LIB} diff --git a/src/shutdown/deps-exe/s6-linux-init-reboot b/src/shutdown/deps-exe/s6-linux-init-reboot new file mode 100644 index 0000000..0c4376c --- /dev/null +++ b/src/shutdown/deps-exe/s6-linux-init-reboot @@ -0,0 +1,5 @@ +libhpr.a.xyzzy +${LIBUTMPS} +-lskarnet +${TAINNOW_LIB} +${SOCKET_LIB} diff --git a/src/shutdown/deps-exe/s6-linux-init-shutdown b/src/shutdown/deps-exe/s6-linux-init-shutdown new file mode 100644 index 0000000..0c4376c --- /dev/null +++ b/src/shutdown/deps-exe/s6-linux-init-shutdown @@ -0,0 +1,5 @@ +libhpr.a.xyzzy +${LIBUTMPS} +-lskarnet +${TAINNOW_LIB} +${SOCKET_LIB} diff --git a/src/shutdown/deps-exe/s6-linux-init-shutdownd b/src/shutdown/deps-exe/s6-linux-init-shutdownd new file mode 100644 index 0000000..5a7d2fd --- /dev/null +++ b/src/shutdown/deps-exe/s6-linux-init-shutdownd @@ -0,0 +1,4 @@ +${LIBUTMPS} +-lskarnet +${TAINNOW_LIB} +${SOCKET_LIB} diff --git a/src/shutdown/deps-lib/hpr b/src/shutdown/deps-lib/hpr new file mode 100644 index 0000000..9e1493d --- /dev/null +++ b/src/shutdown/deps-lib/hpr @@ -0,0 +1,2 @@ +hpr_shutdown.o +hpr_wall.o diff --git a/src/shutdown/hpr.c b/src/shutdown/hpr.c new file mode 100644 index 0000000..a769c8c --- /dev/null +++ b/src/shutdown/hpr.c @@ -0,0 +1,104 @@ +/* ISC license. */ + +#include + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "defaults.h" +#include "hpr.h" + +#ifndef UT_NAMESIZE +#define UT_NAMESIZE 32 +#endif + +#ifndef UT_HOSTSIZE +#define UT_HOSTSIZE 256 +#endif + +#ifndef _PATH_WTMP +#define _PATH_WTMP "/dev/null/wtmp" +#endif + +#define USAGE PROGNAME " [ -h | -p | -r ] [ -d | -w ] [ -W ] [ -f ]" + +int main (int argc, char const *const *argv) +{ + int what = WHATDEFAULT ; + int force = 0 ; + int dowtmp = 1 ; + int dowall = 1 ; + PROG = PROGNAME ; + + { + subgetopt_t l = SUBGETOPT_ZERO ; + for (;;) + { + int opt = subgetopt_r(argc, argv, "hprfdwW", &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 ; + case 'd' : dowtmp = 0 ; break ; + case 'w' : dowtmp = 2 ; break ; + case 'W' : dowall = 0 ; break ; + default : strerr_dieusage(100, USAGE) ; + } + } + argc -= l.ind ; argv += l.ind ; + } + + if (geteuid()) + { + errno = EPERM ; + strerr_dief1sys(100, "nice try, peon") ; + } + + if (force) + { + reboot(what == 3 ? RB_AUTOBOOT : what == 2 ? RB_POWER_OFF : RB_HALT_SYSTEM) ; + strerr_diefu1sys(111, "reboot()") ; + } + + if (!tain_now_g()) strerr_warnw1sys("get current time") ; + if (dowtmp) + { + struct utmpx utx = + { + .ut_type = RUN_LVL, + .ut_pid = getpid(), + .ut_line = "~", + .ut_id = "", + .ut_session = getsid(0) + } ; + strncpy(utx.ut_user, what == 3 ? "reboot" : "shutdown", UT_NAMESIZE) ; + if (gethostname(utx.ut_host, UT_HOSTSIZE) < 0) + { + utx.ut_host[0] = 0 ; + strerr_warnwu1sys("gethostname") ; + } + else utx.ut_host[UT_HOSTSIZE - 1] = 0 ; + if (!timeval_from_tain(&utx.ut_tv, &STAMP)) + strerr_warnwu1sys("timeval_from_tain") ; + updwtmpx(_PATH_WTMP, &utx) ; + } + if (dowall) hpr_wall(HPR_WALL_BANNER) ; + if (dowtmp < 2) + { + if (!hpr_shutdown(what, &STAMP, 0)) + strerr_diefu1sys(111, "notify s6-linux-init-shutdownd") ; + } + return 0 ; +} diff --git a/src/shutdown/hpr.h b/src/shutdown/hpr.h new file mode 100644 index 0000000..993f5ab --- /dev/null +++ b/src/shutdown/hpr.h @@ -0,0 +1,20 @@ +/* ISC license. */ + +#ifndef HPR_H +#define HPR_H + +#include + +#include +#include + +#include "initctl.h" + +#define HPR_WALL_BANNER "\n\n*** WARNING ***\nThe system is going down NOW!\n" + +#define hpr_send(s, n) openwritenclose_unsafe(INITCTL, (s), n) +#define hpr_cancel() hpr_send("c", 1) +extern int hpr_shutdown (unsigned int, tain_t const *, unsigned int) ; +extern void hpr_wall (char const *) ; + +#endif diff --git a/src/shutdown/hpr_shutdown.c b/src/shutdown/hpr_shutdown.c new file mode 100644 index 0000000..9c9ff51 --- /dev/null +++ b/src/shutdown/hpr_shutdown.c @@ -0,0 +1,16 @@ +/* ISC license. */ + +#include + +#include +#include + +#include "hpr.h" + +int hpr_shutdown (unsigned int what, tain_t const *when, unsigned int grace) +{ + char pack[5 + TAIN_PACK] = { " hpr"[what] } ; + tain_pack(pack+1, when) ; + uint32_pack_big(pack + 1 + TAIN_PACK, (uint32_t)grace) ; + return hpr_send(pack, 5 + TAIN_PACK) ; +} diff --git a/src/shutdown/hpr_wall.c b/src/shutdown/hpr_wall.c new file mode 100644 index 0000000..cc6da5e --- /dev/null +++ b/src/shutdown/hpr_wall.c @@ -0,0 +1,47 @@ +/* ISC license. */ + +#include +#include +#include + +#include +#include +#include +#include + +#include "hpr.h" + +#ifndef UT_LINESIZE +#define UT_LINESIZE 32 +#endif + +void hpr_wall (char const *s) +{ + size_t n = strlen(s) ; + char tty[10 + UT_LINESIZE] = "/dev/" ; + char msg[n+1] ; + memcpy(msg, s, n) ; + msg[n++] = '\n' ; + setutxent() ; + for (;;) + { + size_t linelen, idlen ; + struct utmpx *utx ; + int fd ; + errno = 0 ; + utx = getutxent() ; + if (!utx) break ; + if (utx->ut_type != USER_PROCESS) continue ; + linelen = strnlen(utx->ut_line, UT_LINESIZE) ; + idlen = strnlen(utx->ut_id, 4) ; + memcpy(tty + 5, utx->ut_line, linelen) ; + memcpy(tty + 5 + linelen, utx->ut_id, idlen) ; + tty[5 + linelen + idlen] = 0 ; + fd = open_append(tty) ; + if (fd == -1) continue ; + allwrite(fd, msg, n) ; + fd_close(fd) ; + } + if (errno) strerr_warnwu1sys("getutxent") ; + endutxent() ; +} diff --git a/src/shutdown/s6-linux-init-halt.c b/src/shutdown/s6-linux-init-halt.c new file mode 100644 index 0000000..4ccca1d --- /dev/null +++ b/src/shutdown/s6-linux-init-halt.c @@ -0,0 +1,7 @@ +/* ISC license. */ + +#undef PROGNAME +#define PROGNAME "s6-linux-init-halt" +#undef WHATDEFAULT +#define WHATDEFAULT 1 +#include "hpr.c" diff --git a/src/shutdown/s6-linux-init-poweroff.c b/src/shutdown/s6-linux-init-poweroff.c new file mode 100644 index 0000000..d25dbb6 --- /dev/null +++ b/src/shutdown/s6-linux-init-poweroff.c @@ -0,0 +1,7 @@ +/* ISC license. */ + +#undef PROGNAME +#define PROGNAME "s6-linux-init-poweroff" +#undef WHATDEFAULT +#define WHATDEFAULT 2 +#include "hpr.c" diff --git a/src/shutdown/s6-linux-init-reboot.c b/src/shutdown/s6-linux-init-reboot.c new file mode 100644 index 0000000..3612684 --- /dev/null +++ b/src/shutdown/s6-linux-init-reboot.c @@ -0,0 +1,7 @@ +/* ISC license. */ + +#undef PROGNAME +#define PROGNAME "s6-linux-init-reboot" +#undef WHATDEFAULT +#define WHATDEFAULT 3 +#include "hpr.c" diff --git a/src/shutdown/s6-linux-init-shutdown.c b/src/shutdown/s6-linux-init-shutdown.c new file mode 100644 index 0000000..f534944 --- /dev/null +++ b/src/shutdown/s6-linux-init-shutdown.c @@ -0,0 +1,268 @@ +/* ISC license. */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "defaults.h" +#include "initctl.h" +#include "hpr.h" + +#ifndef UT_NAMESIZE +#define UT_NAMESIZE 32 +#endif + +#define USAGE "s6-linux-init-shutdown [ -h | -p | -r | -k ] [ -f | -F ] [ -a ] [ -t sec ] time [ message ] or s6-linux-init-shutdown -c [ message ]" +#define dieusage() strerr_dieusage(100, USAGE) + +#define AC_FILE "/etc/shutdown.allow" +#define AC_BUFSIZE 4096 +#define AC_MAX 64 +#define AC_SHORT_MESSAGE "no authorized users logged in\n" +#define AC_MESSAGE "s6-linux-init-shutdown: " AC_SHORT_MESSAGE + + + /* shutdown 01:23: date/time format parsing */ + +static inline void add_one_day (struct tm *tm) +{ + tm->tm_isdst = -1 ; + if (tm->tm_mday++ < 31) return ; + tm->tm_mday = 1 ; + if (tm->tm_mon++ < 11) return ; + tm->tm_mon = 0 ; + tm->tm_year++ ; +} + +static inline void parse_hourmin (tain_t *when, char const *s) +{ + tai_t taithen ; + struct tm tmnow, tmthen ; + unsigned int hour, minute ; + size_t len = uint_scan(s, &hour) ; + if (!len || len > 2 || s[len] != ':' || hour > 23) + strerr_dief1x(100, "invalid time format") ; + s += len+1 ; + len = uint0_scan(s, &minute) ; + if (!len || len != 2 || minute > 59) + strerr_dief1x(100, "invalid time format") ; + if (!localtm_from_tai(&tmnow, tain_secp(&STAMP), 1)) + strerr_diefu1sys(111, "break down current time into struct tm") ; + tmthen = tmnow ; + tmthen.tm_hour = hour ; + tmthen.tm_min = minute ; + if (!tai_from_localtm(&taithen, &tmthen)) + strerr_diefu1sys(111, "assemble broken-down time into tain_t") ; + if (tai_less(&taithen, tain_secp(&STAMP))) + { + add_one_day(&tmthen) ; + if (!tai_from_localtm(&taithen, &tmthen)) + strerr_diefu1sys(111, "assemble broken-down time into tain_t") ; + } + tai_sub(tain_secp(when), &taithen, tain_secp(&STAMP)) ; + when->nano = 0 ; +} + +static inline void parse_time (tain_t *when, char const *s) +{ + if (!strcmp(s, "now")) tain_copynow(when) ; + else if (s[0] == '+') + { + unsigned int mins ; + if (!uint0_scan(s+1, &mins)) dieusage() ; + tain_addsec_g(when, mins * 60) ; + } + else if (strchr(s, ':')) parse_hourmin(when, s) ; + else + { + unsigned int mins ; + if (!uint0_scan(s, &mins)) dieusage() ; + tain_addsec_g(when, mins * 60) ; + } +} + + + /* shutdown -a: access control */ + +static inline unsigned char cclass (unsigned char c) +{ + switch (c) + { + case 0 : return 0 ; + case '\n' : return 1 ; + case '#' : return 2 ; + default : return 3 ; + } +} + +static inline unsigned int parse_authorized_users (char *buf, char const **users, unsigned int max) +{ + static unsigned char const table[3][4] = + { + { 0x03, 0x00, 0x01, 0x12 }, + { 0x03, 0x00, 0x01, 0x01 }, + { 0x23, 0x20, 0x02, 0x02 } + } ; + size_t pos = 0 ; + size_t mark = 0 ; + unsigned int n = 0 ; + unsigned int state = 0 ; + for (; state < 3 ; pos++) + { + unsigned char what = table[state][cclass(buf[pos])] ; + state = what & 3 ; + if (what & 0x10) mark = pos ; + if (what & 0x20) + { + if (n >= max) + { + char fmt[UINT32_MAX] ; + fmt[uint32_fmt(fmt, AC_MAX)] = 0 ; + strerr_warnw4x(AC_FILE, " lists more than ", fmt, " authorized users - ignoring the extra ones") ; + break ; + } + buf[pos] = 0 ; + users[n++] = buf + mark ; + } + } + return n ; +} + +static inline int match_users_with_utmp (char const *const *users, unsigned int n) +{ + setutxent() ; + for (;;) + { + struct utmpx *utx ; + errno = 0 ; + utx = getutxent() ; + if (!utx) break ; + if (utx->ut_type != USER_PROCESS) continue ; + for (unsigned int i = 0 ; i < n ; i++) + if (!strncmp(utx->ut_user, users[i], UT_NAMESIZE)) goto yes ; + } + if (errno) strerr_warnwu1sys("getutxent") ; + endutxent() ; + return 0 ; + + yes: + endutxent() ; + return 1 ; +} + +static inline void access_control (void) +{ + char buf[AC_BUFSIZE] ; + char const *users[AC_MAX] ; + unsigned int n ; + struct stat st ; + int fd = open_readb(AC_FILE) ; + if (fd == -1) + { + if (errno == ENOENT) return ; + strerr_diefu2sys(111, "open ", AC_FILE) ; + } + if (fstat(fd, &st) == -1) + strerr_diefu2sys(111, "stat ", AC_FILE) ; + if (st.st_size >= AC_BUFSIZE) + { + char fmt[UINT32_FMT] ; + fmt[uint32_fmt(fmt, AC_BUFSIZE - 1)] = 0 ; + strerr_dief4x(1, AC_FILE, " is too big: it needs to be ", fmt, " bytes or less") ; + } + if (allread(fd, buf, st.st_size) < st.st_size) + strerr_diefu2sys(111, "read ", AC_FILE) ; + fd_close(fd) ; + buf[st.st_size] = 0 ; + n = parse_authorized_users(buf, users, AC_MAX) ; + if (!n || !match_users_with_utmp(users, n)) + { + fd = open_append("/dev/console") ; + if (fd == -1) + strerr_diefu1sys(111, "open /dev/console") ; + if (allwrite(fd, AC_MESSAGE, sizeof(AC_MESSAGE) - 1) < sizeof(AC_MESSAGE) - 1) + strerr_diefu1sys(111, "write to /dev/console") ; + strerr_dief1x(1, AC_SHORT_MESSAGE) ; + } +} + + + /* main */ + +int main (int argc, char const *const *argv) +{ + unsigned int gracetime = 0 ; + int what = 0 ; + int doactl = 0 ; + int docancel = 0 ; + tain_t when ; + PROG = "s6-linux-init-shutdown" ; + + { + subgetopt_t l = SUBGETOPT_ZERO ; + for (;;) + { + int opt = subgetopt_r(argc, argv, "hprkafFct:", &l) ; + if (opt == -1) break ; + switch (opt) + { + case 'h' : what = 1 ; break ; + case 'p' : what = 2 ; break ; + case 'r' : what = 3 ; break ; + case 'k' : what = 4 ; break ; + case 'a' : doactl = 1 ; break ; + case 'f' : /* talk to the hand */ break ; + case 'F' : /* no, the other hand */ break ; + case 'c' : docancel = 1 ; break ; + case 't' : if (!uint0_scan(l.arg, &gracetime)) dieusage() ; break ; + default : strerr_dieusage(100, USAGE) ; + } + } + argc -= l.ind ; argv += l.ind ; + } + + if (geteuid()) + { + errno = EPERM ; + strerr_diefu1sys(111, "shutdown") ; + } + if (doactl) access_control() ; + if (!tain_now_g()) strerr_warnw1sys("get current time") ; + if (docancel) + { + if (argv[0]) hpr_wall(argv[0]) ; + if (!hpr_cancel()) goto err ; + return 0 ; + } + if (!argc) dieusage() ; + parse_time(&when, argv[0]) ; + if (argv[1]) hpr_wall(argv[1]) ; + if (what < 4) + { + if (gracetime > 300) + { + gracetime = 300 ; + strerr_warnw1x("delay between SIGTERM and SIGKILL is capped to 300 seconds") ; + } + if (!hpr_shutdown(what, &when, gracetime * 1000)) goto err ; + } + return 0 ; + + err: + strerr_diefu2sys(111, "write to ", INITCTL) ; +} diff --git a/src/shutdown/s6-linux-init-shutdownd.c b/src/shutdown/s6-linux-init-shutdownd.c new file mode 100644 index 0000000..c6d6316 --- /dev/null +++ b/src/shutdown/s6-linux-init-shutdownd.c @@ -0,0 +1,228 @@ +/* ISC license. */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "defaults.h" +#include "initctl.h" +#include "hpr.h" + +#define STAGE4_FILE "stage 4" + +#define USAGE "s6-linux-init-shutdownd [ -c basedir ] [ -g gracetime ]" +#define dieusage() strerr_dieusage(100, USAGE) + +static char const *basedir = BASEDIR ; + +static inline void prepare_shutdown (char c, unsigned int *what, tain_t *deadline, unsigned int *grace_time) +{ + uint32_t u ; + char pack[TAIN_PACK] ; + ssize_t r = sanitize_read(buffer_get(buffer_0small, pack, TAIN_PACK)) ; + if (r == -1) strerr_diefu1sys(111, "read from pipe") ; + if (r < TAIN_PACK + 4) strerr_dief1x(101, "bad shutdown protocol") ; + tain_unpack(pack, deadline) ; + uint32_unpack_big(pack + TAIN_PACK, &u) ; + if (u && u <= 300000) *grace_time = u ; + *what = 1 + byte_chr("hpr", c, 3) ; +} + +static inline void handle_stdin (unsigned int *what, tain_t *deadline, unsigned int *grace_time) +{ + for (;;) + { + char c ; + ssize_t r = sanitize_read(buffer_get(buffer_0small, &c, 1)) ; + if (r == -1) strerr_diefu1sys(111, "read from pipe") ; + else if (!r) break ; + switch (c) + { + case 'h' : + case 'p' : + case 'r' : + prepare_shutdown(c, what, deadline, grace_time) ; + break ; + case 'c' : + *what = 0 ; + tain_add_g(deadline, &tain_infinite_relative) ; + break ; + default : + { + char s[2] = { c, 0 } ; + strerr_warnw2x("unknown command: ", s) ; + } + break ; + } + } +} + +static inline void prepare_stage4 (char const *basedir, unsigned int what) +{ + static char const *table[3] = { "halt", "poweroff", "reboot" } ; + buffer b ; + int fd ; + char buf[512] ; + unlink_void(STAGE4_FILE ".new") ; + fd = open_excl(STAGE4_FILE ".new") ; + if (fd == -1) strerr_diefu3sys(111, "open ", STAGE4_FILE ".new", " for writing") ; + buffer_init(&b, &buffer_write, fd, buf, 512) ; + + if (buffer_puts(&b, + "#!" EXECLINE_SHEBANGPREFIX "/execlineb -P\n\n" + EXECLINE_EXTBINPREFIX "foreground { " + S6_LINUX_INIT_LIBEXECPREFIX "s6-linux-init-umountall }\n" + S6_LINUX_INIT_LIBEXECPREFIX "s6-linux-init-") < 0 + || buffer_puts(&b, table[what-1]) < 0 + || buffer_putsflush(&b, " -f\n") < 0) + strerr_diefu2sys(111, "write to ", STAGE4_FILE ".new") ; + if (fchmod(fd, S_IRWXU) == -1) + strerr_diefu2sys(111, "fchmod ", STAGE4_FILE ".new") ; + fd_close(fd) ; + if (rename(STAGE4_FILE ".new", STAGE4_FILE) == -1) + strerr_diefu4sys(111, "rename ", STAGE4_FILE ".new", " to ", STAGE4_FILE) ; +} + +int main (int argc, char const *const *argv, char const *const *envp) +{ + pid_t pid ; + tain_t deadline ; + unsigned int what = 0 ; + unsigned int grace_time = 3000 ; + PROG = "s6-linux-init-shutdownd" ; + + { + subgetopt_t l = SUBGETOPT_ZERO ; + for (;;) + { + int opt = subgetopt_r(argc, argv, "c:g:", &l) ; + if (opt == -1) break ; + switch (opt) + { + case 'c' : basedir = l.arg ; break ; + case 'g' : if (!uint0_scan(l.arg, &grace_time)) dieusage() ; break ; + default : dieusage() ; + } + } + argc -= l.ind ; argv += l.ind ; + } + if (basedir[0] != '/') + strerr_dief2x(100, "basedir", " must be an absolute path") ; + if (grace_time > 300000) grace_time = 300000 ; + + /* if we're in stage 4, exec it immediately */ + { + char const *stage4_argv[2] = { STAGE4_FILE, 0 } ; + pathexec_run(stage4_argv[0], stage4_argv, envp) ; + if (errno != ENOENT) + strerr_warnwu2sys("exec ", stage4_argv[0]) ; + } + + fd_close(1) ; + fd_close(0) ; + + if (open_read(SHUTDOWND_FIFO)) + strerr_diefu3sys(111, "open ", SHUTDOWND_FIFO, " for reading") ; + if (open_write(SHUTDOWND_FIFO) != 1) + strerr_diefu3sys(111, "open ", SHUTDOWND_FIFO, " for writing") ; + if (sig_ignore(SIGPIPE) == -1) + strerr_diefu1sys(111, "sig_ignore SIGPIPE") ; + tain_now_g() ; + tain_add_g(&deadline, &tain_infinite_relative) ; + + for (;;) + { + iopause_fd x = { .fd = 0, .events = IOPAUSE_READ } ; + int r = iopause_g(&x, 1, &deadline) ; + if (r == -1) strerr_diefu1sys(111, "iopause") ; + if (!r) + { + if (what) break ; + tain_add_g(&deadline, &tain_infinite_relative) ; + continue ; + } + if (x.revents & IOPAUSE_READ) + handle_stdin(&what, &deadline, &grace_time) ; + } + + + /* Stage 3 */ + + fd_close(1) ; + fd_close(0) ; + if (open_readb("/dev/null")) + strerr_diefu1sys(111, "open /dev/null for reading") ; + if (open_write("/dev/console") != 1 || ndelay_off(1) == -1) + strerr_diefu1sys(111, "open /dev/console for writing") ; + { + size_t basedirlen = strlen(basedir) ; + char stage3[basedirlen + sizeof("/" STAGE3)] ; + char const *stage3_argv[2] = { stage3, 0 } ; + memcpy(stage3, basedir, basedirlen) ; + memcpy(stage3 + basedirlen, "/" STAGE3, sizeof("/" STAGE3)) ; + pid = child_spawn0(stage3_argv[0], stage3_argv, envp) ; + if (pid) + { + int wstat ; + if (wait_pid(pid, &wstat) == -1) strerr_diefu1sys(111, "waitpid") ; + if (WIFSIGNALED(wstat)) + { + char fmt[UINT_FMT] ; + fmt[uint_fmt(fmt, WTERMSIG(wstat))] = 0 ; + strerr_warnw3x(stage3, " was killed by signal ", fmt) ; + } + else if (WEXITSTATUS(wstat)) + { + char fmt[UINT_FMT] ; + fmt[uint_fmt(fmt, WTERMSIG(wstat))] = 0 ; + strerr_warnw3x(stage3, " was killed by signal ", fmt) ; + } + else if (WEXITSTATUS(wstat)) + { + char fmt[UINT_FMT] ; + fmt[uint_fmt(fmt, WEXITSTATUS(wstat))] = 0 ; + strerr_warnw3x(stage3, " exited ", fmt) ; + } + } + else strerr_warnwu2sys("spawn ", stage3) ; + } + + + /* The end is coming! */ + + prepare_stage4(basedir, what) ; + if (fd_move(2, 1) == -1) strerr_warnwu1sys("fd_move") ; + sync() ; + if (sig_ignore(SIGTERM) == -1) strerr_warnwu1sys("sig_ignore SIGTERM") ; + strerr_warni1x("sending all processes the TERM signal...") ; + kill(-1, SIGTERM) ; + kill(-1, SIGCONT) ; + tain_from_millisecs(&deadline, grace_time) ; + tain_add_g(&deadline, &deadline) ; + deepsleepuntil_g(&deadline) ; + sync() ; + strerr_warni1x("sending all processes the KILL signal...") ; + kill(-1, SIGKILL) ; + return 0 ; +} diff --git a/src/state/deps-exe/s6-linux-init-halt b/src/state/deps-exe/s6-linux-init-halt deleted file mode 100644 index 0c4376c..0000000 --- a/src/state/deps-exe/s6-linux-init-halt +++ /dev/null @@ -1,5 +0,0 @@ -libhpr.a.xyzzy -${LIBUTMPS} --lskarnet -${TAINNOW_LIB} -${SOCKET_LIB} diff --git a/src/state/deps-exe/s6-linux-init-poweroff b/src/state/deps-exe/s6-linux-init-poweroff deleted file mode 100644 index 0c4376c..0000000 --- a/src/state/deps-exe/s6-linux-init-poweroff +++ /dev/null @@ -1,5 +0,0 @@ -libhpr.a.xyzzy -${LIBUTMPS} --lskarnet -${TAINNOW_LIB} -${SOCKET_LIB} diff --git a/src/state/deps-exe/s6-linux-init-reboot b/src/state/deps-exe/s6-linux-init-reboot deleted file mode 100644 index 0c4376c..0000000 --- a/src/state/deps-exe/s6-linux-init-reboot +++ /dev/null @@ -1,5 +0,0 @@ -libhpr.a.xyzzy -${LIBUTMPS} --lskarnet -${TAINNOW_LIB} -${SOCKET_LIB} diff --git a/src/state/deps-exe/s6-linux-init-shutdown b/src/state/deps-exe/s6-linux-init-shutdown deleted file mode 100644 index 0c4376c..0000000 --- a/src/state/deps-exe/s6-linux-init-shutdown +++ /dev/null @@ -1,5 +0,0 @@ -libhpr.a.xyzzy -${LIBUTMPS} --lskarnet -${TAINNOW_LIB} -${SOCKET_LIB} diff --git a/src/state/deps-exe/s6-linux-init-shutdownd b/src/state/deps-exe/s6-linux-init-shutdownd deleted file mode 100644 index 5a7d2fd..0000000 --- a/src/state/deps-exe/s6-linux-init-shutdownd +++ /dev/null @@ -1,4 +0,0 @@ -${LIBUTMPS} --lskarnet -${TAINNOW_LIB} -${SOCKET_LIB} diff --git a/src/state/deps-exe/s6-linux-init-single b/src/state/deps-exe/s6-linux-init-single deleted file mode 100644 index e7187fe..0000000 --- a/src/state/deps-exe/s6-linux-init-single +++ /dev/null @@ -1 +0,0 @@ --lskarnet diff --git a/src/state/deps-exe/s6-linux-init-telinit b/src/state/deps-exe/s6-linux-init-telinit deleted file mode 100644 index aeb24a6..0000000 --- a/src/state/deps-exe/s6-linux-init-telinit +++ /dev/null @@ -1,2 +0,0 @@ -libhpr.a.xyzzy --lskarnet diff --git a/src/state/deps-lib/hpr b/src/state/deps-lib/hpr deleted file mode 100644 index 066efd2..0000000 --- a/src/state/deps-lib/hpr +++ /dev/null @@ -1,4 +0,0 @@ -hpr_fmttime.o -hpr_shutdown.o -hpr_wall.o -hpr_wall_seconds.o diff --git a/src/state/hpr.c b/src/state/hpr.c deleted file mode 100644 index 401cc97..0000000 --- a/src/state/hpr.c +++ /dev/null @@ -1,102 +0,0 @@ -/* ISC license. */ - -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include - -#include "defaults.h" -#include "hpr.h" - -#ifndef UT_NAMESIZE -#define UT_NAMESIZE 32 -#endif - -#ifndef UT_HOSTSIZE -#define UT_HOSTSIZE 256 -#endif - -#ifndef _PATH_WTMP -#define _PATH_WTMP "/dev/null/wtmp" -#endif - -#define USAGE PROGNAME " [ -h | -p | -r ] [ -d | -w ] [ -W ] [ -f ]" - -int main (int argc, char const *const *argv) -{ - int what = WHATDEFAULT ; - int force = 0 ; - int dowtmp = 1 ; - int dowall = 1 ; - PROG = PROGNAME ; - - { - subgetopt_t l = SUBGETOPT_ZERO ; - for (;;) - { - int opt = subgetopt_r(argc, argv, "hprfdwW", &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 ; - case 'd' : dowtmp = 0 ; break ; - case 'w' : dowtmp = 2 ; break ; - case 'W' : dowall = 0 ; break ; - default : strerr_dieusage(100, USAGE) ; - } - } - argc -= l.ind ; argv += l.ind ; - } - - if (geteuid()) - { - errno = EPERM ; - strerr_dief1sys(100, "nice try, peon") ; - } - - if (force) - { - reboot(what == 3 ? RB_AUTOBOOT : what == 2 ? RB_POWER_OFF : RB_HALT_SYSTEM) ; - strerr_diefu1sys(111, "reboot()") ; - } - - if (!tain_now_g()) strerr_warnw1sys("get current time") ; - if (dowtmp) - { - struct utmpx utx = - { - .ut_type = RUN_LVL, - .ut_pid = getpid(), - .ut_line = "~", - .ut_id = "", - .ut_session = getsid(0) - } ; - strncpy(utx.ut_user, what == 3 ? "reboot" : "shutdown", UT_NAMESIZE) ; - if (gethostname(utx.ut_host, UT_HOSTSIZE) < 0) - { - utx.ut_host[0] = 0 ; - strerr_warnwu1sys("gethostname") ; - } - else utx.ut_host[UT_HOSTSIZE - 1] = 0 ; - if (!timeval_from_tain(&utx.ut_tv, &STAMP)) - strerr_warnwu1sys("timeval_from_tain") ; - updwtmpx(_PATH_WTMP, &utx) ; - } - if (dowall) hpr_wall_seconds(0) ; - if (dowtmp < 2) - { - if (!hpr_shutdown(what, &STAMP, 0)) - strerr_diefu1sys(111, "notify s6-linux-init-shutdownd") ; - } - return 0 ; -} diff --git a/src/state/hpr.h b/src/state/hpr.h deleted file mode 100644 index 9deb6c4..0000000 --- a/src/state/hpr.h +++ /dev/null @@ -1,22 +0,0 @@ -/* ISC license. */ - -#ifndef HPR_H -#define HPR_H - -#include - -#include -#include - -#include "initctl.h" - -#define HPR_WALL_BANNER "\n\n*** WARNING ***\nThe system is going down in " - -#define hpr_send(s, n) openwritenclose_unsafe(INITCTL, (s), n) -#define hpr_cancel() hpr_send("c", 1) -extern int hpr_shutdown (unsigned int, tain_t const *, unsigned int) ; -extern size_t hpr_fmttime (char *, size_t, unsigned int) ; -extern void hpr_wall (char const *) ; -extern void hpr_wall_seconds (unsigned int) ; - -#endif diff --git a/src/state/hpr_fmttime.c b/src/state/hpr_fmttime.c deleted file mode 100644 index 8038cfe..0000000 --- a/src/state/hpr_fmttime.c +++ /dev/null @@ -1,52 +0,0 @@ -/* ISC license. */ - -#include - -#include - -#include "hpr.h" - -size_t hpr_fmttime (char *buf, size_t max, unsigned int seconds) -{ - size_t m = 0 ; - unsigned int minutes = 0 ; - unsigned int hours = 0 ; - if (seconds >= 60) - { - minutes = seconds / 60 ; - seconds %= 60 ; - if (minutes >= 60) - { - hours = minutes / 60 ; - minutes %= 60 ; - } - } - m = sizeof(HPR_WALL_BANNER) - 1 ; - if (m > max) return 0 ; - memcpy(buf, HPR_WALL_BANNER, m) ; - if (hours) - { - if (m + UINT_FMT + 6 > max) return 0 ; - m += uint_fmt(buf + m, hours) ; - memcpy(buf + m, " hour", 5) ; - m += 5 ; - if (hours > 1) buf[m++] = 's' ; - buf[m++] = ' ' ; - } - if (minutes) - { - if (m + UINT_FMT + 8 > max) return 0 ; - m += uint_fmt(buf + m, minutes) ; - memcpy(buf + m, " minute", 7) ; - m += 7 ; - if (minutes > 1) buf[m++] = 's' ; - buf[m++] = ' ' ; - } - if (m + UINT_FMT + 8 > max) return 0 ; - m += uint_fmt(buf + m, seconds) ; - memcpy(buf + m, " second", 7) ; - m += 7 ; - if (seconds != 1) buf[m++] = 's' ; - buf[m++] = ' ' ; - return m ; -} diff --git a/src/state/hpr_shutdown.c b/src/state/hpr_shutdown.c deleted file mode 100644 index 9c9ff51..0000000 --- a/src/state/hpr_shutdown.c +++ /dev/null @@ -1,16 +0,0 @@ -/* ISC license. */ - -#include - -#include -#include - -#include "hpr.h" - -int hpr_shutdown (unsigned int what, tain_t const *when, unsigned int grace) -{ - char pack[5 + TAIN_PACK] = { " hpr"[what] } ; - tain_pack(pack+1, when) ; - uint32_pack_big(pack + 1 + TAIN_PACK, (uint32_t)grace) ; - return hpr_send(pack, 5 + TAIN_PACK) ; -} diff --git a/src/state/hpr_wall.c b/src/state/hpr_wall.c deleted file mode 100644 index cc6da5e..0000000 --- a/src/state/hpr_wall.c +++ /dev/null @@ -1,47 +0,0 @@ -/* ISC license. */ - -#include -#include -#include - -#include -#include -#include -#include - -#include "hpr.h" - -#ifndef UT_LINESIZE -#define UT_LINESIZE 32 -#endif - -void hpr_wall (char const *s) -{ - size_t n = strlen(s) ; - char tty[10 + UT_LINESIZE] = "/dev/" ; - char msg[n+1] ; - memcpy(msg, s, n) ; - msg[n++] = '\n' ; - setutxent() ; - for (;;) - { - size_t linelen, idlen ; - struct utmpx *utx ; - int fd ; - errno = 0 ; - utx = getutxent() ; - if (!utx) break ; - if (utx->ut_type != USER_PROCESS) continue ; - linelen = strnlen(utx->ut_line, UT_LINESIZE) ; - idlen = strnlen(utx->ut_id, 4) ; - memcpy(tty + 5, utx->ut_line, linelen) ; - memcpy(tty + 5 + linelen, utx->ut_id, idlen) ; - tty[5 + linelen + idlen] = 0 ; - fd = open_append(tty) ; - if (fd == -1) continue ; - allwrite(fd, msg, n) ; - fd_close(fd) ; - } - if (errno) strerr_warnwu1sys("getutxent") ; - endutxent() ; -} diff --git a/src/state/hpr_wall_seconds.c b/src/state/hpr_wall_seconds.c deleted file mode 100644 index 6c0363b..0000000 --- a/src/state/hpr_wall_seconds.c +++ /dev/null @@ -1,12 +0,0 @@ -/* ISC license. */ - -#include "hpr.h" - -void hpr_wall_seconds (unsigned int secs) -{ - char buf[300] = HPR_WALL_BANNER ; - size_t n = sizeof(HPR_WALL_BANNER) - 1 ; - n += hpr_fmttime(buf + n, 300 - n, secs) ; - buf[n++] = 0 ; - hpr_wall(buf) ; -} diff --git a/src/state/s6-linux-init-halt.c b/src/state/s6-linux-init-halt.c deleted file mode 100644 index 4ccca1d..0000000 --- a/src/state/s6-linux-init-halt.c +++ /dev/null @@ -1,7 +0,0 @@ -/* ISC license. */ - -#undef PROGNAME -#define PROGNAME "s6-linux-init-halt" -#undef WHATDEFAULT -#define WHATDEFAULT 1 -#include "hpr.c" diff --git a/src/state/s6-linux-init-poweroff.c b/src/state/s6-linux-init-poweroff.c deleted file mode 100644 index d25dbb6..0000000 --- a/src/state/s6-linux-init-poweroff.c +++ /dev/null @@ -1,7 +0,0 @@ -/* ISC license. */ - -#undef PROGNAME -#define PROGNAME "s6-linux-init-poweroff" -#undef WHATDEFAULT -#define WHATDEFAULT 2 -#include "hpr.c" diff --git a/src/state/s6-linux-init-reboot.c b/src/state/s6-linux-init-reboot.c deleted file mode 100644 index 3612684..0000000 --- a/src/state/s6-linux-init-reboot.c +++ /dev/null @@ -1,7 +0,0 @@ -/* ISC license. */ - -#undef PROGNAME -#define PROGNAME "s6-linux-init-reboot" -#undef WHATDEFAULT -#define WHATDEFAULT 3 -#include "hpr.c" diff --git a/src/state/s6-linux-init-shutdown.c b/src/state/s6-linux-init-shutdown.c deleted file mode 100644 index bb0391d..0000000 --- a/src/state/s6-linux-init-shutdown.c +++ /dev/null @@ -1,268 +0,0 @@ -/* ISC license. */ - -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "defaults.h" -#include "initctl.h" -#include "hpr.h" - -#ifndef UT_NAMESIZE -#define UT_NAMESIZE 32 -#endif - -#define USAGE "s6-linux-init-shutdown [ -h | -p | -r | -k ] [ -f | -F ] [ -a ] [ -t sec ] time [ message ] or s6-linux-init-shutdown -c [ message ]" -#define dieusage() strerr_dieusage(100, USAGE) - -#define AC_FILE "/etc/shutdown.allow" -#define AC_BUFSIZE 4096 -#define AC_MAX 64 -#define AC_SHORT_MESSAGE "no authorized users logged in\n" -#define AC_MESSAGE "s6-linux-init-shutdown: " AC_SHORT_MESSAGE - - - /* shutdown 01:23: date/time format parsing */ - -static inline void add_one_day (struct tm *tm) -{ - tm->tm_isdst = -1 ; - if (tm->tm_mday++ < 31) return ; - tm->tm_mday = 1 ; - if (tm->tm_mon++ < 11) return ; - tm->tm_mon = 0 ; - tm->tm_year++ ; -} - -static inline void parse_hourmin (tain_t *when, char const *s) -{ - tai_t taithen ; - struct tm tmnow, tmthen ; - unsigned int hour, minute ; - size_t len = uint_scan(s, &hour) ; - if (!len || len > 2 || s[len] != ':' || hour > 23) - strerr_dief1x(100, "invalid time format") ; - s += len+1 ; - len = uint0_scan(s, &minute) ; - if (!len || len != 2 || minute > 59) - strerr_dief1x(100, "invalid time format") ; - if (!localtm_from_tai(&tmnow, tain_secp(&STAMP), 1)) - strerr_diefu1sys(111, "break down current time into struct tm") ; - tmthen = tmnow ; - tmthen.tm_hour = hour ; - tmthen.tm_min = minute ; - if (!tai_from_localtm(&taithen, &tmthen)) - strerr_diefu1sys(111, "assemble broken-down time into tain_t") ; - if (tai_less(&taithen, tain_secp(&STAMP))) - { - add_one_day(&tmthen) ; - if (!tai_from_localtm(&taithen, &tmthen)) - strerr_diefu1sys(111, "assemble broken-down time into tain_t") ; - } - tai_sub(tain_secp(when), &taithen, tain_secp(&STAMP)) ; - when->nano = 0 ; -} - -static inline void parse_time (tain_t *when, char const *s) -{ - if (!strcmp(s, "now")) tain_copynow(when) ; - else if (s[0] == '+') - { - unsigned int mins ; - if (!uint0_scan(s+1, &mins)) dieusage() ; - tain_addsec_g(when, mins * 60) ; - } - else if (strchr(s, ':')) parse_hourmin(when, s) ; - else - { - unsigned int mins ; - if (!uint0_scan(s, &mins)) dieusage() ; - tain_addsec_g(when, mins * 60) ; - } -} - - - /* shutdown -a: access control */ - -static inline unsigned char cclass (unsigned char c) -{ - switch (c) - { - case 0 : return 0 ; - case '\n' : return 1 ; - case '#' : return 2 ; - default : return 3 ; - } -} - -static inline unsigned int parse_authorized_users (char *buf, char const **users, unsigned int max) -{ - static unsigned char const table[3][4] = - { - { 0x03, 0x00, 0x01, 0x12 }, - { 0x03, 0x00, 0x01, 0x01 }, - { 0x23, 0x20, 0x02, 0x02 } - } ; - size_t pos = 0 ; - size_t mark = 0 ; - unsigned int n = 0 ; - unsigned int state = 0 ; - for (; state < 3 ; pos++) - { - unsigned char what = table[state][cclass(buf[pos])] ; - state = what & 3 ; - if (what & 0x10) mark = pos ; - if (what & 0x20) - { - if (n >= max) - { - char fmt[UINT32_MAX] ; - fmt[uint32_fmt(fmt, AC_MAX)] = 0 ; - strerr_warnw4x(AC_FILE, " lists more than ", fmt, " authorized users - ignoring the extra ones") ; - break ; - } - buf[pos] = 0 ; - users[n++] = buf + mark ; - } - } - return n ; -} - -static inline int match_users_with_utmp (char const *const *users, unsigned int n) -{ - setutxent() ; - for (;;) - { - struct utmpx *utx ; - errno = 0 ; - utx = getutxent() ; - if (!utx) break ; - if (utx->ut_type != USER_PROCESS) continue ; - for (unsigned int i = 0 ; i < n ; i++) - if (!strncmp(utx->ut_user, users[i], UT_NAMESIZE)) goto yes ; - } - if (errno) strerr_warnwu1sys("getutxent") ; - endutxent() ; - return 0 ; - - yes: - endutxent() ; - return 1 ; -} - -static inline void access_control (void) -{ - char buf[AC_BUFSIZE] ; - char const *users[AC_MAX] ; - unsigned int n ; - struct stat st ; - int fd = open_readb(AC_FILE) ; - if (fd == -1) - { - if (errno == ENOENT) return ; - strerr_diefu2sys(111, "open ", AC_FILE) ; - } - if (fstat(fd, &st) == -1) - strerr_diefu2sys(111, "stat ", AC_FILE) ; - if (st.st_size >= AC_BUFSIZE) - { - char fmt[UINT32_FMT] ; - fmt[uint32_fmt(fmt, AC_BUFSIZE - 1)] = 0 ; - strerr_dief4x(1, AC_FILE, " is too big: it needs to be ", fmt, " bytes or less") ; - } - if (allread(fd, buf, st.st_size) < st.st_size) - strerr_diefu2sys(111, "read ", AC_FILE) ; - fd_close(fd) ; - buf[st.st_size] = 0 ; - n = parse_authorized_users(buf, users, AC_MAX) ; - if (!n || !match_users_with_utmp(users, n)) - { - fd = open_append("/dev/console") ; - if (fd == -1) - strerr_diefu1sys(111, "open /dev/console") ; - if (allwrite(fd, AC_MESSAGE, sizeof(AC_MESSAGE) - 1) < sizeof(AC_MESSAGE) - 1) - strerr_diefu1sys(111, "write to /dev/console") ; - strerr_dief1x(1, AC_SHORT_MESSAGE) ; - } -} - - - /* main */ - -int main (int argc, char const *const *argv) -{ - unsigned int gracetime = 0 ; - int what = 0 ; - int doactl = 0 ; - int docancel = 0 ; - tain_t when ; - PROG = "shutdown" ; - - { - subgetopt_t l = SUBGETOPT_ZERO ; - for (;;) - { - int opt = subgetopt_r(argc, argv, "hprkafFct:", &l) ; - if (opt == -1) break ; - switch (opt) - { - case 'h' : what = 1 ; break ; - case 'p' : what = 2 ; break ; - case 'r' : what = 3 ; break ; - case 'k' : what = 4 ; break ; - case 'a' : doactl = 1 ; break ; - case 'f' : /* talk to the hand */ break ; - case 'F' : /* no, the other hand */ break ; - case 'c' : docancel = 1 ; break ; - case 't' : if (!uint0_scan(l.arg, &gracetime)) dieusage() ; break ; - default : strerr_dieusage(100, USAGE) ; - } - } - argc -= l.ind ; argv += l.ind ; - } - - if (geteuid()) - { - errno = EPERM ; - strerr_diefu1sys(111, "shutdown") ; - } - if (doactl) access_control() ; - if (!tain_now_g()) strerr_warnw1sys("get current time") ; - if (docancel) - { - if (argv[0]) hpr_wall(argv[0]) ; - if (!hpr_cancel()) goto err ; - return 0 ; - } - if (!argc) dieusage() ; - parse_time(&when, argv[0]) ; - if (argv[1]) hpr_wall(argv[1]) ; - if (what < 4) - { - if (gracetime > 300) - { - gracetime = 300 ; - strerr_warnw1x("delay between SIGTERM and SIGKILL is capped to 300 seconds") ; - } - if (!hpr_shutdown(what, &when, gracetime * 1000)) goto err ; - } - return 0 ; - - err: - strerr_diefu2sys(111, "write to ", INITCTL) ; -} diff --git a/src/state/s6-linux-init-shutdownd.c b/src/state/s6-linux-init-shutdownd.c deleted file mode 100644 index e330ac6..0000000 --- a/src/state/s6-linux-init-shutdownd.c +++ /dev/null @@ -1,241 +0,0 @@ -/* ISC license. */ - -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "defaults.h" -#include "initctl.h" -#include "hpr.h" - -#define STAGE4_FILE "stage 4" - -#define USAGE "s6-linux-init-shutdownd [ -b bindir ] [ -c basedir ] [ -g gracetime ]" -#define dieusage() strerr_dieusage(100, USAGE) - -static char const *basedir = BASEDIR ; - -static inline void prepare_shutdown (char c, unsigned int *what, tain_t *deadline, unsigned int *grace_time) -{ - uint32_t u ; - char pack[TAIN_PACK] ; - ssize_t r = sanitize_read(buffer_get(buffer_0small, pack, TAIN_PACK)) ; - if (r == -1) strerr_diefu1sys(111, "read from pipe") ; - if (r < TAIN_PACK + 4) strerr_dief1x(101, "bad shutdown protocol") ; - tain_unpack(pack, deadline) ; - uint32_unpack_big(pack + TAIN_PACK, &u) ; - if (u && u <= 300000) *grace_time = u ; - *what = 1 + byte_chr("hpr", c, 3) ; -} - -static inline void handle_stdin (unsigned int *what, tain_t *deadline, unsigned int *grace_time) -{ - for (;;) - { - char c ; - ssize_t r = sanitize_read(buffer_get(buffer_0small, &c, 1)) ; - if (r == -1) strerr_diefu1sys(111, "read from pipe") ; - else if (!r) break ; - switch (c) - { - case 'h' : - case 'p' : - case 'r' : - prepare_shutdown(c, what, deadline, grace_time) ; - break ; - case 'c' : - *what = 0 ; - tain_add_g(deadline, &tain_infinite_relative) ; - break ; - default : - { - char s[2] = { c, 0 } ; - strerr_warnw2x("unknown command: ", s) ; - } - break ; - } - } -} - -static inline void prepare_stage4 (char const *bindir, char const *basedir, unsigned int what) -{ - static char const *table[3] = { "halt", "poweroff", "reboot" } ; - stralloc sa = STRALLOC_ZERO ; - buffer b ; - char buf[512] ; - int fd ; - unlink_void(STAGE4_FILE ".new") ; - fd = open_excl(STAGE4_FILE ".new") ; - if (fd == -1) strerr_diefu3sys(111, "open ", STAGE4_FILE ".new", " for writing") ; - buffer_init(&b, &buffer_write, fd, buf, 512) ; - - if (buffer_puts(&b, "#!") == -1 - || buffer_puts(&b, bindir) == -1 - || buffer_puts(&b, "/execlineb -P\n\n") == -1) - strerr_diefu2sys(111, "write to ", STAGE4_FILE ".new") ; - if (!string_quote(&sa, basedir, strlen(basedir))) - strerr_diefu1sys(111, "string_quote") ; - if (buffer_put(&b, sa.s, sa.len) == -1 - || buffer_puts(&b, "/bin/" STAGE4 " ") == -1 - || buffer_puts(&b, table[what-1]) == -1 - || buffer_putsflush(&b, "\n") == -1) - strerr_diefu2sys(111, "write to ", STAGE4_FILE ".new") ; - stralloc_free(&sa) ; - if (fchmod(fd, S_IRWXU) == -1) - strerr_diefu2sys(111, "fchmod ", STAGE4_FILE ".new") ; - if (fsync(fd) == -1) - strerr_diefu2sys(111, "fsync ", STAGE4_FILE ".new") ; - fd_close(fd) ; - if (rename(STAGE4_FILE ".new", STAGE4_FILE) == -1) - strerr_diefu4sys(111, "rename ", STAGE4_FILE ".new", " to ", STAGE4_FILE) ; -} - -int main (int argc, char const *const *argv) -{ - char const *bindir = BINDIR ; - pid_t pid ; - tain_t deadline ; - unsigned int what = 0 ; - unsigned int grace_time = 3000 ; - int notif = 0 ; - PROG = "s6-linux-init-shutdownd" ; - - { - subgetopt_t l = SUBGETOPT_ZERO ; - for (;;) - { - int opt = subgetopt_r(argc, argv, "b:c:g:", &l) ; - if (opt == -1) break ; - switch (opt) - { - case 'b' : bindir = l.arg ; break ; - case 'c' : basedir = l.arg ; break ; - case 'g' : if (!uint0_scan(l.arg, &grace_time)) dieusage() ; break ; - default : dieusage() ; - } - } - argc -= l.ind ; argv += l.ind ; - } - if (bindir[0] != '/') - strerr_dief2x(100, "bindir", " must be an absolute path") ; - if (basedir[0] != '/') - strerr_dief2x(100, "basedir", " must be an absolute path") ; - if (grace_time > 300000) grace_time = 300000 ; - - /* if we're in stage 4, exec it immediately */ - { - char const *stage4_argv[2] = { STAGE4_FILE, 0 } ; - pathexec_run(stage4_argv[0], stage4_argv, (char const *const *)environ) ; - if (errno != ENOENT) - strerr_warnwu2sys("exec ", stage4_argv[0]) ; - } - - fd_close(1) ; - fd_close(0) ; - - if (open_read(SHUTDOWND_FIFO)) - strerr_diefu3sys(111, "open ", SHUTDOWND_FIFO, " for reading") ; - if (open_write(SHUTDOWND_FIFO) != 1) - strerr_diefu3sys(111, "open ", SHUTDOWND_FIFO, " for writing") ; - if (sig_ignore(SIGPIPE) == -1) - strerr_diefu1sys(111, "sig_ignore SIGPIPE") ; - tain_now_g() ; - tain_add_g(&deadline, &tain_infinite_relative) ; - - for (;;) - { - iopause_fd x = { .fd = 0, .events = IOPAUSE_READ } ; - int r = iopause_g(&x, 1, &deadline) ; - if (r == -1) strerr_diefu1sys(111, "iopause") ; - if (!r) - { - if (what) break ; - tain_add_g(&deadline, &tain_infinite_relative) ; - continue ; - } - if (x.revents & IOPAUSE_READ) - handle_stdin(&what, &deadline, &grace_time) ; - } - - - /* Stage 3 */ - - fd_close(1) ; - fd_close(0) ; - if (open_readb("/dev/null")) - strerr_diefu1sys(111, "open /dev/null for reading") ; - if (open_write("/dev/console") != 1 || ndelay_off(1) == -1) - strerr_diefu1sys(111, "open /dev/console for writing") ; - { - size_t basedirlen = strlen(basedir) ; - char stage3[basedirlen + sizeof("/" STAGE3)] ; - char const *stage3_argv[2] = { stage3, 0 } ; - memcpy(stage3, basedir, basedirlen) ; - memcpy(stage3 + basedirlen, "/" STAGE3, sizeof("/" STAGE3)) ; - sig_block(SIGCHLD) ; - pid = child_spawn0(stage3_argv[0], stage3_argv, (char const *const *)environ) ; - if (!pid) strerr_warnwu2sys("spawn ", stage3) ; - sig_unblock(SIGCHLD) ; - if (pid) - { - int wstat ; - if (wait_pid(pid, &wstat) == -1) strerr_diefu1sys(111, "waitpid") ; - if (WIFSIGNALED(wstat)) - { - char fmt[UINT_FMT] ; - fmt[uint_fmt(fmt, WTERMSIG(wstat))] = 0 ; - strerr_warnw3x(stage3, " was killed by signal ", fmt) ; - } - else if (WEXITSTATUS(wstat)) - { - char fmt[UINT_FMT] ; - fmt[uint_fmt(fmt, WTERMSIG(wstat))] = 0 ; - strerr_warnw3x(stage3, " was killed by signal ", fmt) ; - } - else if (WEXITSTATUS(wstat)) - { - char fmt[UINT_FMT] ; - fmt[uint_fmt(fmt, WEXITSTATUS(wstat))] = 0 ; - strerr_warnw3x(stage3, " exited ", fmt) ; - } - } - } - - - /* The end is coming! */ - - prepare_stage4(bindir, basedir, what) ; - if (fd_move(2, 1) == -1) strerr_warnwu1sys("fd_move") ; - sync() ; - if (sig_ignore(SIGTERM) == -1) strerr_warnwu1sys("sig_ignore SIGTERM") ; - strerr_warni1x("sending all processes the TERM signal...") ; - kill(-1, SIGTERM) ; - kill(-1, SIGCONT) ; - tain_from_millisecs(&deadline, grace_time) ; - tain_add_g(&deadline, &deadline) ; - deepsleepuntil_g(&deadline) ; - sync() ; - strerr_warni1x("sending all processes the KILL signal...") ; - kill(-1, SIGKILL) ; - return 0 ; -} diff --git a/src/state/s6-linux-init-single.c b/src/state/s6-linux-init-single.c deleted file mode 100644 index 4f23340..0000000 --- a/src/state/s6-linux-init-single.c +++ /dev/null @@ -1,54 +0,0 @@ -/* ISC license. */ - -#include -#include -#include - -#include -#include -#include - -#include "initctl.h" - -#define USAGE "s6-linux-init-single" -#define dieusage() strerr_dieusage(100, USAGE) - -#define BANNER "Executing interactive root shell.\n" - -int main (void) -{ - int fd ; - PROG = "s6-linux-init-single" ; - fd = open(EARLYGETTY, O_RDONLY) ; - if (fd == -1) - { - if (errno != ENOENT) - strerr_diefu2sys(111, "opendir ", EARLYGETTY) ; - goto shell ; - } - if (faccessat(fd, "down", F_OK, 0) == 0) - { - fd_close(fd) ; - goto shell ; - } - if (errno != ENOENT) - strerr_diefu3sys(111, "access ", EARLYGETTY, "/down") ; - return 0 ; - - shell: - - fd = open("/dev/console", O_RDONLY) ; - if (fd == -1) strerr_diefu2sys(111, "open /dev/console for ", "reading") ; - if (fd_move(0, fd) == -1) strerr_diefu1sys(111, "move /dev/console fd to 0") ; - fd = open("/dev/console", O_WRONLY) ; - if (fd == -1) strerr_diefu2sys(111, "open /dev/console for ", "writing") ; - if (fd_move(1, fd) == -1) strerr_diefu1sys(111, "move /dev/console fd to 1") ; - if (fd_copy(2, 1) == -1) strerr_diefu1sys(111, "dup2 1 to 2") ; - if (allwrite(1, BANNER, sizeof(BANNER) - 1) < sizeof(BANNER) - 1) - strerr_diefu1sys(111, "write banner") ; - { - char *argv[3] = { "/bin/sh", "-i", 0 } ; - execv(argv[0], argv) ; - strerr_dieexec(111, argv[0]) ; - } -} diff --git a/src/state/s6-linux-init-telinit.c b/src/state/s6-linux-init-telinit.c deleted file mode 100644 index 856e78b..0000000 --- a/src/state/s6-linux-init-telinit.c +++ /dev/null @@ -1,42 +0,0 @@ -/* ISC license. */ - -#include -#include -#include - -#include "defaults.h" -#include "initctl.h" -#include "hpr.h" - -#define USAGE "s6-linux-init-telinit runlevel" -#define dieusage() strerr_dieusage(100, USAGE) - -int main (int argc, char const *const *argv, char const *const *envp) -{ - PROG = "s6-linux-init-telinit" ; - { - 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' : - case 'c' : - case 'p' : - case 's' : - case 'm' : - case 'd' : break ; - default : dieusage() ; - } - } - argc -= l.ind ; argv += l.ind ; - } - - if (!argc) dieusage() ; - if (!argv[0][0] || argv[0][1]) dieusage() ; - if (argv[0][0] != 'S' && (argv[0][0] < '0' || argv[0][0] > '6')) dieusage() ; - if (!hpr_send(argv[0], 1)) strerr_diefu2sys(111, "write to ", INITCTL) ; - return 0 ; -} -- cgit v1.2.3