diff options
55 files changed, 2051 insertions, 591 deletions
@@ -1,4 +1,4 @@ -Copyright (c) 2015-2018 Laurent Bercot <ska-skaware@skarnet.org> +Copyright (c) 2015-2019 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 @@ -6,25 +6,15 @@ Build Instructions - A Linux-based system with a standard C development environment - GNU make version 3.81 or later - - skalibs version 2.6.4.0 or later: http://skarnet.org/software/skalibs/ - - The scripts generated by the s6-linux-init-maker program have some -additional dependencies: - - - execline version 2.3.0.4 or later: http://skarnet.org/software/execline/ - - s6-portable-utils version 2.2.1.1 or later: http://skarnet.org/software/s6-portable-utils/ - - s6-linux-utils version 2.4.0.2 or later: http://skarnet.org/software/s6-linux-utils/ - - s6 version 2.7.1.0 or later: http://skarnet.org/software/s6/ + - skalibs version 2.8.0.0 or later: https://skarnet.org/software/skalibs/ + - execline version 2.5.1.0 or later: https://skarnet.org/software/execline/ + - s6 version 2.8.0.0 or later: https://skarnet.org/software/s6/ + - Recommended: utmps version 0.0.2.0 or later: https://skarnet.org/software/utmps/ This software is Linux-specific. It will run on a Linux kernel, version 2.6.32 or later. However, it should not be too hard to port to other Unix-like operating systems. - The s6-linux-init-maker binary will actually build *and run* on -another operating system; however, the scripts it produces depend on -s6-linux-utils binaries, which are really Linux-specific - so those -scripts will not run properly on another OS. - * Standard usage -------------- @@ -1,5 +1,16 @@ Changelog for s6-linux-init. +In 1.0.0.0 +---------- + + - Entirely different approach, see the documentation overview. + - s6-halt, s6-poweroff, s6-reboot removed. + - s6-linux-init-maker now produces a set of executable scripts +that are a drop-in replacement for a sysvinit installation, and +that respect the LSB-3.0.0 specification. + - Everything builds as PIC by default. + + In 0.4.0.0 ---------- @@ -4,7 +4,7 @@ s6-linux-init - tools to create an s6-based Linux init system s6-linux-init is a suite of tools designed to provide an init program for the Linux kernel - i.e., the first program run at boot time. - See http://skarnet.org/software/s6-linux-init/ for details. + See https://skarnet.org/software/s6-linux-init/ for details. * Installation @@ -22,6 +22,7 @@ Fine tuning of the installation directories: --libexecdir=DIR package-scoped executables [EPREFIX/libexec] --libdir=DIR static library files [PREFIX/lib/$package] --includedir=DIR C header files [PREFIX/include] + --tmpfsdir=DIR assume the initial tmpfs will be mounted on DIR [/run] If no --prefix option is given, by default libdir (but not dynlibdir) will be /usr/lib/$package, and includedir will be /usr/include. @@ -40,11 +41,13 @@ Optional features: --disable-static do not build static libraries [enabled] --disable-allstatic do not prefer linking against static libraries [enabled] --enable-static-libc make entirely static binaries [disabled] - --enable-all-pic build everything as PIC [enabled iff toolchain builds PIE] + --disable-all-pic do not build executables or static libs as PIC [enabled] --enable-slashpackage[=ROOT] assume /package installation at ROOT [disabled] --enable-absolute-paths do not rely on PATH to access this package's binaries, hardcode absolute BINDIR/foobar paths instead [disabled] + --enable-utmps link against the utmps library [disabled] + EOF exit 0 } @@ -143,7 +146,7 @@ sysdeps='$prefix/lib/skalibs/sysdeps' manualsysdeps=false shared=false static=true -allpic=detect +allpic=true slashpackage=false abspath=false sproot= @@ -157,6 +160,8 @@ addlibdpath='' vpaths='' vpathd='' build= +tmpfsdir=/run +utmps=false for arg ; do case "$arg" in @@ -187,6 +192,9 @@ for arg ; do --disable-slashpackage) sproot= ; slashpackage=false ;; --enable-absolute-paths|--enable-absolute-paths=yes) abspath=true ;; --disable-absolute-paths|--enable-absolute-paths=no) abspath=false ;; + --tmpfsdir=*) tmpfsdir=${arg#*=} ;; + --enable-utmps|--enable-utmps=yes) utmps=true ;; + --disable-utmps|--enable-utmps=no) utmps=false ;; --enable-*|--disable-*|--with-*|--without-*|--*dir=*) ;; --host=*|--target=*) target=${arg#*=} ;; --build=*) build=${arg#*=} ;; @@ -303,20 +311,6 @@ if [ "x$target" != "x$(cat $sysdeps/target)" ] ; then exit 1 fi -if test $allpic = detect ; then - echo "Checking whether we need to build everything as PIC..." - if $CC_AUTO $CPPFLAGS_AUTO $CPPFLAGS $CPPFLAGS_POST $CFLAGS_AUTO $CFLAGS $CFLAGS_POST -dM -E - < /dev/null | grep -qF __PIE__ ; then - allpic=true - echo " ... yes" - else - allpic=false - echo " ... no" - fi -fi -if $allpic ; then - tryflag CFLAGS_AUTO -fPIC -fi - spawn_lib=$(cat $sysdeps/spawn.lib) socket_lib=$(cat $sysdeps/socket.lib) sysclock_lib=$(cat $sysdeps/sysclock.lib) @@ -324,6 +318,9 @@ tainnow_lib=$(cat $sysdeps/tainnow.lib) timer_lib=$(cat $sysdeps/timer.lib) util_lib=$(cat $sysdeps/util.lib) +if $allpic ; then + tryflag CPPFLAGS_AUTO -fPIC +fi tryflag CFLAGS_AUTO -std=c99 tryflag CFLAGS -fomit-frame-pointer tryflag CFLAGS_AUTO -fno-exceptions @@ -434,6 +431,11 @@ if $allpic ; then else echo "STATIC_LIBS_ARE_PIC :=" fi +if $utmps ; then + echo "LIBUTMPS := -lutmps" +else + echo "LIBUTMPS :=" +fi exec 1>&3 3>&- echo " ... done." @@ -462,6 +464,13 @@ else echo "#define ${package_macro_name}_EXTBINPREFIX \"\"" fi echo "#define ${package_macro_name}_LIBEXECPREFIX \"$libexecdir/\"" +echo "#define ${package_macro_name}_TMPFS \"$tmpfsdir\"" +echo +if $utmps ; then + echo "#define ${package_macro_name}_HAS_UTMPS" +else + echo "#undef ${package_macro_name}_HAS_UTMPS" +fi echo echo "#endif" exec 1>&3 3>&- diff --git a/doc/s6-halt.html b/doc/s6-halt.html deleted file mode 100644 index 6f2a30e..0000000 --- a/doc/s6-halt.html +++ /dev/null @@ -1,55 +0,0 @@ -<html> - <head> - <meta name="viewport" content="width=device-width, initial-scale=1.0" /> - <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> - <meta http-equiv="Content-Language" content="en" /> - <title>s6-linux-init: the s6-halt program</title> - <meta name="Description" content="s6-linux-init: the s6-halt program" /> - <meta name="Keywords" content="s6 linux init administration root utilities shutdown halt poweroff reboot" /> - <!-- <link rel="stylesheet" type="text/css" href="//skarnet.org/default.css" /> --> - </head> -<body> - -<p> -<a href="index.html">s6-linux-init</a><br /> -<a href="//skarnet.org/software/">Software</a><br /> -<a href="//skarnet.org/">skarnet.org</a> -</p> - -<h1> The <tt>s6-halt</tt> program </h1> - -<p> -<tt>s6-halt</tt> sends a signal to process 1 in order to halt the machine; -or, with the <tt>-f</tt> option, it performs an immediate hard shutdown. -</p> - -<h2> Interface </h2> - -<pre> - s6-halt [ -h | -p | -r ] [ -f ] -</pre> - -<ul> - <li> s6-halt sends a signal to process 1. </li> - <li> It then exits 0. </li> -</ul> - -<h2> Options </h2> - -<ul> - <li> <tt>-h</tt> : halt. The command will order a halt (i.e. the system will -be shut down, but the power will remain up), which means -sending a SIGUSR2 to process 1. This is the default. </li> - <li> <tt>-p</tt> : poweroff. The command will order a power off, which means -sending a SIGUSR1 to process 1. </li> - <li> <tt>-r</tt> : reboot. The command will order a reboot, which means -sending a SIGINT to process 1. </li> - <li> <tt>-f</tt> : force. The command will not send any signal to process 1; -it will just sync the filesystems then tell the kernel to halt, poweroff or reboot. -<tt>s6-reboot -f</tt> or <tt>s6-poweroff -f</tt> should be the last program -executed in the lifetime of a machine, at the end of the shutdown script called -by process 1 when it receives a signal telling it to shut down. </li> -</ul> - -</body> -</html> diff --git a/doc/s6-linux-init-halt.html b/doc/s6-linux-init-halt.html new file mode 100644 index 0000000..0f68995 --- /dev/null +++ b/doc/s6-linux-init-halt.html @@ -0,0 +1,102 @@ +<html> + <head> + <meta name="viewport" content="width=device-width, initial-scale=1.0" /> + <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> + <meta http-equiv="Content-Language" content="en" /> + <title>s6-linux-init: the s6-linux-init-halt program</title> + <meta name="Description" content="s6-linux-init: the s6-linux-init-halt program" /> + <meta name="Keywords" content="s6 linux init administration root utilities shutdown halt poweroff reboot" /> + <!-- <link rel="stylesheet" type="text/css" href="//skarnet.org/default.css" /> --> + </head> +<body> + +<p> +<a href="index.html">s6-linux-init</a><br /> +<a href="//skarnet.org/software/">Software</a><br /> +<a href="//skarnet.org/">skarnet.org</a> +</p> + +<h1> The <tt>s6-linux-init-halt</tt> program </h1> + +<p> +<tt>s6-linux-init-halt</tt> triggers the shutdown procedure in order to +halt the system; or, with the <tt>-f</tt> option, it performs an immediate +hard shutdown. +</p> + +<h2> Interface </h2> + +<pre> + s6-linux-init-halt [ -h | -p | -r ] [ -d | -w ] [ -W ] [ -f ] [ -R <em>tmpfsdir</em> ] +</pre> + +<ul> + <li> If the <tt>-f</tt> option is present, s6-linux-init-halt halts the system immediately. </li> + <li> Else, it triggers the machine's shutdown procedure. + <li> It exits 0. The shutdown procedure happens asynchronously. </li> +</ul> + +<p> + This interface follows the traditional <em>sysvinit</em> interface for the +<tt>halt</tt>, <tt>poweroff</tt> and <tt>reboot</tt> programs as close as possible. +</p> + +<h2> Exit codes </h2> + +<ul> + <li> 0: shutdown procedure triggered. </li> + <li> 100: wrong usage, or user does not have root privileges. </li> + <li> 111: system call failed. </li> +</ul> + +<h2> Options </h2> + +<ul> + <li> <tt>-h</tt> : halt. No matter the name of the command, it will order +a halt (i.e. the system will be shut down, but the power will remain up). +This is the default for <tt>s6-linux-init-halt</tt>. </li> + <li> <tt>-p</tt> : poweroff. No matter the name of the command, it will order +a power off. This is the default for <tt>s6-linux-init-poweroff</tt>. </li> + <li> <tt>-r</tt> : reboot. No matter the name of the command, it will order +a reboot. This is the default for <tt>s6-linux-init-reboot</tt>. </li> + <li> <tt>-d</tt> : Do not write a wtmp shutdown entry. </li> + <li> <tt>-w</tt> : Only write a wtmp shutdown entry; do not actually shut down +the system. </li> + <li> <tt>-W</tt> : Do not send a <tt>wall</tt> message to users before shutting +down the system. Some other implementations of the <tt>halt</tt>, <tt>poweroff</tt> +and <tt>reboot</tt> commands use the <tt>--no-wall</tt> long option to achieve this. </li> + <li> <tt>-f</tt> : force. The command will not trigger a clean shutdown +procedure; it will just sync the filesystems then tell the kernel to immediately +halt, poweroff or reboot. This should be the last step in the lifetime of the +machine. </li> + <li> <tt>-R <em>tmpfsdir</em></tt> : assume that the root-only +boot-time tmpfs has been mounted on <em>tmpfsdir</em>. Default is <tt>/run</tt>. +It is important to get this right, because the contact point with the +<a href="s6-linux-init-shutdownd.html">s6-linux-init-shutdownd</a> daemon, +which manages the shutdown procedure, is located under this directory. The +<tt>halt</tt>, <tt>poweroff</tt> and <tt>reboot</tt> scripts created by a +<a href="s6-linux-init-maker.html">s6-linux-init-maker</a> invocation +automatically use the correct <tt>-R</tt> option. </li> +</ul> + +<h2> Notes </h2> + +<ul> + <li> The +<a href="s6-linux-init-halt.html">s6-linux-init-halt</a>, +<a href="s6-linux-init-poweroff.html">s6-linux-init-poweroff</a>, +<a href="s6-linux-init-reboot.html">s6-linux-init-reboot</a> and +<a href="s6-linux-init-shutdown.html">s6-linux-init-shutdown</a> +binaries are not meant to be called directly by administrators. +Instead, their purpose is to be used in <tt>halt</tt>, <tt>poweroff</tt>, +<tt>reboot</tt> and <tt>shutdown</tt> scripts created by +<a href="s6-linux-init-maker.html">s6-linux-init-maker</a>; those scripts +will call them with the correct <tt>-R <em>tmpfsdir</em></tt> option. +The scripts should typically be linked to <tt>/sbin</tt> after +<a href="s6-linux-init-maker.html">s6-linux-init-maker</a> execution, to +provide drop-in replacements for the <em>sysvinit</em> programs with the +same names. </li> +</ul> + +</body> +</html> diff --git a/doc/s6-linux-init-poweroff.html b/doc/s6-linux-init-poweroff.html new file mode 100644 index 0000000..2126b23 --- /dev/null +++ b/doc/s6-linux-init-poweroff.html @@ -0,0 +1,102 @@ +<html> + <head> + <meta name="viewport" content="width=device-width, initial-scale=1.0" /> + <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> + <meta http-equiv="Content-Language" content="en" /> + <title>s6-linux-init: the s6-linux-init-poweroff program</title> + <meta name="Description" content="s6-linux-init: the s6-linux-init-poweroff program" /> + <meta name="Keywords" content="s6 linux init administration root utilities shutdown halt poweroff reboot" /> + <!-- <link rel="stylesheet" type="text/css" href="//skarnet.org/default.css" /> --> + </head> +<body> + +<p> +<a href="index.html">s6-linux-init</a><br /> +<a href="//skarnet.org/software/">Software</a><br /> +<a href="//skarnet.org/">skarnet.org</a> +</p> + +<h1> The <tt>s6-linux-init-poweroff</tt> program </h1> + +<p> +<tt>s6-linux-init-poweroff</tt> triggers the shutdown procedure in order to +halt the system; or, with the <tt>-f</tt> option, it performs an immediate +hard shutdown. +</p> + +<h2> Interface </h2> + +<pre> + s6-linux-init-poweroff [ -h | -p | -r ] [ -d | -w ] [ -W ] [ -f ] [ -R <em>tmpfsdir</em> ] +</pre> + +<ul> + <li> If the <tt>-f</tt> option is present, s6-linux-init-poweroff halts the system immediately. </li> + <li> Else, it triggers the machine's shutdown procedure. + <li> It exits 0. The shutdown procedure happens asynchronously. </li> +</ul> + +<p> + This interface follows the traditional <em>sysvinit</em> interface for the +<tt>halt</tt>, <tt>poweroff</tt> and <tt>reboot</tt> programs as close as possible. +</p> + +<h2> Exit codes </h2> + +<ul> + <li> 0: shutdown procedure triggered. </li> + <li> 100: wrong usage, or user does not have root privileges. </li> + <li> 111: system call failed. </li> +</ul> + +<h2> Options </h2> + +<ul> + <li> <tt>-h</tt> : halt. No matter the name of the command, it will order +a halt (i.e. the system will be shut down, but the power will remain up). +This is the default for <tt>s6-linux-init-halt</tt>. </li> + <li> <tt>-p</tt> : poweroff. No matter the name of the command, it will order +a power off. This is the default for <tt>s6-linux-init-poweroff</tt>. </li> + <li> <tt>-r</tt> : reboot. No matter the name of the command, it will order +a reboot. This is the default for <tt>s6-linux-init-reboot</tt>. </li> + <li> <tt>-d</tt> : Do not write a wtmp shutdown entry. </li> + <li> <tt>-w</tt> : Only write a wtmp shutdown entry; do not actually shut down +the system. </li> + <li> <tt>-W</tt> : Do not send a <tt>wall</tt> message to users before shutting +down the system. Some other implementations of the <tt>halt</tt>, <tt>poweroff</tt> +and <tt>reboot</tt> commands use the <tt>--no-wall</tt> long option to achieve this. </li> + <li> <tt>-f</tt> : force. The command will not trigger a clean shutdown +procedure; it will just sync the filesystems then tell the kernel to immediately +halt, poweroff or reboot. This should be the last step in the lifetime of the +machine. </li> + <li> <tt>-R <em>tmpfsdir</em></tt> : assume that the root-only +boot-time tmpfs has been mounted on <em>tmpfsdir</em>. Default is <tt>/run</tt>. +It is important to get this right, because the contact point with the +<a href="s6-linux-init-shutdownd.html">s6-linux-init-shutdownd</a> daemon, +which manages the shutdown procedure, is located under this directory. The +<tt>halt</tt>, <tt>poweroff</tt> and <tt>reboot</tt> scripts created by a +<a href="s6-linux-init-maker.html">s6-linux-init-maker</a> invocation +automatically use the correct <tt>-R</tt> option. </li> +</ul> + +<h2> Notes </h2> + +<ul> + <li> The +<a href="s6-linux-init-halt.html">s6-linux-init-halt</a>, +<a href="s6-linux-init-poweroff.html">s6-linux-init-poweroff</a>, +<a href="s6-linux-init-reboot.html">s6-linux-init-reboot</a> and +<a href="s6-linux-init-shutdown.html">s6-linux-init-shutdown</a> +binaries are not meant to be called directly by administrators. +Instead, their purpose is to be used in <tt>halt</tt>, <tt>poweroff</tt>, +<tt>reboot</tt> and <tt>shutdown</tt> scripts created by +<a href="s6-linux-init-maker.html">s6-linux-init-maker</a>; those scripts +will call them with the correct <tt>-R <em>tmpfsdir</em></tt> option. +The scripts should typically be linked to <tt>/sbin</tt> after +<a href="s6-linux-init-maker.html">s6-linux-init-maker</a> execution, to +provide drop-in replacements for the <em>sysvinit</em> programs with the +same names. </li> +</ul> + +</body> +</html> diff --git a/doc/s6-linux-init-reboot.html b/doc/s6-linux-init-reboot.html new file mode 100644 index 0000000..422eeed --- /dev/null +++ b/doc/s6-linux-init-reboot.html @@ -0,0 +1,102 @@ +<html> + <head> + <meta name="viewport" content="width=device-width, initial-scale=1.0" /> + <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> + <meta http-equiv="Content-Language" content="en" /> + <title>s6-linux-init: the s6-linux-init-reboot program</title> + <meta name="Description" content="s6-linux-init: the s6-linux-init-reboot program" /> + <meta name="Keywords" content="s6 linux init administration root utilities shutdown halt poweroff reboot" /> + <!-- <link rel="stylesheet" type="text/css" href="//skarnet.org/default.css" /> --> + </head> +<body> + +<p> +<a href="index.html">s6-linux-init</a><br /> +<a href="//skarnet.org/software/">Software</a><br /> +<a href="//skarnet.org/">skarnet.org</a> +</p> + +<h1> The <tt>s6-linux-init-reboot</tt> program </h1> + +<p> +<tt>s6-linux-init-reboot</tt> triggers the shutdown procedure in order to +reboot the system; or, with the <tt>-f</tt> option, it performs an immediate +hard reboot. +</p> + +<h2> Interface </h2> + +<pre> + s6-linux-init-reboot [ -h | -p | -r ] [ -d | -w ] [ -W ] [ -f ] [ -R <em>tmpfsdir</em> ] +</pre> + +<ul> + <li> If the <tt>-f</tt> option is present, s6-linux-init-reboot reboots the system immediately. </li> + <li> Else, it triggers the machine's shutdown procedure, which will end in a reboot. + <li> It exits 0. The shutdown procedure happens asynchronously. </li> +</ul> + +<p> + This interface follows the traditional <em>sysvinit</em> interface for the +<tt>halt</tt>, <tt>poweroff</tt> and <tt>reboot</tt> programs as close as possible. +</p> + +<h2> Exit codes </h2> + +<ul> + <li> 0: shutdown procedure triggered. </li> + <li> 100: wrong usage, or user does not have root privileges. </li> + <li> 111: system call failed. </li> +</ul> + +<h2> Options </h2> + +<ul> + <li> <tt>-h</tt> : halt. No matter the name of the command, it will order +a halt (i.e. the system will be shut down, but the power will remain up). +This is the default for <tt>s6-linux-init-halt</tt>. </li> + <li> <tt>-p</tt> : poweroff. No matter the name of the command, it will order +a power off. This is the default for <tt>s6-linux-init-poweroff</tt>. </li> + <li> <tt>-r</tt> : reboot. No matter the name of the command, it will order +a reboot. This is the default for <tt>s6-linux-init-reboot</tt>. </li> + <li> <tt>-d</tt> : Do not write a wtmp shutdown entry. </li> + <li> <tt>-w</tt> : Only write a wtmp shutdown entry; do not actually shut down +the system. </li> + <li> <tt>-W</tt> : Do not send a <tt>wall</tt> message to users before shutting +down the system. Some other implementations of the <tt>halt</tt>, <tt>poweroff</tt> +and <tt>reboot</tt> commands use the <tt>--no-wall</tt> long option to achieve this. </li> + <li> <tt>-f</tt> : force. The command will not trigger a clean shutdown +procedure; it will just sync the filesystems then tell the kernel to immediately +halt, poweroff or reboot. This should be the last step in the lifetime of the +machine. </li> + <li> <tt>-R <em>tmpfsdir</em></tt> : assume that the root-only +boot-time tmpfs has been mounted on <em>tmpfsdir</em>. Default is <tt>/run</tt>. +It is important to get this right, because the contact point with the +<a href="s6-linux-init-shutdownd.html">s6-linux-init-shutdownd</a> daemon, +which manages the shutdown procedure, is located under this directory. The +<tt>halt</tt>, <tt>poweroff</tt> and <tt>reboot</tt> scripts created by a +<a href="s6-linux-init-maker.html">s6-linux-init-maker</a> invocation +automatically use the correct <tt>-R</tt> option. </li> +</ul> + +<h2> Notes </h2> + +<ul> + <li> The +<a href="s6-linux-init-halt.html">s6-linux-init-halt</a>, +<a href="s6-linux-init-poweroff.html">s6-linux-init-poweroff</a>, +<a href="s6-linux-init-reboot.html">s6-linux-init-reboot</a> and +<a href="s6-linux-init-shutdown.html">s6-linux-init-shutdown</a> +binaries are not meant to be called directly by administrators. +Instead, their purpose is to be used in <tt>halt</tt>, <tt>poweroff</tt>, +<tt>reboot</tt> and <tt>shutdown</tt> scripts created by +<a href="s6-linux-init-maker.html">s6-linux-init-maker</a>; those scripts +will call them with the correct <tt>-R <em>tmpfsdir</em></tt> option. +The scripts should typically be linked to <tt>/sbin</tt> after +<a href="s6-linux-init-maker.html">s6-linux-init-maker</a> execution, to +provide drop-in replacements for the <em>sysvinit</em> programs with the +same names. </li> +</ul> + +</body> +</html> diff --git a/doc/s6-linux-init-shutdown.html b/doc/s6-linux-init-shutdown.html new file mode 100644 index 0000000..72bbfcf --- /dev/null +++ b/doc/s6-linux-init-shutdown.html @@ -0,0 +1,78 @@ +<html> + <head> + <meta name="viewport" content="width=device-width, initial-scale=1.0" /> + <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> + <meta http-equiv="Content-Language" content="en" /> + <title>s6-linux-init: the s6-linux-init-shutdown program</title> + <meta name="Description" content="s6-linux-init: the s6-linux-init-shutdown program" /> + <meta name="Keywords" content="s6 linux init administration root utilities shutdown halt poweroff reboot" /> + <!-- <link rel="stylesheet" type="text/css" href="//skarnet.org/default.css" /> --> + </head> +<body> + +<p> +<a href="index.html">s6-linux-init</a><br /> +<a href="//skarnet.org/software/">Software</a><br /> +<a href="//skarnet.org/">skarnet.org</a> +</p> + +<h1> The <tt>s6-linux-init-shutdown</tt> program </h1> + +<p> +<tt>s6-linux-init-shutdown</tt> triggers the system shutdown procedure. +</p> + +<h2> Interface </h2> + +<pre> + s6-linux-init-shutdown [ -h | -p | -r | -k ] [ -a ] [ -t <em>sec</em> ] [ -f | -F ] [ -R <em>tmpfsdir</em> ] <em>time</em> [ <em>message</em> ] + s6-linux-init-shutdown -c [ -R <em>tmpfsdir</em> ] [ <em>message</em> ] +</pre> + +<ul> + <li> If the <tt>-c</tt> option is present, a pending shutdown is cancelled. </li> + <li> Else, it triggers the shutdown procedure. + <li> It exits 0. The shutdown procedure happens asynchronously. </li> +</ul> + +<p> + The <tt>s6-linux-init-shutdown</tt> program conforms to the LSB-3.0.0 +<a href="https://refspecs.linuxfoundation.org/LSB_3.0.0/LSB-PDA/LSB-PDA.junk/shutdown.html">shutdown</a> +interface. +</p> + +<h2> Exit codes </h2> + +<ul> + <li> 0: shutdown procedure triggered. </li> + <li> 100: wrong usage, or user does not have root privileges. </li> + <li> 111: system call failed. </li> +</ul> + +<h2> Supplementary options </h2> + +<p> + These options are supported by <tt>s6-linux-init-shutdown</tt> but are not +part of the LSB-3.0.0 <tt>shutdown</tt> specification. +</p> + +<ul> + <li> <tt>-R <em>tmpfsdir</em></tt> : assume that the root-only +boot-time tmpfs has been mounted on <em>tmpfsdir</em>. Default is <tt>/run</tt>. +The <tt>shutdown</tt> script created by a +<a href="s6-linux-init-maker.html">s6-linux-init-maker</a> invocation +automatically execs into <tt>s6-linux-init-shutdown</tt> with the correct +<tt>-R</tt> option. </li> +</ul> + +<h2> Notes </h2> + +<ul> + <li> The <a href="s6-linux-init-shutdown.html">s6-linux-init-shutdown</a> +binary is not meant to be called directly by administrators. +Instead, its purpose is to be used in a <tt>shutdown</tt> script created by +<a href="s6-linux-init-maker.html">s6-linux-init-maker</a>. </li> +</ul> + +</body> +</html> diff --git a/doc/s6-linux-init-shutdownd.html b/doc/s6-linux-init-shutdownd.html new file mode 100644 index 0000000..b6ea412 --- /dev/null +++ b/doc/s6-linux-init-shutdownd.html @@ -0,0 +1,81 @@ +<html> + <head> + <meta name="viewport" content="width=device-width, initial-scale=1.0" /> + <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> + <meta http-equiv="Content-Language" content="en" /> + <title>s6-linux-init: the s6-linux-init-shutdownd internal program</title> + <meta name="Description" content="s6-linux-init: the s6-linux-init-shutdownd internal program" /> + <meta name="Keywords" content="s6 linux init administration root utilities shutdown halt poweroff reboot daemon shutdownd" /> + <!-- <link rel="stylesheet" type="text/css" href="//skarnet.org/default.css" /> --> + </head> +<body> + +<p> +<a href="index.html">s6-linux-init</a><br /> +<a href="//skarnet.org/software/">Software</a><br /> +<a href="//skarnet.org/">skarnet.org</a> +</p> + +<h1> The <tt>s6-linux-init-shutdownd</tt> internal program </h1> + +<p> +<tt>s6-linux-init-shutdownd</tt> is the daemon that manages the shutdown +procedure for a s6-linux-init installation. It is not meant to be called +directly by the user. +</p> + +<h2> Interface </h2> + +<pre> + s6-linux-init-shutdownd [ -b <em>bindir</em> ] [ -c <em>basedir</em> ] [ -g <em>gracetime</em> ] <em>fifo</em> +</pre> + +<ul> + <li> <tt>s6-linux-init-shutdownd</tt> opens the <em>fifo</em> named pipe +and listens to it. This is where programs such as +<a href="s6-linux-init-shutdown.html">s6-linux-init-shutdown</a> send their +commands when they are told to trigger the shutdown procedure.</li> + <li> When it receives a command to shut down, <tt>s6-linux-init-shutdownd</tt> +first spawns the <em>stage3</em> script defined by the last +<a href="s6-linux-init-maker.html">s6-linux-init-maker</a> invocation. </li> + <li> When this script exits, <tt>s6-linux-init-shutdownd</tt> kills all +processes, first with a SIGTERM, then (after the grace time specified by +the shutdown command) with a SIGKILL. </li> + <li> It then runs the automatically-generated <em>stage 4</em> script, +which unmounts the file systems and halts, powers off or reboots the +machine. </li> +</ul> + +<h2> Exit codes </h2> + +<ul> + <li> 100: wrong usage, or user does not have root privileges. </li> + <li> 111: system call failed. </li> +</ul> + +<h2> Options </h2> + +<ul> + <li> <tt>-b <em>bindir</em></tt> : look for the +<a href="//skarnet.org/software/execline/execlineb.html">execlineb</a> +script interpreter in the <em>bindir</em> directory. Default is <tt>/bin</tt>. </li> + <li> <tt>-c <em>basedir</em></tt> : look for the +<em>stage3</em> and <em>stage4</em> scripts in the <em>basedir</em> +directory. Default is <tt>/etc/s6-linux-init</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> +</ul> + +<h2> Notes </h2> + +<ul> + <li> The <a href="s6-linux-init-shutdownd.html">s6-linux-init-shutdownd</a> +binary is not meant to be called directly by administrators. It is run +by a <a href="//skarnet.org/software/s6/">s6</a> service that is automatically +generated by +<a href="s6-linux-init-maker.html">s6-linux-init-maker</a>. </li> +</ul> + +</body> +</html> diff --git a/doc/s6-poweroff.html b/doc/s6-poweroff.html deleted file mode 100644 index e215c38..0000000 --- a/doc/s6-poweroff.html +++ /dev/null @@ -1,55 +0,0 @@ -<html> - <head> - <meta name="viewport" content="width=device-width, initial-scale=1.0" /> - <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> - <meta http-equiv="Content-Language" content="en" /> - <title>s6-linux-init: the s6-poweroff program</title> - <meta name="Description" content="s6-linux-init: the s6-poweroff program" /> - <meta name="Keywords" content="s6 linux init administration root utilities shutdown halt poweroff reboot" /> - <!-- <link rel="stylesheet" type="text/css" href="//skarnet.org/default.css" /> --> - </head> -<body> - -<p> -<a href="index.html">s6-linux-init</a><br /> -<a href="//skarnet.org/software/">Software</a><br /> -<a href="//skarnet.org/">skarnet.org</a> -</p> - -<h1> The <tt>s6-poweroff</tt> program </h1> - -<p> -<tt>s6-poweroff</tt> sends a signal to process 1 in order to power off the machine; -or, with the <tt>-f</tt> option, it performs an immediate hard shutdown. -</p> - -<h2> Interface </h2> - -<pre> - s6-poweroff [ -h | -p | -r ] [ -f ] -</pre> - -<ul> - <li> s6-poweroff sends a signal to process 1. </li> - <li> It then exits 0. </li> -</ul> - -<h2> Options </h2> - -<ul> - <li> <tt>-h</tt> : halt. The command will order a halt (i.e. the system will -be shut down, but the power will remain up), which means -sending a SIGUSR2 to process 1. </li> - <li> <tt>-p</tt> : poweroff. The command will order a power off, which means -sending a SIGUSR1 to process 1. This is the default. </li> - <li> <tt>-r</tt> : reboot. The command will order a reboot, which means -sending a SIGINT to process 1. </li> - <li> <tt>-f</tt> : force. The command will not send any signal to process 1; -it will just sync the filesystems then tell the kernel to halt, poweroff or reboot. -<tt>s6-reboot -f</tt> or <tt>s6-poweroff -f</tt> should be the last program -executed in the lifetime of a machine, at the end of the shutdown script called -by process 1 when it receives a signal telling it to shut down. </li> -</ul> - -</body> -</html> diff --git a/doc/s6-reboot.html b/doc/s6-reboot.html deleted file mode 100644 index 16043c5..0000000 --- a/doc/s6-reboot.html +++ /dev/null @@ -1,55 +0,0 @@ -<html> - <head> - <meta name="viewport" content="width=device-width, initial-scale=1.0" /> - <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> - <meta http-equiv="Content-Language" content="en" /> - <title>s6-linux-init: the s6-reboot program</title> - <meta name="Description" content="s6-linux-init: the s6-reboot program" /> - <meta name="Keywords" content="s6 linux init administration root utilities shutdown halt poweroff reboot" /> - <!-- <link rel="stylesheet" type="text/css" href="//skarnet.org/default.css" /> --> - </head> -<body> - -<p> -<a href="index.html">s6-linux-init</a><br /> -<a href="//skarnet.org/software/">Software</a><br /> -<a href="//skarnet.org/">skarnet.org</a> -</p> - -<h1> The <tt>s6-reboot</tt> program </h1> - -<p> -<tt>s6-reboot</tt> sends a signal to process 1 in order to reboot the machine; -or, with the <tt>-f</tt> option, it performs an immediate reboot. -</p> - -<h2> Interface </h2> - -<pre> - s6-reboot [ -h | -p | -r ] [ -f ] -</pre> - -<ul> - <li> s6-reboot sends a signal to process 1. </li> - <li> It then exits 0. </li> -</ul> - -<h2> Options </h2> - -<ul> - <li> <tt>-h</tt> : halt. The command will order a halt (i.e. the system will -be shut down, but the power will remain up), which means -sending a SIGUSR2 to process 1. </li> - <li> <tt>-p</tt> : poweroff. The command will order a power off, which means -sending a SIGUSR1 to process 1. </li> - <li> <tt>-r</tt> : reboot. The command will order a reboot, which means -sending a SIGINT to process 1. This is the default. </li> - <li> <tt>-f</tt> : force. The command will not send any signal to process 1; -it will just sync the filesystems then tell the kernel to halt, poweroff or reboot. -<tt>s6-reboot -f</tt> or <tt>s6-poweroff -f</tt> should be the last program -executed in the lifetime of a machine, at the end of the shutdown script called -by process 1 when it receives a signal telling it to shut down. </li> -</ul> - -</body> -</html> diff --git a/doc/upgrade.html b/doc/upgrade.html index 7a5d279..2b082fb 100644 --- a/doc/upgrade.html +++ b/doc/upgrade.html @@ -33,6 +33,9 @@ anymore (stage 3 was previously the user-written <tt>/etc/rc.shutdown</tt> script). </li> <li> The default user-provided "end of stage 2, bring down services" script was named <tt>/etc/rc.tini</tt> before; now it's named <tt>/etc/rc.shutdown</tt>. </li> + <li> Everything now builds as PIC by default no matter +the toolchain's settings. Use the <tt>--disable-all-pic</tt> configure +option to build executables and static libraries as non-PIC. </li> </ul> <h2> in 0.3.1.1 </h2> diff --git a/examples/rc.init b/examples/rc.init deleted file mode 100755 index afc7735..0000000 --- a/examples/rc.init +++ /dev/null @@ -1,19 +0,0 @@ -#!/bin/execlineb -P - -# Set the system clock to something sensible, -# to get coherent logs until the initial NTP exchange - -if { s6-clock @400000005a58000000000000 } - - -# Initialize the service manager - -if { s6-rc-init /run/service } - - -# Run the service manager. -# Call it twice because the first time may spuriously "timeout" -# due to the clock jumping forward. - -if -nt { s6-rc -t 600000 -- change ok-all } -s6-rc -t 600000 -- change ok-all diff --git a/examples/rc.shutdown b/examples/rc.shutdown deleted file mode 100755 index 14b4692..0000000 --- a/examples/rc.shutdown +++ /dev/null @@ -1,16 +0,0 @@ -#!/bin/execlineb -P - - -# Write a message to /var/log/syslogd - -foreground -{ - s6-ipcclient -l0 /dev/log - fdmove 1 7 - s6-echo "/etc/rc.shutdown: shutdown requested" -} - - -# Shut down all services - -s6-rc -da change diff --git a/package/deps-build b/package/deps-build index 05d5af4..d63229b 100644 --- a/package/deps-build +++ b/package/deps-build @@ -1 +1,3 @@ /package/prog/skalibs +/package/admin/execline +/package/admin/s6 diff --git a/package/deps.mak b/package/deps.mak index 95fbd4b..3aef281 100644 --- a/package/deps.mak +++ b/package/deps.mak @@ -2,17 +2,55 @@ # This file has been generated by tools/gen-deps.sh # -src/init/hpr.o src/init/hpr.lo: src/init/hpr.c -src/init/s6-halt.o src/init/s6-halt.lo: src/init/s6-halt.c -src/init/s6-linux-init-maker.o src/init/s6-linux-init-maker.lo: src/init/s6-linux-init-maker.c -src/init/s6-poweroff.o src/init/s6-poweroff.lo: src/init/s6-poweroff.c -src/init/s6-reboot.o src/init/s6-reboot.lo: src/init/s6-reboot.c +src/include-local/initctl.h: src/include/s6-linux-init/config.h +src/shutdown/hpr.h: src/include-local/initctl.h +src/init/s6-linux-init-maker.o src/init/s6-linux-init-maker.lo: src/init/s6-linux-init-maker.c src/include-local/defaults.h src/include-local/initctl.h src/include/s6-linux-init/config.h +src/init/s6-linux-init.o src/init/s6-linux-init.lo: src/init/s6-linux-init.c src/include-local/defaults.h src/include-local/initctl.h src/include/s6-linux-init/config.h +src/lib/s6_linux_init_logouthook.o src/lib/s6_linux_init_logouthook.lo: src/lib/s6_linux_init_logouthook.c src/include-local/initctl.h +src/logout-hook/s6-linux-init-logouthookd.o src/logout-hook/s6-linux-init-logouthookd.lo: src/logout-hook/s6-linux-init-logouthookd.c +src/shutdown/hpr.o src/shutdown/hpr.lo: src/shutdown/hpr.c src/include-local/defaults.h src/shutdown/hpr.h +src/shutdown/hpr_fmttime.o src/shutdown/hpr_fmttime.lo: src/shutdown/hpr_fmttime.c src/shutdown/hpr.h +src/shutdown/hpr_shutdown.o src/shutdown/hpr_shutdown.lo: src/shutdown/hpr_shutdown.c src/shutdown/hpr.h +src/shutdown/hpr_wall.o src/shutdown/hpr_wall.lo: src/shutdown/hpr_wall.c src/shutdown/hpr.h +src/shutdown/hpr_wall_seconds.o src/shutdown/hpr_wall_seconds.lo: src/shutdown/hpr_wall_seconds.c src/shutdown/hpr.h +src/shutdown/s6-linux-init-halt.o src/shutdown/s6-linux-init-halt.lo: src/shutdown/s6-linux-init-halt.c +src/shutdown/s6-linux-init-poweroff.o src/shutdown/s6-linux-init-poweroff.lo: src/shutdown/s6-linux-init-poweroff.c +src/shutdown/s6-linux-init-reboot.o src/shutdown/s6-linux-init-reboot.lo: src/shutdown/s6-linux-init-reboot.c +src/shutdown/s6-linux-init-shutdown.o src/shutdown/s6-linux-init-shutdown.lo: src/shutdown/s6-linux-init-shutdown.c src/include-local/defaults.h src/shutdown/hpr.h src/include-local/initctl.h +src/shutdown/s6-linux-init-shutdownd.o src/shutdown/s6-linux-init-shutdownd.lo: src/shutdown/s6-linux-init-shutdownd.c src/include-local/defaults.h src/shutdown/hpr.h src/include-local/initctl.h +src/shutdown/s6-linux-init-single.o src/shutdown/s6-linux-init-single.lo: src/shutdown/s6-linux-init-single.c src/include-local/initctl.h +src/shutdown/s6-linux-init-telinit.o src/shutdown/s6-linux-init-telinit.lo: src/shutdown/s6-linux-init-telinit.c src/include-local/defaults.h src/shutdown/hpr.h src/include-local/initctl.h -s6-halt: EXTRA_LIBS := -s6-halt: src/init/s6-halt.o -lskarnet +s6-linux-init: EXTRA_LIBS := +s6-linux-init: src/init/s6-linux-init.o -lskarnet s6-linux-init-maker: EXTRA_LIBS := s6-linux-init-maker: src/init/s6-linux-init-maker.o -lskarnet -s6-poweroff: EXTRA_LIBS := -s6-poweroff: src/init/s6-poweroff.o -lskarnet -s6-reboot: EXTRA_LIBS := -s6-reboot: src/init/s6-reboot.o -lskarnet +ifeq ($(strip $(STATIC_LIBS_ARE_PIC)),) +libs6_linux_init.a.xyzzy: src/lib/s6_linux_init_logouthook.o +else +libs6_linux_init.a.xyzzy: src/lib/s6_linux_init_logouthook.lo +endif +libs6_linux_init.so.xyzzy: EXTRA_LIBS := -lskarnet ${SOCKET_LIB} ${TAINNOW_LIB} +libs6_linux_init.so.xyzzy: src/lib/s6_linux_init_logouthook.lo +s6-linux-init-logouthookd: EXTRA_LIBS := ${TAINNOW_LIB} ${SOCKET_LIB} +s6-linux-init-logouthookd: src/logout-hook/s6-linux-init-logouthookd.o ${LIBUTMPS} -lskarnet +ifeq ($(strip $(STATIC_LIBS_ARE_PIC)),) +libhpr.a.xyzzy: src/shutdown/hpr_fmttime.o src/shutdown/hpr_shutdown.o src/shutdown/hpr_wall.o src/shutdown/hpr_wall_seconds.o +else +libhpr.a.xyzzy: src/shutdown/hpr_fmttime.lo src/shutdown/hpr_shutdown.lo src/shutdown/hpr_wall.lo src/shutdown/hpr_wall_seconds.lo +endif +s6-linux-init-halt: EXTRA_LIBS := ${TAINNOW_LIB} ${SOCKET_LIB} +s6-linux-init-halt: src/shutdown/s6-linux-init-halt.o libhpr.a.xyzzy ${LIBUTMPS} -lskarnet +s6-linux-init-poweroff: EXTRA_LIBS := ${TAINNOW_LIB} ${SOCKET_LIB} +s6-linux-init-poweroff: src/shutdown/s6-linux-init-poweroff.o libhpr.a.xyzzy ${LIBUTMPS} -lskarnet +s6-linux-init-reboot: EXTRA_LIBS := ${TAINNOW_LIB} ${SOCKET_LIB} +s6-linux-init-reboot: src/shutdown/s6-linux-init-reboot.o libhpr.a.xyzzy ${LIBUTMPS} -lskarnet +s6-linux-init-shutdown: EXTRA_LIBS := ${TAINNOW_LIB} ${SOCKET_LIB} +s6-linux-init-shutdown: src/shutdown/s6-linux-init-shutdown.o libhpr.a.xyzzy ${LIBUTMPS} -lskarnet +s6-linux-init-shutdownd: EXTRA_LIBS := ${TAINNOW_LIB} ${SOCKET_LIB} +s6-linux-init-shutdownd: src/shutdown/s6-linux-init-shutdownd.o ${LIBUTMPS} -lskarnet +s6-linux-init-single: EXTRA_LIBS := +s6-linux-init-single: src/shutdown/s6-linux-init-single.o -lskarnet +s6-linux-init-telinit: EXTRA_LIBS := +s6-linux-init-telinit: src/shutdown/s6-linux-init-telinit.o libhpr.a.xyzzy -lskarnet +INTERNAL_LIBS := libhpr.a.xyzzy diff --git a/package/info b/package/info index f7c3758..d58a5f1 100644 --- a/package/info +++ b/package/info @@ -1,4 +1,4 @@ package=s6-linux-init -version=0.4.0.0 +version=1.0.0.0 category=admin package_macro_name=S6_LINUX_INIT diff --git a/package/modes b/package/modes index f2fafad..cf55736 100644 --- a/package/modes +++ b/package/modes @@ -1,4 +1,10 @@ -s6-linux-init-maker 0700 -s6-halt 0700 -s6-poweroff 0700 -s6-reboot 0700 +s6-linux-init-single 0700 +s6-linux-init-halt 0700 +s6-linux-init-poweroff 0700 +s6-linux-init-reboot 0700 +s6-linux-init-shutdown 0700 +s6-linux-init-shutdownd 0700 +s6-linux-init-logouthookd 0700 +s6-linux-init 0700 +s6-linux-init-telinit 0700 +s6-linux-init-maker 0700 diff --git a/package/targets.mak b/package/targets.mak index 26ef489..1462ca6 100644 --- a/package/targets.mak +++ b/package/targets.mak @@ -1,7 +1,13 @@ BIN_TARGETS := \ -s6-linux-init-maker \ -s6-halt \ -s6-poweroff \ -s6-reboot +s6-linux-init-single \ +s6-linux-init-halt \ +s6-linux-init-poweroff \ +s6-linux-init-reboot \ +s6-linux-init-shutdown \ +s6-linux-init-shutdownd \ +s6-linux-init-logouthookd \ +s6-linux-init \ +s6-linux-init-telinit \ +s6-linux-init-maker -LIBEXEC_TARGETS := +LIB_DEFS := S6_LINUX_INIT=s6_linux_init diff --git a/src/include-local/defaults.h b/src/include-local/defaults.h new file mode 100644 index 0000000..dc36e0f --- /dev/null +++ b/src/include-local/defaults.h @@ -0,0 +1,10 @@ +/* ISC license. */ + +#ifndef S6_LINUX_INIT_DEFAULTS_H +#define S6_LINUX_INIT_DEFAULTS_H + +#define BINDIR "/bin" +#define BASEDIR "/etc/s6-linux-init" +#define INITPATH "/usr/bin:/bin" + +#endif diff --git a/src/include-local/initctl.h b/src/include-local/initctl.h new file mode 100644 index 0000000..4102640 --- /dev/null +++ b/src/include-local/initctl.h @@ -0,0 +1,32 @@ +/* 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 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-global" +#define ENVSTAGE2 "env-stage2" +#define STAGE2 "stage2" +#define STAGE3 "stage3" +#define STAGE4 "stage4" + +#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-reboot b/src/init/deps-exe/s6-reboot deleted file mode 100644 index e7187fe..0000000 --- a/src/init/deps-exe/s6-reboot +++ /dev/null @@ -1 +0,0 @@ --lskarnet diff --git a/src/init/hpr.c b/src/init/hpr.c deleted file mode 100644 index db80ba2..0000000 --- a/src/init/hpr.c +++ /dev/null @@ -1,107 +0,0 @@ -/* ISC license. */ - -#include <skalibs/sysdeps.h> -#include <skalibs/nonposix.h> -#include <unistd.h> -#include <signal.h> -#include <errno.h> -#include <sys/reboot.h> -#include <skalibs/strerr2.h> -#include <skalibs/sgetopt.h> -#include <skalibs/sig.h> -#include <skalibs/djbunix.h> - -#define USAGE PROGNAME " [ -h | -p | -r ] [ -f ]" - -#ifdef SKALIBS_HASNSGETPARENT - -#include <sys/ioctl.h> -#include <linux/nsfs.h> - -static int test_in_namespace (void) -{ - int r ; - int fd = open_read("/proc/1/ns/pid") ; - if (fd < 0) return 0 ; - r = ioctl(fd, NS_GET_PARENT) ; - close(fd) ; - return r >= 0 ; -} - -#else - - /* - When in doubt, always trap signals. This incurs a small race: - if ctrl-alt-del is pressed at the wrong time, the process will - exit and cause a kernel panic. But the alternatives are WAY - more hackish than this. - */ - -static int test_in_namespace (void) -{ - return 1 ; -} - -#endif - -static void sigint_handler (int sig) -{ - (void)sig ; - _exit(1) ; -} - -static void sighup_handler (int sig) -{ - (void)sig ; - _exit(0) ; -} - -int main (int argc, char const *const *argv) -{ - int what = WHATDEFAULT ; - int force = 0 ; - PROG = PROGNAME ; - - { - subgetopt_t l = SUBGETOPT_ZERO ; - for (;;) - { - int opt = subgetopt_r(argc, argv, "hprf", &l) ; - if (opt == -1) break ; - switch (opt) - { - case 'h' : what = 1 ; break ; - case 'p' : what = 2 ; break ; - case 'r' : what = 3 ; break ; - case 'f' : force = 1 ; break ; - default : strerr_dieusage(100, USAGE) ; - } - } - argc -= l.ind ; argv += l.ind ; - } - - if (geteuid()) - { - errno = EPERM ; - strerr_dief1sys(100, "nice try, peon") ; - } - - if (force) - { - sync() ; - if (getpid() == 1) - { - if (test_in_namespace()) - { - if (sig_catch(SIGINT, &sigint_handler) < 0 - || sig_catch(SIGHUP, &sighup_handler) < 0) - strerr_diefu1sys(111, "catch signals") ; - } - } - reboot(what == 3 ? RB_AUTOBOOT : what == 2 ? RB_POWER_OFF : RB_HALT_SYSTEM) ; - strerr_diefu1sys(111, "reboot()") ; - } - else if (kill(1, what == 3 ? SIGINT : what == 2 ? SIGUSR1 : SIGUSR2) < 0) - strerr_diefu1sys(111, "signal process 1") ; - return 0 ; -} diff --git a/src/init/s6-linux-init-maker.c b/src/init/s6-linux-init-maker.c index 30866fe..48c95c6 100644 --- a/src/init/s6-linux-init-maker.c +++ b/src/init/s6-linux-init-maker.c @@ -16,11 +16,17 @@ #include <skalibs/sgetopt.h> #include <skalibs/skamisc.h> -#define USAGE "s6-linux-init-maker [ -c basedir ] [ -l tmpfsdir ] [ -b execline_bindir ] [ -u log_uid -g log_gid | -U ] [ -G early_getty_cmd ] [ -2 stage2_script ] [ -r ] [ -Z shutdownscript ] [ -p initial_path ] [ -m initial_umask ] [ -t timestamp_style ] [ -d dev_style ] [ -s env_store ] [ -e initial_envvar ... ] [ -n ] [ -q final_sleep_time ] dir" +#include <s6/config.h> + +#include <s6-linux-init/config.h> +#include "defaults.h" +#include "initctl.h" + +#define USAGE "s6-linux-init-maker [ -c basedir ] [ -b execline_bindir ] [ -u log_uid -g log_gid | -U ] [ -G early_getty_cmd ] [ -r ] [ -L ] [ -p initial_path ] [ -m initial_umask ] [ -t timestamp_style ] [ -d slashdev ] [ -s env_store ] [ -e initial_envvar ... ] [ -q default_grace_time ] dir" #define dieusage() strerr_dieusage(100, USAGE) #define dienomem() strerr_diefu1sys(111, "stralloc_catb") ; -#define BANNER "\n init created by s6-linux-init-maker\n see http://skarnet.org/software/s6-linux-init/\n\n" +#define UNCAUGHT_DIR "uncaught-logs" #define EXITCODENAME "file\\ created\\ by\\ s6-linux-init,\\ storing\\ a\\ container's\\ exit\\ code" #define CRASH_SCRIPT \ @@ -28,248 +34,177 @@ "redirfd -w 1 /dev/console\n" \ "fdmove -c 2 1\n" \ "foreground { s6-echo -- " \ -"\"s6-svscan crashed. Dropping to an interactive shell.\" }\n" \ +"\"s6-svscan finished. Dropping to an interactive shell.\" }\n" \ "/bin/sh -i\n" -static char const *slashrun = "/run" ; -static char const *robase = "/etc/s6-linux-init" ; -static char const *init_script = "/etc/rc.init" ; -static char const *tini_script = "/etc/rc.shutdown" ; -static char const *bindir = "/bin" ; -static char const *initial_path = "/usr/bin:/usr/sbin:/bin:/sbin" ; +static char const *robase = BASEDIR ; +static char const *bindir = BINDIR ; +static char const *initial_path = INITPATH ; static char const *env_store = 0 ; static char const *early_getty = 0 ; +static char const *slashdev = 0 ; static uid_t uncaught_logs_uid = 0 ; static gid_t uncaught_logs_gid = 0 ; -static unsigned int initial_umask = 022 ; +static unsigned int initial_umask = 0022 ; static unsigned int timestamp_style = 1 ; -static unsigned int slashdev_style = 2 ; -static unsigned int finalsleep = 2000 ; +static unsigned int finalsleep = 3000 ; static int redirect_stage2 = 0 ; -static int in_namespace = 0 ; +static int logouthookd = 0 ; -typedef int writetobuf_func_t (buffer *) ; +typedef int writetobuf_func_t (buffer *, void *) ; typedef writetobuf_func_t *writetobuf_func_t_ref ; -static int put_shebang (buffer *b) +#define put_shebang(b) put_shebang_options((b), "-P") + +static int put_shebang_options (buffer *b, void *data) { + char *options = data ; return buffer_puts(b, "#!") >= 0 && buffer_puts(b, bindir) >= 0 - && buffer_puts(b, "/execlineb -P\n\n") >= 0 ; + && buffer_puts(b, "/execlineb") >= 0 + && (!options || !options[0] || buffer_puts(b, " ") >= 0) + && buffer_puts(b, options) >= 0 + && buffer_puts(b, "\n\n") >= 0 ; } -static int early_getty_script (buffer *b) +static int line_script (buffer *b, void *data) { + char *line = data ; return put_shebang(b) - && buffer_puts(b, early_getty) >= 0 + && buffer_puts(b, line) >= 0 && buffer_put(b, "\n", 1) >= 0 ; } -static int crash_script (buffer *b) +static int linewithargs_script (buffer *b, void *data) +{ + char *line = data ; + return put_shebang_options(b, "-S0") + && buffer_puts(b, line) >= 0 + && buffer_puts(b, " $@\n") >= 0 ; +} + +static int death_script (buffer *b, void *data) { + char *s = data ; return put_shebang(b) - && buffer_puts(b, CRASH_SCRIPT) >= 0 ; + && buffer_puts(b, + "redirfd -r 0 /dev/console\n" + "redirfd -w 1 /dev/console\n" + "fdmove -c 2 1\n" + "foreground { s6-echo -- \"s6-svscan ") >= 0 + && buffer_puts(b, s) >= 0 + && buffer_puts(b, + ". Dropping to an interactive shell.\" }\n" + "/bin/sh -i\n") >= 0 } -static int s6_svscan_log_script (buffer *b) +static int s6_svscan_log_script (buffer *b, void *data) { - size_t sabase = satmp.len ; char fmt[UINT64_FMT] ; - if (!put_shebang(b) - || buffer_puts(b, + (void)data ; + return put_shebang(b) + && buffer_puts(b, "redirfd -w 2 /dev/console\n" "redirfd -w 1 /dev/null\n" - "redirfd -rnb 0 fifo\n" - "s6-applyuidgid -u ") < 0 - || buffer_put(b, fmt, uid_fmt(fmt, uncaught_logs_uid)) < 0 - || buffer_puts(b, " -g ") < 0 - || buffer_put(b, fmt, gid_fmt(fmt, uncaught_logs_gid)) < 0 - || buffer_puts(b, " --\ns6-log -bp -- ") < 0 - || buffer_puts(b, timestamp_style & 1 ? "t " : "") < 0 - || buffer_puts(b, timestamp_style & 2 ? "T " : "") < 0) return 0 ; - if (!string_quote(&satmp, slashrun, strlen(slashrun))) return 0 ; - if (buffer_put(b, satmp.s + sabase, satmp.len - sabase) < 0) - { - satmp.len = sabase ; - return 0 ; - } - satmp.len = sabase ; - if (buffer_puts(b, "/uncaught-logs\n") < 0) return 0 ; - return 1 ; + "redirfd -rnb 0 " LOGGER_FIFO "\n" + "s6-applyuidgid -u ") >= 0 + && buffer_put(b, fmt, uid_fmt(fmt, uncaught_logs_uid)) >= 0 + && buffer_puts(b, " -g ") >= 0 + && buffer_put(b, fmt, gid_fmt(fmt, uncaught_logs_gid)) >= 0 + && buffer_puts(b, " --\ns6-log -bpd3 -- ") >= 0 + && buffer_puts(b, timestamp_style & 1 ? "t " : "") >= 0 + && buffer_puts(b, timestamp_style & 2 ? "T " : "") >= 0 + && buffer_puts(b, S6_LINUX_INIT_TMPFS "/" UNCAUGHT_DIR "\n") >= 0 ; } -static int finish_script (buffer *b) +static int logouthookd_script (buffer *b, void *data) { - size_t sabase = satmp.len ; - char fmt[UINT_FMT] ; - fmt[uint_fmt(fmt, finalsleep)] = 0 ; - if (buffer_puts(b, "#!") < 0 - || buffer_puts(b, bindir) < 0 - || buffer_puts(b, "/execlineb -S0\n\n") < 0 - || !string_quote(&satmp, slashrun, strlen(slashrun))) return 0 ; - - if (in_namespace) - { - if (buffer_puts(b, "ifelse { redirfd -w 2 /dev/null redirfd -r 0 ") < 0 - || buffer_put(b, satmp.s + sabase, satmp.len - sabase) < 0 - || buffer_puts(b, "/" EXITCODENAME " exit 0 }\n{ redirfd -r 0 ") < 0 - || buffer_put(b, satmp.s + sabase, satmp.len - sabase) < 0 - || buffer_puts(b, "/" EXITCODENAME " withstdinas -in CODE foreground { s6-rmrf ") < 0 - || buffer_put(b, satmp.s + sabase, satmp.len - sabase) < 0 - || buffer_puts(b, "/" EXITCODENAME " } importas -ui CODE CODE exit ${CODE} }\n") < 0) goto err ; - } - - if (buffer_puts(b, "cd /\nredirfd -w 2 /dev/console\nfdmove -c 1 2\nforeground { s6-svc -X -- ") < 0 - || buffer_put(b, satmp.s + sabase, satmp.len - sabase) < 0) goto err ; - satmp.len = sabase ; - if (buffer_puts(b, "/service/s6-svscan-log }\n" - "unexport ?\nwait -r -- { }\n" - "foreground { s6-echo \"Syncing disks.\" }\n" - "foreground { s6-sync }\n" - "foreground { s6-echo \"Sending all processes the TERM signal.\" }\n" - "foreground { s6-nuke -th }\n" - "s6-sleep -m -- ") < 0 - || buffer_puts(b, fmt) < 0 - || buffer_puts(b, "\nforeground { s6-echo \"Sending all processes the KILL signal.\" }\n" - "foreground { s6-nuke -k }\n" - "wait { }\n" - "foreground { s6-echo \"Unmounting disks.\" }\n" - "foreground { s6-umount -a }\n" - "foreground { s6-mount -ro remount /dev/root / }\n" - "s6-${1} -f\n") < 0) return 0 ; - return 1 ; - err: - satmp.len = sabase ; - return 0 ; + (void)data ; + return put_shebang(b) + && buffer_puts(b, + S6_EXTBINPREFIX "s6-ipcserver -1 -l0 -- " LOGOUTHOOKD_SOCKET "\n" + S6_LINUX_INIT_BINPREFIX "s6-linux-init-logouthookd\n") >= 0 ; } -static int sig_script (buffer *b, char c) +static int shutdownd_script (buffer *b, void *data) { size_t sabase = satmp.len ; + char fmt[UINT_FMT] ; if (!put_shebang(b) - || buffer_puts(b, "foreground { ") < 0 - || !string_quote(&satmp, tini_script, strlen(tini_script))) return 0 ; - if (buffer_put(b, satmp.s + sabase, satmp.len - sabase) < 0) goto err ; + || buffer_puts(b, S6_LINUX_INIT_BINPREFIX "s6-linux-init-shutdownd -b ") < 0) + || !string_quote(&satmp, bindir, strlen(bindir))) return 0 ; + if (buffer_puts(b, satmp.s + sabase) < 0) goto err ; satmp.len = sabase ; - if (buffer_puts(b, " }\ns6-svscanctl -") < 0 - || buffer_put(b, &c, 1) < 0 - || buffer_puts(b, " -- ") < 0 - || !string_quote(&satmp, slashrun, strlen(slashrun))) return 0 ; - if (buffer_put(b, satmp.s + sabase, satmp.len - sabase) < 0) goto err ; + if (buffer_puts(b, " -c ") < 0 + || !string_quote(&satmp, robase, strlen(robase))) return 0 ; + if (buffer_puts(b, satmp.s + sabase) < 0) goto err ; satmp.len = sabase ; - if (buffer_puts(b, "/service\n") < 0) return 0 ; + if (buffer_puts(b, " -g ") < 0 + || buffer_puts(b, fmt, uint_fmt(fmt, finalsleep)) < 0 + || buffer_puts(b, "\n") < 0) return 0 ; + (void)data ; return 1 ; - err: - satmp.len = sabase ; - return 0 ; -} -static int onlyexit (buffer *b, char c) -{ - size_t sabase = satmp.len ; - if (!put_shebang(b) - || buffer_puts(b, "foreground { redirfd -w 1 ") < 0 - || !string_quote(&satmp, slashrun, strlen(slashrun)) - || buffer_put(b, satmp.s + sabase, satmp.len - sabase) < 0 - || buffer_puts(b, "/" EXITCODENAME " s6-echo -- ") < 0 - || buffer_put(b, &c, 1) < 0 - || buffer_puts(b, " }\ns6-svscanctl -b ") < 0 - || buffer_put(b, satmp.s + sabase, satmp.len - sabase) < 0 - || buffer_puts(b, "/service\n") < 0) goto err ; - satmp.len = sabase ; - return 1 ; err: satmp.len = sabase ; - return 0 ; -} - -static int sigterm_script (buffer *b) -{ - return sig_script(b, 't') ; -} - -static int sighup_script (buffer *b) -{ - return in_namespace ? onlyexit(b, '0') : sig_script(b, 'h') ; -} - -static int sigquit_script (buffer *b) -{ - return sig_script(b, 'q') ; + return 0 ; } -static int sigint_script (buffer *b) +static int sig_script (buffer *b, void *data) { - return in_namespace ? onlyexit(b, '1') : sig_script(b, '6') ; -} - -static int sigusr1_script (buffer *b) -{ - return sig_script(b, '7') ; + char *option = data ; + return put_shebang(b) + && buffer_puts(b, S6_LINUX_INIT_BINPREFIX "s6-linux-init-shutdown -a ") >= 0 + && buffer_puts(b, option) >= 0 + && buffer_puts(" -- now\n") >= 0 ; } -static int sigusr2_script (buffer *b) +static int stage4_script (buffer *b, void *data) { - return sig_script(b, '0') ; + (void)data ; } static inline int stage1_script (buffer *b) { - size_t sabase = satmp.len, pos, pos2 ; - char fmt[UINT_OFMT] ; - if (!put_shebang(b) - || buffer_puts(b, bindir) < 0 - || buffer_puts(b, "/export PATH ") < 0 - || !string_quote(&satmp, initial_path, strlen(initial_path))) return 0 ; - if (buffer_put(b, satmp.s + sabase, satmp.len - sabase) < 0) goto err ; - satmp.len = sabase ; - if (buffer_put(b, "\n", 1) < 0 - || buffer_puts(b, bindir) < 0 - || buffer_puts(b, "/cd /\ns6-setsid -qb --\numask 0") < 0 - || buffer_put(b, fmt, uint_ofmt(fmt, initial_umask)) < 0 - || buffer_puts(b, "\nif { s6-echo -- ") < 0 - || !string_quote(&satmp, BANNER, sizeof(BANNER) - 1)) return 0 ; - if (buffer_put(b, satmp.s, satmp.len) < 0) goto err ; - satmp.len = sabase ; - if (buffer_puts(b, " }\nif { s6-mount -nwt tmpfs -o mode=0755 tmpfs ") < 0 - || !string_quote(&satmp, slashrun, strlen(slashrun))) return 0 ; - pos = satmp.len ; - if (buffer_put(b, satmp.s + sabase, pos - sabase) < 0 - || buffer_puts(b, " }\nif { s6-hiercopy ") < 0 + size_t sabase = satmp.len ; + if (!put_shebang_options(b, "-S0") + || buffer_puts(b, S6_LINUX_INIT_EXTBINPREFIX "s6-linux-init -c ") < 0 || !string_quote(&satmp, robase, strlen(robase))) return 0 ; - pos2 = satmp.len ; - if (buffer_put(b, satmp.s + pos, pos2 - pos) < 0 - || buffer_puts(b, "/run-image ") < 0 - || buffer_put(b, satmp.s + sabase, pos - sabase) < 0 - || buffer_puts(b, " }\n") < 0) goto err ; - if (slashdev_style == 1) + if (buffer_puts(b, satmp.s + sabase) < 0) goto err ; + satmp.len = sabase ; + { + char fmt[UINT_OFMT] ; + if (buffer_puts(b, " -m 00") < 0 + || buffer_put(b, fmt, uint_ofmt(fmt, initial_umask)) < 0) return 0 ; + } + if (redirect_stage2) { - if (buffer_puts(b, "if { s6-mount -nt devtmpfs dev /dev }\n") < 0) goto err ; + if (buffer_puts(b, " -r") < 0) return 0 ; + } + if (initial_path) + { + if (buffer_puts(b, " -p ") < 0 + || !string_quote(&satmp, initial_path, strlen(initial_path))) return 0 ; + if (buffer_puts(b, satmp.s + sabase) < 0) goto err ; + satmp.len = sabase ; } if (env_store) { - size_t base = satmp.len ; - if (!string_quote(&satmp, env_store, strlen(env_store))) return 0 ; - if (buffer_puts(b, "if { unexport PATH s6-dumpenv -- ") < 0 - || buffer_put(b, satmp.s + base, satmp.len - base) < 0 - || buffer_puts(b, " }\n") < 0) goto err ; - satmp.len = base ; + if (buffer_puts(b, " -s ") < 0 + || !string_quote(&satmp, env_store, strlen(env_store))) return 0 ; + if (buffer_puts(b, satmp.s + sabase) < 0) goto err ; + satmp.len = sabase ; + } + if (slashdev) + { + if (buffer_puts(b, " -d ") < 0 + || !string_quote(&satmp, slashdev, strlen(slashdev))) return 0 ; + if (buffer_puts(b, satmp.s + sabase) < 0) goto err ; + satmp.len = sabase ; } - if (buffer_puts(b, "emptyenv -p\ns6-envdir -I -- ") < 0 - || buffer_put(b, satmp.s + pos, pos2 - pos) < 0 - || buffer_puts(b, "/env\nredirfd -r 0 /dev/null\nredirfd -wnb 1 ") < 0 - || buffer_put(b, satmp.s + sabase, pos - sabase) < 0 - || buffer_puts(b, "/service/s6-svscan-log/fifo\nbackground\n{\n s6-setsid --\n redirfd -w 1 ") < 0 - || buffer_put(b, satmp.s + sabase, pos - sabase) < 0 - || buffer_puts(b, "/service/s6-svscan-log/fifo\n fdmove -c ") < 0 - || buffer_puts(b, redirect_stage2 ? "2 1" : "1 2") < 0 - || buffer_puts(b, "\n ") < 0 - || !string_quote(&satmp, init_script, strlen(init_script)) - || buffer_put(b, satmp.s + pos2, satmp.len - pos2) < 0 - || buffer_puts(b, "\n}\nunexport !\ncd ") < 0 - || buffer_put(b, satmp.s + sabase, pos - sabase) < 0 - || buffer_puts(b, "/service\nfdmove -c 2 1\ns6-svscan -st0\n") < 0) goto err ; + if (buffer_puts(b, "\n") < 0) return 0 ; return 1 ; + err: satmp.len = sabase ; return 0 ; @@ -307,13 +242,25 @@ static void auto_file (char const *base, char const *file, char const *s, unsign fn[clen] = '/' ; memcpy(fn + clen + 1, file, flen + 1) ; if (!openwritenclose_unsafe(fn, s, n) - || (executable && chmod(fn, 0755) < 0)) + || chmod(fn, executable ? 0755 : 0644) == -1)) { cleanup(base) ; strerr_diefu2sys(111, "write to ", fn) ; } } +static void auto_symlink (char const *base, char const *name, char const *target) +{ + size_t clen = strlen(base) ; + size_t dlen = strlen(name) ; + char fn[clen + dlen + 2] ; + memcpy(fn, base, clen) ; + fn[clen] = '/' ; + memcpy(fn + clen + 1, name, dlen + 1) ; + if (symlink(target, fn) == -1) + strerr_diefu4sys(111, "make a symlink named ", fn, " pointing to ", target) ; +} + static void auto_fifo (char const *base, char const *fifo) { size_t baselen = strlen(base) ; @@ -329,7 +276,7 @@ static void auto_fifo (char const *base, char const *fifo) } } -static void auto_script (char const *base, char const *file, writetobuf_func_t_ref scriptf) +static void auto_script (char const *base, char const *file, writetobuf_func_t_ref scriptf, void *data) { char buf[4096] ; buffer b ; @@ -347,25 +294,40 @@ static void auto_script (char const *base, char const *file, writetobuf_func_t_r strerr_diefu3sys(111, "open ", fn, " for script writing") ; } buffer_init(&b, &fd_writev, fd, buf, 4096) ; - if (!(*scriptf)(&b) || !buffer_flush(&b)) + if (!(*scriptf)(&b, data) || !buffer_flush(&b)) { cleanup(base) ; strerr_diefu2sys(111, "write to ", fn) ; } - close(fd) ; + fd_close(fd) ; } -static inline void make_env (char const *base, char const *modif, size_t modiflen) +static void auto_exec (char const *base, char const *name, char const *target) { - auto_dir(base, "env", 0, 0, 0755) ; + if (S6_LINUX_INIT_BINPREFIX[0] == '/') + { + size_t len = strlen(target) ; + char fn[sizeof(S6_LINUX_INIT_BINPREFIX) + len] = S6_LINUX_INIT_BINPREFIX ; + memcpy(fn + sizeof(S6_LINUX_INIT_BINPREFIX - 1, len + 1, target)) ; + auto_symlink(base, name, fn) ; + } + else + auto_script(base, name, &linewithargs_script, target) ; +} + +static void make_env (char const *base, char const *envname, char const *modif, size_t modiflen) +{ + size_t envnamelen = strlen(envname) ; + auto_dir(base, envname, 0, 0, 0755) ; while (modiflen) { size_t len = strlen(modif) ; size_t pos = byte_chr(modif, len, '=') ; - char fn[5 + pos] ; - memcpy(fn, "env/", 4) ; - memcpy(fn + 4, modif, pos) ; - fn[4 + pos] = 0 ; + char fn[envnamelen + pos + 2] ; + memcpy(fn, envname, envnamelen) ; + fn[envnamelen] = '/' ; + memcpy(fn + envnamelen + 1, modif, pos) ; + fn[envnamelen + 1 + pos] = 0 ; if (pos + 1 < len) auto_file(base, fn, modif + pos + 1, len - pos - 1, 0) ; else if (pos + 1 == len) auto_file(base, fn, "\n", 1, 0) ; @@ -377,41 +339,71 @@ static inline void make_env (char const *base, char const *modif, size_t modifle static inline void make_image (char const *base) { auto_dir(base, "run-image", 0, 0, 0755) ; - auto_dir(base, "run-image/uncaught-logs", uncaught_logs_uid, uncaught_logs_gid, 02700) ; - auto_dir(base, "run-image/service", 0, 0, 0755) ; - auto_dir(base, "run-image/service/.s6-svscan", 0, 0, 0755) ; - auto_script(base, "run-image/service/.s6-svscan/crash", &crash_script) ; - auto_script(base, "run-image/service/.s6-svscan/finish", &finish_script) ; - auto_script(base, "run-image/service/.s6-svscan/SIGTERM", &sigterm_script) ; - auto_script(base, "run-image/service/.s6-svscan/SIGHUP", &sighup_script) ; - auto_script(base, "run-image/service/.s6-svscan/SIGQUIT", &sigquit_script) ; - auto_script(base, "run-image/service/.s6-svscan/SIGINT", &sigint_script) ; - auto_script(base, "run-image/service/.s6-svscan/SIGUSR1", &sigusr1_script) ; - auto_script(base, "run-image/service/.s6-svscan/SIGUSR2", &sigusr2_script) ; - auto_dir(base, "run-image/service/s6-svscan-log", 0, 0, 0755) ; - auto_fifo(base, "run-image/service/s6-svscan-log/fifo") ; - auto_script(base, "run-image/service/s6-svscan-log/run", &s6_svscan_log_script) ; + auto_dir(base, "run-image/" UNCAUGHT_DIR, uncaught_logs_uid, uncaught_logs_gid, 02700) ; + auto_dir(base, "run-image/" SCANDIR, 0, 0, 0755) ; + auto_dir(base, "run-image/" SCANDIR "/.s6-svscan", 0, 0, 0755) ; + auto_script(base, "run-image/" SCANDIR "/.s6-svscan/crash", &death_script, "crashed") ; + auto_script(base, "run-image/" SCANDIR "/.s6-svscan/finish", &death_script, "exited") ; + auto_script(base, "run-image/" SCANDIR "/.s6-svscan/SIGTERM", &put_shebang_options, 0) ; + auto_script(base, "run-image/" SCANDIR "/.s6-svscan/SIGHUP", &put_shebang_options, 0) ; + auto_script(base, "run-image/" SCANDIR "/.s6-svscan/SIGQUIT", &put_shebang_options, 0) ; + auto_script(base, "run-image/" SCANDIR "/.s6-svscan/SIGINT", &sig_script, "-r") ; + auto_script(base, "run-image/" SCANDIR "/.s6-svscan/SIGUSR1", &sig_script, "-p") ; + auto_script(base, "run-image/" SCANDIR "/.s6-svscan/SIGUSR2", &sig_script, "-h") ; + auto_dir(base, "run-image/" SCANDIR "/" LOGGER_SERVICEDIR, 0, 0, 0755) ; + auto_fifo(base, "run-image/" SCANDIR "/" LOGGER_SERVICEDIR "/" LOGGER_FIFO) ; + auto_file(base, "run-image/" SCANDIR "/" LOGGER_SERVICEDIR "/notification-fd", "3\n", 2, 0) ; + auto_script(base, "run-image/" SCANDIR "/" LOGGER_SERVICEDIR "/run, &s6_svscan_log_script, 0) ; + auto_dir(base, "run-image/" SCANDIR "/" SHUTDOWND_SERVICEDIR, 0, 0, 0755) ; + auto_fifo(base, "run-image/" SCANDIR "/" SHUTDOWND_SERVICEDIR "/" SHUTDOWND_FIFO) ; + auto_script(base, "run-image/" SCANDIR "/" SHUTDOWND_SERVICEDIR "/run", &shutdownd_script, 0) ; + if (logouthookd) + { + auto_dir(base, "run-image/" SCANDIR "/" LOGOUTHOOKD_SERVICEDIR, 0, 0, 0755) ; + auto_file(base, "run-image/" SCANDIR "/" LOGOUTHOOKD_SERVICEDIR "/notification-fd", "1\n", 2, 0) ; + auto_script(base, "run-image/" SCANDIR "/" LOGOUTHOOKD_SERVICEDIR "/run", &logouthookd_script, 0) ; + } if (early_getty) { - auto_dir(base, "run-image/service/s6-linux-init-early-getty", 0, 0, 0755) ; - auto_script(base, "run-image/service/s6-linux-init-early-getty/run", &early_getty_script) ; + auto_dir(base, "run-image/" SCANDIR "/" EARLYGETTY_SERVICEDIR, 0, 0, 0755) ; + auto_script(base, "run-image/" SCANDIR "/" EARLYGETTY_SERVICEDIR "/run", &line_script, early_getty) ; } - auto_script(base, "init", &stage1_script) ; +} + +static inline void make_scripts (char const *base) +{ + auto_dir(base, "scripts", 0, 0, 0755) ; + auto_script(base, "scripts/runlevel", &put_shebang_options, 0) ; + auto_script(base, "scripts/" STAGE2, &put_shebang_options, 0) ; + auto_script(base, "scripts/" STAGE3, &put_shebang_options, 0) ; +} + +static inline void make_bins (char const *base) +{ + auto_dir(base, "bin", 0, 0, 0755) ; + auto_exec(base, "bin/halt", "s6-linux-init-halt") ; + auto_exec(base, "bin/reboot", "s6-linux-init-reboot") ; + auto_exec(base, "bin/poweroff", "s6-linux-init-poweroff") ; + auto_exec(base, "bin/shutdown", "s6-linux-init-shutdown") ; + auto_exec(base, "bin/telinit", "s6-linux-init-telinit") ; + auto_script(base, "bin/init", &stage1_script, 0) ; + auto_script(base, "bin/" STAGE4, &stage4_script, 0) ; } int main (int argc, char const *const *argv, char const *const *envp) { + stralloc saenv1 = STRALLOC_ZERO ; + stralloc saenv2 = STRALLOC_ZERO ; PROG = "s6-linux-init-maker" ; { subgetopt_t l = SUBGETOPT_ZERO ; for (;;) { - int opt = subgetopt_r(argc, argv, "c:l:b:u:g:UG:2:rZ:p:m:t:d:s:e:nq:", &l) ; + int opt = subgetopt_r(argc, argv, "c:b:u:g:UG:rLp:m:t:d:s:e:E:q:", &l) ; if (opt == -1) break ; switch (opt) { case 'c' : robase = l.arg ; break ; - case 'l' : slashrun = l.arg ; break ; case 'b' : bindir = l.arg ; break ; case 'u' : if (!uint0_scan(l.arg, &uncaught_logs_uid)) dieusage() ; break ; case 'g' : if (!uint0_scan(l.arg, &uncaught_logs_gid)) dieusage() ; break ; @@ -425,16 +417,15 @@ int main (int argc, char const *const *argv, char const *const *envp) if (!uint0_scan(x, &uncaught_logs_gid)) strerr_dieinvalid(100, "GID") ; } case 'G' : early_getty = l.arg ; break ; - case '2' : init_script = l.arg ; break ; case 'r' : redirect_stage2 = 1 ; break ; - case 'Z' : tini_script = l.arg ; break ; + case 'L' : logouthookd = 1 ; break ; case 'p' : initial_path = l.arg ; break ; case 'm' : if (!uint0_oscan(l.arg, &initial_umask)) dieusage() ; break ; case 't' : if (!uint0_scan(l.arg, ×tamp_style)) dieusage() ; break ; - case 'd' : if (!uint0_scan(l.arg, &slashdev_style)) dieusage() ; break ; + case 'd' : slashdev = l.arg ; break ; case 's' : env_store = l.arg ; break ; - case 'e' : if (!stralloc_catb(&satmp, l.arg, strlen(l.arg) + 1)) dienomem() ; break ; - case 'n' : in_namespace = 1 ; break ; + case 'e' : if (!stralloc_catb(&saenv1, l.arg, strlen(l.arg) + 1)) dienomem() ; break ; + case 'E' : if (!stralloc_catb(&saenv2, l.arg, strlen(l.arg) + 1)) dienomem() ; break ; case 'q' : if (!uint0_scan(l.arg, &finalsleep)) dieusage() ; break ; default : dieusage() ; } @@ -445,24 +436,27 @@ int main (int argc, char const *const *argv, char const *const *envp) if (robase[0] != '/') strerr_dief3x(100, "base directory ", robase, " is not absolute") ; - if (slashrun[0] != '/') - strerr_dief3x(100, "tmpfs directory ", slashrun, " is not absolute") ; if (bindir[0] != '/') strerr_dief3x(100, "initial location for binaries ", bindir, " is not absolute") ; if (init_script[0] != '/') strerr_dief3x(100, "stage 2 script location ", init_script, " is not absolute") ; if (tini_script[0] != '/') strerr_dief3x(100, "shutdown script location ", tini_script, " is not absolute") ; + if (slashdev && slashdev[0] != '/') + strerr_dief3x(100, "devtmpfs mounting location ", slashdev, " is not absolute") ; if (timestamp_style > 3) strerr_dief1x(100, "-t timestamp_style must be 0, 1, 2 or 3") ; - if (slashdev_style > 2) - strerr_dief1x(100, "-d dev_style must be 0, 1 or 2") ; + umask(0) ; if (mkdir(argv[0], 0755) < 0) strerr_diefu2sys(111, "mkdir ", argv[0]) ; - make_env(argv[0], satmp.s, satmp.len) ; - satmp.len = 0 ; - make_image(argv[0]) ; + make_env(argv[0], ENVSTAGE2, saenv2.s, saenv2.len) ; + stralloc_free(&saenv2) ; + make_env(argv[0], ENVSTAGE1, saenv1.s, saenv1.len) ; + saenv1.len = 0 ; + make_image(argv[0], &saenv1) ; + make_scripts(argv[0]) ; + make_bins(argv[0]) ; return 0 ; } diff --git a/src/init/s6-linux-init.c b/src/init/s6-linux-init.c new file mode 100644 index 0000000..6758682 --- /dev/null +++ b/src/init/s6-linux-init.c @@ -0,0 +1,168 @@ +/* ISC license. */ + +#include <fcntl.h> +#include <string.h> +#include <unistd.h> +#include <stdlib.h> +#include <errno.h> +#include <sys/stat.h> +#include <sys/mount.h> + +#include <skalibs/types.h> +#include <skalibs/allreadwrite.h> +#include <skalibs/sgetopt.h> +#include <skalibs/strerr2.h> +#include <skalibs/stralloc.h> +#include <skalibs/env.h> +#include <skalibs/djbunix.h> + +#include <s6/config.h> + +#include <s6-linux-init/config.h> + +#include "defaults.h" +#include "initctl.h" + +#define USAGE "s6-linux-init [ -r ] [ -c basedir ] [ -p initpath ] [ -s envdumpdir ] [ -m umask ] [ -d devtmpfs ]" +#define dieusage() strerr_dieusage(100, USAGE) + +#define BANNER "\n s6-linux-init version " S6_LINUX_INIT_VERSION "\n\n" + +static inline void run_stage2 (char const *basedir, char const **argv, unsigned int argc, char const *const *envp, size_t envlen, stralloc *envmodifs, int redirect) +{ + size_t dirlen = strlen(basedir) ; + char const *childargv[argc + 2] ; + char fn[dirlen + 1 + (sizeof(ENVSTAGE2) > sizeof(STAGE2) ? sizeof(ENVSTAGE2) : sizeof(STAGE2))] ; + PROG = "s6-linux-init (child)" ; + argv[0] = PROG ; + memcpy(fn, basedir, dirlen) ; + fn[dirlen] = '/' ; + memcpy(fn + dirlen + 1, ENVSTAGE2, sizeof(ENVSTAGE2)) ; + if (envdir(fn, envmodifs) == -1) + strerr_warnwu2sys("envdir ", fn) ; + memcpy(fn + dirlen + 1, STAGE2, sizeof(STAGE2)) ; + childargv[0] = fn ; + for (unsigned int i = 0 ; i < argc ; i++) + childargv[i+1] = argv[i] ; + childargv[argc + 1] = 0 ; + setsid() ; + fd_close(1) ; + if (open(LOGFIFO, O_WRONLY) != 1) /* blocks */ + strerr_diefu1sys(111, "open " LOGFIFO " for writing") ; + if (fd_copy(1 + redirect, 2 - redirect) == -1) + strerr_diefu1sys(111, "redirect output file descriptor") ; + xpathexec_r(childargv, envp, envlen, envmodifs->s, envmodifs->len) ; +} + +int main (int argc, char const **argv, char const *const *envp) +{ + mode_t mask = 0022 ; + char const *basedir = BASEDIR ; + char const *path = INITPATH ; + char const *slashdev = 0 ; + char const *envdumpdir = 0 ; + stralloc envmodifs = STRALLOC_ZERO ; + int redirect = 0 ; + PROG = "s6-linux-init" ; + + if (getpid() != 1) + { + argv[0] = S6_LINUX_INIT_BINPREFIX "/s6-linux-init-telinit" ; + pathexec_run(argv[0], argv, envp) ; + strerr_dieexec(111, argv[0]) ; + } + + { + subgetopt_t l = SUBGETOPT_ZERO ; + for (;;) + { + int opt = subgetopt_r(argc, argv, "rc:p:s:m:d:", &l) ; + if (opt == -1) break ; + switch (opt) + { + case 'r' : redirect = 1 ; break ; + case 'c' : basedir = l.arg ; break ; + case 'p' : path = l.arg ; break ; + case 's' : envdumpdir = l.arg ; break ; + case 'm' : if (!uint0_oscan(l.arg, &mask)) dieusage() ; break ; + case 'd' : slashdev = l.arg ; break ; + default : dieusage() ; + } + } + argc -= l.ind ; argv += l.ind ; + } + + allwrite(1, BANNER, sizeof(BANNER) - 1) ; + if (chdir("/") == -1) strerr_diefu1sys(111, "chdir to /") ; + umask(mask) ; + setpgid(0, 0) ; + fd_close(0) ; + if (slashdev) + { + fd_close(1) ; + fd_close(2) ; + if (mount("dev", slashdev, "devtmpfs", MS_NOSUID | MS_NOEXEC, "") == -1) + { + int e = errno ; + open("/dev/null", O_RDONLY) ; + open("/dev/console", O_WRONLY) ; + fd_copy(2, 1) ; + errno = e ; + strerr_diefu2sys(111, "mount ", slashdev) ; + } + if (open("/dev/console", O_WRONLY) + || fd_copy(1, 0) == -1 + || fd_move(2, 0) == -1) return 111 ; + } + if (open("/dev/null", O_RDONLY)) strerr_diefu1sys(111, "open /dev/null") ; + + if (umount(S6_LINUX_INIT_TMPFS) == -1) + { + if (errno != EINVAL) + strerr_warnwu1sys("umount " S6_LINUX_INIT_TMPFS) ; + } + if (mount("tmpfs", S6_LINUX_INIT_TMPFS, "tmpfs", MS_NODEV | MS_NOSUID, "mode=0755") == -1) + strerr_diefu1sys(111, "mount tmpfs on " S6_LINUX_INIT_TMPFS) ; + { + size_t dirlen = strlen(basedir) ; + char fn[dirlen + 1 + (sizeof(RUNIMAGE) > sizeof(ENVSTAGE1) ? sizeof(RUNIMAGE) : sizeof(ENVSTAGE1))] ; + memcpy(fn, basedir, dirlen) ; + fn[dirlen] = '/' ; + memcpy(fn + dirlen + 1, RUNIMAGE, sizeof(RUNIMAGE)) ; + if (!hiercopy(fn, S6_LINUX_INIT_TMPFS)) + strerr_diefu3sys(111, "copy ", fn, " to " S6_LINUX_INIT_TMPFS) ; + memcpy(fn + dirlen + 1, ENVSTAGE1, sizeof(ENVSTAGE1)) ; + if (envdir(fn, &envmodifs) == -1) + strerr_warnwu2sys("envdir ", fn) ; + } + if (envdumpdir && !env_dump(envdumpdir, 0700, envp)) + strerr_warnwu2sys("dump kernel environment to ", envdumpdir) ; + { + int fdr = open_read(LOGFIFO) ; + if (fdr == -1) strerr_diefu1sys(111, "open " LOGFIFO) ; + fd_close(1) ; + if (open(LOGFIFO, O_WRONLY) != 1) strerr_diefu1sys(111, "open " LOGFIFO) ; + fd_close(fdr) ; + } + { + static char const *const newargv[5] = { S6_EXTBINPREFIX "s6-svscan", "-st0", "--", S6_LINUX_INIT_TMPFS "/" SCANDIR, 0 } ; + char const *newenvp[2] = { 0, 0 } ; + pid_t pid ; + size_t pathlen = path ? strlen(path) : 0 ; + char pathvar[6 + pathlen] ; + if (path) + { + if (setenv("PATH", path, 1) == -1) + strerr_diefu1sys(111, "set initial PATH") ; + memcpy(pathvar, "PATH=", 5) ; + memcpy(pathvar + 5, path, pathlen + 1) ; + newenvp[0] = pathvar ; + } + pid = fork() ; + if (pid == -1) strerr_diefu1sys(111, "fork") ; + if (!pid) run_stage2(basedir, argv, argc, newenvp, !!path, &envmodifs, redirect) ; + if (fd_copy(2, 1) == -1) + strerr_diefu1sys(111, "redirect output file descriptor") ; + xpathexec_r(newargv, newenvp, !!path, envmodifs.s, envmodifs.len) ; + } +} diff --git a/src/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..215365d --- /dev/null +++ b/src/lib/s6_linux_init_logouthook.c @@ -0,0 +1,18 @@ +/* ISC license. */ + +#include <skalibs/djbunix.h> +#include <skalibs/webipc.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/logout-hook/deps-exe/s6-linux-init-logouthookd b/src/logout-hook/deps-exe/s6-linux-init-logouthookd new file mode 100644 index 0000000..5a7d2fd --- /dev/null +++ b/src/logout-hook/deps-exe/s6-linux-init-logouthookd @@ -0,0 +1,4 @@ +${LIBUTMPS} +-lskarnet +${TAINNOW_LIB} +${SOCKET_LIB} diff --git a/src/logout-hook/s6-linux-init-logouthookd.c b/src/logout-hook/s6-linux-init-logouthookd.c new file mode 100644 index 0000000..cc542ba --- /dev/null +++ b/src/logout-hook/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/state/deps-exe/s6-linux-init-halt b/src/state/deps-exe/s6-linux-init-halt new file mode 100644 index 0000000..0c4376c --- /dev/null +++ b/src/state/deps-exe/s6-linux-init-halt @@ -0,0 +1,5 @@ +libhpr.a.xyzzy +${LIBUTMPS} +-lskarnet +${TAINNOW_LIB} +${SOCKET_LIB} diff --git a/src/state/deps-exe/s6-linux-init-poweroff b/src/state/deps-exe/s6-linux-init-poweroff new file mode 100644 index 0000000..0c4376c --- /dev/null +++ b/src/state/deps-exe/s6-linux-init-poweroff @@ -0,0 +1,5 @@ +libhpr.a.xyzzy +${LIBUTMPS} +-lskarnet +${TAINNOW_LIB} +${SOCKET_LIB} diff --git a/src/state/deps-exe/s6-linux-init-reboot b/src/state/deps-exe/s6-linux-init-reboot new file mode 100644 index 0000000..0c4376c --- /dev/null +++ b/src/state/deps-exe/s6-linux-init-reboot @@ -0,0 +1,5 @@ +libhpr.a.xyzzy +${LIBUTMPS} +-lskarnet +${TAINNOW_LIB} +${SOCKET_LIB} diff --git a/src/state/deps-exe/s6-linux-init-shutdown b/src/state/deps-exe/s6-linux-init-shutdown new file mode 100644 index 0000000..0c4376c --- /dev/null +++ b/src/state/deps-exe/s6-linux-init-shutdown @@ -0,0 +1,5 @@ +libhpr.a.xyzzy +${LIBUTMPS} +-lskarnet +${TAINNOW_LIB} +${SOCKET_LIB} diff --git a/src/state/deps-exe/s6-linux-init-shutdownd b/src/state/deps-exe/s6-linux-init-shutdownd new file mode 100644 index 0000000..5a7d2fd --- /dev/null +++ b/src/state/deps-exe/s6-linux-init-shutdownd @@ -0,0 +1,4 @@ +${LIBUTMPS} +-lskarnet +${TAINNOW_LIB} +${SOCKET_LIB} diff --git a/src/init/deps-exe/s6-poweroff b/src/state/deps-exe/s6-linux-init-single index e7187fe..e7187fe 100644 --- a/src/init/deps-exe/s6-poweroff +++ b/src/state/deps-exe/s6-linux-init-single diff --git a/src/state/deps-exe/s6-linux-init-telinit b/src/state/deps-exe/s6-linux-init-telinit new file mode 100644 index 0000000..aeb24a6 --- /dev/null +++ b/src/state/deps-exe/s6-linux-init-telinit @@ -0,0 +1,2 @@ +libhpr.a.xyzzy +-lskarnet diff --git a/src/state/deps-lib/hpr b/src/state/deps-lib/hpr new file mode 100644 index 0000000..066efd2 --- /dev/null +++ b/src/state/deps-lib/hpr @@ -0,0 +1,4 @@ +hpr_fmttime.o +hpr_shutdown.o +hpr_wall.o +hpr_wall_seconds.o diff --git a/src/state/hpr.c b/src/state/hpr.c new file mode 100644 index 0000000..401cc97 --- /dev/null +++ b/src/state/hpr.c @@ -0,0 +1,102 @@ +/* ISC license. */ + +#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 PROGNAME " [ -h | -p | -r ] [ -d | -w ] [ -W ] [ -f ]" + +int main (int argc, char const *const *argv) +{ + int what = WHATDEFAULT ; + int force = 0 ; + int dowtmp = 1 ; + int dowall = 1 ; + PROG = PROGNAME ; + + { + subgetopt_t l = SUBGETOPT_ZERO ; + for (;;) + { + int opt = subgetopt_r(argc, argv, "hprfdwW", &l) ; + if (opt == -1) break ; + switch (opt) + { + case 'h' : what = 1 ; break ; + case 'p' : what = 2 ; break ; + case 'r' : what = 3 ; break ; + case 'f' : force = 1 ; break ; + case 'd' : dowtmp = 0 ; break ; + case 'w' : dowtmp = 2 ; break ; + case 'W' : dowall = 0 ; break ; + default : strerr_dieusage(100, USAGE) ; + } + } + argc -= l.ind ; argv += l.ind ; + } + + if (geteuid()) + { + errno = EPERM ; + strerr_dief1sys(100, "nice try, peon") ; + } + + if (force) + { + reboot(what == 3 ? RB_AUTOBOOT : what == 2 ? RB_POWER_OFF : RB_HALT_SYSTEM) ; + strerr_diefu1sys(111, "reboot()") ; + } + + if (!tain_now_g()) strerr_warnw1sys("get current time") ; + if (dowtmp) + { + struct utmpx utx = + { + .ut_type = RUN_LVL, + .ut_pid = getpid(), + .ut_line = "~", + .ut_id = "", + .ut_session = getsid(0) + } ; + strncpy(utx.ut_user, what == 3 ? "reboot" : "shutdown", UT_NAMESIZE) ; + if (gethostname(utx.ut_host, UT_HOSTSIZE) < 0) + { + utx.ut_host[0] = 0 ; + strerr_warnwu1sys("gethostname") ; + } + else utx.ut_host[UT_HOSTSIZE - 1] = 0 ; + if (!timeval_from_tain(&utx.ut_tv, &STAMP)) + strerr_warnwu1sys("timeval_from_tain") ; + updwtmpx(_PATH_WTMP, &utx) ; + } + if (dowall) hpr_wall_seconds(0) ; + if (dowtmp < 2) + { + if (!hpr_shutdown(what, &STAMP, 0)) + strerr_diefu1sys(111, "notify s6-linux-init-shutdownd") ; + } + return 0 ; +} diff --git a/src/state/hpr.h b/src/state/hpr.h new file mode 100644 index 0000000..9deb6c4 --- /dev/null +++ b/src/state/hpr.h @@ -0,0 +1,22 @@ +/* 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 in " + +#define hpr_send(s, n) openwritenclose_unsafe(INITCTL, (s), n) +#define hpr_cancel() hpr_send("c", 1) +extern int hpr_shutdown (unsigned int, tain_t const *, unsigned int) ; +extern size_t hpr_fmttime (char *, size_t, unsigned int) ; +extern void hpr_wall (char const *) ; +extern void hpr_wall_seconds (unsigned int) ; + +#endif diff --git a/src/state/hpr_fmttime.c b/src/state/hpr_fmttime.c new file mode 100644 index 0000000..8038cfe --- /dev/null +++ b/src/state/hpr_fmttime.c @@ -0,0 +1,52 @@ +/* ISC license. */ + +#include <string.h> + +#include <skalibs/types.h> + +#include "hpr.h" + +size_t hpr_fmttime (char *buf, size_t max, unsigned int seconds) +{ + size_t m = 0 ; + unsigned int minutes = 0 ; + unsigned int hours = 0 ; + if (seconds >= 60) + { + minutes = seconds / 60 ; + seconds %= 60 ; + if (minutes >= 60) + { + hours = minutes / 60 ; + minutes %= 60 ; + } + } + m = sizeof(HPR_WALL_BANNER) - 1 ; + if (m > max) return 0 ; + memcpy(buf, HPR_WALL_BANNER, m) ; + if (hours) + { + if (m + UINT_FMT + 6 > max) return 0 ; + m += uint_fmt(buf + m, hours) ; + memcpy(buf + m, " hour", 5) ; + m += 5 ; + if (hours > 1) buf[m++] = 's' ; + buf[m++] = ' ' ; + } + if (minutes) + { + if (m + UINT_FMT + 8 > max) return 0 ; + m += uint_fmt(buf + m, minutes) ; + memcpy(buf + m, " minute", 7) ; + m += 7 ; + if (minutes > 1) buf[m++] = 's' ; + buf[m++] = ' ' ; + } + if (m + UINT_FMT + 8 > max) return 0 ; + m += uint_fmt(buf + m, seconds) ; + memcpy(buf + m, " second", 7) ; + m += 7 ; + if (seconds != 1) buf[m++] = 's' ; + buf[m++] = ' ' ; + return m ; +} diff --git a/src/state/hpr_shutdown.c b/src/state/hpr_shutdown.c new file mode 100644 index 0000000..9c9ff51 --- /dev/null +++ b/src/state/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] = { " hpr"[what] } ; + tain_pack(pack+1, when) ; + uint32_pack_big(pack + 1 + TAIN_PACK, (uint32_t)grace) ; + return hpr_send(pack, 5 + TAIN_PACK) ; +} diff --git a/src/state/hpr_wall.c b/src/state/hpr_wall.c new file mode 100644 index 0000000..cc6da5e --- /dev/null +++ b/src/state/hpr_wall.c @@ -0,0 +1,47 @@ +/* ISC license. */ + +#include <string.h> +#include <errno.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, idlen ; + struct utmpx *utx ; + int fd ; + errno = 0 ; + utx = getutxent() ; + if (!utx) break ; + if (utx->ut_type != USER_PROCESS) continue ; + linelen = strnlen(utx->ut_line, UT_LINESIZE) ; + idlen = strnlen(utx->ut_id, 4) ; + memcpy(tty + 5, utx->ut_line, linelen) ; + memcpy(tty + 5 + linelen, utx->ut_id, idlen) ; + tty[5 + linelen + idlen] = 0 ; + fd = open_append(tty) ; + if (fd == -1) continue ; + allwrite(fd, msg, n) ; + fd_close(fd) ; + } + if (errno) strerr_warnwu1sys("getutxent") ; + endutxent() ; +} diff --git a/src/state/hpr_wall_seconds.c b/src/state/hpr_wall_seconds.c new file mode 100644 index 0000000..6c0363b --- /dev/null +++ b/src/state/hpr_wall_seconds.c @@ -0,0 +1,12 @@ +/* ISC license. */ + +#include "hpr.h" + +void hpr_wall_seconds (unsigned int secs) +{ + char buf[300] = HPR_WALL_BANNER ; + size_t n = sizeof(HPR_WALL_BANNER) - 1 ; + n += hpr_fmttime(buf + n, 300 - n, secs) ; + buf[n++] = 0 ; + hpr_wall(buf) ; +} diff --git a/src/init/s6-halt.c b/src/state/s6-linux-init-halt.c index d758030..4ccca1d 100644 --- a/src/init/s6-halt.c +++ b/src/state/s6-linux-init-halt.c @@ -1,7 +1,7 @@ /* ISC license. */ #undef PROGNAME -#define PROGNAME "s6-halt" +#define PROGNAME "s6-linux-init-halt" #undef WHATDEFAULT #define WHATDEFAULT 1 #include "hpr.c" diff --git a/src/init/s6-poweroff.c b/src/state/s6-linux-init-poweroff.c index f9f7747..d25dbb6 100644 --- a/src/init/s6-poweroff.c +++ b/src/state/s6-linux-init-poweroff.c @@ -1,7 +1,7 @@ /* ISC license. */ #undef PROGNAME -#define PROGNAME "s6-poweroff" +#define PROGNAME "s6-linux-init-poweroff" #undef WHATDEFAULT #define WHATDEFAULT 2 #include "hpr.c" diff --git a/src/init/s6-reboot.c b/src/state/s6-linux-init-reboot.c index 0bf8785..3612684 100644 --- a/src/init/s6-reboot.c +++ b/src/state/s6-linux-init-reboot.c @@ -1,7 +1,7 @@ /* ISC license. */ #undef PROGNAME -#define PROGNAME "s6-reboot" +#define PROGNAME "s6-linux-init-reboot" #undef WHATDEFAULT #define WHATDEFAULT 3 #include "hpr.c" diff --git a/src/state/s6-linux-init-shutdown.c b/src/state/s6-linux-init-shutdown.c new file mode 100644 index 0000000..eb65d1d --- /dev/null +++ b/src/state/s6-linux-init-shutdown.c @@ -0,0 +1,269 @@ +/* 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 <sys/reboot.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 tmnow, tmthen ; + unsigned int hour, minute ; + size_t len = uint_scan(s, &hour) ; + if (!len || len > 2 || s[len] != ':' || hour > 23) + strerr_dief1x(100, "invalid time format") ; + s += len+1 ; + len = uint0_scan(s, &minute) ; + if (!len || len != 2 || minute > 59) + strerr_dief1x(100, "invalid time format") ; + if (!localtm_from_tai(&tmnow, tain_secp(&STAMP), 1)) + strerr_diefu1sys(111, "break down current time into struct tm") ; + tmthen = tmnow ; + tmthen.tm_hour = hour ; + tmthen.tm_min = minute ; + if (!tai_from_localtm(&taithen, &tmthen)) + strerr_diefu1sys(111, "assemble broken-down time into tain_t") ; + if (tai_less(&taithen, tain_secp(&STAMP))) + { + add_one_day(&tmthen) ; + if (!tai_from_localtm(&taithen, &tmthen)) + strerr_diefu1sys(111, "assemble broken-down time into tain_t") ; + } + tai_sub(tain_secp(when), &taithen, tain_secp(&STAMP)) ; + when->nano = 0 ; +} + +static inline void parse_time (tain_t *when, char const *s) +{ + if (!strcmp(s, "now")) tain_copynow(when) ; + else if (s[0] == '+') + { + unsigned int mins ; + if (!uint0_scan(s+1, &mins)) dieusage() ; + tain_addsec_g(when, mins * 60) ; + } + else if (strchr(s, ':')) parse_hourmin(when, s) ; + else + { + unsigned int mins ; + if (!uint0_scan(s, &mins)) dieusage() ; + tain_addsec_g(when, mins * 60) ; + } +} + + + /* shutdown -a: access control */ + +static inline unsigned char cclass (unsigned char c) +{ + switch (c) + { + case 0 : return 0 ; + case '\n' : return 1 ; + case '#' : return 2 ; + default : return 3 ; + } +} + +static inline unsigned int parse_authorized_users (char *buf, char const **users, unsigned int max) +{ + static unsigned char const table[3][4] = + { + { 0x03, 0x00, 0x01, 0x12 }, + { 0x03, 0x00, 0x01, 0x01 }, + { 0x23, 0x20, 0x02, 0x02 } + } ; + size_t pos = 0 ; + size_t mark = 0 ; + unsigned int n = 0 ; + unsigned int state = 0 ; + for (; state < 3 ; pos++) + { + unsigned char what = table[state][cclass(buf[pos])] ; + state = what & 3 ; + if (what & 0x10) mark = pos ; + if (what & 0x20) + { + if (n >= max) + { + char fmt[UINT32_MAX] ; + fmt[uint32_fmt(fmt, AC_MAX)] = 0 ; + strerr_warnw4x(AC_FILE, " lists more than ", fmt, " authorized users - ignoring the extra ones") ; + break ; + } + buf[pos] = 0 ; + users[n++] = buf + mark ; + } + } + return n ; +} + +static inline int match_users_with_utmp (char const *const *users, unsigned int n) +{ + setutxent() ; + for (;;) + { + struct utmpx *utx ; + errno = 0 ; + utx = getutxent() ; + if (!utx) break ; + if (utx->ut_type != USER_PROCESS) continue ; + for (unsigned int i = 0 ; i < n ; i++) + if (!strncmp(utx->ut_user, users[i], UT_NAMESIZE)) goto yes ; + } + if (errno) strerr_warnwu1sys("getutxent") ; + endutxent() ; + return 0 ; + + yes: + endutxent() ; + return 1 ; +} + +static inline void access_control (void) +{ + char buf[AC_BUFSIZE] ; + char const *users[AC_MAX] ; + unsigned int n ; + struct stat st ; + int fd = open_readb(AC_FILE) ; + if (fd == -1) + { + if (errno == ENOENT) return ; + strerr_diefu2sys(111, "open ", AC_FILE) ; + } + if (fstat(fd, &st) == -1) + strerr_diefu2sys(111, "stat ", AC_FILE) ; + if (st.st_size >= AC_BUFSIZE) + { + char fmt[UINT32_FMT] ; + fmt[uint32_fmt(fmt, AC_BUFSIZE - 1)] = 0 ; + strerr_dief4x(1, AC_FILE, " is too big: it needs to be ", fmt, " bytes or less") ; + } + if (allread(fd, buf, st.st_size) < st.st_size) + strerr_diefu2sys(111, "read ", AC_FILE) ; + fd_close(fd) ; + buf[st.st_size] = 0 ; + n = parse_authorized_users(buf, users, AC_MAX) ; + if (!n || !match_users_with_utmp(users, n)) + { + fd = open_append("/dev/console") ; + if (fd == -1) + strerr_diefu1sys(111, "open /dev/console") ; + if (allwrite(fd, AC_MESSAGE, sizeof(AC_MESSAGE) - 1) < sizeof(AC_MESSAGE) - 1) + strerr_diefu1sys(111, "write to /dev/console") ; + strerr_dief1x(1, AC_SHORT_MESSAGE) ; + } +} + + + /* main */ + +int main (int argc, char const *const *argv) +{ + unsigned int gracetime = 0 ; + int what = 0 ; + int doactl = 0 ; + int docancel = 0 ; + tain_t when ; + PROG = "shutdown" ; + + { + subgetopt_t l = SUBGETOPT_ZERO ; + for (;;) + { + int opt = subgetopt_r(argc, argv, "hprkafFct:", &l) ; + if (opt == -1) break ; + switch (opt) + { + case 'h' : what = 1 ; break ; + case 'p' : what = 2 ; break ; + case 'r' : what = 3 ; break ; + case 'k' : what = 4 ; break ; + case 'a' : doactl = 1 ; break ; + case 'f' : /* talk to the hand */ break ; + case 'F' : /* no, the other hand */ break ; + case 'c' : docancel = 1 ; break ; + case 't' : if (!uint0_scan(l.arg, &gracetime)) dieusage() ; break ; + default : strerr_dieusage(100, USAGE) ; + } + } + argc -= l.ind ; argv += l.ind ; + } + + if (geteuid()) + { + errno = EPERM ; + strerr_diefu1sys(111, "shutdown") ; + } + if (doactl) access_control() ; + if (!tain_now_g()) strerr_warnw1sys("get current time") ; + if (docancel) + { + if (argv[0]) hpr_wall(argv[0]) ; + if (!hpr_cancel()) goto err ; + return 0 ; + } + if (!argc) dieusage() ; + parse_time(&when, argv[0]) ; + if (argv[1]) hpr_wall(argv[1]) ; + if (what < 4) + { + if (gracetime > 300) + { + gracetime = 300 ; + strerr_warnw1x("delay between SIGTERM and SIGKILL is capped to 300 seconds") ; + } + if (!hpr_shutdown(what, &when, gracetime * 1000)) goto err ; + } + return 0 ; + + err: + strerr_diefu2sys(111, "write to ", INITCTL) ; +} diff --git a/src/state/s6-linux-init-shutdownd.c b/src/state/s6-linux-init-shutdownd.c new file mode 100644 index 0000000..d4ecd4b --- /dev/null +++ b/src/state/s6-linux-init-shutdownd.c @@ -0,0 +1,278 @@ +/* ISC license. */ + +#include <sys/types.h> +#include <sys/stat.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/stralloc.h> +#include <skalibs/sig.h> +#include <skalibs/tai.h> +#include <skalibs/djbunix.h> +#include <skalibs/iopause.h> +#include <skalibs/skamisc.h> + +#include "defaults.h" +#include "initctl.h" +#include "hpr.h" + +#define STAGE4_FILE "stage 4" + +#define USAGE "s6-linux-init-shutdownd [ -b bindir ] [ -c basedir ] [ -g gracetime ]" +#define dieusage() strerr_dieusage(100, USAGE) + +static char const *basedir = BASEDIR ; + +static inline void prepare_shutdown (char c, unsigned int *what, tain_t *deadline, unsigned int *grace_time) +{ + uint32_t u ; + char pack[TAIN_PACK] ; + ssize_t r = sanitize_read(buffer_get(buffer_0small, pack, TAIN_PACK)) ; + if (r == -1) strerr_diefu1sys(111, "read from pipe") ; + if (r < TAIN_PACK + 4) strerr_dief1x(101, "bad shutdown protocol") ; + tain_unpack(pack, deadline) ; + uint32_unpack_big(pack + TAIN_PACK, &u) ; + if (u && u <= 300000) *grace_time = u ; + *what = byte_chr(" hpr", c, 4) ; +} + +static inline void change_runlevels (char c) +{ + pid_t pid = doublefork() ; + if (pid == -1) + strerr_warnwu1sys("doublefork for runlevel change") ; + if (pid) + { + size_t basedirlen = strlen(basedir) ; + char fn[basedirlen + sizeof("/scripts/runlevel")] ; + char s[2] = { c, 0 } ; + char const *cargv[3] = { fn, s, 0 } ; + PROG = "s6-linux-init-shutdownd (grandchild)" ; + memcpy(fn, basedir, basedirlen) ; + memcpy(fn + basedirlen, "/scripts/runlevel", sizeof("/scripts/runlevel")) ; + xpathexec_run(cargv[0], cargv, (char const *const *)environ) ; + } +} + +static inline void handle_stdin (unsigned int *what, tain_t *deadline, unsigned int *grace_time) +{ + char rl = 0 ; + for (;;) + { + char c ; + ssize_t r = sanitize_read(buffer_get(buffer_0small, &c, 1)) ; + if (r == -1) strerr_diefu1sys(111, "read from pipe") ; + else if (!r) break ; + switch (c) + { + case 'h' : + case 'p' : + case 'r' : + prepare_shutdown(c, what, deadline, grace_time) ; + break ; + case 'c' : + *what = 0 ; + rl = 0 ; + tain_add_g(deadline, &tain_infinite_relative) ; + break ; + case 'S' : + *what = 4 ; + tain_copynow(deadline) ; + break ; + case '0' : + case '1' : + case '2' : + case '3' : + case '4' : + case '5' : + case '6' : + *what = 0 ; + tain_add_g(deadline, &tain_infinite_relative) ; + rl = c ; + break ; + default : + { + char s[2] = { c, 0 } ; + strerr_warnw2x("unknown command: ", s) ; + } + break ; + } + } + if (rl) change_runlevels(rl) ; +} + +static inline void prepare_stage4 (char const *bindir, char const *basedir, unsigned int what) +{ + static char const *table[4] = { "single", "halt", "poweroff", "reboot" } ; + stralloc sa = STRALLOC_ZERO ; + buffer b ; + char buf[512] ; + int fd ; + unlink_void(STAGE4_FILE ".new") ; + fd = open_excl(STAGE4_FILE ".new") ; + if (fd == -1) strerr_diefu3sys(111, "open ", STAGE4_FILE ".new", " for writing") ; + buffer_init(&b, &buffer_write, fd, buf, 512) ; + + if (buffer_puts(&b, "#!") == -1 + || buffer_puts(&b, bindir) == -1 + || buffer_puts(&b, "/execlineb -P\n\n") == -1) + strerr_diefu2sys(111, "write to ", STAGE4_FILE ".new") ; + if (!string_quote(&sa, basedir, strlen(basedir))) + strerr_diefu1sys(111, "string_quote") ; + if (buffer_put(&b, sa.s, sa.len) == -1 + || buffer_puts(&b, "/bin/" STAGE4 " ") == -1 + || buffer_puts(&b, table[what]) == -1 + || buffer_putsflush(&b, "\n") == -1) + strerr_diefu2sys(111, "write to ", STAGE4_FILE ".new") ; + stralloc_free(&sa) ; + if (fchmod(fd, S_IRWXU) == -1) + strerr_diefu2sys(111, "fchmod ", STAGE4_FILE ".new") ; + if (fsync(fd) == -1) + strerr_diefu2sys(111, "fsync ", STAGE4_FILE ".new") ; + fd_close(fd) ; + if (rename(STAGE4_FILE ".new", STAGE4_FILE) == -1) + strerr_diefu4sys(111, "rename ", STAGE4_FILE ".new", " to ", STAGE4_FILE) ; +} + +int main (int argc, char const *const *argv) +{ + char const *bindir = BINDIR ; + pid_t pid ; + tain_t deadline ; + unsigned int what = 0 ; + unsigned int grace_time = 3000 ; + int notif = 0 ; + PROG = "s6-linux-init-shutdownd" ; + + { + subgetopt_t l = SUBGETOPT_ZERO ; + for (;;) + { + int opt = subgetopt_r(argc, argv, "b:c:g:", &l) ; + if (opt == -1) break ; + switch (opt) + { + case 'b' : bindir = l.arg ; break ; + case 'c' : basedir = l.arg ; break ; + case 'g' : if (!uint0_scan(l.arg, &grace_time)) dieusage() ; break ; + default : dieusage() ; + } + } + argc -= l.ind ; argv += l.ind ; + } + if (bindir[0] != '/') + strerr_dief2x(100, "bindir", " must be an absolute path") ; + if (basedir[0] != '/') + strerr_dief2x(100, "basedir", " must be an absolute path") ; + if (grace_time > 300000) grace_time = 300000 ; + + /* if we're in stage 4, exec it immediately */ + { + char const *stage4_argv[2] = { STAGE4_FILE, 0 } ; + pathexec_run(stage4_argv[0], stage4_argv, (char const *const *)environ) ; + if (errno != ENOENT) + strerr_warnwu2sys("exec ", stage4_argv[0]) ; + } + + fd_close(1) ; + fd_close(0) ; + + if (open_read(SHUTDOWND_FIFO)) + strerr_diefu3sys(111, "open ", SHUTDOWND_FIFO, " for reading") ; + if (open_write(SHUTDOWND_FIFO) != 1) + strerr_diefu3sys(111, "open ", SHUTDOWND_FIFO, " for writing") ; + if (sig_ignore(SIGPIPE) == -1) + strerr_diefu1sys(111, "sig_ignore SIGPIPE") ; + tain_now_g() ; + tain_add_g(&deadline, &tain_infinite_relative) ; + + for (;;) + { + iopause_fd x = { .fd = 0, .events = IOPAUSE_READ } ; + int r = iopause_g(&x, 1, &deadline) ; + if (r == -1) strerr_diefu1sys(111, "iopause") ; + if (!r) + { + if (what) break ; + tain_add_g(&deadline, &tain_infinite_relative) ; + continue ; + } + if (x.revents & IOPAUSE_READ) + handle_stdin(&what, &deadline, &grace_time) ; + } + + if (what == 4) what = 0 ; + + /* Stage 3 */ + + fd_close(1) ; + fd_close(0) ; + if (open_readb("/dev/null")) + strerr_diefu1sys(111, "open /dev/null for reading") ; + if (open_write("/dev/console") != 1 || ndelay_off(1) == -1) + strerr_diefu1sys(111, "open /dev/console for writing") ; + { + size_t basedirlen = strlen(basedir) ; + char stage3[basedirlen + sizeof("/" STAGE3)] ; + char const *stage3_argv[2] = { stage3, 0 } ; + memcpy(stage3, basedir, basedirlen) ; + memcpy(stage3 + basedirlen, "/" STAGE3, sizeof("/" STAGE3)) ; + sig_block(SIGCHLD) ; + pid = child_spawn0(stage3_argv[0], stage3_argv, (char const *const *)environ) ; + if (!pid) strerr_warnwu2sys("spawn ", stage3) ; + sig_unblock(SIGCHLD) ; + if (pid) + { + int wstat ; + if (wait_pid(pid, &wstat) == -1) strerr_diefu1sys(111, "waitpid") ; + if (WIFSIGNALED(wstat)) + { + char fmt[UINT_FMT] ; + fmt[uint_fmt(fmt, WTERMSIG(wstat))] = 0 ; + strerr_warnw3x(stage3, " was killed by signal ", fmt) ; + } + else if (WEXITSTATUS(wstat)) + { + char fmt[UINT_FMT] ; + fmt[uint_fmt(fmt, WTERMSIG(wstat))] = 0 ; + strerr_warnw3x(stage3, " was killed by signal ", fmt) ; + } + else if (WEXITSTATUS(wstat)) + { + char fmt[UINT_FMT] ; + fmt[uint_fmt(fmt, WEXITSTATUS(wstat))] = 0 ; + strerr_warnw3x(stage3, " exited ", fmt) ; + } + } + } + + + /* The end is coming! */ + + prepare_stage4(bindir, basedir, what) ; + if (fd_move(2, 1) == -1) strerr_warnwu1sys("fd_move") ; + sync() ; + if (sig_ignore(SIGTERM) == -1) strerr_warnwu1sys("sig_ignore SIGTERM") ; + strerr_warni1x("sending all processes the TERM signal...") ; + kill(-1, SIGTERM) ; + kill(-1, SIGCONT) ; + tain_from_millisecs(&deadline, grace_time) ; + tain_add_g(&deadline, &deadline) ; + deepsleepuntil_g(&deadline) ; + sync() ; + strerr_warni1x("sending all processes the KILL signal...") ; + kill(-1, SIGKILL) ; + return 0 ; +} diff --git a/src/state/s6-linux-init-single.c b/src/state/s6-linux-init-single.c new file mode 100644 index 0000000..4f23340 --- /dev/null +++ b/src/state/s6-linux-init-single.c @@ -0,0 +1,54 @@ +/* ISC license. */ + +#include <errno.h> +#include <fcntl.h> +#include <unistd.h> + +#include <skalibs/allreadwrite.h> +#include <skalibs/strerr2.h> +#include <skalibs/djbunix.h> + +#include "initctl.h" + +#define USAGE "s6-linux-init-single" +#define dieusage() strerr_dieusage(100, USAGE) + +#define BANNER "Executing interactive root shell.\n" + +int main (void) +{ + int fd ; + PROG = "s6-linux-init-single" ; + fd = open(EARLYGETTY, O_RDONLY) ; + if (fd == -1) + { + if (errno != ENOENT) + strerr_diefu2sys(111, "opendir ", EARLYGETTY) ; + goto shell ; + } + if (faccessat(fd, "down", F_OK, 0) == 0) + { + fd_close(fd) ; + goto shell ; + } + if (errno != ENOENT) + strerr_diefu3sys(111, "access ", EARLYGETTY, "/down") ; + return 0 ; + + shell: + + fd = open("/dev/console", O_RDONLY) ; + if (fd == -1) strerr_diefu2sys(111, "open /dev/console for ", "reading") ; + if (fd_move(0, fd) == -1) strerr_diefu1sys(111, "move /dev/console fd to 0") ; + fd = open("/dev/console", O_WRONLY) ; + if (fd == -1) strerr_diefu2sys(111, "open /dev/console for ", "writing") ; + if (fd_move(1, fd) == -1) strerr_diefu1sys(111, "move /dev/console fd to 1") ; + if (fd_copy(2, 1) == -1) strerr_diefu1sys(111, "dup2 1 to 2") ; + if (allwrite(1, BANNER, sizeof(BANNER) - 1) < sizeof(BANNER) - 1) + strerr_diefu1sys(111, "write banner") ; + { + char *argv[3] = { "/bin/sh", "-i", 0 } ; + execv(argv[0], argv) ; + strerr_dieexec(111, argv[0]) ; + } +} diff --git a/src/state/s6-linux-init-telinit.c b/src/state/s6-linux-init-telinit.c new file mode 100644 index 0000000..856e78b --- /dev/null +++ b/src/state/s6-linux-init-telinit.c @@ -0,0 +1,42 @@ +/* ISC license. */ + +#include <skalibs/sgetopt.h> +#include <skalibs/types.h> +#include <skalibs/strerr2.h> + +#include "defaults.h" +#include "initctl.h" +#include "hpr.h" + +#define USAGE "s6-linux-init-telinit runlevel" +#define dieusage() strerr_dieusage(100, USAGE) + +int main (int argc, char const *const *argv, char const *const *envp) +{ + PROG = "s6-linux-init-telinit" ; + { + subgetopt_t l = SUBGETOPT_ZERO ; + for (;;) + { + int opt = subgetopt_r(argc, argv, "rc:p:s:m:d:", &l) ; + if (opt == -1) break ; + switch (opt) + { + case 'r' : + case 'c' : + case 'p' : + case 's' : + case 'm' : + case 'd' : break ; + default : dieusage() ; + } + } + argc -= l.ind ; argv += l.ind ; + } + + if (!argc) dieusage() ; + if (!argv[0][0] || argv[0][1]) dieusage() ; + if (argv[0][0] != 'S' && (argv[0][0] < '0' || argv[0][0] > '6')) dieusage() ; + if (!hpr_send(argv[0], 1)) strerr_diefu2sys(111, "write to ", INITCTL) ; + return 0 ; +} diff --git a/tools/gen-deps.sh b/tools/gen-deps.sh index 6383ac2..c6d6971 100755 --- a/tools/gen-deps.sh +++ b/tools/gen-deps.sh @@ -7,6 +7,8 @@ echo '# This file has been generated by tools/gen-deps.sh' echo '#' echo +internal_libs= + for dir in src/include/${package} src/* ; do for file in $(ls -1 $dir | grep -- \\.h$) ; do { @@ -70,8 +72,12 @@ for dir in $(ls -1 src | grep -v ^include) ; do echo else echo "lib${file}.a.xyzzy:$(echo "$deps" | sed 's/\.o/.lo/g')" echo endif - echo "lib${file}.so.xyzzy: EXTRA_LIBS :=$libs" - echo "lib${file}.so.xyzzy:$(echo "$deps" | sed 's/\.o/.lo/g')" + if grep -F "LIB_DEFS :=" package/targets.mak | grep -qF "$file" ; then + echo "lib${file}.so.xyzzy: EXTRA_LIBS :=$libs" + echo "lib${file}.so.xyzzy:$(echo "$deps" | sed 's/\.o/.lo/g')" + else + internal_libs="$internal_libs lib${file}.a.xyzzy" + fi done for file in $(ls -1 src/$dir/deps-exe) ; do @@ -91,3 +97,4 @@ for dir in $(ls -1 src | grep -v ^include) ; do echo "$file: src/$dir/$file.o$deps" done done +echo "INTERNAL_LIBS :=$internal_libs" |