rtcwake.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* rtcwake.c - enter sleep state until given time.
  2. *
  3. * Copyright 2020 The Android Open Source Project
  4. USE_RTCWAKE(NEWTOY(rtcwake, "(list-modes);(auto)a(device)d:(local)l(mode)m:(seconds)s#(time)t#(utc)u(verbose)v[!alu]", TOYFLAG_USR|TOYFLAG_BIN))
  5. config RTCWAKE
  6. bool "rtcwake"
  7. default y
  8. help
  9. usage: rtcwake [-aluv] [-d FILE] [-m MODE] [-s SECS] [-t UNIX]
  10. Enter the given sleep state until the given time.
  11. -a RTC uses time specified in /etc/adjtime
  12. -d FILE Device to use (default /dev/rtc)
  13. -l RTC uses local time
  14. -m Mode (--list-modes to see those supported by your kernel):
  15. standby S1: default mem S3: suspend to RAM
  16. disk S4: suspend to disk off S5: power off
  17. disable Cancel current alarm freeze stop processes/processors
  18. no just set wakeup time on just poll RTC for alarm
  19. show just show current alarm
  20. -s SECS Wake SECS seconds from now
  21. -t UNIX Wake UNIX seconds from epoch
  22. -u RTC uses UTC
  23. -v Verbose
  24. */
  25. #define FOR_rtcwake
  26. #include "toys.h"
  27. #include <linux/rtc.h>
  28. GLOBALS(
  29. long t, s;
  30. char *m, *d;
  31. )
  32. void rtcwake_main(void)
  33. {
  34. struct rtc_wkalrm *alarm = (void *)(toybuf+2048);
  35. struct tm rtc_tm;
  36. time_t now, rtc_now, then;
  37. int fd, utc;
  38. if (FLAG(list_modes)) {
  39. printf("off no on disable show %s",
  40. xreadfile("/sys/power/state", toybuf, 2048));
  41. return;
  42. }
  43. // util-linux defaults to "suspend", even though I don't have anything that
  44. // supports that (testing everything from a ~2010 laptop to a 2019 desktop).
  45. if (!TT.m) TT.m = "suspend";
  46. if (FLAG(u)) utc = 1;
  47. else if (FLAG(l)) utc = 0;
  48. else utc = !readfile("/etc/adjtime", toybuf, 2048) || !!strstr(toybuf, "UTC");
  49. if (FLAG(v)) xprintf("RTC time: %s\n", utc ? "UTC" : "local");
  50. if (!TT.d) TT.d = "/dev/rtc0";
  51. if (FLAG(v)) xprintf("Device: %s\n", TT.d);
  52. fd = xopen(TT.d, O_RDWR);
  53. now = time(0);
  54. xioctl(fd, RTC_RD_TIME, &rtc_tm);
  55. rtc_now = xmktime(&rtc_tm, utc);
  56. if (FLAG(v)) {
  57. xprintf("System time:\t%lld / %s", (long long)now, ctime(&now));
  58. xprintf("RTC time:\t%lld / %s", (long long)rtc_now, ctime(&rtc_now));
  59. }
  60. if (!strcmp(TT.m, "show")) { // Don't suspend, just show current alarm.
  61. xioctl(fd, RTC_WKALM_RD, alarm);
  62. if (!alarm->enabled) xputs("alarm: off");
  63. else {
  64. if ((then = mktime((void *)&alarm->time)) < 0) perror_exit("mktime");
  65. xprintf("alarm: on %s", ctime(&then));
  66. }
  67. return;
  68. } else if (!strcmp(TT.m, "disable")) { // Cancel current alarm.
  69. xioctl(fd, RTC_WKALM_RD, alarm);
  70. alarm->enabled = 0;
  71. xioctl(fd, RTC_WKALM_SET, alarm);
  72. return;
  73. }
  74. if (FLAG(s)) then = rtc_now + TT.s + 1; // strace shows util-linux adds 1.
  75. else if (FLAG(t)) {
  76. then = TT.t + (rtc_now - now);
  77. if (then<=rtc_now) error_exit("rtc %lld >= %ld", (long long)rtc_now, TT.t);
  78. } else help_exit("-m %s needs -s or -t", TT.m);
  79. if (FLAG(v)) xprintf("Wake time:\t%lld / %s", (long long)then, ctime(&then));
  80. if (!(utc ? gmtime_r : localtime_r)(&then, (void *)&alarm->time))
  81. error_exit("%s failed", utc ? "gmtime_r" : "localtime_r");
  82. alarm->enabled = 1;
  83. xioctl(fd, RTC_WKALM_SET, alarm);
  84. sync();
  85. xprintf("wakeup using \"%s\" from %s at %s", TT.m, TT.d, ctime(&then));
  86. msleep(10);
  87. if (!strcmp(TT.m, "no")); // Don't suspend, just set wakeup time.
  88. else if (!strcmp(TT.m, "on")) { // Don't suspend, poll RTC for alarm.
  89. unsigned long data = 0;
  90. if (FLAG(v)) xputs("Reading RTC...");
  91. while (!(data & RTC_AF)) {
  92. if (read(fd, &data, sizeof(data)) != sizeof(data)) perror_exit("read");
  93. if (FLAG(v)) xprintf("... %s: %lx\n", TT.d, data);
  94. }
  95. } else if (!strcmp(TT.m, "off")) xexec((char *[]){"poweroff", 0});
  96. // Everything else lands here for one final step. The write will fail with
  97. // EINVAL if the mode is not supported.
  98. else xwrite(xopen("/sys/power/state", O_WRONLY), TT.m, strlen(TT.m));
  99. }