home *** CD-ROM | disk | FTP | other *** search
/ chilidog.highland.cc.ks.us / chilidog.highland.cc.ks.us.zip / chilidog.highland.cc.ks.us / backup / bradford.20101108.etc.tar.gz / bradford.20101108.etc.tar / etc / init.d / powersaved < prev    next >
Text File  |  2006-07-19  |  11KB  |  302 lines

  1. #!/bin/bash
  2. ###########################################################################
  3. #                                                                         #
  4. #                         Powersave Daemon                                #
  5. #                                                                         #
  6. #          Copyright (C) 2004,2005 SUSE Linux Products GmbH               #
  7. #                                                                         #
  8. # This program is free software; you can redistribute it and/or modify it #
  9. # under the terms of the GNU General Public License as published by the   #
  10. # Free Software Foundation; either version 2 of the License, or (at you   #
  11. # option) any later version.                                              #
  12. #                                                                         #
  13. # This program is distributed in the hope that it will be useful, but     #
  14. # WITHOUT ANY WARRANTY; without even the implied warranty of              #
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU       #
  16. # General Public License for more details.                                #
  17. #                                                                         #
  18. # You should have received a copy of the GNU General Public License along #
  19. # with this program; if not, write to the Free Software Foundation, Inc., #
  20. # 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA                  #
  21. #                                                                         #
  22. #                                                                         #
  23. # Author: Thomas Renninger <trenn@suse.de, mail@renninger.de>             #
  24. #                                                                         #
  25. ###########################################################################
  26. #
  27. # /etc/init.d/powersave
  28. #   and its symbolic link
  29. # /usr/sbin/rcpowersave
  30. #
  31. # LSB compliant service control script; see http://www.linuxbase.org/spec/
  32. # Should start contains acpid apmd and cpufreqd, so that I can check
  33. # whether they are running and whether this script should fail
  34. # apmd and cpufreqd should never run together with this service
  35.  
  36. ### BEGIN INIT INFO
  37. # Provides:                   powersaved
  38. # Required-Start:             $remote_fs dbus haldaemon
  39. # Should-Start:               $syslog acpid
  40. # X-UnitedLinux-Should-Start: $syslog acpid dbus haldaemon
  41. # Required-Stop:              $remote_fs dbus haldaemon
  42. # Should-Stop:                $syslog acpid
  43. # X-UnitedLinux-Should-Stop:  $syslog dbus haldaemon acpid
  44. # Default-Start:              2 3 5
  45. # Default-Stop:               0 1 6
  46. # Short-Description: optimises power consumption, specially for laptops
  47. # Description:       supports acpi apm and cpufrequency scaling
  48. #    detects HW support for mentioned systems and optimises power consumption
  49. #    accordingly. 
  50. #       Back-end to the YaST2 power management configuration module
  51. #    Additionally this start script loads all needed modules
  52. #       for cpufrequency scaling and acpi support
  53.  
  54. ### END INIT INFO
  55.  
  56. # Check for missing binaries (stale symlinks should not happen)
  57. powersaved_BIN=/usr/sbin/powersaved
  58. powersave_BIN=/usr/bin/powersave
  59. ACPID_BIN=/sbin/acpid
  60. S2RAM_BIN=/usr/sbin/s2ram
  61. # this is the default set of acpi modules loaded if nothing is configured
  62. DEFAULT_ACPI_MODULES="ac battery button fan processor thermal"
  63.  
  64. test -x $powersaved_BIN || exit 5
  65. # Check for existence of needed config file and read it
  66.  
  67. CONFIG=/etc/powersave
  68.  
  69. # create symlink to /etc where powersave expects configuration files.
  70. # this is an evil hack and is only needed when updating from powersave
  71. # version 0.10.20 but does not hurt
  72. ln -ns /etc/sysconfig/powersave $CONFIG >/dev/null 2>&1
  73.  
  74. test -r $CONFIG || exit 6
  75. . $CONFIG/common
  76. . $CONFIG/cpufreq
  77. . $CONFIG/thermal
  78. if test -e /etc/sysconfig/security; then
  79.  . /etc/sysconfig/security
  80. fi
  81.  
  82. LOGGER="/bin/logger -t rcpowersaved"
  83. # Shell functions sourced from /etc/rc.status:
  84. #      rc_check         check and set local and overall rc status
  85. #      rc_status        check and set local and overall rc status
  86. #      rc_status -v     ditto but be verbose in local rc status
  87. #      rc_status -v -r  ditto and clear the local rc status
  88. #      rc_status -s     display "skipped" and exit with status 3
  89. #      rc_status -u     display "unused" and exit with status 3
  90. #      rc_failed        set local and overall rc status to failed
  91. #      rc_failed <num>  set local and overall rc status to <num>
  92. #      rc_reset         clear local rc status (overall remains)
  93. #      rc_exit          exit appropriate to overall rc status
  94. #      rc_active    checks whether a service is activated by symlinks
  95. #      rc_splash arg    sets the boot splash screen to arg (if active)
  96. . /etc/rc.status
  97.  
  98. # Reset status of this service
  99. rc_reset
  100.  
  101. # Return values acc. to LSB for all commands but status:
  102. # 0      - success
  103. # 1       - generic or unspecified error
  104. # 2       - invalid or excess argument(s)
  105. # 3       - unimplemented feature (e.g. "reload")
  106. # 4       - user had insufficient privileges
  107. # 5       - program is not installed
  108. # 6       - program is not configured
  109. # 7       - program is not running
  110. # 8--199  - reserved (8--99 LSB, 100--149 distrib, 150--199 appl)
  111. # Note that starting an already running service, stopping
  112. # or restarting a not-running service as well as the restart
  113. # with force-reload (in case signaling is not supported) are
  114. # considered a success.
  115.  
  116. OPTS=""
  117. SYSFS_PATH="/sys/devices/system/cpu/cpu0/cpufreq"
  118.  
  119. function load_governors()
  120. {
  121.     if [ ! -r $SYSFS_PATH ];then
  122.     $LOGGER Cannot load cpufreq governors - No cpufreq driver available
  123.     return 1
  124.     fi
  125.     read govs < $SYSFS_PATH/scaling_available_governors
  126.     case "$govs" in
  127.     *powersave*) 
  128.         ;;
  129.     *) 
  130.         modprobe -q cpufreq_powersave >/dev/null 2>&1
  131.         [ $? != 0 ] && $LOGGER powersave cpufreq governor could not be loaded
  132.         ;;
  133.     esac
  134.     case "$govs" in
  135.     *performance*) 
  136.         ;;
  137.     *) 
  138.         modprobe -q cpufreq_performance >/dev/null 2>&1
  139.         [ $? != 0 ] && $LOGGER perfromance cpufreq governor could not be loaded
  140.         ;;
  141.     esac
  142.     case "$govs" in
  143.     *userspace*) 
  144.         ;;
  145.     *) 
  146.         modprobe -q cpufreq_userspace >/dev/null 2>&1
  147.         [ $? != 0 ] && $LOGGER userspace cpufreq governor could not be loaded
  148.         ;;
  149.     esac
  150.     case "$govs" in
  151.     *ondemand*)
  152.         ;;
  153.     *) 
  154.         modprobe -q cpufreq_ondemand >/dev/null 2>&1
  155.         [ $? != 0 ] && $LOGGER ondemand cpufreq governor could not be loaded
  156.         ;;
  157.     esac
  158.     return 0
  159. }
  160.  
  161. case "$1" in
  162.         start)
  163.         echo -n "Starting powersaved: "
  164.         if [ "$POWERSAVED" = "off" -o "$POWERSAVE" = "off" ]; then
  165.             echo -n "not starting (POWERSAVED=off)"
  166.             $LOGGER "powersaved disabled due to POWERSAVED=off"
  167.             rc_status -s
  168.             rc_exit
  169.         fi
  170.         checkproc $powersaved_BIN
  171.         if [ $? = 0 ]; then
  172.             echo -n "daemon already running"
  173.             rc_status -v
  174.             rc_exit
  175.         elif [ -e /var/run/powersaved.pid ];then 
  176.             rm -f  /var/run/powersaved.pid&> /dev/null
  177.         fi;
  178.  
  179.         ACPI_APM=`$powersave_BIN --apm-acpi`
  180.         if [ "$ACPI_APM" = "ACPI" ]; then
  181.             # set thermal polling frequency
  182.             # this should be managed by the daemon later
  183.             if [ "$THERMAL_POLLING_FREQUENCY" != "0" ];then
  184.             [ -z "$THERMAL_POLLING_FREQUENCY" ] && THERMAL_POLLING_FREQUENCY=2
  185.             for x in /proc/acpi/thermal_zone/*; do
  186.                 [ -w $x/polling_frequency ] && echo "$THERMAL_POLLING_FREQUENCY" >$x/polling_frequency
  187.             done
  188.             fi
  189.  
  190.             ACPI_EVENT_FILE="/var/run/acpid.socket"
  191.  
  192.             pidof $ACPID_BIN >/dev/null
  193.             ACPID_NOT_RUNNING=$?
  194.             if [ $ACPID_NOT_RUNNING = 1 ];then
  195.             $LOGGER "WARN: Service powersaved skipped. You have to start acpid before powersaved"
  196.             echo -n "###############################################
  197. # ACPI system but acpid not running.          #
  198. # Start acpid first, then restart powersaved! #
  199. ###############################################"
  200.             rc_status -s
  201.             rc_exit
  202.             fi
  203.         elif [ "$ACPI_APM" = "APM" ]; then
  204.             echo -n "This machine supports APM "
  205.         fi 
  206.  
  207.         CPUFREQ_MODULES="speedstep_centrino powernow_k8 powernow_k7 powernow_k6 longrun acpi speedstep_ich"
  208.         CPUFREQ_MODULES_GREP="^speedstep_centrino\|^speedstep_ich\|^powernow_k8\|^powernow_k7\|^powernow_k6\|^longrun\|^longhaul\|^acpi"
  209.  
  210.         ###### load CPUFREQ modules############
  211.         # module specfied in sysconfig.cpufreq?
  212.         # if the drivers are compiled in, $SYSFS_PATH already exists
  213.         if [ "$CPUFREQ_ENABLED" != "no" ] && [ ! -d $SYSFS_PATH ]; then
  214.             # test for already loaded modules
  215.             ALREADY_LOADED_MODS=`grep $CPUFREQ_MODULES_GREP /proc/modules`
  216.             if [ "$CPUFREQD_MODULE" ]; then
  217.                 modprobe -q $CPUFREQD_MODULE $CPUFREQD_MODULE_OPTS &>/dev/null
  218.                 RETVAL=$?
  219.             # try to load one of the modules we know
  220.             elif [ -z "$ALREADY_LOADED_MODS" ] ; then 
  221.                 for MODULE in $CPUFREQ_MODULES; do
  222.                     modprobe $MODULE &>/dev/null
  223.                     RETVAL=$?
  224.                     [ "$RETVAL" = 0 ] && break
  225.                 done
  226.                 # skip if no module could be loaded!
  227.                 if [ "$RETVAL" != 0 ]; then
  228.                     $LOGGER "CPU frequency scaling is not supported by your processor."
  229.                     $LOGGER "enter 'CPUFREQ_ENABLED=no' in $CONFIG/cpufreq to avoid this warning."
  230.                     # remove eventually loaded modules, bug 150381
  231.                     rmmod speedstep_lib freq_table 2>/dev/null
  232.                 else
  233.                     $LOGGER "enter '$MODULE' into CPUFREQD_MODULE in $CONFIG/cpufreq."
  234.                     $LOGGER "this will speed up starting powersaved and avoid unnecessary warnings in syslog."
  235.                 fi
  236.             fi
  237.         fi
  238.  
  239.         [ "$CPUFREQ_ENABLED" != "no" ] && load_governors
  240.         ###### load CPUFREQ modules############
  241.  
  242.                 #### is s2ram installed? If yes, use it to determine suspend to ram capability
  243.                 if [ -x "$S2RAM_BIN" ]; then
  244.                     . $CONFIG/sleep
  245.                     if [ "$SUSPEND2RAM_FORCE" != yes ]; then
  246.                         $S2RAM_BIN -n > /dev/null 2>&1
  247.                         RET=$?
  248.                         if [ $RET -eq 0 ]; then
  249.                             $LOGGER "s2ram identified your machine as being capable of suspend to RAM. See 's2ram -n' for details."
  250.                         else
  251.                             $LOGGER "s2ram does not know your machine. See 's2ram -i' for details. ($RET)"
  252.                             $LOGGER "Use SUSPEND2RAM_FORCE=yes to override this detection."
  253.                         fi
  254.                     else
  255.                         $LOGGER "SUSPEND2RAM_FORCE is set to yes."
  256.                     fi
  257.                 else
  258.                     $LOGGER "s2ram not installed."
  259.                 fi
  260.         OPTS="$OPTS -v ${DEBUG:-3}"
  261.         startproc -e $powersaved_BIN -d ${ACPI_EVENT_FILE:+-f "$ACPI_EVENT_FILE"} $OPTS
  262.         rc_status -v
  263.         ;;
  264.     stop)   
  265.         echo -n "Shutting down powersaved "
  266.         killproc -TERM $powersaved_BIN
  267.         rc_status -v
  268.         ;;
  269.     try-restart)
  270.             $0 status
  271.         if test $? = 0; then
  272.             $0 restart
  273.         else
  274.             rc_reset        # Not running is not a failure.
  275.         fi
  276.         rc_status
  277.         ;;
  278.     restart)
  279.         $0 stop
  280.         $0 start
  281.         rc_status
  282.         ;;
  283.     force-reload|reload)
  284.         echo -n "Reload service powersaved "
  285.         killproc -HUP $powersaved_BIN
  286.         rc_status -v
  287.         ;;
  288.     status)
  289.         echo -n "Checking for service powersaved "
  290.         checkproc $powersaved_BIN
  291.         rc_status -v
  292.         ;;
  293.     *)
  294.         echo "Usage: $0" \
  295.              "{start|stop|status|try-restart|restart|force-reload|reload}"
  296.         exit 1
  297.         ;;
  298. esac
  299. rc_exit
  300.