]>
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 | 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"; |
c6e47247 | 186 | execute_directories(dirs, DEFAULT_TIMEOUT_USEC, NULL, NULL, arguments); |
6edd7d0a | 187 | |
19adb8a3 ZJS |
188 | return r; |
189 | } | |
6edd7d0a | 190 | |
c58493c0 ML |
191 | static int read_wakealarm(uint64_t *result) { |
192 | _cleanup_free_ char *t = NULL; | |
193 | ||
194 | if (read_one_line_file("/sys/class/rtc/rtc0/since_epoch", &t) >= 0) | |
195 | return safe_atou64(t, result); | |
196 | return -EBADF; | |
197 | } | |
198 | ||
199 | static int write_wakealarm(const char *str) { | |
200 | ||
201 | _cleanup_fclose_ FILE *f = NULL; | |
202 | int r; | |
203 | ||
204 | f = fopen("/sys/class/rtc/rtc0/wakealarm", "we"); | |
205 | if (!f) | |
206 | return log_error_errno(errno, "Failed to open /sys/class/rtc/rtc0/wakealarm: %m"); | |
207 | ||
208 | r = write_string_stream(f, str, 0); | |
209 | if (r < 0) | |
210 | return log_error_errno(r, "Failed to write '%s' to /sys/class/rtc/rtc0/wakealarm: %m", str); | |
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; | |
219 | usec_t orig_time, cmp_time; | |
220 | char time_str[DECIMAL_STR_MAX(uint64_t)]; | |
221 | int r; | |
222 | ||
223 | r = parse_sleep_config("suspend", &suspend_modes, &suspend_states, | |
224 | NULL); | |
225 | if (r < 0) | |
226 | return r; | |
227 | ||
228 | r = parse_sleep_config("hibernate", &hibernate_modes, | |
229 | &hibernate_states, NULL); | |
230 | if (r < 0) | |
231 | return r; | |
232 | ||
233 | r = read_wakealarm(&orig_time); | |
234 | if (r < 0) | |
235 | return log_error_errno(errno, "Failed to read time: %d", r); | |
236 | ||
237 | orig_time += hibernate_delay_sec / USEC_PER_SEC; | |
238 | xsprintf(time_str, "%" PRIu64, orig_time); | |
239 | ||
240 | r = write_wakealarm(time_str); | |
241 | if (r < 0) | |
242 | return r; | |
243 | ||
244 | log_debug("Set RTC wake alarm for %s", time_str); | |
245 | ||
246 | r = execute(suspend_modes, suspend_states); | |
247 | if (r < 0) | |
248 | return r; | |
249 | ||
250 | r = read_wakealarm(&cmp_time); | |
251 | if (r < 0) | |
252 | return log_error_errno(errno, "Failed to read time: %d", r); | |
253 | ||
254 | /* reset RTC */ | |
255 | r = write_wakealarm("0"); | |
256 | if (r < 0) | |
257 | return r; | |
258 | ||
259 | log_debug("Woke up at %"PRIu64, cmp_time); | |
260 | ||
261 | /* if woken up after alarm time, hibernate */ | |
262 | if (cmp_time >= orig_time) | |
263 | r = execute(hibernate_modes, hibernate_states); | |
264 | ||
265 | return r; | |
266 | } | |
267 | ||
601185b4 | 268 | static void help(void) { |
19adb8a3 ZJS |
269 | printf("%s COMMAND\n\n" |
270 | "Suspend the system, hibernate the system, or both.\n\n" | |
271 | "Commands:\n" | |
272 | " -h --help Show this help and exit\n" | |
273 | " --version Print version string and exit\n" | |
274 | " suspend Suspend the system\n" | |
275 | " hibernate Hibernate the system\n" | |
276 | " hybrid-sleep Both hibernate and suspend the system\n" | |
e68c79db | 277 | " suspend-then-hibernate Initially suspend and then hibernate\n" |
c58493c0 | 278 | " the system after a fixed period of time\n" |
601185b4 | 279 | , program_invocation_short_name); |
19adb8a3 | 280 | } |
6edd7d0a | 281 | |
19adb8a3 ZJS |
282 | static int parse_argv(int argc, char *argv[]) { |
283 | enum { | |
284 | ARG_VERSION = 0x100, | |
285 | }; | |
286 | ||
287 | static const struct option options[] = { | |
288 | { "help", no_argument, NULL, 'h' }, | |
289 | { "version", no_argument, NULL, ARG_VERSION }, | |
eb9da376 | 290 | {} |
19adb8a3 ZJS |
291 | }; |
292 | ||
293 | int c; | |
294 | ||
295 | assert(argc >= 0); | |
296 | assert(argv); | |
297 | ||
601185b4 | 298 | while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) |
19adb8a3 ZJS |
299 | switch(c) { |
300 | case 'h': | |
601185b4 ZJS |
301 | help(); |
302 | return 0; /* done */ | |
19adb8a3 ZJS |
303 | |
304 | case ARG_VERSION: | |
3f6fd1ba | 305 | return version(); |
19adb8a3 ZJS |
306 | |
307 | case '?': | |
308 | return -EINVAL; | |
309 | ||
310 | default: | |
eb9da376 | 311 | assert_not_reached("Unhandled option"); |
19adb8a3 ZJS |
312 | } |
313 | ||
314 | if (argc - optind != 1) { | |
315 | log_error("Usage: %s COMMAND", | |
316 | program_invocation_short_name); | |
317 | return -EINVAL; | |
318 | } | |
319 | ||
320 | arg_verb = argv[optind]; | |
6edd7d0a | 321 | |
19adb8a3 ZJS |
322 | if (!streq(arg_verb, "suspend") && |
323 | !streq(arg_verb, "hibernate") && | |
c58493c0 | 324 | !streq(arg_verb, "hybrid-sleep") && |
e68c79db | 325 | !streq(arg_verb, "suspend-then-hibernate")) { |
19adb8a3 ZJS |
326 | log_error("Unknown command '%s'.", arg_verb); |
327 | return -EINVAL; | |
328 | } | |
329 | ||
330 | return 1 /* work to do */; | |
331 | } | |
332 | ||
333 | int main(int argc, char *argv[]) { | |
334 | _cleanup_strv_free_ char **modes = NULL, **states = NULL; | |
c58493c0 | 335 | usec_t delay = 0; |
19adb8a3 ZJS |
336 | int r; |
337 | ||
338 | log_set_target(LOG_TARGET_AUTO); | |
339 | log_parse_environment(); | |
340 | log_open(); | |
341 | ||
342 | r = parse_argv(argc, argv); | |
343 | if (r <= 0) | |
344 | goto finish; | |
345 | ||
c58493c0 | 346 | r = parse_sleep_config(arg_verb, &modes, &states, &delay); |
19adb8a3 ZJS |
347 | if (r < 0) |
348 | goto finish; | |
349 | ||
e68c79db | 350 | if (streq(arg_verb, "suspend-then-hibernate")) |
c58493c0 ML |
351 | r = execute_s2h(delay); |
352 | else | |
353 | r = execute(modes, states); | |
19adb8a3 ZJS |
354 | finish: |
355 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; | |
6edd7d0a | 356 | } |