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