|
13 | 13 | #include <unistd.h> |
14 | 14 |
|
15 | 15 | #include "alloc-util.h" |
| 16 | +#include "bootspec.h" |
16 | 17 | #include "conf-parser.h" |
17 | 18 | #include "def.h" |
18 | 19 | #include "env-util.h" |
|
22 | 23 | #include "macro.h" |
23 | 24 | #include "parse-util.h" |
24 | 25 | #include "path-util.h" |
| 26 | +#include "proc-cmdline.h" |
25 | 27 | #include "sleep-config.h" |
26 | 28 | #include "string-util.h" |
27 | 29 | #include "strv.h" |
@@ -260,12 +262,94 @@ static bool enough_swap_for_hibernation(void) { |
260 | 262 | } |
261 | 263 |
|
262 | 264 | r = act <= (size - used) * HIBERNATION_SWAP_THRESHOLD; |
263 | | - log_debug("Hibernation is %spossible, Active(anon)=%llu kB, size=%zu kB, used=%zu kB, threshold=%.2g%%", |
264 | | - r ? "" : "im", act, size, used, 100*HIBERNATION_SWAP_THRESHOLD); |
| 265 | + log_debug("%s swap for hibernation, Active(anon)=%llu kB, size=%zu kB, used=%zu kB, threshold=%.2g%%", |
| 266 | + r ? "Enough" : "Not enough", act, size, used, 100*HIBERNATION_SWAP_THRESHOLD); |
265 | 267 |
|
266 | 268 | return r; |
267 | 269 | } |
268 | 270 |
|
| 271 | +static int check_resume_keys(const char *key, const char *value, void *data) { |
| 272 | + assert_se(key); |
| 273 | + assert_se(data); |
| 274 | + |
| 275 | + int *resume = data; |
| 276 | + |
| 277 | + if (*resume == 0) |
| 278 | + /* Exit if we already know we can't resume. */ |
| 279 | + return 0; |
| 280 | + |
| 281 | + if (streq(key, "noresume")) { |
| 282 | + log_debug("Found \"noresume\" on the kernel command line, hibernation is disabled."); |
| 283 | + *resume = 0; |
| 284 | + |
| 285 | + } else if (streq(key, "resume")) { |
| 286 | + log_debug("Found resume= option on the kernel command line, hibernation is possible."); |
| 287 | + *resume = 1; |
| 288 | + } |
| 289 | + |
| 290 | + return 0; |
| 291 | +} |
| 292 | + |
| 293 | +static int resume_configured_in_options(const char *options) { |
| 294 | + int resume = -1, r; |
| 295 | + |
| 296 | + /* We don't use PROC_CMDLINE_STRIP_RD_PREFIX here, so rd.resume is *not* supported. */ |
| 297 | + r = proc_cmdline_parse_given(options, check_resume_keys, &resume, 0); |
| 298 | + if (r < 0) |
| 299 | + return r; |
| 300 | + |
| 301 | + if (resume < 0) |
| 302 | + log_debug("Couldn't find resume= option, hibernation is disabled."); |
| 303 | + return resume > 0; |
| 304 | +} |
| 305 | + |
| 306 | +static int resume_configured(void) { |
| 307 | + _cleanup_(boot_config_free) BootConfig config = {}; |
| 308 | + const BootEntry *e; |
| 309 | + int r; |
| 310 | + |
| 311 | + /* Check whether a valid resume= option is present. If possible, we query the boot options |
| 312 | + * for the default kernel. If the system is not using sd-boot, fall back to checking the |
| 313 | + * current kernel command line. This is not perfect, but should suffice for most cases. */ |
| 314 | + |
| 315 | + r = find_default_boot_entry(NULL, NULL, &config, &e); |
| 316 | + if (r == -ENOKEY) |
| 317 | + log_debug_errno(r, "Cannot find the ESP partition mount point, falling back to other checks."); |
| 318 | + else if (r < 0) |
| 319 | + return log_debug_errno(r, "Cannot read boot configuration from ESP, assuming hibernation is not possible."); |
| 320 | + else { |
| 321 | + _cleanup_free_ char *options = NULL; |
| 322 | + |
| 323 | + options = strv_join(e->options, " "); |
| 324 | + if (!options) |
| 325 | + return log_oom(); |
| 326 | + |
| 327 | + r = resume_configured_in_options(options); |
| 328 | + if (r < 0) |
| 329 | + return log_error_errno(r, "Failed to parse kernel options in \"%s\": %m", |
| 330 | + strnull(e->path)); |
| 331 | + return r; |
| 332 | + } |
| 333 | + |
| 334 | + /* If we can't figure out the default boot entry, let's fall back to current kernel cmdline */ |
| 335 | + _cleanup_free_ char *line = NULL; |
| 336 | + r = proc_cmdline(&line); |
| 337 | + if (IN_SET(r, -EPERM, -EACCES, -ENOENT)) |
| 338 | + log_debug_errno(r, "Cannot access /proc/cmdline: %m"); |
| 339 | + else if (r < 0) |
| 340 | + return log_error_errno(r, "Failed to query /proc/cmdline: %m"); |
| 341 | + else { |
| 342 | + r = resume_configured_in_options(line); |
| 343 | + if (r < 0) |
| 344 | + return log_error_errno(r, "Failed to parse kernel proc cmdline: %m"); |
| 345 | + |
| 346 | + return r; |
| 347 | + } |
| 348 | + |
| 349 | + log_debug("Couldn't detect any resume mechanism, hibernation is disabled."); |
| 350 | + return false; |
| 351 | +} |
| 352 | + |
269 | 353 | int read_fiemap(int fd, struct fiemap **ret) { |
270 | 354 | _cleanup_free_ struct fiemap *fiemap = NULL, *result_fiemap = NULL; |
271 | 355 | struct stat statinfo; |
@@ -363,7 +447,7 @@ static bool can_s2h(void) { |
363 | 447 |
|
364 | 448 | FOREACH_STRING(p, "suspend", "hibernate") { |
365 | 449 | r = can_sleep(p); |
366 | | - if (IN_SET(r, 0, -ENOSPC)) { |
| 450 | + if (IN_SET(r, 0, -ENOSPC, -EADV)) { |
367 | 451 | log_debug("Unable to %s system.", p); |
368 | 452 | return false; |
369 | 453 | } |
@@ -396,5 +480,10 @@ int can_sleep(const char *verb) { |
396 | 480 | if (!enough_swap_for_hibernation()) |
397 | 481 | return -ENOSPC; |
398 | 482 |
|
| 483 | + r = resume_configured(); |
| 484 | + if (r <= 0) |
| 485 | + /* We squash all errors (e.g. EPERM) into a single value for reporting. */ |
| 486 | + return -EADV; |
| 487 | + |
399 | 488 | return true; |
400 | 489 | } |
0 commit comments