/[vps]/baselayout-vserver/trunk/sbin/functions.sh
Gentoo

Contents of /baselayout-vserver/trunk/sbin/functions.sh

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.20