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