summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/init/s6-linux-init-maker.c36
-rw-r--r--src/init/s6-linux-init-telinit.c12
-rw-r--r--src/lib/s6_linux_init_logouthook.c1
-rw-r--r--src/shutdown/deps-exe/s6-linux-init-hpr (renamed from src/shutdown/deps-exe/s6-linux-init-halt)0
-rw-r--r--src/shutdown/deps-exe/s6-linux-init-poweroff5
-rw-r--r--src/shutdown/deps-exe/s6-linux-init-reboot5
-rw-r--r--src/shutdown/hpr_shutdown.c2
-rw-r--r--src/shutdown/s6-linux-init-halt.c7
-rw-r--r--src/shutdown/s6-linux-init-hpr.c (renamed from src/shutdown/hpr.c)9
-rw-r--r--src/shutdown/s6-linux-init-poweroff.c7
-rw-r--r--src/shutdown/s6-linux-init-reboot.c7
-rw-r--r--src/shutdown/s6-linux-init-shutdownd.c113
12 files changed, 95 insertions, 109 deletions
diff --git a/src/init/s6-linux-init-maker.c b/src/init/s6-linux-init-maker.c
index 82ab14f..f75371f 100644
--- a/src/init/s6-linux-init-maker.c
+++ b/src/init/s6-linux-init-maker.c
@@ -85,6 +85,14 @@ static int linewithargs_script (buffer *b, char const *line)
&& buffer_puts(b, " $@\n") >= 0 ;
}
+static int hpr_script (buffer *b, char const *what)
+{
+ return put_shebang_options(b, "-S0")
+ && buffer_puts(b, S6_LINUX_INIT_BINPREFIX "s6-linux-init-hpr -") >= 0
+ && buffer_puts(b, what) >= 0
+ && buffer_put(b, " $@\n", 1) >= 0 ;
+}
+
static int death_script (buffer *b, char const *s)
{
return put_shebang(b)
@@ -92,11 +100,11 @@ static int death_script (buffer *b, char const *s)
EXECLINE_EXTBINPREFIX "redirfd -w 1 /dev/console\n"
EXECLINE_EXTBINPREFIX "fdmove -c 2 1\n"
EXECLINE_EXTBINPREFIX "foreground { "
- S6_LINUX_INIT_LIBEXECPREFIX "s6-linux-init-echo -- \"s6-svscan ") >= 0
+ S6_LINUX_INIT_BINPREFIX "s6-linux-init-echo -- \"s6-svscan ") >= 0
&& buffer_puts(b, s) >= 0
&& buffer_puts(b,
". Rebooting.\" }\n"
- S6_LINUX_INIT_LIBEXECPREFIX "s6-linux-init-reboot\n") >= 0 ;
+ S6_LINUX_INIT_BINPREFIX "s6-linux-init-hpr -r -f\n") >= 0 ;
}
static int s6_svscan_log_script (buffer *b, char const *data)
@@ -139,7 +147,7 @@ static int logouthookd_script (buffer *b, char const *data)
return put_shebang(b)
&& buffer_puts(b,
S6_EXTBINPREFIX "s6-ipcserver -1 -l0 -- " LOGOUTHOOKD_SOCKET "\n"
- S6_LINUX_INIT_LIBEXECPREFIX "s6-linux-init-logouthookd\n") >= 0 ;
+ S6_LINUX_INIT_BINPREFIX "s6-linux-init-logouthookd\n") >= 0 ;
}
static int shutdownd_script (buffer *b, char const *data)
@@ -147,7 +155,7 @@ static int shutdownd_script (buffer *b, char const *data)
size_t sabase = satmp.len ;
char fmt[UINT_FMT] ;
if (!put_shebang(b)
- || buffer_puts(b, S6_LINUX_INIT_LIBEXECPREFIX "s6-linux-init-shutdownd -c ") < 0
+ || buffer_puts(b, S6_LINUX_INIT_BINPREFIX "s6-linux-init-shutdownd -c ") < 0
|| !string_quote(&satmp, robase, strlen(robase))) return 0 ;
if (buffer_puts(b, satmp.s + sabase) < 0) goto err ;
satmp.len = sabase ;
@@ -188,7 +196,7 @@ static int runleveld_script (buffer *b, char const *data)
static int sig_script (buffer *b, char const *option)
{
return put_shebang(b)
- && buffer_puts(b, S6_LINUX_INIT_LIBEXECPREFIX "s6-linux-init-shutdown -a ") >= 0
+ && buffer_puts(b, S6_LINUX_INIT_BINPREFIX "s6-linux-init-shutdown -a ") >= 0
&& buffer_puts(b, option) >= 0
&& buffer_puts(b, " -- now\n") >= 0 ;
}
@@ -197,7 +205,7 @@ static inline int stage1_script (buffer *b, char const *data)
{
size_t sabase = satmp.len ;
if (!put_shebang_options(b, "-S0")
- || buffer_puts(b, S6_LINUX_INIT_LIBEXECPREFIX "s6-linux-init -c ") < 0
+ || buffer_puts(b, S6_LINUX_INIT_BINPREFIX "s6-linux-init -c ") < 0
|| !string_quote(&satmp, robase, strlen(robase))) return 0 ;
if (buffer_puts(b, satmp.s + sabase) < 0) goto err ;
satmp.len = sabase ;
@@ -360,12 +368,12 @@ static void copy_script (char const *base, char const *src, char const *dst)
static void auto_exec (char const *base, char const *name, char const *target)
{
- if (S6_LINUX_INIT_LIBEXECPREFIX[0] == '/')
+ if (S6_LINUX_INIT_BINPREFIX[0] == '/')
{
size_t len = strlen(target) ;
- char fn[sizeof(S6_LINUX_INIT_LIBEXECPREFIX) + len] ;
- memcpy(fn, S6_LINUX_INIT_LIBEXECPREFIX, sizeof(S6_LINUX_INIT_LIBEXECPREFIX) - 1) ;
- memcpy(fn + sizeof(S6_LINUX_INIT_LIBEXECPREFIX) - 1, target, len + 1) ;
+ char fn[sizeof(S6_LINUX_INIT_BINPREFIX) + len] ;
+ memcpy(fn, S6_LINUX_INIT_BINPREFIX, sizeof(S6_LINUX_INIT_BINPREFIX) - 1) ;
+ memcpy(fn + sizeof(S6_LINUX_INIT_BINPREFIX) - 1, target, len + 1) ;
auto_symlink(base, name, fn) ;
}
else
@@ -540,12 +548,12 @@ static inline void make_scripts (char const *base)
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_script(base, "bin/init", &stage1_script, 0) ;
+ auto_script(base, "bin/halt", &hpr_script, "h") ;
+ auto_script(base, "bin/poweroff", &hpr_script, "p") ;
+ auto_script(base, "bin/reboot", &hpr_script, "r") ;
auto_exec(base, "bin/shutdown", "s6-linux-init-shutdown") ;
auto_exec(base, "bin/telinit", "s6-linux-init-telinit") ;
- auto_script(base, "bin/init", &stage1_script, 0) ;
}
int main (int argc, char const *const *argv, char const *const *envp)
diff --git a/src/init/s6-linux-init-telinit.c b/src/init/s6-linux-init-telinit.c
index 8678709..42441d6 100644
--- a/src/init/s6-linux-init-telinit.c
+++ b/src/init/s6-linux-init-telinit.c
@@ -24,7 +24,7 @@ int main (int argc, char const *const *argv, char const *const *envp)
subgetopt_t l = SUBGETOPT_ZERO ;
for (;;)
{
- int opt = subgetopt_r(argc, argv, "c:p:s:m:d:", &l) ;
+ int opt = subgetopt_r(argc, argv, "c:p:s:m:d:D:", &l) ;
if (opt == -1) break ;
switch (opt)
{
@@ -32,7 +32,8 @@ int main (int argc, char const *const *argv, char const *const *envp)
case 'p' :
case 's' :
case 'm' :
- case 'd' : break ;
+ case 'd' :
+ case 'D' : break ;
default : dieusage() ;
}
}
@@ -63,9 +64,10 @@ int main (int argc, char const *const *argv, char const *const *envp)
fmt[uint_fmt(fmt, WEXITSTATUS(wstat))] = 0 ;
strerr_dief3x(wait_estatus(wstat), newargv[0], " died with exitcode ", fmt) ;
}
-
- newargv[0] = argv[0][0] == '6' ? S6_LINUX_INIT_BINPREFIX "reboot" : S6_LINUX_INIT_BINPREFIX "poweroff" ;
- newargv[1] = 0 ;
+
+ newargv[0] = S6_LINUX_INIT_BINPREFIX "s6-linux-init-hpr" ;
+ newargv[1] = argv[0][0] == '6' ? "-r" : "-p" ;
+ newargv[2] = 0 ;
}
xpathexec_run(newargv[0], newargv, envp) ;
diff --git a/src/lib/s6_linux_init_logouthook.c b/src/lib/s6_linux_init_logouthook.c
index 215365d..810f29b 100644
--- a/src/lib/s6_linux_init_logouthook.c
+++ b/src/lib/s6_linux_init_logouthook.c
@@ -3,6 +3,7 @@
#include <skalibs/djbunix.h>
#include <skalibs/webipc.h>
+#include <s6-linux-init/s6-linux-init.h>
#include "initctl.h"
int s6_linux_init_logouthook (void)
diff --git a/src/shutdown/deps-exe/s6-linux-init-halt b/src/shutdown/deps-exe/s6-linux-init-hpr
index 0c4376c..0c4376c 100644
--- a/src/shutdown/deps-exe/s6-linux-init-halt
+++ b/src/shutdown/deps-exe/s6-linux-init-hpr
diff --git a/src/shutdown/deps-exe/s6-linux-init-poweroff b/src/shutdown/deps-exe/s6-linux-init-poweroff
deleted file mode 100644
index 0c4376c..0000000
--- a/src/shutdown/deps-exe/s6-linux-init-poweroff
+++ /dev/null
@@ -1,5 +0,0 @@
-libhpr.a.xyzzy
-${LIBUTMPS}
--lskarnet
-${TAINNOW_LIB}
-${SOCKET_LIB}
diff --git a/src/shutdown/deps-exe/s6-linux-init-reboot b/src/shutdown/deps-exe/s6-linux-init-reboot
deleted file mode 100644
index 0c4376c..0000000
--- a/src/shutdown/deps-exe/s6-linux-init-reboot
+++ /dev/null
@@ -1,5 +0,0 @@
-libhpr.a.xyzzy
-${LIBUTMPS}
--lskarnet
-${TAINNOW_LIB}
-${SOCKET_LIB}
diff --git a/src/shutdown/hpr_shutdown.c b/src/shutdown/hpr_shutdown.c
index 9c9ff51..3ede92d 100644
--- a/src/shutdown/hpr_shutdown.c
+++ b/src/shutdown/hpr_shutdown.c
@@ -9,7 +9,7 @@
int hpr_shutdown (unsigned int what, tain_t const *when, unsigned int grace)
{
- char pack[5 + TAIN_PACK] = { " hpr"[what] } ;
+ char pack[5 + TAIN_PACK] = { "Shpr"[what] } ;
tain_pack(pack+1, when) ;
uint32_pack_big(pack + 1 + TAIN_PACK, (uint32_t)grace) ;
return hpr_send(pack, 5 + TAIN_PACK) ;
diff --git a/src/shutdown/s6-linux-init-halt.c b/src/shutdown/s6-linux-init-halt.c
deleted file mode 100644
index 4ccca1d..0000000
--- a/src/shutdown/s6-linux-init-halt.c
+++ /dev/null
@@ -1,7 +0,0 @@
-/* ISC license. */
-
-#undef PROGNAME
-#define PROGNAME "s6-linux-init-halt"
-#undef WHATDEFAULT
-#define WHATDEFAULT 1
-#include "hpr.c"
diff --git a/src/shutdown/hpr.c b/src/shutdown/s6-linux-init-hpr.c
index a769c8c..c5f7afb 100644
--- a/src/shutdown/hpr.c
+++ b/src/shutdown/s6-linux-init-hpr.c
@@ -29,15 +29,15 @@
#define _PATH_WTMP "/dev/null/wtmp"
#endif
-#define USAGE PROGNAME " [ -h | -p | -r ] [ -d | -w ] [ -W ] [ -f ]"
+#define USAGE "s6-linux-init-hpr [ -h | -p | -r ] [ -d | -w ] [ -W ] [ -f ]"
int main (int argc, char const *const *argv)
{
- int what = WHATDEFAULT ;
+ int what = 0 ;
int force = 0 ;
int dowtmp = 1 ;
int dowall = 1 ;
- PROG = PROGNAME ;
+ PROG = "s6-linux-init-hpr" ;
{
subgetopt_t l = SUBGETOPT_ZERO ;
@@ -60,6 +60,9 @@ int main (int argc, char const *const *argv)
argc -= l.ind ; argv += l.ind ;
}
+ if (!what)
+ strerr_dief1x(100, "one of the -h, -p or -r options must be given") ;
+
if (geteuid())
{
errno = EPERM ;
diff --git a/src/shutdown/s6-linux-init-poweroff.c b/src/shutdown/s6-linux-init-poweroff.c
deleted file mode 100644
index d25dbb6..0000000
--- a/src/shutdown/s6-linux-init-poweroff.c
+++ /dev/null
@@ -1,7 +0,0 @@
-/* ISC license. */
-
-#undef PROGNAME
-#define PROGNAME "s6-linux-init-poweroff"
-#undef WHATDEFAULT
-#define WHATDEFAULT 2
-#include "hpr.c"
diff --git a/src/shutdown/s6-linux-init-reboot.c b/src/shutdown/s6-linux-init-reboot.c
deleted file mode 100644
index 3612684..0000000
--- a/src/shutdown/s6-linux-init-reboot.c
+++ /dev/null
@@ -1,7 +0,0 @@
-/* ISC license. */
-
-#undef PROGNAME
-#define PROGNAME "s6-linux-init-reboot"
-#undef WHATDEFAULT
-#define WHATDEFAULT 3
-#include "hpr.c"
diff --git a/src/shutdown/s6-linux-init-shutdownd.c b/src/shutdown/s6-linux-init-shutdownd.c
index c6d6316..90d43e8 100644
--- a/src/shutdown/s6-linux-init-shutdownd.c
+++ b/src/shutdown/s6-linux-init-shutdownd.c
@@ -36,6 +36,41 @@
static char const *basedir = BASEDIR ;
+static inline void run_stage3 (char const *basedir, char const *const *envp)
+{
+ pid_t pid ;
+ size_t basedirlen = strlen(basedir) ;
+ char stage3[basedirlen + sizeof("/" STAGE3)] ;
+ char const *stage3_argv[2] = { stage3, 0 } ;
+ memcpy(stage3, basedir, basedirlen) ;
+ memcpy(stage3 + basedirlen, "/" STAGE3, sizeof("/" STAGE3)) ;
+ pid = child_spawn0(stage3_argv[0], stage3_argv, envp) ;
+ if (pid)
+ {
+ int wstat ;
+ if (wait_pid(pid, &wstat) == -1) strerr_diefu1sys(111, "waitpid") ;
+ if (WIFSIGNALED(wstat))
+ {
+ char fmt[UINT_FMT] ;
+ fmt[uint_fmt(fmt, WTERMSIG(wstat))] = 0 ;
+ strerr_warnw3x(stage3, " was killed by signal ", fmt) ;
+ }
+ else if (WEXITSTATUS(wstat))
+ {
+ char fmt[UINT_FMT] ;
+ fmt[uint_fmt(fmt, WTERMSIG(wstat))] = 0 ;
+ strerr_warnw3x(stage3, " was killed by signal ", fmt) ;
+ }
+ else if (WEXITSTATUS(wstat))
+ {
+ char fmt[UINT_FMT] ;
+ fmt[uint_fmt(fmt, WEXITSTATUS(wstat))] = 0 ;
+ strerr_warnw3x(stage3, " exited ", fmt) ;
+ }
+ }
+ else strerr_warnwu2sys("spawn ", stage3) ;
+}
+
static inline void prepare_shutdown (char c, unsigned int *what, tain_t *deadline, unsigned int *grace_time)
{
uint32_t u ;
@@ -43,22 +78,23 @@ static inline void prepare_shutdown (char c, unsigned int *what, tain_t *deadlin
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") ;
+ *what = byte_chr("Shpr", c, 4) ;
tain_unpack(pack, deadline) ;
uint32_unpack_big(pack + TAIN_PACK, &u) ;
if (u && u <= 300000) *grace_time = u ;
- *what = 1 + byte_chr("hpr", c, 3) ;
}
-static inline void handle_stdin (unsigned int *what, tain_t *deadline, unsigned int *grace_time)
+static inline void handle_fifo (buffer *b, unsigned int *what, tain_t *deadline, unsigned int *grace_time)
{
for (;;)
{
char c ;
- ssize_t r = sanitize_read(buffer_get(buffer_0small, &c, 1)) ;
+ ssize_t r = sanitize_read(buffer_get(b, &c, 1)) ;
if (r == -1) strerr_diefu1sys(111, "read from pipe") ;
else if (!r) break ;
switch (c)
{
+ case 'S' :
case 'h' :
case 'p' :
case 'r' :
@@ -80,7 +116,6 @@ static inline void handle_stdin (unsigned int *what, tain_t *deadline, unsigned
static inline void prepare_stage4 (char const *basedir, unsigned int what)
{
- static char const *table[3] = { "halt", "poweroff", "reboot" } ;
buffer b ;
int fd ;
char buf[512] ;
@@ -92,9 +127,9 @@ static inline void prepare_stage4 (char const *basedir, unsigned int what)
if (buffer_puts(&b,
"#!" EXECLINE_SHEBANGPREFIX "/execlineb -P\n\n"
EXECLINE_EXTBINPREFIX "foreground { "
- S6_LINUX_INIT_LIBEXECPREFIX "s6-linux-init-umountall }\n"
- S6_LINUX_INIT_LIBEXECPREFIX "s6-linux-init-") < 0
- || buffer_puts(&b, table[what-1]) < 0
+ S6_LINUX_INIT_BINPREFIX "s6-linux-init-umountall }\n"
+ S6_LINUX_INIT_BINPREFIX "s6-linux-init-hpr -") < 0
+ || buffer_put(&b, "hpr" + what - 1, 1) < 0
|| buffer_putsflush(&b, " -f\n") < 0)
strerr_diefu2sys(111, "write to ", STAGE4_FILE ".new") ;
if (fchmod(fd, S_IRWXU) == -1)
@@ -106,10 +141,12 @@ static inline void prepare_stage4 (char const *basedir, unsigned int what)
int main (int argc, char const *const *argv, char const *const *envp)
{
- pid_t pid ;
- tain_t deadline ;
unsigned int what = 0 ;
unsigned int grace_time = 3000 ;
+ tain_t deadline ;
+ int fdr, fdw ;
+ buffer b ;
+ char buf[64] ;
PROG = "s6-linux-init-shutdownd" ;
{
@@ -139,80 +176,46 @@ int main (int argc, char const *const *argv, char const *const *envp)
strerr_warnwu2sys("exec ", stage4_argv[0]) ;
}
- fd_close(1) ;
- fd_close(0) ;
-
- if (open_read(SHUTDOWND_FIFO))
+ fdr = open_read(SHUTDOWND_FIFO) ;
+ if (fdr == -1 || coe(fdr) == -1)
strerr_diefu3sys(111, "open ", SHUTDOWND_FIFO, " for reading") ;
- if (open_write(SHUTDOWND_FIFO) != 1)
+ fdw = open_write(SHUTDOWND_FIFO) ;
+ if (fdw == -1 || coe(fdw) == -1)
strerr_diefu3sys(111, "open ", SHUTDOWND_FIFO, " for writing") ;
if (sig_ignore(SIGPIPE) == -1)
strerr_diefu1sys(111, "sig_ignore SIGPIPE") ;
+ buffer_init(&b, &buffer_read, fdr, buf, 64) ;
tain_now_g() ;
tain_add_g(&deadline, &tain_infinite_relative) ;
for (;;)
{
- iopause_fd x = { .fd = 0, .events = IOPAUSE_READ } ;
+ iopause_fd x = { .fd = fdr, .events = IOPAUSE_READ } ;
int r = iopause_g(&x, 1, &deadline) ;
if (r == -1) strerr_diefu1sys(111, "iopause") ;
if (!r)
{
+ run_stage3(basedir, envp) ;
+ tain_now_g() ;
if (what) break ;
tain_add_g(&deadline, &tain_infinite_relative) ;
continue ;
}
if (x.revents & IOPAUSE_READ)
- handle_stdin(&what, &deadline, &grace_time) ;
+ handle_fifo(&b, &what, &deadline, &grace_time) ;
}
-
- /* Stage 3 */
-
+ fd_close(fdw) ;
+ fd_close(fdr) ;
fd_close(1) ;
- fd_close(0) ;
- if (open_readb("/dev/null"))
- strerr_diefu1sys(111, "open /dev/null for reading") ;
if (open_write("/dev/console") != 1 || ndelay_off(1) == -1)
strerr_diefu1sys(111, "open /dev/console for writing") ;
- {
- size_t basedirlen = strlen(basedir) ;
- char stage3[basedirlen + sizeof("/" STAGE3)] ;
- char const *stage3_argv[2] = { stage3, 0 } ;
- memcpy(stage3, basedir, basedirlen) ;
- memcpy(stage3 + basedirlen, "/" STAGE3, sizeof("/" STAGE3)) ;
- pid = child_spawn0(stage3_argv[0], stage3_argv, envp) ;
- if (pid)
- {
- int wstat ;
- if (wait_pid(pid, &wstat) == -1) strerr_diefu1sys(111, "waitpid") ;
- if (WIFSIGNALED(wstat))
- {
- char fmt[UINT_FMT] ;
- fmt[uint_fmt(fmt, WTERMSIG(wstat))] = 0 ;
- strerr_warnw3x(stage3, " was killed by signal ", fmt) ;
- }
- else if (WEXITSTATUS(wstat))
- {
- char fmt[UINT_FMT] ;
- fmt[uint_fmt(fmt, WTERMSIG(wstat))] = 0 ;
- strerr_warnw3x(stage3, " was killed by signal ", fmt) ;
- }
- else if (WEXITSTATUS(wstat))
- {
- char fmt[UINT_FMT] ;
- fmt[uint_fmt(fmt, WEXITSTATUS(wstat))] = 0 ;
- strerr_warnw3x(stage3, " exited ", fmt) ;
- }
- }
- else strerr_warnwu2sys("spawn ", stage3) ;
- }
+ if (fd_copy(2, 1) == -1) strerr_warnwu1sys("fd_copy") ;
/* The end is coming! */
prepare_stage4(basedir, what) ;
- if (fd_move(2, 1) == -1) strerr_warnwu1sys("fd_move") ;
sync() ;
if (sig_ignore(SIGTERM) == -1) strerr_warnwu1sys("sig_ignore SIGTERM") ;
strerr_warni1x("sending all processes the TERM signal...") ;