| 1 |
#! /bin/sh |
| 2 |
# |
| 3 |
# rc |
| 4 |
# |
| 5 |
# Starts/stops services on runlevel changes. |
| 6 |
# |
| 7 |
# Authors: |
| 8 |
# Miquel van Smoorenburg <[email protected]> |
| 9 |
# Bruce Perens <[email protected]> |
| 10 |
|
| 11 |
PATH=/sbin:/usr/sbin:/bin:/usr/bin |
| 12 |
export PATH |
| 13 |
|
| 14 |
# Un-comment the following for interactive debugging. Do not un-comment |
| 15 |
# this for debugging a real boot process as no scripts will be executed. |
| 16 |
# debug=echo |
| 17 |
|
| 18 |
# Make sure the name survive changing the argument list |
| 19 |
scriptname="$0" |
| 20 |
|
| 21 |
umask 022 |
| 22 |
|
| 23 |
on_exit() { |
| 24 |
echo "error: '$scriptname' exited outside the expected code flow." |
| 25 |
} |
| 26 |
trap on_exit EXIT # Enable emergency handler |
| 27 |
|
| 28 |
# Ignore CTRL-C only in this shell, so we can interrupt subprocesses. |
| 29 |
trap ":" INT QUIT TSTP |
| 30 |
|
| 31 |
# Set opost and onlcr to avoid staircase effect. |
| 32 |
stty opost onlcr 0>&1 |
| 33 |
|
| 34 |
# Now find out what the current and what the previous runlevel are. |
| 35 |
|
| 36 |
# shellcheck disable=SC2153 # this variable is set by /sbin/init |
| 37 |
runlevel=$RUNLEVEL |
| 38 |
|
| 39 |
# Get first argument. Set new runlevel to this argument. |
| 40 |
[ "$1" != "" ] && runlevel=$1 |
| 41 |
if [ "$runlevel" = "" ]; then |
| 42 |
echo "Usage: $scriptname <runlevel>" >&2 |
| 43 |
exit 1 |
| 44 |
fi |
| 45 |
previous=$PREVLEVEL |
| 46 |
[ "$previous" = "" ] && previous=N |
| 47 |
|
| 48 |
export runlevel previous |
| 49 |
|
| 50 |
if [ -f /etc/default/rcS ]; then |
| 51 |
. /etc/default/rcS |
| 52 |
fi |
| 53 |
export VERBOSE |
| 54 |
|
| 55 |
. /lib/lsb/init-functions |
| 56 |
|
| 57 |
# |
| 58 |
# Check if we are able to use make like booting. It require the |
| 59 |
# insserv package to be enabled. Boot concurrency also requires |
| 60 |
# startpar to be installed. |
| 61 |
# |
| 62 |
CONCURRENCY=makefile |
| 63 |
[ -s /etc/init.d/.depend.boot ] || CONCURRENCY="none" |
| 64 |
[ -s /etc/init.d/.depend.start ] || CONCURRENCY="none" |
| 65 |
[ -s /etc/init.d/.depend.stop ] || CONCURRENCY="none" |
| 66 |
if [ -e /etc/init.d/.legacy-bootordering ]; then |
| 67 |
CONCURRENCY="none" |
| 68 |
fi |
| 69 |
if grep -wqs concurrency=none /proc/cmdline; then |
| 70 |
CONCURRENCY="none" |
| 71 |
fi |
| 72 |
if [ ! -e /proc/stat ]; then |
| 73 |
# startpar requires /proc/stat |
| 74 |
if [ "$(uname)" = "GNU/kFreeBSD" ]; then |
| 75 |
mount -t linprocfs linprocfs /proc |
| 76 |
elif [ "$(uname)" = "GNU" ]; then |
| 77 |
mount -t proc none /proc -ocompatible |
| 78 |
else |
| 79 |
mount -t proc none /proc |
| 80 |
fi |
| 81 |
fi |
| 82 |
|
| 83 |
# |
| 84 |
# Start script or program. |
| 85 |
# |
| 86 |
case "$CONCURRENCY" in |
| 87 |
makefile | startpar | shell) # startpar and shell are obsolete |
| 88 |
CONCURRENCY=makefile |
| 89 |
log_action_msg "Using makefile-style concurrent boot in runlevel $runlevel" |
| 90 |
startup() { |
| 91 |
eval "$(startpar -p 4 -t 20 -T 3 -M $1 -P $previous -R $runlevel)" |
| 92 |
|
| 93 |
if [ -n "$failed_service" ]; then |
| 94 |
log_failure_msg "startpar: service(s) returned failure: $failed_service" |
| 95 |
fi |
| 96 |
|
| 97 |
if [ -n "$skipped_service_not_installed" ]; then |
| 98 |
log_warning_msg "startpar: service(s) skipped, program is not installed: $skipped_service_not_installed" |
| 99 |
fi |
| 100 |
|
| 101 |
if [ -n "$skipped_service_not_configured" ]; then |
| 102 |
log_warning_msg "startpar: service(s) skipped, program is not configured: $skipped_service_not_configured" |
| 103 |
fi |
| 104 |
|
| 105 |
unset failed_service skipped_service_not_installed skipped_service_not_configured |
| 106 |
} |
| 107 |
;; |
| 108 |
none | *) |
| 109 |
startup() { |
| 110 |
action=$1 |
| 111 |
shift |
| 112 |
for script; do |
| 113 |
"$script" "$action" |
| 114 |
done |
| 115 |
} |
| 116 |
;; |
| 117 |
esac |
| 118 |
|
| 119 |
# Is there an rc directory for this new runlevel? |
| 120 |
if [ -d "/etc/rc$runlevel.d" ]; then |
| 121 |
case "$runlevel" in |
| 122 |
0 | 6) |
| 123 |
ACTION=stop |
| 124 |
;; |
| 125 |
S) |
| 126 |
ACTION=start |
| 127 |
;; |
| 128 |
*) |
| 129 |
ACTION=start |
| 130 |
;; |
| 131 |
esac |
| 132 |
|
| 133 |
# First, run the KILL scripts. |
| 134 |
if [ makefile = "$CONCURRENCY" ]; then |
| 135 |
if [ "$ACTION" = "start" ] && [ "$previous" != N ]; then |
| 136 |
startup stop |
| 137 |
fi |
| 138 |
elif [ "$previous" != N ]; then |
| 139 |
CURLEVEL="" |
| 140 |
for s in "/etc/rc$runlevel.d/K"*; do |
| 141 |
# Extract order value from symlink |
| 142 |
level=${s#/etc/rc$runlevel.d/K} |
| 143 |
level=${level%%[a-zA-Z]*} |
| 144 |
if [ "$level" = "$CURLEVEL" ]; then |
| 145 |
continue |
| 146 |
fi |
| 147 |
CURLEVEL=$level |
| 148 |
SCRIPTS="" |
| 149 |
for i in "/etc/rc$runlevel.d/K$level"*; do |
| 150 |
# Check if the script is there. |
| 151 |
[ ! -f "$i" ] && continue |
| 152 |
|
| 153 |
# Stop the service. |
| 154 |
SCRIPTS="$SCRIPTS $i" |
| 155 |
done |
| 156 |
startup stop $SCRIPTS |
| 157 |
done |
| 158 |
fi |
| 159 |
|
| 160 |
if [ makefile = "$CONCURRENCY" ]; then |
| 161 |
if [ S = "$runlevel" ]; then |
| 162 |
startup boot |
| 163 |
else |
| 164 |
startup $ACTION |
| 165 |
fi |
| 166 |
else |
| 167 |
# Now run the START scripts for this runlevel. |
| 168 |
CURLEVEL="" |
| 169 |
for s in "/etc/rc$runlevel.d/S"*; do |
| 170 |
# Extract order value from symlink |
| 171 |
level=${s#/etc/rc$runlevel.d/S} |
| 172 |
level=${level%%[a-zA-Z]*} |
| 173 |
if [ "$level" = "$CURLEVEL" ]; then |
| 174 |
continue |
| 175 |
fi |
| 176 |
CURLEVEL=$level |
| 177 |
SCRIPTS="" |
| 178 |
for i in "/etc/rc$runlevel.d/S$level"*; do |
| 179 |
[ ! -f "$i" ] && continue |
| 180 |
|
| 181 |
SCRIPTS="$SCRIPTS $i" |
| 182 |
done |
| 183 |
startup $ACTION $SCRIPTS |
| 184 |
done |
| 185 |
fi |
| 186 |
fi |
| 187 |
|
| 188 |
trap - EXIT # Disable emergency handler |
| 189 |
|
| 190 |
exit 0 |