[Pm-utils] [PATCH 1/7] Simplify hook implementation.
Victor Lowther
victor.lowther at gmail.com
Sat Feb 9 21:24:45 PST 2008
modified: pm/HOWTO.hooks
modified: pm/functions.in
modified: pm/sleep.d/Makefile.am
deleted: pm/sleep.d/zzz
* Merge the functionality of find_hooks and run_hooks. This results in
a new run_hooks function which is much simpler than the old one and is
much more resistant to breaking due to odd filenames.
* Actually implements the functionality mentioned in README where you
can create a nonexecutable file in /etc/pm/(sleep|power).d that will
prevent the copy in /usr/lib/pm-utils/(sleep|power).d from running.
* Undoes the "nonzero exit code means do not run on resume" and
auto-reverse pseudofeatures I added in the POSIX series. If anyone likes
them, they can be added back at the cost of either passing a function to
run_hooks or maintaining a global variable.
* Merges the functionality that was in zzz back into pm_main. Although
zzz was a slightly funny name, upon further reflection it was a bad idea
and makes it harder to implement some partitioning of functionality that
will be needed if/when the hooks are split into their own project.
* Updates the documentation to reflect these changes.
---
pm/HOWTO.hooks | 16 +----------
pm/functions.in | 65 +++++++++++++++++++++--------------------------
pm/sleep.d/Makefile.am | 3 +-
pm/sleep.d/zzz | 10 -------
4 files changed, 32 insertions(+), 62 deletions(-)
diff --git a/pm/HOWTO.hooks b/pm/HOWTO.hooks
index 482af72..43fe40d 100644
--- a/pm/HOWTO.hooks
+++ b/pm/HOWTO.hooks
@@ -26,20 +26,8 @@ All hooks are run in lexical sort order according to the C locale.
SLEEP.D SPECIFIC NOTES
For any given sleep/wakeup cycle, the hooks in sleep.d are run twice:
-Once in C lexical sort order before the system goes to sleep, and
-Once in reverse C lexical sort order when the system wakes up.
-
-If a hook does not need to be run when waking up, it SHOULD return a non-zero
-exit code -- this will signal the hook-running infrastructure to skip that
-hook when waking up.
-
-Similarly, if your hook only needs to run on system with certain specific
-configurations (such as the (0|9)5led hooks, which are IBM specific),
-or relies on certain specific commands that are not guaranteed to be present
-(such as the 10NetworkManager hook, which relies on D-Bus and Network Manager
-both being present), you SHOULD test for those conditions first and you SHOULD
-return a non-zero exit code if your hook is not applicable to the system it
-is running on.
+* Once in C lexical sort order before the system goes to sleep, and
+* Once in reverse C lexical sort order when the system wakes up.
CONVENIENCE FUNCTIONS
diff --git a/pm/functions.in b/pm/functions.in
index a9f0c1b..996c8dd 100644
--- a/pm/functions.in
+++ b/pm/functions.in
@@ -104,49 +104,38 @@ command_exists()
return $?
}
-find_hooks() {
+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"
+ local sort="sort"
local base
-
- for base in $(for f in $syshooks/*[!~] $phooks/*[!~];
- do [ -f "$f" ] && echo ${f##*/} ; done | sort | uniq) ;
+ 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 [ -f "$f" ] && echo ${f##*/} ; done | ${sort} | uniq) ;
do
- if [ -x "$syshooks/$base" ]; then
- echo $syshooks/$base
- elif [ -x "$phooks/$base" ]; then
- echo $phooks/$base
+ if [ -f "$syshooks/$base" ]; then
+ hook="$syshooks/$base"
+ elif [ -f "$phooks/$base" ]; then
+ hook="$phooks/$base"
fi
+ [ -x "${hook}" ] && {
+ IFS="${oifs}"
+ echo "$(date): running ${hook} $2"
+ "${hook}" $2
+ IFS="${nifs}"
+ }
done
-}
-
-run_hooks() {
- # $1 = hooks to run
- # $2 = parameters to pass to hooks
- # $3 = if $3 = "reverse", then also run the hooks that ran sucessfully
- # backwards with parameters in $4
- # $4 = parameters to pass to scripts when running backwards
- echo "$(date): running $1 hooks."
- local hooks="$1"
- local params="$2"
- shift; shift
- local revhooks
- local file
- for file in $(find_hooks "${hooks}"); do
- echo "===== $(date): running hook: $file $params ====="
- "$file" $params && revhooks="${file}${revhooks:+" ${revhooks}"}"
- done
- echo "$(date): done running $hooks:$params hooks."
- if [ "x$1" = "xreverse" ]; then
- echo "$(date): running $hooks hooks backwards."
- for file in $revhooks; do
- echo "===== $(date): running hook :$file $2 ====="
- "${file}" $2
- done
- echo "$(date): done running $hooks:$2 backwards"
- fi
+ IFS="${oifs}"
}
get_power_status()
@@ -200,7 +189,11 @@ pm_main()
rm -f "$INHIBIT"
- run_hooks sleep "$1" reverse "$2"
+ run_hooks sleep "$1"
+ command_exists "do_$1" && [ ! -e "${INHIBIT}" ] && { \
+ sync; "do_$1"
+ }
+ run_hooks sleep "$2" reverse
return 0
}
diff --git a/pm/sleep.d/Makefile.am b/pm/sleep.d/Makefile.am
index 5092a42..256ee0b 100644
--- a/pm/sleep.d/Makefile.am
+++ b/pm/sleep.d/Makefile.am
@@ -13,8 +13,7 @@ sleep_SCRIPTS = \
90clock \
94cpufreq \
95led \
- 99video \
- zzz
+ 99video
EXTRA_DIST=$(sleep_SCRIPTS)
diff --git a/pm/sleep.d/zzz b/pm/sleep.d/zzz
deleted file mode 100755
index 202e3a0..0000000
--- a/pm/sleep.d/zzz
+++ /dev/null
@@ -1,10 +0,0 @@
-#!/bin/sh
-. "${PM_FUNCTIONS}"
-[ -e "${INHIBIT}" ] && return 1
-sync;sync;sync;
-case $1 in
- suspend|hibernate|suspend_hybrid) "do_$1" ;;
- resume|thaw) exit 0 ;;
- *) exit 1 ;;
-esac
-exit $?
--
1.5.3.8
More information about the Pm-utils
mailing list