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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.20