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