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

# Common functionality for the hooks.

# for great debugging!
[ "${PM_DEBUG}" = "true" ] && set -x

# try to take the lock.  Fail if we cannot get it.
try_lock()
{
	# $1 = file to use as lockfile
	# $2 (optional) content to write to the lockfile,
	#               extra newline will be appended
	# make sure the directory where the lockfile should be exists
	mkdir -p "${LOCKDIR}"
	local lock="${LOCKDIR}/${1##*/}"
	# we use noclobber to make sure there are no race conditions
	(set -o noclobber; echo "${2}" > "${lock}") 2> /dev/null || return 1
	return 0
}

# spin waiting for the lock with optional timeout.  
# return once we have it, or the timeout has expired
spin_lock()
{
	# $1 = lockfile
	# $2 = optional timeout
	local elapsed=0
	while ! try_lock $1; do
		[ "x$2" != "x" ] && [ $(( $elapsed == $2 )) -ne 0 ] && return 1
		elapsed=$(($elapsed + 1))
		sleep 1;
	done
}

# release the lock
release_lock()
{
	# $1 = lockfile
	# make sure it is ours first.i
	local lock="${LOCKDIR}/${1##*/}"
	rm -f "${lock}"
	return $?
}



command_exists()
{
	# $1 = command to test for.  It can be an executable in the path,
	# a shell function, or a shell builtin.
	type "$1" >/dev/null 2>&1
	return $?
}

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

_rmmod()
{
	if modprobe -r "$1"; then
		touch "${STORAGEDIR}/module:$1"
		return 0
	else
		log "# 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="$(echo $1 |tr - _)" RET=1 

	while read MOD D C USED D; do
		[ "$MOD" = "$UNL" ] || continue
		if [ "$USED" = "-" ]; then
			# no dependent modules, just try to remove this one.
			_rmmod "$MOD" $C
			RET=$?
		else
			# modules depend on this one.  try to remove them first.
			MODS="${USED%%*,}"
			while [ -n "${MODS}" ]; do
				# try to unload the last one first
				MOD="${MODS##*,}"
				modunload $MOD && RET=0
				# prune the last one from the list
				MODS="${MODS%,*}"
			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
}

# reload all the modules in no particular order.
modreload()
{
	for x in "${STORAGEDIR}"/module:* ; do
		[ -O "${x}" ] || continue
		modprobe "${x##*:}" >/dev/null 2>&1 || \
			log "Could not reload module ${x##*:}."

	done
}

if ! command_exists service; then
	service()
	{
		if [ -x "/etc/init.d/$1" ]; then
			svc="$1"
			shift
			"/etc/init.d/$svc" "$@"
		else
			log "$1" $": unrecognized service" 1>&2
			return 1
		fi
	}
fi

stopservice()
{
	if service "$1" status 2>/dev/null | grep -c -q running; then
		touch "${STORAGEDIR}/service:$1"
		service "$1" stop
	fi
}

restartservice()
{
	[ -O "${STORAGEDIR}/service:$1" ] && service "$1" start
}

disablehook()
{
	echo "${2:-${0##*/}}" > "${STORAGEDIR}/disable_hook:${1##*/}"
}

savestate()
{
	if [ -n "$2" ]; then
		echo "$2" > "${STORAGEDIR}/state:$1"
	else
		cat > "${STORAGEDIR}/state:$1"
	fi
}

state_exists()
{
	[ -O "${STORAGEDIR}/state:$1" ]
}


restorestate()
{
	state_exists "$1" && cat "${STORAGEDIR}/state:$1"
}