02rtcwake 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #!/bin/sh
  2. #
  3. # Written by Gabriel Burt <gburt@novell.com>
  4. # Copyright 2008 Novell, Inc.
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of version 2 of the GNU General Public License as
  8. # published by the Free Software Foundation.
  9. #
  10. # Use rtcwake to wake the computer from sleep or hibernate
  11. # as configured in /etc/pm/config.d/rtcwake.config
  12. . "${PM_FUNCTIONS}"
  13. rtcwake_cmd="/usr/sbin/rtcwake";
  14. command_exists $rtcwake_cmd || exit $NA
  15. rtcwake_config_file="/etc/pm/config.d/rtcwake.conf"
  16. function start_of_day {
  17. echo `date -d "$1" "+%D"`
  18. }
  19. function since_epoch {
  20. echo `date -d "$1" "+%s"`
  21. }
  22. function day_of_week {
  23. echo `date -d "$1" "+%u"`
  24. }
  25. function isAlarmDow {
  26. is=0
  27. for dow in $RTCWAKE_DAYS; do
  28. if test "x$dow" = "x$1"; then
  29. is=1;
  30. break;
  31. fi
  32. done
  33. echo $is
  34. }
  35. function set_alarm
  36. {
  37. # If USER_RTCWAKE_ALLOWED is yes and the user passed in a num_seconds_to_sleep variable, then
  38. # we will respect it (and ignore the system config)
  39. # Do require that the number of seconds is at least 30 though.
  40. if test "x$USER_RTCWAKE_ALLOWED" != "xno" && test "x$NUM_SECONDS_TO_SLEEP" != "x" && test $(($NUM_SECONDS_TO_SLEEP > 30)) == 1; then
  41. echo "Have NUM_SECONDS_TO_SLEEP: $NUM_SECONDS_TO_SLEEP";
  42. # Actually set/configure the rtcwake
  43. sh -c "$rtcwake_cmd -m on -s $NUM_SECONDS_TO_SLEEP &"
  44. elif test "x$1" = "xyes"; then
  45. echo "alarm enabled, configuring rtcwake...";
  46. echo "RTCWAKE time: $RTCWAKE_TIME";
  47. echo "RTCWAKE days: $RTCWAKE_DAYS";
  48. next_alarm=0
  49. now=$(since_epoch now);
  50. # The next alarm has to be at most 7 days from now
  51. for days_from_now in 0 1 2 3 4 5 6 7; do
  52. # Get the date N days from now
  53. alarm_day=$(start_of_day "$days_from_now day");
  54. # Check that this day is an alarm-enabled day of the week
  55. alarm_dow=$(day_of_week $alarm_day);
  56. is_alarm_dow=$(isAlarmDow $alarm_dow);
  57. if test "x$is_alarm_dow" = "x1"; then
  58. # Get the actual alarm time on that day
  59. alarm_time=$(since_epoch "$alarm_day $RTCWAKE_TIME");
  60. # Ensure the alarm time is more than 15 minutes from now
  61. # - otherwise we set the alarm for the next slot
  62. # 900 seconds = 15 minutes * 60 seconds/minute
  63. is_enough_in_future=$(($alarm_time - $now > 900));
  64. if test "x$is_enough_in_future" = "x1"; then
  65. next_alarm=$alarm_time
  66. break;
  67. fi
  68. fi
  69. done
  70. if test "x$next_alarm" != "x0"; then
  71. # Recalculate now to be as accurate as possible
  72. now=$(since_epoch now);
  73. seconds_from_now=$(($next_alarm - $now));
  74. echo "Will set alarm for $seconds_from_now seconds from now, at $alarm_day $RTCWAKE_TIME ($next_alarm)"
  75. # Actually set/configure the rtcwake
  76. sh -c "$rtcwake_cmd -m on -s $seconds_from_now &"
  77. else
  78. echo "No acceptable time found to set the rtcwake alarm. Review your configuration in $rtcwake_config_file"
  79. exit $NA
  80. fi
  81. else
  82. echo "rtcwake alarm not enabled in $rtcwake_config_file, doing nothing...";
  83. exit $NA
  84. fi
  85. }
  86. case "$1" in
  87. suspend) set_alarm $SUSPEND_RTCWAKE_ENABLED ;;
  88. hibernate) set_alarm $HIBERNATE_RTCWAKE_ENABLED ;;
  89. *) exit $NA ;;
  90. esac