]> git.ipfire.org Git - thirdparty/util-linux.git/blame - sys-utils/rtcwake.c
Merge branch 'maybe-for-v2.32' of https://github.com/rudimeier/util-linux
[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
5b4185e1 22#include <errno.h>
76700389 23#include <fcntl.h>
5b4185e1 24#include <getopt.h>
5b4185e1 25#include <linux/rtc.h>
c8ff2e55 26#include <poll.h>
5b4185e1 27#include <stdio.h>
76700389
BW
28#include <stdlib.h>
29#include <string.h>
76700389
BW
30#include <sys/ioctl.h>
31#include <sys/time.h>
32#include <sys/types.h>
c8ff2e55 33#include <termios.h>
5b4185e1
SK
34#include <time.h>
35#include <unistd.h>
76700389 36
5b4185e1
SK
37#include "c.h"
38#include "closestream.h"
984a6096 39#include "env.h"
76700389 40#include "nls.h"
254e9e58 41#include "optutils.h"
77f5744c 42#include "pathnames.h"
07b336c9 43#include "strutils.h"
f87b73ab 44#include "strv.h"
a10ce9a3 45#include "timeutils.h"
5b4185e1 46#include "xalloc.h"
76700389 47
356b2989 48#ifndef RTC_AF
f0781342 49# define RTC_AF 0x20 /* Alarm interrupt */
356b2989 50#endif
76700389 51
f0781342 52#define ADJTIME_ZONE_BUFSIZ 8
067fde32 53#define SYS_WAKEUP_PATH_TEMPLATE "/sys/class/rtc/%s/device/power/wakeup"
f0781342
KZ
54#define SYS_POWER_STATE_PATH "/sys/power/state"
55#define DEFAULT_RTC_DEVICE "/dev/rtc0"
6e1ec14f
SK
56
57enum rtc_modes { /* manual page --mode option explains these. */
f87b73ab 58 OFF_MODE = 0,
6e1ec14f
SK
59 NO_MODE,
60 ON_MODE,
61 DISABLE_MODE,
f87b73ab
SK
62 SHOW_MODE,
63
64 SYSFS_MODE /* keep it last */
65
6e1ec14f
SK
66};
67
f87b73ab 68static const char *rtcwake_mode_string[] = {
6e1ec14f
SK
69 [OFF_MODE] = "off",
70 [NO_MODE] = "no",
71 [ON_MODE] = "on",
72 [DISABLE_MODE] = "disable",
73 [SHOW_MODE] = "show"
74};
76700389 75
71d95e92 76enum clock_modes {
76700389
BW
77 CM_AUTO,
78 CM_UTC,
79 CM_LOCAL
80};
81
71d95e92 82struct rtcwake_control {
f87b73ab
SK
83 char *mode_str; /* name of the requested mode */
84 char **possible_modes; /* modes listed in /sys/power/state */
71d95e92
SK
85 char *adjfile; /* adjtime file path */
86 enum clock_modes clock_mode; /* hwclock timezone */
87 time_t sys_time; /* system time */
88 time_t rtc_time; /* hardware time */
f0781342
KZ
89 unsigned int verbose:1, /* verbose messaging */
90 dryrun:1; /* do not set alarm, suspend system, etc */
71d95e92 91};
76700389 92
9325dbfd 93static void __attribute__((__noreturn__)) usage(void)
76700389 94{
9325dbfd 95 FILE *out = stdout;
0e00261d 96 fputs(USAGE_HEADER, out);
704c7705
KZ
97 fprintf(out,
98 _(" %s [options]\n"), program_invocation_short_name);
99
451dbcfa
BS
100 fputs(USAGE_SEPARATOR, out);
101 fputs(_("Enter a system sleep state until a specified wakeup time.\n"), out);
102
0e00261d 103 fputs(USAGE_OPTIONS, out);
49ebda9b 104 fputs(_(" -a, --auto reads the clock mode from adjust file (default)\n"), out);
3a2f3e82
KZ
105 fprintf(out,
106 _(" -A, --adjfile <file> specifies the path to the adjust file\n"
7528fae9 107 " the default is %s\n"), _PATH_ADJTIME);
a10ce9a3 108 fputs(_(" --date <timestamp> date time of timestamp to wake\n"), out);
09e092ad
KZ
109 fputs(_(" -d, --device <device> select rtc device (rtc0|rtc1|...)\n"), out);
110 fputs(_(" -n, --dry-run does everything, but suspend\n"), out);
111 fputs(_(" -l, --local RTC uses local timezone\n"), out);
43a44bfc 112 fputs(_(" --list-modes list available modes\n"), out);
09e092ad
KZ
113 fputs(_(" -m, --mode <mode> standby|mem|... sleep mode\n"), out);
114 fputs(_(" -s, --seconds <seconds> seconds to sleep\n"), out);
115 fputs(_(" -t, --time <time_t> time to wake\n"), out);
116 fputs(_(" -u, --utc RTC uses UTC\n"), out);
117 fputs(_(" -v, --verbose verbose messages\n"), out);
704c7705 118
9325dbfd 119 fputs(USAGE_SEPARATOR, out);
f45f3ec3 120 printf(USAGE_HELP_OPTIONS(26));
0e00261d 121 printf(USAGE_MAN_TAIL("rtcwake(8)"));
9325dbfd 122 exit(EXIT_SUCCESS);
76700389
BW
123}
124
f8d87ab1 125static int is_wakeup_enabled(const char *devname)
76700389
BW
126{
127 char buf[128], *s;
128 FILE *f;
067fde32 129 size_t skip = 0;
76700389 130
067fde32
SK
131 if (startswith(devname, "/dev/"))
132 skip = 5;
133 snprintf(buf, sizeof buf, SYS_WAKEUP_PATH_TEMPLATE, devname + skip);
76700389
BW
134 f = fopen(buf, "r");
135 if (!f) {
289dcc90 136 warn(_("cannot open %s"), buf);
76700389
BW
137 return 0;
138 }
f0781342 139
76700389
BW
140 s = fgets(buf, sizeof buf, f);
141 fclose(f);
142 if (!s)
143 return 0;
76700389
BW
144 s = strchr(buf, '\n');
145 if (!s)
146 return 0;
147 *s = 0;
76700389
BW
148 /* wakeup events could be disabled or not supported */
149 return strcmp(buf, "enabled") == 0;
150}
151
71d95e92 152static int get_basetimes(struct rtcwake_control *ctl, int fd)
76700389 153{
f0781342 154 struct tm tm = { 0 };
76700389
BW
155 struct rtc_time rtc;
156
5b4185e1 157 /* This process works in RTC time, except when working
76700389
BW
158 * with the system clock (which always uses UTC).
159 */
71d95e92 160 if (ctl->clock_mode == CM_UTC)
984a6096 161 xsetenv("TZ", "UTC", 1);
76700389 162 tzset();
5b4185e1 163 /* Read rtc and system clocks "at the same time", or as
76700389
BW
164 * precisely (+/- a second) as we can read them.
165 */
166 if (ioctl(fd, RTC_RD_TIME, &rtc) < 0) {
07b336c9 167 warn(_("read rtc time failed"));
f8d87ab1 168 return -1;
76700389 169 }
f0781342 170
87918040 171 ctl->sys_time = time(NULL);
71d95e92 172 if (ctl->sys_time == (time_t)-1) {
07b336c9 173 warn(_("read system time failed"));
f8d87ab1 174 return -1;
76700389 175 }
5b4185e1 176 /* Convert rtc_time to normal arithmetic-friendly form,
76700389
BW
177 * updating tm.tm_wday as used by asctime().
178 */
76700389
BW
179 tm.tm_sec = rtc.tm_sec;
180 tm.tm_min = rtc.tm_min;
181 tm.tm_hour = rtc.tm_hour;
182 tm.tm_mday = rtc.tm_mday;
183 tm.tm_mon = rtc.tm_mon;
184 tm.tm_year = rtc.tm_year;
1da17ec6 185 tm.tm_isdst = -1; /* assume the system knows better than the RTC */
f0781342 186
71d95e92 187 ctl->rtc_time = mktime(&tm);
71d95e92 188 if (ctl->rtc_time == (time_t)-1) {
07b336c9 189 warn(_("convert rtc time failed"));
f8d87ab1 190 return -1;
76700389 191 }
f0781342 192
71d95e92 193 if (ctl->verbose) {
2148b051
DB
194 /* Unless the system uses UTC, either delta or tzone
195 * reflects a seconds offset from UTC. The value can
5b4185e1 196 * help sort out problems like bugs in your C library. */
71d95e92 197 printf("\tdelta = %ld\n", ctl->sys_time - ctl->rtc_time);
2148b051 198 printf("\ttzone = %ld\n", timezone);
2148b051 199 printf("\ttzname = %s\n", tzname[daylight]);
71d95e92 200 gmtime_r(&ctl->rtc_time, &tm);
2148b051 201 printf("\tsystime = %ld, (UTC) %s",
71d95e92 202 (long) ctl->sys_time, asctime(gmtime(&ctl->sys_time)));
2148b051 203 printf("\trtctime = %ld, (UTC) %s",
71d95e92 204 (long) ctl->rtc_time, asctime(&tm));
76700389 205 }
f8d87ab1 206 return 0;
76700389
BW
207}
208
71d95e92 209static int setup_alarm(struct rtcwake_control *ctl, int fd, time_t *wakeup)
76700389
BW
210{
211 struct tm *tm;
76037ac7 212 struct rtc_wkalrm wake = { 0 };
76700389 213
5b4185e1
SK
214 /* The wakeup time is in POSIX time (more or less UTC). Ideally
215 * RTCs use that same time; but PCs can't do that if they need to
216 * boot MS-Windows. Messy...
1b7c164c 217 *
5b4185e1
SK
218 * When clock_mode == CM_UTC this process's timezone is UTC, so
219 * we'll pass a UTC date to the RTC.
1b7c164c 220 *
5b4185e1
SK
221 * Else clock_mode == CM_LOCAL so the time given to the RTC will
222 * instead use the local time zone. */
1b7c164c 223 tm = localtime(wakeup);
76700389
BW
224 wake.time.tm_sec = tm->tm_sec;
225 wake.time.tm_min = tm->tm_min;
226 wake.time.tm_hour = tm->tm_hour;
227 wake.time.tm_mday = tm->tm_mday;
228 wake.time.tm_mon = tm->tm_mon;
229 wake.time.tm_year = tm->tm_year;
5b4185e1 230 /* wday, yday, and isdst fields are unused */
2148b051
DB
231 wake.time.tm_wday = -1;
232 wake.time.tm_yday = -1;
233 wake.time.tm_isdst = -1;
fc181184 234 wake.enabled = 1;
f0781342 235
71d95e92 236 if (!ctl->dryrun && ioctl(fd, RTC_WKALM_SET, &wake) < 0) {
5fde2f0d
SK
237 warn(_("set rtc wake alarm failed"));
238 return -1;
76700389 239 }
f8d87ab1 240 return 0;
76700389
BW
241}
242
f87b73ab 243static char **get_sys_power_states(struct rtcwake_control *ctl)
e6d1dc94 244{
6b497c0e
KZ
245 int fd = -1;
246
f87b73ab 247 if (!ctl->possible_modes) {
f87b73ab
SK
248 char buf[256] = { 0 };
249
6b497c0e
KZ
250 fd = open(SYS_POWER_STATE_PATH, O_RDONLY);
251 if (fd < 0)
252 goto nothing;
a325f3c2 253 if (read(fd, &buf, sizeof(buf) - 1) <= 0)
6b497c0e 254 goto nothing;
f87b73ab 255 ctl->possible_modes = strv_split(buf, " \n");
6b497c0e 256 close(fd);
f87b73ab
SK
257 }
258 return ctl->possible_modes;
6b497c0e
KZ
259nothing:
260 if (fd >= 0)
261 close(fd);
262 return NULL;
e6d1dc94
LR
263}
264
c8ff2e55
SK
265static void wait_stdin(struct rtcwake_control *ctl)
266{
267 struct pollfd fd[] = {
268 {.fd = STDIN_FILENO, .events = POLLIN}
269 };
270 int tries = 0;
271
272 while (tries < 8 && poll(fd, 1, 10) == 1) {
273 if (ctl->verbose)
274 warnx(_("discarding stdin"));
275 xusleep(250000);
276 tcflush(STDIN_FILENO, TCIFLUSH);
277 tries++;
278 }
279}
280
f87b73ab 281static void suspend_system(struct rtcwake_control *ctl)
76700389 282{
d3cf5414 283 FILE *f = fopen(SYS_POWER_STATE_PATH, "w");
76700389
BW
284
285 if (!f) {
289dcc90 286 warn(_("cannot open %s"), SYS_POWER_STATE_PATH);
76700389
BW
287 return;
288 }
f0781342 289
71d95e92 290 if (!ctl->dryrun) {
c8ff2e55
SK
291 if (isatty(STDIN_FILENO))
292 wait_stdin(ctl);
f87b73ab 293 fprintf(f, "%s\n", ctl->mode_str);
569f3ca2
KZ
294 fflush(f);
295 }
76700389 296 /* this executes after wake from suspend */
efb8854f
SK
297 if (close_stream(f))
298 errx(EXIT_FAILURE, _("write error"));
76700389
BW
299}
300
71d95e92 301static int read_clock_mode(struct rtcwake_control *ctl)
76700389
BW
302{
303 FILE *fp;
f0781342 304 char linebuf[ADJTIME_ZONE_BUFSIZ];
76700389 305
71d95e92 306 fp = fopen(ctl->adjfile, "r");
76700389 307 if (!fp)
f8d87ab1 308 return -1;
3e5a5455
SK
309 /* skip two lines */
310 if (skip_fline(fp) || skip_fline(fp)) {
76700389 311 fclose(fp);
f8d87ab1 312 return -1;
76700389 313 }
76700389 314 /* read third line */
3e5a5455 315 if (!fgets(linebuf, sizeof linebuf, fp)) {
76700389 316 fclose(fp);
f8d87ab1 317 return -1;
76700389 318 }
f0781342 319
76700389 320 if (strncmp(linebuf, "UTC", 3) == 0)
71d95e92 321 ctl->clock_mode = CM_UTC;
76700389 322 else if (strncmp(linebuf, "LOCAL", 5) == 0)
71d95e92 323 ctl->clock_mode = CM_LOCAL;
3e5a5455
SK
324 else if (ctl->verbose)
325 warnx(_("unexpected third line in: %s: %s"), ctl->adjfile, linebuf);
f0781342 326
76700389 327 fclose(fp);
f8d87ab1 328 return 0;
76700389
BW
329}
330
71d95e92 331static int print_alarm(struct rtcwake_control *ctl, int fd)
fcf67294
MO
332{
333 struct rtc_wkalrm wake;
499a0c79 334 struct tm tm = { 0 };
fcf67294
MO
335 time_t alarm;
336
fcf67294 337 if (ioctl(fd, RTC_WKALM_RD, &wake) < 0) {
5fde2f0d
SK
338 warn(_("read rtc alarm failed"));
339 return -1;
fcf67294 340 }
f0781342 341
fcf67294
MO
342 if (wake.enabled != 1 || wake.time.tm_year == -1) {
343 printf(_("alarm: off\n"));
344 return 0;
345 }
499a0c79
SK
346 tm.tm_sec = wake.time.tm_sec;
347 tm.tm_min = wake.time.tm_min;
348 tm.tm_hour = wake.time.tm_hour;
349 tm.tm_mday = wake.time.tm_mday;
350 tm.tm_mon = wake.time.tm_mon;
351 tm.tm_year = wake.time.tm_year;
fcf67294 352 tm.tm_isdst = -1; /* assume the system knows better than the RTC */
f0781342 353
fcf67294
MO
354 alarm = mktime(&tm);
355 if (alarm == (time_t)-1) {
07b336c9 356 warn(_("convert time failed"));
fcf67294
MO
357 return -1;
358 }
fcf67294 359 /* 0 if both UTC, or expresses diff if RTC in local time */
71d95e92 360 alarm += ctl->sys_time - ctl->rtc_time;
07b336c9 361 printf(_("alarm: on %s"), ctime(&alarm));
f0781342 362
fcf67294
MO
363 return 0;
364}
365
eb2306e6 366static int get_rtc_mode(struct rtcwake_control *ctl, const char *s)
6e1ec14f 367{
f87b73ab
SK
368 size_t i;
369 char **modes = get_sys_power_states(ctl), **m;
6e1ec14f 370
f87b73ab 371 STRV_FOREACH(m, modes) {
eb2306e6 372 if (strcmp(s, *m) == 0)
f87b73ab
SK
373 return SYSFS_MODE;
374 }
375
bac778c0 376 for (i = 0; i < ARRAY_SIZE(rtcwake_mode_string); i++)
eb2306e6 377 if (!strcmp(s, rtcwake_mode_string[i]))
6e1ec14f 378 return i;
f87b73ab 379
6e1ec14f
SK
380 return -EINVAL;
381}
382
067fde32
SK
383static int open_dev_rtc(const char *devname)
384{
385 int fd;
386 char *devpath = NULL;
387
388 if (startswith(devname, "/dev"))
389 devpath = xstrdup(devname);
390 else
391 xasprintf(&devpath, "/dev/%s", devname);
392 fd = open(devpath, O_RDONLY | O_CLOEXEC);
393 if (fd < 0)
394 err(EXIT_FAILURE, _("%s: unable to find device"), devpath);
395 free(devpath);
396 return fd;
397}
398
f87b73ab 399static void list_modes(struct rtcwake_control *ctl)
43a44bfc 400{
f87b73ab
SK
401 size_t i;
402 char **modes = get_sys_power_states(ctl), **m;
403
404 if (!modes)
405 errx(EXIT_FAILURE, _("could not read: %s"), SYS_POWER_STATE_PATH);
43a44bfc 406
f87b73ab
SK
407 STRV_FOREACH(m, modes)
408 printf("%s ", *m);
409
410 for (i = 0; i < ARRAY_SIZE(rtcwake_mode_string); i++)
411 printf("%s ", rtcwake_mode_string[i]);
43a44bfc
SK
412 putchar('\n');
413}
414
76700389
BW
415int main(int argc, char **argv)
416{
71d95e92 417 struct rtcwake_control ctl = {
f87b73ab 418 .mode_str = "suspend", /* default mode */
71d95e92 419 .adjfile = _PATH_ADJTIME,
f0781342 420 .clock_mode = CM_AUTO
71d95e92 421 };
f0781342 422 char *devname = DEFAULT_RTC_DEVICE;
5b4185e1 423 unsigned seconds = 0;
f87b73ab 424 int suspend = SYSFS_MODE;
5b4185e1
SK
425 int rc = EXIT_SUCCESS;
426 int t;
427 int fd;
428 time_t alarm = 0;
a10ce9a3 429 enum {
43a44bfc
SK
430 OPT_DATE = CHAR_MAX + 1,
431 OPT_LIST
a10ce9a3 432 };
3a2f3e82 433 static const struct option long_options[] = {
87918040
SK
434 { "adjfile", required_argument, NULL, 'A' },
435 { "auto", no_argument, NULL, 'a' },
436 { "dry-run", no_argument, NULL, 'n' },
437 { "local", no_argument, NULL, 'l' },
438 { "utc", no_argument, NULL, 'u' },
439 { "verbose", no_argument, NULL, 'v' },
440 { "version", no_argument, NULL, 'V' },
441 { "help", no_argument, NULL, 'h' },
442 { "mode", required_argument, NULL, 'm' },
443 { "device", required_argument, NULL, 'd' },
444 { "seconds", required_argument, NULL, 's' },
445 { "time", required_argument, NULL, 't' },
446 { "date", required_argument, NULL, OPT_DATE },
447 { "list-modes", no_argument, NULL, OPT_LIST },
448 { NULL, 0, NULL, 0 }
3a2f3e82 449 };
254e9e58
SK
450 static const ul_excl_t excl[] = {
451 { 'a', 'l', 'u' },
452 { 's', 't', OPT_DATE },
453 };
454 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
3a2f3e82 455
76700389
BW
456 setlocale(LC_ALL, "");
457 bindtextdomain(PACKAGE, LOCALEDIR);
458 textdomain(PACKAGE);
efb8854f 459 atexit(close_stdout);
f87b73ab 460
3a2f3e82 461 while ((t = getopt_long(argc, argv, "A:ahd:lm:ns:t:uVv",
76700389 462 long_options, NULL)) != EOF) {
254e9e58 463 err_exclusive_options(t, long_options, excl, excl_st);
76700389 464 switch (t) {
3a2f3e82
KZ
465 case 'A':
466 /* for better compatibility with hwclock */
71d95e92 467 ctl.adjfile = optarg;
3a2f3e82 468 break;
76700389 469 case 'a':
6e1ec14f 470 ctl.clock_mode = CM_AUTO;
76700389 471 break;
76700389 472 case 'd':
c1196d3a 473 devname = optarg;
76700389 474 break;
76700389 475 case 'l':
71d95e92 476 ctl.clock_mode = CM_LOCAL;
76700389
BW
477 break;
478
43a44bfc 479 case OPT_LIST:
f87b73ab 480 list_modes(&ctl);
43a44bfc
SK
481 return EXIT_SUCCESS;
482
76700389 483 case 'm':
f87b73ab 484 if ((suspend = get_rtc_mode(&ctl, optarg)) < 0)
6e1ec14f 485 errx(EXIT_FAILURE, _("unrecognized suspend state '%s'"), optarg);
f87b73ab 486 ctl.mode_str = optarg;
07b336c9 487 break;
569f3ca2 488 case 'n':
71d95e92 489 ctl.dryrun = 1;
569f3ca2 490 break;
76700389 491 case 's':
a10ce9a3 492 /* alarm time, seconds-to-sleep (relative) */
20a39982 493 seconds = strtou32_or_err(optarg, _("invalid seconds argument"));
76700389 494 break;
76700389 495 case 't':
9e930041 496 /* alarm time, time_t (absolute, seconds since epoch) */
20a39982 497 alarm = strtou32_or_err(optarg, _("invalid time argument"));
76700389 498 break;
a10ce9a3 499 case OPT_DATE:
5b4185e1 500 { /* alarm time, see timestamp format from manual */
a10ce9a3
SK
501 usec_t p;
502 if (parse_timestamp(optarg, &p) < 0)
503 errx(EXIT_FAILURE, _("invalid time value \"%s\""), optarg);
504 alarm = (time_t) (p / 1000000);
505 break;
506 }
76700389 507 case 'u':
71d95e92 508 ctl.clock_mode = CM_UTC;
76700389 509 break;
76700389 510 case 'v':
71d95e92 511 ctl.verbose = 1;
76700389 512 break;
76700389 513 case 'V':
f6277500 514 printf(UTIL_LINUX_VERSION);
76700389 515 exit(EXIT_SUCCESS);
76700389 516 case 'h':
9325dbfd 517 usage();
76700389 518 default:
9325dbfd 519 errtryhelp(EXIT_FAILURE);
76700389
BW
520 }
521 }
f0781342 522
74ce680a
SK
523 if (ctl.clock_mode == CM_AUTO && read_clock_mode(&ctl) < 0) {
524 printf(_("%s: assuming RTC uses UTC ...\n"), program_invocation_short_name);
525 ctl.clock_mode = CM_UTC;
76700389 526 }
f0781342 527
71d95e92
SK
528 if (ctl.verbose)
529 printf("%s", ctl.clock_mode == CM_UTC ? _("Using UTC time.\n") :
2148b051 530 _("Using local time.\n"));
2626f123
KZ
531
532 if (!alarm && !seconds && suspend != DISABLE_MODE && suspend != SHOW_MODE)
533 errx(EXIT_FAILURE, _("must provide wake time (see --seconds, --time and --date options)"));
534
067fde32
SK
535 /* device must exist and (if we'll sleep) be wakeup-enabled */
536 fd = open_dev_rtc(devname);
f0781342 537
6e1ec14f 538 if (suspend != ON_MODE && suspend != NO_MODE && !is_wakeup_enabled(devname))
07b336c9 539 errx(EXIT_FAILURE, _("%s not enabled for wakeup events"), devname);
f87b73ab 540
76700389 541 /* relative or absolute alarm time, normalized to time_t */
71d95e92 542 if (get_basetimes(&ctl, fd) < 0)
76700389 543 exit(EXIT_FAILURE);
f87b73ab 544
71d95e92 545 if (ctl.verbose)
76700389 546 printf(_("alarm %ld, sys_time %ld, rtc_time %ld, seconds %u\n"),
71d95e92 547 alarm, ctl.sys_time, ctl.rtc_time, seconds);
f87b73ab 548
6e1ec14f 549 if (suspend != DISABLE_MODE && suspend != SHOW_MODE) {
f0781342 550 /* perform alarm setup when the show or disable modes are not set */
fcf67294 551 if (alarm) {
71d95e92 552 if (alarm < ctl.sys_time)
07b336c9
KZ
553 errx(EXIT_FAILURE, _("time doesn't go backward to %s"),
554 ctime(&alarm));
e72027da 555 alarm -= ctl.sys_time - ctl.rtc_time;
fcf67294 556 } else
71d95e92 557 alarm = ctl.rtc_time + seconds + 1;
f0781342 558
71d95e92 559 if (setup_alarm(&ctl, fd, &alarm) < 0)
fcf67294 560 exit(EXIT_FAILURE);
f0781342 561
6e1ec14f 562 if (suspend == NO_MODE || suspend == ON_MODE)
07b336c9
KZ
563 printf(_("%s: wakeup using %s at %s"),
564 program_invocation_short_name, devname,
565 ctime(&alarm));
566 else
567 printf(_("%s: wakeup from \"%s\" using %s at %s"),
f87b73ab 568 program_invocation_short_name, ctl.mode_str, devname,
fcf67294
MO
569 ctime(&alarm));
570 fflush(stdout);
a5bd7939 571 xusleep(10 * 1000);
fcf67294 572 }
f87b73ab 573
64a80678
SK
574 switch (suspend) {
575 case NO_MODE:
71d95e92 576 if (ctl.verbose)
ecd55f96 577 printf(_("suspend mode: no; leaving\n"));
71d95e92 578 ctl.dryrun = 1; /* to skip disabling alarm at the end */
64a80678 579 break;
64a80678
SK
580 case OFF_MODE:
581 {
caf60f22 582 char *arg[5];
77f5744c
KZ
583 int i = 0;
584
71d95e92 585 if (ctl.verbose)
ecd55f96
KZ
586 printf(_("suspend mode: off; executing %s\n"),
587 _PATH_SHUTDOWN);
77f5744c 588 arg[i++] = _PATH_SHUTDOWN;
caf60f22 589 arg[i++] = "-h";
77f5744c
KZ
590 arg[i++] = "-P";
591 arg[i++] = "now";
592 arg[i] = NULL;
71d95e92 593 if (!ctl.dryrun) {
569f3ca2 594 execv(arg[0], arg);
07ff972e 595 warn(_("failed to execute %s"), _PATH_SHUTDOWN);
569f3ca2
KZ
596 rc = EXIT_FAILURE;
597 }
64a80678
SK
598 break;
599 }
600 case ON_MODE:
601 {
76700389
BW
602 unsigned long data;
603
71d95e92 604 if (ctl.verbose)
ecd55f96 605 printf(_("suspend mode: on; reading rtc\n"));
71d95e92 606 if (!ctl.dryrun) {
569f3ca2
KZ
607 do {
608 t = read(fd, &data, sizeof data);
609 if (t < 0) {
07b336c9 610 warn(_("rtc read failed"));
569f3ca2
KZ
611 break;
612 }
71d95e92 613 if (ctl.verbose)
569f3ca2
KZ
614 printf("... %s: %03lx\n", devname, data);
615 } while (!(data & RTC_AF));
616 }
64a80678
SK
617 break;
618 }
619 case DISABLE_MODE:
c15dd93b 620 /* just break, alarm gets disabled in the end */
71d95e92 621 if (ctl.verbose)
c15dd93b 622 printf(_("suspend mode: disable; disabling alarm\n"));
64a80678 623 break;
64a80678 624 case SHOW_MODE:
71d95e92 625 if (ctl.verbose)
fcf67294 626 printf(_("suspend mode: show; printing alarm info\n"));
71d95e92 627 if (print_alarm(&ctl, fd))
fcf67294 628 rc = EXIT_FAILURE;
71d95e92 629 ctl.dryrun = 1; /* don't really disable alarm in the end, just show */
64a80678 630 break;
64a80678 631 default:
71d95e92 632 if (ctl.verbose)
f87b73ab 633 printf(_("suspend mode: %s; suspending system\n"), ctl.mode_str);
ecd55f96 634 sync();
f87b73ab 635 suspend_system(&ctl);
76700389
BW
636 }
637
71d95e92 638 if (!ctl.dryrun) {
829eab67
G
639 struct rtc_wkalrm wake;
640
641 if (ioctl(fd, RTC_WKALM_RD, &wake) < 0) {
5fde2f0d
SK
642 warn(_("read rtc alarm failed"));
643 rc = EXIT_FAILURE;
829eab67
G
644 } else {
645 wake.enabled = 0;
646 if (ioctl(fd, RTC_WKALM_SET, &wake) < 0) {
647 warn(_("disable rtc alarm interrupt failed"));
648 rc = EXIT_FAILURE;
649 }
650 }
651 }
f0781342 652
76700389 653 close(fd);
77f5744c 654 return rc;
76700389 655}