[Pm-utils] [PATCH 1/6] Split hook running into two phases -- core and aux

Victor Lowther victor.lowther at gmail.com
Sun May 11 18:43:46 PDT 2008


Core phase is for hooks that work around software or hardware quirks
that may cause the system to not suspend correctly. Hooks will automatically
fall into this category if their filenames start with at least two digits.

Aux phase is for programs that want to do something when the system suspends
or resumes, but that do not need to run in any particular order or whose
success or failure has no bearing on whether or not the suspend/resume
process should fail.

This is a first-pass implementation and has not been actually tested.
---
 pm/pm-functions.in |   81 ++++++++++++++++++++++++++++++++++------------------
 1 files changed, 53 insertions(+), 28 deletions(-)

diff --git a/pm/pm-functions.in b/pm/pm-functions.in
index 3c11667..3efe5d9 100644
--- a/pm/pm-functions.in
+++ b/pm/pm-functions.in
@@ -155,40 +155,65 @@ hook_ok()
 	return 0
 }
 
-# Run all applicalbe hooks, logging success and failure as we go.
+get_core_hooks() 
+{
+	local syshooks="${PM_UTILS_ETCDIR}/$1.d"
+	local phooks="${PM_UTILS_LIBDIR}/$1.d"
+	local sort="sort"
+	[ "$2" = "reverse" ] && sort="sort -r"
+	for f in "$syshooks/"[0-9][0-9]*[!~] "$phooks/"[0-9][0-9]*[!~]; 
+	do [ -O "$f" ] && echo ${f##*/}; done |$sort |uniq
+}
+
+get_aux_hooks() 
+{
+	local syshooks="${PM_UTILS_ETCDIR}/$1.d"
+	local phooks="${PM_UTILS_LIBDIR}/$1.d"
+	local sort="sort"
+	[ "$2" = "reverse" ] && sort="sort -r"
+	for f in "$syshooks/"[!0-9][!0-9]*[!~] "$phooks/"[!0-9][!0-9]*[!~]; 
+	do [ -O "$f" ] && echo ${f##*/}; done |$sort |uniq
+}
+
+run_some_hooks()
+{
+	# $1 = type of hook to find
+	# $2 = core or aux hook?
+	# $3 = order to sort hooks in
+	# rest are parameters for hooks
+	local hook_type="$1"
+	local hook_coa="$2"
+	local hook_order="$3"
+	shift; shift; shift
+	get_${hook_coa}_hooks $hook_type $hook_order |while read base; do
+	    if [ -f "$syshooks/$base" ]; then
+		hook="$syshooks/$base"
+	    elif [ -f "$phooks/$base" ]; then
+		hook="$phooks/$base"
+	    else
+		log "$base is not a hook at all!"
+		continue
+	    fi
+	    log -n "${hook} $@: "
+	    hook_ok "$hook" && "${hook}" "$@"
+	    hook_exit_status $?
+	done
+}
+	    
+# Run all applicable hooks, logging success and failure as we go.
 run_hooks() {
 	# $1 = type of hook to find.  
 	# $2 = paramaters to pass to hooks.
 	# $3 = if present and equal to "reverse", run hooks backwards.
 	# Currently only power and sleep are meaningful.
-	local syshooks="${PM_UTILS_ETCDIR}/$1.d"
-	local phooks="${PM_UTILS_LIBDIR}/$1.d"
 	command_exists before_hooks && before_hooks
-	local sort="sort"
-	local base
-	local hook
-	local oifs="${IFS}"
-	# the next two lines are not a typo or a formatting error!
-	local nifs="
-"
-	IFS="${nifs}" # tolerate spaces in filenames.
-	[ "$3" = "reverse" ] && sort="sort -r"
-	for base in $(IFS="${oifs}"; for f in "$syshooks/"*[!~] "$phooks/"*[!~];
-		do [ -O "$f" ] && echo ${f##*/} ; done | $sort | uniq) ;
-	do
-		if [ -f "$syshooks/$base" ]; then
-			hook="$syshooks/$base"
-		elif [ -f "$phooks/$base" ]; then
-			hook="$phooks/$base"
-		fi
-		log -n "${hook} $2: "
-		hook_ok "$hook" && (
-			IFS="${oifs}"
-			"${hook}" $2
-		)
-		hook_exit_status $?
-	done
-	IFS="${oifs}"
+	if [ "$3" -a "$3" = "reverse" ]; then
+	    run_some_hooks "$1" core reverse $2
+	    run_some_hooks "$1" aux reverse $2
+	else
+	    run_some_hooks "$1" aux forward $2
+	    run_some_hooks "$1" core forward $2
+	fi
 }
 
 # Try to reinitalize the logfile. Fail unless certian criteria are met.
-- 
1.5.4.3



More information about the Pm-utils mailing list