For ease of testing, and to give everyone on the list something more
concrete to discuss, attached is a patch that actually works that
implements pluggable sleep modules.

The patch applies cleanly on top of git head, and includes an
explanatory HOWTO.modules.
diff --git a/configure.in b/configure.in
index b59ad9d..bf803a5 100644
--- a/configure.in
+++ b/configure.in
@@ -36,6 +36,7 @@ src/Makefile
 pm/Makefile
 pm/sleep.d/Makefile
 pm/power.d/Makefile
+pm/method.d/Makefile
 ])
 
 dnl ==========================================================================
diff --git a/pm/HOWTO.modules b/pm/HOWTO.modules
new file mode 100644
index 0000000..89b8eae
--- /dev/null
+++ b/pm/HOWTO.modules
@@ -0,0 +1,42 @@
+How to write a sleep module
+
+Sleep modules are programs (usually shell scripts) that do two things:
+1: Perform the act of putting the system into the appropriate sleep mode.
+2: Determine whether or not this system supports the sleep mode in question.
+
+NAMING SCHEME
+
+Sleep modules are stored in the /usr/lib/pm-utils/modules.d directory, and
+will be invoked according to the following naming scheme:
+  ${SUSPEND_METHOD}-${SUSPEND_ACTION}
+
+SUSPEND_METHOD is a particular suspend/resume implementation, such as 
+* kernel   for the built-in swsusp support,
+* tuxonice for Nigel's tuxonice suspend/resume system, and
+* uswsusp  for the userspace swsusp implementation that Rafael maintains.
+pm-utils only supports kernel right now.
+
+SUSPEND_METHOD is a pm-utils exported environment variable.
+
+SUSPEND_ACTION is the particular suspend/resume action, such as
+* suspend for suspend to ram,
+* hibernate for suspend to disk, and
+* suspend-hybrid for the case where we write memory out to disk as in the
+  hibernate case, and then perfom a suspend-to-ram.
+
+INVOCATION
+
+Currently, modules MUST accept two parameters:
+* (no parameter)  When a module is called with no parameter, it MUST perform
+  the appropriate function, and it MUST output on stdout one of
+  * resume if the system is returning from suspend to ram, or
+  * thaw   of the system is returning from suspend to disk.
+
+* inhibit   When a module is called with inhibit, it MUST do everything
+  it would normally do when called with no parameter EXCEPT for performing
+  the actual sleep action.
+
+* check     When called with the check parameter, the module MUST
+  test to see of the system can support the sleep method that the module
+  implements, and return a zero error code if it does, and any other error
+  code if it does not.
diff --git a/pm/Makefile.am b/pm/Makefile.am
index a063dbb..486c713 100644
--- a/pm/Makefile.am
+++ b/pm/Makefile.am
@@ -1,4 +1,4 @@
-SUBDIRS =  sleep.d power.d
+SUBDIRS =  sleep.d power.d method.d
 
 extradir = $(libdir)/pm-utils
 
diff --git a/pm/functions b/pm/functions
index 76aafd1..922e371 100755
--- a/pm/functions
+++ b/pm/functions
@@ -18,6 +18,7 @@ SUSPEND_MODULES=""
 TEMPORARY_CPUFREQ_GOVERNOR="performance"
 LOCK="/var/run/pm-utils/lock"
 STORAGEDIR="/var/run/pm-utils/storage"
+SUSPEND_METHOD="kernel"
 
 # Use c sort order
 export LC_COLLATE=C
@@ -124,7 +125,8 @@ run_hooks() {
 	# $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	
+        # $4 = Function (with optional arguments) to run in between running
+	# forwards and backwards.
 	echo "$(date): running $1 hooks."
 	local hooks="$1"
 	local params="$2"
@@ -135,14 +137,19 @@ run_hooks() {
 		echo "===== $(date): running hook: $file $params ====="
 		"$file" $params && revhooks="${file}${revhooks:+" ${revhooks}"}"
 	done
-	echo "$(date): done running $hooks:$params hooks."
+	echo "$(date): done running $hooks hooks."
 	if [ "x$1" = "xreverse" ]; then
+		local pivot_function="$2"
+		# The lack of quotes around ${pivot_function} is deliberate.
+		# ${pivot_function} is actually a string containing a function
+		# and its parameters, and we rely on it expanding.
+		local revparam="$(${pivot_function})"
 		echo "$(date): running $hooks hooks backwards."
 		for file in $revhooks; do
-			echo "===== $(date): running hook :$file $2 ====="
-			"${file}" $2
+			echo "===== $(date): running hook :$file $revparam ====="
+			"${file}" $revparam
 		done
-		echo "$(date): done running $hooks:$2 backwards"
+		echo "$(date): done running $hooks backwards"
 	fi
 }
 
@@ -165,20 +172,24 @@ get_power_status()
 	return $RETVAL
 }
 
-do_suspend()
-{
-	pm-pmu --suspend || echo -n "mem" > /sys/power/state
-}
-
-do_hibernate()
-{
-	echo -n "${HIBERNATE_MODE}" > /sys/power/disk
-	echo -n "disk" > /sys/power/state
+# check to make sure we can actually do the requested type of sleep.
+check_sleep() {
+	# $1 = sleep type
+	local hook="/usr/lib/pm-utils/method.d/${SUSPEND_METHOD}-$1"
+	[ -O "${hook}" -a -x "${hook}" ] || return 1
+	"${hook}" check
+	return $?
 }
 
-do_suspend_hybrid()
-{
-	return 1
+do_sleep() {
+	# $1 = type of sleep to perform.
+	# Currently supported are suspend, hibernate, and suspend-hybrid
+	local hook="/usr/lib/pm-utils/method.d/${SUSPEND_METHOD}-$1"
+	if [ -e "${INHIBIT}" ]; then
+		"${hook}" inhibit
+	else
+		"${hook}"
+	fi
 }
 
 init_logfile() {
@@ -194,10 +205,11 @@ pm_main()
 
 	# make sure that our locks are unlocked no matter how the script exits
 	trap remove_suspend_lock 0
+	check_sleep "$1" || exit 1
 
 	rm -f "$INHIBIT"
-
-	run_hooks sleep "$1" reverse "$2"
+	# the last parameter is actually a function and its parameter.
+	run_hooks sleep "$1" reverse "do_sleep $1"
 
 	return 0
 }
diff --git a/pm/method.d/Makefile.am b/pm/method.d/Makefile.am
new file mode 100644
index 0000000..593f418
--- /dev/null
+++ b/pm/method.d/Makefile.am
@@ -0,0 +1,9 @@
+methoddir = $(libdir)/pm-utils/method.d
+
+method_SCRIPTS = kernel-suspend    \
+		 kernel-hibernate  
+
+EXTRA_DIST=$(config_SCRIPTS)
+
+clean-local :
+	rm -f *~
diff --git a/pm/method.d/kernel-hibernate b/pm/method.d/kernel-hibernate
new file mode 100755
index 0000000..3c9320e
--- /dev/null
+++ b/pm/method.d/kernel-hibernate
@@ -0,0 +1,10 @@
+#!/bin/sh
+case $1 in
+	check)	[ -f /sys/power/disk ] || exit 1
+		grep -q disk /sys/power/state || exit 1
+		exit 0 ;;
+	inhibit) ;;
+	"") 	echo -n "${HIBERNATE_MODE}" >/sys/power/disk
+		echo -n "disk" >/sys/power/state ;;
+esac
+echo thaw
diff --git a/pm/method.d/kernel-suspend b/pm/method.d/kernel-suspend
new file mode 100755
index 0000000..44d7d9e
--- /dev/null
+++ b/pm/method.d/kernel-suspend
@@ -0,0 +1,7 @@
+#!/bin/sh
+case $1 in 
+	check)	grep -q mem /sys/power/state; exit $? ;;
+	inhibit) ;;
+	"") echo -n "mem" > /sys/power/state ;;
+esac
+echo resume
diff --git a/pm/sleep.d/Makefile.am b/pm/sleep.d/Makefile.am
index 5092a42..93ab711 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 a7ae472..0000000
--- a/pm/sleep.d/zzz
+++ /dev/null
@@ -1,10 +0,0 @@
-#!/bin/sh
-. /usr/lib/pm-utils/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 $?
diff --git a/src/pm-action b/src/pm-action
index 3a1e6a5..4e28301 100755
--- a/src/pm-action
+++ b/src/pm-action
@@ -120,6 +120,6 @@ esac
 
 export PM_CMDLINE="$@"
 
-pm_main "$(echo $ACTION | tr - _)" "$REVERSE"
+pm_main "$ACTION" "$REVERSE"
 
 exit $?
diff --git a/src/pm-is-supported b/src/pm-is-supported
index 9520e0b..2e3b467 100644
--- a/src/pm-is-supported
+++ b/src/pm-is-supported
@@ -25,29 +25,13 @@ export LC_COLLATE=C
 
 ARG="${1#--}"
 
-[ -f /sys/power/state ] || exit 1
+. /usr/lib/pm-utils/functions
 
 case "$ARG" in
-	suspend)
-		grep -q mem /sys/power/state || exit 1
-		;;
-	hibernate)
-		[ -f /sys/power/disk ] || exit 1
-		grep -q disk /sys/power/state || exit 1
-		;;
-	suspend-hybrid)
-		grep -q mem /sys/power/state || exit 1
-		[ -d /sys/power/tuxonice ] && exit 0
-		#grep -q disk /sys/power/state || exit 1
-		exit 1
-		;;
-	help)
-		help_options
-		;;
-	*)
-		help_options 1>&2
-		exit 1
-		;;
+	help)	help_options ;;
+	"")	help_options 1>&2
+		exit 1 ;;
+	*)	check_sleep "$ARG"; exit $? ;;
 esac
 
 exit 0
_______________________________________________
Pm-utils mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/pm-utils

Reply via email to