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