summaryrefslogtreecommitdiff
path: root/pm/functions
blob: 741d5cfd17a364253903bce325f52bef5cc27e65 (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
#!/bin/bash

. /etc/rc.d/init.d/functions

# default values go here
HIBERNATE_RESUME_POST_VIDEO=no
SUSPEND_MODULES=""

[ -f /etc/pm/config ] && . /etc/pm/config

# export them all here
export HIBERNATE_RESUME_POST_VIDEO
export SUSPEND_MODULES

take_suspend_lock()
{
	VT=$(/usr/bin/fgconsole)
	chvt 63
	if [ -f /.suspended ]; then
		pid=$(cat /.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 $VT
	openvt -- sh -c "usleep $1 ; rm -f /.suspended >/dev/null 2>&1 0<&1" >/dev/null 2>&1 0<&1 &
}

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

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

	files="/etc/pm/hooks/*"
	if [ "$2" = "reverse" ]; then
		files=$(echo $files | awk '{ for (i=NF; i>=1; i--) print $i }')
	fi
	for file in $files ; do
		[ -x $file ] && $file $1
	done
}

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

pm_main()
{
	take_suspend_lock || exit 1
	run_hooks "$1"
	sync ; sync ; sync

	case "$1" in
		suspend)
			echo -n "mem" > /sys/power/state
			run_hooks resume reverse
			;;
		hibernate)
			echo -n "platform" > /sys/power/disk
			echo -n "disk" > /sys/power/state
			run_hooks thaw reverse
			;;
	esac

	remove_suspend_lock 200

	return 0
}

modunload()
{
	/sbin/lsmod 2>/dev/null | grep -q "$1"
	if [ "$?" == "0" ]; then
		echo "export ${1}_MODULE_LOAD=yes" >> /var/run/pm-suspend
		/sbin/modprobe -r "$1" >/dev/null 2>&1
	fi
}

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

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

restartservice()
{
	if [ "$(eval echo \$${1}_SERVICE_ACTIVATE)" == "yes" ]; then
		/sbin/service "$1" start >/dev/null 2>&1
	fi
}

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

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