Magellan Linux

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 274 - (hide annotations) (download)
Fri Oct 21 15:21:41 2005 UTC (18 years, 6 months ago) by niro
File size: 7261 byte(s)
added is_older_than function from gentoo; needed by modules-update

1 niro 2 #!/bin/bash
2 niro 274 # $Header: /home/cvsd/magellan-cvs/magellan-src/magellan-initscripts/etc/rc.d/init.d/functions,v 1.5 2005-10-21 15:21:41 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     kernel_major_version()
352     {
353     local KV
354     KV="$(uname -r|cut -d. -f1-2)"
355     echo "${KV}"
356     }
357    
358     dolisting()
359     {
360     local x=
361     local y=
362     local tmpstr=
363     local mylist=
364     local mypath="${*}"
365    
366     if [ "${mypath%/\*}" != "${mypath}" ]
367     then
368     mypath="${mypath%/\*}"
369     fi
370    
371     for x in ${mypath}
372     do
373     [ ! -e "${x}" ] && continue
374    
375     if [ ! -d "${x}" ] && ( [ -L "${x}" -o -f "${x}" ] )
376     then
377     mylist="${mylist} $(ls "${x}" 2> /dev/null)"
378     else
379     [ "${x%/}" != "${x}" ] && x="${x%/}"
380    
381     cd "${x}"; tmpstr="$(ls)"
382    
383     for y in ${tmpstr}
384     do
385     mylist="${mylist} ${x}/${y}"
386     done
387     fi
388     done
389    
390     echo "${mylist}"
391     }
392 niro 147
393     # searches /proc/mounts for mounted fstypes (like ext3)
394     is_fstype_mounted() {
395     local filesys
396     local i
397     filesys=$1
398    
399     i="$(cat /proc/mounts | grep ${filesys} | cut -d ' ' -f3)"
400     [[ ${i} != ${filesys} ]] && return 1
401    
402     return 0
403     }
404    
405     # searches /proc/mounts for mounted fs names (like udev, proc)
406     is_fs_mounted() {
407     local filesys
408     local i
409     filesys=$1
410    
411     i="$(cat /proc/mounts | grep ${filesys} | cut -d ' ' -f1)"
412     [[ ${i} != ${filesys} ]] && return 1
413    
414     return 0
415     }
416    
417     # checks if kernel supports fs xy
418     kernel_supports_fs() {
419     local filesys
420     local i
421     filesys=$1
422    
423     i="$(cat /proc/filesystems | grep ${filesys} | cut -d $'\t' -f2)"
424     [[ ${i} != ${filesys} ]] && return 1
425    
426     return 0
427     }
428 niro 274
429     # bool is_older_than(reference, files/dirs to check)
430     #
431     # return 0 if any of the files/dirs are newer than
432     # the reference file
433     #
434     # EXAMPLE: if is_older_than a.out *.o ; then ...
435     is_older_than() {
436     local x=
437     local ref="$1"
438     shift
439    
440     for x in "$@"
441     do
442     [[ ${x} -nt ${ref} ]] && return 0
443    
444     if [[ -d ${x} ]]
445     then
446     is_older_than "${ref}" "${x}"/* && return 0
447     fi
448     done
449    
450     return 1
451     }

Properties

Name Value
svn:executable *