xref: /OK3568_Linux_fs/yocto/poky/meta/recipes-core/sysvinit/sysvinit/rc (revision 4882a59341e53eb6f0b4789bf948001014eff981)
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
19export VERBOSE
20export PSPLASH_FIFO_DIR
21
22startup_progress() {
23    step=$(($step + $step_change))
24    if [ "$num_steps" != "0" ]; then
25        progress=$((($step * $progress_size / $num_steps) + $first_step))
26    else
27        progress=$progress_size
28    fi
29    #echo "PROGRESS is $progress $runlevel $first_step + ($step of $num_steps) $step_change $progress_size"
30    if type psplash-write >/dev/null 2>&1; then
31        psplash-write "PROGRESS $progress" || true
32    fi
33}
34
35
36#
37# Start script or program.
38#
39startup() {
40  # Handle verbosity
41  [ "$VERBOSE" = very ] && echo "INIT: Running $@..."
42
43  case "$1" in
44	*.sh)
45		# Source shell script for speed.
46		(
47			trap - INT QUIT TSTP
48			scriptname=$1
49			shift
50			. $scriptname
51		)
52		;;
53	*)
54		"$@"
55		;;
56  esac
57  startup_progress
58}
59
60  # Ignore CTRL-C only in this shell, so we can interrupt subprocesses.
61  trap ":" INT QUIT TSTP
62
63  # Set onlcr to avoid staircase effect.
64  stty onlcr 0>&1
65
66  # Limit stack size for startup scripts
67  [ "$STACK_SIZE" = "" ] || ulimit -S -s $STACK_SIZE
68
69  # Now find out what the current and what the previous runlevel are.
70
71  runlevel=$RUNLEVEL
72  # Get first argument. Set new runlevel to this argument.
73  [ "$1" != "" ] && runlevel=$1
74  if [ "$runlevel" = "" ]
75  then
76	echo "Usage: $0 <runlevel>" >&2
77	exit 1
78  fi
79  previous=$PREVLEVEL
80  [ "$previous" = "" ] && previous=N
81
82  export runlevel previous
83
84  # Is there an rc directory for this new runlevel?
85  if [ -d /etc/rc$runlevel.d ]
86  then
87	# Find out where in the progress bar the initramfs got to.
88	PROGRESS_STATE=0
89	#if [ -f /dev/.initramfs/progress_state ]; then
90	#    . /dev/.initramfs/progress_state
91	#fi
92
93	# Split the remaining portion of the progress bar into thirds
94	progress_size=$(((100 - $PROGRESS_STATE) / 3))
95
96	case "$runlevel" in
97		0|6)
98			# Count down from -100 to 0 and use the entire bar
99			first_step=-100
100			progress_size=100
101			step_change=1
102			;;
103	        S)
104			# Begin where the initramfs left off and use 2/3
105			# of the remaining space
106			first_step=$PROGRESS_STATE
107			progress_size=$(($progress_size * 2))
108			step_change=1
109			;;
110		*)
111			# Begin where rcS left off and use the final 1/3 of
112			# the space (by leaving progress_size unchanged)
113			first_step=$(($progress_size * 2 + $PROGRESS_STATE))
114			step_change=1
115			;;
116	esac
117
118	num_steps=0
119	for s in /etc/rc$runlevel.d/[SK]*; do
120            case "${s##/etc/rc$runlevel.d/S??}" in
121                gdm|xdm|kdm|reboot|halt)
122                    break
123                    ;;
124            esac
125            num_steps=$(($num_steps + 1))
126        done
127        step=0
128
129	# First, run the KILL scripts.
130	if [ $previous != N ]
131	then
132		for i in /etc/rc$runlevel.d/K[0-9][0-9]*
133		do
134			# Check if the script is there.
135			[ ! -f $i ] && continue
136
137			# Stop the service.
138			startup $i stop
139		done
140	fi
141
142	# Now run the START scripts for this runlevel.
143	for i in /etc/rc$runlevel.d/S*
144	do
145		[ ! -f $i ] && continue
146
147		if [ $previous != N ] && [ $previous != S ]
148		then
149			#
150			# Find start script in previous runlevel and
151			# stop script in this runlevel.
152			#
153			suffix=${i#/etc/rc$runlevel.d/S[0-9][0-9]}
154			stop=/etc/rc$runlevel.d/K[0-9][0-9]$suffix
155			previous_start=/etc/rc$previous.d/S[0-9][0-9]$suffix
156			#
157			# If there is a start script in the previous level
158			# and _no_ stop script in this level, we don't
159			# have to re-start the service.
160			#
161			[ -f $previous_start ] && [ ! -f $stop ] && continue
162		fi
163		if [ x"${PSPLASH_TEXT_UPDATES}" = x"yes" ]; then
164			psplash-write "MSG $(basename $i .sh | cut -c 4-)" || true
165		fi
166		case "$runlevel" in
167			0|6)
168				startup $i stop
169				;;
170			*)
171				startup $i start
172				;;
173		esac
174	done
175  fi
176
177#Uncomment to cause psplash to exit manually, otherwise it exits when it sees a VC switch
178if [ "x$runlevel" != "xS" ] && [ ! -x /etc/rc${runlevel}.d/S??xserver-nodm ]; then
179    if type psplash-write >/dev/null 2>&1; then
180        psplash-write "QUIT" || true
181    fi
182fi
183