/[vps]/baselayout-vserver/branches/baselayout-1_12/sbin/rc
Gentoo

Contents of /baselayout-vserver/branches/baselayout-1_12/sbin/rc

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.20