diff --git a/builder/build-latest b/builder/build-latest index 7b347eb..2001164 100755 --- a/builder/build-latest +++ b/builder/build-latest @@ -100,8 +100,11 @@ for output in "${outputs[@]}"; do mkdir -p $overlaydstpath/etc/s6/services/s6-fdholderd/supervise chmod 0700 $overlaydstpath/etc/s6/services/s6-fdholderd/supervise - # fix fix-attrs perms + # fix perms for utilities chmod 0755 $overlaydstpath/usr/bin/fix-attrs + chmod 0755 $overlaydstpath/usr/bin/import-from-envdir + chmod 0755 $overlaydstpath/usr/bin/with-contenv + chmod 0755 $overlaydstpath/usr/bin/with-notifywhenup # fix init perms chmod 0755 $overlaydstpath/init diff --git a/builder/overlay-rootfs/etc/s6/init-catchall/init-stage2 b/builder/overlay-rootfs/etc/s6/init-catchall/init-stage2 index f351cec..942c923 100755 --- a/builder/overlay-rootfs/etc/s6/init-catchall/init-stage2 +++ b/builder/overlay-rootfs/etc/s6/init-catchall/init-stage2 @@ -5,20 +5,24 @@ # environment. # Purpose of this file: to perform all the one-time initialization tasks. -if -nt +foreground { if { redirfd -w 1 /var/run/s6/uncaught-logs-fifo fdmove -c 2 1 - /etc/s6/init/init-stage2-fix-attrs - /etc/s6/init/init-stage2-cont-inits - /etc/s6/init/init-stage2-services + /etc/s6/init/init-stage2-partial } - /etc/s6/init/init-stage2-cmd $@ } - -# if anything in the if -nt { } block fails, control jumps here. +import -u ? +if { s6-test ${?} -ne 0 } +import-from-envdir -u -D 0 /var/run/s6/container_environment S6_BEHAVIOUR_IF_STAGE2_FAILS +if { s6-test ${S6_BEHAVIOUR_IF_STAGE2_FAILS} -ne 0 } +ifelse { s6-test ${S6_BEHAVIOUR_IF_STAGE2_FAILS} -ne 1 } +{ + # Stop supervision tree + s6-svscanctl -t /var/run/s6/services +} s6-echo -- "\n!!!!!\n init-stage2 failed.\n Logs are in /var/log/s6-uncaught-logs\n!!!!!" diff --git a/builder/overlay-rootfs/etc/s6/init-no-catchall/init-stage2 b/builder/overlay-rootfs/etc/s6/init-no-catchall/init-stage2 index c4b67d3..4cdbde9 100644 --- a/builder/overlay-rootfs/etc/s6/init-no-catchall/init-stage2 +++ b/builder/overlay-rootfs/etc/s6/init-no-catchall/init-stage2 @@ -5,17 +5,18 @@ # environment. # Purpose of this file: to perform all the one-time initialization tasks. -if -nt +foreground { - if - { - /etc/s6/init/init-stage2-fix-attrs - /etc/s6/init/init-stage2-cont-inits - /etc/s6/init/init-stage2-services - } - + if { /etc/s6/init/init-stage2-partial } /etc/s6/init/init-stage2-cmd $@ } - -# if anything in the if -nt { } block fails, control jumps here. +import -u ? +if { s6-test ${?} -ne 0 } +import-from-envdir -u -D 0 /var/run/s6/container_environment S6_BEHAVIOUR_IF_STAGE2_FAILS +if { s6-test ${S6_BEHAVIOUR_IF_STAGE2_FAILS} -ne 0 } +ifelse { s6-test ${S6_BEHAVIOUR_IF_STAGE2_FAILS} -ne 1 } +{ + # Stop supervision tree + s6-svscanctl -t /var/run/s6/services +} s6-echo -- "\n!!!!!\n init-stage2 failed.\n!!!!!" diff --git a/builder/overlay-rootfs/etc/s6/init/init-stage1 b/builder/overlay-rootfs/etc/s6/init/init-stage1 index 9ddfd11..49621e5 100644 --- a/builder/overlay-rootfs/etc/s6/init/init-stage1 +++ b/builder/overlay-rootfs/etc/s6/init/init-stage1 @@ -18,17 +18,12 @@ /usr/bin/s6-envdir /etc/s6/init/env /usr/bin/exec -- + ## ## route based on what was provided in S6_USE_CATCHALL_LOGGER ## -backtick -n -D 0 S6_USE_CATCHALL_LOGGER -{ - if { s6-test -f /var/run/s6/container_environment/S6_USE_CATCHALL_LOGGER } - redirfd -r 0 /var/run/s6/container_environment/S6_USE_CATCHALL_LOGGER - s6-cat -} -import -u S6_USE_CATCHALL_LOGGER +import-from-envdir -u -D 0 /var/run/s6/container_environment S6_USE_CATCHALL_LOGGER ifelse { s6-test ${S6_USE_CATCHALL_LOGGER} -eq 1 } { /etc/s6/init-catchall/init-stage1 $@ diff --git a/builder/overlay-rootfs/etc/s6/init/init-stage2-cmd b/builder/overlay-rootfs/etc/s6/init/init-stage2-cmd index 6d17d76..c0f4ec7 100644 --- a/builder/overlay-rootfs/etc/s6/init/init-stage2-cmd +++ b/builder/overlay-rootfs/etc/s6/init/init-stage2-cmd @@ -7,7 +7,10 @@ if -t { s6-test $# -ne 0 } foreground { s6-setsid -gq -- with-contenv $@ } -foreground { import -u ? s6-echo "${1} exited ${?}. Stopping the supervision tree." } +import -u ? +if { s6-echo "${1} exited ${?}" } + +# Stop supervision tree foreground { s6-svscanctl -t /var/run/s6/services } # Race condition here, but it's ok: the sync in stage 3 takes ages, diff --git a/builder/overlay-rootfs/etc/s6/init/init-stage2-cont-inits b/builder/overlay-rootfs/etc/s6/init/init-stage2-cont-inits deleted file mode 100644 index fb15053..0000000 --- a/builder/overlay-rootfs/etc/s6/init/init-stage2-cont-inits +++ /dev/null @@ -1,19 +0,0 @@ -#!/usr/bin/execlineb -S0 - -## -## cont-init.d: one-time init scripts -## - -if { s6-echo "[cont-init.d] executing container initialization scripts..." } -if -{ - pipeline { s6-ls -0 -- /etc/cont-init.d } - pipeline { s6-sort -0 -- } - forstdin -0 -- i - import -u i - if { s6-echo -n -- "[cont-init.d] executing ${i}... " } - foreground { with-contenv /etc/cont-init.d/${i} } - import -u ? s6-echo -- " ... exited ${?}" -} -if { s6-echo -- "[cont-init.d] done." } -$@ diff --git a/builder/overlay-rootfs/etc/s6/init/init-stage2-fix-attrs b/builder/overlay-rootfs/etc/s6/init/init-stage2-fix-attrs deleted file mode 100644 index 2e3afc0..0000000 --- a/builder/overlay-rootfs/etc/s6/init/init-stage2-fix-attrs +++ /dev/null @@ -1,19 +0,0 @@ -#!/usr/bin/execlineb -S0 - -## -## fix-attrs.d: ensure owners and permissions are correct -## - -if { s6-echo "[fix-attrs.d] applying owners & permissions fixes..." } -if -{ - pipeline { s6-ls -0 -- /etc/fix-attrs.d } - pipeline { s6-sort -0 -- } - forstdin -0 -- i - import -u i - if { s6-echo -n -- "[fix-attrs.d] applying ${i}... " } - foreground { redirfd -r 0 /etc/fix-attrs.d/${i} fix-attrs } - import -u ? s6-echo -- " exited ${?}" -} -if { s6-echo -- "[fix-attrs.d] done." } -$@ diff --git a/builder/overlay-rootfs/etc/s6/init/init-stage2-partial b/builder/overlay-rootfs/etc/s6/init/init-stage2-partial new file mode 100644 index 0000000..ce88b2a --- /dev/null +++ b/builder/overlay-rootfs/etc/s6/init/init-stage2-partial @@ -0,0 +1,68 @@ +#!/usr/bin/execlineb -S0 + +# this env decides what to do if stage2 fails +import-from-envdir -u -D 0 /var/run/s6/container_environment S6_BEHAVIOUR_IF_STAGE2_FAILS + +foreground +{ + ## + ## fix-attrs.d: ensure owners and permissions are correct + ## + + if { s6-echo "[fix-attrs.d] applying owners & permissions fixes..." } + if + { + pipeline { s6-ls -0 -- /etc/fix-attrs.d } + pipeline { s6-sort -0 -- } + forstdin -0 -- i + import -u i + if { s6-echo -n -- "[fix-attrs.d] applying ${i}... " } + foreground { redirfd -r 0 /etc/fix-attrs.d/${i} fix-attrs } + import -u ? + if { s6-echo -- " exited ${?}" } + ifelse { s6-test ${S6_BEHAVIOUR_IF_STAGE2_FAILS} -eq 0 } { exit 0 } + exit ${?} + } + if { s6-echo -- "[fix-attrs.d] done." } + + + ## + ## cont-init.d: one-time init scripts + ## + + if { s6-echo "[cont-init.d] executing container initialization scripts..." } + if + { + pipeline { s6-ls -0 -- /etc/cont-init.d } + pipeline { s6-sort -0 -- } + forstdin -o 0 -0 -- i + import -u i + if { s6-echo -n -- "[cont-init.d] executing ${i}... " } + foreground { with-contenv /etc/cont-init.d/${i} } + import -u ? + if { s6-echo -- " exited ${?}" } + ifelse { s6-test ${S6_BEHAVIOUR_IF_STAGE2_FAILS} -eq 0 } { exit 0 } + exit ${?} + } + if { s6-echo -- "[cont-init.d] done." } + + ## + ## services.d: long-lived processes to be supervised + ## + + + if { s6-echo "[services.d] starting services" } + if + { + pipeline { s6-ls -0 -- /etc/services.d } + forstdin -0 -p -- i + import -u i + if { s6-test -d /etc/services.d/${i} } + s6-hiercopy /etc/services.d/${i} /var/run/s6/services/${i} + } + if { s6-svscanctl -a /var/run/s6/services } + if { s6-echo -- "[services.d] done." } +} +import -u ? +ifelse { s6-test ${S6_BEHAVIOUR_IF_STAGE2_FAILS} -eq 0 } { exit 0 } +exit ${?} diff --git a/builder/overlay-rootfs/etc/s6/init/init-stage2-services b/builder/overlay-rootfs/etc/s6/init/init-stage2-services deleted file mode 100644 index 5fcaa32..0000000 --- a/builder/overlay-rootfs/etc/s6/init/init-stage2-services +++ /dev/null @@ -1,18 +0,0 @@ -#!/usr/bin/execlineb -S0 - -## -## services.d: long-lived processes to be supervised -## - -if { s6-echo "[services.d] starting services" } -if -{ - pipeline { s6-ls -0 -- /etc/services.d } - forstdin -0 -p -- i - import -u i - if { s6-test -d /etc/services.d/${i} } - s6-hiercopy /etc/services.d/${i} /var/run/s6/services/${i} -} -if { s6-svscanctl -a /var/run/s6/services } -if { s6-echo -- "[services.d] done." } -$@ diff --git a/builder/overlay-rootfs/etc/s6/init/init-stage3 b/builder/overlay-rootfs/etc/s6/init/init-stage3 index b374627..702a3b3 100644 --- a/builder/overlay-rootfs/etc/s6/init/init-stage3 +++ b/builder/overlay-rootfs/etc/s6/init/init-stage3 @@ -15,12 +15,8 @@ foreground { s6-sync } foreground { s6-echo "[s6-finish] sending all processes the TERM signal." } foreground { s6-nuke -th } # foreground is process 1: it survives -foreground -{ - with-contenv - /usr/bin/import -u -D 3000 S6_KILL_GRACETIME - /usr/bin/s6-sleep -m -- ${S6_KILL_GRACETIME} -} +import-from-envdir -u -D 3000 /var/run/s6/container_environment S6_KILL_GRACETIME +foreground { s6-sleep -m -- ${S6_KILL_GRACETIME} } # Last message, then close our pipes and give the logger some time. foreground { s6-echo "[s6-finish] sending all processes the KILL signal and exiting." } diff --git a/builder/overlay-rootfs/usr/bin/import-from-envdir b/builder/overlay-rootfs/usr/bin/import-from-envdir new file mode 100644 index 0000000..e6e9849 --- /dev/null +++ b/builder/overlay-rootfs/usr/bin/import-from-envdir @@ -0,0 +1,29 @@ +#!/usr/bin/execlineb + +elgetopt "uD:" +multisubstitute +{ + importas -u envdir 1 + importas -u envvar 2 + importas -u -D "" defval ELGETOPT_D + import -u -D 0 ELGETOPT_u +} +shift -n 2 +elgetpositionals -P0 +emptyenv -P +backtick -n ${envvar} +{ + ifelse { s6-test -f ${envdir}/${envvar} } + { + redirfd -r 0 ${envdir}/${envvar} + s6-cat + } + s6-echo -n "${defval}" +} +ifelse { s6-test ${ELGETOPT_u} -eq 1 } +{ + import -u ${envvar} + $@ +} +import ${envvar} +$@