]>
Commit | Line | Data |
---|---|---|
76700389 BW |
1 | /* |
2 | * rtcwake -- enter a system sleep state until specified wakeup time. | |
3 | * | |
4 | * This uses cross-platform Linux interfaces to enter a system sleep state, | |
5 | * and leave it no later than a specified time. It uses any RTC framework | |
6 | * driver that supports standard driver model wakeup flags. | |
7 | * | |
8 | * This is normally used like the old "apmsleep" utility, to wake from a | |
9 | * suspend state like ACPI S1 (standby) or S3 (suspend-to-RAM). Most | |
10 | * platforms can implement those without analogues of BIOS, APM, or ACPI. | |
11 | * | |
12 | * On some systems, this can also be used like "nvram-wakeup", waking | |
13 | * from states like ACPI S4 (suspend to disk). Not all systems have | |
14 | * persistent media that are appropriate for such suspend modes. | |
15 | * | |
16 | * The best way to set the system's RTC is so that it holds the current | |
17 | * time in UTC. Use the "-l" flag to tell this program that the system | |
18 | * RTC uses a local timezone instead (maybe you dual-boot MS-Windows). | |
2148b051 | 19 | * That flag should not be needed on systems with adjtime support. |
76700389 BW |
20 | */ |
21 | ||
22 | #include <stdio.h> | |
23 | #include <getopt.h> | |
24 | #include <fcntl.h> | |
c41e1340 | 25 | #include <libgen.h> |
76700389 BW |
26 | #include <stdlib.h> |
27 | #include <string.h> | |
28 | #include <unistd.h> | |
29 | #include <errno.h> | |
30 | #include <time.h> | |
31 | ||
32 | #include <sys/ioctl.h> | |
33 | #include <sys/time.h> | |
34 | #include <sys/types.h> | |
35 | ||
36 | #include <linux/rtc.h> | |
37 | ||
38 | #include "nls.h" | |
77f5744c | 39 | #include "pathnames.h" |
437fa54f | 40 | #include "usleep.h" |
76700389 BW |
41 | |
42 | /* constants from legacy PC/AT hardware */ | |
43 | #define RTC_PF 0x40 | |
44 | #define RTC_AF 0x20 | |
45 | #define RTC_UF 0x10 | |
46 | ||
47 | #define MAX_LINE 1024 | |
48 | ||
49 | static char *progname; | |
50 | ||
51 | #define VERSION_STRING "rtcwake from " PACKAGE_STRING | |
52 | #define RTC_PATH "/sys/class/rtc/%s/device/power/wakeup" | |
d3cf5414 | 53 | #define SYS_POWER_STATE_PATH "/sys/power/state" |
76700389 BW |
54 | #define ADJTIME_PATH "/etc/adjtime" |
55 | #define DEFAULT_DEVICE "/dev/rtc0" | |
47bf8ef7 | 56 | #define DEFAULT_MODE "standby" |
76700389 BW |
57 | |
58 | enum ClockMode { | |
59 | CM_AUTO, | |
60 | CM_UTC, | |
61 | CM_LOCAL | |
62 | }; | |
63 | ||
64 | static unsigned verbose; | |
65 | enum ClockMode clock_mode = CM_AUTO; | |
66 | ||
67 | static struct option long_options[] = { | |
68 | {"auto", no_argument, 0, 'a'}, | |
69 | {"local", no_argument, 0, 'l'}, | |
70 | {"utc", no_argument, 0, 'u'}, | |
71 | {"verbose", no_argument, 0, 'v'}, | |
72 | {"version", no_argument, 0, 'V'}, | |
73 | {"help", no_argument, 0, 'h'}, | |
74 | {"mode", required_argument, 0, 'm'}, | |
75 | {"device", required_argument, 0, 'd'}, | |
76 | {"seconds", required_argument, 0, 's'}, | |
77 | {"time", required_argument, 0, 't'}, | |
78 | {0, 0, 0, 0 } | |
79 | }; | |
80 | ||
81 | static void usage(int retval) | |
82 | { | |
83 | printf(_("usage: %s [options]\n" | |
84 | " -d | --device <device> select rtc device (rtc0|rtc1|...)\n" | |
85 | " -l | --local RTC uses local timezone\n" | |
86 | " -m | --mode standby|mem|... sleep mode\n" | |
87 | " -s | --seconds <seconds> seconds to sleep\n" | |
88 | " -t | --time <time_t> time to wake\n" | |
89 | " -u | --utc RTC uses UTC\n" | |
90 | " -v | --verbose verbose messages\n" | |
91 | " -V | --version show version\n"), | |
92 | progname); | |
93 | exit(retval); | |
94 | } | |
95 | ||
f8d87ab1 | 96 | static int is_wakeup_enabled(const char *devname) |
76700389 BW |
97 | { |
98 | char buf[128], *s; | |
99 | FILE *f; | |
100 | ||
101 | /* strip the '/dev/' from the devname here */ | |
102 | snprintf(buf, sizeof buf, RTC_PATH, devname + strlen("/dev/")); | |
103 | f = fopen(buf, "r"); | |
104 | if (!f) { | |
105 | perror(buf); | |
106 | return 0; | |
107 | } | |
108 | s = fgets(buf, sizeof buf, f); | |
109 | fclose(f); | |
110 | if (!s) | |
111 | return 0; | |
112 | ||
113 | s = strchr(buf, '\n'); | |
114 | if (!s) | |
115 | return 0; | |
116 | *s = 0; | |
117 | ||
118 | /* wakeup events could be disabled or not supported */ | |
119 | return strcmp(buf, "enabled") == 0; | |
120 | } | |
121 | ||
122 | /* all times should be in UTC */ | |
123 | static time_t sys_time; | |
124 | static time_t rtc_time; | |
125 | ||
126 | static int get_basetimes(int fd) | |
127 | { | |
128 | struct tm tm; | |
129 | struct rtc_time rtc; | |
130 | ||
131 | /* this process works in RTC time, except when working | |
132 | * with the system clock (which always uses UTC). | |
133 | */ | |
134 | if (clock_mode == CM_UTC) | |
135 | setenv("TZ", "UTC", 1); | |
136 | tzset(); | |
137 | ||
138 | /* read rtc and system clocks "at the same time", or as | |
139 | * precisely (+/- a second) as we can read them. | |
140 | */ | |
141 | if (ioctl(fd, RTC_RD_TIME, &rtc) < 0) { | |
142 | perror(_("read rtc time")); | |
f8d87ab1 | 143 | return -1; |
76700389 BW |
144 | } |
145 | sys_time = time(0); | |
146 | if (sys_time == (time_t)-1) { | |
147 | perror(_("read system time")); | |
f8d87ab1 | 148 | return -1; |
76700389 BW |
149 | } |
150 | ||
151 | /* convert rtc_time to normal arithmetic-friendly form, | |
152 | * updating tm.tm_wday as used by asctime(). | |
153 | */ | |
154 | memset(&tm, 0, sizeof tm); | |
155 | tm.tm_sec = rtc.tm_sec; | |
156 | tm.tm_min = rtc.tm_min; | |
157 | tm.tm_hour = rtc.tm_hour; | |
158 | tm.tm_mday = rtc.tm_mday; | |
159 | tm.tm_mon = rtc.tm_mon; | |
160 | tm.tm_year = rtc.tm_year; | |
1da17ec6 | 161 | tm.tm_isdst = -1; /* assume the system knows better than the RTC */ |
76700389 BW |
162 | rtc_time = mktime(&tm); |
163 | ||
164 | if (rtc_time == (time_t)-1) { | |
165 | perror(_("convert rtc time")); | |
f8d87ab1 | 166 | return -1; |
76700389 BW |
167 | } |
168 | ||
169 | if (verbose) { | |
2148b051 DB |
170 | /* Unless the system uses UTC, either delta or tzone |
171 | * reflects a seconds offset from UTC. The value can | |
172 | * help sort out problems like bugs in your C library. | |
173 | */ | |
174 | printf("\tdelta = %ld\n", sys_time - rtc_time); | |
175 | printf("\ttzone = %ld\n", timezone); | |
176 | ||
177 | printf("\ttzname = %s\n", tzname[daylight]); | |
178 | gmtime_r(&rtc_time, &tm); | |
179 | printf("\tsystime = %ld, (UTC) %s", | |
76700389 | 180 | (long) sys_time, asctime(gmtime(&sys_time))); |
2148b051 | 181 | printf("\trtctime = %ld, (UTC) %s", |
76700389 BW |
182 | (long) rtc_time, asctime(&tm)); |
183 | } | |
184 | ||
f8d87ab1 | 185 | return 0; |
76700389 BW |
186 | } |
187 | ||
188 | static int setup_alarm(int fd, time_t *wakeup) | |
189 | { | |
190 | struct tm *tm; | |
191 | struct rtc_wkalrm wake; | |
192 | ||
1b7c164c DB |
193 | /* The wakeup time is in POSIX time (more or less UTC). |
194 | * Ideally RTCs use that same time; but PCs can't do that | |
195 | * if they need to boot MS-Windows. Messy... | |
196 | * | |
197 | * When clock_mode == CM_UTC this process's timezone is UTC, | |
198 | * so we'll pass a UTC date to the RTC. | |
199 | * | |
200 | * Else clock_mode == CM_LOCAL so the time given to the RTC | |
201 | * will instead use the local time zone. | |
202 | */ | |
203 | tm = localtime(wakeup); | |
76700389 BW |
204 | |
205 | wake.time.tm_sec = tm->tm_sec; | |
206 | wake.time.tm_min = tm->tm_min; | |
207 | wake.time.tm_hour = tm->tm_hour; | |
208 | wake.time.tm_mday = tm->tm_mday; | |
209 | wake.time.tm_mon = tm->tm_mon; | |
210 | wake.time.tm_year = tm->tm_year; | |
2148b051 DB |
211 | /* wday, yday, and isdst fields are unused by Linux */ |
212 | wake.time.tm_wday = -1; | |
213 | wake.time.tm_yday = -1; | |
214 | wake.time.tm_isdst = -1; | |
76700389 | 215 | |
fc181184 GB |
216 | wake.enabled = 1; |
217 | /* First try the preferred RTC_WKALM_SET */ | |
218 | if (ioctl(fd, RTC_WKALM_SET, &wake) < 0) { | |
219 | wake.enabled = 0; | |
220 | /* Fall back on the non-preferred way of setting wakeups; only | |
221 | * works for alarms < 24 hours from now */ | |
222 | if ((rtc_time + (24 * 60 * 60)) > *wakeup) { | |
223 | if (ioctl(fd, RTC_ALM_SET, &wake.time) < 0) { | |
224 | perror(_("set rtc alarm")); | |
225 | return -1; | |
226 | } | |
227 | if (ioctl(fd, RTC_AIE_ON, 0) < 0) { | |
228 | perror(_("enable rtc alarm")); | |
229 | return -1; | |
230 | } | |
231 | } else { | |
76700389 | 232 | perror(_("set rtc wake alarm")); |
fc181184 | 233 | return -1; |
76700389 BW |
234 | } |
235 | } | |
236 | ||
f8d87ab1 | 237 | return 0; |
76700389 BW |
238 | } |
239 | ||
240 | static void suspend_system(const char *suspend) | |
241 | { | |
d3cf5414 | 242 | FILE *f = fopen(SYS_POWER_STATE_PATH, "w"); |
76700389 BW |
243 | |
244 | if (!f) { | |
d3cf5414 | 245 | perror(SYS_POWER_STATE_PATH); |
76700389 BW |
246 | return; |
247 | } | |
248 | ||
249 | fprintf(f, "%s\n", suspend); | |
250 | fflush(f); | |
251 | ||
252 | /* this executes after wake from suspend */ | |
253 | fclose(f); | |
254 | } | |
255 | ||
256 | ||
257 | static int read_clock_mode(void) | |
258 | { | |
259 | FILE *fp; | |
260 | char linebuf[MAX_LINE]; | |
261 | ||
262 | fp = fopen(ADJTIME_PATH, "r"); | |
263 | if (!fp) | |
f8d87ab1 | 264 | return -1; |
76700389 BW |
265 | |
266 | /* skip first line */ | |
267 | if (!fgets(linebuf, MAX_LINE, fp)) { | |
268 | fclose(fp); | |
f8d87ab1 | 269 | return -1; |
76700389 BW |
270 | } |
271 | ||
272 | /* skip second line */ | |
273 | if (!fgets(linebuf, MAX_LINE, fp)) { | |
274 | fclose(fp); | |
f8d87ab1 | 275 | return -1; |
76700389 BW |
276 | } |
277 | ||
278 | /* read third line */ | |
279 | if (!fgets(linebuf, MAX_LINE, fp)) { | |
280 | fclose(fp); | |
f8d87ab1 | 281 | return -1; |
76700389 BW |
282 | } |
283 | ||
284 | if (strncmp(linebuf, "UTC", 3) == 0) | |
285 | clock_mode = CM_UTC; | |
286 | else if (strncmp(linebuf, "LOCAL", 5) == 0) | |
287 | clock_mode = CM_LOCAL; | |
288 | ||
289 | fclose(fp); | |
290 | ||
f8d87ab1 | 291 | return 0; |
76700389 BW |
292 | } |
293 | ||
294 | int main(int argc, char **argv) | |
295 | { | |
296 | char *devname = DEFAULT_DEVICE; | |
297 | unsigned seconds = 0; | |
298 | char *suspend = DEFAULT_MODE; | |
299 | ||
77f5744c | 300 | int rc = EXIT_SUCCESS; |
76700389 BW |
301 | int t; |
302 | int fd; | |
303 | time_t alarm = 0; | |
304 | ||
305 | setlocale(LC_ALL, ""); | |
306 | bindtextdomain(PACKAGE, LOCALEDIR); | |
307 | textdomain(PACKAGE); | |
308 | ||
309 | progname = basename(argv[0]); | |
310 | ||
311 | while ((t = getopt_long(argc, argv, "ahd:lm:s:t:uVv", | |
312 | long_options, NULL)) != EOF) { | |
313 | switch (t) { | |
314 | case 'a': | |
315 | /* CM_AUTO is default */ | |
316 | break; | |
317 | ||
318 | case 'd': | |
319 | devname = strdup(optarg); | |
320 | break; | |
321 | ||
322 | case 'l': | |
323 | clock_mode = CM_LOCAL; | |
324 | break; | |
325 | ||
326 | /* what system power mode to use? for now handle only | |
327 | * standardized mode names; eventually when systems | |
328 | * define their own state names, parse | |
329 | * /sys/power/state. | |
330 | * | |
331 | * "on" is used just to test the RTC alarm mechanism, | |
332 | * bypassing all the wakeup-from-sleep infrastructure. | |
333 | */ | |
334 | case 'm': | |
335 | if (strcmp(optarg, "standby") == 0 | |
336 | || strcmp(optarg, "mem") == 0 | |
337 | || strcmp(optarg, "disk") == 0 | |
338 | || strcmp(optarg, "on") == 0 | |
e4b0fc36 | 339 | || strcmp(optarg, "no") == 0 |
77f5744c | 340 | || strcmp(optarg, "off") == 0 |
76700389 BW |
341 | ) { |
342 | suspend = strdup(optarg); | |
343 | break; | |
344 | } | |
2148b051 DB |
345 | fprintf(stderr, |
346 | _("%s: unrecognized suspend state '%s'\n"), | |
347 | progname, optarg); | |
76700389 BW |
348 | usage(EXIT_FAILURE); |
349 | ||
350 | /* alarm time, seconds-to-sleep (relative) */ | |
351 | case 's': | |
352 | t = atoi(optarg); | |
353 | if (t < 0) { | |
354 | fprintf(stderr, | |
355 | _("%s: illegal interval %s seconds\n"), | |
356 | progname, optarg); | |
357 | usage(EXIT_FAILURE); | |
358 | } | |
359 | seconds = t; | |
360 | break; | |
361 | ||
362 | /* alarm time, time_t (absolute, seconds since | |
363 | * 1/1 1970 UTC) | |
364 | */ | |
365 | case 't': | |
366 | t = atoi(optarg); | |
367 | if (t < 0) { | |
368 | fprintf(stderr, | |
369 | _("%s: illegal time_t value %s\n"), | |
370 | progname, optarg); | |
371 | usage(EXIT_FAILURE); | |
372 | } | |
373 | alarm = t; | |
374 | break; | |
375 | ||
376 | case 'u': | |
377 | clock_mode = CM_UTC; | |
378 | break; | |
379 | ||
380 | case 'v': | |
381 | verbose++; | |
382 | break; | |
383 | ||
384 | case 'V': | |
385 | printf(_("%s: version %s\n"), progname, VERSION_STRING); | |
386 | exit(EXIT_SUCCESS); | |
387 | ||
388 | case 'h': | |
389 | usage(EXIT_SUCCESS); | |
390 | ||
391 | default: | |
392 | usage(EXIT_FAILURE); | |
393 | } | |
394 | } | |
395 | ||
396 | if (clock_mode == CM_AUTO) { | |
f8d87ab1 | 397 | if (read_clock_mode() < 0) { |
76700389 BW |
398 | printf(_("%s: assuming RTC uses UTC ...\n"), progname); |
399 | clock_mode = CM_UTC; | |
400 | } | |
76700389 | 401 | } |
2148b051 DB |
402 | if (verbose) |
403 | printf(clock_mode == CM_UTC ? _("Using UTC time.\n") : | |
404 | _("Using local time.\n")); | |
76700389 BW |
405 | |
406 | if (!alarm && !seconds) { | |
407 | fprintf(stderr, _("%s: must provide wake time\n"), progname); | |
408 | usage(EXIT_FAILURE); | |
409 | } | |
410 | ||
411 | /* when devname doesn't start with /dev, append it */ | |
412 | if (strncmp(devname, "/dev/", strlen("/dev/")) != 0) { | |
413 | char *new_devname; | |
414 | ||
415 | new_devname = malloc(strlen(devname) + strlen("/dev/") + 1); | |
416 | if (!new_devname) { | |
417 | perror(_("malloc() failed")); | |
418 | exit(EXIT_FAILURE); | |
419 | } | |
420 | ||
421 | strcpy(new_devname, "/dev/"); | |
422 | strcat(new_devname, devname); | |
423 | free(devname); | |
424 | devname = new_devname; | |
425 | } | |
426 | ||
e4b0fc36 MI |
427 | if (strcmp(suspend, "on") != 0 && strcmp(suspend, "no") != 0 |
428 | && !is_wakeup_enabled(devname)) { | |
76700389 BW |
429 | fprintf(stderr, _("%s: %s not enabled for wakeup events\n"), |
430 | progname, devname); | |
431 | exit(EXIT_FAILURE); | |
432 | } | |
433 | ||
434 | /* this RTC must exist and (if we'll sleep) be wakeup-enabled */ | |
77f5744c KZ |
435 | #ifdef O_CLOEXEC |
436 | fd = open(devname, O_RDONLY | O_CLOEXEC); | |
437 | #else | |
76700389 | 438 | fd = open(devname, O_RDONLY); |
77f5744c | 439 | #endif |
76700389 BW |
440 | if (fd < 0) { |
441 | perror(devname); | |
442 | exit(EXIT_FAILURE); | |
443 | } | |
444 | ||
445 | /* relative or absolute alarm time, normalized to time_t */ | |
f8d87ab1 | 446 | if (get_basetimes(fd) < 0) |
76700389 BW |
447 | exit(EXIT_FAILURE); |
448 | if (verbose) | |
449 | printf(_("alarm %ld, sys_time %ld, rtc_time %ld, seconds %u\n"), | |
450 | alarm, sys_time, rtc_time, seconds); | |
451 | if (alarm) { | |
452 | if (alarm < sys_time) { | |
2148b051 DB |
453 | fprintf(stderr, |
454 | _("%s: time doesn't go backward to %s\n"), | |
455 | progname, ctime(&alarm)); | |
76700389 BW |
456 | exit(EXIT_FAILURE); |
457 | } | |
458 | alarm += sys_time - rtc_time; | |
459 | } else | |
460 | alarm = rtc_time + seconds + 1; | |
77f5744c | 461 | |
76700389 BW |
462 | if (setup_alarm(fd, &alarm) < 0) |
463 | exit(EXIT_FAILURE); | |
464 | ||
76700389 BW |
465 | printf(_("%s: wakeup from \"%s\" using %s at %s\n"), |
466 | progname, suspend, devname, | |
467 | ctime(&alarm)); | |
468 | fflush(stdout); | |
469 | usleep(10 * 1000); | |
470 | ||
ecd55f96 KZ |
471 | if (strcmp(suspend, "no") == 0) { |
472 | if (verbose) | |
473 | printf(_("suspend mode: no; leaving\n")); | |
474 | close(fd); | |
e4b0fc36 | 475 | exit(EXIT_SUCCESS); |
ecd55f96 | 476 | |
77f5744c KZ |
477 | } else if (strcmp(suspend, "off") == 0) { |
478 | char *arg[4]; | |
479 | int i = 0; | |
480 | ||
ecd55f96 KZ |
481 | if (verbose) |
482 | printf(_("suspend mode: off; executing %s\n"), | |
483 | _PATH_SHUTDOWN); | |
77f5744c KZ |
484 | arg[i++] = _PATH_SHUTDOWN; |
485 | arg[i++] = "-P"; | |
486 | arg[i++] = "now"; | |
487 | arg[i] = NULL; | |
488 | ||
489 | execv(arg[0], arg); | |
490 | ||
491 | fprintf(stderr, _("%s: unable to execute %s: %s\n"), | |
492 | progname, _PATH_SHUTDOWN, strerror(errno)); | |
493 | rc = EXIT_FAILURE; | |
ecd55f96 KZ |
494 | |
495 | } else if (strcmp(suspend, "on") == 0) { | |
76700389 BW |
496 | unsigned long data; |
497 | ||
ecd55f96 KZ |
498 | if (verbose) |
499 | printf(_("suspend mode: on; reading rtc\n")); | |
500 | ||
76700389 BW |
501 | do { |
502 | t = read(fd, &data, sizeof data); | |
503 | if (t < 0) { | |
504 | perror(_("rtc read")); | |
505 | break; | |
506 | } | |
507 | if (verbose) | |
508 | printf("... %s: %03lx\n", devname, data); | |
509 | } while (!(data & RTC_AF)); | |
ecd55f96 KZ |
510 | |
511 | } else { | |
512 | if (verbose) | |
513 | printf(_("suspend mode: %s; suspending system\n"), suspend); | |
514 | sync(); | |
515 | suspend_system(suspend); | |
76700389 BW |
516 | } |
517 | ||
518 | if (ioctl(fd, RTC_AIE_OFF, 0) < 0) | |
519 | perror(_("disable rtc alarm interrupt")); | |
520 | ||
521 | close(fd); | |
522 | ||
77f5744c | 523 | return rc; |
76700389 | 524 | } |