/[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 177 - (show annotations) (download) (as text)
Thu Dec 22 09:40:51 2005 UTC (13 years ago) by phreak
File MIME type: text/x-sh
File size: 16214 byte(s)
Importing latest baselayout/trunk changes. This is a merge with revision 1768.
1 # Copyright 1999-2005 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 # 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/${myservice}" ]] ; then
478 mkdir -p -m 0755 "${svcdir}/options/${myservice}"
479 fi
480
481 echo "$*" > "${svcdir}/options/${myservice}/${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/${myservice}/$1" ]] ; then
493 echo "$(< ${svcdir}/options/${myservice}/$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 return 0
506 }
507
508 # char *get_base_ver()
509 #
510 # get the version of baselayout that this system is running
511 #
512 get_base_ver() {
513 [[ ! -r /etc/gentoo-release ]] && return 0
514 local ver=$(</etc/gentoo-release)
515 echo "${ver##* }"
516 }
517
518 # Network filesystems list for common use in rc-scripts.
519 # This variable is used in is_net_fs and other places such as
520 # localmount.
521 NET_FS_LIST="afs cifs coda davfs gfs ncpfs nfs nfs4 ocfs2 shfs smbfs"
522
523 # bool is_net_fs(path)
524 #
525 # return 0 if path is the mountpoint of a networked filesystem
526 #
527 # EXAMPLE: if is_net_fs / ; then ...
528 #
529 is_net_fs() {
530 local fstype
531 # /proc/mounts is always accurate
532 fstype=$( sed -n -e '/^rootfs/!s:.* '"$1"' \([^ ]*\).*:\1:p' /proc/mounts )
533 [[ " ${NET_FS_LIST} " == *" ${fstype} "* ]]
534 return $?
535 }
536
537 # bool is_uml_sys()
538 #
539 # return 0 if the currently running system is User Mode Linux
540 #
541 # EXAMPLE: if is_uml_sys ; then ...
542 #
543 is_uml_sys() {
544 return 1
545 }
546
547 # bool is_vserver_sys()
548 #
549 # return 0 if the currently running system is a Linux VServer
550 #
551 # EXAMPLE: if is_vserver_sys ; then ...
552 #
553 is_vserver_sys() {
554 return 0
555 }
556
557 # bool is_xenU_sys()
558 #
559 # return 0 if the currently running system is an unprivileged Xen domain
560 #
561 # EXAMPLE: if is_xenU_sys ; then ...
562 #
563 is_xenU_sys() {
564 return 1
565 }
566
567 # bool get_mount_fstab(path)
568 #
569 # return the parameters to pass to the mount command generated from fstab
570 #
571 # EXAMPLE: cmd=$( get_mount_fstab /proc )
572 # cmd=${cmd:--t proc none /proc}
573 # mount -n ${cmd}
574 #
575 get_mount_fstab() {
576 echo
577 }
578
579 # char *reverse_list(list)
580 #
581 # Returns the reversed order of list
582 #
583 reverse_list() {
584 for (( i = $# ; i > 0 ; --i )) ; do
585 echo -n "${!i} "
586 done
587 }
588
589 # void start_addon(addon)
590 #
591 # Starts addon.
592 #
593 start_addon() {
594 local addon=$1
595 (import_addon "${addon}-start.sh")
596 return 0
597 }
598
599 # void start_volumes()
600 #
601 # Starts all volumes in RC_VOLUME_ORDER.
602 #
603 start_volumes() {
604 return 0
605 }
606
607 # void stop_addon(addon)
608 #
609 # Stops addon.
610 #
611 stop_addon() {
612 local addon=$1
613 (import_addon "${addon}-stop.sh")
614 return 0
615 }
616
617 # void stop_volumes()
618 #
619 # Stops all volumes in RC_VOLUME_ORDER (reverse order).
620 #
621 stop_volumes() {
622 return 0
623 }
624
625 # bool is_older_than(reference, files/dirs to check)
626 #
627 # return 0 if any of the files/dirs are newer than
628 # the reference file
629 #
630 # EXAMPLE: if is_older_than a.out *.o ; then ...
631 is_older_than() {
632 local x=
633 local ref=$1
634 shift
635
636 for x in "$@" ; do
637 [[ ${x} -nt "${ref}" ]] && return 0
638
639 if [[ -d ${x} ]] ; then
640 is_older_than "${ref}" "${x}"/* && return 0
641 fi
642 done
643
644 return 1
645 }
646
647 # char* bash_variable(char *variable)
648 #
649 # Turns the given variable into something that bash can use
650 # Basically replaces anything not a-z,A-Z into a _
651 #
652 bash_variable() {
653 local args="$@"
654 LC_ALL=C echo "${args//[![:word:]]/_}"
655 }
656
657 # void requote()
658 #
659 # Requotes params so they're suitable to be eval'd, just like this would:
660 # set -- 1 2 "3 4"
661 # /usr/bin/getopt -- '' "$@" | sed 's/^ -- //'
662 #
663 requote() {
664 local q=\'
665 set -- "${@//\'/$q\'$q}" # quote inner instances of '
666 set -- "${@/#/$q}" # add ' to start of each param
667 set -- "${@/%/$q}" # add ' to end of each param
668 echo "$*"
669 }
670
671 ##############################################################################
672 # #
673 # This should be the last code in here, please add all functions above!! #
674 # #
675 # *** START LAST CODE *** #
676 # #
677 ##############################################################################
678
679 if [[ -z ${EBUILD} ]] ; then
680 # Setup a basic $PATH. Just add system default to existing.
681 # This should solve both /sbin and /usr/sbin not present when
682 # doing 'su -c foo', or for something like: PATH= rcscript start
683 PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/sbin:${PATH}"
684
685 # Cache the CONSOLETYPE - this is important as backgrounded shells don't
686 # have a TTY. rc unsets it at the end of running so it shouldn't hang
687 # around
688 if [[ -z ${CONSOLETYPE} ]] ; then
689 export CONSOLETYPE=$( /sbin/consoletype 2>/dev/null )
690 fi
691 if [[ ${CONSOLETYPE} == "serial" ]] ; then
692 RC_NOCOLOR="yes"
693 RC_ENDCOL="no"
694 fi
695
696 for arg in "$@" ; do
697 case "${arg}" in
698 # Lastly check if the user disabled it with --nocolor argument
699 --nocolor|-nc)
700 RC_NOCOLOR="yes"
701 ;;
702 esac
703 done
704
705 setup_defaultlevels
706 else
707 setup_defaultlevels
708 # Should we use colors ?
709 if [[ $* != *depend* ]] ; then
710 # Check user pref in portage
711 RC_NOCOLOR=$(portageq envvar NOCOLOR 2>/dev/null)
712 [[ ${RC_NOCOLOR} == "true" ]] && RC_NOCOLOR="yes"
713 else
714 # We do not want colors during emerge depend
715 RC_NOCOLOR="yes"
716 # No output is seen during emerge depend, so this is not needed.
717 RC_ENDCOL="no"
718 fi
719 fi
720
721 if [[ -n ${EBUILD} && $* == *depend* ]] ; then
722 # We do not want stty to run during emerge depend
723 COLS=80
724 else
725 # Setup COLS and ENDCOL so eend can line up the [ ok ]
726 COLS=${COLUMNS:-0} # bash's internal COLUMNS variable
727 (( COLS == 0 )) && COLS=$(stty size 2>/dev/null | cut -d' ' -f2)
728 (( COLS > 0 )) || (( COLS = 80 )) # width of [ ok ] == 7
729 fi
730
731 if [[ ${RC_ENDCOL} == "yes" ]] ; then
732 ENDCOL=$'\e[A\e['$(( COLS - 8 ))'C'
733 else
734 ENDCOL=''
735 fi
736
737 # Setup the colors so our messages all look pretty
738 if [[ ${RC_NOCOLOR} == "yes" ]] ; then
739 unset GOOD WARN BAD NORMAL HILITE BRACKET
740 else
741 if [[ ${RC_COLORS} == "pink" ]] ; then
742 GOOD=$'\e[35;01m'
743 else
744 GOOD=$'\e[32;01m'
745 fi
746 WARN=$'\e[33;01m'
747 BAD=$'\e[31;01m'
748 HILITE=$'\e[36;01m'
749 BRACKET=$'\e[34;01m'
750 NORMAL=$'\e[0m'
751 fi
752
753 ##############################################################################
754 # #
755 # *** END LAST CODE *** #
756 # #
757 # This should be the last code in here, please add all functions above!! #
758 # #
759 ##############################################################################
760
761
762 # vim:ts=4

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.20