openpkg/rc.func

Mon, 28 Jan 2013 17:37:18 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Mon, 28 Jan 2013 17:37:18 +0100
changeset 758
a2c6460cfb16
parent 427
71503088f51b
permissions
-rw-r--r--

Correct socket error reporting improvement with IPv6 portable code,
after helpful recommendation by Saúl Ibarra Corretgé on OSips devlist.

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

mercurial