]>
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" | |
2ab428f6 | 39 | #include "xalloc.h" |
77f5744c | 40 | #include "pathnames.h" |
07b336c9 | 41 | #include "strutils.h" |
eb76ca98 | 42 | #include "c.h" |
efb8854f | 43 | #include "closestream.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 | 52 | #define RTC_PATH "/sys/class/rtc/%s/device/power/wakeup" |
d3cf5414 | 53 | #define SYS_POWER_STATE_PATH "/sys/power/state" |
76700389 | 54 | #define DEFAULT_DEVICE "/dev/rtc0" |
47bf8ef7 | 55 | #define DEFAULT_MODE "standby" |
76700389 BW |
56 | |
57 | enum ClockMode { | |
58 | CM_AUTO, | |
59 | CM_UTC, | |
60 | CM_LOCAL | |
61 | }; | |
62 | ||
3a2f3e82 | 63 | static char *adjfile = _PATH_ADJPATH; |
76700389 | 64 | static unsigned verbose; |
569f3ca2 | 65 | static unsigned dryrun; |
76700389 BW |
66 | enum ClockMode clock_mode = CM_AUTO; |
67 | ||
07b336c9 | 68 | static void __attribute__((__noreturn__)) usage(FILE *out) |
76700389 | 69 | { |
0e00261d | 70 | fputs(USAGE_HEADER, out); |
704c7705 KZ |
71 | fprintf(out, |
72 | _(" %s [options]\n"), program_invocation_short_name); | |
73 | ||
0e00261d | 74 | fputs(USAGE_OPTIONS, out); |
3a2f3e82 KZ |
75 | fprintf(out, |
76 | _(" -A, --adjfile <file> specifies the path to the adjust file\n" | |
77 | " the default is %s\n"), _PATH_ADJPATH); | |
704c7705 KZ |
78 | fputs(_(" -d, --device <device> select rtc device (rtc0|rtc1|...)\n" |
79 | " -n, --dry-run does everything, but suspend\n" | |
80 | " -l, --local RTC uses local timezone\n" | |
81 | " -m, --mode <mode> standby|mem|... sleep mode\n" | |
82 | " -s, --seconds <seconds> seconds to sleep\n" | |
83 | " -t, --time <time_t> time to wake\n" | |
84 | " -u, --utc RTC uses UTC\n" | |
0e00261d | 85 | " -v, --verbose verbose messages\n"), out); |
704c7705 | 86 | |
0e00261d SK |
87 | printf(USAGE_SEPARATOR); |
88 | printf(USAGE_HELP); | |
89 | printf(USAGE_VERSION); | |
90 | ||
91 | printf(USAGE_MAN_TAIL("rtcwake(8)")); | |
07b336c9 KZ |
92 | |
93 | exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS); | |
76700389 BW |
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) { | |
289dcc90 | 105 | warn(_("cannot open %s"), buf); |
76700389 BW |
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) { | |
07b336c9 | 142 | warn(_("read rtc time failed")); |
f8d87ab1 | 143 | return -1; |
76700389 BW |
144 | } |
145 | sys_time = time(0); | |
146 | if (sys_time == (time_t)-1) { | |
07b336c9 | 147 | warn(_("read system time failed")); |
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) { | |
07b336c9 | 165 | warn(_("convert rtc time failed")); |
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 | 216 | wake.enabled = 1; |
569f3ca2 | 217 | |
fc181184 | 218 | /* First try the preferred RTC_WKALM_SET */ |
569f3ca2 | 219 | if (!dryrun && ioctl(fd, RTC_WKALM_SET, &wake) < 0) { |
fc181184 GB |
220 | wake.enabled = 0; |
221 | /* Fall back on the non-preferred way of setting wakeups; only | |
222 | * works for alarms < 24 hours from now */ | |
223 | if ((rtc_time + (24 * 60 * 60)) > *wakeup) { | |
224 | if (ioctl(fd, RTC_ALM_SET, &wake.time) < 0) { | |
07b336c9 | 225 | warn(_("set rtc alarm failed")); |
fc181184 GB |
226 | return -1; |
227 | } | |
228 | if (ioctl(fd, RTC_AIE_ON, 0) < 0) { | |
07b336c9 | 229 | warn(_("enable rtc alarm failed")); |
fc181184 GB |
230 | return -1; |
231 | } | |
232 | } else { | |
07b336c9 | 233 | warn(_("set rtc wake alarm failed")); |
fc181184 | 234 | return -1; |
76700389 BW |
235 | } |
236 | } | |
237 | ||
f8d87ab1 | 238 | return 0; |
76700389 BW |
239 | } |
240 | ||
e6d1dc94 LR |
241 | static int is_suspend_available(const char *suspend) |
242 | { | |
243 | int rc; | |
244 | char buf[32]; | |
245 | FILE *f = fopen(SYS_POWER_STATE_PATH, "r"); | |
246 | ||
247 | if (!f) | |
248 | return -1; | |
249 | ||
250 | if (fgets(buf, sizeof buf, f) == NULL) | |
251 | rc = -1; | |
252 | else | |
253 | rc = strstr(buf, suspend) != NULL; | |
254 | ||
255 | fclose(f); | |
256 | return rc; | |
257 | } | |
258 | ||
76700389 BW |
259 | static void suspend_system(const char *suspend) |
260 | { | |
d3cf5414 | 261 | FILE *f = fopen(SYS_POWER_STATE_PATH, "w"); |
76700389 BW |
262 | |
263 | if (!f) { | |
289dcc90 | 264 | warn(_("cannot open %s"), SYS_POWER_STATE_PATH); |
76700389 BW |
265 | return; |
266 | } | |
267 | ||
569f3ca2 KZ |
268 | if (!dryrun) { |
269 | fprintf(f, "%s\n", suspend); | |
270 | fflush(f); | |
271 | } | |
76700389 BW |
272 | |
273 | /* this executes after wake from suspend */ | |
efb8854f SK |
274 | if (close_stream(f)) |
275 | errx(EXIT_FAILURE, _("write error")); | |
76700389 BW |
276 | } |
277 | ||
278 | ||
279 | static int read_clock_mode(void) | |
280 | { | |
281 | FILE *fp; | |
282 | char linebuf[MAX_LINE]; | |
283 | ||
3a2f3e82 | 284 | fp = fopen(adjfile, "r"); |
76700389 | 285 | if (!fp) |
f8d87ab1 | 286 | return -1; |
76700389 BW |
287 | |
288 | /* skip first line */ | |
289 | if (!fgets(linebuf, MAX_LINE, fp)) { | |
290 | fclose(fp); | |
f8d87ab1 | 291 | return -1; |
76700389 BW |
292 | } |
293 | ||
294 | /* skip second line */ | |
295 | if (!fgets(linebuf, MAX_LINE, fp)) { | |
296 | fclose(fp); | |
f8d87ab1 | 297 | return -1; |
76700389 BW |
298 | } |
299 | ||
300 | /* read third line */ | |
301 | if (!fgets(linebuf, MAX_LINE, fp)) { | |
302 | fclose(fp); | |
f8d87ab1 | 303 | return -1; |
76700389 BW |
304 | } |
305 | ||
306 | if (strncmp(linebuf, "UTC", 3) == 0) | |
307 | clock_mode = CM_UTC; | |
308 | else if (strncmp(linebuf, "LOCAL", 5) == 0) | |
309 | clock_mode = CM_LOCAL; | |
310 | ||
311 | fclose(fp); | |
312 | ||
f8d87ab1 | 313 | return 0; |
76700389 BW |
314 | } |
315 | ||
fcf67294 MO |
316 | /** |
317 | * print basic alarm settings | |
318 | */ | |
319 | static int print_alarm(int fd) | |
320 | { | |
321 | struct rtc_wkalrm wake; | |
322 | struct rtc_time rtc; | |
323 | struct tm tm; | |
324 | time_t alarm; | |
325 | ||
326 | /* First try the preferred RTC_WKALM_RD */ | |
327 | if (ioctl(fd, RTC_WKALM_RD, &wake) < 0) { | |
328 | /* Fall back on the non-preferred way of reading wakeups; only | |
329 | * works for alarms < 24 hours from now | |
330 | * | |
331 | * set wake.enabled to 1 and determine from value of the year-1 | |
332 | * means disabled | |
333 | */ | |
334 | wake.enabled = 1; | |
335 | if (ioctl(fd, RTC_ALM_READ, &wake.time) < 0) { | |
07b336c9 | 336 | warn(_("read rtc alarm failed")); |
fcf67294 MO |
337 | return -1; |
338 | } | |
339 | } | |
340 | ||
341 | if (wake.enabled != 1 || wake.time.tm_year == -1) { | |
342 | printf(_("alarm: off\n")); | |
343 | return 0; | |
344 | } | |
345 | ||
346 | rtc = wake.time; | |
347 | ||
348 | memset(&tm, 0, sizeof tm); | |
349 | tm.tm_sec = rtc.tm_sec; | |
350 | tm.tm_min = rtc.tm_min; | |
351 | tm.tm_hour = rtc.tm_hour; | |
352 | tm.tm_mday = rtc.tm_mday; | |
353 | tm.tm_mon = rtc.tm_mon; | |
354 | tm.tm_year = rtc.tm_year; | |
355 | tm.tm_isdst = -1; /* assume the system knows better than the RTC */ | |
356 | ||
357 | alarm = mktime(&tm); | |
358 | if (alarm == (time_t)-1) { | |
07b336c9 | 359 | warn(_("convert time failed")); |
fcf67294 MO |
360 | return -1; |
361 | } | |
362 | ||
363 | /* 0 if both UTC, or expresses diff if RTC in local time */ | |
364 | alarm += sys_time - rtc_time; | |
365 | ||
07b336c9 | 366 | printf(_("alarm: on %s"), ctime(&alarm)); |
fcf67294 MO |
367 | return 0; |
368 | } | |
369 | ||
76700389 BW |
370 | int main(int argc, char **argv) |
371 | { | |
372 | char *devname = DEFAULT_DEVICE; | |
373 | unsigned seconds = 0; | |
374 | char *suspend = DEFAULT_MODE; | |
375 | ||
77f5744c | 376 | int rc = EXIT_SUCCESS; |
76700389 BW |
377 | int t; |
378 | int fd; | |
379 | time_t alarm = 0; | |
380 | ||
3a2f3e82 KZ |
381 | static const struct option long_options[] = { |
382 | {"adjfile", required_argument, 0, 'A'}, | |
383 | {"auto", no_argument, 0, 'a'}, | |
384 | {"dry-run", no_argument, 0, 'n'}, | |
385 | {"local", no_argument, 0, 'l'}, | |
386 | {"utc", no_argument, 0, 'u'}, | |
387 | {"verbose", no_argument, 0, 'v'}, | |
388 | {"version", no_argument, 0, 'V'}, | |
389 | {"help", no_argument, 0, 'h'}, | |
390 | {"mode", required_argument, 0, 'm'}, | |
391 | {"device", required_argument, 0, 'd'}, | |
392 | {"seconds", required_argument, 0, 's'}, | |
393 | {"time", required_argument, 0, 't'}, | |
394 | {0, 0, 0, 0 } | |
395 | }; | |
396 | ||
76700389 BW |
397 | setlocale(LC_ALL, ""); |
398 | bindtextdomain(PACKAGE, LOCALEDIR); | |
399 | textdomain(PACKAGE); | |
efb8854f | 400 | atexit(close_stdout); |
76700389 | 401 | |
3a2f3e82 | 402 | while ((t = getopt_long(argc, argv, "A:ahd:lm:ns:t:uVv", |
76700389 BW |
403 | long_options, NULL)) != EOF) { |
404 | switch (t) { | |
3a2f3e82 KZ |
405 | case 'A': |
406 | /* for better compatibility with hwclock */ | |
407 | adjfile = optarg; | |
408 | break; | |
76700389 BW |
409 | case 'a': |
410 | /* CM_AUTO is default */ | |
411 | break; | |
412 | ||
413 | case 'd': | |
c1196d3a | 414 | devname = optarg; |
76700389 BW |
415 | break; |
416 | ||
417 | case 'l': | |
418 | clock_mode = CM_LOCAL; | |
419 | break; | |
420 | ||
421 | /* what system power mode to use? for now handle only | |
422 | * standardized mode names; eventually when systems | |
423 | * define their own state names, parse | |
424 | * /sys/power/state. | |
425 | * | |
426 | * "on" is used just to test the RTC alarm mechanism, | |
427 | * bypassing all the wakeup-from-sleep infrastructure. | |
428 | */ | |
429 | case 'm': | |
430 | if (strcmp(optarg, "standby") == 0 | |
431 | || strcmp(optarg, "mem") == 0 | |
432 | || strcmp(optarg, "disk") == 0 | |
433 | || strcmp(optarg, "on") == 0 | |
e4b0fc36 | 434 | || strcmp(optarg, "no") == 0 |
77f5744c | 435 | || strcmp(optarg, "off") == 0 |
c15dd93b | 436 | || strcmp(optarg, "disable") == 0 |
fcf67294 | 437 | || strcmp(optarg, "show") == 0 |
76700389 | 438 | ) { |
c1196d3a | 439 | suspend = optarg; |
76700389 BW |
440 | break; |
441 | } | |
07b336c9 KZ |
442 | |
443 | errx(EXIT_FAILURE, _("unrecognized suspend state '%s'"), | |
444 | optarg); | |
445 | break; | |
76700389 | 446 | |
569f3ca2 KZ |
447 | case 'n': |
448 | dryrun = 1; | |
449 | break; | |
450 | ||
76700389 BW |
451 | /* alarm time, seconds-to-sleep (relative) */ |
452 | case 's': | |
20a39982 | 453 | seconds = strtou32_or_err(optarg, _("invalid seconds argument")); |
76700389 BW |
454 | break; |
455 | ||
456 | /* alarm time, time_t (absolute, seconds since | |
457 | * 1/1 1970 UTC) | |
458 | */ | |
459 | case 't': | |
20a39982 | 460 | alarm = strtou32_or_err(optarg, _("invalid time argument")); |
76700389 BW |
461 | break; |
462 | ||
463 | case 'u': | |
464 | clock_mode = CM_UTC; | |
465 | break; | |
466 | ||
467 | case 'v': | |
468 | verbose++; | |
469 | break; | |
470 | ||
471 | case 'V': | |
217615e8 BS |
472 | printf(_("%s from %s\n"), |
473 | program_invocation_short_name, PACKAGE_STRING); | |
76700389 BW |
474 | exit(EXIT_SUCCESS); |
475 | ||
476 | case 'h': | |
07b336c9 | 477 | usage(stdout); |
76700389 | 478 | default: |
07b336c9 | 479 | usage(stderr); |
76700389 BW |
480 | } |
481 | } | |
482 | ||
483 | if (clock_mode == CM_AUTO) { | |
f8d87ab1 | 484 | if (read_clock_mode() < 0) { |
07b336c9 KZ |
485 | printf(_("%s: assuming RTC uses UTC ...\n"), |
486 | program_invocation_short_name); | |
76700389 BW |
487 | clock_mode = CM_UTC; |
488 | } | |
76700389 | 489 | } |
2148b051 DB |
490 | if (verbose) |
491 | printf(clock_mode == CM_UTC ? _("Using UTC time.\n") : | |
492 | _("Using local time.\n")); | |
76700389 | 493 | |
fcf67294 MO |
494 | if (!alarm && !seconds && strcmp(suspend,"disable") && |
495 | strcmp(suspend,"show")) { | |
496 | ||
07b336c9 KZ |
497 | warnx(_("must provide wake time (see -t and -s options)")); |
498 | usage(stderr); | |
76700389 BW |
499 | } |
500 | ||
501 | /* when devname doesn't start with /dev, append it */ | |
502 | if (strncmp(devname, "/dev/", strlen("/dev/")) != 0) { | |
503 | char *new_devname; | |
504 | ||
2ab428f6 | 505 | new_devname = xmalloc(strlen(devname) + strlen("/dev/") + 1); |
76700389 BW |
506 | |
507 | strcpy(new_devname, "/dev/"); | |
508 | strcat(new_devname, devname); | |
76700389 BW |
509 | devname = new_devname; |
510 | } | |
511 | ||
e4b0fc36 | 512 | if (strcmp(suspend, "on") != 0 && strcmp(suspend, "no") != 0 |
07b336c9 KZ |
513 | && !is_wakeup_enabled(devname)) |
514 | errx(EXIT_FAILURE, _("%s not enabled for wakeup events"), devname); | |
76700389 BW |
515 | |
516 | /* this RTC must exist and (if we'll sleep) be wakeup-enabled */ | |
77f5744c KZ |
517 | #ifdef O_CLOEXEC |
518 | fd = open(devname, O_RDONLY | O_CLOEXEC); | |
519 | #else | |
76700389 | 520 | fd = open(devname, O_RDONLY); |
77f5744c | 521 | #endif |
07b336c9 | 522 | if (fd < 0) |
289dcc90 | 523 | err(EXIT_FAILURE, _("cannot open %s"), devname); |
76700389 BW |
524 | |
525 | /* relative or absolute alarm time, normalized to time_t */ | |
f8d87ab1 | 526 | if (get_basetimes(fd) < 0) |
76700389 BW |
527 | exit(EXIT_FAILURE); |
528 | if (verbose) | |
529 | printf(_("alarm %ld, sys_time %ld, rtc_time %ld, seconds %u\n"), | |
530 | alarm, sys_time, rtc_time, seconds); | |
77f5744c | 531 | |
fcf67294 | 532 | if (strcmp(suspend, "show") && strcmp(suspend, "disable")) { |
e6d1dc94 LR |
533 | if (strcmp(suspend, "no") && strcmp(suspend, "on") && |
534 | strcmp(suspend, "off") && is_suspend_available(suspend) <= 0) { | |
535 | errx(EXIT_FAILURE, _("suspend to \"%s\" unavailable"), suspend); | |
536 | } | |
537 | ||
fcf67294 MO |
538 | /* care about alarm setup only if the show|disable |
539 | * modes are not set | |
540 | */ | |
541 | if (alarm) { | |
07b336c9 KZ |
542 | if (alarm < sys_time) |
543 | errx(EXIT_FAILURE, _("time doesn't go backward to %s"), | |
544 | ctime(&alarm)); | |
fcf67294 MO |
545 | alarm += sys_time - rtc_time; |
546 | } else | |
547 | alarm = rtc_time + seconds + 1; | |
548 | ||
549 | if (setup_alarm(fd, &alarm) < 0) | |
550 | exit(EXIT_FAILURE); | |
76700389 | 551 | |
07b336c9 KZ |
552 | if (strcmp(suspend, "no") == 0 || strcmp(suspend, "on") == 0) |
553 | printf(_("%s: wakeup using %s at %s"), | |
554 | program_invocation_short_name, devname, | |
555 | ctime(&alarm)); | |
556 | else | |
557 | printf(_("%s: wakeup from \"%s\" using %s at %s"), | |
558 | program_invocation_short_name, suspend, devname, | |
fcf67294 MO |
559 | ctime(&alarm)); |
560 | fflush(stdout); | |
561 | usleep(10 * 1000); | |
562 | } | |
76700389 | 563 | |
ecd55f96 KZ |
564 | if (strcmp(suspend, "no") == 0) { |
565 | if (verbose) | |
566 | printf(_("suspend mode: no; leaving\n")); | |
07b336c9 | 567 | dryrun = 1; /* to skip disabling alarm at the end */ |
ecd55f96 | 568 | |
77f5744c KZ |
569 | } else if (strcmp(suspend, "off") == 0) { |
570 | char *arg[4]; | |
571 | int i = 0; | |
572 | ||
ecd55f96 KZ |
573 | if (verbose) |
574 | printf(_("suspend mode: off; executing %s\n"), | |
575 | _PATH_SHUTDOWN); | |
77f5744c KZ |
576 | arg[i++] = _PATH_SHUTDOWN; |
577 | arg[i++] = "-P"; | |
578 | arg[i++] = "now"; | |
579 | arg[i] = NULL; | |
580 | ||
569f3ca2 KZ |
581 | if (!dryrun) { |
582 | execv(arg[0], arg); | |
77f5744c | 583 | |
07ff972e | 584 | warn(_("failed to execute %s"), _PATH_SHUTDOWN); |
569f3ca2 KZ |
585 | rc = EXIT_FAILURE; |
586 | } | |
ecd55f96 KZ |
587 | |
588 | } else if (strcmp(suspend, "on") == 0) { | |
76700389 BW |
589 | unsigned long data; |
590 | ||
ecd55f96 KZ |
591 | if (verbose) |
592 | printf(_("suspend mode: on; reading rtc\n")); | |
593 | ||
569f3ca2 KZ |
594 | if (!dryrun) { |
595 | do { | |
596 | t = read(fd, &data, sizeof data); | |
597 | if (t < 0) { | |
07b336c9 | 598 | warn(_("rtc read failed")); |
569f3ca2 KZ |
599 | break; |
600 | } | |
601 | if (verbose) | |
602 | printf("... %s: %03lx\n", devname, data); | |
603 | } while (!(data & RTC_AF)); | |
604 | } | |
ecd55f96 | 605 | |
c15dd93b MO |
606 | } else if (strcmp(suspend, "disable") == 0) { |
607 | /* just break, alarm gets disabled in the end */ | |
608 | if (verbose) | |
609 | printf(_("suspend mode: disable; disabling alarm\n")); | |
fcf67294 MO |
610 | |
611 | } else if(strcmp(suspend,"show") == 0) { | |
612 | if (verbose) | |
613 | printf(_("suspend mode: show; printing alarm info\n")); | |
614 | if (print_alarm(fd)) | |
615 | rc = EXIT_FAILURE; | |
616 | dryrun = 1; /* don't really disable alarm in the end, just show */ | |
617 | ||
ecd55f96 KZ |
618 | } else { |
619 | if (verbose) | |
620 | printf(_("suspend mode: %s; suspending system\n"), suspend); | |
621 | sync(); | |
622 | suspend_system(suspend); | |
76700389 BW |
623 | } |
624 | ||
829eab67 G |
625 | if (!dryrun) { |
626 | /* try to disable the alarm with the preferred RTC_WKALM_RD and | |
627 | * RTC_WKALM_SET calls, if it fails fall back to RTC_AIE_OFF | |
628 | */ | |
629 | struct rtc_wkalrm wake; | |
630 | ||
631 | if (ioctl(fd, RTC_WKALM_RD, &wake) < 0) { | |
632 | if (ioctl(fd, RTC_AIE_OFF, 0) < 0) { | |
633 | warn(_("disable rtc alarm interrupt failed")); | |
634 | rc = EXIT_FAILURE; | |
635 | } | |
636 | } else { | |
637 | wake.enabled = 0; | |
638 | if (ioctl(fd, RTC_WKALM_SET, &wake) < 0) { | |
639 | warn(_("disable rtc alarm interrupt failed")); | |
640 | rc = EXIT_FAILURE; | |
641 | } | |
642 | } | |
643 | } | |
76700389 BW |
644 | |
645 | close(fd); | |
77f5744c | 646 | return rc; |
76700389 | 647 | } |