summaryrefslogtreecommitdiff
path: root/src/pm-action.in
blob: 3359a1b44f392c54ede686e59877e0b829d870f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/bin/sh
#
# Simple suspend script
#
# Copyright 2006 Red Hat, Inc.
#
# Based on work from:
#    Bill Nottingham <notting@redhat.com>
#    Peter Jones <pjones@redhat.com>
#    David Zeuthen <davidz@redhat.com>
#    Richard Hughes <richard@hughsie.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

# The rule here? Simplicity.

. "@PM-UTILS-LIBDIR@/pm-functions"

# save the command line parameters for the hooks.
export PM_CMDLINE="$*"
help()
{
	echo "${0##*/} [options]"
	echo
	echo "Options can change how suspend or hibernate is done."
	run_hooks sleep help
	exit 0
}

if [ -n "$EUID" -a "$EUID" != "0" ]; then
	echo This utility may only be run by the root user. 1>&2
	exit 1
fi

# Save the command line options for the hooks
while [ $# -gt 0 ]
do
	[ "$1" = "--help" ] && help
	shift
done

ACTION="$(echo ${0##*pm-} |tr - _)"

case "$ACTION" in
	suspend) 	REVERSE=resume ;;
	hibernate) 	REVERSE=thaw ;;
	suspend_hybrid) REVERSE=resume ;;
	*)		echo "Don't know how to ${ACTION}."
			exit 1 ;;
esac

"check_$ACTION" || { echo "System does not support $ACTION sleep."; exit 1; }
take_suspend_lock || exit 1

# make sure we release the lock no matter how we exit
trap remove_suspend_lock 0
init_logfile "${PM_LOGFILE}"

# Make sure we are not inhibited before we start.
rm -f "${INHIBIT}"

# run the sleep hooks
run_hooks sleep "$ACTION"

# Sleep only if we know how and if a hook did not inhibit us.
command_exists "do_$ACTION" && [ ! -e "$INHIBIT" ] && { sync; "do_$ACTION"; }

# run the sleep hooks in reverse with the wakeup action
run_hooks sleep "$REVERSE" reverse