Changeset 28865


Ignore:
Timestamp:
2011-11-08T23:47:49+01:00 (6 years ago)
Author:
nico
Message:

package/base-files: enhance & cleanup service wrappers

  • add service_check function for checking if a process is alive
  • add service_signal for sending arbitrary signals to a process
  • change service_stop to send a TERM signal first, wait for the process to die and send a KILL signal if it doen't
  • have service_kill print a warning on STDERR stating it has been deprecated
Location:
trunk/package/base-files
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/package/base-files/Makefile

    r28862 r28865  
    1111 
    1212PKG_NAME:=base-files 
    13 PKG_RELEASE:=90 
     13PKG_RELEASE:=91 
    1414 
    1515PKG_FILE_DEPENDS:=$(PLATFORM_DIR)/ $(GENERIC_PLATFORM_DIR)/base-files/ 
  • trunk/package/base-files/files/lib/functions.sh

    r28864 r28865  
    11#!/bin/sh 
    2 # Copyright (C) 2006 OpenWrt.org 
     2# Copyright (C) 2006-2011 OpenWrt.org 
    33# Copyright (C) 2006 Fokus Fraunhofer <carsten.tittel@fokus.fraunhofer.de> 
    44 
     
    282282# 
    283283# Action: 
    284 #   -S,--start           start EXEC, passing it ARGS as its arguments 
    285 #   -K,--stop            stop EXEC (send it a $SERVICE_SIG_STOP signal) 
    286 #   -R,--reload          reload EXEC (send it a $SERVICE_SIG_RELOAD signal) 
    287 # 
    288 # Environment variables used: 
     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: 
    289289#   SERVICE_DAEMONIZE    run EXEC in background 
    290290#   SERVICE_WRITE_PID    create a pid file and use it 
    291291#   SERVICE_USE_PID      assume EXEC creates its own pid file and use it 
    292292#   SERVICE_PID_FILE     pid file to use (default to /var/run/EXEC.pid) 
    293 #   SERVICE_SIG_RELOAD   signal used for reloading 
    294 #   SERVICE_SIG_STOP     signal used for stopping 
     293#   SERVICE_SIG          signal to send when using -K 
     294#   SERVICE_SIG_RELOAD   default signal used for reloading 
     295#   SERVICE_SIG_STOP     default signal used for stopping 
     296#   SERVICE_STOP_TIME    time to wait for a process to stop gracefully before killing it 
    295297#   SERVICE_UID          user EXEC should be run as 
    296298#   SERVICE_GID          group EXEC should be run as 
     
    303305SERVICE_SIG_RELOAD="HUP" 
    304306SERVICE_SIG_STOP="TERM" 
     307SERVICE_STOP_TIME=5 
    305308 
    306309service() { 
    307310        local ssd 
    308         local ssd_pid_file 
    309         local ssd_sig 
    310         local ssd_start 
    311311        ssd="${SERVICE_DEBUG:+echo }start-stop-daemon${SERVICE_QUIET:+ -q}" 
    312312        case "$1" in 
    313           -S|--start|start) 
    314                 ssd="$ssd -S" 
    315                 ssd_start=1 
     313          -C) 
     314                ssd="$ssd -K -t" 
    316315                ;; 
    317           -K|--stop|stop) 
    318                 ssd="$ssd -K" 
    319                 ssd_sig="$SERVICE_SIG_STOP" 
     316          -S) 
     317                ssd="$ssd -S${SERVICE_DAEMONIZE:+ -b}${SERVICE_WRITE_PID:+ -m}" 
    320318                ;; 
    321           -R|--reload|reload) 
    322                 ssd="$ssd -K" 
    323                 ssd_sig="$SERVICE_SIG_RELOAD" 
     319          -K) 
     320                ssd="$ssd -K${SERVICE_SIG:+ -s $SERVICE_SIG}" 
    324321                ;; 
    325322          *) 
     
    337334        fi 
    338335        ssd="$ssd${SERVICE_UID:+ -c $SERVICE_UID${SERVICE_GID:+:$SERVICE_GID}}" 
    339         if [ -n "$ssd_start" ]; then 
    340                 ssd="$ssd${SERVICE_DAEMONIZE:+ -b}${SERVICE_WRITE_PID:+ -m}" 
    341         else 
    342                 ssd="$ssd${ssd_sig:+ -s $ssd_sig}" 
    343         fi 
    344336        shift 
    345337        $ssd${1:+ -- "$@"} 
    346338} 
    347339 
     340service_check() { 
     341        service -C "$@" 
     342} 
     343 
     344service_signal() { 
     345        SERVICE_SIG="${SERVICE_SIG:-USR1}" service -K "$@" 
     346} 
     347 
    348348service_start() { 
    349349        service -S "$@" 
     
    351351 
    352352service_stop() { 
    353         service -K "$@" 
     353        local try 
     354        SERVICE_SIG="${SERVICE_SIG:-$SERVICE_SIG_STOP}" service -K "$@" 
     355        while [ $((try++)) -lt $SERVICE_STOP_TIME ]; do 
     356                service -C "$@" || return 0 
     357                sleep 1 
     358        done 
     359        SERVICE_SIG="KILL" service -K "$@" 
     360        sleep 1 
     361        ! service -C "$@" 
    354362} 
    355363 
    356364service_reload() { 
    357         service -R "$@" 
     365        SERVICE_SIG="${SERVICE_SIG:-$SERVICE_SIG_RELOAD}" service -K "$@" 
    358366} 
    359367 
    360368service_kill() { 
     369        cat 1>&2 << __END_OF_WARNING__ 
     370# 
     371# WARNING: the 'service_kill' function is now deprecated and might be  
     372# removed soon. Consider using the other new service_* wrappers instead. 
     373# 
     374__END_OF_WARNING__ 
    361375        local name="${1}" 
    362376        local pid="${2:-$(pidof "$name")}" 
Note: See TracChangeset for help on using the changeset viewer.