]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
6edd7d0a | 2 | /*** |
96b2fb93 LP |
3 | Copyright © 2010-2017 Canonical |
4 | Copyright © 2018 Dell Inc. | |
6edd7d0a LP |
5 | ***/ |
6 | ||
6edd7d0a | 7 | #include <errno.h> |
19adb8a3 | 8 | #include <getopt.h> |
17c40b3a | 9 | #include <linux/fiemap.h> |
3f6fd1ba | 10 | #include <stdio.h> |
6edd7d0a | 11 | |
aa62a893 | 12 | #include "sd-messages.h" |
3f6fd1ba LP |
13 | |
14 | #include "def.h" | |
89711996 | 15 | #include "exec-util.h" |
3ffd4af2 | 16 | #include "fd-util.h" |
a5c32cff | 17 | #include "fileio.h" |
3f6fd1ba | 18 | #include "log.h" |
ed698d30 | 19 | #include "parse-util.h" |
19adb8a3 | 20 | #include "sleep-config.h" |
c58493c0 | 21 | #include "stdio-util.h" |
07630cea | 22 | #include "string-util.h" |
3f6fd1ba | 23 | #include "strv.h" |
37ec0fdd | 24 | #include "terminal-util.h" |
3f6fd1ba | 25 | #include "util.h" |
19adb8a3 ZJS |
26 | |
27 | static char* arg_verb = NULL; | |
28 | ||
17c40b3a ML |
29 | static int write_hibernate_location_info(void) { |
30 | _cleanup_free_ char *device = NULL, *type = NULL; | |
31 | _cleanup_free_ struct fiemap *fiemap = NULL; | |
32 | char offset_str[DECIMAL_STR_MAX(uint64_t)]; | |
33 | char device_str[DECIMAL_STR_MAX(uint64_t)]; | |
34 | _cleanup_close_ int fd = -1; | |
35 | struct stat stb; | |
36 | uint64_t offset; | |
37 | int r; | |
38 | ||
39 | r = find_hibernate_location(&device, &type, NULL, NULL); | |
40 | if (r < 0) | |
41 | return log_debug_errno(r, "Unable to find hibernation location: %m"); | |
42 | ||
43 | /* if it's a swap partition, we just write the disk to /sys/power/resume */ | |
ed698d30 LP |
44 | if (streq(type, "partition")) { |
45 | r = write_string_file("/sys/power/resume", device, 0); | |
46 | if (r < 0) | |
47 | return log_debug_errno(r, "Faileed to write partitoin device to /sys/power/resume: %m"); | |
48 | ||
49 | return r; | |
50 | } | |
51 | if (!streq(type, "file")) { | |
52 | log_debug("Invalid hibernate type: %s", type); | |
53 | return -EINVAL; | |
54 | } | |
17c40b3a ML |
55 | |
56 | /* Only available in 4.17+ */ | |
c695101f LP |
57 | if (access("/sys/power/resume_offset", W_OK) < 0) { |
58 | if (errno == ENOENT) { | |
59 | log_debug("Kernel too old, can't configure resume offset, ignoring."); | |
17c40b3a | 60 | return 0; |
c695101f | 61 | } |
ed698d30 | 62 | |
17c40b3a | 63 | return log_debug_errno(errno, "/sys/power/resume_offset not writeable: %m"); |
c695101f | 64 | } |
17c40b3a ML |
65 | |
66 | fd = open(device, O_RDONLY | O_CLOEXEC | O_NONBLOCK); | |
67 | if (fd < 0) | |
68 | return log_debug_errno(errno, "Unable to open '%s': %m", device); | |
69 | r = fstat(fd, &stb); | |
70 | if (r < 0) | |
71 | return log_debug_errno(errno, "Unable to stat %s: %m", device); | |
ed698d30 | 72 | |
17c40b3a ML |
73 | r = read_fiemap(fd, &fiemap); |
74 | if (r < 0) | |
ed698d30 | 75 | return log_debug_errno(r, "Unable to read extent map for '%s': %m", device); |
17c40b3a ML |
76 | if (fiemap->fm_mapped_extents == 0) { |
77 | log_debug("No extents found in '%s'", device); | |
78 | return -EINVAL; | |
79 | } | |
ed698d30 | 80 | |
17c40b3a ML |
81 | offset = fiemap->fm_extents[0].fe_physical / page_size(); |
82 | xsprintf(offset_str, "%" PRIu64, offset); | |
83 | r = write_string_file("/sys/power/resume_offset", offset_str, 0); | |
84 | if (r < 0) | |
ed698d30 | 85 | return log_debug_errno(r, "Failed to write offset '%s': %m", offset_str); |
17c40b3a ML |
86 | |
87 | xsprintf(device_str, "%lx", (unsigned long)stb.st_dev); | |
88 | r = write_string_file("/sys/power/resume", device_str, 0); | |
89 | if (r < 0) | |
ed698d30 LP |
90 | return log_debug_errno(r, "Failed to write device '%s': %m", device_str); |
91 | ||
17c40b3a ML |
92 | return 0; |
93 | } | |
94 | ||
19adb8a3 ZJS |
95 | static int write_mode(char **modes) { |
96 | int r = 0; | |
97 | char **mode; | |
98 | ||
99 | STRV_FOREACH(mode, modes) { | |
aa62a893 LP |
100 | int k; |
101 | ||
4c1fc3e4 | 102 | k = write_string_file("/sys/power/disk", *mode, 0); |
ed698d30 | 103 | if (k >= 0) |
19adb8a3 | 104 | return 0; |
aa62a893 | 105 | |
ed698d30 LP |
106 | log_debug_errno(k, "Failed to write '%s' to /sys/power/disk: %m", *mode); |
107 | if (r >= 0) | |
19adb8a3 ZJS |
108 | r = k; |
109 | } | |
6edd7d0a | 110 | |
19adb8a3 ZJS |
111 | return r; |
112 | } | |
6edd7d0a | 113 | |
2fd069b1 | 114 | static int write_state(FILE **f, char **states) { |
19adb8a3 ZJS |
115 | char **state; |
116 | int r = 0; | |
117 | ||
118 | STRV_FOREACH(state, states) { | |
119 | int k; | |
120 | ||
b1837133 | 121 | k = write_string_stream(*f, *state, 0); |
ed698d30 | 122 | if (k >= 0) |
19adb8a3 | 123 | return 0; |
ed698d30 LP |
124 | log_debug_errno(k, "Failed to write '%s' to /sys/power/state: %m", *state); |
125 | if (r >= 0) | |
19adb8a3 ZJS |
126 | r = k; |
127 | ||
2fd069b1 ZJS |
128 | fclose(*f); |
129 | *f = fopen("/sys/power/state", "we"); | |
4a62c710 | 130 | if (!*f) |
0ca906ac | 131 | return -errno; |
6edd7d0a LP |
132 | } |
133 | ||
19adb8a3 ZJS |
134 | return r; |
135 | } | |
6edd7d0a | 136 | |
19adb8a3 | 137 | static int execute(char **modes, char **states) { |
e2cc6eca LP |
138 | char *arguments[] = { |
139 | NULL, | |
140 | (char*) "pre", | |
141 | arg_verb, | |
142 | NULL | |
143 | }; | |
b5084605 LP |
144 | static const char* const dirs[] = { |
145 | SYSTEM_SLEEP_PATH, | |
146 | NULL | |
147 | }; | |
e2cc6eca | 148 | |
19adb8a3 | 149 | int r; |
2fd069b1 | 150 | _cleanup_fclose_ FILE *f = NULL; |
6524990f | 151 | |
19adb8a3 | 152 | /* This file is opened first, so that if we hit an error, |
09692409 | 153 | * we can abort before modifying any state. */ |
6edd7d0a | 154 | f = fopen("/sys/power/state", "we"); |
4a62c710 MS |
155 | if (!f) |
156 | return log_error_errno(errno, "Failed to open /sys/power/state: %m"); | |
6edd7d0a | 157 | |
19adb8a3 | 158 | /* Configure the hibernation mode */ |
17c40b3a ML |
159 | if (!strv_isempty(modes)) { |
160 | r = write_hibernate_location_info(); | |
161 | if (r < 0) | |
162 | return log_error_errno(r, "Failed to write hibernation disk offset: %m"); | |
163 | r = write_mode(modes); | |
164 | if (r < 0) | |
0ca906ac | 165 | return log_error_errno(r, "Failed to write mode to /sys/power/disk: %m");; |
17c40b3a | 166 | } |
19adb8a3 | 167 | |
78ec1bb4 | 168 | execute_directories(dirs, DEFAULT_TIMEOUT_USEC, NULL, NULL, arguments, NULL); |
6edd7d0a | 169 | |
19adb8a3 | 170 | log_struct(LOG_INFO, |
2b044526 | 171 | "MESSAGE_ID=" SD_MESSAGE_SLEEP_START_STR, |
e2cc6eca | 172 | LOG_MESSAGE("Suspending system..."), |
a1230ff9 | 173 | "SLEEP=%s", arg_verb); |
19adb8a3 | 174 | |
2fd069b1 | 175 | r = write_state(&f, states); |
19adb8a3 | 176 | if (r < 0) |
14250f09 LP |
177 | log_struct_errno(LOG_ERR, r, |
178 | "MESSAGE_ID=" SD_MESSAGE_SLEEP_STOP_STR, | |
179 | LOG_MESSAGE("Failed to suspend system. System resumed again: %m"), | |
180 | "SLEEP=%s", arg_verb); | |
181 | else | |
182 | log_struct(LOG_INFO, | |
183 | "MESSAGE_ID=" SD_MESSAGE_SLEEP_STOP_STR, | |
184 | LOG_MESSAGE("System resumed."), | |
185 | "SLEEP=%s", arg_verb); | |
eb267289 | 186 | |
6edd7d0a | 187 | arguments[1] = (char*) "post"; |
78ec1bb4 | 188 | execute_directories(dirs, DEFAULT_TIMEOUT_USEC, NULL, NULL, arguments, NULL); |
6edd7d0a | 189 | |
19adb8a3 ZJS |
190 | return r; |
191 | } | |
6edd7d0a | 192 | |
c16669a3 | 193 | static int rtc_read_time(uint64_t *ret_sec) { |
c58493c0 | 194 | _cleanup_free_ char *t = NULL; |
58220e6b LP |
195 | int r; |
196 | ||
197 | r = read_one_line_file("/sys/class/rtc/rtc0/since_epoch", &t); | |
198 | if (r < 0) | |
033cea5c LP |
199 | return log_error_errno(r, "Failed to read RTC time: %m"); |
200 | ||
201 | r = safe_atou64(t, ret_sec); | |
202 | if (r < 0) | |
203 | return log_error_errno(r, "Failed to parse RTC time '%s': %m", t); | |
c58493c0 | 204 | |
033cea5c | 205 | return 0; |
c58493c0 ML |
206 | } |
207 | ||
f780e438 LP |
208 | static int rtc_write_wake_alarm(uint64_t sec) { |
209 | char buf[DECIMAL_STR_MAX(uint64_t)]; | |
c58493c0 ML |
210 | int r; |
211 | ||
f780e438 LP |
212 | xsprintf(buf, "%" PRIu64, sec); |
213 | ||
214 | r = write_string_file("/sys/class/rtc/rtc0/wakealarm", buf, 0); | |
c58493c0 | 215 | if (r < 0) |
f780e438 | 216 | return log_error_errno(r, "Failed to write '%s' to /sys/class/rtc/rtc0/wakealarm: %m", buf); |
c58493c0 ML |
217 | |
218 | return 0; | |
219 | } | |
220 | ||
221 | static int execute_s2h(usec_t hibernate_delay_sec) { | |
222 | ||
223 | _cleanup_strv_free_ char **hibernate_modes = NULL, **hibernate_states = NULL, | |
224 | **suspend_modes = NULL, **suspend_states = NULL; | |
f780e438 | 225 | usec_t original_time, wake_time, cmp_time; |
c58493c0 ML |
226 | int r; |
227 | ||
e8f1d00d | 228 | r = parse_sleep_config("suspend", NULL, &suspend_modes, &suspend_states, NULL); |
c58493c0 ML |
229 | if (r < 0) |
230 | return r; | |
231 | ||
e8f1d00d | 232 | r = parse_sleep_config("hibernate", NULL, &hibernate_modes, &hibernate_states, NULL); |
c58493c0 ML |
233 | if (r < 0) |
234 | return r; | |
235 | ||
f780e438 | 236 | r = rtc_read_time(&original_time); |
c58493c0 | 237 | if (r < 0) |
033cea5c | 238 | return r; |
c58493c0 | 239 | |
d029a3a8 | 240 | wake_time = original_time + DIV_ROUND_UP(hibernate_delay_sec, USEC_PER_SEC); |
f780e438 | 241 | r = rtc_write_wake_alarm(wake_time); |
c58493c0 ML |
242 | if (r < 0) |
243 | return r; | |
244 | ||
f780e438 | 245 | log_debug("Set RTC wake alarm for %" PRIu64, wake_time); |
c58493c0 ML |
246 | |
247 | r = execute(suspend_modes, suspend_states); | |
248 | if (r < 0) | |
249 | return r; | |
250 | ||
eabcf200 LP |
251 | /* Reset RTC right-away */ |
252 | r = rtc_write_wake_alarm(0); | |
c58493c0 | 253 | if (r < 0) |
033cea5c | 254 | return r; |
c58493c0 | 255 | |
eabcf200 | 256 | r = rtc_read_time(&cmp_time); |
c58493c0 ML |
257 | if (r < 0) |
258 | return r; | |
259 | ||
260 | log_debug("Woke up at %"PRIu64, cmp_time); | |
261 | ||
eabcf200 LP |
262 | if (cmp_time < wake_time) /* We woke up before the alarm time, we are done. */ |
263 | return 0; | |
c58493c0 | 264 | |
eabcf200 | 265 | /* If woken up after alarm time, hibernate */ |
f05e1ae6 LP |
266 | r = execute(hibernate_modes, hibernate_states); |
267 | if (r < 0) { | |
268 | log_notice("Couldn't hibernate, will try to suspend again."); | |
269 | r = execute(suspend_modes, suspend_states); | |
270 | if (r < 0) { | |
271 | log_notice("Could neither hibernate nor suspend again, giving up."); | |
272 | return r; | |
273 | } | |
274 | } | |
275 | ||
276 | return 0; | |
c58493c0 ML |
277 | } |
278 | ||
37ec0fdd LP |
279 | static int help(void) { |
280 | _cleanup_free_ char *link = NULL; | |
281 | int r; | |
282 | ||
283 | r = terminal_urlify_man("systemd-suspend.service", "8", &link); | |
284 | if (r < 0) | |
285 | return log_oom(); | |
286 | ||
19adb8a3 ZJS |
287 | printf("%s COMMAND\n\n" |
288 | "Suspend the system, hibernate the system, or both.\n\n" | |
37ec0fdd LP |
289 | " -h --help Show this help and exit\n" |
290 | " --version Print version string and exit\n" | |
291 | "\nCommands:\n" | |
292 | " suspend Suspend the system\n" | |
293 | " hibernate Hibernate the system\n" | |
294 | " hybrid-sleep Both hibernate and suspend the system\n" | |
e68c79db | 295 | " suspend-then-hibernate Initially suspend and then hibernate\n" |
37ec0fdd LP |
296 | " the system after a fixed period of time\n" |
297 | "\nSee the %s for details.\n" | |
298 | , program_invocation_short_name | |
299 | , link | |
300 | ); | |
301 | ||
302 | return 0; | |
19adb8a3 | 303 | } |
6edd7d0a | 304 | |
19adb8a3 ZJS |
305 | static int parse_argv(int argc, char *argv[]) { |
306 | enum { | |
307 | ARG_VERSION = 0x100, | |
308 | }; | |
309 | ||
310 | static const struct option options[] = { | |
311 | { "help", no_argument, NULL, 'h' }, | |
312 | { "version", no_argument, NULL, ARG_VERSION }, | |
eb9da376 | 313 | {} |
19adb8a3 ZJS |
314 | }; |
315 | ||
316 | int c; | |
317 | ||
318 | assert(argc >= 0); | |
319 | assert(argv); | |
320 | ||
601185b4 | 321 | while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) |
19adb8a3 ZJS |
322 | switch(c) { |
323 | case 'h': | |
37ec0fdd | 324 | return help(); |
19adb8a3 ZJS |
325 | |
326 | case ARG_VERSION: | |
3f6fd1ba | 327 | return version(); |
19adb8a3 ZJS |
328 | |
329 | case '?': | |
330 | return -EINVAL; | |
331 | ||
332 | default: | |
eb9da376 | 333 | assert_not_reached("Unhandled option"); |
19adb8a3 ZJS |
334 | } |
335 | ||
336 | if (argc - optind != 1) { | |
337 | log_error("Usage: %s COMMAND", | |
338 | program_invocation_short_name); | |
339 | return -EINVAL; | |
340 | } | |
341 | ||
342 | arg_verb = argv[optind]; | |
6edd7d0a | 343 | |
ed698d30 | 344 | if (!STR_IN_SET(arg_verb, "suspend", "hibernate", "hybrid-sleep", "suspend-then-hibernate")) { |
19adb8a3 ZJS |
345 | log_error("Unknown command '%s'.", arg_verb); |
346 | return -EINVAL; | |
347 | } | |
348 | ||
349 | return 1 /* work to do */; | |
350 | } | |
351 | ||
352 | int main(int argc, char *argv[]) { | |
e8f1d00d | 353 | bool allow; |
19adb8a3 | 354 | _cleanup_strv_free_ char **modes = NULL, **states = NULL; |
c58493c0 | 355 | usec_t delay = 0; |
19adb8a3 ZJS |
356 | int r; |
357 | ||
358 | log_set_target(LOG_TARGET_AUTO); | |
359 | log_parse_environment(); | |
360 | log_open(); | |
361 | ||
362 | r = parse_argv(argc, argv); | |
363 | if (r <= 0) | |
364 | goto finish; | |
365 | ||
e8f1d00d | 366 | r = parse_sleep_config(arg_verb, &allow, &modes, &states, &delay); |
19adb8a3 ZJS |
367 | if (r < 0) |
368 | goto finish; | |
369 | ||
e8f1d00d ZJS |
370 | if (!allow) { |
371 | log_error("Sleep mode \"%s\" is disabled by configuration, refusing.", arg_verb); | |
372 | return EXIT_FAILURE; | |
373 | } | |
374 | ||
e68c79db | 375 | if (streq(arg_verb, "suspend-then-hibernate")) |
c58493c0 ML |
376 | r = execute_s2h(delay); |
377 | else | |
378 | r = execute(modes, states); | |
ed698d30 | 379 | |
19adb8a3 ZJS |
380 | finish: |
381 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; | |
6edd7d0a | 382 | } |