Welcome to little lamb

Code » anopa » jjk » tree

[jjk] / src / scripts / aa-stage3

#!@BINDIR@/execlineb -S0
#
# anopa - Copyright (C) 2015-2017 Olivier Brunel
#
# aa-stage3
# Copyright (C) 2015-2018 Olivier Brunel <jjk@jjacky.com>
#
# This file is part of anopa.
#
# anopa is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# anopa is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# anopa. If not, see http://www.gnu.org/licenses/
#

# Set up a pipe to log messages
piperw 3 4
background
{
    fdclose 4 fdmove 0 3
    redirfd -a 1 /var/log/boot/current
    fdmove -c 2 1
    # put a @ as first char in argv[0] so it is skipped by the aa-kill calls
    exec -a @logger
    s6-tai64n
}
fdclose 3
fdmove 3 4

# Make sure we're sane
cd /
backtick -n TTY { aa-tty }
importas -u TTY TTY
redirfd -r 0 ${TTY}
redirfd -w 1 /dev/console
fdmove -c 2 1

foreground { aa-echo -O3 -B "Stage 3: Preparing ${1}..." }
# Stop all running services -- s6-svscan did only exec into us, leaving the
# whole supervised tree intact. Here we stop everything (longrun & oneshot) in
# order, save for the catch-all (it will get an 'x' though).
foreground {
    foreground {
        emptyenv -c s6-setsid aa-ctty -O3 -s aa-stop -O3 -aak uncaught-logs -t20
    }
    if { aa-test -e /run/services/.anopa/post-stop }
    importas -u RC ? emptyenv -c /run/services/.anopa/post-stop $RC
}
# Now stop the catch-all logger (its supervisor will then exit)
foreground { emptyenv -c s6-setsid aa-stop -O3 uncaught-logs }

# Kill everything left
foreground { aa-echo -O3 -B "Killing remaining processes..." }
foreground { aa-echo -O3 -t "Sending SIGTERM to all processes..." }
foreground { aa-kill -O3 -st }
wait -t 4200 { } # Reap zombies
foreground { aa-echo -O3 -t "Sending SIGKILL to all processes..." }
foreground { aa-kill -O3 -sk }
wait -t 2300 { } # Wait for all children

foreground { aa-sync }
foreground { aa-echo -O3 -B "Pivoting root..." }

cd /run/initramfs
foreground {
    if -n
    {
        # grab the chroot binary for after the pivot
        if { cp @BINDIR@/aa-chroot . }
        if { cp @BINDIR@/fdclose . }
        # PIVOT!
        if { aa-pivot -O3 . root-fs }
    }

    # Something went wrong
    foreground { aa-echo -O3 -Be "Unable to ${1}: Failed to pivot root" }
    foreground { aa-echo -O3 -t "Trying to open a shell; " +g exit +w " to continue" }
    emptyenv -c sh -i
}
# close the logger now
./fdclose 3
# And let initramfs end things ($1 is halt/reboot/poweroff)
./aa-chroot . /shutdown ${1}