Annotation of /tags/busybox-init-0_1_12/rc/functions
Parent Directory | Revision Log
Revision 1230 -
(hide annotations)
(download)
Thu Mar 3 18:41:34 2011 UTC (13 years, 6 months ago) by niro
File size: 7623 byte(s)
Thu Mar 3 18:41:34 2011 UTC (13 years, 6 months ago) by niro
File size: 7623 byte(s)
tagged 'busybox-init-0_1_12'
1 | niro | 724 | #!/bin/sh |
2 | niro | 729 | # $Header: /home/cvsd/magellan-cvs/magellan-src/busybox-initscripts/rc/functions,v 1.2 2008-06-02 22:53:36 niro Exp $ |
3 | niro | 724 | |
4 | # Begin $rc_base/init.d/functions - Run Level Control Functions | ||
5 | |||
6 | # Based on functions script from LFS-3.1 and earlier. | ||
7 | # Rewritten by Gerard Beekmans - gerard@linuxfromscratch.org | ||
8 | |||
9 | # With code based on Matthias Benkmann's simpleinit-msb @ | ||
10 | # http://winterdrache.de/linux/newboot/index.html | ||
11 | |||
12 | umask 022 | ||
13 | |||
14 | export PATH="/bin:/usr/bin:/sbin:/usr/sbin" | ||
15 | |||
16 | KILLDELAY=3 | ||
17 | |||
18 | COLUMNS=$(stty size) | ||
19 | COLUMNS=${COLUMNS##* } | ||
20 | COL=$[ $COLUMNS - 10 ] | ||
21 | WCOL=$[ $COLUMNS - 30 ] | ||
22 | WWCOL=$[ $COLUMNS - 40 ] | ||
23 | SET_COL="echo -en \\033[${COL}G" | ||
24 | SET_WCOL="echo -en \\033[${WCOL}G" | ||
25 | SET_WWCOL="echo -en \\033[${WWCOL}G" | ||
26 | CURS_UP="echo -en \\033[A" | ||
27 | |||
28 | NORMAL="echo -en \\033[0;39m" | ||
29 | SUCCESS="echo -en \\033[1;32m" | ||
30 | WARNING="echo -en \\033[1;33m" | ||
31 | FAILURE="echo -en \\033[1;31m" | ||
32 | |||
33 | COLRED="\033[1;6m\033[31m" | ||
34 | COLGREEN="\033[1;6m\033[32m" | ||
35 | COLYELLOW="\033[1;6m\033[33m" | ||
36 | COLBLUE="\033[1;6m\033[34m" | ||
37 | COLMAGENTA="\033[1;6m\033[35m" | ||
38 | COLWHITE="\033[1;6m\033[37m" | ||
39 | COLDEFAULT="\033[0m" | ||
40 | |||
41 | COLOREDSTAR="${COLBLUE}(${COLGREEN}*${COLBLUE})${COLDEFAULT} " | ||
42 | |||
43 | niro | 1195 | # location to save the started services |
44 | niro | 724 | svcdir="/var/lib/init.d" |
45 | svclib="/lib/rcscripts" | ||
46 | svcmount="no" | ||
47 | svcfstype="tmpfs" | ||
48 | svcsize=1024 | ||
49 | |||
50 | niro | 1168 | rc_echo() { echo $@; } |
51 | rc_print() { rc_echo -e "${COLOREDSTAR}$@"; } | ||
52 | niro | 724 | |
53 | # dummy function; needed if splashutils are not installed | ||
54 | splash() { return 0; } | ||
55 | |||
56 | niro | 1195 | # source splash functions if exists |
57 | niro | 724 | [ -f /etc/init.d/splash-functions ] && source /etc/init.d/splash-functions |
58 | |||
59 | print_error_msg() | ||
60 | { | ||
61 | return 0 | ||
62 | echo | ||
63 | $FAILURE | ||
64 | echo -e " An Error occurred." | ||
65 | echo -e " Script: ${COLWHITE}$i${COLRED}" | ||
66 | echo -e " Exitcode: ${COLWHITE}$error_value${COLRED}" | ||
67 | echo | ||
68 | $NORMAL | ||
69 | echo | ||
70 | echo "Press Enter to continue..." | ||
71 | read | ||
72 | } | ||
73 | |||
74 | check_script_status() | ||
75 | { | ||
76 | if [ ! -f $i ] | ||
77 | then | ||
78 | echo "$i is not a valid symlink" | ||
79 | continue | ||
80 | fi | ||
81 | |||
82 | if [ ! -x $i ] | ||
83 | then | ||
84 | echo "$i is not executable, skipping" | ||
85 | continue | ||
86 | fi | ||
87 | } | ||
88 | |||
89 | update_svcstatus() | ||
90 | { | ||
91 | #$1 script | ||
92 | #$2 start/stop | ||
93 | |||
94 | local SVCDIR_MOUNTED | ||
95 | local SVCD_INITSCRIPT | ||
96 | local x | ||
97 | |||
98 | #do this only if proc is mounted | ||
99 | [ ! -f /proc/mounts ] && return 0 | ||
100 | |||
101 | #check if svcdir is mounted | ||
102 | SVCDIR_MOUNTED="$(cat /proc/mounts | grep ${svcdir} | cut -d ' ' -f2)" | ||
103 | if [ "${SVCDIR_MOUNTED}" == "${svcdir}" ] | ||
104 | then | ||
105 | #check if statedir exists | ||
106 | [ ! -d ${svcdir}/started ] && mkdir ${svcdir}/started | ||
107 | |||
108 | #get real name of the initscript, not from the symlink | ||
109 | if [ -L "$0" ] | ||
110 | then | ||
111 | x="$(readlink $0)" | ||
112 | SVCD_INITSCRIPT="$(basename ${x})" | ||
113 | else | ||
114 | SVCD_INITSCRIPT="$(basename $0)" | ||
115 | fi | ||
116 | |||
117 | case $1 in | ||
118 | start) | ||
119 | #write to svcddir | ||
120 | #write state | ||
121 | echo ok > ${svcdir}/started/"${SVCD_INITSCRIPT}" | ||
122 | ;; | ||
123 | stop) | ||
124 | #echo "$(ls ${svcdir}/started)" | ||
125 | if [ -f ${svcdir}/started/${SVCD_INITSCRIPT} ] | ||
126 | then | ||
127 | rm ${svcdir}/started/${SVCD_INITSCRIPT} | ||
128 | fi | ||
129 | ;; | ||
130 | esac | ||
131 | fi | ||
132 | } | ||
133 | |||
134 | evaluate_retval() | ||
135 | { | ||
136 | local error_value="$1" | ||
137 | [[ -z ${error_value} ]] && error_value=$? | ||
138 | |||
139 | if [[ ${error_value} = 0 ]] | ||
140 | then | ||
141 | print_status success | ||
142 | else | ||
143 | print_status failure | ||
144 | niro | 1204 | |
145 | if [[ ${SPLASH_VERBOSE_ON_ERRORS} = 1 ]] | ||
146 | then | ||
147 | splash "rc_verbose" "${runlevel}" | ||
148 | fi | ||
149 | niro | 724 | fi |
150 | |||
151 | return ${error_value} | ||
152 | } | ||
153 | |||
154 | print_status() | ||
155 | { | ||
156 | if [ $# = 0 ] | ||
157 | then | ||
158 | echo "Usage: $0 {success|warning|failure}" | ||
159 | return 1 | ||
160 | fi | ||
161 | |||
162 | $CURS_UP | ||
163 | |||
164 | case "$1" in | ||
165 | success) | ||
166 | $SET_COL | ||
167 | echo -e -n $COLBLUE"[ " | ||
168 | $SUCCESS | ||
169 | echo -e -n "OK" | ||
170 | echo -e $COLBLUE" ]" | ||
171 | $NORMAL | ||
172 | ;; | ||
173 | warning) | ||
174 | case "$2" in | ||
175 | running) | ||
176 | $SET_WCOL | ||
177 | echo "Already running" | ||
178 | $CURS_UP | ||
179 | ;; | ||
180 | not_running) | ||
181 | $SET_WCOL | ||
182 | echo "Not running" | ||
183 | $CURS_UP | ||
184 | ;; | ||
185 | esac | ||
186 | $SET_COL | ||
187 | echo -e -n $COLBLUE"[ " | ||
188 | $WARNING | ||
189 | echo -e -n "ATTN" | ||
190 | echo -e $COLBLUE" ]" | ||
191 | $NORMAL | ||
192 | ;; | ||
193 | failure) | ||
194 | $SET_COL | ||
195 | echo -e -n $COLBLUE"[" | ||
196 | $FAILURE | ||
197 | echo -e -n "FAILED" | ||
198 | echo -e $COLBLUE"]" | ||
199 | $NORMAL | ||
200 | ;; | ||
201 | esac | ||
202 | } | ||
203 | |||
204 | getpids() | ||
205 | { | ||
206 | base=${1##*/} | ||
207 | niro | 729 | pidlist=$(pidof -o $$ -o $PPID $base) |
208 | niro | 724 | } |
209 | |||
210 | loadproc() | ||
211 | { | ||
212 | if [ $# = 0 ] | ||
213 | then | ||
214 | echo "Usage: loadproc {program}" | ||
215 | exit 1 | ||
216 | fi | ||
217 | |||
218 | getpids $1 | ||
219 | |||
220 | if [ -z "$pidlist" ] | ||
221 | then | ||
222 | "$@" | ||
223 | evaluate_retval | ||
224 | else | ||
225 | $SET_WCOL | ||
226 | print_status warning running | ||
227 | fi | ||
228 | } | ||
229 | |||
230 | killproc() | ||
231 | { | ||
232 | if [ $# = 0 ] | ||
233 | then | ||
234 | echo "Usage: killproc {program} [signal]" | ||
235 | exit 1 | ||
236 | fi | ||
237 | |||
238 | if [ -z "$2" ]; then | ||
239 | signal=TERM | ||
240 | fallback=KILL | ||
241 | else | ||
242 | signal=${2##-} | ||
243 | signal=${signal##SIG} | ||
244 | fallback="" | ||
245 | fi | ||
246 | |||
247 | getpids $1 | ||
248 | |||
249 | if [ -n "$pidlist" ]; then | ||
250 | failure=0 | ||
251 | |||
252 | for pid in $pidlist | ||
253 | do | ||
254 | kill -$signal $pid 2>/dev/null | ||
255 | |||
256 | for ((i=0; $i<5000; i=$i+1)); do :; done | ||
257 | |||
258 | for ((i=0; $i<$KILLDELAY; i=$i+1)); do | ||
259 | kill -0 $pid 2>/dev/null || break | ||
260 | sleep 1 | ||
261 | done | ||
262 | |||
263 | if [ -n "$fallback" ]; then kill -$fallback $pid 2>/dev/null; fi | ||
264 | |||
265 | kill -0 $pid 2>/dev/null && failure=1 | ||
266 | done | ||
267 | |||
268 | base=${1##*/} | ||
269 | if [ $failure = 0 ]; then rm -f /var/run/$base.pid; fi | ||
270 | |||
271 | (exit $failure) | ||
272 | evaluate_retval | ||
273 | else | ||
274 | $SET_WCOL | ||
275 | print_status warning not_running | ||
276 | fi | ||
277 | } | ||
278 | |||
279 | reloadproc() | ||
280 | { | ||
281 | if [ $# = 0 ] | ||
282 | then | ||
283 | echo "Usage: reloadproc {program} [signal]" | ||
284 | exit 1 | ||
285 | fi | ||
286 | |||
287 | if [ -z "$2" ]; then | ||
288 | signal=HUP | ||
289 | else | ||
290 | signal=${2##-} | ||
291 | signal=${signal##SIG} | ||
292 | fi | ||
293 | |||
294 | getpids $1 | ||
295 | |||
296 | if [ -n "$pidlist" ] | ||
297 | then | ||
298 | failure=0 | ||
299 | |||
300 | for pid in $pidlist | ||
301 | do | ||
302 | kill -$signal $pid || failure=1 | ||
303 | done | ||
304 | |||
305 | (exit $failure) | ||
306 | evaluate_retval | ||
307 | else | ||
308 | $SET_WCOL | ||
309 | print_status warning not_running | ||
310 | fi | ||
311 | } | ||
312 | |||
313 | statusproc() | ||
314 | { | ||
315 | if [ $# = 0 ] | ||
316 | then | ||
317 | echo "Usage: statusproc {program}" | ||
318 | exit 1 | ||
319 | fi | ||
320 | |||
321 | base=${1##*/} | ||
322 | getpids $base | ||
323 | |||
324 | if [ -n "$pidlist" ] | ||
325 | then | ||
326 | echo "$base is running with Process ID(s) $pidlist" | ||
327 | else | ||
328 | if [ -s /var/run/$base.pid ] | ||
329 | then | ||
330 | echo "$1 is not running but /var/run/$base.pid exists" | ||
331 | return 1 | ||
332 | else | ||
333 | echo "$1 is not running" | ||
334 | fi | ||
335 | fi | ||
336 | } | ||
337 | |||
338 | progressbar() | ||
339 | { | ||
340 | if [ $# != 1 ] | ||
341 | then | ||
342 | echo "Usage: progressbar {progress}" | ||
343 | exit 1 | ||
344 | fi | ||
345 | |||
346 | if [ -f /proc/splash ] | ||
347 | then | ||
348 | echo "show $(( 65534 * $1 / 100 ))" > /proc/splash | ||
349 | fi | ||
350 | } | ||
351 | |||
352 | kernel_version() | ||
353 | { | ||
354 | local KV="$(uname -r | cut -d- -f1)" | ||
355 | echo "${KV}" | ||
356 | } | ||
357 | |||
358 | kernel_major_version() | ||
359 | { | ||
360 | local KV | ||
361 | KV="$(uname -r | cut -d. -f1-2)" | ||
362 | echo "${KV}" | ||
363 | } | ||
364 | |||
365 | dolisting() | ||
366 | { | ||
367 | local x= | ||
368 | local y= | ||
369 | local tmpstr= | ||
370 | local mylist= | ||
371 | local mypath="${*}" | ||
372 | |||
373 | if [ "${mypath%/\*}" != "${mypath}" ] | ||
374 | then | ||
375 | mypath="${mypath%/\*}" | ||
376 | fi | ||
377 | |||
378 | for x in ${mypath} | ||
379 | do | ||
380 | [ ! -e "${x}" ] && continue | ||
381 | |||
382 | if [ ! -d "${x}" ] && ( [ -L "${x}" -o -f "${x}" ] ) | ||
383 | then | ||
384 | mylist="${mylist} $(ls "${x}" 2> /dev/null)" | ||
385 | else | ||
386 | [ "${x%/}" != "${x}" ] && x="${x%/}" | ||
387 | |||
388 | cd "${x}"; tmpstr="$(ls)" | ||
389 | |||
390 | for y in ${tmpstr} | ||
391 | do | ||
392 | mylist="${mylist} ${x}/${y}" | ||
393 | done | ||
394 | fi | ||
395 | done | ||
396 | |||
397 | echo "${mylist}" | ||
398 | } | ||
399 | |||
400 | # searches /proc/mounts for mounted fstypes (like ext3) | ||
401 | is_fstype_mounted() | ||
402 | { | ||
403 | local filesys | ||
404 | local i | ||
405 | filesys=$1 | ||
406 | |||
407 | i="$(cat /proc/mounts | grep ${filesys} | cut -d ' ' -f3)" | ||
408 | [[ ${i} != ${filesys} ]] && return 1 | ||
409 | |||
410 | return 0 | ||
411 | } | ||
412 | |||
413 | # searches /proc/mounts for mounted fs names (like udev, proc) | ||
414 | is_fs_mounted() | ||
415 | { | ||
416 | local filesys | ||
417 | local i | ||
418 | filesys=$1 | ||
419 | |||
420 | i="$(cat /proc/mounts | grep ${filesys} | cut -d ' ' -f1)" | ||
421 | [[ ${i} != ${filesys} ]] && return 1 | ||
422 | |||
423 | return 0 | ||
424 | } | ||
425 | |||
426 | # checks if kernel supports fs xy | ||
427 | kernel_supports_fs() | ||
428 | { | ||
429 | local filesys | ||
430 | local i | ||
431 | filesys=$1 | ||
432 | |||
433 | i="$(cat /proc/filesystems | grep ${filesys} | cut -d $'\t' -f2)" | ||
434 | [[ ${i} != ${filesys} ]] && return 1 | ||
435 | |||
436 | return 0 | ||
437 | } | ||
438 | |||
439 | # bool is_older_than(reference, files/dirs to check) | ||
440 | # | ||
441 | # return 0 if any of the files/dirs are newer than | ||
442 | # the reference file | ||
443 | # | ||
444 | # EXAMPLE: if is_older_than a.out *.o ; then ... | ||
445 | is_older_than() | ||
446 | { | ||
447 | local x= | ||
448 | local ref="$1" | ||
449 | shift | ||
450 | |||
451 | for x in "$@" | ||
452 | do | ||
453 | [[ ${x} -nt ${ref} ]] && return 0 | ||
454 | |||
455 | if [[ -d ${x} ]] | ||
456 | then | ||
457 | is_older_than "${ref}" "${x}"/* && return 0 | ||
458 | fi | ||
459 | done | ||
460 | |||
461 | return 1 | ||
462 | } | ||
463 | niro | 1195 | |
464 | start_devicemanager() { touch /dev/.none; return 0; } | ||
465 | stop_devicemanager() { return 0; } |