Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # |
| 3 | # rc This file is responsible for starting/stopping |
| 4 | # services when the runlevel changes. |
| 5 | # |
| 6 | # Optimization feature: |
| 7 | # A startup script is _not_ run when the service was |
| 8 | # running in the previous runlevel and it wasn't stopped |
| 9 | # in the runlevel transition (most Debian services don't |
| 10 | # have K?? links in rc{1,2,3,4,5} ) |
| 11 | # |
| 12 | # Author: Miquel van Smoorenburg <miquels@cistron.nl> |
| 13 | # Bruce Perens <Bruce@Pixar.com> |
| 14 | # |
| 15 | # Version: @(#)rc 2.78 07-Nov-1999 miquels@cistron.nl |
| 16 | # |
| 17 | |
| 18 | . /etc/default/rcS |
| 19 | export VERBOSE |
| 20 | |
| 21 | startup_progress() { |
| 22 | step=$(($step + $step_change)) |
| 23 | if [ "$num_steps" != "0" ]; then |
| 24 | progress=$((($step * $progress_size / $num_steps) + $first_step)) |
| 25 | else |
| 26 | progress=$progress_size |
| 27 | fi |
| 28 | #echo "PROGRESS is $progress $runlevel $first_step + ($step of $num_steps) $step_change $progress_size" |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 29 | if type psplash-write >/dev/null 2>&1; then |
Andrew Geissler | 706d5aa | 2021-02-12 15:55:30 -0600 | [diff] [blame^] | 30 | PSPLASH_FIFO_DIR=/mnt/.psplash psplash-write "PROGRESS $progress" || true |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 31 | fi |
| 32 | } |
| 33 | |
| 34 | |
| 35 | # |
| 36 | # Start script or program. |
| 37 | # |
| 38 | startup() { |
| 39 | # Handle verbosity |
| 40 | [ "$VERBOSE" = very ] && echo "INIT: Running $@..." |
| 41 | |
| 42 | case "$1" in |
| 43 | *.sh) |
| 44 | # Source shell script for speed. |
| 45 | ( |
| 46 | trap - INT QUIT TSTP |
| 47 | scriptname=$1 |
| 48 | shift |
| 49 | . $scriptname |
| 50 | ) |
| 51 | ;; |
| 52 | *) |
| 53 | "$@" |
| 54 | ;; |
| 55 | esac |
| 56 | startup_progress |
| 57 | } |
| 58 | |
| 59 | # Ignore CTRL-C only in this shell, so we can interrupt subprocesses. |
| 60 | trap ":" INT QUIT TSTP |
| 61 | |
| 62 | # Set onlcr to avoid staircase effect. |
| 63 | stty onlcr 0>&1 |
| 64 | |
| 65 | # Limit stack size for startup scripts |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 66 | [ "$STACK_SIZE" = "" ] || ulimit -S -s $STACK_SIZE |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 67 | |
| 68 | # Now find out what the current and what the previous runlevel are. |
| 69 | |
| 70 | runlevel=$RUNLEVEL |
| 71 | # Get first argument. Set new runlevel to this argument. |
| 72 | [ "$1" != "" ] && runlevel=$1 |
| 73 | if [ "$runlevel" = "" ] |
| 74 | then |
| 75 | echo "Usage: $0 <runlevel>" >&2 |
| 76 | exit 1 |
| 77 | fi |
| 78 | previous=$PREVLEVEL |
| 79 | [ "$previous" = "" ] && previous=N |
| 80 | |
| 81 | export runlevel previous |
| 82 | |
| 83 | # Is there an rc directory for this new runlevel? |
| 84 | if [ -d /etc/rc$runlevel.d ] |
| 85 | then |
| 86 | # Find out where in the progress bar the initramfs got to. |
| 87 | PROGRESS_STATE=0 |
| 88 | #if [ -f /dev/.initramfs/progress_state ]; then |
| 89 | # . /dev/.initramfs/progress_state |
| 90 | #fi |
| 91 | |
| 92 | # Split the remaining portion of the progress bar into thirds |
| 93 | progress_size=$(((100 - $PROGRESS_STATE) / 3)) |
| 94 | |
| 95 | case "$runlevel" in |
| 96 | 0|6) |
| 97 | # Count down from -100 to 0 and use the entire bar |
| 98 | first_step=-100 |
| 99 | progress_size=100 |
| 100 | step_change=1 |
| 101 | ;; |
| 102 | S) |
| 103 | # Begin where the initramfs left off and use 2/3 |
| 104 | # of the remaining space |
| 105 | first_step=$PROGRESS_STATE |
| 106 | progress_size=$(($progress_size * 2)) |
| 107 | step_change=1 |
| 108 | ;; |
| 109 | *) |
| 110 | # Begin where rcS left off and use the final 1/3 of |
| 111 | # the space (by leaving progress_size unchanged) |
| 112 | first_step=$(($progress_size * 2 + $PROGRESS_STATE)) |
| 113 | step_change=1 |
| 114 | ;; |
| 115 | esac |
| 116 | |
| 117 | num_steps=0 |
| 118 | for s in /etc/rc$runlevel.d/[SK]*; do |
| 119 | case "${s##/etc/rc$runlevel.d/S??}" in |
| 120 | gdm|xdm|kdm|reboot|halt) |
| 121 | break |
| 122 | ;; |
| 123 | esac |
| 124 | num_steps=$(($num_steps + 1)) |
| 125 | done |
| 126 | step=0 |
| 127 | |
| 128 | # First, run the KILL scripts. |
| 129 | if [ $previous != N ] |
| 130 | then |
| 131 | for i in /etc/rc$runlevel.d/K[0-9][0-9]* |
| 132 | do |
| 133 | # Check if the script is there. |
| 134 | [ ! -f $i ] && continue |
| 135 | |
| 136 | # Stop the service. |
| 137 | startup $i stop |
| 138 | done |
| 139 | fi |
| 140 | |
| 141 | # Now run the START scripts for this runlevel. |
| 142 | for i in /etc/rc$runlevel.d/S* |
| 143 | do |
| 144 | [ ! -f $i ] && continue |
| 145 | |
| 146 | if [ $previous != N ] && [ $previous != S ] |
| 147 | then |
| 148 | # |
| 149 | # Find start script in previous runlevel and |
| 150 | # stop script in this runlevel. |
| 151 | # |
| 152 | suffix=${i#/etc/rc$runlevel.d/S[0-9][0-9]} |
| 153 | stop=/etc/rc$runlevel.d/K[0-9][0-9]$suffix |
| 154 | previous_start=/etc/rc$previous.d/S[0-9][0-9]$suffix |
| 155 | # |
| 156 | # If there is a start script in the previous level |
| 157 | # and _no_ stop script in this level, we don't |
| 158 | # have to re-start the service. |
| 159 | # |
| 160 | [ -f $previous_start ] && [ ! -f $stop ] && continue |
| 161 | fi |
| 162 | case "$runlevel" in |
| 163 | 0|6) |
| 164 | startup $i stop |
| 165 | ;; |
| 166 | *) |
| 167 | startup $i start |
| 168 | ;; |
| 169 | esac |
| 170 | done |
| 171 | fi |
| 172 | |
| 173 | #Uncomment to cause psplash to exit manually, otherwise it exits when it sees a VC switch |
| 174 | if [ "x$runlevel" != "xS" ] && [ ! -x /etc/rc${runlevel}.d/S??xserver-nodm ]; then |
| 175 | if type psplash-write >/dev/null 2>&1; then |
Andrew Geissler | 706d5aa | 2021-02-12 15:55:30 -0600 | [diff] [blame^] | 176 | PSPLASH_FIFO_DIR=/mnt/.psplash psplash-write "QUIT" || true |
| 177 | umount -l /mnt/.psplash |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 178 | fi |
| 179 | fi |