openpkg/rc.func

Tue, 29 Nov 2011 18:44:37 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 29 Nov 2011 18:44:37 +0100
changeset 395
41738a0e98d1
child 427
71503088f51b
permissions
-rw-r--r--

Reduce private memory allocation after observing stable operation.

michael@13 1 ##
michael@13 2 ## @l_prefix@/etc/rc.func -- Run-Command Helper Functions
michael@13 3 ## Copyright (c) 2000-2007 OpenPKG Foundation e.V. <http://openpkg.net/>
michael@13 4 ## Copyright (c) 2000-2007 Ralf S. Engelschall <http://engelschall.com/>
michael@13 5 ##
michael@13 6 ## Permission to use, copy, modify, and distribute this software for
michael@13 7 ## any purpose with or without fee is hereby granted, provided that
michael@13 8 ## the above copyright notice and this permission notice appear in all
michael@13 9 ## copies.
michael@13 10 ##
michael@13 11 ## THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
michael@13 12 ## WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
michael@13 13 ## MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
michael@13 14 ## IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
michael@13 15 ## CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
michael@13 16 ## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
michael@13 17 ## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
michael@13 18 ## USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
michael@13 19 ## ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
michael@13 20 ## OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
michael@13 21 ## OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
michael@13 22 ## SUCH DAMAGE.
michael@13 23 ##
michael@13 24
michael@13 25 ##
michael@13 26 ## NOTICE: This script is a collection of reusable shell functions
michael@13 27 ## running under GNU Bash during the execution of OpenPKG run-command
michael@13 28 ## sections.
michael@13 29 ##
michael@13 30
michael@13 31 #
michael@13 32 # rcMsg (display message)
michael@13 33 #
michael@13 34 # Usage: rcMsg [-e] [-w] <message>
michael@13 35 # Example: rcMsg -e "invalid command line"
michael@13 36 # Description: display a regular/warning/error message.
michael@13 37 #
michael@13 38 rcMsg () {
michael@13 39 local prefix="rc:"
michael@13 40 while [ $# -gt 0 ]; do
michael@13 41 case $1 in
michael@13 42 -e ) prefix="${prefix}ERROR:"; shift ;;
michael@13 43 -w ) prefix="${prefix}WARNING:"; shift ;;
michael@13 44 * ) break ;;
michael@13 45 esac
michael@13 46 done
michael@13 47 echo "${prefix} $*"
michael@13 48 return 0
michael@13 49 }
michael@13 50
michael@13 51 #
michael@13 52 # rcPath (manipulate colon-separated PATH-style variable)
michael@13 53 #
michael@13 54 # Usage: rcPath [-a] [-r] [-p] [-e] <var> <dir> [<dir> ...]
michael@13 55 # Example: rcPath -a -e PATH /bin /sbin /usr/bin /usr/sbin /usr/ccs/bin
michael@13 56 # Description: removes (-r) or adds (-a) by appending or prepending
michael@13 57 # (-p) one or more directories <dir> (optionally have
michael@13 58 # to be existing if -e is given) to a colon-separated
michael@13 59 # PATH-style variable <var>. In case a directory already
michael@13 60 # exists, it is first removed.
michael@13 61 #
michael@13 62 rcPath () {
michael@13 63 local mode=""
michael@13 64 local prepend=0
michael@13 65 local exists=0
michael@13 66 while [ $# -gt 0 ]; do
michael@13 67 case $1 in
michael@13 68 -a ) mode="add"; shift ;;
michael@13 69 -r ) mode="remove"; shift ;;
michael@13 70 -p ) prepend=1; shift ;;
michael@13 71 -e ) exists=1; shift ;;
michael@13 72 * ) break ;;
michael@13 73 esac
michael@13 74 done
michael@13 75 local var="$1"
michael@13 76 shift
michael@13 77 if [ ".${mode}" = .add ]; then
michael@13 78 local edit_del=""
michael@13 79 local edit_add=""
michael@13 80 local dir
michael@13 81 for dir in "$@"; do
michael@13 82 if [ ".${exists}" = .1 ] && [ ! -d "${dir}" ]; then
michael@13 83 continue
michael@13 84 fi
michael@13 85 edit_del="${edit_del} -e 's;^${dir}\$;;' -e 's;^${dir}:;;'"
michael@13 86 edit_del="${edit_del} -e 's;:${dir}:;:;' -e 's;:${dir}\$;;'"
michael@13 87 if [ ".${prepend}" = .0 ]; then
michael@13 88 edit_add="${edit_add} -e 's;\$;:${dir};'"
michael@13 89 else
michael@13 90 edit_add="-e 's;^;${dir}:;' ${edit_add}"
michael@13 91 fi
michael@13 92 done
michael@13 93 if [ ".${edit_del}${edit_add}" != . ]; then
michael@13 94 eval "${var}=\`echo \"\$${var}\" | sed ${edit_del} ${edit_add}\`"
michael@13 95 fi
michael@13 96 return 0
michael@13 97 elif [ ".${mode}" = .remove ]; then
michael@13 98 local edit=""
michael@13 99 local dir
michael@13 100 for dir in "$@"; do
michael@13 101 edit="${edit} -e 's;^${dir}\$;;' -e 's;^${dir}:;;'"
michael@13 102 edit="${edit} -e 's;:${dir}:;:;' -e 's;:${dir}\$;;'"
michael@13 103 done
michael@13 104 eval "${var}=\`echo \"\$${var}\" | sed ${edit}\`"
michael@13 105 return 0
michael@13 106 else
michael@13 107 rcMsg -e "rcPath: neither add (-a) nor remove (-r) operation specified"
michael@13 108 return 1
michael@13 109 fi
michael@13 110 }
michael@13 111
michael@13 112 #
michael@13 113 # rcTmp (temporary file handling)
michael@13 114 #
michael@13 115 # Usage: rcTmp [-i] [-f [-n <name>]] [-k]
michael@13 116 # Example: rcTmp -i; tmpfile=`rcTmp -f -n tmp`; ...; rcTmp -k
michael@13 117 # Description: ???
michael@13 118 #
michael@13 119 rcTmp () {
michael@13 120 local mode=""
michael@13 121 local name=""
michael@13 122 while [ $# -gt 0 ]; do
michael@13 123 case $1 in
michael@13 124 -i ) mode="init"; shift ;;
michael@13 125 -f ) mode="file"; shift ;;
michael@13 126 -k ) mode="kill"; shift ;;
michael@13 127 -n ) name="$2"; shift; shift ;;
michael@13 128 * ) break ;;
michael@13 129 esac
michael@13 130 done
michael@13 131 if [ ".${mode}" = .init ]; then
michael@13 132 if [ ".${RC_TMPDIR}" = . ]; then
michael@13 133 local i=0
michael@13 134 while [ ${i} -lt 10 ]; do
michael@13 135 RC_TMPDIR="@l_prefix@/RPM/TMP/rc-`date '+%Y%m%d%H%M%S'`-$$"
michael@13 136 (umask 022; mkdir ${RC_TMPDIR} >/dev/null 2>&1) && break
michael@13 137 i=$((${i} + 1))
michael@13 138 sleep 1
michael@13 139 done
michael@13 140 if [ ${i} -eq 10 ]; then
michael@13 141 rcMsg -e "rcTmp: unable to establish secure temporary directory" 1>&2
michael@13 142 return 1
michael@13 143 fi
michael@13 144 declare -r RC_TMPDIR
michael@13 145 fi
michael@13 146 return 0
michael@13 147 elif [ ".${mode}" = .file ]; then
michael@13 148 echo "${RC_TMPDIR}/${name:-tmp}"
michael@13 149 return 0
michael@13 150 elif [ ".${mode}" = .kill ]; then
michael@13 151 if [ ".${RC_TMPDIR}" = . ]; then
michael@13 152 rcMsg -e "rcTmp: no secure temporary directory known"
michael@13 153 return 1
michael@13 154 else
michael@13 155 rm -rf ${RC_TMPDIR}
michael@13 156 return 0
michael@13 157 fi
michael@13 158 else
michael@13 159 rcMsg -e "rcTmp: neither init (-i), file (-f) nor kill (-k) operation specified"
michael@13 160 return 1
michael@13 161 fi
michael@13 162 }
michael@13 163
michael@13 164 #
michael@13 165 # rcService (check for service status enable/active/usable)
michael@13 166 #
michael@13 167 # Usage: rcService <pkg> <service> <val>
michael@13 168 # Example: if rcService openssh enable yes; then ...
michael@13 169 # Description: check <service> of package <pkg> against value <val>.
michael@13 170 # <service> has to be one of "enable", "active" or "usable".
michael@13 171 # <val> has to be either "no", "yes", or "unknown".
michael@13 172 #
michael@13 173 rcService () {
michael@13 174 local pkg="`echo ${1} | sed -e 's;-;_;g'`"
michael@13 175 local var="${pkg}_${2}"
michael@13 176 local chk="${3}"
michael@13 177 eval "local val=\$${var}"
michael@13 178 if [ ".${val}" = . ]; then
michael@13 179 eval `@l_prefix@/bin/openpkg rc 2>/dev/null --silent ${1} status || true`
michael@13 180 eval "local val=\$${var}"
michael@13 181 fi
michael@13 182 if [ ".${val}" = ".${chk}" ]; then
michael@13 183 return 0
michael@13 184 else
michael@13 185 return 1
michael@13 186 fi
michael@13 187 }
michael@13 188
michael@13 189 #
michael@13 190 # rcVarIsYes (check variable for positive value)
michael@13 191 #
michael@13 192 # Usage: rcVarIsYes <var>
michael@13 193 # Example: if rcVarIsYes foo; then ...
michael@13 194 # Description: check whether a variable <var> contains a positive
michael@13 195 # value, i.e., the values "yes", "true", "on" or "1" in
michael@13 196 # arbitrary lower or upper case.
michael@13 197 #
michael@13 198 rcVarIsYes () {
michael@13 199 local var="${1}"
michael@13 200 eval "local val=\"\$${var}\""
michael@13 201 case "${val}" in
michael@13 202 [Yy][Ee][Ss] | [Tt][Rr][Uu][Ee] | [Oo][Nn] | 1 )
michael@13 203 return 0
michael@13 204 ;;
michael@13 205 * )
michael@13 206 return 1
michael@13 207 ;;
michael@13 208 esac
michael@13 209 }
michael@13 210

mercurial