From 9e6d0f168bf59df9cd829d6ebe63fb08ea9ae01e Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Sat, 20 Apr 2019 05:58:37 +0000 Subject: Add skel files; add initdefault; remove env-stage2 --- configure | 2 +- skel/rc.init | 34 ++++++++++++++++++++++++++++++++++ skel/rc.shutdown | 17 +++++++++++++++++ skel/runlevel | 15 +++++++++++++++ src/include-local/initctl.h | 3 +-- src/init/s6-linux-init-maker.c | 36 ++++++++++++++++++++---------------- src/init/s6-linux-init.c | 31 ++++++++++++++++++++----------- 7 files changed, 108 insertions(+), 30 deletions(-) diff --git a/configure b/configure index 5aff30e..5d7d438 100755 --- a/configure +++ b/configure @@ -127,7 +127,7 @@ tryldflag () { } getmacro () { - echo "#include <$1>" | $CC_AUTO -v $CPPFLAGS_AUTO $CPPFLAGS $CPPFLAGS_POST $CFLAGS_AUTO $CFLAGS $CFLAGS_POST -E -dM - | grep -F -- "$2" | cut -d' ' -f3- + echo "#include <$1>" | $CC_AUTO $CPPFLAGS_AUTO $CPPFLAGS $CPPFLAGS_POST $CFLAGS_AUTO $CFLAGS $CFLAGS_POST -E -dM - | grep -F -- "$2" | cut -d' ' -f3- } getmacrostring () { diff --git a/skel/rc.init b/skel/rc.init index e69de29..37c3ea9 100755 --- a/skel/rc.init +++ b/skel/rc.init @@ -0,0 +1,34 @@ +#!/bin/sh -e + +rl="$1" +shift + +### argv now contains the arguments of the kernel command line that are +### not of the form key=value. (The key=value arguments were stored by +### s6-linux-init into an envdir, if instructed so via the -s option.) +### Normally this argv remains unused because programs that need the +### kernel command line usually read it later on from /proc/cmdline - +### but just in case, it's available here. + + +### 1. Early preparation +### This is done only once at boot time. +### Ideally, this phase should just initialize the service manager. + +### If your services are managed by sysv-rc: +# /etc/init.d/rcS + +### If your services are managed by OpenRC: +# /sbin/openrc sysinit +# /sbin/openrc boot + +### If your services are managed by s6-rc: +### (replace /run/service with your scandir) +# s6-rc-init /run/service + + +### 2. Starting the wanted set of services +### This is also called every time you change runlevels with telinit. +### (edit the location to suit your installation) + +# exec /etc/s6-linux-init/current/scripts/runlevel "$rl" diff --git a/skel/rc.shutdown b/skel/rc.shutdown index e69de29..6acd0f0 100755 --- a/skel/rc.shutdown +++ b/skel/rc.shutdown @@ -0,0 +1,17 @@ +#!/bin/sh -e + +### Things to do before hardware halt/reboot/poweroff. +### Ideally, it should be a single call to the service manager, +### telling it to bring all the services down. + +### If your services are managed by sysv-rc: +### remove the K11reboot link from /etc/rc6.d to prevent +### sysv-rc from rebooting prematurely - because sysvinit does +### not properly separate state changes from system init/shutdown. +# exec /etc/init.d/rc 6 + +### If your services are managed by OpenRC: +# exec /sbin/openrc shutdown + +### If your services are managed by s6-rc: +# exec s6-rc -da change diff --git a/skel/runlevel b/skel/runlevel index e69de29..6368b4b 100755 --- a/skel/runlevel +++ b/skel/runlevel @@ -0,0 +1,15 @@ +#!/bin/sh -e + +### This script is called once at boot time by rc.init, and is +### also called by the runleveld service every time the user +### requests a machine state change via telinit. +### Ideally, it should just be a call to the service manager. + +### If your services are managed by sysv-rc: +# exec /etc/init.d/rc "$1" + +### If your services are managed by OpenRC: +# exec /sbin/openrc "$1" + +### If your services are managed by s6-rc: +# exec s6-rc -v2 change "$1" diff --git a/src/include-local/initctl.h b/src/include-local/initctl.h index 73caf02..507aefe 100644 --- a/src/include-local/initctl.h +++ b/src/include-local/initctl.h @@ -27,8 +27,7 @@ #define EARLYGETTY S6_LINUX_INIT_TMPFS "/" SCANDIR "/" EARLYGETTY_SERVICEDIR #define RUNIMAGE "run-image" -#define ENVSTAGE1 "env-global" -#define ENVSTAGE2 "env-stage2" +#define ENVSTAGE1 "env" #define STAGE2 "rc.init" #define STAGE3 "rc.shutdown" diff --git a/src/init/s6-linux-init-maker.c b/src/init/s6-linux-init-maker.c index 62f04da..82ab14f 100644 --- a/src/init/s6-linux-init-maker.c +++ b/src/init/s6-linux-init-maker.c @@ -28,12 +28,12 @@ #ifdef S6_LINUX_INIT_UTMPD_PATH # include -# define USAGE "s6-linux-init-maker [ -c basedir ] [ -u log_user ] [ -G early_getty_cmd ] [ -1 ] [ -L ] [ -p initial_path ] [ -m initial_umask ] [ -t timestamp_style ] [ -d slashdev ] [ -s env_store ] [ -e initial_envvar ... ] [ -q default_grace_time ] [ -U utmp_user ]" -# define OPTION_STRING "c:u:G:1Lp:m:t:d:s:e:E:q:U:" +# define USAGE "s6-linux-init-maker [ -c basedir ] [ -u log_user ] [ -G early_getty_cmd ] [ -1 ] [ -L ] [ -p initial_path ] [ -m initial_umask ] [ -t timestamp_style ] [ -d slashdev ] [ -s env_store ] [ -e initial_envvar ... ] [ -q default_grace_time ] [ -D initdefault ] [ -U utmp_user ]" +# define OPTION_STRING "c:u:G:1Lp:m:t:d:s:e:E:q:U:D:" # define UTMPS_DIR "utmps" #else -# define USAGE "s6-linux-init-maker [ -c basedir ] [ -u log_user ] [ -G early_getty_cmd ] [ -1 ] [ -L ] [ -p initial_path ] [ -m initial_umask ] [ -t timestamp_style ] [ -d slashdev ] [ -s env_store ] [ -e initial_envvar ... ] [ -q default_grace_time ]" -# define OPTION_STRING "c:u:G:1Lp:m:t:d:s:e:E:q:" +# define USAGE "s6-linux-init-maker [ -c basedir ] [ -u log_user ] [ -G early_getty_cmd ] [ -1 ] [ -L ] [ -p initial_path ] [ -m initial_umask ] [ -t timestamp_style ] [ -d slashdev ] [ -s env_store ] [ -e initial_envvar ... ] [ -q default_grace_time ] [ -D initdefault ]" +# define OPTION_STRING "c:u:G:1Lp:m:t:d:s:e:E:q:D:" #endif #define dieusage() strerr_dieusage(100, USAGE) @@ -47,6 +47,7 @@ static char const *env_store = 0 ; static char const *early_getty = 0 ; static char const *slashdev = 0 ; static char const *log_user = "root" ; +static char const *initdefault = 0 ; static unsigned int initial_umask = 0022 ; static unsigned int timestamp_style = 1 ; static unsigned int finalsleep = 3000 ; @@ -226,6 +227,13 @@ static inline int stage1_script (buffer *b, char const *data) if (buffer_puts(b, satmp.s + sabase) < 0) goto err ; satmp.len = sabase ; } + if (initdefault) + { + if (buffer_puts(b, " -D ") < 0 + || !string_quote(&satmp, initdefault, strlen(initdefault))) return 0 ; + if (buffer_puts(b, satmp.s + sabase) < 0) goto err ; + satmp.len = sabase ; + } if (buffer_puts(b, "\n") < 0) return 0 ; (void)data ; return 1 ; @@ -404,15 +412,15 @@ static void getug (char const *s, uid_t *uid, gid_t *gid) static inline void auto_basedir (char const *base, char const *dir, uid_t uid, gid_t gid, unsigned int mode) { size_t n = strlen(dir) ; - char tmp[n+1] ; - for (size_t i = 0; i < n ; i++) + char tmp[n + 1] ; + for (size_t i = 0 ; i < n ; i++) { - if ((s[i] == '/') && i) + if ((dir[i] == '/') && i) { tmp[i] = 0 ; auto_dir_internal(base, tmp, uid, gid, mode, 0) ; } - tmp[i] = s[i] ; + tmp[i] = dir[i] ; } } @@ -542,8 +550,6 @@ static inline void make_bins (char const *base) 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 ; @@ -563,9 +569,9 @@ int main (int argc, char const *const *argv, char const *const *envp) case 't' : if (!uint0_scan(l.arg, ×tamp_style)) dieusage() ; break ; case 'd' : slashdev = l.arg ; break ; case 's' : env_store = l.arg ; 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 'e' : if (!stralloc_catb(&satmp, l.arg, strlen(l.arg) + 1)) dienomem() ; break ; case 'q' : if (!uint0_scan(l.arg, &finalsleep)) dieusage() ; break ; + case 'D' : initdefault = l.arg ; break ; #ifdef S6_LINUX_INIT_UTMPD_PATH case 'U' : utmp_user = l.arg ; break ; #endif @@ -587,10 +593,8 @@ int main (int argc, char const *const *argv, char const *const *envp) if (mkdir(argv[0], 0755) < 0) strerr_diefu2sys(111, "mkdir ", 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_env(argv[0], ENVSTAGE1, satmp.s, satmp.len) ; + satmp.len = 0 ; make_image(argv[0]) ; make_scripts(argv[0]) ; make_bins(argv[0]) ; diff --git a/src/init/s6-linux-init.c b/src/init/s6-linux-init.c index aeddbac..94a2054 100644 --- a/src/init/s6-linux-init.c +++ b/src/init/s6-linux-init.c @@ -23,28 +23,35 @@ #include "defaults.h" #include "initctl.h" -#define USAGE "s6-linux-init [ -c basedir ] [ -p initpath ] [ -s envdumpdir ] [ -m umask ] [ -d devtmpfs ]" +#define USAGE "s6-linux-init [ -c basedir ] [ -p initpath ] [ -s envdumpdir ] [ -m umask ] [ -d devtmpfs ] [ -D initdefault ]" #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) +static inline char const *scan_cmdline (char const *initdefault, char const *const *argv, unsigned int argc) +{ + static char const *valid[] = { "default", "2", "3", "4", "5", 0 } ; + for (unsigned int i = 0 ; i < argc ; i++) + for (char const *const *p = valid ; *p ; p++) + if (!strcmp(argv[i], *p)) return argv[i] ; + return initdefault ; +} + +static inline void run_stage2 (char const *basedir, char const **argv, unsigned int argc, char const *const *envp, size_t envlen, stralloc *envmodifs, char const *initdefault) { size_t dirlen = strlen(basedir) ; - char const *childargv[argc + 2] ; - char fn[dirlen + 1 + (sizeof(ENVSTAGE2) > sizeof(STAGE2) ? sizeof(ENVSTAGE2) : sizeof(STAGE2))] ; + char const *childargv[argc + 3] ; + char fn[dirlen + 1 + 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 ; + childargv[1] = scan_cmdline(initdefault, argv + 1, argc - 1) ; for (unsigned int i = 0 ; i < argc ; i++) - childargv[i+1] = argv[i] ; - childargv[argc + 1] = 0 ; + childargv[i+2] = argv[i] ; + childargv[argc + 2] = 0 ; setsid() ; fd_close(1) ; if (open(LOGFIFO, O_WRONLY) != 1) /* blocks until catch-all logger is up */ @@ -61,6 +68,7 @@ int main (int argc, char const **argv, char const *const *envp) char const *path = INITPATH ; char const *slashdev = 0 ; char const *envdumpdir = 0 ; + char const *initdefault = "default" ; stralloc envmodifs = STRALLOC_ZERO ; PROG = "s6-linux-init" ; @@ -75,7 +83,7 @@ int main (int argc, char 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) { @@ -84,6 +92,7 @@ int main (int argc, char const **argv, char const *const *envp) case 's' : envdumpdir = l.arg ; break ; case 'm' : if (!uint0_oscan(l.arg, &mask)) dieusage() ; break ; case 'd' : slashdev = l.arg ; break ; + case 'D' : initdefault = l.arg ; break ; default : dieusage() ; } } @@ -158,7 +167,7 @@ int main (int argc, char const **argv, char const *const *envp) } pid = fork() ; if (pid == -1) strerr_diefu1sys(111, "fork") ; - if (!pid) run_stage2(basedir, argv, argc, newenvp, !!path, &envmodifs) ; + if (!pid) run_stage2(basedir, argv, argc, newenvp, !!path, &envmodifs, initdefault) ; if (fd_copy(2, 1) == -1) strerr_diefu1sys(111, "redirect output file descriptor") ; xpathexec_r(newargv, newenvp, !!path, envmodifs.s, envmodifs.len) ; -- cgit v1.2.3