]> git.ipfire.org Git - thirdparty/util-linux.git/blame - sys-utils/rtcwake.c
renice: cleanup usage()
[thirdparty/util-linux.git] / sys-utils / rtcwake.c
CommitLineData
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"
2ab428f6 39#include "xalloc.h"
77f5744c 40#include "pathnames.h"
437fa54f 41#include "usleep.h"
07b336c9 42#include "strutils.h"
eb76ca98 43#include "c.h"
76700389
BW
44
45/* constants from legacy PC/AT hardware */
46#define RTC_PF 0x40
47#define RTC_AF 0x20
48#define RTC_UF 0x10
49
50#define MAX_LINE 1024
51
76700389
BW
52#define VERSION_STRING "rtcwake from " PACKAGE_STRING
53#define RTC_PATH "/sys/class/rtc/%s/device/power/wakeup"
d3cf5414 54#define SYS_POWER_STATE_PATH "/sys/power/state"
76700389
BW
55#define ADJTIME_PATH "/etc/adjtime"
56#define DEFAULT_DEVICE "/dev/rtc0"
47bf8ef7 57#define DEFAULT_MODE "standby"
76700389
BW
58
59enum ClockMode {
60 CM_AUTO,
61 CM_UTC,
62 CM_LOCAL
63};
64
65static unsigned verbose;
569f3ca2 66static unsigned dryrun;
76700389
BW
67enum ClockMode clock_mode = CM_AUTO;
68
69static struct option long_options[] = {
70 {"auto", no_argument, 0, 'a'},
569f3ca2 71 {"dry-run", no_argument, 0, 'n'},
76700389
BW
72 {"local", no_argument, 0, 'l'},
73 {"utc", no_argument, 0, 'u'},
74 {"verbose", no_argument, 0, 'v'},
75 {"version", no_argument, 0, 'V'},
76 {"help", no_argument, 0, 'h'},
77 {"mode", required_argument, 0, 'm'},
78 {"device", required_argument, 0, 'd'},
79 {"seconds", required_argument, 0, 's'},
80 {"time", required_argument, 0, 't'},
81 {0, 0, 0, 0 }
82};
83
07b336c9 84static void __attribute__((__noreturn__)) usage(FILE *out)
76700389 85{
07b336c9
KZ
86 fprintf(out, _("Usage: %s [options]\n\nOptions:\n"),
87 program_invocation_short_name);
88
89 fprintf(out, _(
76700389 90 " -d | --device <device> select rtc device (rtc0|rtc1|...)\n"
569f3ca2 91 " -n | --dry-run does everything, but suspend\n"
76700389 92 " -l | --local RTC uses local timezone\n"
07b336c9 93 " -m | --mode <mode> standby|mem|... sleep mode\n"
76700389
BW
94 " -s | --seconds <seconds> seconds to sleep\n"
95 " -t | --time <time_t> time to wake\n"
96 " -u | --utc RTC uses UTC\n"
97 " -v | --verbose verbose messages\n"
07b336c9
KZ
98 " -V | --version show version\n"));
99
100 fprintf(out, _("\nFor more information see rtcwake(8).\n"));
101
102 exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
76700389
BW
103}
104
f8d87ab1 105static int is_wakeup_enabled(const char *devname)
76700389
BW
106{
107 char buf[128], *s;
108 FILE *f;
109
110 /* strip the '/dev/' from the devname here */
111 snprintf(buf, sizeof buf, RTC_PATH, devname + strlen("/dev/"));
112 f = fopen(buf, "r");
113 if (!f) {
07b336c9 114 warn(_("open failed: %s"), buf);
76700389
BW
115 return 0;
116 }
117 s = fgets(buf, sizeof buf, f);
118 fclose(f);
119 if (!s)
120 return 0;
121
122 s = strchr(buf, '\n');
123 if (!s)
124 return 0;
125 *s = 0;
126
127 /* wakeup events could be disabled or not supported */
128 return strcmp(buf, "enabled") == 0;
129}
130
131/* all times should be in UTC */
132static time_t sys_time;
133static time_t rtc_time;
134
135static int get_basetimes(int fd)
136{
137 struct tm tm;
138 struct rtc_time rtc;
139
140 /* this process works in RTC time, except when working
141 * with the system clock (which always uses UTC).
142 */
143 if (clock_mode == CM_UTC)
144 setenv("TZ", "UTC", 1);
145 tzset();
146
147 /* read rtc and system clocks "at the same time", or as
148 * precisely (+/- a second) as we can read them.
149 */
150 if (ioctl(fd, RTC_RD_TIME, &rtc) < 0) {
07b336c9 151 warn(_("read rtc time failed"));
f8d87ab1 152 return -1;
76700389
BW
153 }
154 sys_time = time(0);
155 if (sys_time == (time_t)-1) {
07b336c9 156 warn(_("read system time failed"));
f8d87ab1 157 return -1;
76700389
BW
158 }
159
160 /* convert rtc_time to normal arithmetic-friendly form,
161 * updating tm.tm_wday as used by asctime().
162 */
163 memset(&tm, 0, sizeof tm);
164 tm.tm_sec = rtc.tm_sec;
165 tm.tm_min = rtc.tm_min;
166 tm.tm_hour = rtc.tm_hour;
167 tm.tm_mday = rtc.tm_mday;
168 tm.tm_mon = rtc.tm_mon;
169 tm.tm_year = rtc.tm_year;
1da17ec6 170 tm.tm_isdst = -1; /* assume the system knows better than the RTC */
76700389
BW
171 rtc_time = mktime(&tm);
172
173 if (rtc_time == (time_t)-1) {
07b336c9 174 warn(_("convert rtc time failed"));
f8d87ab1 175 return -1;
76700389
BW
176 }
177
178 if (verbose) {
2148b051
DB
179 /* Unless the system uses UTC, either delta or tzone
180 * reflects a seconds offset from UTC. The value can
181 * help sort out problems like bugs in your C library.
182 */
183 printf("\tdelta = %ld\n", sys_time - rtc_time);
184 printf("\ttzone = %ld\n", timezone);
185
186 printf("\ttzname = %s\n", tzname[daylight]);
187 gmtime_r(&rtc_time, &tm);
188 printf("\tsystime = %ld, (UTC) %s",
76700389 189 (long) sys_time, asctime(gmtime(&sys_time)));
2148b051 190 printf("\trtctime = %ld, (UTC) %s",
76700389
BW
191 (long) rtc_time, asctime(&tm));
192 }
193
f8d87ab1 194 return 0;
76700389
BW
195}
196
197static int setup_alarm(int fd, time_t *wakeup)
198{
199 struct tm *tm;
200 struct rtc_wkalrm wake;
201
1b7c164c
DB
202 /* The wakeup time is in POSIX time (more or less UTC).
203 * Ideally RTCs use that same time; but PCs can't do that
204 * if they need to boot MS-Windows. Messy...
205 *
206 * When clock_mode == CM_UTC this process's timezone is UTC,
207 * so we'll pass a UTC date to the RTC.
208 *
209 * Else clock_mode == CM_LOCAL so the time given to the RTC
210 * will instead use the local time zone.
211 */
212 tm = localtime(wakeup);
76700389
BW
213
214 wake.time.tm_sec = tm->tm_sec;
215 wake.time.tm_min = tm->tm_min;
216 wake.time.tm_hour = tm->tm_hour;
217 wake.time.tm_mday = tm->tm_mday;
218 wake.time.tm_mon = tm->tm_mon;
219 wake.time.tm_year = tm->tm_year;
2148b051
DB
220 /* wday, yday, and isdst fields are unused by Linux */
221 wake.time.tm_wday = -1;
222 wake.time.tm_yday = -1;
223 wake.time.tm_isdst = -1;
76700389 224
fc181184 225 wake.enabled = 1;
569f3ca2 226
fc181184 227 /* First try the preferred RTC_WKALM_SET */
569f3ca2 228 if (!dryrun && ioctl(fd, RTC_WKALM_SET, &wake) < 0) {
fc181184
GB
229 wake.enabled = 0;
230 /* Fall back on the non-preferred way of setting wakeups; only
231 * works for alarms < 24 hours from now */
232 if ((rtc_time + (24 * 60 * 60)) > *wakeup) {
233 if (ioctl(fd, RTC_ALM_SET, &wake.time) < 0) {
07b336c9 234 warn(_("set rtc alarm failed"));
fc181184
GB
235 return -1;
236 }
237 if (ioctl(fd, RTC_AIE_ON, 0) < 0) {
07b336c9 238 warn(_("enable rtc alarm failed"));
fc181184
GB
239 return -1;
240 }
241 } else {
07b336c9 242 warn(_("set rtc wake alarm failed"));
fc181184 243 return -1;
76700389
BW
244 }
245 }
246
f8d87ab1 247 return 0;
76700389
BW
248}
249
e6d1dc94
LR
250static int is_suspend_available(const char *suspend)
251{
252 int rc;
253 char buf[32];
254 FILE *f = fopen(SYS_POWER_STATE_PATH, "r");
255
256 if (!f)
257 return -1;
258
259 if (fgets(buf, sizeof buf, f) == NULL)
260 rc = -1;
261 else
262 rc = strstr(buf, suspend) != NULL;
263
264 fclose(f);
265 return rc;
266}
267
76700389
BW
268static void suspend_system(const char *suspend)
269{
d3cf5414 270 FILE *f = fopen(SYS_POWER_STATE_PATH, "w");
76700389
BW
271
272 if (!f) {
07b336c9 273 warn(_("open failed: %s"), SYS_POWER_STATE_PATH);
76700389
BW
274 return;
275 }
276
569f3ca2
KZ
277 if (!dryrun) {
278 fprintf(f, "%s\n", suspend);
279 fflush(f);
280 }
76700389
BW
281
282 /* this executes after wake from suspend */
283 fclose(f);
284}
285
286
287static int read_clock_mode(void)
288{
289 FILE *fp;
290 char linebuf[MAX_LINE];
291
292 fp = fopen(ADJTIME_PATH, "r");
293 if (!fp)
f8d87ab1 294 return -1;
76700389
BW
295
296 /* skip first line */
297 if (!fgets(linebuf, MAX_LINE, fp)) {
298 fclose(fp);
f8d87ab1 299 return -1;
76700389
BW
300 }
301
302 /* skip second line */
303 if (!fgets(linebuf, MAX_LINE, fp)) {
304 fclose(fp);
f8d87ab1 305 return -1;
76700389
BW
306 }
307
308 /* read third line */
309 if (!fgets(linebuf, MAX_LINE, fp)) {
310 fclose(fp);
f8d87ab1 311 return -1;
76700389
BW
312 }
313
314 if (strncmp(linebuf, "UTC", 3) == 0)
315 clock_mode = CM_UTC;
316 else if (strncmp(linebuf, "LOCAL", 5) == 0)
317 clock_mode = CM_LOCAL;
318
319 fclose(fp);
320
f8d87ab1 321 return 0;
76700389
BW
322}
323
fcf67294
MO
324/**
325 * print basic alarm settings
326 */
327static int print_alarm(int fd)
328{
329 struct rtc_wkalrm wake;
330 struct rtc_time rtc;
331 struct tm tm;
332 time_t alarm;
333
334 /* First try the preferred RTC_WKALM_RD */
335 if (ioctl(fd, RTC_WKALM_RD, &wake) < 0) {
336 /* Fall back on the non-preferred way of reading wakeups; only
337 * works for alarms < 24 hours from now
338 *
339 * set wake.enabled to 1 and determine from value of the year-1
340 * means disabled
341 */
342 wake.enabled = 1;
343 if (ioctl(fd, RTC_ALM_READ, &wake.time) < 0) {
07b336c9 344 warn(_("read rtc alarm failed"));
fcf67294
MO
345 return -1;
346 }
347 }
348
349 if (wake.enabled != 1 || wake.time.tm_year == -1) {
350 printf(_("alarm: off\n"));
351 return 0;
352 }
353
354 rtc = wake.time;
355
356 memset(&tm, 0, sizeof tm);
357 tm.tm_sec = rtc.tm_sec;
358 tm.tm_min = rtc.tm_min;
359 tm.tm_hour = rtc.tm_hour;
360 tm.tm_mday = rtc.tm_mday;
361 tm.tm_mon = rtc.tm_mon;
362 tm.tm_year = rtc.tm_year;
363 tm.tm_isdst = -1; /* assume the system knows better than the RTC */
364
365 alarm = mktime(&tm);
366 if (alarm == (time_t)-1) {
07b336c9 367 warn(_("convert time failed"));
fcf67294
MO
368 return -1;
369 }
370
371 /* 0 if both UTC, or expresses diff if RTC in local time */
372 alarm += sys_time - rtc_time;
373
07b336c9 374 printf(_("alarm: on %s"), ctime(&alarm));
fcf67294
MO
375 return 0;
376}
377
76700389
BW
378int main(int argc, char **argv)
379{
380 char *devname = DEFAULT_DEVICE;
381 unsigned seconds = 0;
382 char *suspend = DEFAULT_MODE;
383
77f5744c 384 int rc = EXIT_SUCCESS;
76700389
BW
385 int t;
386 int fd;
387 time_t alarm = 0;
388
389 setlocale(LC_ALL, "");
390 bindtextdomain(PACKAGE, LOCALEDIR);
391 textdomain(PACKAGE);
392
569f3ca2 393 while ((t = getopt_long(argc, argv, "ahd:lm:ns:t:uVv",
76700389
BW
394 long_options, NULL)) != EOF) {
395 switch (t) {
396 case 'a':
397 /* CM_AUTO is default */
398 break;
399
400 case 'd':
c1196d3a 401 devname = optarg;
76700389
BW
402 break;
403
404 case 'l':
405 clock_mode = CM_LOCAL;
406 break;
407
408 /* what system power mode to use? for now handle only
409 * standardized mode names; eventually when systems
410 * define their own state names, parse
411 * /sys/power/state.
412 *
413 * "on" is used just to test the RTC alarm mechanism,
414 * bypassing all the wakeup-from-sleep infrastructure.
415 */
416 case 'm':
417 if (strcmp(optarg, "standby") == 0
418 || strcmp(optarg, "mem") == 0
419 || strcmp(optarg, "disk") == 0
420 || strcmp(optarg, "on") == 0
e4b0fc36 421 || strcmp(optarg, "no") == 0
77f5744c 422 || strcmp(optarg, "off") == 0
c15dd93b 423 || strcmp(optarg, "disable") == 0
fcf67294 424 || strcmp(optarg, "show") == 0
76700389 425 ) {
c1196d3a 426 suspend = optarg;
76700389
BW
427 break;
428 }
07b336c9
KZ
429
430 errx(EXIT_FAILURE, _("unrecognized suspend state '%s'"),
431 optarg);
432 break;
76700389 433
569f3ca2
KZ
434 case 'n':
435 dryrun = 1;
436 break;
437
76700389
BW
438 /* alarm time, seconds-to-sleep (relative) */
439 case 's':
07b336c9
KZ
440 seconds = strtol_or_err(optarg,
441 _("failed to parse seconds value"));
76700389
BW
442 break;
443
444 /* alarm time, time_t (absolute, seconds since
445 * 1/1 1970 UTC)
446 */
447 case 't':
07b336c9
KZ
448 alarm = strtol_or_err(optarg,
449 _("failed to parse time_t value"));
76700389
BW
450 break;
451
452 case 'u':
453 clock_mode = CM_UTC;
454 break;
455
456 case 'v':
457 verbose++;
458 break;
459
460 case 'V':
07b336c9 461 printf("%s\n", VERSION_STRING);
76700389
BW
462 exit(EXIT_SUCCESS);
463
464 case 'h':
07b336c9 465 usage(stdout);
76700389 466 default:
07b336c9 467 usage(stderr);
76700389
BW
468 }
469 }
470
471 if (clock_mode == CM_AUTO) {
f8d87ab1 472 if (read_clock_mode() < 0) {
07b336c9
KZ
473 printf(_("%s: assuming RTC uses UTC ...\n"),
474 program_invocation_short_name);
76700389
BW
475 clock_mode = CM_UTC;
476 }
76700389 477 }
2148b051
DB
478 if (verbose)
479 printf(clock_mode == CM_UTC ? _("Using UTC time.\n") :
480 _("Using local time.\n"));
76700389 481
fcf67294
MO
482 if (!alarm && !seconds && strcmp(suspend,"disable") &&
483 strcmp(suspend,"show")) {
484
07b336c9
KZ
485 warnx(_("must provide wake time (see -t and -s options)"));
486 usage(stderr);
76700389
BW
487 }
488
489 /* when devname doesn't start with /dev, append it */
490 if (strncmp(devname, "/dev/", strlen("/dev/")) != 0) {
491 char *new_devname;
492
2ab428f6 493 new_devname = xmalloc(strlen(devname) + strlen("/dev/") + 1);
76700389
BW
494
495 strcpy(new_devname, "/dev/");
496 strcat(new_devname, devname);
497 free(devname);
498 devname = new_devname;
499 }
500
e4b0fc36 501 if (strcmp(suspend, "on") != 0 && strcmp(suspend, "no") != 0
07b336c9
KZ
502 && !is_wakeup_enabled(devname))
503 errx(EXIT_FAILURE, _("%s not enabled for wakeup events"), devname);
76700389
BW
504
505 /* this RTC must exist and (if we'll sleep) be wakeup-enabled */
77f5744c
KZ
506#ifdef O_CLOEXEC
507 fd = open(devname, O_RDONLY | O_CLOEXEC);
508#else
76700389 509 fd = open(devname, O_RDONLY);
77f5744c 510#endif
07b336c9
KZ
511 if (fd < 0)
512 err(EXIT_FAILURE, _("open failed: %s"), devname);
76700389
BW
513
514 /* relative or absolute alarm time, normalized to time_t */
f8d87ab1 515 if (get_basetimes(fd) < 0)
76700389
BW
516 exit(EXIT_FAILURE);
517 if (verbose)
518 printf(_("alarm %ld, sys_time %ld, rtc_time %ld, seconds %u\n"),
519 alarm, sys_time, rtc_time, seconds);
77f5744c 520
fcf67294 521 if (strcmp(suspend, "show") && strcmp(suspend, "disable")) {
e6d1dc94
LR
522 if (strcmp(suspend, "no") && strcmp(suspend, "on") &&
523 strcmp(suspend, "off") && is_suspend_available(suspend) <= 0) {
524 errx(EXIT_FAILURE, _("suspend to \"%s\" unavailable"), suspend);
525 }
526
fcf67294
MO
527 /* care about alarm setup only if the show|disable
528 * modes are not set
529 */
530 if (alarm) {
07b336c9
KZ
531 if (alarm < sys_time)
532 errx(EXIT_FAILURE, _("time doesn't go backward to %s"),
533 ctime(&alarm));
fcf67294
MO
534 alarm += sys_time - rtc_time;
535 } else
536 alarm = rtc_time + seconds + 1;
537
538 if (setup_alarm(fd, &alarm) < 0)
539 exit(EXIT_FAILURE);
76700389 540
07b336c9
KZ
541 if (strcmp(suspend, "no") == 0 || strcmp(suspend, "on") == 0)
542 printf(_("%s: wakeup using %s at %s"),
543 program_invocation_short_name, devname,
544 ctime(&alarm));
545 else
546 printf(_("%s: wakeup from \"%s\" using %s at %s"),
547 program_invocation_short_name, suspend, devname,
fcf67294
MO
548 ctime(&alarm));
549 fflush(stdout);
550 usleep(10 * 1000);
551 }
76700389 552
ecd55f96
KZ
553 if (strcmp(suspend, "no") == 0) {
554 if (verbose)
555 printf(_("suspend mode: no; leaving\n"));
07b336c9 556 dryrun = 1; /* to skip disabling alarm at the end */
ecd55f96 557
77f5744c
KZ
558 } else if (strcmp(suspend, "off") == 0) {
559 char *arg[4];
560 int i = 0;
561
ecd55f96
KZ
562 if (verbose)
563 printf(_("suspend mode: off; executing %s\n"),
564 _PATH_SHUTDOWN);
77f5744c
KZ
565 arg[i++] = _PATH_SHUTDOWN;
566 arg[i++] = "-P";
567 arg[i++] = "now";
568 arg[i] = NULL;
569
569f3ca2
KZ
570 if (!dryrun) {
571 execv(arg[0], arg);
77f5744c 572
07b336c9 573 warn(_("unable to execute %s"), _PATH_SHUTDOWN);
569f3ca2
KZ
574 rc = EXIT_FAILURE;
575 }
ecd55f96
KZ
576
577 } else if (strcmp(suspend, "on") == 0) {
76700389
BW
578 unsigned long data;
579
ecd55f96
KZ
580 if (verbose)
581 printf(_("suspend mode: on; reading rtc\n"));
582
569f3ca2
KZ
583 if (!dryrun) {
584 do {
585 t = read(fd, &data, sizeof data);
586 if (t < 0) {
07b336c9 587 warn(_("rtc read failed"));
569f3ca2
KZ
588 break;
589 }
590 if (verbose)
591 printf("... %s: %03lx\n", devname, data);
592 } while (!(data & RTC_AF));
593 }
ecd55f96 594
c15dd93b
MO
595 } else if (strcmp(suspend, "disable") == 0) {
596 /* just break, alarm gets disabled in the end */
597 if (verbose)
598 printf(_("suspend mode: disable; disabling alarm\n"));
fcf67294
MO
599
600 } else if(strcmp(suspend,"show") == 0) {
601 if (verbose)
602 printf(_("suspend mode: show; printing alarm info\n"));
603 if (print_alarm(fd))
604 rc = EXIT_FAILURE;
605 dryrun = 1; /* don't really disable alarm in the end, just show */
606
ecd55f96
KZ
607 } else {
608 if (verbose)
609 printf(_("suspend mode: %s; suspending system\n"), suspend);
610 sync();
611 suspend_system(suspend);
76700389
BW
612 }
613
569f3ca2 614 if (!dryrun && ioctl(fd, RTC_AIE_OFF, 0) < 0)
07b336c9 615 warn(_("disable rtc alarm interrupt failed"));
76700389
BW
616
617 close(fd);
77f5744c 618 return rc;
76700389 619}