Magellan Linux

Contents of /trunk/initscripts/sysvinit/rc/functions

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

Name Value
svn:executable *