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