]>
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) |
0ca906ac | 177 | return log_error_errno(r, "Failed to write /sys/power/state: %m"); |
19adb8a3 ZJS |
178 | |
179 | log_struct(LOG_INFO, | |
2b044526 | 180 | "MESSAGE_ID=" SD_MESSAGE_SLEEP_STOP_STR, |
a5ccdb98 | 181 | LOG_MESSAGE("System resumed."), |
a1230ff9 | 182 | "SLEEP=%s", arg_verb); |
eb267289 | 183 | |
6edd7d0a | 184 | arguments[1] = (char*) "post"; |
78ec1bb4 | 185 | execute_directories(dirs, DEFAULT_TIMEOUT_USEC, NULL, NULL, arguments, NULL); |
6edd7d0a | 186 | |
19adb8a3 ZJS |
187 | return r; |
188 | } | |
6edd7d0a | 189 | |
c16669a3 | 190 | static int rtc_read_time(uint64_t *ret_sec) { |
c58493c0 | 191 | _cleanup_free_ char *t = NULL; |
58220e6b LP |
192 | int r; |
193 | ||
194 | r = read_one_line_file("/sys/class/rtc/rtc0/since_epoch", &t); | |
195 | if (r < 0) | |
033cea5c LP |
196 | return log_error_errno(r, "Failed to read RTC time: %m"); |
197 | ||
198 | r = safe_atou64(t, ret_sec); | |
199 | if (r < 0) | |
200 | return log_error_errno(r, "Failed to parse RTC time '%s': %m", t); | |
c58493c0 | 201 | |
033cea5c | 202 | return 0; |
c58493c0 ML |
203 | } |
204 | ||
f780e438 LP |
205 | static int rtc_write_wake_alarm(uint64_t sec) { |
206 | char buf[DECIMAL_STR_MAX(uint64_t)]; | |
c58493c0 ML |
207 | int r; |
208 | ||
f780e438 LP |
209 | xsprintf(buf, "%" PRIu64, sec); |
210 | ||
211 | r = write_string_file("/sys/class/rtc/rtc0/wakealarm", buf, 0); | |
c58493c0 | 212 | if (r < 0) |
f780e438 | 213 | return log_error_errno(r, "Failed to write '%s' to /sys/class/rtc/rtc0/wakealarm: %m", buf); |
c58493c0 ML |
214 | |
215 | return 0; | |
216 | } | |
217 | ||
218 | static int execute_s2h(usec_t hibernate_delay_sec) { | |
219 | ||
220 | _cleanup_strv_free_ char **hibernate_modes = NULL, **hibernate_states = NULL, | |
221 | **suspend_modes = NULL, **suspend_states = NULL; | |
f780e438 | 222 | usec_t original_time, wake_time, cmp_time; |
c58493c0 ML |
223 | int r; |
224 | ||
e8f1d00d | 225 | r = parse_sleep_config("suspend", NULL, &suspend_modes, &suspend_states, NULL); |
c58493c0 ML |
226 | if (r < 0) |
227 | return r; | |
228 | ||
e8f1d00d | 229 | r = parse_sleep_config("hibernate", NULL, &hibernate_modes, &hibernate_states, NULL); |
c58493c0 ML |
230 | if (r < 0) |
231 | return r; | |
232 | ||
f780e438 | 233 | r = rtc_read_time(&original_time); |
c58493c0 | 234 | if (r < 0) |
033cea5c | 235 | return r; |
c58493c0 | 236 | |
d029a3a8 | 237 | wake_time = original_time + DIV_ROUND_UP(hibernate_delay_sec, USEC_PER_SEC); |
f780e438 | 238 | r = rtc_write_wake_alarm(wake_time); |
c58493c0 ML |
239 | if (r < 0) |
240 | return r; | |
241 | ||
f780e438 | 242 | log_debug("Set RTC wake alarm for %" PRIu64, wake_time); |
c58493c0 ML |
243 | |
244 | r = execute(suspend_modes, suspend_states); | |
245 | if (r < 0) | |
246 | return r; | |
247 | ||
eabcf200 LP |
248 | /* Reset RTC right-away */ |
249 | r = rtc_write_wake_alarm(0); | |
c58493c0 | 250 | if (r < 0) |
033cea5c | 251 | return r; |
c58493c0 | 252 | |
eabcf200 | 253 | r = rtc_read_time(&cmp_time); |
c58493c0 ML |
254 | if (r < 0) |
255 | return r; | |
256 | ||
257 | log_debug("Woke up at %"PRIu64, cmp_time); | |
258 | ||
eabcf200 LP |
259 | if (cmp_time < wake_time) /* We woke up before the alarm time, we are done. */ |
260 | return 0; | |
c58493c0 | 261 | |
eabcf200 LP |
262 | /* If woken up after alarm time, hibernate */ |
263 | return execute(hibernate_modes, hibernate_states); | |
c58493c0 ML |
264 | } |
265 | ||
37ec0fdd LP |
266 | static int help(void) { |
267 | _cleanup_free_ char *link = NULL; | |
268 | int r; | |
269 | ||
270 | r = terminal_urlify_man("systemd-suspend.service", "8", &link); | |
271 | if (r < 0) | |
272 | return log_oom(); | |
273 | ||
19adb8a3 ZJS |
274 | printf("%s COMMAND\n\n" |
275 | "Suspend the system, hibernate the system, or both.\n\n" | |
37ec0fdd LP |
276 | " -h --help Show this help and exit\n" |
277 | " --version Print version string and exit\n" | |
278 | "\nCommands:\n" | |
279 | " suspend Suspend the system\n" | |
280 | " hibernate Hibernate the system\n" | |
281 | " hybrid-sleep Both hibernate and suspend the system\n" | |
e68c79db | 282 | " suspend-then-hibernate Initially suspend and then hibernate\n" |
37ec0fdd LP |
283 | " the system after a fixed period of time\n" |
284 | "\nSee the %s for details.\n" | |
285 | , program_invocation_short_name | |
286 | , link | |
287 | ); | |
288 | ||
289 | return 0; | |
19adb8a3 | 290 | } |
6edd7d0a | 291 | |
19adb8a3 ZJS |
292 | static int parse_argv(int argc, char *argv[]) { |
293 | enum { | |
294 | ARG_VERSION = 0x100, | |
295 | }; | |
296 | ||
297 | static const struct option options[] = { | |
298 | { "help", no_argument, NULL, 'h' }, | |
299 | { "version", no_argument, NULL, ARG_VERSION }, | |
eb9da376 | 300 | {} |
19adb8a3 ZJS |
301 | }; |
302 | ||
303 | int c; | |
304 | ||
305 | assert(argc >= 0); | |
306 | assert(argv); | |
307 | ||
601185b4 | 308 | while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) |
19adb8a3 ZJS |
309 | switch(c) { |
310 | case 'h': | |
37ec0fdd | 311 | return help(); |
19adb8a3 ZJS |
312 | |
313 | case ARG_VERSION: | |
3f6fd1ba | 314 | return version(); |
19adb8a3 ZJS |
315 | |
316 | case '?': | |
317 | return -EINVAL; | |
318 | ||
319 | default: | |
eb9da376 | 320 | assert_not_reached("Unhandled option"); |
19adb8a3 ZJS |
321 | } |
322 | ||
323 | if (argc - optind != 1) { | |
324 | log_error("Usage: %s COMMAND", | |
325 | program_invocation_short_name); | |
326 | return -EINVAL; | |
327 | } | |
328 | ||
329 | arg_verb = argv[optind]; | |
6edd7d0a | 330 | |
ed698d30 | 331 | if (!STR_IN_SET(arg_verb, "suspend", "hibernate", "hybrid-sleep", "suspend-then-hibernate")) { |
19adb8a3 ZJS |
332 | log_error("Unknown command '%s'.", arg_verb); |
333 | return -EINVAL; | |
334 | } | |
335 | ||
336 | return 1 /* work to do */; | |
337 | } | |
338 | ||
339 | int main(int argc, char *argv[]) { | |
e8f1d00d | 340 | bool allow; |
19adb8a3 | 341 | _cleanup_strv_free_ char **modes = NULL, **states = NULL; |
c58493c0 | 342 | usec_t delay = 0; |
19adb8a3 ZJS |
343 | int r; |
344 | ||
345 | log_set_target(LOG_TARGET_AUTO); | |
346 | log_parse_environment(); | |
347 | log_open(); | |
348 | ||
349 | r = parse_argv(argc, argv); | |
350 | if (r <= 0) | |
351 | goto finish; | |
352 | ||
e8f1d00d | 353 | r = parse_sleep_config(arg_verb, &allow, &modes, &states, &delay); |
19adb8a3 ZJS |
354 | if (r < 0) |
355 | goto finish; | |
356 | ||
e8f1d00d ZJS |
357 | if (!allow) { |
358 | log_error("Sleep mode \"%s\" is disabled by configuration, refusing.", arg_verb); | |
359 | return EXIT_FAILURE; | |
360 | } | |
361 | ||
e68c79db | 362 | if (streq(arg_verb, "suspend-then-hibernate")) |
c58493c0 ML |
363 | r = execute_s2h(delay); |
364 | else | |
365 | r = execute(modes, states); | |
ed698d30 | 366 | |
19adb8a3 ZJS |
367 | finish: |
368 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; | |
6edd7d0a | 369 | } |