From: Jo-Philipp Wich Date: Sun, 19 Sep 2010 14:03:32 +0000 (+0000) Subject: [package] base-files: introduce service_kill() into functions.sh - a convenience... X-Git-Url: http://git.openwrt.org/?p=openwrt%2Fsvn-archive%2Farchive.git;a=commitdiff_plain;h=ec313265d251b5bd8e313d84a5feada189d2b45c;ds=sidebyside [package] base-files: introduce service_kill() into functions.sh - a convenience wrapper for killing services by pid or pidfile with a grace period for termination before kill SVN-Revision: 23087 --- diff --git a/package/base-files/Makefile b/package/base-files/Makefile index bdaf53c771..7abcd8b2b5 100644 --- a/package/base-files/Makefile +++ b/package/base-files/Makefile @@ -10,7 +10,7 @@ include $(TOPDIR)/rules.mk include $(INCLUDE_DIR)/kernel.mk PKG_NAME:=base-files -PKG_RELEASE:=54 +PKG_RELEASE:=55 PKG_FILE_DEPENDS:=$(PLATFORM_DIR)/ $(GENERIC_PLATFORM_DIR)/base-files/ PKG_BUILD_DEPENDS:=opkg/host diff --git a/package/base-files/files/etc/functions.sh b/package/base-files/files/etc/functions.sh index d5ffc13535..c5df49970b 100755 --- a/package/base-files/files/etc/functions.sh +++ b/package/base-files/files/etc/functions.sh @@ -275,4 +275,21 @@ uci_apply_defaults() { uci commit } +service_kill() { + local name="${1}" + local pid="${2:-$(pidof "$name")}" + local grace="${3:-5}" + + [ -f "$pid" ] && pid="$(head -n1 "$pid" 2>/dev/null)" + + for pid in $pid; do + [ -d "/proc/$pid" ] || continue + local try=0 + kill -TERM $pid 2>/dev/null && \ + while grep -qs "$name" "/proc/$pid/cmdline" && [ $((try++)) -lt $grace ]; do sleep 1; done + kill -KILL $pid 2>/dev/null && \ + while grep -qs "$name" "/proc/$pid/cmdline"; do sleep 1; done + done +} + [ -z "$IPKG_INSTROOT" -a -f /lib/config/uci.sh ] && . /lib/config/uci.sh