summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/include-local/defaults.h9
-rw-r--r--src/include-local/initctl.h34
-rw-r--r--src/include/s6-linux-init/s6-linux-init.h8
-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-linux-init-maker2
-rw-r--r--src/init/deps-exe/s6-linux-init-telinit (renamed from src/init/deps-exe/s6-poweroff)0
-rw-r--r--src/init/hpr.c107
-rw-r--r--src/init/s6-halt.c7
-rw-r--r--src/init/s6-linux-init-maker.c666
-rw-r--r--src/init/s6-linux-init-telinit.c74
-rw-r--r--src/init/s6-linux-init.c189
-rw-r--r--src/init/s6-poweroff.c7
-rw-r--r--src/init/s6-reboot.c7
-rw-r--r--src/lib/deps-lib/s6_linux_init4
-rw-r--r--src/lib/s6_linux_init_logouthook.c19
-rw-r--r--src/misc/deps-exe/s6-linux-init-echo (renamed from src/init/deps-exe/s6-reboot)0
-rw-r--r--src/misc/deps-exe/s6-linux-init-logouthookd4
-rw-r--r--src/misc/deps-exe/s6-linux-init-umountall1
-rw-r--r--src/misc/s6-linux-init-echo.c39
-rw-r--r--src/misc/s6-linux-init-logouthookd.c69
-rw-r--r--src/misc/s6-linux-init-umountall.c70
-rw-r--r--src/shutdown/deps-exe/s6-linux-init-hpr5
-rw-r--r--src/shutdown/deps-exe/s6-linux-init-shutdown5
-rw-r--r--src/shutdown/deps-exe/s6-linux-init-shutdownd5
-rw-r--r--src/shutdown/deps-lib/hpr2
-rw-r--r--src/shutdown/hpr.h20
-rw-r--r--src/shutdown/hpr_shutdown.c16
-rw-r--r--src/shutdown/hpr_wall.c41
-rw-r--r--src/shutdown/s6-linux-init-hpr.c120
-rw-r--r--src/shutdown/s6-linux-init-shutdown.c265
-rw-r--r--src/shutdown/s6-linux-init-shutdownd.c309
31 files changed, 1721 insertions, 383 deletions
diff --git a/src/include-local/defaults.h b/src/include-local/defaults.h
new file mode 100644
index 0000000..a28e2e5
--- /dev/null
+++ b/src/include-local/defaults.h
@@ -0,0 +1,9 @@
+/* ISC license. */
+
+#ifndef S6_LINUX_INIT_DEFAULTS_H
+#define S6_LINUX_INIT_DEFAULTS_H
+
+#define BASEDIR "/etc/s6-linux-init/current"
+#define INITPATH "/usr/bin:/bin"
+
+#endif
diff --git a/src/include-local/initctl.h b/src/include-local/initctl.h
new file mode 100644
index 0000000..507aefe
--- /dev/null
+++ b/src/include-local/initctl.h
@@ -0,0 +1,34 @@
+/* ISC license. */
+
+#ifndef S6_LINUX_INIT_INITCTL_H
+#define S6_LINUX_INIT_INITCTL_H
+
+#include <s6-linux-init/config.h>
+
+#define SCANDIR "service"
+
+#define SHUTDOWND_SERVICEDIR "s6-linux-init-shutdownd"
+#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
+
+#define LOGGER_SERVICEDIR "s6-svscan-log"
+#define LOGGER_FIFO "fifo"
+#define LOGFIFO S6_LINUX_INIT_TMPFS "/" SCANDIR "/" LOGGER_SERVICEDIR "/" LOGGER_FIFO
+
+#define EARLYGETTY_SERVICEDIR "s6-linux-init-early-getty"
+#define EARLYGETTY S6_LINUX_INIT_TMPFS "/" SCANDIR "/" EARLYGETTY_SERVICEDIR
+
+#define RUNIMAGE "run-image"
+#define ENVSTAGE1 "env"
+#define STAGE2 "rc.init"
+#define STAGE3 "rc.shutdown"
+
+#endif
diff --git a/src/include/s6-linux-init/s6-linux-init.h b/src/include/s6-linux-init/s6-linux-init.h
new file mode 100644
index 0000000..ed8e684
--- /dev/null
+++ b/src/include/s6-linux-init/s6-linux-init.h
@@ -0,0 +1,8 @@
+/* ISC license. */
+
+#ifndef S6_LINUX_INIT_H
+#define S6_LINUX_INIT_H
+
+extern int s6_linux_init_logouthook (void) ;
+
+#endif
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-linux-init-maker b/src/init/deps-exe/s6-linux-init-maker
index e7187fe..ee4ed0b 100644
--- a/src/init/deps-exe/s6-linux-init-maker
+++ b/src/init/deps-exe/s6-linux-init-maker
@@ -1 +1,3 @@
+${LIBNSSS}
-lskarnet
+${MAYBEPTHREAD_LIB}
diff --git a/src/init/deps-exe/s6-poweroff b/src/init/deps-exe/s6-linux-init-telinit
index e7187fe..e7187fe 100644
--- a/src/init/deps-exe/s6-poweroff
+++ b/src/init/deps-exe/s6-linux-init-telinit
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..1005811 100644
--- a/src/init/s6-linux-init-maker.c
+++ b/src/init/s6-linux-init-maker.c
@@ -4,6 +4,8 @@
#include <string.h>
#include <unistd.h>
#include <errno.h>
+#include <pwd.h>
+
#include <skalibs/uint64.h>
#include <skalibs/types.h>
#include <skalibs/bytestr.h>
@@ -16,260 +18,235 @@
#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 <execline/config.h>
+
+#include <s6/config.h>
+
+#include <s6-linux-init/config.h>
+#include "defaults.h"
+#include "initctl.h"
+
+#ifdef S6_LINUX_INIT_UTMPD_PATH
+# include <utmps/config.h>
+# define USAGE "s6-linux-init-maker [ -c basedir ] [ -u log_user ] [ -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 ] [ -D initdefault ] [ -n | -N ] [ -U utmp_user ] dir"
+# define OPTION_STRING "c:u:G:1Lp:m:t:d:s:e:E:q:D:nNU:"
+# define UTMPS_DIR "utmps"
+#else
+# define USAGE "s6-linux-init-maker [ -c basedir ] [ -u log_user ] [ -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 ] [ -D initdefault ] [ -n | -N ] dir"
+# define OPTION_STRING "c:u:G:1Lp:m:t:d:s:e:E:q:D:nN"
+#endif
+
#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 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 crashed. 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" ;
+#define UNCAUGHT_DIR "uncaught-logs"
+
+static char const *robase = BASEDIR ;
+static char const *initial_path = INITPATH ;
static char const *env_store = 0 ;
static char const *early_getty = 0 ;
-static uid_t uncaught_logs_uid = 0 ;
-static gid_t uncaught_logs_gid = 0 ;
-static unsigned int initial_umask = 022 ;
+static char const *slashdev = 0 ;
+static char const *log_user = "root" ;
+static char const *initdefault = 0 ;
+static unsigned int initial_umask = 0022 ;
static unsigned int timestamp_style = 1 ;
-static unsigned int slashdev_style = 2 ;
-static unsigned int finalsleep = 2000 ;
-static int redirect_stage2 = 0 ;
-static int in_namespace = 0 ;
+static unsigned int finalsleep = 3000 ;
+static int mounttype = 1 ;
+static int console = 0 ;
+static int logouthookd = 0 ;
-typedef int writetobuf_func_t (buffer *) ;
+#ifdef S6_LINUX_INIT_UTMPD_PATH
+static char const *utmp_user = "utmp" ;
+#endif
+
+typedef int writetobuf_func_t (buffer *, char const *) ;
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, char const *options)
{
- return buffer_puts(b, "#!") >= 0
- && buffer_puts(b, bindir) >= 0
- && buffer_puts(b, "/execlineb -P\n\n") >= 0 ;
+ return buffer_puts(b, "#!" EXECLINE_SHEBANGPREFIX "execlineb ") >= 0
+ && buffer_puts(b, options && options[0] ? options : "-P") >= 0
+ && buffer_puts(b, "\n\n") >= 0 ;
}
-static int early_getty_script (buffer *b)
+static int line_script (buffer *b, char const *line)
{
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, char const *line)
+{
+ return put_shebang_options(b, "-S0")
+ && buffer_puts(b, line) >= 0
+ && buffer_puts(b, " $@\n") >= 0 ;
+}
+
+static int hpr_script (buffer *b, char const *what)
+{
+ return put_shebang_options(b, "-S0")
+ && buffer_puts(b, S6_LINUX_INIT_BINPREFIX "s6-linux-init-hpr -") >= 0
+ && buffer_puts(b, what) >= 0
+ && buffer_puts(b, " $@\n") >= 0 ;
+}
+
+static int death_script (buffer *b, char const *s)
{
return put_shebang(b)
- && buffer_puts(b, CRASH_SCRIPT) >= 0 ;
+ && buffer_puts(b,
+ EXECLINE_EXTBINPREFIX "redirfd -w 1 /dev/console\n"
+ EXECLINE_EXTBINPREFIX "fdmove -c 2 1\n"
+ EXECLINE_EXTBINPREFIX "foreground { "
+ S6_LINUX_INIT_BINPREFIX "s6-linux-init-echo -- \"s6-svscan ") >= 0
+ && buffer_puts(b, s) >= 0
+ && buffer_puts(b,
+ ". Rebooting.\" }\n"
+ S6_LINUX_INIT_BINPREFIX "s6-linux-init-hpr -r -f\n") >= 0 ;
}
-static int s6_svscan_log_script (buffer *b)
+static int s6_svscan_log_script (buffer *b, char const *data)
{
size_t sabase = satmp.len ;
- char fmt[UINT64_FMT] ;
if (!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 ;
- }
+ 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"
+ EXECLINE_EXTBINPREFIX "redirfd -rnb 0 " LOGGER_FIFO "\n"
+ S6_EXTBINPREFIX "s6-setuidgid ") < 0
+ || !string_quote(&satmp, log_user, strlen(log_user))) return 0 ;
+ if (buffer_put(b, satmp.s + sabase, satmp.len - sabase) < 0) goto err ;
satmp.len = sabase ;
- if (buffer_puts(b, "/uncaught-logs\n") < 0) return 0 ;
+ if (buffer_puts(b, "\ns6-log -bpd3 -- ") < 0) return 0 ;
+ if (console && buffer_puts(b, "1 ") < 0) return 0 ;
+ if (timestamp_style & 1 && buffer_puts(b, "t ") < 0
+ || 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 finish_script (buffer *b)
-{
- 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 ;
}
-static int sig_script (buffer *b, char c)
+static int logouthookd_script (buffer *b, char const *data)
+{
+ (void)data ;
+ return put_shebang(b)
+ && buffer_puts(b,
+ S6_EXTBINPREFIX "s6-ipcserver -1 -a 0700 -c 1000 -C 1000 -- " LOGOUTHOOKD_SOCKET "\n"
+ S6_LINUX_INIT_BINPREFIX "s6-linux-init-logouthookd\n") >= 0 ;
+}
+
+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, "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 ;
- 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 ;
+ || buffer_puts(b, S6_LINUX_INIT_BINPREFIX "s6-linux-init-shutdownd -c ") < 0
+ || !string_quote(&satmp, robase, strlen(robase))) return 0 ;
if (buffer_put(b, satmp.s + sabase, satmp.len - sabase) < 0) goto err ;
satmp.len = sabase ;
- if (buffer_puts(b, "/service\n") < 0) return 0 ;
+ if (buffer_puts(b, " -g ") < 0
+ || buffer_put(b, fmt, uint_fmt(fmt, finalsleep)) < 0
+ || buffer_puts(b, "\n") < 0) return 0 ;
+ (void)data ;
return 1 ;
+
err:
satmp.len = sabase ;
- return 0 ;
+ return 0 ;
}
-static int onlyexit (buffer *b, char c)
+static int runleveld_script (buffer *b, char const *data)
{
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 ;
+ || buffer_puts(b,
+ EXECLINE_EXTBINPREFIX "fdmove -c 2 1\n"
+ EXECLINE_EXTBINPREFIX "fdmove 1 3\n"
+ S6_EXTBINPREFIX "s6-ipcserver -1 -a 0700 -c 1 -- " RUNLEVELD_SOCKET "\n"
+ S6_EXTBINPREFIX "s6-sudod -0 -1 -2 -t 30000 --\n") < 0
+ || !string_quote(&satmp, robase, strlen(robase))) return 0 ;
+ if (buffer_put(b, satmp.s + sabase, satmp.len - 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 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') ;
-}
-
-static int sigint_script (buffer *b)
-{
- return in_namespace ? onlyexit(b, '1') : sig_script(b, '6') ;
-}
-
-static int sigusr1_script (buffer *b)
+static int sig_script (buffer *b, char const *option)
{
- return sig_script(b, '7') ;
-}
-
-static int sigusr2_script (buffer *b)
-{
- return sig_script(b, '0') ;
+ return put_shebang(b)
+ && buffer_puts(b, S6_LINUX_INIT_BINPREFIX "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, 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 ;
+ size_t sabase = satmp.len ;
+ if (!put_shebang_options(b, "-S0")
+ || buffer_puts(b, S6_LINUX_INIT_BINPREFIX "s6-linux-init -c ") < 0
+ || !string_quote(&satmp, robase, strlen(robase))) 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
- || !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, "if { s6-mount -nt devtmpfs dev /dev }\n") < 0) goto err ;
+ char fmt[UINT_OFMT] ;
+ if (buffer_puts(b, " -m 00") < 0
+ || buffer_put(b, fmt, uint_ofmt(fmt, initial_umask)) < 0) return 0 ;
+ }
+ if (initial_path)
+ {
+ if (buffer_puts(b, " -p ") < 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 (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_put(b, satmp.s + sabase, satmp.len - 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_put(b, satmp.s + sabase, satmp.len - sabase) < 0) goto err ;
+ satmp.len = sabase ;
+ }
+ if (initdefault)
+ {
+ if (buffer_puts(b, " -D ") < 0
+ || !string_quote(&satmp, initdefault, strlen(initdefault))) return 0 ;
+ if (buffer_put(b, satmp.s + sabase, satmp.len - sabase) < 0) goto err ;
+ satmp.len = sabase ;
+ }
+ if (mounttype == 2)
+ {
+ if (buffer_puts(b, " -n") < 0) return 0 ;
+ }
+ else if (!mounttype)
+ {
+ if (buffer_puts(b, " -N") < 0) return 0 ;
}
- 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 ;
+ (void)data ;
return 1 ;
+
err:
satmp.len = sabase ;
return 0 ;
@@ -282,7 +259,7 @@ static void cleanup (char const *base)
errno = e ;
}
-static void auto_dir (char const *base, char const *dir, uid_t uid, gid_t gid, unsigned int mode)
+static void auto_dir_internal (char const *base, char const *dir, uid_t uid, gid_t gid, unsigned int mode, int strict)
{
size_t clen = strlen(base) ;
size_t dlen = strlen(dir) ;
@@ -290,15 +267,26 @@ static void auto_dir (char const *base, char const *dir, uid_t uid, gid_t gid, u
memcpy(fn, base, clen) ;
fn[clen] = dlen ? '/' : 0 ;
memcpy(fn + clen + 1, dir, dlen + 1) ;
- if (mkdir(fn, mode) < 0
- || ((uid || gid) && (chown(fn, uid, gid) < 0 || chmod(fn, mode) < 0)))
+
+ if (mkdir(fn, mode) < 0)
{
- cleanup(base) ;
- strerr_diefu2sys(111, "mkdir ", fn) ;
+ if (errno != EEXIST || strict) goto err ;
+ }
+ else if (uid || gid)
+ {
+ if (chown(fn, uid, gid) < 0
+ || chmod(fn, mode) < 0) goto err ;
}
+ return ;
+
+ err:
+ cleanup(base) ;
+ strerr_diefu2sys(111, "mkdir ", fn) ;
}
-static void auto_file (char const *base, char const *file, char const *s, unsigned int n, int executable)
+#define auto_dir(base, dir, uid, gid, mode) auto_dir_internal(base, dir, uid, gid, (mode), 1)
+
+static void auto_file (char const *base, char const *file, char const *s, unsigned int n)
{
size_t clen = strlen(base) ;
size_t flen = strlen(file) ;
@@ -307,13 +295,28 @@ 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, 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)
+ {
+ cleanup(base) ;
+ 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 +332,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, char const *data)
{
char buf[4096] ;
buffer b ;
@@ -347,56 +350,215 @@ 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 copy_script (char const *base, char const *src, char const *dst)
{
- auto_dir(base, "env", 0, 0, 0755) ;
+ size_t baselen = strlen(base) ;
+ size_t dstlen = strlen(dst) ;
+ char fn[baselen + dstlen + 2] ;
+ memcpy(fn, base, baselen) ;
+ fn[baselen] = '/' ;
+ memcpy(fn + baselen + 1, dst, dstlen + 1) ;
+ if (!filecopy_unsafe(src, fn, 0755))
+ {
+ cleanup(base) ;
+ strerr_diefu4sys(111, "copy ", src, " to ", fn) ;
+ }
+}
+
+static void auto_exec (char const *base, char const *name, char const *target)
+{
+ if (S6_LINUX_INIT_BINPREFIX[0] == '/')
+ {
+ size_t len = strlen(target) ;
+ char fn[sizeof(S6_LINUX_INIT_BINPREFIX) + len] ;
+ memcpy(fn, S6_LINUX_INIT_BINPREFIX, sizeof(S6_LINUX_INIT_BINPREFIX) - 1) ;
+ memcpy(fn + sizeof(S6_LINUX_INIT_BINPREFIX) - 1, target, len + 1) ;
+ auto_symlink(base, name, fn) ;
+ }
+ else
+ auto_script(base, name, &linewithargs_script, target) ;
+}
+
+static void make_env (char const *base, char const *envname, char *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) ;
- else auto_file(base, fn, "", 0, 0) ;
+ if (pos + 1 < len)
+ {
+ modif[len] = '\n' ;
+ auto_file(base, fn, modif + pos + 1, len - pos) ;
+ modif[len] = 0 ;
+ }
+ else if (pos + 1 == len) auto_file(base, fn, "\n", 1) ;
+ else auto_file(base, fn, "", 0) ;
modif += len+1 ; modiflen -= len+1 ;
}
}
+static void getug (char const *base, char const *s, uid_t *uid, gid_t *gid)
+{
+ struct passwd *pw ;
+ errno = 0 ;
+ pw = getpwnam(s) ;
+ if (!pw)
+ {
+ cleanup(base) ;
+ if (!errno) strerr_diefu3x(100, "find user ", s, " in passwd database") ;
+ else strerr_diefu2sys(111, "getpwnam for ", s) ;
+ }
+ *uid = pw->pw_uid ;
+ *gid = pw->pw_gid ;
+}
+
+#ifdef S6_LINUX_INIT_UTMPD_PATH
+
+static inline void auto_basedir (char const *base, char const *dir, uid_t uid, gid_t gid, unsigned int mode)
+{
+ size_t n = strlen(dir) ;
+ char tmp[n + 1] ;
+ for (size_t i = 0 ; i < n ; i++)
+ {
+ if ((dir[i] == '/') && i)
+ {
+ tmp[i] = 0 ;
+ auto_dir_internal(base, tmp, uid, gid, mode, 0) ;
+ }
+ tmp[i] = dir[i] ;
+ }
+}
+
+static int utmpd_script (buffer *b, char const *uw)
+{
+ size_t sabase = satmp.len ;
+ if (!put_shebang(b)
+ || buffer_puts(b,
+ EXECLINE_EXTBINPREFIX "fdmove -c 2 1\n"
+ S6_EXTBINPREFIX "s6-setuidgid ") < 0
+ || !string_quote(&satmp, utmp_user, strlen(utmp_user))) return 0 ;
+ if (buffer_put(b, satmp.s + sabase, satmp.len - sabase) < 0) goto err ;
+ satmp.len = sabase ;
+ if (buffer_puts(b, "\n"
+ EXECLINE_EXTBINPREFIX "cd " S6_LINUX_INIT_TMPFS "/" UTMPS_DIR "\n"
+ EXECLINE_EXTBINPREFIX "fdmove 1 3\n"
+ S6_EXTBINPREFIX "s6-ipcserver -1 -c 1000 -- ") < 0) return 0 ;
+ if (buffer_puts(b, uw[0] == 'u' ? UTMPS_UTMPD_PATH : UTMPS_WTMPD_PATH) < 0
+ || buffer_puts(b, "\n"
+ UTMPS_EXTBINPREFIX "utmps-") < 0
+ || buffer_puts(b, uw) < 0
+ || buffer_puts(b, "tmpd\n") < 0) return 0 ;
+ return 1 ;
+
+ err:
+ satmp.len = sabase ;
+ return 0 ;
+}
+
+static inline void make_utmps (char const *base)
+{
+ auto_dir(base, "run-image/" SCANDIR "/utmpd", 0, 0, 0755) ;
+ auto_file(base, "run-image/" SCANDIR "/utmpd/notification-fd", "3\n", 2) ;
+ auto_script(base, "run-image/" SCANDIR "/utmpd/run", &utmpd_script, "u") ;
+ auto_dir(base, "run-image/" SCANDIR "/wtmpd", 0, 0, 0755) ;
+ auto_file(base, "run-image/" SCANDIR "/wtmpd/notification-fd", "3\n", 2) ;
+ auto_script(base, "run-image/" SCANDIR "/wtmpd/run", &utmpd_script, "w") ;
+ {
+ uid_t uid ;
+ gid_t gid ;
+ getug(base, utmp_user, &uid, &gid) ;
+ auto_dir(base, "run-image/" UTMPS_DIR, uid, gid, 0755) ;
+ auto_basedir(base, "run-image/" S6_LINUX_INIT_UTMPD_PATH, uid, gid, 0755) ;
+ auto_basedir(base, "run-image/" S6_LINUX_INIT_WTMPD_PATH, uid, gid, 0755) ;
+ }
+}
+
+#endif
+
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) ;
+ {
+ uid_t uid ;
+ gid_t gid ;
+ getug(base, log_user, &uid, &gid) ;
+ auto_dir(base, "run-image/" UNCAUGHT_DIR, uid, 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) ;
+ 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_file(base, "run-image/" SCANDIR "/" RUNLEVELD_SERVICEDIR "/notification-fd", "3\n", 2) ;
+ 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) ;
+ 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) ;
+
+#ifdef S6_LINUX_INIT_UTMPD_PATH
+ if (utmp_user[0]) make_utmps(base) ;
+#endif
+}
+
+static inline void make_scripts (char const *base)
+{
+ auto_dir(base, "scripts", 0, 0, 0755) ;
+ copy_script(base, S6_LINUX_INIT_SKELDIR "/runlevel", "scripts/runlevel") ;
+ copy_script(base, S6_LINUX_INIT_SKELDIR "/" STAGE2, "scripts/" STAGE2) ;
+ copy_script(base, S6_LINUX_INIT_SKELDIR "/" STAGE3, "scripts/" STAGE3) ;
+}
+
+static inline void make_bins (char const *base)
+{
+ auto_dir(base, "bin", 0, 0, 0755) ;
+ auto_script(base, "bin/init", &stage1_script, 0) ;
+ auto_script(base, "bin/halt", &hpr_script, "h") ;
+ auto_script(base, "bin/poweroff", &hpr_script, "p") ;
+ auto_script(base, "bin/reboot", &hpr_script, "r") ;
+ auto_exec(base, "bin/shutdown", "s6-linux-init-shutdown") ;
+ auto_exec(base, "bin/telinit", "s6-linux-init-telinit") ;
}
int main (int argc, char const *const *argv, char const *const *envp)
@@ -406,36 +568,28 @@ 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:l:b:u:g:UG:2:rZ:p:m:t:d:s:e:nq:", &l) ;
+ int opt = subgetopt_r(argc, argv, OPTION_STRING, &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 ;
- case 'U' :
- {
- char const *x = env_get2(envp, "UID") ;
- if (!x) strerr_dienotset(100, "UID") ;
- if (!uint0_scan(x, &uncaught_logs_uid)) strerr_dieinvalid(100, "UID") ;
- x = env_get2(envp, "GID") ;
- if (!x) strerr_dienotset(100, "GID") ;
- if (!uint0_scan(x, &uncaught_logs_gid)) strerr_dieinvalid(100, "GID") ;
- }
+ case 'u' : log_user = l.arg ; break ;
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 '1' : console = 1 ; 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, &timestamp_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 'q' : if (!uint0_scan(l.arg, &finalsleep)) dieusage() ; break ;
+ case 'D' : initdefault = l.arg ; break ;
+ case 'n' : mounttype = 2 ; break ;
+ case 'N' : mounttype = 0 ; break ;
+#ifdef S6_LINUX_INIT_UTMPD_PATH
+ case 'U' : utmp_user = l.arg ; break ;
+#endif
default : dieusage() ;
}
}
@@ -445,24 +599,26 @@ 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 directory ", slashdev, " is not absolute") ;
+ if (env_store)
+ {
+ if (env_store[0] != '/')
+ strerr_dief3x(100, "kernel environment store ", env_store, " is not absolute") ;
+ if (!str_start(env_store, S6_LINUX_INIT_TMPFS "/"))
+ strerr_warnw3x("kernel environment store ", env_store, " is not located under initial tmpfs " S6_LINUX_INIT_TMPFS) ;
+ }
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) ;
+ make_env(argv[0], ENVSTAGE1, satmp.s, satmp.len) ;
satmp.len = 0 ;
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..42441d6
--- /dev/null
+++ b/src/init/s6-linux-init-telinit.c
@@ -0,0 +1,74 @@
+/* ISC license. */
+
+#include <string.h>
+#include <sys/wait.h>
+
+#include <skalibs/types.h>
+#include <skalibs/sgetopt.h>
+#include <skalibs/strerr2.h>
+#include <skalibs/djbunix.h>
+
+#include <s6/config.h>
+
+#include <s6-linux-init/config.h>
+#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: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' :
+ 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] = S6_LINUX_INIT_BINPREFIX "s6-linux-init-hpr" ;
+ newargv[1] = argv[0][0] == '6' ? "-r" : "-p" ;
+ newargv[2] = 0 ;
+ }
+
+ xpathexec_run(newargv[0], newargv, envp) ;
+}
diff --git a/src/init/s6-linux-init.c b/src/init/s6-linux-init.c
new file mode 100644
index 0000000..e322fa4
--- /dev/null
+++ b/src/init/s6-linux-init.c
@@ -0,0 +1,189 @@
+/* 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 [ -c basedir ] [ -p initpath ] [ -s envdumpdir ] [ -m umask ] [ -d devtmpfs ] [ -D initdefault ] [ -n | -N ]"
+#define dieusage() strerr_dieusage(100, USAGE)
+
+#define BANNER "\n s6-linux-init version " S6_LINUX_INIT_VERSION "\n\n"
+
+static inline char const *scan_cmdline (char const *initdefault, char const *const *argv, unsigned int argc)
+{
+ static char const *valid[] = { "default", "2", "3", "4", "5", 0 } ;
+ for (unsigned int i = 0 ; i < argc ; i++)
+ for (char const *const *p = valid ; *p ; p++)
+ if (!strcmp(argv[i], *p)) return argv[i] ;
+ return initdefault ;
+}
+
+static inline void run_stage2 (char const *basedir, char const **argv, unsigned int argc, char const *const *envp, size_t envlen, char const *modifs, size_t modiflen, char const *initdefault)
+{
+ size_t dirlen = strlen(basedir) ;
+ char const *childargv[argc + 3] ;
+ char fn[dirlen + sizeof("/scripts/" STAGE2)] ;
+ PROG = "s6-linux-init (child)" ;
+ argv[0] = PROG ;
+ memcpy(fn, basedir, dirlen) ;
+ memcpy(fn + dirlen, "/scripts/" STAGE2, sizeof("/scripts/" STAGE2)) ;
+ childargv[0] = fn ;
+ childargv[1] = scan_cmdline(initdefault, argv, argc) ;
+ for (unsigned int i = 0 ; i < argc ; i++)
+ childargv[i+2] = argv[i] ;
+ childargv[argc + 2] = 0 ;
+ setsid() ;
+ fd_close(1) ;
+ if (open(LOGFIFO, O_WRONLY) != 1) /* blocks until catch-all logger is up */
+ strerr_diefu1sys(111, "open " LOGFIFO " for writing") ;
+ if (fd_copy(2, 1) == -1)
+ strerr_diefu1sys(111, "fd_copy stdout to stderr") ;
+ xpathexec_r(childargv, envp, envlen, modifs, modiflen) ;
+}
+
+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 ;
+ char const *initdefault = "default" ;
+ int mounttype = 1 ;
+ stralloc envmodifs = STRALLOC_ZERO ;
+ 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, "c:p:s:m:d:D:nN", &l) ;
+ if (opt == -1) break ;
+ switch (opt)
+ {
+ 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 ;
+ case 'D' : initdefault = l.arg ; break ;
+ case 'n' : mounttype = 2 ; break ;
+ case 'N' : mounttype = 0 ; 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 (mounttype)
+ {
+ if (mounttype == 2)
+ {
+ if (mount("tmpfs", S6_LINUX_INIT_TMPFS, "tmpfs", MS_REMOUNT | MS_NODEV | MS_NOSUID, "mode=0755") == -1)
+ strerr_diefu1sys(111, "remount " S6_LINUX_INIT_TMPFS) ;
+ }
+ else
+ {
+ 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.s, envmodifs.len, initdefault) ;
+ 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"
diff --git a/src/lib/deps-lib/s6_linux_init b/src/lib/deps-lib/s6_linux_init
new file mode 100644
index 0000000..4c849c7
--- /dev/null
+++ b/src/lib/deps-lib/s6_linux_init
@@ -0,0 +1,4 @@
+s6_linux_init_logouthook.o
+-lskarnet
+${SOCKET_LIB}
+${TAINNOW_LIB}
diff --git a/src/lib/s6_linux_init_logouthook.c b/src/lib/s6_linux_init_logouthook.c
new file mode 100644
index 0000000..810f29b
--- /dev/null
+++ b/src/lib/s6_linux_init_logouthook.c
@@ -0,0 +1,19 @@
+/* ISC license. */
+
+#include <skalibs/djbunix.h>
+#include <skalibs/webipc.h>
+
+#include <s6-linux-init/s6-linux-init.h>
+#include "initctl.h"
+
+int s6_linux_init_logouthook (void)
+{
+ int fd = ipc_stream_nbcoe() ;
+ if (fd < 0) return -1 ;
+ if (!ipc_timed_connect(fd, LOGOUTHOOKD_PATH, 0, 0))
+ {
+ fd_close(fd) ;
+ return -1 ;
+ }
+ return fd ;
+}
diff --git a/src/init/deps-exe/s6-reboot b/src/misc/deps-exe/s6-linux-init-echo
index e7187fe..e7187fe 100644
--- a/src/init/deps-exe/s6-reboot
+++ b/src/misc/deps-exe/s6-linux-init-echo
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..483efe2
--- /dev/null
+++ b/src/misc/s6-linux-init-echo.c
@@ -0,0 +1,39 @@
+/* ISC license. */
+
+#include <skalibs/sgetopt.h>
+#include <skalibs/buffer.h>
+#include <skalibs/strerr2.h>
+
+#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-linux-init-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 <skalibs/sysdeps.h>
+
+#ifndef SKALIBS_HASSOPEERCRED
+#error "The SO_PEERCRED option to getsockopt() is required."
+#endif
+
+#include <skalibs/nonposix.h>
+
+#include <string.h>
+#include <unistd.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <errno.h>
+#include <utmpx.h>
+
+#include <skalibs/allreadwrite.h>
+#include <skalibs/strerr2.h>
+
+#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 <string.h>
+#include <sys/mount.h>
+
+#include <skalibs/bytestr.h>
+#include <skalibs/buffer.h>
+#include <skalibs/strerr2.h>
+#include <skalibs/stralloc.h>
+#include <skalibs/djbunix.h>
+#include <skalibs/skamisc.h>
+
+#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-hpr b/src/shutdown/deps-exe/s6-linux-init-hpr
new file mode 100644
index 0000000..0c4376c
--- /dev/null
+++ b/src/shutdown/deps-exe/s6-linux-init-hpr
@@ -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..01c9db2
--- /dev/null
+++ b/src/shutdown/deps-exe/s6-linux-init-shutdownd
@@ -0,0 +1,5 @@
+-ls6
+${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.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 <stddef.h>
+
+#include <skalibs/tai.h>
+#include <skalibs/djbunix.h>
+
+#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..3ede92d
--- /dev/null
+++ b/src/shutdown/hpr_shutdown.c
@@ -0,0 +1,16 @@
+/* ISC license. */
+
+#include <stdint.h>
+
+#include <skalibs/uint32.h>
+#include <skalibs/tai.h>
+
+#include "hpr.h"
+
+int hpr_shutdown (unsigned int what, tain_t const *when, unsigned int grace)
+{
+ char pack[5 + TAIN_PACK] = { "Shpr"[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..e63ddea
--- /dev/null
+++ b/src/shutdown/hpr_wall.c
@@ -0,0 +1,41 @@
+/* ISC license. */
+
+#include <string.h>
+#include <utmpx.h>
+
+#include <skalibs/posixishard.h>
+#include <skalibs/allreadwrite.h>
+#include <skalibs/strerr2.h>
+#include <skalibs/djbunix.h>
+
+#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 ;
+ int fd ;
+ struct utmpx *utx = getutxent() ;
+ if (!utx) break ;
+ if (utx->ut_type != USER_PROCESS) continue ;
+ linelen = strnlen(utx->ut_line, UT_LINESIZE) ;
+ memcpy(tty + 5, utx->ut_line, linelen) ;
+ tty[5 + linelen] = 0 ;
+ fd = open_append(tty) ;
+ if (fd == -1) continue ;
+ allwrite(fd, msg, n) ;
+ fd_close(fd) ;
+ }
+ endutxent() ;
+}
diff --git a/src/shutdown/s6-linux-init-hpr.c b/src/shutdown/s6-linux-init-hpr.c
new file mode 100644
index 0000000..886f10b
--- /dev/null
+++ b/src/shutdown/s6-linux-init-hpr.c
@@ -0,0 +1,120 @@
+/* ISC license. */
+
+#include <skalibs/nonposix.h>
+
+#include <unistd.h>
+#include <signal.h>
+#include <errno.h>
+#include <utmpx.h>
+#include <sys/reboot.h>
+
+#include <skalibs/strerr2.h>
+#include <skalibs/sgetopt.h>
+#include <skalibs/sig.h>
+#include <skalibs/tai.h>
+#include <skalibs/djbunix.h>
+
+#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 "s6-linux-init-hpr [ -h | -p | -r ] [ -d | -w ] [ -W ] [ -f ]"
+
+int main (int argc, char const *const *argv)
+{
+ int what = 0 ;
+ int force = 0 ;
+ int dowtmp = 1 ;
+ int dowall = 1 ;
+ PROG = "s6-linux-init-hpr" ;
+
+ {
+ 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 (!what)
+ strerr_dief1x(100, "one of the -h, -p or -r options must be given") ;
+
+ 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 ;
+
+/* glibc multilib can go fuck itself */
+#ifdef __WORDSIZE_TIME64_COMPAT32
+ {
+ struct timeval tv ;
+ if (!timeval_from_tain(&tv, &STAMP))
+ strerr_warnwu1sys("timeval_from_tain") ;
+ utx.ut_tv.tv_sec = tv.tv_sec ;
+ utx.ut_tv.tv_usec = tv.tv_usec ;
+ }
+#else
+ if (!timeval_from_tain(&utx.ut_tv, &STAMP))
+ strerr_warnwu1sys("timeval_from_tain") ;
+#endif
+
+ 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/s6-linux-init-shutdown.c b/src/shutdown/s6-linux-init-shutdown.c
new file mode 100644
index 0000000..2779537
--- /dev/null
+++ b/src/shutdown/s6-linux-init-shutdown.c
@@ -0,0 +1,265 @@
+/* ISC license. */
+
+#include <stdint.h>
+#include <string.h>
+#include <unistd.h>
+#include <signal.h>
+#include <sys/stat.h>
+#include <errno.h>
+#include <time.h>
+#include <utmpx.h>
+
+#include <skalibs/uint32.h>
+#include <skalibs/types.h>
+#include <skalibs/allreadwrite.h>
+#include <skalibs/strerr2.h>
+#include <skalibs/sgetopt.h>
+#include <skalibs/sig.h>
+#include <skalibs/tai.h>
+#include <skalibs/djbunix.h>
+#include <skalibs/djbtime.h>
+
+#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 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(&tmthen, tain_secp(&STAMP), 1))
+ strerr_diefu1sys(111, "break down current time into struct tm") ;
+ tmthen.tm_hour = hour ;
+ tmthen.tm_min = minute ;
+ tmthen.tm_sec = 0 ;
+ 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") ;
+ }
+ when->sec = taithen ;
+ when->nano = 0 ;
+}
+
+static void parse_mins (tain_t *when, char const *s)
+{
+ unsigned int mins ;
+ if (!uint0_scan(s, &mins)) dieusage() ;
+ tain_addsec_g(when, mins * 60) ;
+}
+
+static inline void parse_time (tain_t *when, char const *s)
+{
+ if (!strcmp(s, "now")) tain_copynow(when) ;
+ else if (s[0] == '+') parse_mins(when, s+1) ;
+ else if (strchr(s, ':')) parse_hourmin(when, s) ;
+ else parse_mins(when, s) ;
+}
+
+
+ /* 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..7f86e66
--- /dev/null
+++ b/src/shutdown/s6-linux-init-shutdownd.c
@@ -0,0 +1,309 @@
+/* ISC license. */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <string.h>
+#include <errno.h>
+#include <signal.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <sys/wait.h>
+
+#include <skalibs/posixplz.h>
+#include <skalibs/uint32.h>
+#include <skalibs/types.h>
+#include <skalibs/allreadwrite.h>
+#include <skalibs/bytestr.h>
+#include <skalibs/buffer.h>
+#include <skalibs/strerr2.h>
+#include <skalibs/sgetopt.h>
+#include <skalibs/sig.h>
+#include <skalibs/tai.h>
+#include <skalibs/direntry.h>
+#include <skalibs/djbunix.h>
+#include <skalibs/iopause.h>
+#include <skalibs/skamisc.h>
+
+#include <execline/config.h>
+
+#include <s6/s6-supervise.h>
+
+#include <s6-linux-init/config.h>
+#include "defaults.h"
+#include "initctl.h"
+#include "hpr.h"
+
+#define STAGE4_FILE "stage 4"
+#define SCANPREFIX S6_LINUX_INIT_TMPFS "/" SCANDIR "/"
+#define SCANPREFIXLEN (sizeof(SCANPREFIX) - 1)
+#define DOTPREFIX ".s6-linux-init-shutdownd:"
+#define DOTPREFIXLEN (sizeof(DOTPREFIX) - 1)
+#define DOTSUFFIX ":XXXXXX"
+#define DOTSUFFIXLEN (sizeof(DOTSUFFIX) - 1)
+
+#define USAGE "s6-linux-init-shutdownd [ -c basedir ] [ -g gracetime ]"
+#define dieusage() strerr_dieusage(100, USAGE)
+
+static char const *basedir = BASEDIR ;
+
+struct at_s
+{
+ int fd ;
+ char const *name ;
+} ;
+
+static int renametemp (char const *s, mode_t mode, void *data)
+{
+ struct at_s *at = data ;
+ (void)mode ;
+ return renameat(at->fd, at->name, at->fd, s) ;
+}
+
+static int mkrenametemp (int fd, char const *src, char *dst)
+{
+ struct at_s at = { .fd = fd, .name = src } ;
+ return mkfiletemp(dst, &renametemp, 0700, &at) ;
+}
+
+static inline void run_stage3 (char const *basedir, char const *const *envp)
+{
+ pid_t pid ;
+ size_t basedirlen = strlen(basedir) ;
+ char stage3[basedirlen + sizeof("/scripts/" STAGE3)] ;
+ char const *stage3_argv[2] = { stage3, 0 } ;
+ memcpy(stage3, basedir, basedirlen) ;
+ memcpy(stage3 + basedirlen, "/scripts/" STAGE3, sizeof("/scripts/" 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) ;
+}
+
+static inline void prepare_shutdown (buffer *b, tain_t *deadline, unsigned int *grace_time)
+{
+ uint32_t u ;
+ char pack[TAIN_PACK + 4] ;
+ ssize_t r = sanitize_read(buffer_get(b, pack, TAIN_PACK + 4)) ;
+ 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 ;
+}
+
+static inline void handle_fifo (buffer *b, char *what, tain_t *deadline, unsigned int *grace_time)
+{
+ for (;;)
+ {
+ char c ;
+ ssize_t r = sanitize_read(buffer_get(b, &c, 1)) ;
+ if (r == -1) strerr_diefu1sys(111, "read from pipe") ;
+ else if (!r) break ;
+ switch (c)
+ {
+ case 'S' :
+ case 'h' :
+ case 'p' :
+ case 'r' :
+ *what = c ;
+ prepare_shutdown(b, deadline, grace_time) ;
+ break ;
+ case 'c' :
+ *what = 'S' ;
+ 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, char what)
+{
+ 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_BINPREFIX "s6-linux-init-umountall }\n"
+ S6_LINUX_INIT_BINPREFIX "s6-linux-init-hpr -f -") < 0
+ || buffer_put(&b, &what, 1) < 0
+ || buffer_putsflush(&b, "\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) ;
+}
+
+static inline void unsupervise_tree (void)
+{
+ static char const *except[] =
+ {
+ LOGGER_SERVICEDIR,
+ SHUTDOWND_SERVICEDIR,
+ /* EARLYGETTY_SERVICEDIR, */
+ 0
+ } ;
+ DIR *dir = opendir(S6_LINUX_INIT_TMPFS "/" SCANDIR) ;
+ int fdd ;
+ if (!dir)
+ strerr_diefu1sys(111, "opendir " S6_LINUX_INIT_TMPFS "/" SCANDIR) ;
+ fdd = dirfd(dir) ;
+ if (fdd == -1)
+ strerr_diefu1sys(111, "dir_fd " S6_LINUX_INIT_TMPFS "/" SCANDIR) ;
+ for (;;)
+ {
+ char const *const *p = except ;
+ direntry *d ;
+ errno = 0 ;
+ d = readdir(dir) ;
+ if (!d) break ;
+ if (d->d_name[0] == '.') continue ;
+ for (; *p ; p++) if (!strcmp(*p, d->d_name)) break ;
+ if (!*p)
+ {
+ size_t dlen = strlen(d->d_name) ;
+ char fn[SCANPREFIXLEN + DOTPREFIXLEN + dlen + DOTSUFFIXLEN + 1] ;
+ memcpy(fn, SCANPREFIX DOTPREFIX, SCANPREFIXLEN + DOTPREFIXLEN) ;
+ memcpy(fn + SCANPREFIXLEN + DOTPREFIXLEN, d->d_name, dlen) ;
+ memcpy(fn + SCANPREFIXLEN + DOTPREFIXLEN + dlen, DOTSUFFIX, DOTSUFFIXLEN + 1) ;
+ if (mkrenametemp(fdd, d->d_name, fn + SCANPREFIXLEN) == -1)
+ {
+ strerr_warnwu4sys("rename " SCANPREFIX, d->d_name, " to something based on ", fn) ;
+ unlinkat(fdd, d->d_name, 0) ;
+ /* if it still fails, too bad, it will restart in stage 4 and race */
+ }
+ else
+ s6_svc_writectl(fn, S6_SUPERVISE_CTLDIR, "dx", 2) ;
+ }
+ }
+ if (errno)
+ strerr_diefu1sys(111, "readdir " S6_LINUX_INIT_TMPFS "/" SCANDIR) ;
+ dir_close(dir) ;
+}
+
+int main (int argc, char const *const *argv, char const *const *envp)
+{
+ char what = 'S' ;
+ unsigned int grace_time = 3000 ;
+ tain_t deadline ;
+ int fdr, fdw ;
+ buffer b ;
+ char buf[64] ;
+ 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 } ;
+ execve(stage4_argv[0], (char **)stage4_argv, (char *const *)envp) ;
+ if (errno != ENOENT)
+ strerr_warnwu2sys("exec ", stage4_argv[0]) ;
+ }
+
+ fdr = open_read(SHUTDOWND_FIFO) ;
+ if (fdr == -1 || coe(fdr) == -1)
+ strerr_diefu3sys(111, "open ", SHUTDOWND_FIFO, " for reading") ;
+ fdw = open_write(SHUTDOWND_FIFO) ;
+ if (fdw == -1 || coe(fdw) == -1)
+ strerr_diefu3sys(111, "open ", SHUTDOWND_FIFO, " for writing") ;
+ if (sig_ignore(SIGPIPE) == -1)
+ strerr_diefu1sys(111, "sig_ignore SIGPIPE") ;
+ buffer_init(&b, &buffer_read, fdr, buf, 64) ;
+ tain_now_g() ;
+ tain_add_g(&deadline, &tain_infinite_relative) ;
+
+ for (;;)
+ {
+ iopause_fd x = { .fd = fdr, .events = IOPAUSE_READ } ;
+ int r = iopause_g(&x, 1, &deadline) ;
+ if (r == -1) strerr_diefu1sys(111, "iopause") ;
+ if (!r)
+ {
+ run_stage3(basedir, envp) ;
+ tain_now_g() ;
+ if (what != 'S') break ;
+ tain_add_g(&deadline, &tain_infinite_relative) ;
+ continue ;
+ }
+ if (x.revents & IOPAUSE_READ)
+ handle_fifo(&b, &what, &deadline, &grace_time) ;
+ }
+
+ fd_close(fdw) ;
+ fd_close(fdr) ;
+ fd_close(1) ;
+ if (open("/dev/console", O_WRONLY) != 1)
+ strerr_diefu1sys(111, "open /dev/console for writing") ;
+ if (fd_copy(2, 1) == -1) strerr_warnwu1sys("fd_copy") ;
+
+
+ /* The end is coming! */
+
+ prepare_stage4(basedir, what) ;
+ unsupervise_tree() ;
+ 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 ;
+}