File: suspendorhibernate

package info (click to toggle)
acpi-support 0.143-5.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 828 kB
  • sloc: sh: 816; ansic: 131; makefile: 38
file content (212 lines) | stat: -rw-r--r-- 5,361 bytes parent folder | download | duplicates (6)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/bin/sh
#
# How we handle suspend/hibernate is a bit complicated:
#
# If gnome-power-manager or klaptopdaemon are running, we send a fake key
# and that's it. The daemons may have policies that turn off suspend in
# response to suspend keys etc., so we don't want to do anything ourselves.
#
# If not, we follow the SUSPEND_METHODS setting.
#
#
# This script takes parameter "suspend" or "hibernate".
#
. /etc/default/acpi-support
. /usr/share/acpi-support/power-funcs
. /usr/share/acpi-support/device-funcs
. /usr/share/acpi-support/policy-funcs
. /usr/share/acpi-support/key-constants

DeviceConfig;

# The difference between suspend and hibernate
if [ "$1" = "suspend" ] ; then
	KEY=$KEY_SLEEP
	HIBERNATE_CMD=/usr/sbin/hibernate-ram
	PM_UTILS_CMD=/usr/sbin/pm-suspend
	DBUS_METHOD=Suspend
	DBUS_PARAMS="int32:0"
elif [ "$1" = "hibernate" ] ; then
	KEY=$KEY_SUSPEND
	HIBERNATE_CMD=/usr/sbin/hibernate-disk
	PM_UTILS_CMD=/usr/sbin/pm-hibernate
	DBUS_METHOD=Hibernate
	DBUS_PARAMS=
else
	echo "'Usage: '$0' (suspend|hibernate)'"
fi


#
# Backward compatibility
#

# Backward compatibility for setting USE_HIBERNATE_PACKAGE
if [ "$SUSPEND_METHODS" = "" ] &&
   [ "$USE_HIBERNATE_PACKAGE" = "true" ] &&
   [ -x "$HIBERNATE_CMD" ] ; then
	SUSPEND_METHODS="hibernate"
fi

# Backward compatibility for setups before SUSPEND_METHODS existed.
if [ "$SUSPEND_METHODS" = "" ] ; then
	# Legacy configuration -- use the built-in legacy suspend support. We
	# can NEVER change this explicitly, because it will break people's
	# working suspend setups, especially ones that depend on custom scripts
	# in /etc/acpi/suspend.d and /etc/acpi/resume.d.
	SUSPEND_METHODS="acpi-support"
fi

#
# Just in case ...
#
sync

#
# Try the SUSPEND_METHODS in order.
#

for METHOD in $SUSPEND_METHODS; do
	case $METHOD in
	none)
		exit
		;;
		
	dbus-pm)
		if [ -x /usr/bin/dbus-send ] ; then
			# Call the power management daemon (which, if it is
			# running, we probably don't know about, since we send
			# keys if we detect one running that we know of).
			if /usr/bin/dbus-send --session \
			  --dest=org.freedesktop.PowerManagement \
			  --type=method_call \
			  --print-reply \
			  --reply-timeout=2000 \
			  /org/freedesktop/PowerManagement \
			  org.freedesktop.PowerManagement.$DBUS_METHOD ;
			  then			  
			  	# The other side exists, we have access to it,
			  	# and it received the message. It may have
			  	# failed (I tested this: if pm-suspend returns
			  	# exit code 1, then the return code of dbus-send
			  	# is still 0 and you get no errors), but that
			  	# doesn't matter: the first method in the list
			  	# that we can access is the one that has to do
			  	# it.
			  	exit
			fi
			# We got a DBUS error, which means that the other side
			# does not exist or we don't have access to it. We
			# continue by trying the next option.
		fi
		;;
		
	dbus-hal)
		if [ -x /usr/bin/dbus-send ] ; then
			# Call HAL directly.
			if /usr/bin/dbus-send --system \
			  --dest=org.freedesktop.Hal \
			  --type=method_call \
			  --print-reply \
			  --reply-timeout=2000 \
			  /org/freedesktop/Hal/devices/computer \
			  org.freedesktop.Hal.Device.SystemPowerManagement.$DBUS_METHOD $DBUS_PARAMS ;
			  then			  
			  	# The other side exists, we have access to it,
			  	# and it received the message. It may have
			  	# failed (I tested this: if pm-suspend returns
			  	# exit code 1, then the return code of dbus-send
			  	# is still 0 and you get no errors), but that
			  	# doesn't matter: the first method in the list
			  	# that we can access is the one that has to do
			  	# it.
			  	exit
			fi
			# We got a DBUS error, which means that the other side
			# does not exist or we don't have access to it. We
			# continue by trying the next option.
		fi
		;;
		
	pm-utils)
		if [ -x $PM_UTILS_CMD ] ; then
			$PM_UTILS_CMD
			exit
		fi
		;;
		
	hibernate)
		if [ -x $HIBERNATE_CMD ] ; then
			$HIBERNATE_CMD
			exit		
		fi
		;;
		
	acpi-support)
		if [ "$1" = "hibernate" ] ; then
			if [ x$ACPI_HIBERNATE != xtrue ]; then
			  exit;
			fi
					
			# Unset video posting - it's not needed for suspend to disk
			unset POST_VIDEO
			unset USE_DPMS

			. /etc/acpi/prepare.sh

			echo -n $HIBERNATE_MODE >/sys/power/disk

			if [ -x /usr/sbin/s2disk ]; then
			    /usr/sbin/s2disk
			else
			    echo -n "disk" >/sys/power/state
			fi

			. /etc/acpi/resume.sh
		else	# $1 = suspend			
			if [ x$ACPI_SLEEP != xtrue ]; then
			  exit;
			fi

			if [ x$LOCK_SCREEN = xtrue ]; then
			    if pidof xscreensaver > /dev/null; then 
				d=/tmp/.X11-unix
				for x in $d/*; do
				    displaynum=${x#$d/X}
				    getXuser;
				    if [ x"$XAUTHORITY" != x"" ]; then	    
					export DISPLAY=":$displaynum"
					. /usr/share/acpi-support/screenblank
				    fi
				done
			    fi
			fi

			# Generic preparation code
			. /etc/acpi/prepare.sh

			if [ x$DISABLE_DMA = xtrue ] && [ -b /dev/hda ]; then
			  hdparm -d 0 /dev/hda
			fi

			echo -n $ACPI_SLEEP_MODE >/sys/power/state

			if [ x$RESET_DRIVE = xtrue ] && [ -b /dev/hda ]; then
			    hdparm -w /dev/hda
			    hdparm -C /dev/hda
			    hdparm -C /dev/hda
			    hdparm -C /dev/hda
			    hdparm -d 1 /dev/hda
			fi

			if [ x$DISABLE_DMA = xtrue ] && [ -b /dev/hda ]; then
			  hdparm -d 1 /dev/hda
			fi

			# Generic wakeup code
			. /etc/acpi/resume.sh	
		fi	
		exit
		;;
	esac
done