summaryrefslogtreecommitdiff
path: root/pm/functions
blob: 39872e372f2d4f3a847d5e682e3ad975cc8b53bd (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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/bin/bash
# vim:noexpandtab

export PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/lib/pm-utils/bin

# Default values go here.  It is important to _not_ initialize some
# variables here.  They are:
#
# PM_CMDLINE
# RESUME_MODULES
#
set -a
HIBERNATE_RESUME_POST_VIDEO=no
INHIBIT=/var/run/pm-utils.inhibit
PM_LOGFILE=${PM_LOGFILE:=/var/log/pm-suspend.log}
SUSPEND_MODULES=""
TEMPORARY_CPUFREQ_GOVERNOR="userspace"

[ -f /usr/lib/pm-utils/defaults ] && . /usr/lib/pm-utils/defaults
set +a

# set nullglob to make glob results empty in case the pattern does not
# match any files
shopt -s nullglob

source_configs()
{
	cfgs="/etc/pm/config.d/*[^~]"
	for cfg in $cfgs ; do
		[ -f $cfg ] || continue
		set -a
		. $cfg
		set +a
	done
}

source_configs

take_suspend_lock()
{
	VT=$(fgconsole)
	chvt 63
	if [ -f /.suspended ]; then
		read pid < /.suspended
		if [ -d /proc/$pid ]; then
			return 1
		fi
	fi
        echo "$$" > /.suspended
	rm -f /var/run/pm-suspend
	touch /var/run/pm-suspend
	return 0
}

remove_suspend_lock()
{
	rm -f /var/run/pm-suspend
	chvt 1
	chvt $VT
	openvt -- sh -c "usleep $1 ; rm -f /.suspended >/dev/null 2>&1 0<&1" >/dev/null 2>&1 0<&1 &
}

find_sleepd_files()
{
	flist="/etc/pm/sleep.d/*[^~] /usr/lib/pm-utils/sleep.d/*[^~]"
	bases=$(for file in $flist ; do echo $(basename $file) ; done | sort -n | uniq)
	for base in $bases ; do
		if [ -x "/etc/pm/sleep.d/$base" ]; then
			echo /etc/pm/sleep.d/$base
		elif [ -x "/usr/lib/pm-utils/sleep.d/$base" ]; then
			echo /usr/lib/pm-utils/sleep.d/$base
		fi
	done
}

run_hooks()
{
	[ -z "$1" ] && return 0

	[ -f /var/run/pm-suspend ] && . /var/run/pm-suspend
	rm -f /var/run/pm-suspend

	echo "$(date): running $1 hooks."

	files=$(find_sleepd_files)
	if [ "$2" = "reverse" ]; then
		filea=($files)
		filen=${#filea[*]}
		while [ "$filen" -gt 0 ]; do
			let filen--
			file="${filea[$filen]}"
			echo "===== $(date): running hook: $file ====="
			$file $1
		done
	else
		for file in $files ; do
			echo "===== $(date): running hook: $file ====="
			$file $1
		done
	fi

	echo "$(date): done running $1 hooks."
}

get_power_status()
{
	RETVAL=0
	on_ac_power
	case "$?" in
		"0")
			echo "ac"
			;;
		"1")
			echo "battery"
			;;
		"255")
			echo "error"
			RETVAL=1
			;;
	esac
	return $RETVAL
}

do_suspend()
{
	pm-pmu --suspend || echo -n "mem" > /sys/power/state
}

do_hibernate()
{
	echo -n "platform" > /sys/power/disk
	echo -n "disk" > /sys/power/state
}

do_suspend_hybrid()
{
	return 1
}

pm_main()
{
	if [ -n "$PM_LOGFILE" ]; then
		exec > "$PM_LOGFILE" 2>&1
	fi
	take_suspend_lock || exit 1

	rm -f "$INHIBIT"

	run_hooks "$1"

	if [ ! -e "$INHIBIT" -a "$(type -t "do_$1")" == "function" ]; then
		sync ; sync ; sync
		"do_$1"
	fi

	run_hooks "$2" reverse

	remove_suspend_lock 200

	return 0
}

_rmmod() {
	if modprobe -r $1; then
		echo "export RESUME_MODULES=\"$1 \$RESUME_MODULES\"" \
						>> /var/run/pm-suspend
		return 0
	else
		echo "# could not unload '$1', usage count was $2"
		return 1
	fi
}

# this recursively unloads the given modules and all that depend on it
# first parameter is the module to be unloaded
modunload()
{
	local MOD D C USED MODS I
	local UNL=$1 RET=1
	# the kernel only knows underscores in module names, no dashes
	UNL=${UNL//-/_}
	# RET is the return code.
	# If at least one module was unloaded, return 0.
	# if the module was not loaded, also return 0 since this is no error.
	# if no module was unloaded successfully, return 1
	while read MOD D C USED D; do
		[ "$MOD" = "$UNL" ] || continue
		if [ "$USED" == "-" ]; then
			_rmmod $MOD $C
			RET=$?
		else
			USED=${USED//,/ }
			MODS=($USED)
			# it seems slightly more likely to rmmod in one pass,
			# if we try backwards.
			for I in `seq $[${#MODS[@]}-1] -1 0`; do
				MOD=${MODS[$I]}
				modunload $MOD && RET=0
			done
			# if we unloaded at least one module, then let's
			# try again!
			[ $RET -eq 0 ] && modunload $MOD
			RET=$?
		fi
		return $RET
	done < /proc/modules
	# if we came this far, there was nothing to do, 
	# the module is no longer loaded.
	return 0
}

modreload()
{
	if [ "$(eval echo \$${1}_MODULE_LOAD)" == "yes" ]; then
		modprobe "$1" >/dev/null 2>&1
	fi
}

_service=$(type -p service)
if [ -z "$_service" ]; then
	service() {
		if [ -x "/etc/init.d/$1" ]; then
			"/etc/init.d/$@"
		else
			echo "$1" $": unrecognized service" 1>&2
			return 1
		fi
	}
fi
unset _service

stopservice()
{
	service "$1" status 2>/dev/null | grep -c -q running
	if [ "$?" == "0" ]; then
		N=${1//[-.]/_}
		echo "export ${N}_SERVICE_ACTIVATE=yes" >> /var/run/pm-suspend
		service "$1" stop
	fi
}

restartservice()
{
	N=${1//[-.]/_}
	if [ "x$(eval echo \$${N}_SERVICE_ACTIVATE)" == "xyes" ]; then
		service "$1" start
	fi
}

savestate()
{
	echo "export ${1}_STATE=$2" >> /var/run/pm-suspend
}

restorestate()
{
	eval echo \$${1}_STATE
}