#! /bin/sh
PATH=/sbin:/usr/sbin:/bin:/usr/bin #设置环境变量
export PATH
CONCURRENCY=makefile
scriptname="$0" #$0表示传入的第一个参数,如执行 ./rc 1 2 第一个参数是./rc,第二个是1。
umask 022 #设置当前目录默认文件权限
on_exit() { #函数
echo "error: '$scriptname' exited outside the expected code flow."
}
trap on_exit EXIT #若遇到EXIT信号则执行on_exit()函数
trap ":" INT QUIT TSTP #若遇到INT等信号则执行空语句(冒号表示空语句)
stty onlcr 0>&1 #设置终端命令行,标准输入重定向到标准输出(0:标准输入;1:标准输出;2:标准错误输出)
if [ -e /lib/init/splash-functions-base ] ; then #若存在xx文件
. /lib/init/splash-functions-base #执行脚本(可以理解为执行脚本./splash-functions-base类似语法结构)
else
splash_progress() { return 1; } #
splash_stop() { return 1; } #
fi
runlevel=$RUNLEVEL
[ "$1" != "" ] && runlevel=$1 #若有第2个参数,则赋值给变量runlevel(运行级别)
if [ "$runlevel" = "" ]
then
echo "Usage: $scriptname <runlevel>" >&2 #标准输出重定向到标准错误输出
exit 1
fi
previous=$PREVLEVEL
[ "$previous" = "" ] && previous=N
export runlevel previous #设置为环境变量(当前shell有效)
if [ -f /etc/default/rcS ] ; then #是普通文件则运行(非目录、设备文件等)
. /etc/default/rcS
fi
export VERBOSE
if [ -f /lib/lsb/init-functions ] ; then #是普通文件则运行(非目录、设备文件等)
. /lib/lsb/init-functions
else
log_action_msg() { echo $@; } #$@显示所有向此脚本传递的参数
log_failure_msg() { echo $@; }
log_warning_msg() { echo $@; }
fi
startup_progress() {
if [ 0 -eq "$num_steps" ] ; then return; fi
step=$(($step + $step_change))
progress=$(($step * $progress_size / $num_steps + $first_step))
$debug splash_progress "$progress" || true
}
if [ "none" != "$CONCURRENCY" ] ; then
test -s /etc/init.d/.depend.boot || CONCURRENCY="none"
test -s /etc/init.d/.depend.start || CONCURRENCY="none"
test -s /etc/init.d/.depend.stop || CONCURRENCY="none"
if test -e /etc/init.d/.legacy-bootordering ; then
CONCURRENCY="none"
fi
startpar -v > /dev/null 2>&1 || CONCURRENCY="none"
fi
case "$CONCURRENCY" in
makefile|startpar|shell) # startpar and shell are obsolete
CONCURRENCY=makefile
log_action_msg "Using makefile-style concurrent boot in runlevel $runlevel"
startup() {
if [ start = "$1" ] || [ boot = "$1" ]
then
$debug splash_stop || true
fi
eval "$(startpar -p 4 -t 20 -T 3 -M $1 -P $previous -R $runlevel)"
if [ -n "$failed_service" ]
then
log_failure_msg "startpar: service(s) returned failure: $failed_service"
fi
if [ -n "$skipped_service" ]
then
log_warning_msg "startpar: service(s) skipped: $skipped_service"
fi
unset failed_service skipped_service
}
;;
none|*)
startup() {
action=$1
shift
scripts="$@"
for script in $scripts ; do
$debug "$script" $action
startup_progress
done
}
;;
esac
is_splash_stop_scripts() {
scriptname=$1
case "$scriptname" in
# killprocs is used in runlevel 1
gdm|xdm|kdm|ltsp-client|ltsp-client-core|reboot|halt|killprocs)
return 0
;;
esac
return 1
}
if [ -d /etc/rc$runlevel.d ]
then
PROGRESS_STATE=0
if [ -f /dev/.initramfs/progress_state ]; then
. /dev/.initramfs/progress_state
fi
progress_size=$(((100 - $PROGRESS_STATE) / 3))
case "$runlevel" in
0|6)
ACTION=stop
first_step=0
progress_size=100
step_change=-1
;;
S)
ACTION=start
first_step=$PROGRESS_STATE
progress_size=$(($progress_size * 2))
step_change=1
;;
*)
ACTION=start
first_step=$(($progress_size * 2 + $PROGRESS_STATE))
step_change=1
;;
esac
num_steps=0
for s in /etc/rc$runlevel.d/[SK]*; do
if is_splash_stop_scripts "${s##/etc/rc$runlevel.d/S??}" ; then
break
fi
num_steps=$(($num_steps + 1))
done
step=0
if [ makefile = "$CONCURRENCY" ]
then
if [ "$ACTION" = "start" ] && [ "$previous" != N ]
then
startup stop
fi
elif [ "$previous" != N ]
then
# Run all scripts with the same level in parallel
CURLEVEL=""
for s in /etc/rc$runlevel.d/K*
do
# Extract order value from symlink
level=${s#/etc/rc$runlevel.d/K}
level=${level%%[a-zA-Z]*}
if [ "$level" = "$CURLEVEL" ]
then
continue
fi
CURLEVEL=$level
SCRIPTS=""
for i in /etc/rc$runlevel.d/K$level*
do
# Check if the script is there.
[ ! -f $i ] && continue
#
# Find stop script in previous runlevel but
# no start script there.
#
suffix=${i#/etc/rc$runlevel.d/K[0-9][0-9]}
previous_stop=/etc/rc$previous.d/K[0-9][0-9]$suffix
previous_start=/etc/rc$previous.d/S[0-9][0-9]$suffix
#
# If there is a stop script in the previous level
# and _no_ start script there, we don't
# have to re-stop the service.
#
[ -f $previous_stop ] && [ ! -f $previous_start ] && continue
# Stop the service.
SCRIPTS="$SCRIPTS $i"
if is_splash_stop_scripts "$suffix" ; then
$debug splash_stop || true
fi
done
startup stop $SCRIPTS
done
fi
if [ makefile = "$CONCURRENCY" ]
then
if [ S = "$runlevel" ]
then
startup boot
else
startup $ACTION
fi
else
# Now run the START scripts for this runlevel.
# Run all scripts with the same level in parallel
CURLEVEL=""
for s in /etc/rc$runlevel.d/S*
do
# Extract order value from symlink
level=${s#/etc/rc$runlevel.d/S}
level=${level%%[a-zA-Z]*}
if [ "$level" = "$CURLEVEL" ]
then
continue
fi
CURLEVEL=$level
SCRIPTS=""
for i in /etc/rc$runlevel.d/S$level*
do
[ ! -f $i ] && continue
suffix=${i#/etc/rc$runlevel.d/S[0-9][0-9]}
if [ "$previous" != N ]
then
#
# Find start script in previous runlevel and
# stop script in this runlevel.
#
stop=/etc/rc$runlevel.d/K[0-9][0-9]$suffix
previous_start=/etc/rc$previous.d/S[0-9][0-9]$suffix
#
# If there is a start script in the previous level
# and _no_ stop script in this level, we don't
# have to re-start the service.
#
if [ start = "$ACTION" ] ; then
[ -f $previous_start ] && [ ! -f $stop ] && continue
else
# Workaround for the special
# handling of runlevels 0 and 6.
previous_stop=/etc/rc$previous.d/K[0-9][0-9]$suffix
#
# If there is a stop script in the previous level
# and _no_ start script there, we don't
# have to re-stop the service.
#
[ -f $previous_stop ] && [ ! -f $previous_start ] && continue
fi
fi
SCRIPTS="$SCRIPTS $i"
if is_splash_stop_scripts "$suffix" ; then
$debug splash_stop || true
fi
done
startup $ACTION $SCRIPTS
done
fi
fi
trap - EXIT # Disable emergency handler
exit 0
/etc/init.d/rc文件语法解析
最新推荐文章于 2024-06-11 17:46:34 发布