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