source: bootscripts/lfs/init.d/rc@ 27d23b1

11.2 11.2-rc1 11.3 11.3-rc1 12.0 12.0-rc1 12.1 12.1-rc1 bdubbs/gcc13 multilib renodr/libudev-from-systemd s6-init trunk xry111/arm64 xry111/arm64-12.0 xry111/clfs-ng xry111/loongarch xry111/loongarch-12.0 xry111/loongarch-12.1 xry111/mips64el xry111/pip3 xry111/rust-wip-20221008 xry111/update-glibc
Last change on this file since 27d23b1 was 27d23b1, checked in by Pierre Labastie <pierre.labastie@…>, 2 years ago

Change semantics of S and K files

Presently, there are a lot of special cases:

  • runlevel 0 and 6 unconditionally run "script stop" if they find a Kxxscript symlink. This may lead to trying to stop an already stopped device if for example switching to runlevel 0/6 from runlevel 1. This can be fixed by stating the convention that it is the responsability of scripts to check that the service is running before killing it (or not running before starting it). Still, we shouldn't try to stop a service if it was marked K in the previous runlevel. And same for S files: we shouldn't try to start a service that was marked S in the previous runlevel. Note that changing runlevel is not a "reset": if a user has manually changed the state of a daemon, this state will remain the same upon changing runlevel if the S/K status of that dameon is the same in both runlevels.
  • Sxxscript symlinks in runlevel 0/6 are run as "script stop" instead of the more intuitive "script start". This does not interact well with LSB-tools (some scripts would need "Default-Start: S 0 6" but then it is impossible to get correct "Required-Start" or "Should-Start" fields). Furthermore, having a counter-intuitive behavior is error prone. So now runlevel 0/6 will run "script sart" for a Sxxscript.
  • Property mode set to 100644
File size: 6.2 KB
Line 
1#!/bin/bash
2########################################################################
3# Begin rc
4#
5# Description : Main Run Level Control Script
6#
7# Authors : Gerard Beekmans - gerard@linuxfromscratch.org
8# : DJ Lucas - dj@linuxfromscratch.org
9# Updates : Bruce Dubbs - bdubbs@linuxfromscratch.org
10# : Pierre Labastie - pierre@linuxfromscratch.org
11#
12# Version : LFS 7.0
13#
14# Notes : Updates March 24th, 2022: new semantics of S/K files
15# - Instead of testing that S scripts were K scripts in the
16# previous runlevel, test that they were not S scripts
17# - Instead of testing that K scripts were S scripts in the
18# previous runlevel, test that they were not K scripts
19# - S scripts in runlevel 0 or 6 are now run with
20# "script start" (was "script stop" previously).
21########################################################################
22
23. /lib/lsb/init-functions
24
25print_error_msg()
26{
27 log_failure_msg
28 # $i is set when called
29 MSG="FAILURE:\n\nYou should not be reading this error message.\n\n"
30 MSG="${MSG}It means that an unforeseen error took place in\n"
31 MSG="${MSG}${i},\n"
32 MSG="${MSG}which exited with a return value of ${error_value}.\n"
33
34 MSG="${MSG}If you're able to track this error down to a bug in one of\n"
35 MSG="${MSG}the files provided by the ${DISTRO_MINI} book,\n"
36 MSG="${MSG}please be so kind to inform us at ${DISTRO_CONTACT}.\n"
37 log_failure_msg "${MSG}"
38
39 log_info_msg "Press Enter to continue..."
40 wait_for_user
41}
42
43check_script_status()
44{
45 # $i is set when called
46 if [ ! -f ${i} ]; then
47 log_warning_msg "${i} is not a valid symlink."
48 SCRIPT_STAT="1"
49 fi
50
51 if [ ! -x ${i} ]; then
52 log_warning_msg "${i} is not executable, skipping."
53 SCRIPT_STAT="1"
54 fi
55}
56
57run()
58{
59 if [ -z $interactive ]; then
60 ${1} ${2}
61 return $?
62 fi
63
64 while true; do
65 read -p "Run ${1} ${2} (Yes/no/continue)? " -n 1 runit
66 echo
67
68 case ${runit} in
69 c | C)
70 interactive=""
71 ${i} ${2}
72 ret=${?}
73 break;
74 ;;
75
76 n | N)
77 return 0
78 ;;
79
80 y | Y)
81 ${i} ${2}
82 ret=${?}
83 break
84 ;;
85 esac
86 done
87
88 return $ret
89}
90
91# Read any local settings/overrides
92[ -r /etc/sysconfig/rc.site ] && source /etc/sysconfig/rc.site
93
94DISTRO=${DISTRO:-"Linux From Scratch"}
95DISTRO_CONTACT=${DISTRO_CONTACT:-"lfs-dev@lists.linuxfromscratch.org (Registration required)"}
96DISTRO_MINI=${DISTRO_MINI:-"LFS"}
97IPROMPT=${IPROMPT:-"no"}
98
99# These 3 signals will not cause our script to exit
100trap "" INT QUIT TSTP
101
102[ "${1}" != "" ] && runlevel=${1}
103
104if [ "${runlevel}" == "" ]; then
105 echo "Usage: ${0} <runlevel>" >&2
106 exit 1
107fi
108
109previous=${PREVLEVEL}
110[ "${previous}" == "" ] && previous=N
111
112if [ ! -d /etc/rc.d/rc${runlevel}.d ]; then
113 log_info_msg "/etc/rc.d/rc${runlevel}.d does not exist.\n"
114 exit 1
115fi
116
117if [ "$runlevel" == "6" -o "$runlevel" == "0" ]; then IPROMPT="no"; fi
118
119# Note: In ${LOGLEVEL:-7}, it is ':' 'dash' '7', not minus 7
120if [ "$runlevel" == "S" ]; then
121 [ -r /etc/sysconfig/console ] && source /etc/sysconfig/console
122 dmesg -n "${LOGLEVEL:-7}"
123fi
124
125if [ "${IPROMPT}" == "yes" -a "${runlevel}" == "S" ]; then
126 # The total length of the distro welcome string, without escape codes
127 wlen=${wlen:-$(echo "Welcome to ${DISTRO}" | wc -c )}
128 welcome_message=${welcome_message:-"Welcome to ${INFO}${DISTRO}${NORMAL}"}
129
130 # The total length of the interactive string, without escape codes
131 ilen=${ilen:-$(echo "Press 'I' to enter interactive startup" | wc -c )}
132 i_message=${i_message:-"Press '${FAILURE}I${NORMAL}' to enter interactive startup"}
133
134
135 # dcol and icol are spaces before the message to center the message
136 # on screen. itime is the amount of wait time for the user to press a key
137 wcol=$(( ( ${COLUMNS} - ${wlen} ) / 2 ))
138 icol=$(( ( ${COLUMNS} - ${ilen} ) / 2 ))
139 itime=${itime:-"3"}
140
141 echo -e "\n\n"
142 echo -e "\\033[${wcol}G${welcome_message}"
143 echo -e "\\033[${icol}G${i_message}${NORMAL}"
144 echo ""
145 read -t "${itime}" -n 1 interactive 2>&1 > /dev/null
146fi
147
148# Make lower case
149[ "${interactive}" == "I" ] && interactive="i"
150[ "${interactive}" != "i" ] && interactive=""
151
152# Read the state file if it exists from runlevel S
153[ -r /run/interactive ] && source /run/interactive
154
155# Stop all services marked as K, except if marked as K in the previous
156# runlevel: it is the responsibility of the script to not try to kill
157# a non running service
158if [ "${previous}" != "N" ]; then
159 for i in $(ls -v /etc/rc.d/rc${runlevel}.d/K* 2> /dev/null)
160 do
161 check_script_status
162 if [ "${SCRIPT_STAT}" == "1" ]; then
163 SCRIPT_STAT="0"
164 continue
165 fi
166
167 suffix=${i#/etc/rc.d/rc${runlevel}.d/K[0-9][0-9]}
168 [ -e /etc/rc.d/rc${previous}.d/K[0-9][0-9]$suffix ] && continue
169
170 run ${i} stop
171 error_value=${?}
172
173 if [ "${error_value}" != "0" ]; then print_error_msg; fi
174 done
175fi
176
177if [ "${previous}" == "N" ]; then export IN_BOOT=1; fi
178
179if [ "$runlevel" == "6" -a -n "${FASTBOOT}" ]; then
180 touch /fastboot
181fi
182
183
184# Start all services marked as S in this runlevel, except if marked as
185# S in the previous runlevel
186# it is the responsabily of the script to not try to start an already running
187# service
188for i in $( ls -v /etc/rc.d/rc${runlevel}.d/S* 2> /dev/null)
189do
190
191 if [ "${previous}" != "N" ]; then
192 suffix=${i#/etc/rc.d/rc${runlevel}.d/S[0-9][0-9]}
193 [ -e /etc/rc.d/rc${previous}.d/S[0-9][0-9]$suffix ] && continue
194 fi
195
196 check_script_status
197 if [ "${SCRIPT_STAT}" == "1" ]; then
198 SCRIPT_STAT="0"
199 continue
200 fi
201
202 run ${i} start
203
204 error_value=${?}
205
206 if [ "${error_value}" != "0" ]; then print_error_msg; fi
207done
208
209# Store interactive variable on switch from runlevel S and remove if not
210if [ "${runlevel}" == "S" -a "${interactive}" == "i" ]; then
211 echo "interactive=\"i\"" > /run/interactive
212else
213 rm -f /run/interactive 2> /dev/null
214fi
215
216# Copy the boot log on initial boot only
217if [ "${previous}" == "N" -a "${runlevel}" != "S" ]; then
218 cat $BOOTLOG >> /var/log/boot.log
219
220 # Mark the end of boot
221 echo "--------" >> /var/log/boot.log
222
223 # Remove the temporary file
224 rm -f $BOOTLOG 2> /dev/null
225fi
226
227# End rc
Note: See TracBrowser for help on using the repository browser.