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