michael@13: ##
michael@13: ## @l_prefix@/etc/rc.func -- Run-Command Helper Functions
michael@13: ## Copyright (c) 2000-2007 OpenPKG Foundation e.V.
michael@13: ## Copyright (c) 2000-2007 Ralf S. Engelschall
michael@13: ##
michael@13: ## Permission to use, copy, modify, and distribute this software for
michael@13: ## any purpose with or without fee is hereby granted, provided that
michael@13: ## the above copyright notice and this permission notice appear in all
michael@13: ## copies.
michael@13: ##
michael@13: ## THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
michael@13: ## WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
michael@13: ## MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
michael@13: ## IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
michael@13: ## CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
michael@13: ## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
michael@13: ## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
michael@13: ## USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
michael@13: ## ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
michael@13: ## OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
michael@13: ## OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
michael@13: ## SUCH DAMAGE.
michael@13: ##
michael@13:
michael@13: ##
michael@13: ## NOTICE: This script is a collection of reusable shell functions
michael@13: ## running under GNU Bash during the execution of OpenPKG run-command
michael@13: ## sections.
michael@13: ##
michael@13:
michael@13: #
michael@13: # rcMsg (display message)
michael@13: #
michael@13: # Usage: rcMsg [-e] [-w]
michael@13: # Example: rcMsg -e "invalid command line"
michael@13: # Description: display a regular/warning/error message.
michael@13: #
michael@13: rcMsg () {
michael@13: local prefix="rc:"
michael@13: while [ $# -gt 0 ]; do
michael@13: case $1 in
michael@13: -e ) prefix="${prefix}ERROR:"; shift ;;
michael@13: -w ) prefix="${prefix}WARNING:"; shift ;;
michael@13: * ) break ;;
michael@13: esac
michael@13: done
michael@13: echo "${prefix} $*"
michael@13: return 0
michael@13: }
michael@13:
michael@13: #
michael@13: # rcPath (manipulate colon-separated PATH-style variable)
michael@13: #
michael@13: # Usage: rcPath [-a] [-r] [-p] [-e] [ ...]
michael@13: # Example: rcPath -a -e PATH /bin /sbin /usr/bin /usr/sbin /usr/ccs/bin
michael@13: # Description: removes (-r) or adds (-a) by appending or prepending
michael@13: # (-p) one or more directories (optionally have
michael@13: # to be existing if -e is given) to a colon-separated
michael@13: # PATH-style variable . In case a directory already
michael@13: # exists, it is first removed.
michael@13: #
michael@13: rcPath () {
michael@13: local mode=""
michael@13: local prepend=0
michael@13: local exists=0
michael@13: while [ $# -gt 0 ]; do
michael@13: case $1 in
michael@13: -a ) mode="add"; shift ;;
michael@13: -r ) mode="remove"; shift ;;
michael@13: -p ) prepend=1; shift ;;
michael@13: -e ) exists=1; shift ;;
michael@13: * ) break ;;
michael@13: esac
michael@13: done
michael@13: local var="$1"
michael@13: shift
michael@13: if [ ".${mode}" = .add ]; then
michael@13: local edit_del=""
michael@13: local edit_add=""
michael@13: local dir
michael@13: for dir in "$@"; do
michael@13: if [ ".${exists}" = .1 ] && [ ! -d "${dir}" ]; then
michael@13: continue
michael@13: fi
michael@13: edit_del="${edit_del} -e 's;^${dir}\$;;' -e 's;^${dir}:;;'"
michael@13: edit_del="${edit_del} -e 's;:${dir}:;:;' -e 's;:${dir}\$;;'"
michael@13: if [ ".${prepend}" = .0 ]; then
michael@13: edit_add="${edit_add} -e 's;\$;:${dir};'"
michael@13: else
michael@13: edit_add="-e 's;^;${dir}:;' ${edit_add}"
michael@13: fi
michael@13: done
michael@13: if [ ".${edit_del}${edit_add}" != . ]; then
michael@13: eval "${var}=\`echo \"\$${var}\" | sed ${edit_del} ${edit_add}\`"
michael@13: fi
michael@13: return 0
michael@13: elif [ ".${mode}" = .remove ]; then
michael@13: local edit=""
michael@13: local dir
michael@13: for dir in "$@"; do
michael@13: edit="${edit} -e 's;^${dir}\$;;' -e 's;^${dir}:;;'"
michael@13: edit="${edit} -e 's;:${dir}:;:;' -e 's;:${dir}\$;;'"
michael@13: done
michael@13: eval "${var}=\`echo \"\$${var}\" | sed ${edit}\`"
michael@13: return 0
michael@13: else
michael@13: rcMsg -e "rcPath: neither add (-a) nor remove (-r) operation specified"
michael@13: return 1
michael@13: fi
michael@13: }
michael@13:
michael@13: #
michael@13: # rcTmp (temporary file handling)
michael@13: #
michael@13: # Usage: rcTmp [-i] [-f [-n ]] [-k]
michael@13: # Example: rcTmp -i; tmpfile=`rcTmp -f -n tmp`; ...; rcTmp -k
michael@13: # Description: ???
michael@13: #
michael@13: rcTmp () {
michael@13: local mode=""
michael@13: local name=""
michael@13: while [ $# -gt 0 ]; do
michael@13: case $1 in
michael@13: -i ) mode="init"; shift ;;
michael@13: -f ) mode="file"; shift ;;
michael@13: -k ) mode="kill"; shift ;;
michael@13: -n ) name="$2"; shift; shift ;;
michael@13: * ) break ;;
michael@13: esac
michael@13: done
michael@13: if [ ".${mode}" = .init ]; then
michael@13: if [ ".${RC_TMPDIR}" = . ]; then
michael@13: local i=0
michael@13: while [ ${i} -lt 10 ]; do
michael@13: RC_TMPDIR="@l_prefix@/RPM/TMP/rc-`date '+%Y%m%d%H%M%S'`-$$"
michael@13: (umask 022; mkdir ${RC_TMPDIR} >/dev/null 2>&1) && break
michael@13: i=$((${i} + 1))
michael@13: sleep 1
michael@13: done
michael@13: if [ ${i} -eq 10 ]; then
michael@13: rcMsg -e "rcTmp: unable to establish secure temporary directory" 1>&2
michael@13: return 1
michael@13: fi
michael@13: declare -r RC_TMPDIR
michael@13: fi
michael@13: return 0
michael@13: elif [ ".${mode}" = .file ]; then
michael@13: echo "${RC_TMPDIR}/${name:-tmp}"
michael@13: return 0
michael@13: elif [ ".${mode}" = .kill ]; then
michael@13: if [ ".${RC_TMPDIR}" = . ]; then
michael@13: rcMsg -e "rcTmp: no secure temporary directory known"
michael@13: return 1
michael@13: else
michael@13: rm -rf ${RC_TMPDIR}
michael@13: return 0
michael@13: fi
michael@13: else
michael@13: rcMsg -e "rcTmp: neither init (-i), file (-f) nor kill (-k) operation specified"
michael@13: return 1
michael@13: fi
michael@13: }
michael@13:
michael@13: #
michael@13: # rcService (check for service status enable/active/usable)
michael@13: #
michael@13: # Usage: rcService
michael@13: # Example: if rcService openssh enable yes; then ...
michael@13: # Description: check of package against value .
michael@13: # has to be one of "enable", "active" or "usable".
michael@13: # has to be either "no", "yes", or "unknown".
michael@13: #
michael@13: rcService () {
michael@13: local pkg="`echo ${1} | sed -e 's;-;_;g'`"
michael@13: local var="${pkg}_${2}"
michael@13: local chk="${3}"
michael@13: eval "local val=\$${var}"
michael@13: if [ ".${val}" = . ]; then
michael@13: eval `@l_prefix@/bin/openpkg rc 2>/dev/null --silent ${1} status || true`
michael@13: eval "local val=\$${var}"
michael@13: fi
michael@13: if [ ".${val}" = ".${chk}" ]; then
michael@13: return 0
michael@13: else
michael@13: return 1
michael@13: fi
michael@13: }
michael@13:
michael@13: #
michael@13: # rcVarIsYes (check variable for positive value)
michael@13: #
michael@13: # Usage: rcVarIsYes
michael@13: # Example: if rcVarIsYes foo; then ...
michael@13: # Description: check whether a variable contains a positive
michael@13: # value, i.e., the values "yes", "true", "on" or "1" in
michael@13: # arbitrary lower or upper case.
michael@13: #
michael@13: rcVarIsYes () {
michael@13: local var="${1}"
michael@13: eval "local val=\"\$${var}\""
michael@13: case "${val}" in
michael@13: [Yy][Ee][Ss] | [Tt][Rr][Uu][Ee] | [Oo][Nn] | 1 )
michael@13: return 0
michael@13: ;;
michael@13: * )
michael@13: return 1
michael@13: ;;
michael@13: esac
michael@13: }
michael@13: