Magellan Linux

Contents of /trunk/busybox-initscripts/rc/functions

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1195 - (show annotations) (download)
Thu Jan 27 20:41:59 2011 UTC (13 years, 3 months ago) by niro
File size: 7530 byte(s)
-merged with upstream
1 #!/bin/sh
2 # $Header: /home/cvsd/magellan-cvs/magellan-src/busybox-initscripts/rc/functions,v 1.2 2008-06-02 22:53:36 niro Exp $
3
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 # location to save the started services
44 svcdir="/var/lib/init.d"
45 svclib="/lib/rcscripts"
46 svcmount="no"
47 svcfstype="tmpfs"
48 svcsize=1024
49
50 rc_echo() { echo $@; }
51 rc_print() { rc_echo -e "${COLOREDSTAR}$@"; }
52
53 # dummy function; needed if splashutils are not installed
54 splash() { return 0; }
55
56 # source splash functions if exists
57 [ -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 fi
145
146 return ${error_value}
147 }
148
149 print_status()
150 {
151 if [ $# = 0 ]
152 then
153 echo "Usage: $0 {success|warning|failure}"
154 return 1
155 fi
156
157 $CURS_UP
158
159 case "$1" in
160 success)
161 $SET_COL
162 echo -e -n $COLBLUE"[ "
163 $SUCCESS
164 echo -e -n "OK"
165 echo -e $COLBLUE" ]"
166 $NORMAL
167 ;;
168 warning)
169 case "$2" in
170 running)
171 $SET_WCOL
172 echo "Already running"
173 $CURS_UP
174 ;;
175 not_running)
176 $SET_WCOL
177 echo "Not running"
178 $CURS_UP
179 ;;
180 esac
181 $SET_COL
182 echo -e -n $COLBLUE"[ "
183 $WARNING
184 echo -e -n "ATTN"
185 echo -e $COLBLUE" ]"
186 $NORMAL
187 ;;
188 failure)
189 $SET_COL
190 echo -e -n $COLBLUE"["
191 $FAILURE
192 echo -e -n "FAILED"
193 echo -e $COLBLUE"]"
194 $NORMAL
195 ;;
196 esac
197 }
198
199 getpids()
200 {
201 base=${1##*/}
202 pidlist=$(pidof -o $$ -o $PPID $base)
203 }
204
205 loadproc()
206 {
207 if [ $# = 0 ]
208 then
209 echo "Usage: loadproc {program}"
210 exit 1
211 fi
212
213 getpids $1
214
215 if [ -z "$pidlist" ]
216 then
217 "$@"
218 evaluate_retval
219 else
220 $SET_WCOL
221 print_status warning running
222 fi
223 }
224
225 killproc()
226 {
227 if [ $# = 0 ]
228 then
229 echo "Usage: killproc {program} [signal]"
230 exit 1
231 fi
232
233 if [ -z "$2" ]; then
234 signal=TERM
235 fallback=KILL
236 else
237 signal=${2##-}
238 signal=${signal##SIG}
239 fallback=""
240 fi
241
242 getpids $1
243
244 if [ -n "$pidlist" ]; then
245 failure=0
246
247 for pid in $pidlist
248 do
249 kill -$signal $pid 2>/dev/null
250
251 for ((i=0; $i<5000; i=$i+1)); do :; done
252
253 for ((i=0; $i<$KILLDELAY; i=$i+1)); do
254 kill -0 $pid 2>/dev/null || break
255 sleep 1
256 done
257
258 if [ -n "$fallback" ]; then kill -$fallback $pid 2>/dev/null; fi
259
260 kill -0 $pid 2>/dev/null && failure=1
261 done
262
263 base=${1##*/}
264 if [ $failure = 0 ]; then rm -f /var/run/$base.pid; fi
265
266 (exit $failure)
267 evaluate_retval
268 else
269 $SET_WCOL
270 print_status warning not_running
271 fi
272 }
273
274 reloadproc()
275 {
276 if [ $# = 0 ]
277 then
278 echo "Usage: reloadproc {program} [signal]"
279 exit 1
280 fi
281
282 if [ -z "$2" ]; then
283 signal=HUP
284 else
285 signal=${2##-}
286 signal=${signal##SIG}
287 fi
288
289 getpids $1
290
291 if [ -n "$pidlist" ]
292 then
293 failure=0
294
295 for pid in $pidlist
296 do
297 kill -$signal $pid || failure=1
298 done
299
300 (exit $failure)
301 evaluate_retval
302 else
303 $SET_WCOL
304 print_status warning not_running
305 fi
306 }
307
308 statusproc()
309 {
310 if [ $# = 0 ]
311 then
312 echo "Usage: statusproc {program}"
313 exit 1
314 fi
315
316 base=${1##*/}
317 getpids $base
318
319 if [ -n "$pidlist" ]
320 then
321 echo "$base is running with Process ID(s) $pidlist"
322 else
323 if [ -s /var/run/$base.pid ]
324 then
325 echo "$1 is not running but /var/run/$base.pid exists"
326 return 1
327 else
328 echo "$1 is not running"
329 fi
330 fi
331 }
332
333 progressbar()
334 {
335 if [ $# != 1 ]
336 then
337 echo "Usage: progressbar {progress}"
338 exit 1
339 fi
340
341 if [ -f /proc/splash ]
342 then
343 echo "show $(( 65534 * $1 / 100 ))" > /proc/splash
344 fi
345 }
346
347 kernel_version()
348 {
349 local KV="$(uname -r | cut -d- -f1)"
350 echo "${KV}"
351 }
352
353 kernel_major_version()
354 {
355 local KV
356 KV="$(uname -r | cut -d. -f1-2)"
357 echo "${KV}"
358 }
359
360 dolisting()
361 {
362 local x=
363 local y=
364 local tmpstr=
365 local mylist=
366 local mypath="${*}"
367
368 if [ "${mypath%/\*}" != "${mypath}" ]
369 then
370 mypath="${mypath%/\*}"
371 fi
372
373 for x in ${mypath}
374 do
375 [ ! -e "${x}" ] && continue
376
377 if [ ! -d "${x}" ] && ( [ -L "${x}" -o -f "${x}" ] )
378 then
379 mylist="${mylist} $(ls "${x}" 2> /dev/null)"
380 else
381 [ "${x%/}" != "${x}" ] && x="${x%/}"
382
383 cd "${x}"; tmpstr="$(ls)"
384
385 for y in ${tmpstr}
386 do
387 mylist="${mylist} ${x}/${y}"
388 done
389 fi
390 done
391
392 echo "${mylist}"
393 }
394
395 # searches /proc/mounts for mounted fstypes (like ext3)
396 is_fstype_mounted()
397 {
398 local filesys
399 local i
400 filesys=$1
401
402 i="$(cat /proc/mounts | grep ${filesys} | cut -d ' ' -f3)"
403 [[ ${i} != ${filesys} ]] && return 1
404
405 return 0
406 }
407
408 # searches /proc/mounts for mounted fs names (like udev, proc)
409 is_fs_mounted()
410 {
411 local filesys
412 local i
413 filesys=$1
414
415 i="$(cat /proc/mounts | grep ${filesys} | cut -d ' ' -f1)"
416 [[ ${i} != ${filesys} ]] && return 1
417
418 return 0
419 }
420
421 # checks if kernel supports fs xy
422 kernel_supports_fs()
423 {
424 local filesys
425 local i
426 filesys=$1
427
428 i="$(cat /proc/filesystems | grep ${filesys} | cut -d $'\t' -f2)"
429 [[ ${i} != ${filesys} ]] && return 1
430
431 return 0
432 }
433
434 # bool is_older_than(reference, files/dirs to check)
435 #
436 # return 0 if any of the files/dirs are newer than
437 # the reference file
438 #
439 # EXAMPLE: if is_older_than a.out *.o ; then ...
440 is_older_than()
441 {
442 local x=
443 local ref="$1"
444 shift
445
446 for x in "$@"
447 do
448 [[ ${x} -nt ${ref} ]] && return 0
449
450 if [[ -d ${x} ]]
451 then
452 is_older_than "${ref}" "${x}"/* && return 0
453 fi
454 done
455
456 return 1
457 }
458
459 start_devicemanager() { touch /dev/.none; return 0; }
460 stop_devicemanager() { return 0; }