Changeset 31042


Ignore:
Timestamp:
2012-03-20T15:29:50+01:00 (6 years ago)
Author:
nbd
Message:

base-files: split service related functions off to /lib/functions/service.sh

Location:
trunk/package
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/package/base-files-network/files/lib/network/config.sh

    r30919 r31042  
    33 
    44# DEBUG="echo" 
     5 
     6. /lib/functions/service.sh 
    57 
    68do_sysctl() { 
  • trunk/package/base-files/files/etc/rc.common

    r28864 r31042  
    22# Copyright (C) 2006-2011 OpenWrt.org 
    33 
    4 . $IPKG_INSTROOT/etc/functions.sh 
     4. $IPKG_INSTROOT/lib/functions.sh 
     5. $IPKG_INSTROOT/lib/functions/service.sh 
    56 
    67initscript=$1 
  • trunk/package/base-files/files/lib/functions.sh

    r30775 r31042  
    275275        uci commit 
    276276} 
    277  
    278 # 
    279 # service: simple wrapper around start-stop-daemon 
    280 # 
    281 # Usage: service ACTION EXEC ARGS... 
    282 # 
    283 # Action: 
    284 #   -C   check if EXEC is alive 
    285 #   -S   start EXEC, passing it ARGS as its arguments 
    286 #   -K   kill EXEC, sending it a TERM signal if not specified otherwise 
    287 # 
    288 # Environment variables exposed: 
    289 #   SERVICE_DAEMONIZE    run EXEC in background 
    290 #   SERVICE_WRITE_PID    create a pid-file and use it for matching 
    291 #   SERVICE_MATCH_EXEC   use EXEC command-line for matching (default) 
    292 #   SERVICE_MATCH_NAME   use EXEC process name for matching 
    293 #   SERVICE_USE_PID      assume EXEC create its own pid-file and use it for matching 
    294 #   SERVICE_NAME         process name to use (default to EXEC file part) 
    295 #   SERVICE_PID_FILE     pid file to use (default to /var/run/$SERVICE_NAME.pid) 
    296 #   SERVICE_SIG          signal to send when using -K 
    297 #   SERVICE_SIG_RELOAD   default signal used when reloading 
    298 #   SERVICE_SIG_STOP     default signal used when stopping 
    299 #   SERVICE_STOP_TIME    time to wait for a process to stop gracefully before killing it 
    300 #   SERVICE_UID          user EXEC should be run as 
    301 #   SERVICE_GID          group EXEC should be run as 
    302 # 
    303 #   SERVICE_DEBUG        don't do anything, but show what would be done 
    304 #   SERVICE_QUIET        don't print anything 
    305 # 
    306  
    307 SERVICE_QUIET=1 
    308 SERVICE_SIG_RELOAD="HUP" 
    309 SERVICE_SIG_STOP="TERM" 
    310 SERVICE_STOP_TIME=5 
    311 SERVICE_MATCH_EXEC=1 
    312  
    313 service() { 
    314         local ssd 
    315         local exec 
    316         local name 
    317         local start 
    318         ssd="${SERVICE_DEBUG:+echo }start-stop-daemon${SERVICE_QUIET:+ -q}" 
    319         case "$1" in 
    320           -C) 
    321                 ssd="$ssd -K -t" 
    322                 ;; 
    323           -S) 
    324                 ssd="$ssd -S${SERVICE_DAEMONIZE:+ -b}${SERVICE_WRITE_PID:+ -m}" 
    325                 start=1 
    326                 ;; 
    327           -K) 
    328                 ssd="$ssd -K${SERVICE_SIG:+ -s $SERVICE_SIG}" 
    329                 ;; 
    330           *) 
    331                 echo "service: unknown ACTION '$1'" 1>&2 
    332                 return 1 
    333         esac 
    334         shift 
    335         exec="$1" 
    336         [ -n "$exec" ] || { 
    337                 echo "service: missing argument" 1>&2 
    338                 return 1 
    339         } 
    340         [ -x "$exec" ] || { 
    341                 echo "service: file '$exec' is not executable" 1>&2 
    342                 return 1 
    343         } 
    344         name="${SERVICE_NAME:-${exec##*/}}" 
    345         [ -z "$SERVICE_USE_PID$SERVICE_WRITE_PID$SERVICE_PID_FILE" ] \ 
    346                 || ssd="$ssd -p ${SERVICE_PID_FILE:-/var/run/$name.pid}" 
    347         [ -z "$SERVICE_MATCH_NAME" ] || ssd="$ssd -n $name" 
    348         ssd="$ssd${SERVICE_UID:+ -c $SERVICE_UID${SERVICE_GID:+:$SERVICE_GID}}" 
    349         [ -z "$SERVICE_MATCH_EXEC$start" ] || ssd="$ssd -x $exec" 
    350         shift 
    351         $ssd${1:+ -- "$@"} 
    352 } 
    353  
    354 service_check() { 
    355         service -C "$@" 
    356 } 
    357  
    358 service_signal() { 
    359         SERVICE_SIG="${SERVICE_SIG:-USR1}" service -K "$@" 
    360 } 
    361  
    362 service_start() { 
    363         service -S "$@" 
    364 } 
    365  
    366 service_stop() { 
    367         local try 
    368         SERVICE_SIG="${SERVICE_SIG:-$SERVICE_SIG_STOP}" service -K "$@" || return 1 
    369         while [ $((try++)) -lt $SERVICE_STOP_TIME ]; do 
    370                 service -C "$@" || return 0 
    371                 sleep 1 
    372         done 
    373         SERVICE_SIG="KILL" service -K "$@" 
    374         sleep 1 
    375         ! service -C "$@" 
    376 } 
    377  
    378 service_reload() { 
    379         SERVICE_SIG="${SERVICE_SIG:-$SERVICE_SIG_RELOAD}" service -K "$@" 
    380 } 
    381  
    382 service_kill() { 
    383         cat 1>&2 << __END_OF_WARNING__ 
    384 # 
    385 # WARNING: the 'service_kill' function is now deprecated and might be 
    386 # removed soon. Consider using the other new service_* wrappers instead. 
    387 # 
    388 __END_OF_WARNING__ 
    389         local name="${1}" 
    390         local pid="${2:-$(pidof "$name")}" 
    391         local grace="${3:-5}" 
    392  
    393         [ -f "$pid" ] && pid="$(head -n1 "$pid" 2>/dev/null)" 
    394  
    395         for pid in $pid; do 
    396                 [ -d "/proc/$pid" ] || continue 
    397                 local try=0 
    398                 kill -TERM $pid 2>/dev/null && \ 
    399                         while grep -qs "$name" "/proc/$pid/cmdline" && [ $((try++)) -lt $grace ]; do sleep 1; done 
    400                 kill -KILL $pid 2>/dev/null && \ 
    401                         while grep -qs "$name" "/proc/$pid/cmdline"; do sleep 1; done 
    402         done 
    403 } 
    404  
    405277 
    406278group_add() { 
Note: See TracChangeset for help on using the changeset viewer.