1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
|
#!/sbin/runscript
# Copyright 1999-2005 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
trap ":" INT QUIT TSTP
source /sbin/functions.sh
umask 022
# Check that $1 exists ...
check_statedir() {
[ -z "$1" ] && return 0
if [ ! -d "$1" ] ; then
if ! mkdir -p "$1" &>/dev/null ; then
echo
eerror "For Gentoo to function properly, \"$1\" needs to exist."
einfo "Rebooting"
/sbin/reboot -f
fi
fi
return 0
}
get_critical_services() {
local x=
CRITICAL_SERVICES=
if [ -f "/etc/runlevels/${BOOTLEVEL}/.critical" ]
then
for x in $(< /etc/runlevels/${BOOTLEVEL}/.critical)
do
CRITICAL_SERVICES="${CRITICAL_SERVICES} ${x##*/}"
done
else
CRITICAL_SERVICES="hostname"
fi
export CRITICAL_SERVICES
return 0
}
# Save $1
argv1="$1"
# we need this to prevent a warning below
[[ ! -e ${svcdir}/softlevel ]] && touch ${svcdir}/softlevel
# First time boot stuff goes here. Note that 'sysinit' is an internal runlevel
# used to bring up local filesystems, and should not be started with /sbin/rc
# directly ...
if [[ ${argv1} = "sysinit" || ( ${argv1} = "boot" && "$(<${svcdir}/softlevel)" != "sysinit" ) ]]
then
# Setup initial $PATH just in case
PATH="/bin:/sbin:/usr/bin:/usr/sbin:${PATH}"
echo
echo -e "${GOOD}Gentoo Linux${GENTOO_VERS}; ${BRACKET}http://www.gentoo.org/${NORMAL}"
echo -e " Copyright 1999-2005 Gentoo Foundation; Distributed under the GPLv2"
echo
check_statedir /proc
check_statedir /dev
# We set the forced softlevel from the kernel command line
# It needs to be run right after proc is mounted for the
# boot runlevel
setup_defaultlevels
# $BOOT can be used by rc-scripts to test if it is the first time
# the 'boot' runlevel is executed. Now also needed by some stuff in
# the 'sysinit' runlevel ...
export BOOT="yes"
start_critical_service() {
(
local retval=
local service=$1
# Needed for some addons like dm-crypt that starts in critical services
local myservice=$1
source "/etc/init.d/${service}" || eerror "Failed to source /etc/init.d/${service}"
retval=$?
[ "${retval}" -ne 0 ] && return "${retval}"
[ -e "/etc/conf.d/${service}" ] && source "/etc/conf.d/${service}"
source /etc/rc.conf
start || eerror "Failed to start /etc/init.d/${service}"
retval=$?
return "${retval}"
)
}
# We first try to find a locally defined list of critical services
# for a particular runlevel. If we cannot find it, we use the
# defaults.
get_critical_services
export START_CRITICAL="yes"
# We do not want to break compatibility, so we do not fully integrate
# these into /sbin/rc, but rather start them by hand ...
for x in ${CRITICAL_SERVICES}
do
if ! start_critical_service "${x}" ; then
echo
eerror "One of more critical startup scripts failed to start!"
einfo "Rebooting"
/sbin/reboot -f
fi
done
unset START_CRITICAL
# Check that $svcdir exists ...
check_statedir "${svcdir}"
# Clear $svcdir from stale entries, but leave the caches around, as it
# should help speed things up a bit
rm -rf $(ls -d1 "${svcdir}/"* 2>/dev/null | \
grep -ve '\(depcache\|deptree\|envcache\)')
echo "sysinit" > "${svcdir}/softlevel"
# Update the dependency cache
/sbin/depscan.sh -u
# Now that the dependency cache are up to date, make sure these
# are marked as started ...
(
# Needed for mark_service_started()
source "${svclib}/sh/rc-services.sh"
for x in ${CRITICAL_SERVICES}
do
mark_service_started "${x}"
done
)
# Setup login records ... this has to be done here because when
# we exit this runlevel, init will write a boot record to utmp
# If /var/run is readonly, then print a warning, not errors
if touch /var/run/utmp 2>/dev/null
then
> /var/run/utmp
touch /var/log/wtmp
chgrp utmp /var/run/utmp /var/log/wtmp
chmod 0664 /var/run/utmp /var/log/wtmp
# Remove /var/run/utmpx (bug from the past)
rm -f /var/run/utmpx
else
ewarn "Skipping /var/run/utmp initialization (ro root?)"
fi
[[ ${argv1} = "boot" ]] || exit 0
fi # Sysinit ends here
if [[ ${argv1} == "boot" ]]
then
setup_defaultlevels
# $BOOT can be used by rc-scripts to test if it is the first time
# the 'boot' runlevel is executed
export BOOT="yes"
# We reset argv1 to the bootlevel given on the kernel command line
# if there is one
argv1="${BOOTLEVEL}"
fi
source "${svclib}/sh/rc-services.sh"
if [ -f "${svcdir}/softlevel" ]
then
# Set OLDSOFTLEVEL if we had a valid SOFTLEVEL
export OLDSOFTLEVEL="$(< ${svcdir}/softlevel)"
else
export OLDSOFTLEVEL=
fi
if [ -z "${argv1}" ]
then
if [ -f "${svcdir}/softlevel" ]
then
export SOFTLEVEL="$(< ${svcdir}/softlevel)"
else
export SOFTLEVEL="${BOOTLEVEL}"
fi
else
export SOFTLEVEL="${argv1}"
fi
if [ ! -f "${svcdir}/softlevel" ]
then
echo "${SOFTLEVEL}" > "${svcdir}/softlevel"
fi
# For keeping a list of services that fails during boot/halt
if [ ! -d "${svcdir}/failed" ]
then
mkdir -p -m 0755 "${svcdir}/failed"
else
rm -rf "${svcdir}"/failed/*
fi
if [ "${SOFTLEVEL}" = "reboot" -o "${SOFTLEVEL}" = "shutdown" ]
then
myscripts=
elif [ ! -d "/etc/runlevels/${SOFTLEVEL}" ]
then
eerror "ERROR: runlevel ${SOFTLEVEL} does not exist; exiting ..."
exit 1
else
myscripts=
if [ "${SOFTLEVEL}" != "${BOOTLEVEL}" ]
then
# Normal runlevels *include* boot scripts
mylevels="$(dolisting "/etc/runlevels/${SOFTLEVEL}/")"
mylevels="${mylevels} $(dolisting /etc/runlevels/${BOOTLEVEL}/)"
else
# Non-normal runlevels don't include boot scripts as default
mylevels="$(dolisting "/etc/runlevels/${SOFTLEVEL}/")"
fi
for x in ${mylevels}
do
[ -L "${x}" ] && myscripts="${myscripts} ${x##*/}"
done
fi
# The softscripts dir contains all scripts that belong to the
# runlevel specified in ${svcdir}/softlevel
# It needs to be a new directory, else when stopping the services
# and the old directory is not intact, things get broken
mkdir -p -m 0755 "${svcdir}/softscripts.new"
for x in ${myscripts} ; do
if [[ ! -e /etc/init.d/${x} ]] ; then
ewarn "WARNING: /etc/init.d/${x} missing; skipping ..."
continue
fi
# The -f eliminates a warning if the symlink already exists,
# which can happen if a service is in both the boot level and
# the current "normal" runlevel
ln -snf "/etc/init.d/${x}" "${svcdir}/softscripts.new/${x}"
done
get_stop_services() {
local x list
for x in $(dolisting "${svcdir}/inactive/") \
$(dolisting "${svcdir}/started/") ; do
list="${list} ${x##*/}"
done
reverse_list $(trace_dependencies ${list})
}
dep_stop() {
local x dep needsme depservice
local myservice=${1##*/}
service_stopped "${myservice}" && return 0
# Candidate for zapping ?
[[ ! -L ${svcdir}/softscripts.new/${myservice} ]] || \
return 0
# Should not work for 'use'
if [[ -z $(needsme "${myservice}") ]] ; then
# Nothing depends on me
stop_service "${myservice}"
else
# Something may depend on me
needsme=0
for dep in $(needsme "${myservice}") ; do
#if service_started "${dep}" && \
if [[ -L "${svcdir}/softscripts.new/${dep}" ]] ; then
# This dep is valid
needsme=1
break
fi
done
[[ ${needsme} -eq 0 ]] && stop_service "${myservice}"
fi
}
# Stop services
if [[ ${SOFTLEVEL} != "reboot" && \
${SOFTLEVEL} != "shutdown" ]]
then
for i in $(get_stop_services) ; do
dep_stop "${i}"
done
# Wait for any services that may still be stopping ...
[ "${RC_PARALLEL_STARTUP}" = "yes" ] && wait
else
get_critical_services
is_critical_service() {
local x
local myservice=${1##*/}
for x in ${CRITICAL_SERVICES} ${LOGGER_SERVICE} ; do
[[ ${myservice} == "${x}" ]] && return 0
done
return 1
}
# First stop non critical services
for i in $(get_stop_services) ; do
is_critical_service "${i}" || dep_stop "${i}"
done
# Wait for any services that may still be stopping ...
[ "${RC_PARALLEL_STARTUP}" = "yes" ] && wait
export STOP_CRITICAL="yes"
# Now stop the rest
for i in $(get_stop_services) ; do
dep_stop "${i}"
done
unset STOP_CRITICAL
fi
# Only change softlevel AFTER all the services have been stopped,
# else they will not get the depend's right (wrong SOFTLEVEL)
echo "${SOFTLEVEL}" > "${svcdir}/softlevel"
if [[ ${SOFTLEVEL} == "reboot" || ${SOFTLEVEL} == "shutdown" ]] ; then
source /sbin/functions.sh
# Clear $svcdir from stale entries, but leave the caches around, as it
# should help speed things up a bit
rm -rf $(ls -d1 "${svcdir}/"* 2>/dev/null | \
grep -ve '\(depcache\|deptree\|envcache\)')
source /etc/init.d/halt.sh
# we just die here since we have no init
# and we should be the last running process
if [[ ${RC_INITSTYLE_HACK} == "yes" ]] ; then
exit 0
fi
if [[ ${SOFTLEVEL} == "reboot" ]] ; then
source /etc/init.d/reboot.sh
else
source /etc/init.d/shutdown.sh
fi
# Should never get here
exit 0
fi
# Move the old softscritps directory to a different one
# and make the new softscripts directory the current
mv -f "${svcdir}/softscripts" "${svcdir}/softscripts.old"
mv -f "${svcdir}/softscripts.new" "${svcdir}/softscripts"
get_start_services() {
local x list
get_critical_services
list=${CRITICAL_SERVICES}
[[ -n ${LOGGER_SERVICE} && \
-L ${svcdir}/softscripts/${LOGGER_SERVICE} ]] && \
list="${list} ${LOGGER_SERVICE}"
for x in $(dolisting "${svcdir}/softscripts/") ; do
list="${list} ${x##*/}"
done
trace_dependencies ${list}
}
# Start scripts
for i in $(get_start_services) ; do
if service_stopped "${i}" ; then
start_service "${i}"
fi
done
# Wait for any services that may still be running ...
[ "${RC_PARALLEL_STARTUP}" = "yes" ] && wait
# Clean the old runlevel
rm -rf "${svcdir}/softscripts.old" &>/dev/null
# Runlevel end, so clear stale fail list
rm -rf "${svcdir}/failed" &>/dev/null
# If we were in the boot runlevel, it is done now ...
if [[ -n ${BOOT} ]] ; then
unset BOOT
if [[ "${RC_INITSTYLE_HACK}" = "yes" ]]; then
/sbin/rc default
fi
fi
# Remove the cached CONSOLETYPE
unset CONSOLETYPE
# vim:ts=4
|