Magellan Linux

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 729 - (hide annotations) (download)
Mon Jun 2 22:53:36 2008 UTC (15 years, 11 months ago) by niro
File size: 7374 byte(s)
-busybox doesn't know the -x function in pidof

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     #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    
51     # dummy function; needed if splashutils are not installed
52     splash() { return 0; }
53    
54    
55     #source splash functions if exists
56     [ -f /etc/init.d/splash-functions ] && source /etc/init.d/splash-functions
57    
58    
59    
60     print_error_msg()
61     {
62     return 0
63     echo
64     $FAILURE
65     echo -e " An Error occurred."
66     echo -e " Script: ${COLWHITE}$i${COLRED}"
67     echo -e " Exitcode: ${COLWHITE}$error_value${COLRED}"
68     echo
69     $NORMAL
70     echo
71     echo "Press Enter to continue..."
72     read
73     }
74    
75     check_script_status()
76     {
77     if [ ! -f $i ]
78     then
79     echo "$i is not a valid symlink"
80     continue
81     fi
82    
83     if [ ! -x $i ]
84     then
85     echo "$i is not executable, skipping"
86     continue
87     fi
88     }
89    
90     update_svcstatus()
91     {
92     #$1 script
93     #$2 start/stop
94    
95     local SVCDIR_MOUNTED
96     local SVCD_INITSCRIPT
97     local x
98    
99    
100     #do this only if proc is mounted
101     [ ! -f /proc/mounts ] && return 0
102    
103     #check if svcdir is mounted
104     SVCDIR_MOUNTED="$(cat /proc/mounts | grep ${svcdir} | cut -d ' ' -f2)"
105     if [ "${SVCDIR_MOUNTED}" == "${svcdir}" ]
106     then
107     #check if statedir exists
108     [ ! -d ${svcdir}/started ] && mkdir ${svcdir}/started
109    
110     #get real name of the initscript, not from the symlink
111     if [ -L "$0" ]
112     then
113     x="$(readlink $0)"
114     SVCD_INITSCRIPT="$(basename ${x})"
115     else
116     SVCD_INITSCRIPT="$(basename $0)"
117     fi
118    
119     case $1 in
120     start)
121     #write to svcddir
122     #write state
123     echo ok > ${svcdir}/started/"${SVCD_INITSCRIPT}"
124     ;;
125     stop)
126     #echo "$(ls ${svcdir}/started)"
127     if [ -f ${svcdir}/started/${SVCD_INITSCRIPT} ]
128     then
129     rm ${svcdir}/started/${SVCD_INITSCRIPT}
130     fi
131     ;;
132     esac
133     fi
134     }
135    
136     evaluate_retval()
137     {
138     local error_value="$1"
139     [[ -z ${error_value} ]] && error_value=$?
140    
141     if [[ ${error_value} = 0 ]]
142     then
143     print_status success
144     else
145     print_status failure
146     fi
147    
148     return ${error_value}
149     }
150    
151     print_status()
152     {
153     if [ $# = 0 ]
154     then
155     echo "Usage: $0 {success|warning|failure}"
156     return 1
157     fi
158    
159     $CURS_UP
160    
161     case "$1" in
162     success)
163     $SET_COL
164     echo -e -n $COLBLUE"[ "
165     $SUCCESS
166     echo -e -n "OK"
167     echo -e $COLBLUE" ]"
168     $NORMAL
169     ;;
170     warning)
171     case "$2" in
172     running)
173     $SET_WCOL
174     echo "Already running"
175     $CURS_UP
176     ;;
177     not_running)
178     $SET_WCOL
179     echo "Not running"
180     $CURS_UP
181     ;;
182     esac
183     $SET_COL
184     echo -e -n $COLBLUE"[ "
185     $WARNING
186     echo -e -n "ATTN"
187     echo -e $COLBLUE" ]"
188     $NORMAL
189     ;;
190     failure)
191     $SET_COL
192     echo -e -n $COLBLUE"["
193     $FAILURE
194     echo -e -n "FAILED"
195     echo -e $COLBLUE"]"
196     $NORMAL
197     ;;
198     esac
199     }
200    
201     getpids()
202     {
203     base=${1##*/}
204 niro 729 pidlist=$(pidof -o $$ -o $PPID $base)
205 niro 724 }
206    
207     loadproc()
208     {
209     if [ $# = 0 ]
210     then
211     echo "Usage: loadproc {program}"
212     exit 1
213     fi
214    
215     getpids $1
216    
217     if [ -z "$pidlist" ]
218     then
219     "$@"
220     evaluate_retval
221     else
222     $SET_WCOL
223     print_status warning running
224     fi
225     }
226    
227     killproc()
228     {
229     if [ $# = 0 ]
230     then
231     echo "Usage: killproc {program} [signal]"
232     exit 1
233     fi
234    
235     if [ -z "$2" ]; then
236     signal=TERM
237     fallback=KILL
238     else
239     signal=${2##-}
240     signal=${signal##SIG}
241     fallback=""
242     fi
243    
244     getpids $1
245    
246     if [ -n "$pidlist" ]; then
247     failure=0
248    
249     for pid in $pidlist
250     do
251     kill -$signal $pid 2>/dev/null
252    
253     for ((i=0; $i<5000; i=$i+1)); do :; done
254    
255     for ((i=0; $i<$KILLDELAY; i=$i+1)); do
256     kill -0 $pid 2>/dev/null || break
257     sleep 1
258     done
259    
260     if [ -n "$fallback" ]; then kill -$fallback $pid 2>/dev/null; fi
261    
262     kill -0 $pid 2>/dev/null && failure=1
263     done
264    
265     base=${1##*/}
266     if [ $failure = 0 ]; then rm -f /var/run/$base.pid; fi
267    
268     (exit $failure)
269     evaluate_retval
270     else
271     $SET_WCOL
272     print_status warning not_running
273     fi
274     }
275    
276     reloadproc()
277     {
278     if [ $# = 0 ]
279     then
280     echo "Usage: reloadproc {program} [signal]"
281     exit 1
282     fi
283    
284     if [ -z "$2" ]; then
285     signal=HUP
286     else
287     signal=${2##-}
288     signal=${signal##SIG}
289    
290     fi
291    
292     getpids $1
293    
294     if [ -n "$pidlist" ]
295     then
296     failure=0
297    
298     for pid in $pidlist
299     do
300     kill -$signal $pid || failure=1
301     done
302    
303     (exit $failure)
304     evaluate_retval
305     else
306     $SET_WCOL
307     print_status warning not_running
308     fi
309     }
310    
311     statusproc()
312     {
313     if [ $# = 0 ]
314     then
315     echo "Usage: statusproc {program}"
316     exit 1
317     fi
318    
319     base=${1##*/}
320     getpids $base
321    
322     if [ -n "$pidlist" ]
323     then
324     echo "$base is running with Process ID(s) $pidlist"
325     else
326     if [ -s /var/run/$base.pid ]
327     then
328     echo "$1 is not running but /var/run/$base.pid exists"
329     return 1
330     else
331     echo "$1 is not running"
332     fi
333     fi
334     }
335    
336     progressbar()
337     {
338     if [ $# != 1 ]
339     then
340     echo "Usage: progressbar {progress}"
341     exit 1
342     fi
343    
344     if [ -f /proc/splash ]
345     then
346     echo "show $(( 65534 * $1 / 100 ))" > /proc/splash
347     fi
348     }
349    
350     kernel_version()
351     {
352     local KV="$(uname -r | cut -d- -f1)"
353     echo "${KV}"
354     }
355    
356     kernel_major_version()
357     {
358     local KV
359     KV="$(uname -r | cut -d. -f1-2)"
360     echo "${KV}"
361     }
362    
363     dolisting()
364     {
365     local x=
366     local y=
367     local tmpstr=
368     local mylist=
369     local mypath="${*}"
370    
371     if [ "${mypath%/\*}" != "${mypath}" ]
372     then
373     mypath="${mypath%/\*}"
374     fi
375    
376     for x in ${mypath}
377     do
378     [ ! -e "${x}" ] && continue
379    
380     if [ ! -d "${x}" ] && ( [ -L "${x}" -o -f "${x}" ] )
381     then
382     mylist="${mylist} $(ls "${x}" 2> /dev/null)"
383     else
384     [ "${x%/}" != "${x}" ] && x="${x%/}"
385    
386     cd "${x}"; tmpstr="$(ls)"
387    
388     for y in ${tmpstr}
389     do
390     mylist="${mylist} ${x}/${y}"
391     done
392     fi
393     done
394    
395     echo "${mylist}"
396     }
397    
398     # searches /proc/mounts for mounted fstypes (like ext3)
399     is_fstype_mounted()
400     {
401     local filesys
402     local i
403     filesys=$1
404    
405     i="$(cat /proc/mounts | grep ${filesys} | cut -d ' ' -f3)"
406     [[ ${i} != ${filesys} ]] && return 1
407    
408     return 0
409     }
410    
411     # searches /proc/mounts for mounted fs names (like udev, proc)
412     is_fs_mounted()
413     {
414     local filesys
415     local i
416     filesys=$1
417    
418     i="$(cat /proc/mounts | grep ${filesys} | cut -d ' ' -f1)"
419     [[ ${i} != ${filesys} ]] && return 1
420    
421     return 0
422     }
423    
424     # checks if kernel supports fs xy
425     kernel_supports_fs()
426     {
427     local filesys
428     local i
429     filesys=$1
430    
431     i="$(cat /proc/filesystems | grep ${filesys} | cut -d $'\t' -f2)"
432     [[ ${i} != ${filesys} ]] && return 1
433    
434     return 0
435     }
436    
437     # bool is_older_than(reference, files/dirs to check)
438     #
439     # return 0 if any of the files/dirs are newer than
440     # the reference file
441     #
442     # EXAMPLE: if is_older_than a.out *.o ; then ...
443     is_older_than()
444     {
445     local x=
446     local ref="$1"
447     shift
448    
449     for x in "$@"
450     do
451     [[ ${x} -nt ${ref} ]] && return 0
452    
453     if [[ -d ${x} ]]
454     then
455     is_older_than "${ref}" "${x}"/* && return 0
456     fi
457     done
458    
459     return 1
460     }