bootscripts: rc.shutdown: Replace shell process when reboot/halt
[dragora.git] / archive / etc / rc.d / rc.shutdown
blob1124d657bc81a36705d2fcd7b3cf10e50be87458
1 #! /bin/sh -
3 # Prepare to halt or reboot the system
5 # Copyright (c) 2017-2020 Matias Fonzo, <selk@dragora.org>.
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
11 # http://www.apache.org/licenses/LICENSE-2.0
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
19 umask 022
20 IFS='
22 PATH=/usr/local/sbin:/usr/local/bin:/sbin:/usr/sbin:/bin:/usr/bin
24 # Set linefeed mode to avoid staircase effect
25 stty onlcr 0>&1
27 # Be immune to the following signals
28 trap "" HUP INT QUIT ABRT TERM
30 # Save system clock
32 if grep -q '^RC_HWCLOCK=local' /etc/rc.conf
33 then
34 echo "(Shutdown) Saving hardware clock in local time"
35 hwclock --systohc --localtime
36 else
37 echo "(Shutdown) Saving hardware clock in UTC"
38 hwclock --systohc --utc
41 # Stop any quota system (if needed)
43 if egrep -q -m 1 '(usr|grp)quota' /etc/fstab
44 then
45 if type quotaoff > /dev/null
46 then
47 echo "(Shutdown) Set any quota file system to OFF"
48 quotaoff -va
52 # Save the random number generator, see random(4)
54 echo "(Shutdown) Saving random number generator"
55 echo " /dev/urandom <-> /etc/random-seed ..."
57 # Read pool size from /proc or assign a default value
58 read -r < /proc/sys/kernel/random/poolsize size || size=512
60 # Save seed file containing the whole entropy pool
61 dd if=/dev/urandom of=/etc/random-seed count=1 bs=${size} 2> /dev/null
62 unset size ; chmod 600 -- /etc/random-seed
64 # Try to write a login record (at /var/log/wtmp)
65 halt -w
67 # Flush file system buffers, update super block
68 sync
70 # Stop FUSE filesystem
71 if test -x /etc/rc.d/rc.fuse
72 then
73 /etc/rc.d/rc.fuse stop > /dev/null
76 # Kill all processes, except the one that is currently running
77 killall5 -15 -o $$
78 sleep 9
80 echo "(Shutdown) Deactivating swap devices"
81 swapoff -v -a
83 echo "(Shutdown) Unmounting local and remote file systems"
84 umount -v -a -r
85 sleep 1
86 sync
88 echo "(Shutdown) Remounting root filesystem in read-only mode"
89 mount -n -o remount,ro /
91 # Wait for completion of all the processes
92 wait
94 # Reboot, or halt the system
95 case $0 in
96 *.reboot)
97 echo "Rebooting ..."
98 exec reboot -d -f
101 exec halt -d -f -p
103 esac