Magellan Linux

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 931 - (hide annotations) (download)
Thu Nov 5 12:57:14 2009 UTC (14 years, 6 months ago) by niro
File size: 7268 byte(s)
-fixed header
1 niro 2 #!/bin/bash
2 niro 931 # $Id$
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 niro 505 local error_value="$1"
141     [[ -z ${error_value} ]] && error_value=$?
142 niro 2
143 niro 505 if [[ ${error_value} = 0 ]]
144 niro 2 then
145     print_status success
146     else
147     print_status failure
148     fi
149    
150 niro 505 return ${error_value}
151 niro 2 }
152    
153     print_status()
154     {
155     if [ $# = 0 ]
156     then
157     echo "Usage: $0 {success|warning|failure}"
158     return 1
159     fi
160    
161     $CURS_UP
162    
163     case "$1" in
164     success)
165     $SET_COL
166     echo -e -n $COLBLUE"[ "
167     $SUCCESS
168     echo -e -n "OK"
169     echo -e $COLBLUE" ]"
170     $NORMAL
171     ;;
172     warning)
173     case "$2" in
174     running)
175     $SET_WCOL
176     echo "Already running"
177     $CURS_UP
178     ;;
179     not_running)
180     $SET_WCOL
181     echo "Not running"
182     $CURS_UP
183     ;;
184     esac
185     $SET_COL
186     echo -e -n $COLBLUE"[ "
187     $WARNING
188     echo -e -n "ATTN"
189     echo -e $COLBLUE" ]"
190     $NORMAL
191     ;;
192     failure)
193     $SET_COL
194     echo -e -n $COLBLUE"["
195     $FAILURE
196     echo -e -n "FAILED"
197     echo -e $COLBLUE"]"
198     $NORMAL
199     ;;
200     esac
201     }
202    
203     getpids()
204     {
205     base=${1##*/}
206     pidlist=$(pidof -o $$ -o $PPID -x $base)
207     }
208    
209     loadproc()
210     {
211     if [ $# = 0 ]
212     then
213     echo "Usage: loadproc {program}"
214     exit 1
215     fi
216    
217     getpids $1
218    
219     if [ -z "$pidlist" ]
220     then
221     "$@"
222     evaluate_retval
223     else
224     $SET_WCOL
225     print_status warning running
226     fi
227     }
228    
229     killproc()
230     {
231     if [ $# = 0 ]
232     then
233     echo "Usage: killproc {program} [signal]"
234     exit 1
235     fi
236    
237     if [ -z "$2" ]; then
238     signal=TERM
239     fallback=KILL
240     else
241     signal=${2##-}
242     signal=${signal##SIG}
243     fallback=""
244     fi
245    
246     getpids $1
247    
248     if [ -n "$pidlist" ]; then
249     failure=0
250    
251     for pid in $pidlist
252     do
253     kill -$signal $pid 2>/dev/null
254    
255     for ((i=0; $i<5000; i=$i+1)); do :; done
256    
257     for ((i=0; $i<$KILLDELAY; i=$i+1)); do
258     kill -0 $pid 2>/dev/null || break
259     sleep 1
260     done
261    
262     if [ -n "$fallback" ]; then kill -$fallback $pid 2>/dev/null; fi
263    
264     kill -0 $pid 2>/dev/null && failure=1
265     done
266    
267     base=${1##*/}
268     if [ $failure = 0 ]; then rm -f /var/run/$base.pid; fi
269    
270     (exit $failure)
271     evaluate_retval
272     else
273     $SET_WCOL
274     print_status warning not_running
275     fi
276     }
277    
278     reloadproc()
279     {
280     if [ $# = 0 ]
281     then
282     echo "Usage: reloadproc {program} [signal]"
283     exit 1
284     fi
285    
286     if [ -z "$2" ]; then
287     signal=HUP
288     else
289     signal=${2##-}
290     signal=${signal##SIG}
291    
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 niro 317 kernel_version()
353     {
354     local KV="$(uname -r | cut -d- -f1)"
355     echo "${KV}"
356     }
357    
358 niro 2 kernel_major_version()
359     {
360     local KV
361 niro 317 KV="$(uname -r | cut -d. -f1-2)"
362 niro 2 echo "${KV}"
363     }
364    
365 niro 317 dolisting()
366 niro 2 {
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 niro 147
400     # searches /proc/mounts for mounted fstypes (like ext3)
401     is_fstype_mounted() {
402     local filesys
403     local i
404     filesys=$1
405    
406     i="$(cat /proc/mounts | grep ${filesys} | cut -d ' ' -f3)"
407     [[ ${i} != ${filesys} ]] && return 1
408    
409     return 0
410     }
411    
412     # searches /proc/mounts for mounted fs names (like udev, proc)
413     is_fs_mounted() {
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     local filesys
427     local i
428     filesys=$1
429    
430     i="$(cat /proc/filesystems | grep ${filesys} | cut -d $'\t' -f2)"
431     [[ ${i} != ${filesys} ]] && return 1
432    
433     return 0
434     }
435 niro 274
436     # bool is_older_than(reference, files/dirs to check)
437     #
438     # return 0 if any of the files/dirs are newer than
439     # the reference file
440     #
441     # EXAMPLE: if is_older_than a.out *.o ; then ...
442     is_older_than() {
443     local x=
444     local ref="$1"
445     shift
446    
447     for x in "$@"
448     do
449     [[ ${x} -nt ${ref} ]] && return 0
450    
451     if [[ -d ${x} ]]
452     then
453     is_older_than "${ref}" "${x}"/* && return 0
454     fi
455     done
456    
457     return 1
458     }

Properties

Name Value
svn:executable *