]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - src/initscripts/init.d/functions
Habe mal die Meldung geaendert, dass niemand die LFS-Leute informiert wenn er nen...
[people/pmueller/ipfire-2.x.git] / src / initscripts / init.d / functions
1 #!/bin/sh
2 ########################################################################
3 # Begin $rc_base/init.d/functions
4 #
5 # Description : Run Level Control Functions
6 #
7 # Authors : Gerard Beekmans - gerard@linuxfromscratch.org
8 #
9 # Version : 00.00
10 #
11 # Notes : With code based on Matthias Benkmann's simpleinit-msb
12 # http://winterdrache.de/linux/newboot/index.html
13 #
14 ########################################################################
15
16 ## Environmental setup
17 # Setup default values for environment
18 umask 022
19 export PATH="/bin:/usr/bin:/sbin:/usr/sbin"
20
21 # Signal sent to running processes to refresh their configuration
22 RELOADSIG="HUP"
23
24 # Number of seconds between STOPSIG and FALLBACK when stopping processes
25 KILLDELAY="3"
26
27 ## Screen Dimensions
28 # Find current screen size
29 if [ -z "${COLUMNS}" ]; then
30 COLUMNS=$(stty size)
31 COLUMNS=${COLUMNS##* }
32 fi
33
34 # When using remote connections, such as a serial port, stty size returns 0
35 if [ "${COLUMNS}" = "0" ]; then
36 COLUMNS=80
37 fi
38
39 ## Measurements for positioning result messages
40 COL=$((${COLUMNS} - 8))
41 WCOL=$((${COL} - 2))
42
43 ## Set Cursor Position Commands, used via echo -e
44 SET_COL="\\033[${COL}G" # at the $COL char
45 SET_WCOL="\\033[${WCOL}G" # at the $WCOL char
46 CURS_UP="\\033[1A\\033[0G" # Up one line, at the 0'th char
47
48 ## Set color commands, used via echo -e
49 # Please consult `man console_codes for more information
50 # under the "ECMA-48 Set Graphics Rendition" section
51 #
52 # Warning: when switching from a 8bit to a 9bit font,
53 # the linux console will reinterpret the bold (1;) to
54 # the top 256 glyphs of the 9bit font. This does
55 # not affect framebuffer consoles
56 NORMAL="\\033[0;39m" # Standard console grey
57 SUCCESS="\\033[1;32m" # Success is green
58 WARNING="\\033[1;33m" # Warnings are yellow
59 FAILURE="\\033[1;31m" # Failures are red
60 INFO="\\033[1;36m" # Information is light cyan
61 BRACKET="\\033[1;34m" # Brackets are blue
62
63 STRING_LENGTH="0" # the length of the current message
64
65 #*******************************************************************************
66 # Function - boot_mesg()
67 #
68 # Purpose: Sending information from bootup scripts to the console
69 #
70 # Inputs: $1 is the message
71 # $2 is the colorcode for the console
72 #
73 # Outputs: Standard Output
74 #
75 # Dependencies: - sed for parsing strings.
76 # - grep for counting string length.
77 #
78 # Todo:
79 #*******************************************************************************
80 boot_mesg()
81 {
82 local ECHOPARM=""
83
84 while true
85 do
86 case "${1}" in
87 -n)
88 ECHOPARM=" -n "
89 shift 1
90 ;;
91 -*)
92 echo "Unknown Option: ${1}"
93 return 1
94 ;;
95 *)
96 break
97 ;;
98 esac
99 done
100
101 ## Figure out the length of what is to be printed to be used
102 ## for warning messges.
103 STRING_LENGTH="`echo "${1}" | sed \
104 -e 's,.,.,g' -e 'l 1' | grep -c \$`"
105
106 # Print the message to the screen
107 echo ${ECHOPARM} -e "${2}${1}"
108
109 }
110
111 boot_mesg_flush()
112 {
113 # Reset STRING_LENGTH for next message
114 STRING_LENGTH="0"
115 }
116
117 boot_log()
118 {
119 # Left in for backwards compatibility
120 echo -n ""
121 }
122
123 echo_ok()
124 {
125 echo -n -e "${CURS_UP}${SET_COL}${BRACKET}[${SUCCESS} OK ${BRACKET}]"
126 echo -e "${NORMAL}"
127 boot_mesg_flush
128 }
129
130 echo_failure()
131 {
132 echo -n -e "${CURS_UP}${SET_COL}${BRACKET}[${FAILURE} FAIL ${BRACKET}]"
133 echo -e "${NORMAL}"
134 boot_mesg_flush
135 }
136
137 echo_warning()
138 {
139 echo -n -e "${CURS_UP}${SET_COL}${BRACKET}[${WARNING} WARN ${BRACKET}]"
140 echo -e "${NORMAL}"
141 boot_mesg_flush
142 }
143
144 print_error_msg()
145 {
146 echo_failure
147 # $i is inherited by the rc script
148 boot_mesg -n "FAILURE:\n\nYou should not be reading this error message.\n\n" ${FAILURE}
149 boot_mesg -n " It means that an unforeseen error took"
150 boot_mesg -n " place in ${i}, which exited with a return value of"
151 boot_mesg " ${error_value}.\n"
152 boot_mesg_flush
153 boot_mesg -n "If you're able to track this"
154 boot_mesg -n " error down to a bug in one of the files provided by"
155 boot_mesg -n " ipfire, please be so kind to inform us at"
156 boot_mesg " info@ipfire.org.\n"
157 boot_mesg_flush
158 boot_mesg -n "Press Enter to continue or wait 3 minutes..." ${INFO}
159 boot_mesg "" ${NORMAL}
160 while sleep 180; do
161 read ENTER
162 break
163 done
164 }
165
166 check_script_status()
167 {
168 # $i is inherited by the rc script
169 if [ ! -f ${i} ]; then
170 boot_mesg "${i} is not a valid symlink." ${WARNING}
171 echo_warning
172 continue
173 fi
174
175 if [ ! -x ${i} ]; then
176 boot_mesg "${i} is not executable, skipping." ${WARNING}
177 echo_warning
178 continue
179 fi
180 }
181
182 evaluate_retval()
183 {
184 error_value="${?}"
185
186 if [ ${error_value} = 0 ]; then
187 echo_ok
188 else
189 echo_failure
190 fi
191
192 # This prevents the 'An Unexpected Error Has Occurred' from trivial
193 # errors.
194 return 0
195 }
196
197 print_status()
198 {
199 if [ "${#}" = "0" ]; then
200 echo "Usage: ${0} {success|warning|failure}"
201 return 1
202 fi
203
204 case "${1}" in
205
206 success)
207 echo_ok
208 ;;
209
210 warning)
211 # Leave this extra case in because old scripts
212 # may call it this way.
213 case "${2}" in
214 running)
215 echo -e -n "${CURS_UP}"
216 echo -e -n "\\033[${STRING_LENGTH}G "
217 boot_mesg "Already running." ${WARNING}
218 echo_warning
219 ;;
220 not_running)
221 echo -e -n "${CURS_UP}"
222 echo -e -n "\\033[${STRING_LENGTH}G "
223 boot_mesg "Not running." ${WARNING}
224 echo_warning
225 ;;
226 not_available)
227 echo -e -n "${CURS_UP}"
228 echo -e -n "\\033[${STRING_LENGTH}G "
229 boot_mesg "Not available." ${WARNING}
230 echo_warning
231 ;;
232 *)
233 # This is how it is supposed to
234 # be called
235 echo_warning
236 ;;
237 esac
238 ;;
239
240 failure)
241 echo_failure
242 ;;
243
244 esac
245
246 }
247
248 reloadproc()
249 {
250 if [ "${#}" = "0" ]; then
251 echo "Usage: reloadproc [{program}]"
252 exit 1
253 fi
254
255 getpids "${1}"
256
257 if [ -n "${pidlist}" ]; then
258 failure="0"
259 for pid in ${pidlist}
260 do
261 kill -"${RELOADSIG}" "${pid}" || failure="1"
262 done
263
264 (exit ${failure})
265 evaluate_retval
266
267 else
268 boot_mesg "Process ${1} not running." ${WARNING}
269 echo_warning
270 fi
271 }
272
273 statusproc()
274 {
275 if [ "${#}" = "0" ]
276 then
277 echo "Usage: statusproc {program}"
278 exit 1
279 fi
280
281 getpids "${1}"
282
283 if [ -n "${pidlist}" ]; then
284 echo -e "${INFO}${base} is running with Process"\
285 "ID(s) ${pidlist}.${NORMAL}"
286 else
287 if [ -n "${base}" -a -e "/var/run/${base}.pid" ]; then
288 echo -e "${WARNING}${1} is not running but"\
289 "/var/run/${base}.pid exists.${NORMAL}"
290 else
291 if [ -n "${PIDFILE}" -a -e "${PIDFILE}" ]; then
292 echo -e "${WARNING}${1} is not running"\
293 "but ${PIDFILE} exists.${NORMAL}"
294 else
295 echo -e "${INFO}${1} is not running.${NORMAL}"
296 fi
297 fi
298 fi
299 }
300
301 # The below functions are documented in the LSB-generic 2.1.0
302
303 #*******************************************************************************
304 # Function - pidofproc [-s] [-p pidfile] pathname
305 #
306 # Purpose: This function returns one or more pid(s) for a particular daemon
307 #
308 # Inputs: -p pidfile, use the specified pidfile instead of pidof
309 # pathname, path to the specified program
310 #
311 # Outputs: return 0 - Success, pid's in stdout
312 # return 1 - Program is dead, pidfile exists
313 # return 2 - Invalid or excessive number of arguments,
314 # warning in stdout
315 # return 3 - Program is not running
316 #
317 # Dependencies: pidof, echo, head
318 #
319 # Todo: Remove dependency on head
320 # This depreciates getpids
321 # Test changes to pidof
322 #
323 #*******************************************************************************
324 pidofproc()
325 {
326 local pidfile=""
327 local lpids=""
328 local silent=""
329 pidlist=""
330 while true
331 do
332 case "${1}" in
333 -p)
334 pidfile="${2}"
335 shift 2
336 ;;
337
338 -s)
339 # Added for legacy opperation of getpids
340 # eliminates several '> /dev/null'
341 silent="1"
342 shift 1
343 ;;
344 -*)
345 log_failure_msg "Unknown Option: ${1}"
346 return 2
347 ;;
348 *)
349 break
350 ;;
351 esac
352 done
353
354 if [ "${#}" != "1" ]; then
355 shift 1
356 log_failure_msg "Usage: pidofproc [-s] [-p pidfile] pathname"
357 return 2
358 fi
359
360 if [ -n "${pidfile}" ]; then
361 if [ ! -r "${pidfile}" ]; then
362 return 3 # Program is not running
363 fi
364
365 lpids=`head -n 1 ${pidfile}`
366 for pid in ${lpids}
367 do
368 if [ "${pid}" -ne "$$" -a "${pid}" -ne "${PPID}" ]; then
369 kill -0 "${pid}" > /dev/null &&
370 pidlist="${pidlist} ${pid}"
371 fi
372
373 if [ "${silent}" -ne "1" ]; then
374 echo "${pidlist}"
375 fi
376
377 test -z "${pidlist}" &&
378 # Program is dead, pidfile exists
379 return 1
380 # else
381 return 0
382 done
383
384 else
385 pidlist=`pidof -o $$ -o $PPID -x "$1"`
386 if [ "x${silent}" != "x1" ]; then
387 echo "${pidlist}"
388 fi
389
390 # Get provide correct running status
391 if [ -n "${pidlist}" ]; then
392 return 0
393 else
394 return 3
395 fi
396
397 fi
398
399 if [ "$?" != "0" ]; then
400 return 3 # Program is not running
401 fi
402 }
403
404 # This will ensure compatibility with previous LFS Bootscripts
405 getpids()
406 {
407 if [ -z "${PIDFILE}" ]; then
408 pidofproc -s -p "${PIDFILE}" $@
409 else
410 pidofproc -s $@
411 fi
412 base="${1##*/}"
413 }
414
415 #*******************************************************************************
416 # Function - loadproc [-f] [-n nicelevel] [-p pidfile] pathname [args]
417 #
418 # Purpose: This runs the specified program as a daemon
419 #
420 # Inputs: -f, run the program even if it is already running
421 # -n nicelevel, specifies a nice level. See nice(1).
422 # -p pidfile, uses the specified pidfile
423 # pathname, pathname to the specified program
424 # args, arguments to pass to specified program
425 #
426 # Outputs: return 0 - Success
427 # return 2 - Invalid of excessive number of arguments,
428 # warning in stdout
429 # return 4 - Program or service status is unknown
430 #
431 # Dependencies: nice
432 #
433 # Todo: LSB says this should be called start_daemon
434 # LSB does not say that it should call evaluate_retval
435 # It checks for PIDFILE, which is deprecated.
436 # Will be removed after BLFS 6.0
437 # loadproc returns 0 if program is already running, not LSB compliant
438 #
439 #*******************************************************************************
440 loadproc()
441 {
442 local pidfile=""
443 local forcestart=""
444 local nicelevel="10"
445
446 # This will ensure compatibility with previous LFS Bootscripts
447 if [ -n "${PIDFILE}" ]; then
448 pidfile="${PIDFILE}"
449 fi
450
451 while true
452 do
453 case "${1}" in
454 -f)
455 forcestart="1"
456 shift 1
457 ;;
458 -n)
459 nicelevel="${2}"
460 shift 2
461 ;;
462 -p)
463 pidfile="${2}"
464 shift 2
465 ;;
466 -*)
467 log_failure_msg "Unknown Option: ${1}"
468 return 2 #invalid or excess argument(s)
469 ;;
470 *)
471 break
472 ;;
473 esac
474 done
475
476 if [ "${#}" = "0" ]; then
477 log_failure_msg "Usage: loadproc [-f] [-n nicelevel] [-p pidfile] pathname [args]"
478 return 2 #invalid or excess argument(s)
479 fi
480
481 if [ -z "${forcestart}" ]; then
482 if [ -z "${pidfile}" ]; then
483 pidofproc -s "${1}"
484 else
485 pidofproc -s -p "${pidfile}" "${1}"
486 fi
487
488 case "${?}" in
489 0)
490 log_warning_msg "Unable to continue: ${1} is running"
491 return 0 # 4
492 ;;
493 1)
494 log_warning_msg "Unable to continue: ${pidfile} exists"
495 return 0 # 4
496 ;;
497 3)
498 ;;
499 *)
500 log_failure_msg "Unknown error code from pidofproc: ${?}"
501 return 4
502 ;;
503 esac
504 fi
505
506 nice -n "${nicelevel}" "${@}"
507 evaluate_retval # This is "Probably" not LSB compliant, but required to be compatible with older bootscripts
508 return 0
509 }
510
511 #*******************************************************************************
512 # Function - killproc [-p pidfile] pathname [signal]
513 #
514 # Purpose:
515 #
516 # Inputs: -p pidfile, uses the specified pidfile
517 # pathname, pathname to the specified program
518 # signal, send this signal to pathname
519 #
520 # Outputs: return 0 - Success
521 # return 2 - Invalid of excessive number of arguments,
522 # warning in stdout
523 # return 4 - Unknown Status
524 #
525 # Dependencies: kill
526 #
527 # Todo: LSB does not say that it should call evaluate_retval
528 # It checks for PIDFILE, which is deprecated.
529 # Will be removed after BLFS 6.0
530 #
531 #*******************************************************************************
532 killproc()
533 {
534 local pidfile=""
535 local killsig=""
536 pidlist=""
537
538 # This will ensure compatibility with previous LFS Bootscripts
539 if [ -n "${PIDFILE}" ]; then
540 pidfile="${PIDFILE}"
541 fi
542
543 while true
544 do
545 case "${1}" in
546 -p)
547 pidfile="${2}"
548 shift 2
549 ;;
550 -*)
551 log_failure_msg "Unknown Option: ${1}"
552 return 2
553 ;;
554 *)
555 break
556 ;;
557 esac
558 done
559
560 if [ "${#}" = "2" ]; then
561 killsig="${2}"
562 elif [ "${#}" != "1" ]; then
563 shift 2
564 log_failure_msg "Usage: killproc [-p pidfile] pathname [signal]"
565 return 2
566 fi
567
568 if [ -z "${pidfile}" ]; then
569 pidofproc -s "${1}"
570 else
571 pidofproc -s -p "${pidfile}" "${1}"
572 fi
573
574 # Change....
575 if [ -n "${pidlist}" ]; then
576 for pid in ${pidlist}
577 do
578 kill -${killsig:-TERM} ${pid} 2>/dev/null
579 if [ -z "${killsig}" ]; then
580 # Wait up to 3 seconds, for ${pid} to terminate
581 local dtime=${KILLDELAY}
582 while [ "${dtime}" != "0" ]
583 do
584 kill -0 ${pid} 2>/dev/null || break
585 sleep 1
586 dtime=$(( ${dtime} - 1))
587 done
588 # If ${pid} is still running, kill it
589 kill -0 ${pid} 2>/dev/null && kill -KILL ${pid} 2>/dev/null
590 fi
591 done
592
593 if [ -z "${killsig}" ]; then
594 pidofproc -s "${1}"
595
596 # Program was terminated
597 if [ "$?" != "0" ]; then
598 # Pidfile Exists
599 if [ -f "${pidfile}" ]; then
600 rm -f "${pidfile}"
601 fi
602 echo_ok
603 return 0
604 else # Program is still running
605 echo_failure
606 return 4 # Unknown Status
607 fi
608 else
609 if [ -z "${pidfile}" ]; then
610 pidofproc -s "${1}"
611 else
612 pidofproc -s -p "${pidfile}" "${1}"
613 fi
614 fi
615
616 evaluate_retval # This is "Probably" not LSB compliant, but required to be compatible with older bootscripts
617
618 else
619 print_status warning not_running
620 fi
621 }
622
623
624 #*******************************************************************************
625 # Function - log_success_msg "message"
626 #
627 # Purpose: Print a success message
628 #
629 # Inputs: $@ - Message
630 #
631 # Outputs: Text output to screen
632 #
633 # Dependencies: echo
634 #
635 # Todo: logging
636 #
637 #*******************************************************************************
638 log_success_msg()
639 {
640 echo -n -e "${BOOTMESG_PREFIX}${@}"
641 echo -e "${SET_COL}""${BRACKET}""[""${SUCCESS}"" OK ""${BRACKET}""]""${NORMAL}"
642 return 0
643 }
644
645 #*******************************************************************************
646 # Function - log_failure_msg "message"
647 #
648 # Purpose: Print a failure message
649 #
650 # Inputs: $@ - Message
651 #
652 # Outputs: Text output to screen
653 #
654 # Dependencies: echo
655 #
656 # Todo: logging
657 #
658 #*******************************************************************************
659 log_failure_msg() {
660 echo -n -e "${BOOTMESG_PREFIX}${@}"
661 echo -e "${SET_COL}""${BRACKET}""[""${FAILURE}"" FAIL ""${BRACKET}""]""${NORMAL}"
662 return 0
663 }
664
665 #*******************************************************************************
666 # Function - log_warning_msg "message"
667 #
668 # Purpose: print a warning message
669 #
670 # Inputs: $@ - Message
671 #
672 # Outputs: Text output to screen
673 #
674 # Dependencies: echo
675 #
676 # Todo: logging
677 #
678 #*******************************************************************************
679 log_warning_msg() {
680 echo -n -e "${BOOTMESG_PREFIX}${@}"
681 echo -e "${SET_COL}""${BRACKET}""[""${WARNING}"" WARN ""${BRACKET}""]""${NORMAL}"
682 return 0
683 }
684
685 animate()
686 {
687 if [ $# = 0 ]
688 then
689 echo "Usage: animate {hook}"
690 exit 1
691 fi
692
693 splash "$*"
694 }
695
696 # End $rc_base/init.d/functions