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