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