[arch-general] [PATCH 08/48] Slightly simplify hook-running infrastructure.

Victor Lowther victor.lowther at gmail.com
Wed Jun 30 17:47:42 EDT 2010


Go ahead and declare add_hook and run_hook as readonly functions
to prevent hooks from overwriting them.  Too bad bash does not have
lexically scoped variables -- if it does, we could do the same with
the hook_funcs associative array.
---
 functions |   49 ++++++++++++++++++++++++++-----------------------
 1 files changed, 26 insertions(+), 23 deletions(-)

diff --git a/functions b/functions
index 8bbdfc6..d9f55fa 100644
--- a/functions
+++ b/functions
@@ -148,38 +148,39 @@ in_array() {
 # daemons:
 
 add_daemon() {
-	[ -d /var/run/daemons ] || /bin/mkdir -p /var/run/daemons
-	/bin/touch /var/run/daemons/$1
+	[[ -d /var/run/daemons ]] || /bin/mkdir -p /var/run/daemons
+	> /var/run/daemons/"$1"
 }
 
 rm_daemon() {
-	/bin/rm -f /var/run/daemons/$1
+	/bin/rm -f /var/run/daemons/"$1"
 }
 
 ck_daemon() {
-	[ -f /var/run/daemons/$1 ] && return 1
-	return 0
+	[[ ! -f /var/run/daemons/$1 ]]
 }
 
-ck_depends() {
-	for daemon in $@; do
-		if ck_daemon $daemon; then
-			/etc/rc.d/$daemon start
-		fi
-	done
+have_daemon() {
+    [[ -x /etc/rc.d/"$1" ]]
 }
 
 start_daemon() {
-	/etc/rc.d/$1 start
+    have_daemon "$1" && /etc/rc.d/"$1" start
+}
+
+ck_depends() {
+    for daemon in "$@"; do
+	ck_daemon "$daemon" && start_daemon "$daemon" 
+    done
 }
 
 start_daemon_bkgd() {
 	stat_bkgd "Starting $1"
-	(/etc/rc.d/$1 start) &>/dev/null &
+	have_daemon "$1" && (start_daemon "$1") &>/dev/null &
 }
 
 stop_daemon() {
-	/etc/rc.d/$1 stop
+	have_daemon "$1" && /etc/rc.d/"$1" stop
 }
 
 # Status functions
@@ -234,24 +235,26 @@ ck_status() {
 # single_postkillall: after all processes have been killed in rc.single
 # shutdown_poweroff: directly before powering off in rc.shutdown
 #
-# Make sure to never override the add_hook and run_hook functions via functions.d
+# Declare add_hook and run_hook as read-only to prevent overwriting them.
+# Too bad we cannot do the same thing with hook_funcs 
 
 declare -A hook_funcs
 
 add_hook() {
-	[ -z "$1" -o -z "$2" ] && return 1
-	hook_funcs["$1"]="${hook_funcs["$1"]} $2"
+	[[ $1 && $2 ]] || return 1
+	hook_funcs["$1"]+=" $2"
 }
 
 run_hook() {
-	local func
-
-	[ -z "$1" ] && return 1
-	for func in ${hook_funcs["$1"]}; do
-		${func}
-	done
+    [[ $1 ]] || return 1
+    local func
+    for func in ${hook_funcs["$1"]}; do
+	"${func}"
+    done
 }
 
+declare -r add_hook run_hook
+
 # Function for setting console font if required
 set_consolefont() {
     if [ -n "$CONSOLEFONT" ]; then
-- 
1.7.1



More information about the arch-general mailing list