diff options
-rw-r--r-- | COPYING | 2 | ||||
-rw-r--r-- | INSTALL | 4 | ||||
-rw-r--r-- | NEWS | 10 | ||||
-rw-r--r-- | doc/index.html | 6 | ||||
-rw-r--r-- | doc/overview.html | 19 | ||||
-rw-r--r-- | doc/s6-linux-init-maker.html | 120 | ||||
-rw-r--r-- | doc/s6-linux-init-shutdownd.html | 11 | ||||
-rw-r--r-- | doc/s6-linux-init-telinit.html | 5 | ||||
-rw-r--r-- | doc/s6-linux-init.html | 16 | ||||
-rw-r--r-- | doc/upgrade.html | 21 | ||||
-rw-r--r-- | package/info | 2 | ||||
-rw-r--r-- | src/include-local/initctl.h | 2 | ||||
-rw-r--r-- | src/init/s6-linux-init-maker.c | 123 | ||||
-rw-r--r-- | src/init/s6-linux-init-telinit.c | 6 | ||||
-rw-r--r-- | src/init/s6-linux-init.c | 150 | ||||
-rw-r--r-- | src/misc/s6-linux-init-umountall.c | 4 | ||||
-rw-r--r-- | src/shutdown/s6-linux-init-hpr.c | 10 | ||||
-rw-r--r-- | src/shutdown/s6-linux-init-shutdownd.c | 104 |
18 files changed, 459 insertions, 156 deletions
@@ -1,4 +1,4 @@ -Copyright (c) 2015-2019 Laurent Bercot <ska-skaware@skarnet.org> +Copyright (c) 2015-2020 Laurent Bercot <ska-skaware@skarnet.org> Permission to use, copy, modify, and distribute this software for any purpose with or without fee is hereby granted, provided that the above @@ -7,8 +7,8 @@ Build Instructions - A Linux-based system with a standard C development environment - GNU make version 3.81 or later - skalibs version 2.9.1.0 or later: https://skarnet.org/software/skalibs/ - - execline version 2.5.3.0 or later: https://skarnet.org/software/execline/ - - s6 version 2.9.0.1 or later: https://skarnet.org/software/s6/ + - execline version 2.6.0.0 or later: https://skarnet.org/software/execline/ + - s6 version 2.9.1.0 or later: https://skarnet.org/software/s6/ - Optional: nsss version 0.0.2.1 or later: https://skarnet.org/software/nsss/ - Optional: utmps version 0.0.3.1 or later: https://skarnet.org/software/utmps/ @@ -1,5 +1,15 @@ Changelog for s6-linux-init. +In 1.0.4.0 +---------- + + - New -n option to s6-linux-init-hpr: don't sync. + - New -C and -B options to s6-linux-init-maker, +s6-linux-init and s6-linux-init-shutdownd, to handle running +in a container, and doing without a catch-all logger, respectively. + - Various robustness improvements. + + In 1.0.3.1 ---------- diff --git a/doc/index.html b/doc/index.html index 85727a8..2b912ab 100644 --- a/doc/index.html +++ b/doc/index.html @@ -74,9 +74,9 @@ kernel command line </li> <li> <a href="//skarnet.org/software/skalibs/">skalibs</a> version 2.9.1.0 or later </li> <li> <a href="//skarnet.org/software/execline/">execline</a> version -2.5.3.0 or later </li> +2.6.0.0 or later </li> <li> <a href="//skarnet.org/software/s6/">s6</a> version -2.9.0.1 or later </li> +2.9.1.0 or later </li> </ul> <p> @@ -110,7 +110,7 @@ all the other packages against the <em>static</em> version of libskarnet. <ul> <li> The current released version of s6-linux-init is -<a href="s6-linux-init-1.0.3.1.tar.gz">1.0.3.1</a>. </li> +<a href="s6-linux-init-1.0.4.0.tar.gz">1.0.4.0</a>. </li> <li> Alternatively, you can checkout a copy of the <a href="//git.skarnet.org/cgi-bin/cgit.cgi/s6-linux-init/">s6-linux-init git repository</a>: diff --git a/doc/overview.html b/doc/overview.html index d779dd2..adaa414 100644 --- a/doc/overview.html +++ b/doc/overview.html @@ -80,7 +80,8 @@ procedure when the admin runs a <tt>halt</tt>, <tt>poweroff</tt>, <tt>reboot</tt> or <tt>shutdown</tt> command. </li> <li> <em>runlevel</em>: the script executing a machine state change at boot time (normally invoked by <em>rc.init</em>, towards the default -runlevel) or when the administrator runs a <tt>telinit</tt> command. </li> +runlevel) or when the administrator runs a <tt>telinit</tt> command. +This script is unused in a container. </li> <li> <em>rc.shutdown.final</em>: a script that will be run at the very end of the shutdown procedure, after all processes have been killed and all filesystems have been unmounted, <em>just before</em> the system @@ -110,21 +111,25 @@ via the <tt>-c</tt> option. </li> <li> Some early services are defined in <tt>/run/service</tt>, and running. They are not seen by the service manager and should remain up all the time, until the machine shuts down: they are considered a part of the init system, -even if they're not process 1. Each of these services uses very few resources. -The services are: +even if they're not process 1. +Apart from <tt>s6-linux-init-shutdownd</tt>, which is necessary for the +machine (or container) to respond to shutdown requests, they are only present +depending on the configuration, and different +<a href="s6-linux-init-maker.html">s6-linux-init-maker</a> invocations will +yield different sets of services. The services are picked from this list: <ul> - <li> <tt>s6-svscan-log</tt>: the catch-all logger </li> <li> <tt>s6-linux-init-shutdownd</tt>: the shutdown manager, running the shutdown sequence in a reproducible environment when a shutdown command is executed, then performing the last shutdown steps. </li> + <li> <tt>s6-svscan-log</tt>: the catch-all logger </li> <li> <tt>s6-linux-init-runleveld</tt>: the runlevel manager, running the <em>runlevel</em> script in a reproducible environment when a <tt>telinit</tt> command is executed. </li> - <li> (optionally) <tt>s6-linux-init-logouthookd</tt>: a local service performing + <li> <tt>s6-linux-init-logouthookd</tt>: a local service performing utmp record cleanup duty for patched <tt>login</tt> programs. </li> - <li> (optionally) <tt>s6-linux-init-early-getty</tt>: the early getty, + <li> <tt>s6-linux-init-early-getty</tt>: the early getty, allowing the user to login even if <em>rc.init</em> fails early. </li> - <li> (optionally) <tt>utmpd</tt> and <tt>wtmpd</tt>: the services performing + <li> <tt>utmpd</tt> and <tt>wtmpd</tt>: the services performing utmp and wtmp access when <a href="//skarnet.org/software/utmps/">utmps</a> is used. </li> </ul> </li> diff --git a/doc/s6-linux-init-maker.html b/doc/s6-linux-init-maker.html index 7fe963b..d8c1432 100644 --- a/doc/s6-linux-init-maker.html +++ b/doc/s6-linux-init-maker.html @@ -62,6 +62,8 @@ machine</em>. If it is not the case, the system will fail to boot. [ -n | -N ] \ [ -f <em>skeldir</em> ] \ [ -U <em>utmp_user</em> ] \ + [ -C ] \ + [ -B ] \ <em>dir</em> </pre> @@ -102,13 +104,20 @@ the machine should be brought to, traditionally <tt>default</tt> for OpenRC and <tt>2</tt> or <tt>5</tt> for sysv-rc), and the rest of the command line is made of the kernel's command line except for the kernel arguments of the <em>key=value</em> form, -which have been stored into <em>env_store</em>. </li> +which have been stored into <em>env_store</em>. If the <tt>-C</tt> +option has been given to <tt>s6-linux-init-maker</tt> and the +system is indeed running in a container, the rest of the +command line is just the command line that has been given to +the container's <tt>init</tt> (e.g. for Docker: the CMD). Note +that the <tt>runlevel</tt> script should not be invoked in a +container, which does not have a notion of runlevels. </li> <li> <tt>rc.shutdown</tt>: this script will be run as the <em>shutdown sequence</em>, when the administrator runs the <tt>shutdown</tt>, <tt>halt</tt>, <tt>poweroff</tt> or <tt>reboot</tt> -command. (As well as <tt>init 0</tt>, <tt>init 6</tt>, -<tt>telinit 0</tt> and <tt>telinit 6</tt> for compatibility -reasons.) It should ask the service manager to bring all the +command. (As well, for non-containerized systems, +as <tt>init 0</tt>, <tt>init 6</tt>, <tt>telinit 0</tt> and +<tt>telinit 6</tt> for sysvinit compatibility reasons.) +It should ask the service manager to bring all the services down, and exit when it's done (in other words: it should not try to perform a hard halt/poweroff/reboot itself.) No arguments are given to this script. </li> @@ -117,7 +126,8 @@ No arguments are given to this script. </li> given one argument: the name of the runlevel to change to. Typically, the <em>runlevel</em> script should just invoke the service manager, asking it to bring the machine state to the -wanted runlevel. </li> +wanted runlevel. In a containerized system, this script should +not be used at all.</li> </ul> </li> <li> Copy the <em>dir</em> directory to the place declared as <em>basedir</em> (<tt>/etc/s6-linux-init/current</tt> by default). @@ -312,6 +322,60 @@ available when the s6-linux-init package has been built with the <a href="//skarnet.org/software/utmps/">utmps</a> package. The option defines the user that the <tt>utmpd</tt> and <tt>wtmpd</tt> services will run as. Default is <strong><tt>utmp</tt></strong>. </li> <p /> + + <li> <tt>-C</tt> : create a set of scripts that is suitable +for running <em>in a container</em>. This modifies some behaviours: + <ul> + <li> No early <tt>runleveld</tt> service is created. Changing +runlevels via +<a href="s6-linux-init-telinit.html">s6-linux-init-telinit</a> +will be unsupported in a container. </li> + <li> Consequently, the first argument to the <tt>rc.init</tt> script +will always be <tt>default</tt> (or <em>initdefault</em> if the <tt>-D</tt> +option has been given to <tt>s6-linux-init-maker</tt>). The rest of the +arguments to the <tt>rc.init</tt> script will be the arguments given +to the <tt>init</tt> program when running the container. </li> + <li> If the <tt>-s</tt> option has been given, <em>env_store</em> +will contain the initial environment given to the container. </li> + <li> The ultimate output fallback (i.e. the place where error messages +go when nothing catches them, e.g. the error messages from the catch-all +logger and the +<a href="//skarnet.org/software/s6/s6-supervise.html">s6-supervise</a> +process managing the catch-all logger) is not <tt>/dev/console</tt>, but +the descriptor that was <tt>init</tt>'s standard error. </li> + <li> Stopping the container with <tt>reboot</tt> will make the +container's init program report being killed by a SIGHUP. Stopping it +with <tt>poweroff</tt> will make it report being killed by a SIGINT. +(This is according to the +<a href="http://man7.org/linux/man-pages/man2/reboot.2.html">reboot(2)</a> +specification. </li> + <li> Stopping the container with <tt>halt</tt>, however, is different. +It will make the container's pid 1 read a number in the +<tt>/run/s6-linux-init-container-results/exitcode</tt> file (the +<tt>/run</tt> prefix can be changed at build time via the <tt>--tmpfsdir</tt> +configure option), and exit with the code it has read. (Default is 0.) +This means that in order to run a command in a container managed by +s6-linux-init and exit the container when the command dies while reporting +the exit code to its parent, you should: + <ul> + <li> Run that command via <tt>rc.init</tt> </li> + <li> Store its exit code in the +<tt>/run/s6-linux-init-container-results/exitcode</tt> file </li> + <li> Call <tt>halt</tt> </li> + </ul> + All the running services will be killed, all the zombies will be +reaped, and the container will exit with the required exit code. </li> + </ul> </li> + + <li> <tt>-B</tt> : run the system without a catch-all logger. +On a non-containerized system, that means that all the logs from the +s6 supervision tree will go to <tt>/dev/console</tt>, and that +<tt>/dev/console</tt> will also be the default stdout and stderr for +services running under the supervision tree: use of this option is +discouraged. On a containerized system (when paired with the <tt>-C</tt> +option), it simply means that these outputs go to the default stdout and +stderr given to the container's <tt>init</tt> - this should generally +not be the default, but might be useful in some cases. </li> </ul> <h2> Organization of the created directory </h2> @@ -349,7 +413,8 @@ subdirectories it contains are the following: <li> <tt>uncaught-logs</tt>: this is the directory where the catch-all logger will store and rotate the error messages produced by the s6 supervision tree and the services that do not redirect -their own logs. </li> +their own logs. Not present if the <tt>-B</tt> option has been +given. </li> <li> <tt>service</tt>: <tt>/run/service</tt> will be the scandir. It initially contains a <tt>.s6-svscan</tt> subdirectory that tells <a href="//skarnet.org/software/s6/s6-svscan.html">s6-svscan</a> @@ -359,24 +424,26 @@ also contains a list of early services, i.e. s6 services that will be run at boot time as soon as <tt>s6-svscan</tt> is executed. These services are: <ul> - <li> <tt>s6-svscan-log</tt>: the catch-all logger. </li> + <li> <tt>s6-svscan-log</tt>: the catch-all logger. Not present +if the <tt>-B</tt> option has been given. </li> <li> <tt>s6-linux-init-shutdownd</tt>: a service that listens to shutdown commands such as <tt>reboot</tt> and triggers the software shutdown procedure. </li> <li> <tt>s6-linux-init-runleveld</tt>: a service that listens to runlevel change commands such as <tt>telinit</tt> and calls the <em>runlevel</em> script in a reproducible environment to bring the -machine to the wanted state. </li> - <li> (If the <tt>-L</tt> option has been given to -<tt>s6-linux-init-maker</tt>) <tt>s6-linux-init-logouthookd</tt>: +machine to the wanted state. Not present if the <tt>-C</tt> option +has been given. </li> + <li> <tt>s6-linux-init-logouthookd</tt>: the "clean up user utmp records at logout time" service. See the <a href="s6-linux-init-logouthookd.html">s6-linux-init-logouthookd</a> -page for details. </li> - <li> (If the <tt>-G</tt> option has been given to -<tt>s6-linux-init-maker</tt>) <tt>s6-linux-init-early-getty</tt>: +page for details. Not present if the <tt>-L</tt> option has not been +given. </li> + <li> <tt>s6-linux-init-early-getty</tt>: the early getty service, that will allow a user to log in even if <em>rc.init</em> fails to bring the machine to a state where logins -are possible. </li> +are possible. Not present if the <tt>-G</tt> option has not been +given. </li> </ul> </li> </ul> </li> </ul> @@ -400,7 +467,9 @@ providing secure utmp functionality. </li> <p> A directory created by <tt>s6-linux-init-maker</tt> is only valid on the machine it has been created on. Pre-creating init directories for -other machines is not supported. +other machines is not supported. Of course, the scripts are editable, +so advanced users can run <tt>s6-linux-init-maker</tt> to create a +basic template, and then make their own modifications. </p> <p> @@ -430,22 +499,19 @@ is difficult: in particular, the mechanisms around the shutdown procedure are fundamentally different from about any other init system, so even a simple command such as <tt>reboot</tt> needs an ad-hoc implementation. </li> + <li> Even for simple systems such as containerized ones, making +sure that the wanted commands only run when s6-svscan is ready +requires a bit of manipulation. </li> </ul> <p> The main benefit of <tt>s6-linux-init-maker</tt> is that it offers -transparent compatibility while automating the tricky technical part. -That means that <tt>s6-linux-init-maker</tt> has been designed for -<em>real hardware</em>, or at least full-fledged Linux systems, -where the above issues apply. If you are building an init system for a -container, or anything similar that does not -have the <tt>/dev/console</tt> issue, the read-only rootfs issue, -or the need for sysvinit compatibility, -you will probably not reap much benefit from using <tt>s6-linux-init-maker</tt>: -you could probably invoke -<a href="//skarnet.org/software/s6/s6-svscan.html">s6-svscan</a> -directly as your process 1, or build a script by hand, which -would result in a simpler init with less dependencies. +transparent compatibility while automating the tricky technical parts. +Whether it is used for real hardware or for containers, +<tt>s6-linux-init-maker</tt> gives you a turnkey init system that +frees your mind from the details of getting a +<a href="//skarnet.org/software/s6/">s6</a> supervision tree running +prior to everything else. </p> </body> diff --git a/doc/s6-linux-init-shutdownd.html b/doc/s6-linux-init-shutdownd.html index ce03543..a5e92ca 100644 --- a/doc/s6-linux-init-shutdownd.html +++ b/doc/s6-linux-init-shutdownd.html @@ -27,7 +27,7 @@ directly by the user. <h2> Interface </h2> <pre> - s6-linux-init-shutdownd [ -c <em>basedir</em> ] [ -g <em>gracetime</em> ] + s6-linux-init-shutdownd [ -c <em>basedir</em> ] [ -g <em>gracetime</em> ] [ -C ] [ -B ] </pre> <ul> @@ -57,6 +57,15 @@ directory. Default is <tt>/etc/s6-linux-init/current</tt>. </li> <li> <tt>-g <em>gracetime</em></tt> : if the shutdown command does not specify a grace time between the SIGTERM and the SIGKILL, use <em>gracetime</em> milliseconds. Default is 3000. </li> + <li> <tt>-C</tt> : inform the shutdown daemon that it is running +in a container. This changes the nature of <em>stage 4</em>, since +exiting a container is slightly different from rebooting a real machine +(in particular, pid 1 has to exit, and great care must be given not to +leave any zombie hanging around). </li> + <li> <tt>-B</tt> : inform the shutdown daemon that no catch-all +logger is running. This also changes the shutdown procedure slightly, +since some operations are specific to the existence of a catch-all +logger. </li> </ul> <h2> Notes </h2> diff --git a/doc/s6-linux-init-telinit.html b/doc/s6-linux-init-telinit.html index 49862d7..e01b44d 100644 --- a/doc/s6-linux-init-telinit.html +++ b/doc/s6-linux-init-telinit.html @@ -78,6 +78,11 @@ the <tt>bin/</tt> subdirectory of the target will contain a <tt>telinit</tt> symlink to <tt>s6-linux-init-telinit</tt>. The <tt>bin/</tt> subdirectory should be copied by the administrator into <tt>/sbin</tt> for full interface compatibility with sysvinit. </li> + <li> When the system is running inside a container (and the <tt>-C</tt> +option has been given to +<a href="s6-linux-init-maker.html">s6-linux-init-maker</a>), the runleveld +service is not running, so <tt>s6-linux-init-telinit</tt> is not +functional and the <tt>runlevel</tt> script is never invoked. </li> </ul> </body> diff --git a/doc/s6-linux-init.html b/doc/s6-linux-init.html index 184877a..21d97bf 100644 --- a/doc/s6-linux-init.html +++ b/doc/s6-linux-init.html @@ -27,7 +27,7 @@ and execs into <a href="//skarnet.org/software/s6/s6-svscan.html">s6-svscan</a>. <h2> Interface </h2> <pre> - s6-linux-init [ -c <em>basedir</em> ] [ -p <em>initial_path</em> ] [ -s <em>env_store</em> ] [ -m <em>umask</em> ] [ -d <em>slashdev</em> ] [ -D <em>initdefault</em> ] [ -n | -N ] [ <em>args...</em> ] + s6-linux-init [ -c <em>basedir</em> ] [ -p <em>initial_path</em> ] [ -s <em>env_store</em> ] [ -m <em>umask</em> ] [ -d <em>slashdev</em> ] [ -D <em>initdefault</em> ] [ -n | -N ] [ -C ] [ -B ] [ <em>args...</em> ] </pre> <ul> @@ -67,6 +67,19 @@ Default is <tt>default</tt>. </li> <li> <tt>-n</tt> : instead of unmounting <tt>/run</tt> and mounting a tmpfs on it, just remount <tt>/run</tt>. </li> <li> <tt>-N</tt> : do not touch <tt>/run</tt> at all. </li> + <li> <tt>-C</tt> : run in a container. This option modifies a few +of the operations described below, to accommodate running in a container +instead of on real hardware. For instance: it does not scan the command +line for a specific runlevel, it does not trap ctrl-alt-del, and before +anything else it waits for its descriptor 3, if present, to close. +(Docker uses this fd 3 mechanism as synchronization between the Docker +daemon and the container's <tt>init</tt>.) </li> + <li> <tt>-B</tt> : do not run the catch-all logger. This option +removes the catch-all-logger-related operations from the list below; +<tt>s6-linux-init</tt> will not redirect output descriptors, and will +use a different synchronization mechanism to ensure <tt>rc.init</tt> +only runs when <a href="//skarnet.org/software/s6/s6-svscan.html">s6-svscan</a> +is ready. </li> </ul> <h2> Early preparation </h2> @@ -107,6 +120,7 @@ it uses <em>initdefault</em>. </li> <li> The child blocks until the catch-all logger runs. </li> </ul> </li> <li> It also makes the catch-all logger's fifo its stderr. </li> + <li> It traps the ctrl-alt-del keyboard combination. </li> <li> It execs into <a href="//skarnet.org/software/s6/s6-svscan.html">s6-svscan</a> with <tt>/run/service</tt> as its scandir (or <em>tmpfsdir</em>/service). </li> <ul> diff --git a/doc/upgrade.html b/doc/upgrade.html index 0331011..5bc518a 100644 --- a/doc/upgrade.html +++ b/doc/upgrade.html @@ -18,6 +18,27 @@ <h1> What has changed in s6-linux-init </h1> +<h2> in 1.0.4.0 </h2> + +<ul> + <li> <a href="//skarnet.org/software/execline/">execline</a> +dependency bumped to 2.6.0.0. </li> + <li> <a href="//skarnet.org/software/s6/">s6</a> +dependency bumped to 2.9.1.0. </li> + <li> New <tt>-n</tt> option to +<a href="s6-linux-init-hpr.html">s6-linux-init-hpr</a>, to +disable the <tt>sync()</tt> call before the hardware reboot or +power off. </li> + <li> New options added to +<a href="s6-linux-init-maker.html">s6-linux-init-maker</a> and +impacted binaries (<a href="s6-linux-init.html">s6-linux-init</a> +and <a href="s6-linux-init-shutdownd.html">s6-linux-init-shutdownd</a>): + <ul> + <li> <tt>-C</tt>: to boot a s6-based system in a container </li> + <li> <tt>-B</tt>: to disable the catch-all logger </li> + </ul> </li> +</ul> + <h2> in 1.0.3.1 </h2> <ul> diff --git a/package/info b/package/info index 3716640..b20fbe9 100644 --- a/package/info +++ b/package/info @@ -1,4 +1,4 @@ package=s6-linux-init -version=1.0.3.1 +version=1.0.4.0 category=admin package_macro_name=S6_LINUX_INIT diff --git a/src/include-local/initctl.h b/src/include-local/initctl.h index 9c67b06..a1be5c8 100644 --- a/src/include-local/initctl.h +++ b/src/include-local/initctl.h @@ -26,6 +26,8 @@ #define EARLYGETTY_SERVICEDIR "s6-linux-init-early-getty" #define EARLYGETTY S6_LINUX_INIT_TMPFS "/" SCANDIR "/" EARLYGETTY_SERVICEDIR +#define CONTAINER_RESULTS "s6-linux-init-container-results" + #define RUNIMAGE "run-image" #define ENVSTAGE1 "env" #define STAGE2 "rc.init" diff --git a/src/init/s6-linux-init-maker.c b/src/init/s6-linux-init-maker.c index ccf1f08..9277acd 100644 --- a/src/init/s6-linux-init-maker.c +++ b/src/init/s6-linux-init-maker.c @@ -28,12 +28,12 @@ #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 ] [ -f skeldir ] [ -U utmp_user ] dir" -# define OPTION_STRING "c:u:G:1Lp:m:t:d:s:e:E:q:D:nNf:U:" +# 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 ] [ -f skeldir ] [ -U utmp_user ] [ -C ] [ -B ] dir" +# define OPTION_STRING "c:u:G:1Lp:m:t:d:s:e:E:q:D:nNf:U:CB" # 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 ] [ -f skeldir ] dir" -# define OPTION_STRING "c:u:G:1Lp:m:t:d:s:e:E:q:D:nNf:" +# 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 ] [ -f skeldir ] [ -C ] [ -B ] dir" +# define OPTION_STRING "c:u:G:1Lp:m:t:d:s:e:E:q:D:nNf:CB" #endif #define dieusage() strerr_dieusage(100, USAGE) @@ -55,6 +55,8 @@ static unsigned int finalsleep = 3000 ; static int mounttype = 1 ; static int console = 0 ; static int logouthookd = 0 ; +static int inns = 0 ; +static int nologger = 0 ; #ifdef S6_LINUX_INIT_UTMPD_PATH static char const *utmp_user = "utmp" ; @@ -89,33 +91,61 @@ static int linewithargs_script (buffer *b, char const *line) static int hpr_script (buffer *b, char const *what) { return put_shebang_options(b, "-S0") - && buffer_puts(b, S6_LINUX_INIT_EXTBINPREFIX "s6-linux-init-hpr -") >= 0 - && buffer_puts(b, what) >= 0 - && buffer_puts(b, " $@\n") >= 0 ; + && buffer_puts(b, S6_LINUX_INIT_EXTBINPREFIX "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, - EXECLINE_EXTBINPREFIX "redirfd -w 1 /dev/console\n" - EXECLINE_EXTBINPREFIX "fdmove -c 2 1\n" - EXECLINE_EXTBINPREFIX "foreground { " - S6_LINUX_INIT_EXTBINPREFIX "s6-linux-init-echo -- \"s6-svscan ") >= 0 - && buffer_puts(b, s) >= 0 - && buffer_puts(b, - ". Rebooting.\" }\n" - S6_LINUX_INIT_EXTBINPREFIX "s6-linux-init-hpr -r -f\n") >= 0 ; + && buffer_puts(b, + EXECLINE_EXTBINPREFIX "redirfd -w 2 /dev/console\n" + EXECLINE_EXTBINPREFIX "fdmove -c 1 2\n" + EXECLINE_EXTBINPREFIX "foreground { " + S6_LINUX_INIT_EXTBINPREFIX "s6-linux-init-echo -- \"s6-svscan ") >= 0 + && buffer_puts(b, s) >= 0 + && buffer_puts(b, + ". Rebooting.\" }\n" + S6_LINUX_INIT_EXTBINPREFIX "s6-linux-init-hpr -fr\n") >= 0 ; +} + +static int container_crash_script (buffer *b, char const *data) +{ + (void)data ; + return put_shebang(b) + && buffer_puts(b, + EXECLINE_EXTBINPREFIX "foreground\n{\n " + EXECLINE_EXTBINPREFIX "fdmove -c 1 2\n " + S6_LINUX_INIT_EXTBINPREFIX "s6-linux-init-echo -- \"s6-svscan crashed. Killing everything and exiting.\"\n}\n" + EXECLINE_EXTBINPREFIX "foreground { kill -9 -1 }\n" + EXECLINE_EXTBINPREFIX "wait { }\n" + S6_LINUX_INIT_EXTBINPREFIX "s6-linux-init-hpr -fnp\n") >= 0 ; +} + +static int container_exit_script (buffer *b, char const *results) +{ + return put_shebang_options(b, "-S0") + && buffer_puts(b, + EXECLINE_EXTBINPREFIX "ifelse -X { test $1 = halt }\n{\n " + S6_EXTBINPREFIX "s6-envdir -- ") >= 0 + && buffer_puts(b, results) >= 0 + && buffer_puts(b, "\n " + EXECLINE_EXTBINPREFIX "importas -D0 -- EXITCODE exitcode\n " + EXECLINE_EXTBINPREFIX "exit $EXITCODE\n}\n" + EXECLINE_EXTBINPREFIX "ifte -X\n { " + S6_LINUX_INIT_EXTBINPREFIX "s6-linux-init-hpr -fnr }\n { " + S6_LINUX_INIT_EXTBINPREFIX "s6-linux-init-hpr -fnp }\n" + "test $1 = reboot\n") >= 0 ; } static int s6_svscan_log_script (buffer *b, char const *data) { size_t sabase = satmp.len ; if (!put_shebang(b) - || buffer_puts(b, - 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, console || inns ? + EXECLINE_EXTBINPREFIX "fdmove -c 1 2" : + EXECLINE_EXTBINPREFIX "redirfd -w 1 /dev/null") < 0 || buffer_puts(b, "\n" EXECLINE_EXTBINPREFIX "redirfd -rnb 0 " LOGGER_FIFO "\n" S6_EXTBINPREFIX "s6-setuidgid ") < 0 @@ -156,6 +186,8 @@ static int shutdownd_script (buffer *b, char const *data) satmp.len = sabase ; if (buffer_puts(b, " -g ") < 0 || buffer_put(b, fmt, uint_fmt(fmt, finalsleep)) < 0 + || (inns && buffer_puts(b, " -C") < 0) + || (nologger && buffer_puts(b, " -B") < 0) || buffer_puts(b, "\n") < 0) return 0 ; (void)data ; return 1 ; @@ -243,6 +275,8 @@ static inline int stage1_script (buffer *b, char const *data) { if (buffer_puts(b, " -N") < 0) return 0 ; } + if (inns && buffer_puts(b, " -C") < 0) return 0 ; + if (nologger && buffer_puts(b, " -B") < 0) return 0 ; if (buffer_puts(b, "\n") < 0) return 0 ; (void)data ; @@ -500,16 +534,8 @@ static inline void make_utmps (char const *base) static inline void make_image (char const *base) { auto_dir(base, "run-image", 0, 0, 0755) ; - { - uid_t uid ; - gid_t gid ; - getug(base, log_user, &uid, &gid) ; - auto_dir(base, "run-image/" UNCAUGHT_DIR, uid, gid, 02750) ; - } 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) ; @@ -517,18 +543,37 @@ static inline void make_image (char const *base) 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) ; + if (!nologger) + { + uid_t uid ; + gid_t gid ; + getug(base, log_user, &uid, &gid) ; + auto_dir(base, "run-image/" UNCAUGHT_DIR, uid, gid, 02750) ; + 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 (inns) + { + auto_script(base, "run-image/" SCANDIR "/.s6-svscan/crash", &container_crash_script, "") ; + auto_script(base, "run-image/" SCANDIR "/.s6-svscan/finish", &container_exit_script, S6_LINUX_INIT_TMPFS "/" CONTAINER_RESULTS) ; + auto_dir(base, "run-image/" CONTAINER_RESULTS, 0, 0, 0755) ; + auto_file(base, "run-image/" CONTAINER_RESULTS "/exitcode", "0\n", 2) ; + } + else + { + 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_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) { @@ -551,7 +596,7 @@ static inline void make_image (char const *base) static inline void make_scripts (char const *base) { auto_dir(base, "scripts", 0, 0, 0755) ; - copy_script(base, "runlevel", 1) ; + if (!inns) copy_script(base, "runlevel", 1) ; copy_script(base, STAGE2, 1) ; copy_script(base, STAGE3, 1) ; copy_script(base, STAGE4, 0) ; @@ -565,7 +610,7 @@ static inline void make_bins (char const *base) 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") ; + if (!inns) auto_exec(base, "bin/telinit", "s6-linux-init-telinit") ; } int main (int argc, char const *const *argv, char const *const *envp) @@ -598,6 +643,8 @@ int main (int argc, char const *const *argv, char const *const *envp) #ifdef S6_LINUX_INIT_UTMPD_PATH case 'U' : utmp_user = l.arg ; break ; #endif + case 'C' : inns = 1 ; break ; + case 'B' : nologger = 1 ; break ; default : dieusage() ; } } @@ -618,6 +665,8 @@ int main (int argc, char const *const *argv, char const *const *envp) } if (timestamp_style > 3) strerr_dief1x(100, "-t timestamp_style must be 0, 1, 2 or 3") ; + if (inns && slashdev) + strerr_warnw1x("both -C and -d options given; are you sure your container does not come with a pre-mounted /dev?") ; umask(0) ; if (mkdir(argv[0], 0755) < 0) diff --git a/src/init/s6-linux-init-telinit.c b/src/init/s6-linux-init-telinit.c index 42441d6..cf05a7e 100644 --- a/src/init/s6-linux-init-telinit.c +++ b/src/init/s6-linux-init-telinit.c @@ -24,11 +24,13 @@ 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:p:s:m:d:D:", &l) ; + int opt = subgetopt_r(argc, argv, "CBc: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 'C' : /* s6-linux-init may be called with these options, don't choke on them */ + case 'B' : + case 'c' : case 'p' : case 's' : case 'm' : diff --git a/src/init/s6-linux-init.c b/src/init/s6-linux-init.c index 78cf3d0..530093e 100644 --- a/src/init/s6-linux-init.c +++ b/src/init/s6-linux-init.c @@ -23,20 +23,53 @@ #include "defaults.h" #include "initctl.h" -#define USAGE "s6-linux-init [ -c basedir ] [ -p initpath ] [ -s envdumpdir ] [ -m umask ] [ -d devtmpfs ] [ -D initdefault ] [ -n | -N ]" +#define USAGE "s6-linux-init [ -c basedir ] [ -p initpath ] [ -s envdumpdir ] [ -m umask ] [ -d devtmpfs ] [ -D initdefault ] [ -n | -N ] [ -C ] [ -B ]" #define dieusage() strerr_dieusage(100, USAGE) #define BANNER "\n s6-linux-init version " S6_LINUX_INIT_VERSION "\n\n" +static int inns = 0 ; +static int nologger = 0 ; +static int notifpipe[2] ; + static inline char const *scan_cmdline (char const *initdefault, char const *const *argv, unsigned int argc) { - static char const *valid[] = { "default", "2", "3", "4", "5", 0 } ; - for (unsigned int i = 0 ; i < argc ; i++) - for (char const *const *p = valid ; *p ; p++) - if (!strcmp(argv[i], *p)) return argv[i] ; + if (!inns) + { + static char const *valid[] = { "default", "2", "3", "4", "5", 0 } ; + for (unsigned int i = 0 ; i < argc ; i++) + for (char const *const *p = valid ; *p ; p++) + if (!strcmp(argv[i], *p)) return argv[i] ; + } return initdefault ; } +static inline void wait_for_notif (int fd) +{ + char buf[16] ; + for (;;) + { + ssize_t r = read(fd, buf, 16) ; + if (r < 0) strerr_diefu1sys(111, "read from notification pipe") ; + if (!r) + { + strerr_warnw1x("s6-svscan failed to send a notification byte!") ; + break ; + } + if (memchr(buf, '\n', r)) break ; + } + close(fd) ; +} + +static void disablecad (void) +{ + if (!inns) + { + if (reboot(RB_DISABLE_CAD) == -1) + strerr_warnwu1sys("trap ctrl-alt-del") ; + } +} + static inline void run_stage2 (char const *basedir, char const **argv, unsigned int argc, char const *const *envp, size_t envlen, char const *modifs, size_t modiflen, char const *initdefault) { size_t dirlen = strlen(basedir) ; @@ -51,11 +84,20 @@ static inline void run_stage2 (char const *basedir, char const **argv, unsigned childargv[i+2] = argv[i] ; childargv[argc + 2] = 0 ; setsid() ; - fd_close(1) ; - if (open(LOGFIFO, O_WRONLY) != 1) /* blocks until catch-all logger is up */ - strerr_diefu1sys(111, "open " LOGFIFO " for writing") ; - if (fd_copy(2, 1) == -1) - strerr_diefu1sys(111, "fd_copy stdout to stderr") ; + if (nologger) + { + close(notifpipe[1]) ; + wait_for_notif(notifpipe[0]) ; + } + else + { + /* block on opening the log fifo until the catch-all logger is up */ + close(1) ; + if (open(LOGFIFO, O_WRONLY) != 1) + strerr_diefu1sys(111, "open " LOGFIFO " for writing") ; + if (fd_copy(2, 1) == -1) + strerr_diefu1sys(111, "fd_copy stdout to stderr") ; + } xpathexec_r(childargv, envp, envlen, modifs, modiflen) ; } @@ -74,15 +116,14 @@ int main (int argc, char const **argv, char const *const *envp) if (getpid() != 1) { argv[0] = S6_LINUX_INIT_BINPREFIX "s6-linux-init-telinit" ; - pathexec_run(argv[0], argv, envp) ; - strerr_dieexec(111, argv[0]) ; + xpathexec_run(argv[0], argv, envp) ; } { subgetopt_t l = SUBGETOPT_ZERO ; for (;;) { - int opt = subgetopt_r(argc, argv, "c:p:s:m:d:D:nN", &l) ; + int opt = subgetopt_r(argc, argv, "c:p:s:m:d:D:nNCB", &l) ; if (opt == -1) break ; switch (opt) { @@ -94,35 +135,60 @@ int main (int argc, char const **argv, char const *const *envp) case 'D' : initdefault = l.arg ; break ; case 'n' : mounttype = 2 ; break ; case 'N' : mounttype = 0 ; break ; + case 'C' : inns = 1 ; break ; + case 'B' : nologger = 1 ; break ; default : dieusage() ; } } argc -= l.ind ; argv += l.ind ; } - allwrite(1, BANNER, sizeof(BANNER) - 1) ; + if (inns) + { /* If there's a Docker synchronization pipe, wait on it */ + char c ; + ssize_t r = read(3, &c, 1) ; + if (r < 0) + { + if (errno != EBADF) strerr_diefu1sys(111, "read from fd 3") ; + } + else + { + if (r) strerr_warnw1x("parent wrote to fd 3!") ; + close(3) ; + } + } + else allwrite(1, BANNER, sizeof(BANNER) - 1) ; if (chdir("/") == -1) strerr_diefu1sys(111, "chdir to /") ; umask(mask) ; setpgid(0, 0) ; - fd_close(0) ; + close(0) ; + if (slashdev) { - fd_close(1) ; - fd_close(2) ; - if (mount("dev", slashdev, "devtmpfs", MS_NOSUID | MS_NOEXEC, "") == -1) + int nope, e ; + close(1) ; + close(2) ; + /* at this point we're totally in the dark, hoping /dev/console will work */ + nope = mount("dev", slashdev, "devtmpfs", MS_NOSUID | MS_NOEXEC, "") < 0 ; + e = errno ; + if (open("/dev/console", O_WRONLY) + || fd_move(2, 0) < 0 + || fd_copy(1, 2) < 0) return 111 ; + if (nope) { - int e = errno ; - open("/dev/null", O_RDONLY) ; - open("/dev/console", O_WRONLY) ; - fd_copy(2, 1) ; errno = e ; - strerr_diefu2sys(111, "mount ", slashdev) ; + strerr_diefu1sys(111, "mount a devtmpfs on /dev") ; } - if (open("/dev/console", O_WRONLY) - || fd_copy(1, 0) == -1 - || fd_move(2, 0) == -1) return 111 ; } - if (open("/dev/null", O_RDONLY)) strerr_diefu1sys(111, "open /dev/null") ; + + if (open("/dev/null", O_RDONLY)) + { /* ghetto /dev/null to the rescue */ + int p[2] ; + strerr_warnwu1sys("open /dev/null") ; + if (pipe(p) < 0) strerr_diefu1sys(111, "pipe") ; + close(p[1]) ; + if (fd_move(0, p[0]) < 0) strerr_diefu1sys(111, "fd_move to stdin") ; + } if (mounttype) { @@ -157,6 +223,8 @@ int main (int argc, char const **argv, char const *const *envp) } if (envdumpdir && !env_dump(envdumpdir, 0700, envp)) strerr_warnwu2sys("dump kernel environment to ", envdumpdir) ; + + if (!nologger) { int fdr = open_read(LOGFIFO) ; if (fdr == -1) strerr_diefu1sys(111, "open " LOGFIFO) ; @@ -164,11 +232,13 @@ int main (int argc, char const **argv, char const *const *envp) if (open(LOGFIFO, O_WRONLY) != 1) strerr_diefu1sys(111, "open " LOGFIFO) ; fd_close(fdr) ; } + { - static char const *const newargv[5] = { S6_EXTBINPREFIX "s6-svscan", "-st0", "--", S6_LINUX_INIT_TMPFS "/" SCANDIR, 0 } ; char const *newenvp[2] = { 0, 0 } ; - pid_t pid ; size_t pathlen = path ? strlen(path) : 0 ; + pid_t pid ; + char fmtfd[2 + UINT_FMT] = "-" ; + char const *newargv[6] = { S6_EXTBINPREFIX "s6-svscan", "-st0", fmtfd, "--", S6_LINUX_INIT_TMPFS "/" SCANDIR, 0 } ; char pathvar[6 + pathlen] ; if (path) { @@ -178,13 +248,27 @@ int main (int argc, char const **argv, char const *const *envp) memcpy(pathvar + 5, path, pathlen + 1) ; newenvp[0] = pathvar ; } + if (nologger && pipe(notifpipe) < 0) strerr_diefu1sys(111, "pipe") ; pid = fork() ; if (pid == -1) strerr_diefu1sys(111, "fork") ; if (!pid) run_stage2(basedir, argv, argc, newenvp, !!path, envmodifs.s, envmodifs.len, initdefault) ; - if (reboot(RB_DISABLE_CAD) == -1) - strerr_warnwu1sys("trap ctrl-alt-del") ; - if (fd_copy(2, 1) == -1) - strerr_diefu1sys(111, "redirect output file descriptor") ; + if (nologger) + { + close(notifpipe[0]) ; + fmtfd[1] = 'd' ; + fmtfd[2 + uint_fmt(fmtfd + 2, notifpipe[1])] = 0 ; + disablecad() ; + } + else + { + int fd = dup(2) ; + if (fd < 0) strerr_diefu1sys(111, "dup stderr") ; + fmtfd[1] = 'X' ; + fmtfd[2 + uint_fmt(fmtfd + 2, (unsigned int)fd)] = 0 ; + disablecad() ; + if (fd_copy(2, 1) == -1) + strerr_diefu1sys(111, "redirect output file descriptor") ; + } xpathexec_r(newargv, newenvp, !!path, envmodifs.s, envmodifs.len) ; } } diff --git a/src/misc/s6-linux-init-umountall.c b/src/misc/s6-linux-init-umountall.c index 93b0966..49c9f97 100644 --- a/src/misc/s6-linux-init-umountall.c +++ b/src/misc/s6-linux-init-umountall.c @@ -21,12 +21,10 @@ int main (int argc, char const *const *argv) unsigned int got[EXCLUDEN] = { 0, 0, 0 } ; stralloc sa = STRALLOC_ZERO ; unsigned int line = 0 ; - FILE *fp ; int e = 0 ; - + FILE *fp = setmntent("/proc/mounts", "r") ; PROG = "s6-linux-init-umountall" ; - fp = setmntent("/proc/mounts", "r") ; if (!fp) strerr_diefu1sys(111, "open /proc/mounts") ; for (;;) { diff --git a/src/shutdown/s6-linux-init-hpr.c b/src/shutdown/s6-linux-init-hpr.c index 265cc30..d3cd74b 100644 --- a/src/shutdown/s6-linux-init-hpr.c +++ b/src/shutdown/s6-linux-init-hpr.c @@ -29,7 +29,7 @@ #define _PATH_WTMP "/dev/null/wtmp" #endif -#define USAGE "s6-linux-init-hpr [ -h | -p | -r ] [ -d | -w ] [ -W ] [ -f ]" +#define USAGE "s6-linux-init-hpr [ -h | -p | -r ] [ -n ] [ -d | -w ] [ -W ] [ -f ]" int main (int argc, char const *const *argv) { @@ -37,13 +37,14 @@ int main (int argc, char const *const *argv) int force = 0 ; int dowtmp = 1 ; int dowall = 1 ; + int dosync = 1 ; PROG = "s6-linux-init-hpr" ; { subgetopt_t l = SUBGETOPT_ZERO ; for (;;) { - int opt = subgetopt_r(argc, argv, "hprfdwW", &l) ; + int opt = subgetopt_r(argc, argv, "hprfdwWn", &l) ; if (opt == -1) break ; switch (opt) { @@ -54,6 +55,7 @@ int main (int argc, char const *const *argv) case 'd' : dowtmp = 0 ; break ; case 'w' : dowtmp = 2 ; break ; case 'W' : dowall = 0 ; break ; + case 'n' : dosync = 0 ; break ; default : strerr_dieusage(100, USAGE) ; } } @@ -71,7 +73,7 @@ int main (int argc, char const *const *argv) if (force) { - sync() ; + if (dosync) sync() ; reboot(what == 3 ? RB_AUTOBOOT : what == 2 ? RB_POWER_OFF : RB_HALT_SYSTEM) ; strerr_diefu1sys(111, "reboot()") ; } @@ -95,7 +97,7 @@ int main (int argc, char const *const *argv) } else utx.ut_host[UT_HOSTSIZE - 1] = 0 ; -/* glibc multilib can go fuck itself */ + /* glibc multilib can go fuck itself */ #ifdef __WORDSIZE_TIME64_COMPAT32 { struct timeval tv ; diff --git a/src/shutdown/s6-linux-init-shutdownd.c b/src/shutdown/s6-linux-init-shutdownd.c index f5c436c..5e681e1 100644 --- a/src/shutdown/s6-linux-init-shutdownd.c +++ b/src/shutdown/s6-linux-init-shutdownd.c @@ -27,6 +27,7 @@ #include <execline/config.h> +#include <s6/config.h> #include <s6/s6-supervise.h> #include <s6-linux-init/config.h> @@ -35,17 +36,20 @@ #include "hpr.h" #define STAGE4_FILE "stage 4" -#define SCANPREFIX S6_LINUX_INIT_TMPFS "/" SCANDIR "/" +#define SCANDIRFULL S6_LINUX_INIT_TMPFS "/" SCANDIR +#define SCANPREFIX SCANDIRFULL "/" #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 USAGE "s6-linux-init-shutdownd [ -c basedir ] [ -g gracetime ] [ -C ] [ -B ]" #define dieusage() strerr_dieusage(100, USAGE) static char const *basedir = BASEDIR ; +static int inns = 0 ; +static int nologger = 0 ; struct at_s { @@ -139,6 +143,17 @@ static inline void handle_fifo (buffer *b, char *what, tain_t *deadline, unsigne } } +static void restore_console (void) +{ + if (!inns && !nologger) + { + fd_close(1) ; + if (open("/dev/console", O_WRONLY) != 1) + strerr_diefu1sys(111, "open /dev/console for writing") ; + if (fd_copy(2, 1) < 0) strerr_warnwu1sys("fd_copy") ; + } +} + static inline void prepare_stage4 (char const *basedir, char what) { buffer b ; @@ -149,20 +164,37 @@ static inline void prepare_stage4 (char const *basedir, char what) 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" - EXECLINE_EXTBINPREFIX "foreground { ") < 0 - || !string_quote(&satmp, basedir, strlen(basedir)) - || buffer_put(&b, satmp.s + sabase, satmp.len - sabase) < 0 - || buffer_puts(&b, "/scripts/" STAGE4 " }\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") ; - satmp.len = sabase ; + if (inns) + { + if (buffer_puts(&b, "#!" + EXECLINE_SHEBANGPREFIX "execlineb -P\n\n" + EXECLINE_EXTBINPREFIX "foreground { " + S6_EXTBINPREFIX "s6-svc -Ox -- . }\n" + EXECLINE_EXTBINPREFIX "background\n{\n ") < 0 + || (!nologger && buffer_puts(&b, + EXECLINE_EXTBINPREFIX "foreground { " + S6_EXTBINPREFIX "s6-svc -Xh -- " SCANPREFIX LOGGER_SERVICEDIR " }\n ") < 0) + || buffer_puts(&b, S6_EXTBINPREFIX "s6-svscanctl -") < 0 + || buffer_put(&b, what == 'h' ? "s" : &what, 1) < 0 + || buffer_putsflush(&b, "b -- " SCANDIRFULL "\n}\n") < 0) + strerr_diefu2sys(111, "write to ", STAGE4_FILE ".new") ; + } + else + { + if (buffer_puts(&b, "#!" + EXECLINE_SHEBANGPREFIX "execlineb -P\n\n" + EXECLINE_EXTBINPREFIX "foreground { " + S6_LINUX_INIT_BINPREFIX "s6-linux-init-umountall }\n" + EXECLINE_EXTBINPREFIX "foreground { ") < 0 + || !string_quote(&satmp, basedir, strlen(basedir)) + || buffer_put(&b, satmp.s + sabase, satmp.len - sabase) < 0 + || buffer_puts(&b, "/scripts/" STAGE4 " }\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") ; + satmp.len = sabase ; + } if (fchmod(fd, S_IRWXU) == -1) strerr_diefu2sys(111, "fchmod ", STAGE4_FILE ".new") ; fd_close(fd) ; @@ -172,20 +204,18 @@ static inline void prepare_stage4 (char const *basedir, char what) static inline void unsupervise_tree (void) { - static char const *except[] = + char const *except[3] = { - LOGGER_SERVICEDIR, SHUTDOWND_SERVICEDIR, - /* EARLYGETTY_SERVICEDIR, */ + nologger ? 0 : LOGGER_SERVICEDIR, 0 } ; - DIR *dir = opendir(S6_LINUX_INIT_TMPFS "/" SCANDIR) ; + DIR *dir = opendir(SCANDIRFULL) ; int fdd ; - if (!dir) - strerr_diefu1sys(111, "opendir " S6_LINUX_INIT_TMPFS "/" SCANDIR) ; + if (!dir) strerr_diefu1sys(111, "opendir " SCANDIRFULL) ; fdd = dirfd(dir) ; if (fdd == -1) - strerr_diefu1sys(111, "dir_fd " S6_LINUX_INIT_TMPFS "/" SCANDIR) ; + strerr_diefu1sys(111, "dir_fd " SCANDIRFULL) ; for (;;) { char const *const *p = except ; @@ -213,17 +243,17 @@ static inline void unsupervise_tree (void) } } if (errno) - strerr_diefu1sys(111, "readdir " S6_LINUX_INIT_TMPFS "/" SCANDIR) ; + strerr_diefu1sys(111, "readdir " SCANDIRFULL) ; 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 what = 'S' ; char buf[64] ; PROG = "s6-linux-init-shutdownd" ; @@ -231,12 +261,14 @@ 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:g:", &l) ; + int opt = subgetopt_r(argc, argv, "c:g:CB", &l) ; if (opt == -1) break ; switch (opt) { case 'c' : basedir = l.arg ; break ; case 'g' : if (!uint0_scan(l.arg, &grace_time)) dieusage() ; break ; + case 'C' : inns = 1 ; break ; + case 'B' : nologger = 1 ; break ; default : dieusage() ; } } @@ -249,6 +281,7 @@ int main (int argc, char const *const *argv, char const *const *envp) /* if we're in stage 4, exec it immediately */ { char const *stage4_argv[2] = { "./" STAGE4_FILE, 0 } ; + restore_console() ; execve(stage4_argv[0], (char **)stage4_argv, (char *const *)envp) ; if (errno != ENOENT) strerr_warnwu2sys("exec ", stage4_argv[0]) ; @@ -285,27 +318,30 @@ int main (int argc, char const *const *argv, char const *const *envp) 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") ; + restore_console() ; /* 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...") ; + if (!inns) + { + sync() ; + strerr_warni1x("sending all processes the TERM signal...") ; + } kill(-1, SIGTERM) ; kill(-1, SIGCONT) ; tain_from_millisecs(&deadline, grace_time) ; tain_now_g() ; tain_add_g(&deadline, &deadline) ; deepsleepuntil_g(&deadline) ; - sync() ; - strerr_warni1x("sending all processes the KILL signal...") ; + if (!inns) + { + sync() ; + strerr_warni1x("sending all processes the KILL signal...") ; + } kill(-1, SIGKILL) ; return 0 ; } |