kernel: update linux 3.2 to 3.2.12
[openwrt.git] / package / base-files / files / lib / functions.sh
index 82e8932..8c6cd07 100755 (executable)
@@ -1,5 +1,5 @@
 #!/bin/sh
 #!/bin/sh
-# Copyright (C) 2006 OpenWrt.org
+# Copyright (C) 2006-2011 OpenWrt.org
 # Copyright (C) 2006 Fokus Fraunhofer <carsten.tittel@fokus.fraunhofer.de>
 
 
 # Copyright (C) 2006 Fokus Fraunhofer <carsten.tittel@fokus.fraunhofer.de>
 
 
@@ -27,7 +27,7 @@ append() {
        local var="$1"
        local value="$2"
        local sep="${3:- }"
        local var="$1"
        local value="$2"
        local sep="${3:- }"
-       
+
        eval "export ${NO_EXPORT:+-n} -- \"$var=\${$var:+\${$var}\${value:+\$sep}}\$value\""
 }
 
        eval "export ${NO_EXPORT:+-n} -- \"$var=\${$var:+\${$var}\${value:+\$sep}}\$value\""
 }
 
@@ -74,7 +74,7 @@ package() {
 config () {
        local cfgtype="$1"
        local name="$2"
 config () {
        local cfgtype="$1"
        local name="$2"
-       
+
        export ${NO_EXPORT:+-n} CONFIG_NUM_SECTIONS=$(($CONFIG_NUM_SECTIONS + 1))
        name="${name:-cfg$CONFIG_NUM_SECTIONS}"
        append CONFIG_SECTIONS "$name"
        export ${NO_EXPORT:+-n} CONFIG_NUM_SECTIONS=$(($CONFIG_NUM_SECTIONS + 1))
        name="${name:-cfg$CONFIG_NUM_SECTIONS}"
        append CONFIG_SECTIONS "$name"
@@ -110,7 +110,7 @@ config_rename() {
        local NEW="$2"
        local oldvar
        local newvar
        local NEW="$2"
        local oldvar
        local newvar
-       
+
        [ -n "$OLD" -a -n "$NEW" ] || return
        for oldvar in `set | grep ^CONFIG_${OLD}_ | \
                sed -e 's/\(.*\)=.*$/\1/'` ; do
        [ -n "$OLD" -a -n "$NEW" ] || return
        for oldvar in `set | grep ^CONFIG_${OLD}_ | \
                sed -e 's/\(.*\)=.*$/\1/'` ; do
@@ -119,7 +119,7 @@ config_rename() {
                unset "$oldvar"
        done
        export ${NO_EXPORT:+-n} CONFIG_SECTIONS="$(echo " $CONFIG_SECTIONS " | sed -e "s, $OLD , $NEW ,")"
                unset "$oldvar"
        done
        export ${NO_EXPORT:+-n} CONFIG_SECTIONS="$(echo " $CONFIG_SECTIONS " | sed -e "s, $OLD , $NEW ,")"
-       
+
        [ "$CONFIG_SECTION" = "$OLD" ] && export ${NO_EXPORT:+-n} CONFIG_SECTION="$NEW"
 }
 
        [ "$CONFIG_SECTION" = "$OLD" ] && export ${NO_EXPORT:+-n} CONFIG_SECTION="$NEW"
 }
 
@@ -135,8 +135,8 @@ config_clear() {
        export ${NO_EXPORT:+-n} CONFIG_SECTIONS="${SECTION:+$CONFIG_SECTIONS}"
 
        for oldvar in `set | grep ^CONFIG_${SECTION:+${SECTION}_} | \
        export ${NO_EXPORT:+-n} CONFIG_SECTIONS="${SECTION:+$CONFIG_SECTIONS}"
 
        for oldvar in `set | grep ^CONFIG_${SECTION:+${SECTION}_} | \
-               sed -e 's/\(.*\)=.*$/\1/'` ; do 
-               unset $oldvar 
+               sed -e 's/\(.*\)=.*$/\1/'` ; do
+               unset $oldvar
        done
 }
 
        done
 }
 
@@ -178,7 +178,7 @@ config_foreach() {
        local type="$1"
        [ "$#" -ge 1 ] && shift
        local section cfgtype
        local type="$1"
        [ "$#" -ge 1 ] && shift
        local section cfgtype
-       
+
        [ -z "$CONFIG_SECTIONS" ] && return 0
        for section in ${CONFIG_SECTIONS}; do
                config_get cfgtype "$section" TYPE
        [ -z "$CONFIG_SECTIONS" ] && return 0
        for section in ${CONFIG_SECTIONS}; do
                config_get cfgtype "$section" TYPE
@@ -214,7 +214,7 @@ load_modules() {
 
 include() {
        local file
 
 include() {
        local file
-       
+
        for file in $(ls $1/*.sh 2>/dev/null); do
                . $file
        done
        for file in $(ls $1/*.sh 2>/dev/null); do
                . $file
        done
@@ -223,7 +223,7 @@ include() {
 find_mtd_part() {
        local PART="$(grep "\"$1\"" /proc/mtd | awk -F: '{print $1}')"
        local PREFIX=/dev/mtdblock
 find_mtd_part() {
        local PART="$(grep "\"$1\"" /proc/mtd | awk -F: '{print $1}')"
        local PREFIX=/dev/mtdblock
-       
+
        PART="${PART##mtd}"
        [ -d /dev/mtdblock ] && PREFIX=/dev/mtdblock/
        echo "${PART:+$PREFIX$PART}"
        PART="${PART##mtd}"
        [ -d /dev/mtdblock ] && PREFIX=/dev/mtdblock/
        echo "${PART:+$PREFIX$PART}"
@@ -281,17 +281,22 @@ uci_apply_defaults() {
 # Usage: service ACTION EXEC ARGS...
 #
 # Action:
 # Usage: service ACTION EXEC ARGS...
 #
 # Action:
-#   -S,--start           start EXEC, passing it ARGS as its arguments
-#   -K,--stop            stop EXEC (send it a $SERVICE_SIG_STOP signal)
-#   -R,--reload          reload EXEC (send it a $SERVICE_SIG_RELOAD signal)
+#   -C   check if EXEC is alive
+#   -S   start EXEC, passing it ARGS as its arguments
+#   -K   kill EXEC, sending it a TERM signal if not specified otherwise
 #
 #
-# Environment variables used:
+# Environment variables exposed:
 #   SERVICE_DAEMONIZE    run EXEC in background
 #   SERVICE_DAEMONIZE    run EXEC in background
-#   SERVICE_WRITE_PID    create a pid file and use it
-#   SERVICE_USE_PID      assume EXEC creates its own pid file and use it
-#   SERVICE_PID_FILE     pid file to use (default to /var/run/EXEC.pid)
-#   SERVICE_SIG_RELOAD   signal used for reloading
-#   SERVICE_SIG_STOP     signal used for stopping
+#   SERVICE_WRITE_PID    create a pid-file and use it for matching
+#   SERVICE_MATCH_EXEC   use EXEC command-line for matching (default)
+#   SERVICE_MATCH_NAME   use EXEC process name for matching
+#   SERVICE_USE_PID      assume EXEC create its own pid-file and use it for matching
+#   SERVICE_NAME         process name to use (default to EXEC file part)
+#   SERVICE_PID_FILE     pid file to use (default to /var/run/$SERVICE_NAME.pid)
+#   SERVICE_SIG          signal to send when using -K
+#   SERVICE_SIG_RELOAD   default signal used when reloading
+#   SERVICE_SIG_STOP     default signal used when stopping
+#   SERVICE_STOP_TIME    time to wait for a process to stop gracefully before killing it
 #   SERVICE_UID          user EXEC should be run as
 #   SERVICE_GID          group EXEC should be run as
 #
 #   SERVICE_UID          user EXEC should be run as
 #   SERVICE_GID          group EXEC should be run as
 #
@@ -302,62 +307,85 @@ uci_apply_defaults() {
 SERVICE_QUIET=1
 SERVICE_SIG_RELOAD="HUP"
 SERVICE_SIG_STOP="TERM"
 SERVICE_QUIET=1
 SERVICE_SIG_RELOAD="HUP"
 SERVICE_SIG_STOP="TERM"
+SERVICE_STOP_TIME=5
+SERVICE_MATCH_EXEC=1
 
 service() {
        local ssd
 
 service() {
        local ssd
-       local ssd_pid_file
-       local ssd_sig
-       local ssd_start
+       local exec
+       local name
+       local start
        ssd="${SERVICE_DEBUG:+echo }start-stop-daemon${SERVICE_QUIET:+ -q}"
        case "$1" in
        ssd="${SERVICE_DEBUG:+echo }start-stop-daemon${SERVICE_QUIET:+ -q}"
        case "$1" in
-         -S|--start|start)
-               ssd="$ssd -S"
-               ssd_start=1
+         -C)
+               ssd="$ssd -K -t"
                ;;
                ;;
-         -K|--stop|stop)
-               ssd="$ssd -K"
-               ssd_sig="$SERVICE_SIG_STOP"
+         -S)
+               ssd="$ssd -S${SERVICE_DAEMONIZE:+ -b}${SERVICE_WRITE_PID:+ -m}"
+               start=1
                ;;
                ;;
-         -R|--reload|reload)
-               ssd="$ssd -K"
-               ssd_sig="$SERVICE_SIG_RELOAD"
+         -K)
+               ssd="$ssd -K${SERVICE_SIG:+ -s $SERVICE_SIG}"
                ;;
          *)
                ;;
          *)
-               echo "ssd: unknow action '$1'" 1>&2
+               echo "service: unknown ACTION '$1'" 1>&2
                return 1
        esac
        shift
                return 1
        esac
        shift
-       if [ -z "$1" ]; then
-               echo "ssd: missing arguments" 1>&2
+       exec="$1"
+       [ -n "$exec" ] || {
+               echo "service: missing argument" 1>&2
                return 1
                return 1
-       fi
-       ssd="$ssd -x $1"
-       if [ -n "$SERVICE_PID_FILE$SERVICE_USE_PID$SERVICE_WRITE_PID" ]; then
-               ssd="$ssd -p ${SERVICE_PID_FILE:-/var/run/${1##*/}.pid}"
-       fi
+       }
+       [ -x "$exec" ] || {
+               echo "service: file '$exec' is not executable" 1>&2
+               return 1
+       }
+       name="${SERVICE_NAME:-${exec##*/}}"
+       [ -z "$SERVICE_USE_PID$SERVICE_WRITE_PID$SERVICE_PID_FILE" ] \
+               || ssd="$ssd -p ${SERVICE_PID_FILE:-/var/run/$name.pid}"
+       [ -z "$SERVICE_MATCH_NAME" ] || ssd="$ssd -n $name"
        ssd="$ssd${SERVICE_UID:+ -c $SERVICE_UID${SERVICE_GID:+:$SERVICE_GID}}"
        ssd="$ssd${SERVICE_UID:+ -c $SERVICE_UID${SERVICE_GID:+:$SERVICE_GID}}"
-       if [ -n "$ssd_start" ]; then
-               ssd="$ssd${SERVICE_DAEMONIZE:+ -b}${SERVICE_WRITE_PID:+ -m}"
-       else
-               ssd="$ssd${ssd_sig:+ -s $ssd_sig}"
-       fi
+       [ -z "$SERVICE_MATCH_EXEC$start" ] || ssd="$ssd -x $exec"
        shift
        $ssd${1:+ -- "$@"}
 }
 
        shift
        $ssd${1:+ -- "$@"}
 }
 
+service_check() {
+       service -C "$@"
+}
+
+service_signal() {
+       SERVICE_SIG="${SERVICE_SIG:-USR1}" service -K "$@"
+}
+
 service_start() {
        service -S "$@"
 }
 
 service_stop() {
 service_start() {
        service -S "$@"
 }
 
 service_stop() {
-       service -K "$@"
+       local try
+       SERVICE_SIG="${SERVICE_SIG:-$SERVICE_SIG_STOP}" service -K "$@" || return 1
+       while [ $((try++)) -lt $SERVICE_STOP_TIME ]; do
+               service -C "$@" || return 0
+               sleep 1
+       done
+       SERVICE_SIG="KILL" service -K "$@"
+       sleep 1
+       ! service -C "$@"
 }
 
 service_reload() {
 }
 
 service_reload() {
-       service -R "$@"
+       SERVICE_SIG="${SERVICE_SIG:-$SERVICE_SIG_RELOAD}" service -K "$@"
 }
 
 service_kill() {
 }
 
 service_kill() {
+       cat 1>&2 << __END_OF_WARNING__
+#
+# WARNING: the 'service_kill' function is now deprecated and might be
+# removed soon. Consider using the other new service_* wrappers instead.
+#
+__END_OF_WARNING__
        local name="${1}"
        local pid="${2:-$(pidof "$name")}"
        local grace="${3:-5}"
        local name="${1}"
        local pid="${2:-$(pidof "$name")}"
        local grace="${3:-5}"
@@ -375,6 +403,43 @@ service_kill() {
 }
 
 
 }
 
 
+group_add() {
+       local name="$1"
+       local gid="$2"
+       local rc
+       [ -f "${IPKG_INSTROOT}/etc/group" ] || return 1
+       [ -n "$IPKG_INSTROOT" ] || lock /var/lock/group
+       echo "${name}:x:${gid}:" >> ${IPKG_INSTROOT}/etc/group
+       rc=$?
+       [ -n "$IPKG_INSTROOT" ] || lock -u /var/lock/group
+       return $rc
+}
+
+group_exists() {
+       grep -qs "^${1}:" ${IPKG_INSTROOT}/etc/group
+}
+
+user_add() {
+       local name="${1}"
+       local uid="${2}"
+       local gid="${3:-$2}"
+       local desc="${4:-$1}"
+       local home="${5:-/var/run/$1}"
+       local shell="${6:-/bin/false}"
+       local rc
+       [ -f "${IPKG_INSTROOT}/etc/passwd" ] || return 1
+       [ -n "$IPKG_INSTROOT" ] || lock /var/lock/passwd
+       echo "${name}:x:${uid}:${gid}:${desc}:${home}:${shell}" >> ${IPKG_INSTROOT}/etc/passwd
+       rc=$?
+       [ -n "$IPKG_INSTROOT" ] || lock -u /var/lock/passwd
+       return $rc
+}
+
+user_exists() {
+       grep -qs "^${1}:" ${IPKG_INSTROOT}/etc/passwd
+}
+
+
 pi_include() {
        if [ -f "/tmp/overlay/$1" ]; then
                . "/tmp/overlay/$1"
 pi_include() {
        if [ -f "/tmp/overlay/$1" ]; then
                . "/tmp/overlay/$1"
This page took 0.030004 seconds and 4 git commands to generate.