summaryrefslogtreecommitdiff
path: root/core/runit/runit-3
blob: f4747dfe99a5be1ff8c01038130b9635456038ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/bin/sh
# /etc/runit/3 - rawnix system shutdown

exec 2>&1
PATH=/sbin:/bin:/usr/sbin:/usr/bin
export PATH
stty onlcr 2>/dev/null

printf '\nrawnix is shutting down...\n\n'

# Close SSH sessions while the network is still up, so clients get a FIN
# instead of hanging until TCP timeout.  sv only stops the sshd listener;
# each login is a separate sshd-session process (OpenSSH >= 9.8).
if pkill -TERM -x sshd-session 2>/dev/null; then
    printf 'Closing SSH sessions...\n'
    sleep 1
fi

printf 'Stopping services...\n'
sv force-stop /service/* 2>/dev/null
sv exit /service/* 2>/dev/null

[ -x /etc/rc.shutdown ] && /etc/rc.shutdown

printf 'Saving system clock...\n'
hwclock --systohc 2>/dev/null

printf 'Saving urandom seed...\n'
mkdir -p /var/lib/random
chmod 700 /var/lib/random
dd if=/dev/urandom of=/var/lib/random/seed bs=512 count=1 2>/dev/null
chmod 600 /var/lib/random/seed

printf 'Sending TERM signal...\n'
kill -TERM -1 2>/dev/null
sleep 2

printf 'Sending KILL signal...\n'
kill -KILL -1 2>/dev/null
sleep 1

printf 'Unmounting filesystems...\n'
sync
umount -a -d -r -t nosysfs,noproc,nodevtmpfs,notmpfs 2>/dev/null

printf 'Closing LUKS volumes...\n'
# Only what stage 1 opened.  Root is absent from crypttab by design, so
# the mapping the filesystem is still running from is never touched.
if command -v cryptsetup >/dev/null 2>&1 && [ -r /etc/crypttab ]; then
    while read -r name _rest <&3; do
        case "$name" in ''|\#*) continue ;; esac
        [ -e "/dev/mapper/$name" ] || continue
        cryptsetup close "$name" 2>/dev/null
    done 3< /etc/crypttab
fi

printf 'Remounting root read-only...\n'
sync
mount -o remount,ro / 2>/dev/null

swapoff -a 2>/dev/null

printf 'Shutdown complete.\n'
exit 0