Skip to content
This repository was archived by the owner on Aug 6, 2020. It is now read-only.

Commit 6789dca

Browse files
committed
shared/sleep-config: forbid hibernation if resume= is not configured
(cherry picked from commit 5fdf2d5)
1 parent 304dd73 commit 6789dca

2 files changed

Lines changed: 96 additions & 6 deletions

File tree

‎src/login/logind-dbus.c‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1770,8 +1770,9 @@ static int method_do_shutdown_or_sleep(
17701770
if (sleep_verb) {
17711771
r = can_sleep(sleep_verb);
17721772
if (r == -ENOSPC)
1773-
return sd_bus_error_set(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED,
1774-
"Not enough swap space for hibernation");
1773+
return sd_bus_error_set(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED, "Not enough swap space for hibernation");
1774+
if (r == -EADV)
1775+
return sd_bus_error_set(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED, "Resume not configured, can't hibernate");
17751776
if (r == 0)
17761777
return sd_bus_error_setf(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED,
17771778
"Sleep verb \"%s\" not supported", sleep_verb);
@@ -2199,7 +2200,7 @@ static int method_can_shutdown_or_sleep(
21992200

22002201
if (sleep_verb) {
22012202
r = can_sleep(sleep_verb);
2202-
if (IN_SET(r, 0, -ENOSPC))
2203+
if (IN_SET(r, 0, -ENOSPC, -EADV))
22032204
return sd_bus_reply_method_return(message, "s", "na");
22042205
if (r < 0)
22052206
return r;

‎src/shared/sleep-config.c‎

Lines changed: 92 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <unistd.h>
1414

1515
#include "alloc-util.h"
16+
#include "bootspec.h"
1617
#include "conf-parser.h"
1718
#include "def.h"
1819
#include "env-util.h"
@@ -22,6 +23,7 @@
2223
#include "macro.h"
2324
#include "parse-util.h"
2425
#include "path-util.h"
26+
#include "proc-cmdline.h"
2527
#include "sleep-config.h"
2628
#include "string-util.h"
2729
#include "strv.h"
@@ -260,12 +262,94 @@ static bool enough_swap_for_hibernation(void) {
260262
}
261263

262264
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);
265267

266268
return r;
267269
}
268270

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+
269353
int read_fiemap(int fd, struct fiemap **ret) {
270354
_cleanup_free_ struct fiemap *fiemap = NULL, *result_fiemap = NULL;
271355
struct stat statinfo;
@@ -363,7 +447,7 @@ static bool can_s2h(void) {
363447

364448
FOREACH_STRING(p, "suspend", "hibernate") {
365449
r = can_sleep(p);
366-
if (IN_SET(r, 0, -ENOSPC)) {
450+
if (IN_SET(r, 0, -ENOSPC, -EADV)) {
367451
log_debug("Unable to %s system.", p);
368452
return false;
369453
}
@@ -396,5 +480,10 @@ int can_sleep(const char *verb) {
396480
if (!enough_swap_for_hibernation())
397481
return -ENOSPC;
398482

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+
399488
return true;
400489
}

0 commit comments

Comments
 (0)