Magellan Linux

Annotation of /trunk/magellan-initscripts/etc/rc.d/init.d/functions

Parent Directory Parent Directory | Revision Log Revision Log


Revision 317 - (hide annotations) (download)
Tue Jan 3 18:03:11 2006 UTC (18 years, 4 months ago) by niro
File size: 7336 byte(s)
added kernel_version function

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

Properties

Name Value
svn:executable *