summaryrefslogtreecommitdiff
path: root/pm/pm-functions.in
blob: f4bf755ea75b7d60a243bd36804c077384232e46 (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
#!/bin/sh
# vim:noexpandtab


# Default values go here.  It is important to _not_ initialize some
# variables here.  They are:
#
# PM_CMDLINE
# RESUME_MODULES
#
# for great debugging!
[ "${PM_DEBUG}" = "true" ] && {
	export PM_DEBUG
	set -x
}
set -a
PM_UTILS_LIBDIR="@PM-UTILS-LIBDIR@"
PM_UTILS_ETCDIR="@PM-UTILS-SYSCONFDIR@"
PM_UTILS_RUNDIR="/var/run/pm-utils"
PM_CMDLINE="$*"

PATH=/sbin:/usr/sbin:/bin:/usr/bin:"${PM_UTILS_LIBDIR}"/bin
INHIBIT="${PM_UTILS_RUNDIR}/inhibit"
PM_LOGFILE="${PM_LOGFILE:=/var/log/pm-suspend.log}"
TEMPORARY_CPUFREQ_GOVERNOR="performance"
LOCKDIR="${PM_UTILS_RUNDIR}/locks"
STORAGEDIR="${PM_UTILS_RUNDIR}/storage"
NA=254
NX=253
DX=252
PM_FUNCTIONS="$PM_UTILS_LIBDIR/functions"
# Use c sort order
LC_COLLATE=C

# These should be overridden by defaults and/or config.d settings.
# Specifically, distros should override these by modifying defaults,
# and end users should modify these using files in /etc/pm/config.
HIBERNATE_MODE="shutdown"
HIBERNATE_RESUME_POST_VIDEO="no"
SLEEP_MODULE="kernel"
SUSPEND_MODULES=""

[ -f "${PM_UTILS_LIBDIR}"/defaults ] && . "${PM_UTILS_LIBDIR}"/defaults

set +a

for cfg in "${PM_UTILS_ETCDIR}"/config.d/*[!~] ; do
	[ -f "$cfg" ] || continue
	set -a
	. "${cfg}"
	set +a
done

. "${PM_FUNCTIONS}"

log() { [ $LOGGING ] && echo $*; }

load_hook_blacklist()
{
	[ -f "$PM_UTILS_ETCDIR/blacklist" ] || return
	# loop through the blacklist file, adding entries to our hook blacklist.
	# Blacklist file format:
	# name debugging text
	# Comments begin with hash signs.
	sed 's,#.*$,,g' < "$PM_UTILS_ETCDIR/blacklist" | \
	while read entry comment; do
		# skip blank lines
		[ -z "$entry" ] && continue
		disablehook "${entry##*/}" "${comment:-blacklist}"
		log "Blacklisting ${entry##*/}."
	done
}

remove_parameter() {
	local x=""
	for p in $PM_CMDLINE; do
		[ "$1" = "$p" ] && {
			log "Removing parameter $1."
			continue
		}
		x="$x $p"
		[ "$2" ] && shift
	done
	PM_CMDLINE="$x"
}

add_parameter() {
	log "Adding parameters $@"
	PM_CMDLINE="$PM_CMDLINE $@"
}

load_hook_parameters()
{
	[ -f "$PM_UTILS_ETCDIR/parameters" ] || return
	# loop through the parameters file, and perform the appropriate
	# action for each parameter.
	# Currently, only "add" and "drop" are supported.
	# Comments begin with hashmarks and run to the end of the line.
	# Because we are modifying an env var, the while read loop cannot
	# run in a pipe like the previous one did.
	while read action parameter; do
		parameter="$(echo $parameter |sed 's,#.*$,,g')"
		[ -z "$parameter" ] && continue
		case $action in
			add) add_parameter $parameter ;;
			drop) remove_parameter $parameter ;;
		esac
	done < "$PM_UTILS_ETCDIR/parameters"
}

take_suspend_lock()
{
	try_lock "pm-utils.lock" || return 1
	mkdir -p "${STORAGEDIR}"
	return 0
}

remove_suspend_lock()
{
	rm -rf "${STORAGEDIR}"
	release_lock "pm-utils.lock"
}

hook_exit_status(){
	case $1 in
		0) log "success." ;;
		$NA) log "not applicable." ;;
		$NX) log "not executable." ;;
		$DX) log "disabled." ;;
		*) log "Returned exit code $1." ;;
	esac
}

hook_ok()
{
	local hook="${1##*/}"
	## the actual name as passed to us.
	[ -f "$STORAGEDIR/disable_hook:$hook" ] && return $DX
	## name with zeros chopped off the filename
	[ -f "$STORAGEDIR/disable_hook:${hook#[0-9][0-9]}" ] && return $DX
	[ -x "$1" ] || return $NX
	return 0
}

run_hooks() {
	# $1 = type of hook to find.  
	# $2 = paramaters to pass to hooks.
	# $3 = if present and equal to "reverse", run hooks backwards.
	# Currently only power and sleep are meaningful.
	local syshooks="${PM_UTILS_ETCDIR}/$1.d"
	local phooks="${PM_UTILS_LIBDIR}/$1.d"
	command_exists before_hooks && before_hooks
	local sort="sort"
	local base
	local hook
	local oifs="${IFS}"
	# the next two lines are not a typo or a formatting error!
	local nifs="
"
	IFS="${nifs}" # tolerate spaces in filenames.
	[ "$3" = "reverse" ] && sort="sort -r"
	for base in $(IFS="${oifs}"; for f in "$syshooks/"*[!~] "$phooks/"*[!~];
		do [ -O "$f" ] && echo ${f##*/} ; done | $sort | uniq) ;
	do
		if [ -f "$syshooks/$base" ]; then
			hook="$syshooks/$base"
		elif [ -f "$phooks/$base" ]; then
			hook="$phooks/$base"
		fi
		log  $(date): running ${hook} $2
		hook_ok "$hook" && (
			IFS="${oifs}"
			"${hook}" $2
		)
		hook_exit_status $?
	done
	IFS="${oifs}"
}

init_logfile()
{
	if [ -h "$1" ]; then
		echo "$1 is a symbolic link, refusing to overwrite."
		return 1
	elif [ ! -O "$1" ]; then
		echo "We do not own $1, refusing to overwrite."
		return 1
	elif [ -z "$1" ]; then
		echo "Please pass a filename to init_logfile."
		return 1
	fi
	export LOGGING=true
	exec > "$1" 2>&1
}


SLEEP_FUNCTIONS="${PM_UTILS_LIBDIR}/module.d/${SLEEP_MODULE}"
[ -f "${SLEEP_FUNCTIONS}" ] || { 
	echo "Requested sleep module $SLEEP_MODULE not available."
	exit 1
}

. "${SLEEP_FUNCTIONS}"