/[vps]/baselayout-vserver/branches/baselayout-1_12/sbin/functions.sh
Gentoo

Contents of /baselayout-vserver/branches/baselayout-1_12/sbin/functions.sh

Parent Directory Parent Directory | Revision Log Revision Log


Revision 206 - (show annotations) (download) (as text)
Sun Jan 15 10:27:03 2006 UTC (10 years, 6 months ago) by phreak
File MIME type: text/x-sh
File size: 16089 byte(s)
Merging r1822 for branches/baselayout-1_12
1 # Copyright 1999-2006 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3
4 RC_GOT_FUNCTIONS="yes"
5
6 # Override defaults with user settings ...
7 [ -f /etc/conf.d/rc ] && source /etc/conf.d/rc
8
9 # daemontools dir
10 SVCDIR="/var/lib/supervise"
11
12 # Check /etc/conf.d/rc for a description of these ...
13 svclib="/lib/rcscripts"
14 svcdir=${svcdir:-/var/lib/init.d}
15
16 # Different types of dependencies
17 deptypes="need use"
18 # Different types of order deps
19 ordtypes="before after"
20
21 #
22 # Internal variables
23 #
24
25 # Dont output to stdout?
26 RC_QUIET_STDOUT="no"
27 RC_VERBOSE=${RC_VERBOSE:-no}
28
29 # Should we use color?
30 RC_NOCOLOR=${RC_NOCOLOR:-no}
31 # Can the terminal handle endcols?
32 RC_ENDCOL="yes"
33
34 #
35 # Default values for rc system
36 #
37 RC_TTY_NUMBER=${RC_TTY_NUMBER:-0}
38 RC_PARALLEL_STARTUP=${RC_PARALLEL_STARTUP:-no}
39 RC_NET_STRICT_CHECKING=${RC_NET_STRICT_CHECKING:-none}
40 RC_DOWN_INTERFACE=${RC_DOWN_INTERFACE:-yes}
41
42 #
43 # Default values for e-message indentation and dots
44 #
45 RC_INDENTATION=''
46 RC_DEFAULT_INDENT=2
47 #RC_DOT_PATTERN=' .'
48 RC_DOT_PATTERN=''
49
50 # void import_addon(char *addon)
51 #
52 # Import code from the specified addon if it exists
53 #
54 import_addon() {
55 local addon="${svclib}/addons/$1"
56 if [[ -r ${addon} ]] ; then
57 source "${addon}"
58 return 0
59 fi
60 return 1
61 }
62
63 # void splash(...)
64 #
65 # Notify bootsplash/splashutils/gensplash/whatever about
66 # important events.
67 #
68 splash() {
69 return 0
70 }
71
72 # void profiling(...)
73 #
74 # Notify bootsplash/whatever about important events.
75 #
76 profiling() {
77 return 0
78 }
79
80 # void bootlog(...)
81 #
82 # Notify bootlogger about important events.
83 bootlog() {
84 return 0
85 }
86
87 # void get_bootconfig()
88 #
89 # Get the BOOTLEVEL and SOFTLEVEL by setting
90 # 'bootlevel' and 'softlevel' via kernel
91 # parameters.
92 #
93 get_bootconfig() {
94 export BOOTLEVEL="boot"
95 export DEFAULTLEVEL="default"
96
97 return 0
98 }
99
100 setup_defaultlevels() {
101 get_bootconfig
102
103 if [[ -z ${SOFTLEVEL} ]] ; then
104 if [[ -f "${svcdir}/softlevel" ]] ; then
105 export SOFTLEVEL=$(< "${svcdir}/softlevel")
106 else
107 export SOFTLEVEL="${BOOTLEVEL}"
108 fi
109 fi
110
111 return 0
112 }
113
114 # void get_libdir(void)
115 #
116 # prints the current libdir {lib,lib32,lib64}
117 #
118 get_libdir() {
119 if [[ -n ${CONF_LIBDIR_OVERRIDE} ]] ; then
120 CONF_LIBDIR="${CONF_LIBDIR_OVERRIDE}"
121 elif [[ -x /usr/bin/portageq ]] ; then
122 CONF_LIBDIR=$(/usr/bin/portageq envvar CONF_LIBDIR)
123 fi
124 echo ${CONF_LIBDIR:=lib}
125 }
126
127 # void esyslog(char* priority, char* tag, char* message)
128 #
129 # use the system logger to log a message
130 #
131 esyslog() {
132 local pri=
133 local tag=
134
135 if [[ -x /usr/bin/logger ]] ; then
136 pri="$1"
137 tag="$2"
138
139 shift 2
140 [[ -z "$*" ]] && return 0
141
142 /usr/bin/logger -p "${pri}" -t "${tag}" -- "$*"
143 fi
144
145 return 0
146 }
147
148 # void eindent(int num)
149 #
150 # increase the indent used for e-commands.
151 #
152 eindent() {
153 local i="$1"
154 (( i > 0 )) || (( i = RC_DEFAULT_INDENT ))
155 esetdent $(( ${#RC_INDENTATION} + i ))
156 }
157
158 # void eoutdent(int num)
159 #
160 # decrease the indent used for e-commands.
161 #
162 eoutdent() {
163 local i="$1"
164 (( i > 0 )) || (( i = RC_DEFAULT_INDENT ))
165 esetdent $(( ${#RC_INDENTATION} - i ))
166 }
167
168 # void esetdent(int num)
169 #
170 # hard set the indent used for e-commands.
171 # num defaults to 0
172 #
173 esetdent() {
174 local i="$1"
175 (( i < 0 )) && (( i = 0 ))
176 RC_INDENTATION=$(printf "%${i}s" '')
177 }
178
179 # void einfo(char* message)
180 #
181 # show an informative message (with a newline)
182 #
183 einfo() {
184 einfon "$*\n"
185 LAST_E_CMD="einfo"
186 return 0
187 }
188
189 # void einfon(char* message)
190 #
191 # show an informative message (without a newline)
192 #
193 einfon() {
194 [[ ${RC_QUIET_STDOUT} == "yes" ]] && return 0
195 [[ ${RC_ENDCOL} != "yes" && ${LAST_E_CMD} == "ebegin" ]] && echo
196 echo -ne " ${GOOD}*${NORMAL} ${RC_INDENTATION}$*"
197 LAST_E_CMD="einfon"
198 return 0
199 }
200
201 # void ewarn(char* message)
202 #
203 # show a warning message + log it
204 #
205 ewarn() {
206 if [[ ${RC_QUIET_STDOUT} == "yes" ]] ; then
207 echo " $*"
208 else
209 [[ ${RC_ENDCOL} != "yes" && ${LAST_E_CMD} == "ebegin" ]] && echo
210 echo -e " ${WARN}*${NORMAL} ${RC_INDENTATION}$*"
211 fi
212
213 # Log warnings to system log
214 esyslog "daemon.warning" "rc-scripts" "$*"
215
216 LAST_E_CMD="ewarn"
217 return 0
218 }
219
220 # void eerror(char* message)
221 #
222 # show an error message + log it
223 #
224 eerror() {
225 if [[ ${RC_QUIET_STDOUT} == "yes" ]] ; then
226 echo " $*" >/dev/stderr
227 else
228 [[ ${RC_ENDCOL} != "yes" && ${LAST_E_CMD} == "ebegin" ]] && echo
229 echo -e " ${BAD}*${NORMAL} ${RC_INDENTATION}$*"
230 fi
231
232 # Log errors to system log
233 esyslog "daemon.err" "rc-scripts" "$*"
234
235 LAST_E_CMD="eerror"
236 return 0
237 }
238
239 # void ebegin(char* message)
240 #
241 # show a message indicating the start of a process
242 #
243 ebegin() {
244 local msg="$*" dots spaces=${RC_DOT_PATTERN//?/ }
245 [[ ${RC_QUIET_STDOUT} == "yes" ]] && return 0
246
247 if [[ -n ${RC_DOT_PATTERN} ]] ; then
248 dots=$(printf "%$(( COLS - 3 - ${#RC_INDENTATION} - ${#msg} - 7 ))s" '')
249 dots=${dots//${spaces}/${RC_DOT_PATTERN}}
250 msg="${msg}${dots}"
251 else
252 msg="${msg} ..."
253 fi
254 einfon "${msg}"
255 [[ ${RC_ENDCOL} == "yes" ]] && echo
256
257 LAST_E_LEN=$(( 3 + ${#RC_INDENTATION} + ${#msg} ))
258 LAST_E_CMD="ebegin"
259 return 0
260 }
261
262 # void _eend(int error, char *efunc, char* errstr)
263 #
264 # indicate the completion of process, called from eend/ewend
265 # if error, show errstr via efunc
266 #
267 # This function is private to functions.sh. Do not call it from a
268 # script.
269 #
270 _eend() {
271 local retval=${1:-0} efunc=${2:-eerror} msg
272 shift 2
273
274 if [[ ${retval} == "0" ]] ; then
275 [[ ${RC_QUIET_STDOUT} == "yes" ]] && return 0
276 msg="${BRACKET}[ ${GOOD}ok${BRACKET} ]${NORMAL}"
277 else
278 if [[ -n $* ]] ; then
279 ${efunc} "$*"
280 fi
281 msg="${BRACKET}[ ${BAD}!!${BRACKET} ]${NORMAL}"
282 fi
283
284 if [[ ${RC_ENDCOL} == "yes" ]] ; then
285 echo -e "${ENDCOL} ${msg}"
286 else
287 [[ ${LAST_E_CMD} == ebegin ]] || LAST_E_LEN=0
288 printf "%$(( COLS - LAST_E_LEN - 6 ))s%b\n" '' "${msg}"
289 fi
290
291 return ${retval}
292 }
293
294 # void eend(int error, char* errstr)
295 #
296 # indicate the completion of process
297 # if error, show errstr via eerror
298 #
299 eend() {
300 local retval=${1:-0}
301 shift
302
303 _eend ${retval} eerror "$*"
304
305 LAST_E_CMD="eend"
306 return ${retval}
307 }
308
309 # void ewend(int error, char* errstr)
310 #
311 # indicate the completion of process
312 # if error, show errstr via ewarn
313 #
314 ewend() {
315 local retval=${1:-0}
316 shift
317
318 _eend ${retval} ewarn "$*"
319
320 LAST_E_CMD="ewend"
321 return ${retval}
322 }
323
324 # v-e-commands honor RC_VERBOSE which defaults to no.
325 # The condition is negated so the return value will be zero.
326 veinfo() { [[ ${RC_VERBOSE} != "yes" ]] || einfo "$@"; }
327 veinfon() { [[ ${RC_VERBOSE} != "yes" ]] || einfon "$@"; }
328 vewarn() { [[ ${RC_VERBOSE} != "yes" ]] || ewarn "$@"; }
329 veerror() { [[ ${RC_VERBOSE} != "yes" ]] || eerror "$@"; }
330 vebegin() { [[ ${RC_VERBOSE} != "yes" ]] || ebegin "$@"; }
331 veend() {
332 [[ ${RC_VERBOSE} == "yes" ]] && { eend "$@"; return $?; }
333 return ${1:-0}
334 }
335 veend() {
336 [[ ${RC_VERBOSE} == "yes" ]] && { ewend "$@"; return $?; }
337 return ${1:-0}
338 }
339
340 # char *KV_major(string)
341 #
342 # Return the Major (X of X.Y.Z) kernel version
343 #
344 KV_major() {
345 [[ -z $1 ]] && return 1
346
347 local KV=$@
348 echo "${KV%%.*}"
349 }
350
351 # char *KV_minor(string)
352 #
353 # Return the Minor (Y of X.Y.Z) kernel version
354 #
355 KV_minor() {
356 [[ -z $1 ]] && return 1
357
358 local KV=$@
359 KV=${KV#*.}
360 echo "${KV%%.*}"
361 }
362
363 # char *KV_micro(string)
364 #
365 # Return the Micro (Z of X.Y.Z) kernel version.
366 #
367 KV_micro() {
368 [[ -z $1 ]] && return 1
369
370 local KV=$@
371 KV=${KV#*.*.}
372 echo "${KV%%[^[:digit:]]*}"
373 }
374
375 # int KV_to_int(string)
376 #
377 # Convert a string type kernel version (2.4.0) to an int (132096)
378 # for easy compairing or versions ...
379 #
380 KV_to_int() {
381 [[ -z $1 ]] && return 1
382
383 local KV_MAJOR=$(KV_major "$1")
384 local KV_MINOR=$(KV_minor "$1")
385 local KV_MICRO=$(KV_micro "$1")
386 local KV_int=$(( KV_MAJOR * 65536 + KV_MINOR * 256 + KV_MICRO ))
387
388 # We make version 2.2.0 the minimum version we will handle as
389 # a sanity check ... if its less, we fail ...
390 if [[ ${KV_int} -ge 131584 ]] ; then
391 echo "${KV_int}"
392 return 0
393 fi
394
395 return 1
396 }
397
398 # int get_KV()
399 #
400 # Return the kernel version (major, minor and micro concated) as an integer.
401 # Assumes X and Y of X.Y.Z are numbers. Also assumes that some leading
402 # portion of Z is a number.
403 # e.g. 2.4.25, 2.6.10, 2.6.4-rc3, 2.2.40-poop, 2.0.15+foo
404 #
405 _RC_GET_KV_CACHE=""
406 get_KV() {
407 [[ -z ${_RC_GET_KV_CACHE} ]] \
408 && _RC_GET_KV_CACHE=$(uname -r)
409
410 echo $(KV_to_int "${_RC_GET_KV_CACHE}")
411
412 return $?
413 }
414
415 # bool get_bootparam(param)
416 #
417 # return 0 if gentoo=param was passed to the kernel
418 #
419 # EXAMPLE: if get_bootparam "nodevfs" ; then ....
420 #
421 get_bootparam() {
422 return 1
423 }
424
425 # Safer way to list the contents of a directory,
426 # as it do not have the "empty dir bug".
427 #
428 # char *dolisting(param)
429 #
430 # print a list of the directory contents
431 #
432 # NOTE: quote the params if they contain globs.
433 # also, error checking is not that extensive ...
434 #
435 dolisting() {
436 local x=
437 local y=
438 local tmpstr=
439 local mylist=
440 local mypath="$*"
441
442 if [[ ${mypath%/\*} != "${mypath}" ]] ; then
443 mypath=${mypath%/\*}
444 fi
445
446 for x in ${mypath} ; do
447 [[ ! -e ${x} ]] && continue
448
449 if [[ ! -d ${x} ]] && [[ -L ${x} || -f ${x} ]] ; then
450 mylist="${mylist} $(ls "${x}" 2> /dev/null)"
451 else
452 [[ ${x%/} != "${x}" ]] && x=${x%/}
453
454 cd "${x}"; tmpstr=$(ls)
455
456 for y in ${tmpstr} ; do
457 mylist="${mylist} ${x}/${y}"
458 done
459 fi
460 done
461
462 echo "${mylist}"
463 }
464
465 # void save_options(char *option, char *optstring)
466 #
467 # save the settings ("optstring") for "option"
468 #
469 save_options() {
470 local myopts="$1"
471
472 shift
473 if [[ ! -d "${svcdir}/options/${myservice}" ]] ; then
474 mkdir -p -m 0755 "${svcdir}/options/${myservice}"
475 fi
476
477 echo "$*" > "${svcdir}/options/${myservice}/${myopts}"
478
479 return 0
480 }
481
482 # char *get_options(char *option)
483 #
484 # get the "optstring" for "option" that was saved
485 # by calling the save_options function
486 #
487 get_options() {
488 if [[ -f "${svcdir}/options/${myservice}/$1" ]] ; then
489 echo "$(< ${svcdir}/options/${myservice}/$1)"
490 fi
491
492 return 0
493 }
494
495 # char *add_suffix(char * configfile)
496 #
497 # Returns a config file name with the softlevel suffix
498 # appended to it. For use with multi-config services.
499 add_suffix() {
500 echo "$1"
501 return 0
502 }
503
504 # char *get_base_ver()
505 #
506 # get the version of baselayout that this system is running
507 #
508 get_base_ver() {
509 [[ ! -r /etc/gentoo-release ]] && return 0
510 local ver=$(</etc/gentoo-release)
511 echo "${ver##* }"
512 }
513
514 # Network filesystems list for common use in rc-scripts.
515 # This variable is used in is_net_fs and other places such as
516 # localmount.
517 NET_FS_LIST="afs cifs coda davfs fuse gfs ncpfs nfs nfs4 ocfs2 shfs smbfs"
518
519 # bool is_net_fs(path)
520 #
521 # return 0 if path is the mountpoint of a networked filesystem
522 #
523 # EXAMPLE: if is_net_fs / ; then ...
524 #
525 is_net_fs() {
526 local fstype
527 # /proc/mounts is always accurate
528 fstype=$( sed -n -e '/^rootfs/!s:.* '"$1"' \([^ ]*\).*:\1:p' /proc/mounts )
529 [[ " ${NET_FS_LIST} " == *" ${fstype} "* ]]
530 return $?
531 }
532
533 # bool is_uml_sys()
534 #
535 # return 0 if the currently running system is User Mode Linux
536 #
537 # EXAMPLE: if is_uml_sys ; then ...
538 #
539 is_uml_sys() {
540 return 1
541 }
542
543 # bool is_vserver_sys()
544 #
545 # return 0 if the currently running system is a Linux VServer
546 #
547 # EXAMPLE: if is_vserver_sys ; then ...
548 #
549 is_vserver_sys() {
550 return 0
551 }
552
553 # bool is_xenU_sys()
554 #
555 # return 0 if the currently running system is an unprivileged Xen domain
556 #
557 # EXAMPLE: if is_xenU_sys ; then ...
558 #
559 is_xenU_sys() {
560 return 1
561 }
562
563 # bool get_mount_fstab(path)
564 #
565 # return the parameters to pass to the mount command generated from fstab
566 #
567 # EXAMPLE: cmd=$( get_mount_fstab /proc )
568 # cmd=${cmd:--t proc none /proc}
569 # mount -n ${cmd}
570 #
571 get_mount_fstab() {
572 echo
573 }
574
575 # char *reverse_list(list)
576 #
577 # Returns the reversed order of list
578 #
579 reverse_list() {
580 for (( i = $# ; i > 0 ; --i )) ; do
581 echo -n "${!i} "
582 done
583 }
584
585 # void start_addon(addon)
586 #
587 # Starts addon.
588 #
589 start_addon() {
590 local addon="$1"
591 (import_addon "${addon}-start.sh")
592 return 0
593 }
594
595 # void start_volumes()
596 #
597 # Starts all volumes in RC_VOLUME_ORDER.
598 #
599 start_volumes() {
600 return 0
601 }
602
603 # void stop_addon(addon)
604 #
605 # Stops addon.
606 #
607 stop_addon() {
608 local addon=$1
609 (import_addon "${addon}-stop.sh")
610 return 0
611 }
612
613 # void stop_volumes()
614 #
615 # Stops all volumes in RC_VOLUME_ORDER (reverse order).
616 #
617 stop_volumes() {
618 return 0
619 }
620
621 # bool is_older_than(reference, files/dirs to check)
622 #
623 # return 0 if any of the files/dirs are newer than
624 # the reference file
625 #
626 # EXAMPLE: if is_older_than a.out *.o ; then ...
627 is_older_than() {
628 local x=
629 local ref="$1"
630 shift
631
632 for x in "$@" ; do
633 [[ ${x} -nt "${ref}" ]] && return 0
634
635 if [[ -d ${x} ]] ; then
636 is_older_than "${ref}" "${x}"/* && return 0
637 fi
638 done
639
640 return 1
641 }
642
643 # char* bash_variable(char *variable)
644 #
645 # Turns the given variable into something that bash can use
646 # Basically replaces anything not a-z,A-Z into a _
647 #
648 bash_variable() {
649 local args="$@"
650 LC_ALL=C echo "${args//[![:word:]]/_}"
651 }
652
653 # void requote()
654 #
655 # Requotes params so they're suitable to be eval'd, just like this would:
656 # set -- 1 2 "3 4"
657 # /usr/bin/getopt -- '' "$@" | sed 's/^ -- //'
658 #
659 requote() {
660 local q=\'
661 set -- "${@//\'/$q\'$q}" # quote inner instances of '
662 set -- "${@/#/$q}" # add ' to start of each param
663 set -- "${@/%/$q}" # add ' to end of each param
664 echo "$*"
665 }
666
667 ##############################################################################
668 # #
669 # This should be the last code in here, please add all functions above!! #
670 # #
671 # *** START LAST CODE *** #
672 # #
673 ##############################################################################
674
675 if [[ -z ${EBUILD} ]] ; then
676 # Setup a basic $PATH. Just add system default to existing.
677 # This should solve both /sbin and /usr/sbin not present when
678 # doing 'su -c foo', or for something like: PATH= rcscript start
679 PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/sbin:${PATH}"
680
681 # Cache the CONSOLETYPE - this is important as backgrounded shells don't
682 # have a TTY. rc unsets it at the end of running so it shouldn't hang
683 # around
684 if [[ -z ${CONSOLETYPE} ]] ; then
685 export CONSOLETYPE=$( /sbin/consoletype 2>/dev/null )
686 fi
687 if [[ ${CONSOLETYPE} == "serial" ]] ; then
688 RC_NOCOLOR="yes"
689 RC_ENDCOL="no"
690 fi
691
692 for arg in "$@" ; do
693 case "${arg}" in
694 # Lastly check if the user disabled it with --nocolor argument
695 --nocolor|-nc)
696 RC_NOCOLOR="yes"
697 ;;
698 esac
699 done
700
701 setup_defaultlevels
702 else
703 setup_defaultlevels
704 # Should we use colors ?
705 if [[ $* != *depend* ]] ; then
706 # Check user pref in portage
707 RC_NOCOLOR="$(portageq envvar NOCOLOR 2>/dev/null)"
708 [[ ${RC_NOCOLOR} == "true" ]] && RC_NOCOLOR="yes"
709 else
710 # We do not want colors during emerge depend
711 RC_NOCOLOR="yes"
712 # No output is seen during emerge depend, so this is not needed.
713 RC_ENDCOL="no"
714 fi
715 fi
716
717 if [[ -n ${EBUILD} && $* == *depend* ]] ; then
718 # We do not want stty to run during emerge depend
719 COLS=80
720 else
721 # Setup COLS and ENDCOL so eend can line up the [ ok ]
722 COLS=${COLUMNS:-0} # bash's internal COLUMNS variable
723 (( COLS == 0 )) && COLS=$(stty size 2>/dev/null | cut -d' ' -f2)
724 (( COLS > 0 )) || (( COLS = 80 )) # width of [ ok ] == 7
725 fi
726
727 if [[ ${RC_ENDCOL} == "yes" ]] ; then
728 ENDCOL=$'\e[A\e['$(( COLS - 8 ))'C'
729 else
730 ENDCOL=''
731 fi
732
733 # Setup the colors so our messages all look pretty
734 if [[ ${RC_NOCOLOR} == "yes" ]] ; then
735 unset GOOD WARN BAD NORMAL HILITE BRACKET
736 else
737 GOOD=$'\e[32;01m'
738 WARN=$'\e[33;01m'
739 BAD=$'\e[31;01m'
740 HILITE=$'\e[36;01m'
741 BRACKET=$'\e[34;01m'
742 NORMAL=$'\e[0m'
743 fi
744
745 ##############################################################################
746 # #
747 # *** END LAST CODE *** #
748 # #
749 # This should be the last code in here, please add all functions above!! #
750 # #
751 ##############################################################################
752
753
754 # vim:ts=4

  ViewVC Help
Powered by ViewVC 1.1.20