]>
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> |
ca78ad1d | 8 | #include <fcntl.h> |
19adb8a3 | 9 | #include <getopt.h> |
17c40b3a | 10 | #include <linux/fiemap.h> |
1bbbefe7 | 11 | #include <poll.h> |
3f6fd1ba | 12 | #include <stdio.h> |
ca78ad1d ZJS |
13 | #include <sys/stat.h> |
14 | #include <sys/types.h> | |
1bbbefe7 | 15 | #include <sys/timerfd.h> |
ca78ad1d | 16 | #include <unistd.h> |
6edd7d0a | 17 | |
aa62a893 | 18 | #include "sd-messages.h" |
3f6fd1ba LP |
19 | |
20 | #include "def.h" | |
89711996 | 21 | #include "exec-util.h" |
3ffd4af2 | 22 | #include "fd-util.h" |
ca78ad1d | 23 | #include "format-util.h" |
a5c32cff | 24 | #include "fileio.h" |
3f6fd1ba | 25 | #include "log.h" |
5e332028 | 26 | #include "main-func.h" |
ed698d30 | 27 | #include "parse-util.h" |
294bf0c3 | 28 | #include "pretty-print.h" |
19adb8a3 | 29 | #include "sleep-config.h" |
c58493c0 | 30 | #include "stdio-util.h" |
07630cea | 31 | #include "string-util.h" |
3f6fd1ba | 32 | #include "strv.h" |
1bbbefe7 | 33 | #include "time-util.h" |
3f6fd1ba | 34 | #include "util.h" |
19adb8a3 ZJS |
35 | |
36 | static char* arg_verb = NULL; | |
37 | ||
17c40b3a ML |
38 | static int write_hibernate_location_info(void) { |
39 | _cleanup_free_ char *device = NULL, *type = NULL; | |
40 | _cleanup_free_ struct fiemap *fiemap = NULL; | |
41 | char offset_str[DECIMAL_STR_MAX(uint64_t)]; | |
42 | char device_str[DECIMAL_STR_MAX(uint64_t)]; | |
43 | _cleanup_close_ int fd = -1; | |
44 | struct stat stb; | |
45 | uint64_t offset; | |
46 | int r; | |
47 | ||
48 | r = find_hibernate_location(&device, &type, NULL, NULL); | |
49 | if (r < 0) | |
50 | return log_debug_errno(r, "Unable to find hibernation location: %m"); | |
51 | ||
52 | /* if it's a swap partition, we just write the disk to /sys/power/resume */ | |
ed698d30 | 53 | if (streq(type, "partition")) { |
57512c89 | 54 | r = write_string_file("/sys/power/resume", device, WRITE_STRING_FILE_DISABLE_BUFFER); |
ed698d30 | 55 | if (r < 0) |
6835d78d | 56 | return log_debug_errno(r, "Failed to write partition device to /sys/power/resume: %m"); |
ed698d30 LP |
57 | |
58 | return r; | |
59 | } | |
baaa35ad ZJS |
60 | if (!streq(type, "file")) |
61 | return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), | |
62 | "Invalid hibernate type: %s", type); | |
17c40b3a ML |
63 | |
64 | /* Only available in 4.17+ */ | |
c695101f LP |
65 | if (access("/sys/power/resume_offset", W_OK) < 0) { |
66 | if (errno == ENOENT) { | |
67 | log_debug("Kernel too old, can't configure resume offset, ignoring."); | |
17c40b3a | 68 | return 0; |
c695101f | 69 | } |
ed698d30 | 70 | |
17c40b3a | 71 | return log_debug_errno(errno, "/sys/power/resume_offset not writeable: %m"); |
c695101f | 72 | } |
17c40b3a ML |
73 | |
74 | fd = open(device, O_RDONLY | O_CLOEXEC | O_NONBLOCK); | |
75 | if (fd < 0) | |
76 | return log_debug_errno(errno, "Unable to open '%s': %m", device); | |
77 | r = fstat(fd, &stb); | |
78 | if (r < 0) | |
79 | return log_debug_errno(errno, "Unable to stat %s: %m", device); | |
ed698d30 | 80 | |
17c40b3a ML |
81 | r = read_fiemap(fd, &fiemap); |
82 | if (r < 0) | |
ed698d30 | 83 | return log_debug_errno(r, "Unable to read extent map for '%s': %m", device); |
baaa35ad ZJS |
84 | if (fiemap->fm_mapped_extents == 0) |
85 | return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), | |
86 | "No extents found in '%s'", device); | |
ed698d30 | 87 | |
17c40b3a ML |
88 | offset = fiemap->fm_extents[0].fe_physical / page_size(); |
89 | xsprintf(offset_str, "%" PRIu64, offset); | |
57512c89 | 90 | r = write_string_file("/sys/power/resume_offset", offset_str, WRITE_STRING_FILE_DISABLE_BUFFER); |
17c40b3a | 91 | if (r < 0) |
ed698d30 | 92 | return log_debug_errno(r, "Failed to write offset '%s': %m", offset_str); |
17c40b3a ML |
93 | |
94 | xsprintf(device_str, "%lx", (unsigned long)stb.st_dev); | |
57512c89 | 95 | r = write_string_file("/sys/power/resume", device_str, WRITE_STRING_FILE_DISABLE_BUFFER); |
17c40b3a | 96 | if (r < 0) |
ed698d30 LP |
97 | return log_debug_errno(r, "Failed to write device '%s': %m", device_str); |
98 | ||
17c40b3a ML |
99 | return 0; |
100 | } | |
101 | ||
19adb8a3 ZJS |
102 | static int write_mode(char **modes) { |
103 | int r = 0; | |
104 | char **mode; | |
105 | ||
106 | STRV_FOREACH(mode, modes) { | |
aa62a893 LP |
107 | int k; |
108 | ||
57512c89 | 109 | k = write_string_file("/sys/power/disk", *mode, WRITE_STRING_FILE_DISABLE_BUFFER); |
ed698d30 | 110 | if (k >= 0) |
19adb8a3 | 111 | return 0; |
aa62a893 | 112 | |
ed698d30 LP |
113 | log_debug_errno(k, "Failed to write '%s' to /sys/power/disk: %m", *mode); |
114 | if (r >= 0) | |
19adb8a3 ZJS |
115 | r = k; |
116 | } | |
6edd7d0a | 117 | |
19adb8a3 ZJS |
118 | return r; |
119 | } | |
6edd7d0a | 120 | |
2fd069b1 | 121 | static int write_state(FILE **f, char **states) { |
19adb8a3 ZJS |
122 | char **state; |
123 | int r = 0; | |
124 | ||
125 | STRV_FOREACH(state, states) { | |
126 | int k; | |
127 | ||
57512c89 | 128 | k = write_string_stream(*f, *state, WRITE_STRING_FILE_DISABLE_BUFFER); |
ed698d30 | 129 | if (k >= 0) |
19adb8a3 | 130 | return 0; |
ed698d30 LP |
131 | log_debug_errno(k, "Failed to write '%s' to /sys/power/state: %m", *state); |
132 | if (r >= 0) | |
19adb8a3 ZJS |
133 | r = k; |
134 | ||
2fd069b1 ZJS |
135 | fclose(*f); |
136 | *f = fopen("/sys/power/state", "we"); | |
4a62c710 | 137 | if (!*f) |
0ca906ac | 138 | return -errno; |
6edd7d0a LP |
139 | } |
140 | ||
19adb8a3 ZJS |
141 | return r; |
142 | } | |
6edd7d0a | 143 | |
19adb8a3 | 144 | static int execute(char **modes, char **states) { |
e2cc6eca LP |
145 | char *arguments[] = { |
146 | NULL, | |
147 | (char*) "pre", | |
148 | arg_verb, | |
149 | NULL | |
150 | }; | |
b5084605 LP |
151 | static const char* const dirs[] = { |
152 | SYSTEM_SLEEP_PATH, | |
153 | NULL | |
154 | }; | |
e2cc6eca | 155 | |
19adb8a3 | 156 | int r; |
2fd069b1 | 157 | _cleanup_fclose_ FILE *f = NULL; |
6524990f | 158 | |
19adb8a3 | 159 | /* This file is opened first, so that if we hit an error, |
09692409 | 160 | * we can abort before modifying any state. */ |
6edd7d0a | 161 | f = fopen("/sys/power/state", "we"); |
4a62c710 MS |
162 | if (!f) |
163 | return log_error_errno(errno, "Failed to open /sys/power/state: %m"); | |
6edd7d0a | 164 | |
57512c89 YW |
165 | setvbuf(f, NULL, _IONBF, 0); |
166 | ||
19adb8a3 | 167 | /* Configure the hibernation mode */ |
17c40b3a ML |
168 | if (!strv_isempty(modes)) { |
169 | r = write_hibernate_location_info(); | |
170 | if (r < 0) | |
171 | return log_error_errno(r, "Failed to write hibernation disk offset: %m"); | |
172 | r = write_mode(modes); | |
173 | if (r < 0) | |
0ca906ac | 174 | return log_error_errno(r, "Failed to write mode to /sys/power/disk: %m");; |
17c40b3a | 175 | } |
19adb8a3 | 176 | |
aed98342 | 177 | (void) execute_directories(dirs, DEFAULT_TIMEOUT_USEC, NULL, NULL, arguments, NULL, EXEC_DIR_PARALLEL | EXEC_DIR_IGNORE_ERRORS); |
6edd7d0a | 178 | |
19adb8a3 | 179 | log_struct(LOG_INFO, |
2b044526 | 180 | "MESSAGE_ID=" SD_MESSAGE_SLEEP_START_STR, |
e2cc6eca | 181 | LOG_MESSAGE("Suspending system..."), |
a1230ff9 | 182 | "SLEEP=%s", arg_verb); |
19adb8a3 | 183 | |
2fd069b1 | 184 | r = write_state(&f, states); |
19adb8a3 | 185 | if (r < 0) |
14250f09 LP |
186 | log_struct_errno(LOG_ERR, r, |
187 | "MESSAGE_ID=" SD_MESSAGE_SLEEP_STOP_STR, | |
188 | LOG_MESSAGE("Failed to suspend system. System resumed again: %m"), | |
189 | "SLEEP=%s", arg_verb); | |
190 | else | |
191 | log_struct(LOG_INFO, | |
192 | "MESSAGE_ID=" SD_MESSAGE_SLEEP_STOP_STR, | |
193 | LOG_MESSAGE("System resumed."), | |
194 | "SLEEP=%s", arg_verb); | |
eb267289 | 195 | |
6edd7d0a | 196 | arguments[1] = (char*) "post"; |
aed98342 | 197 | (void) execute_directories(dirs, DEFAULT_TIMEOUT_USEC, NULL, NULL, arguments, NULL, EXEC_DIR_PARALLEL | EXEC_DIR_IGNORE_ERRORS); |
6edd7d0a | 198 | |
19adb8a3 ZJS |
199 | return r; |
200 | } | |
6edd7d0a | 201 | |
28ca9c24 | 202 | static int execute_s2h(const SleepConfig *sleep_config) { |
1bbbefe7 ZS |
203 | _cleanup_close_ int tfd = -1; |
204 | char buf[FORMAT_TIMESPAN_MAX]; | |
205 | struct itimerspec ts = {}; | |
206 | struct pollfd fds; | |
c58493c0 ML |
207 | int r; |
208 | ||
28ca9c24 | 209 | assert(sleep_config); |
c58493c0 | 210 | |
1bbbefe7 ZS |
211 | tfd = timerfd_create(CLOCK_BOOTTIME_ALARM, TFD_NONBLOCK|TFD_CLOEXEC); |
212 | if (tfd < 0) | |
213 | return log_error_errno(errno, "Error creating timerfd: %m"); | |
c58493c0 | 214 | |
1bbbefe7 | 215 | log_debug("Set timerfd wake alarm for %s", |
28ca9c24 | 216 | format_timespan(buf, sizeof(buf), sleep_config->hibernate_delay_sec, USEC_PER_SEC)); |
c58493c0 | 217 | |
28ca9c24 | 218 | timespec_store(&ts.it_value, sleep_config->hibernate_delay_sec); |
c58493c0 | 219 | |
1bbbefe7 | 220 | r = timerfd_settime(tfd, 0, &ts, NULL); |
c58493c0 | 221 | if (r < 0) |
1bbbefe7 | 222 | return log_error_errno(errno, "Error setting hibernate timer: %m"); |
c58493c0 | 223 | |
28ca9c24 | 224 | r = execute(sleep_config->suspend_modes, sleep_config->suspend_states); |
c58493c0 | 225 | if (r < 0) |
033cea5c | 226 | return r; |
c58493c0 | 227 | |
1bbbefe7 ZS |
228 | fds = (struct pollfd) { |
229 | .fd = tfd, | |
230 | .events = POLLIN, | |
231 | }; | |
232 | r = poll(&fds, 1, 0); | |
c58493c0 | 233 | if (r < 0) |
1bbbefe7 | 234 | return log_error_errno(errno, "Error polling timerfd: %m"); |
c58493c0 | 235 | |
1bbbefe7 | 236 | tfd = safe_close(tfd); |
c58493c0 | 237 | |
1bbbefe7 | 238 | if (!FLAGS_SET(fds.revents, POLLIN)) /* We woke up before the alarm time, we are done. */ |
eabcf200 | 239 | return 0; |
c58493c0 | 240 | |
eabcf200 | 241 | /* If woken up after alarm time, hibernate */ |
1bbbefe7 | 242 | log_debug("Attempting to hibernate after waking from %s timer", |
28ca9c24 ZS |
243 | format_timespan(buf, sizeof(buf), sleep_config->hibernate_delay_sec, USEC_PER_SEC)); |
244 | ||
245 | r = execute(sleep_config->hibernate_modes, sleep_config->hibernate_states); | |
f05e1ae6 LP |
246 | if (r < 0) { |
247 | log_notice("Couldn't hibernate, will try to suspend again."); | |
28ca9c24 | 248 | r = execute(sleep_config->suspend_modes, sleep_config->suspend_states); |
f05e1ae6 LP |
249 | if (r < 0) { |
250 | log_notice("Could neither hibernate nor suspend again, giving up."); | |
251 | return r; | |
252 | } | |
253 | } | |
254 | ||
255 | return 0; | |
c58493c0 ML |
256 | } |
257 | ||
37ec0fdd LP |
258 | static int help(void) { |
259 | _cleanup_free_ char *link = NULL; | |
260 | int r; | |
261 | ||
262 | r = terminal_urlify_man("systemd-suspend.service", "8", &link); | |
263 | if (r < 0) | |
264 | return log_oom(); | |
265 | ||
19adb8a3 ZJS |
266 | printf("%s COMMAND\n\n" |
267 | "Suspend the system, hibernate the system, or both.\n\n" | |
37ec0fdd LP |
268 | " -h --help Show this help and exit\n" |
269 | " --version Print version string and exit\n" | |
270 | "\nCommands:\n" | |
271 | " suspend Suspend the system\n" | |
272 | " hibernate Hibernate the system\n" | |
273 | " hybrid-sleep Both hibernate and suspend the system\n" | |
e68c79db | 274 | " suspend-then-hibernate Initially suspend and then hibernate\n" |
37ec0fdd LP |
275 | " the system after a fixed period of time\n" |
276 | "\nSee the %s for details.\n" | |
277 | , program_invocation_short_name | |
278 | , link | |
279 | ); | |
280 | ||
281 | return 0; | |
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': | |
37ec0fdd | 303 | return help(); |
19adb8a3 ZJS |
304 | |
305 | case ARG_VERSION: | |
3f6fd1ba | 306 | return version(); |
19adb8a3 ZJS |
307 | |
308 | case '?': | |
309 | return -EINVAL; | |
310 | ||
311 | default: | |
eb9da376 | 312 | assert_not_reached("Unhandled option"); |
19adb8a3 ZJS |
313 | } |
314 | ||
baaa35ad ZJS |
315 | if (argc - optind != 1) |
316 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
317 | "Usage: %s COMMAND", | |
318 | program_invocation_short_name); | |
19adb8a3 ZJS |
319 | |
320 | arg_verb = argv[optind]; | |
6edd7d0a | 321 | |
baaa35ad ZJS |
322 | if (!STR_IN_SET(arg_verb, "suspend", "hibernate", "hybrid-sleep", "suspend-then-hibernate")) |
323 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
324 | "Unknown command '%s'.", arg_verb); | |
19adb8a3 ZJS |
325 | |
326 | return 1 /* work to do */; | |
327 | } | |
328 | ||
7caefb81 | 329 | static int run(int argc, char *argv[]) { |
e8f1d00d | 330 | bool allow; |
28ca9c24 ZS |
331 | char **modes = NULL, **states = NULL; |
332 | _cleanup_(free_sleep_configp) SleepConfig *sleep_config = NULL; | |
19adb8a3 ZJS |
333 | int r; |
334 | ||
6bf3c61c | 335 | log_setup_service(); |
19adb8a3 ZJS |
336 | |
337 | r = parse_argv(argc, argv); | |
338 | if (r <= 0) | |
7caefb81 | 339 | return r; |
19adb8a3 | 340 | |
28ca9c24 ZS |
341 | r = parse_sleep_config(&sleep_config); |
342 | if (r < 0) | |
343 | return r; | |
344 | ||
345 | r = sleep_settings(arg_verb, sleep_config, &allow, &modes, &states); | |
19adb8a3 | 346 | if (r < 0) |
7caefb81 | 347 | return r; |
19adb8a3 | 348 | |
baaa35ad ZJS |
349 | if (!allow) |
350 | return log_error_errno(SYNTHETIC_ERRNO(EACCES), | |
351 | "Sleep mode \"%s\" is disabled by configuration, refusing.", | |
352 | arg_verb); | |
e8f1d00d | 353 | |
e68c79db | 354 | if (streq(arg_verb, "suspend-then-hibernate")) |
28ca9c24 | 355 | return execute_s2h(sleep_config); |
c58493c0 | 356 | else |
7caefb81 | 357 | return execute(modes, states); |
6edd7d0a | 358 | } |
7caefb81 ZJS |
359 | |
360 | DEFINE_MAIN_FUNCTION(run); |