Sophie

Sophie

distrib > Mandriva > 2008.1 > x86_64 > by-pkgid > b7dfe737e63c84a66685dbd61982b62a > files > 3

powersave-0.14.0-1mdv2008.1.src.rpm

#!/bin/bash
###########################################################################
#                                                                         #
#                         Powersave Daemon                                #
#                                                                         #
#          Copyright (C) 2004,2005 SUSE Linux Products GmbH               #
#                                                                         #
#               Author(s): Holger Macht <hmacht@suse.de>                  #
#                          Danny Kukawka <danny.kukawka@web.de>           #
#                                                                         #
# 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                  #
#                                                                         #
###########################################################################

# 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.
#       Additionally this start script loads all needed modules
#       for cpufrequency scaling and acpi support
### END INIT INFO
# shutup rpmlint: /var/lock/subsys/powersaved
 
# Check for missing binaries (stale symlinks should not happen)
powersaved_BIN=/usr/sbin/powersaved
powersave_BIN=/usr/bin/powersave
ACPID_BIN=/usr/sbin/acpid
# 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=/etc/powersave

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

LOGGER="/usr/bin/logger -t rcpowersaved"

# source functions
. /etc/init.d/functions


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
}

start() {
		#checkpid $powersaved_BIN
		/etc/init.d/powersaved status &>/dev/null
		RETVAL=$?
		if [ $RETVAL = 0 ]; then
		    echo -n "daemon already running"
		    echo 
		    return $RETVAL;
		elif [ -e /var/run/powersaved.pid ];then 
		    rm -f  /var/run/powersaved.pid&> /dev/null
		fi;
		#checkpid "cpufreqd" &>/dev/null
		/etc/init.d/cpufreqd status &>/dev/null
		RETVAL=$?
		if [ $RETVAL = 0 ]; then
		    echo -n "cpufreqd already running, stop it first and restart service"
		    echo
		    return $RETVAL
		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

		    ACCESS_PROC_EVENTS=" (accessing ACPI events over acpid) "
		    ACPI_EVENT_FILE="/var/run/acpid.socket"

		    pidof $ACPID_BIN >/dev/null
		    ACPID_NOT_RUNNING=$?
		    # if acpid is not there we need access to /proc/acpi/event
		    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! #
###############################################"
			return $ACPID_NOT_RUNNING
		    fi
		elif [ "$ACPI_APM" = "APM" ]; then
		    # check whether apmd is already running
		    /etc/init.d/apmd status &>/dev/null
		    RETVAL=$?
		    if [ $RETVAL = 0 ];then
			echo -n "APM daemon is already running, stop it first and restart service"
			echo
			return $RETVAL
		    fi
		    echo -n "This machine supports APM "
		
		#### when neither APM nor ACPI is found, do still start the powersave daemon
        #### we still provide suspend to disk and possibly cpufreq feature
        #### this is always the case on ppc workstations
#		else
#		    $LOGGER "INFO: Your system does neither support ACPI nor APM. \
#the powersave service does not provide any features for this machine, therefore the service is skipped."
#		    rc_status -s
		fi 

		CPUFREQ_MODULES="speedstep_centrino powernow_k8 powernow_k7 powernow_k6 longrun longhaul 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?
		# also check whether /sys/.../cpufreq already exists This might
		# happen if modules are already loaded or compiled into kernel 
                # (default on ppc)
		if [ "$CPUFREQD_MODULE" != "off" ] && [ ! -d /sys/devices/system/cpu/cpu0/cpufreq ]; 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 'CPUFREQD_MODULE=off' in $CONFIG/cpufreq to avoid this warning."
				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
			# sleeping should not be necessary, all is processed sequential
                        # usleep 10000
		fi

		# see function above 
                # -> load powersave,performance,userspace and ondemand governor
		[ "$CPUFREQD_MODULE" != "off" ] && load_governors

		###### load CPUFREQ modules############
		echo -ne "Starting powersaved $ACCESS_PROC_EVENTS"
		OPTS="$OPTS -v ${DEBUG:-3}"
		daemon $powersaved_BIN -d ${ACPI_EVENT_FILE:+-f "$ACPI_EVENT_FILE"} $OPTS
		RETVAL=$?
		echo 
		return $RETVAL
}

stop() {
    echo -n "Shutting down powersaved "
    killproc $powersaved_BIN
    RETVAL=$?
    echo
    return $RETVAL
}

case "$1" in
        start)
	        start
		;;
	stop)   
	        stop
		;;
	restart|reload)
		stop
		start
		;;
	status)
		status $powersaved_BIN
		RETVAL=$?
		;;
	*)
		echo "Usage: $0" \
		     "{start|stop|status|reload}"
		exit 1
		;;
esac

exit $RETVAL