File: rcpowersaved.in

package info (click to toggle)
powersave 0.14.0-5
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 4,764 kB
  • ctags: 999
  • sloc: sh: 11,357; cpp: 8,103; ansic: 2,631; makefile: 388
file content (301 lines) | stat: -rwxr-xr-x 11,205 bytes parent folder | download
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#!/bin/bash
###########################################################################
#                                                                         #
#                         Powersave Daemon                                #
#                                                                         #
#          Copyright (C) 2004,2005 SUSE Linux Products GmbH               #
#                                                                         #
# This program is free software; you can redistribute it and/or modify it #
# under the terms of the GNU General Public License as published by the   #
# Free Software Foundation; either version 2 of the License, or (at you   #
# option) any later version.                                              #
#                                                                         #
# This program is distributed in the hope that it will be useful, but     #
# WITHOUT ANY WARRANTY; without even the implied warranty of              #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU       #
# General Public License for more details.                                #
#                                                                         #
# You should have received a copy of the GNU General Public License along #
# with this program; if not, write to the Free Software Foundation, Inc., #
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA                  #
#                                                                         #
#                                                                         #
# Author: Thomas Renninger <trenn@suse.de, mail@renninger.de>             #
#                                                                         #
###########################################################################
#
# /etc/init.d/powersave
#   and its symbolic link
# /usr/sbin/rcpowersave
#
# LSB compliant service control script; see http://www.linuxbase.org/spec/
# 
# Should start contains acpid apmd and cpufreqd, so that I can check
# whether they are running and whether this script should fail
# apmd and cpufreqd should never run together with this service

### BEGIN INIT INFO
# Provides:                   powersaved
# Required-Start:             $remote_fs dbus haldaemon
# Should-Start:               $syslog acpid
# X-UnitedLinux-Should-Start: $syslog acpid dbus haldaemon
# Required-Stop:              $remote_fs dbus haldaemon
# Should-Stop:                $syslog acpid
# X-UnitedLinux-Should-Stop:  $syslog dbus haldaemon acpid
# Default-Start:              2 3 5
# Default-Stop:               0 1 6
# Short-Description: optimises power consumption, specially for laptops
# Description:       supports acpi apm and cpufrequency scaling
#	detects HW support for mentioned systems and optimises power consumption
#	accordingly. 
#       Back-end to the YaST2 power management configuration module
#	Additionally this start script loads all needed modules
#       for cpufrequency scaling and acpi support

### END INIT INFO

# Check for missing binaries (stale symlinks should not happen)
powersaved_BIN=/usr/sbin/powersaved
powersave_BIN=@bindir@/powersave
ACPID_BIN=/sbin/acpid
S2RAM_BIN=/usr/sbin/s2ram
# this is the default set of acpi modules loaded if nothing is configured
DEFAULT_ACPI_MODULES="ac battery button fan processor thermal"

test -x $powersaved_BIN || exit 5
# Check for existence of needed config file and read it

CONFIG=@configdir@

# create symlink to /etc where powersave expects configuration files.
# this is an evil hack and is only needed when updating from powersave
# version 0.10.20 but does not hurt
ln -ns /etc/sysconfig/powersave $CONFIG >/dev/null 2>&1

test -r $CONFIG || exit 6
. $CONFIG/common
. $CONFIG/cpufreq
. $CONFIG/thermal
if test -e /etc/sysconfig/security; then
 . /etc/sysconfig/security
fi

LOGGER="/bin/logger -t rcpowersaved"
# Shell functions sourced from /etc/rc.status:
#      rc_check         check and set local and overall rc status
#      rc_status        check and set local and overall rc status
#      rc_status -v     ditto but be verbose in local rc status
#      rc_status -v -r  ditto and clear the local rc status
#      rc_status -s     display "skipped" and exit with status 3
#      rc_status -u     display "unused" and exit with status 3
#      rc_failed        set local and overall rc status to failed
#      rc_failed <num>  set local and overall rc status to <num>
#      rc_reset         clear local rc status (overall remains)
#      rc_exit          exit appropriate to overall rc status
#      rc_active	checks whether a service is activated by symlinks
#      rc_splash arg    sets the boot splash screen to arg (if active)
. /etc/rc.status

# Reset status of this service
rc_reset

# Return values acc. to LSB for all commands but status:
# 0	  - success
# 1       - generic or unspecified error
# 2       - invalid or excess argument(s)
# 3       - unimplemented feature (e.g. "reload")
# 4       - user had insufficient privileges
# 5       - program is not installed
# 6       - program is not configured
# 7       - program is not running
# 8--199  - reserved (8--99 LSB, 100--149 distrib, 150--199 appl)
# 
# Note that starting an already running service, stopping
# or restarting a not-running service as well as the restart
# with force-reload (in case signaling is not supported) are
# considered a success.

OPTS=""
SYSFS_PATH="/sys/devices/system/cpu/cpu0/cpufreq"

function load_governors()
{
    if [ ! -r $SYSFS_PATH ];then
	$LOGGER Cannot load cpufreq governors - No cpufreq driver available
	return 1
    fi
    read govs < $SYSFS_PATH/scaling_available_governors
    case "$govs" in
	*powersave*) 
	    ;;
	*) 
	    modprobe -q cpufreq_powersave >/dev/null 2>&1
	    [ $? != 0 ] && $LOGGER powersave cpufreq governor could not be loaded
	    ;;
    esac
    case "$govs" in
	*performance*) 
	    ;;
	*) 
	    modprobe -q cpufreq_performance >/dev/null 2>&1
	    [ $? != 0 ] && $LOGGER perfromance cpufreq governor could not be loaded
	    ;;
    esac
    case "$govs" in
	*userspace*) 
	    ;;
	*) 
	    modprobe -q cpufreq_userspace >/dev/null 2>&1
	    [ $? != 0 ] && $LOGGER userspace cpufreq governor could not be loaded
	    ;;
    esac
    case "$govs" in
	*ondemand*)
	    ;;
	*) 
	    modprobe -q cpufreq_ondemand >/dev/null 2>&1
	    [ $? != 0 ] && $LOGGER ondemand cpufreq governor could not be loaded
	    ;;
    esac
    return 0
}

case "$1" in
        start)
		echo -n "Starting powersaved: "
		if [ "$POWERSAVED" = "off" -o "$POWERSAVE" = "off" ]; then
			echo -n "not starting (POWERSAVED=off)"
			$LOGGER "powersaved disabled due to POWERSAVED=off"
			rc_status -s
			rc_exit
		fi
		checkproc $powersaved_BIN
		if [ $? = 0 ]; then
		    echo -n "daemon already running"
		    rc_status -v
		    rc_exit
		elif [ -e /var/run/powersaved.pid ];then 
		    rm -f  /var/run/powersaved.pid&> /dev/null
		fi;

		ACPI_APM=`$powersave_BIN --apm-acpi`
		if [ "$ACPI_APM" = "ACPI" ]; then
	        # set thermal polling frequency
	        # this should be managed by the daemon later
		    if [ "$THERMAL_POLLING_FREQUENCY" != "0" ];then
			[ -z "$THERMAL_POLLING_FREQUENCY" ] && THERMAL_POLLING_FREQUENCY=2
			for x in /proc/acpi/thermal_zone/*; do
			    [ -w $x/polling_frequency ] && echo "$THERMAL_POLLING_FREQUENCY" >$x/polling_frequency
			done
		    fi

		    ACPI_EVENT_FILE="/var/run/acpid.socket"

		    pidof $ACPID_BIN >/dev/null
		    ACPID_NOT_RUNNING=$?
		    if [ $ACPID_NOT_RUNNING = 1 ];then
			$LOGGER "WARN: Service powersaved skipped. You have to start acpid before powersaved"
			echo -n "###############################################
# ACPI system but acpid not running.          #
# Start acpid first, then restart powersaved! #
###############################################"
			rc_status -s
			rc_exit
		    fi
		elif [ "$ACPI_APM" = "APM" ]; then
		    echo -n "This machine supports APM "
		fi 

		CPUFREQ_MODULES="speedstep_centrino powernow_k8 powernow_k7 powernow_k6 longrun acpi speedstep_ich"
		CPUFREQ_MODULES_GREP="^speedstep_centrino\|^speedstep_ich\|^powernow_k8\|^powernow_k7\|^powernow_k6\|^longrun\|^longhaul\|^acpi"

		###### load CPUFREQ modules############
		# module specfied in sysconfig.cpufreq?
		# if the drivers are compiled in, $SYSFS_PATH already exists
		if [ "$CPUFREQ_ENABLED" != "no" ] && [ ! -d $SYSFS_PATH ]; then
			# test for already loaded modules
			ALREADY_LOADED_MODS=`grep $CPUFREQ_MODULES_GREP /proc/modules`
			if [ "$CPUFREQD_MODULE" ]; then
				modprobe -q $CPUFREQD_MODULE $CPUFREQD_MODULE_OPTS &>/dev/null
				RETVAL=$?
			# try to load one of the modules we know
			elif [ -z "$ALREADY_LOADED_MODS" ] ; then 
				for MODULE in $CPUFREQ_MODULES; do
					modprobe $MODULE &>/dev/null
					RETVAL=$?
					[ "$RETVAL" = 0 ] && break
				done
				# skip if no module could be loaded!
				if [ "$RETVAL" != 0 ]; then
					$LOGGER "CPU frequency scaling is not supported by your processor."
					$LOGGER "enter 'CPUFREQ_ENABLED=no' in $CONFIG/cpufreq to avoid this warning."
					# remove eventually loaded modules, bug 150381
					rmmod speedstep_lib freq_table 2>/dev/null
				else
					$LOGGER "enter '$MODULE' into CPUFREQD_MODULE in $CONFIG/cpufreq."
					$LOGGER "this will speed up starting powersaved and avoid unnecessary warnings in syslog."
				fi
			fi
		fi

		[ "$CPUFREQ_ENABLED" != "no" ] && load_governors
		###### load CPUFREQ modules############

                #### is s2ram installed? If yes, use it to determine suspend to ram capability
                if [ -x "$S2RAM_BIN" ]; then
                    . $CONFIG/sleep
                    if [ "$SUSPEND2RAM_FORCE" != yes ]; then
                        $S2RAM_BIN -n > /dev/null 2>&1
                        RET=$?
                        if [ $RET -eq 0 ]; then
                            $LOGGER "s2ram identified your machine as being capable of suspend to RAM. See 's2ram -n' for details."
                        else
                            $LOGGER "s2ram does not know your machine. See 's2ram -i' for details. ($RET)"
                            $LOGGER "Use SUSPEND2RAM_FORCE=yes to override this detection."
                        fi
                    else
                        $LOGGER "SUSPEND2RAM_FORCE is set to yes."
                    fi
                else
                    $LOGGER "s2ram not installed."
                fi
		OPTS="$OPTS -v ${DEBUG:-3}"
		startproc -e $powersaved_BIN -d ${ACPI_EVENT_FILE:+-f "$ACPI_EVENT_FILE"} $OPTS
		rc_status -v
		;;
	stop)   
		echo -n "Shutting down powersaved "
		killproc -TERM $powersaved_BIN
		rc_status -v
		;;
	try-restart)
	        $0 status
		if test $? = 0; then
			$0 restart
		else
			rc_reset        # Not running is not a failure.
		fi
		rc_status
		;;
	restart)
		$0 stop
		$0 start
		rc_status
		;;
	force-reload|reload)
		echo -n "Reload service powersaved "
		killproc -HUP $powersaved_BIN
		rc_status -v
		;;
	status)
		echo -n "Checking for service powersaved "
		checkproc $powersaved_BIN
		rc_status -v
		;;
	*)
		echo "Usage: $0" \
		     "{start|stop|status|try-restart|restart|force-reload|reload}"
		exit 1
		;;
esac
rc_exit