]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/sleep-config.c
sleep-config: return correct error code
[thirdparty/systemd.git] / src / shared / sleep-config.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright © 2018 Dell Inc.
4 ***/
5
6 #include <errno.h>
7 #include <fcntl.h>
8 #include <linux/fs.h>
9 #include <stdbool.h>
10 #include <stddef.h>
11 #include <sys/ioctl.h>
12 #include <sys/stat.h>
13 #include <sys/types.h>
14 #include <syslog.h>
15 #include <unistd.h>
16
17 #include "alloc-util.h"
18 #include "blockdev-util.h"
19 #include "btrfs-util.h"
20 #include "conf-parser.h"
21 #include "def.h"
22 #include "env-util.h"
23 #include "errno-util.h"
24 #include "fd-util.h"
25 #include "fileio.h"
26 #include "log.h"
27 #include "macro.h"
28 #include "parse-util.h"
29 #include "path-util.h"
30 #include "sleep-config.h"
31 #include "stdio-util.h"
32 #include "string-util.h"
33 #include "strv.h"
34 #include "time-util.h"
35
36 int parse_sleep_config(SleepConfig **ret_sleep_config) {
37 _cleanup_(free_sleep_configp) SleepConfig *sc;
38 int allow_suspend = -1, allow_hibernate = -1,
39 allow_s2h = -1, allow_hybrid_sleep = -1;
40
41 sc = new0(SleepConfig, 1);
42 if (!sc)
43 return log_oom();
44
45 const ConfigTableItem items[] = {
46 { "Sleep", "AllowSuspend", config_parse_tristate, 0, &allow_suspend },
47 { "Sleep", "AllowHibernation", config_parse_tristate, 0, &allow_hibernate },
48 { "Sleep", "AllowSuspendThenHibernate", config_parse_tristate, 0, &allow_s2h },
49 { "Sleep", "AllowHybridSleep", config_parse_tristate, 0, &allow_hybrid_sleep },
50
51 { "Sleep", "SuspendMode", config_parse_strv, 0, &sc->suspend_modes },
52 { "Sleep", "SuspendState", config_parse_strv, 0, &sc->suspend_states },
53 { "Sleep", "HibernateMode", config_parse_strv, 0, &sc->hibernate_modes },
54 { "Sleep", "HibernateState", config_parse_strv, 0, &sc->hibernate_states },
55 { "Sleep", "HybridSleepMode", config_parse_strv, 0, &sc->hybrid_modes },
56 { "Sleep", "HybridSleepState", config_parse_strv, 0, &sc->hybrid_states },
57
58 { "Sleep", "HibernateDelaySec", config_parse_sec, 0, &sc->hibernate_delay_sec},
59 {}
60 };
61
62 (void) config_parse_many_nulstr(
63 PKGSYSCONFDIR "/sleep.conf",
64 CONF_PATHS_NULSTR("systemd/sleep.conf.d"),
65 "Sleep\0",
66 config_item_table_lookup, items,
67 CONFIG_PARSE_WARN,
68 NULL,
69 NULL);
70
71 /* use default values unless set */
72 sc->allow_suspend = allow_suspend != 0;
73 sc->allow_hibernate = allow_hibernate != 0;
74 sc->allow_hybrid_sleep = allow_hybrid_sleep >= 0 ? allow_hybrid_sleep
75 : (allow_suspend != 0 && allow_hibernate != 0);
76 sc->allow_s2h = allow_s2h >= 0 ? allow_s2h
77 : (allow_suspend != 0 && allow_hibernate != 0);
78
79 if (!sc->suspend_states)
80 sc->suspend_states = strv_new("mem", "standby", "freeze");
81 if (!sc->hibernate_modes)
82 sc->hibernate_modes = strv_new("platform", "shutdown");
83 if (!sc->hibernate_states)
84 sc->hibernate_states = strv_new("disk");
85 if (!sc->hybrid_modes)
86 sc->hybrid_modes = strv_new("suspend", "platform", "shutdown");
87 if (!sc->hybrid_states)
88 sc->hybrid_states = strv_new("disk");
89 if (sc->hibernate_delay_sec == 0)
90 sc->hibernate_delay_sec = 2 * USEC_PER_HOUR;
91
92 /* ensure values set for all required fields */
93 if (!sc->suspend_states || !sc->hibernate_modes
94 || !sc->hibernate_states || !sc->hybrid_modes || !sc->hybrid_states)
95 return log_oom();
96
97 *ret_sleep_config = TAKE_PTR(sc);
98
99 return 0;
100 }
101
102 int can_sleep_state(char **types) {
103 char **type;
104 int r;
105 _cleanup_free_ char *p = NULL;
106
107 if (strv_isempty(types))
108 return true;
109
110 /* If /sys is read-only we cannot sleep */
111 if (access("/sys/power/state", W_OK) < 0) {
112 log_debug_errno(errno, "/sys/power/state is not writable, cannot sleep: %m");
113 return false;
114 }
115
116 r = read_one_line_file("/sys/power/state", &p);
117 if (r < 0) {
118 log_debug_errno(r, "Failed to read /sys/power/state, cannot sleep: %m");
119 return false;
120 }
121
122 STRV_FOREACH(type, types) {
123 const char *word, *state;
124 size_t l, k;
125
126 k = strlen(*type);
127 FOREACH_WORD_SEPARATOR(word, l, p, WHITESPACE, state)
128 if (l == k && memcmp(word, *type, l) == 0)
129 return true;
130 }
131
132 return false;
133 }
134
135 int can_sleep_disk(char **types) {
136 _cleanup_free_ char *p = NULL;
137 char **type;
138 int r;
139
140 if (strv_isempty(types))
141 return true;
142
143 /* If /sys is read-only we cannot sleep */
144 if (access("/sys/power/disk", W_OK) < 0) {
145 log_debug_errno(errno, "/sys/power/disk is not writable: %m");
146 return false;
147 }
148
149 r = read_one_line_file("/sys/power/disk", &p);
150 if (r < 0) {
151 log_debug_errno(r, "Couldn't read /sys/power/disk: %m");
152 return false;
153 }
154
155 STRV_FOREACH(type, types) {
156 const char *word, *state;
157 size_t l, k;
158
159 k = strlen(*type);
160 FOREACH_WORD_SEPARATOR(word, l, p, WHITESPACE, state) {
161 if (l == k && memcmp(word, *type, l) == 0)
162 return true;
163
164 if (l == k + 2 &&
165 word[0] == '[' &&
166 memcmp(word + 1, *type, l - 2) == 0 &&
167 word[l-1] == ']')
168 return true;
169 }
170 }
171
172 return false;
173 }
174
175 #define HIBERNATION_SWAP_THRESHOLD 0.98
176
177 SwapEntry* swap_entry_free(SwapEntry *se) {
178 if (!se)
179 return NULL;
180
181 free(se->device);
182 free(se->type);
183
184 return mfree(se);
185 }
186
187 HibernateLocation* hibernate_location_free(HibernateLocation *hl) {
188 if (!hl)
189 return NULL;
190
191 swap_entry_free(hl->swap);
192
193 return mfree(hl);
194 }
195
196 static int swap_device_to_device_id(const SwapEntry *swap, dev_t *ret_dev) {
197 struct stat sb;
198 int r;
199
200 assert(swap);
201 assert(swap->device);
202 assert(swap->type);
203
204 r = stat(swap->device, &sb);
205 if (r < 0)
206 return -errno;
207
208 if (streq(swap->type, "partition")) {
209 if (!S_ISBLK(sb.st_mode))
210 return -ENOTBLK;
211
212 *ret_dev = sb.st_rdev;
213 return 0;
214 }
215
216 return get_block_device(swap->device, ret_dev);
217 }
218
219 /*
220 * Attempt to calculate the swap file offset on supported filesystems. On unsupported
221 * filesystems, a debug message is logged and ret_offset is set to UINT64_MAX.
222 */
223 static int calculate_swap_file_offset(const SwapEntry *swap, uint64_t *ret_offset) {
224 _cleanup_close_ int fd = -1;
225 _cleanup_free_ struct fiemap *fiemap = NULL;
226 struct stat sb;
227 int r, btrfs;
228
229 assert(swap);
230 assert(swap->device);
231 assert(streq(swap->type, "file"));
232
233 fd = open(swap->device, O_RDONLY|O_CLOEXEC|O_NOCTTY);
234 if (fd < 0)
235 return log_error_errno(errno, "Failed to open swap file %s to determine on-disk offset: %m", swap->device);
236
237 if (fstat(fd, &sb) < 0)
238 return log_error_errno(errno, "Failed to stat %s: %m", swap->device);
239
240 btrfs = btrfs_is_filesystem(fd);
241 if (btrfs < 0)
242 return log_error_errno(btrfs, "Error checking %s for Btrfs filesystem: %m", swap->device);
243 else if (btrfs > 0) {
244 log_debug("%s: detection of swap file offset on Btrfs is not supported", swap->device);
245 *ret_offset = UINT64_MAX;
246 return 0;
247 }
248
249 r = read_fiemap(fd, &fiemap);
250 if (r < 0)
251 return log_debug_errno(r, "Unable to read extent map for '%s': %m", swap->device);
252
253 *ret_offset = fiemap->fm_extents[0].fe_physical / page_size();
254
255 return 0;
256 }
257
258 static int read_resume_files(dev_t *ret_resume, uint64_t *ret_resume_offset) {
259 _cleanup_free_ char *resume_str = NULL, *resume_offset_str = NULL;
260 dev_t resume;
261 uint64_t resume_offset = 0;
262 int r;
263
264 r = read_one_line_file("/sys/power/resume", &resume_str);
265 if (r < 0)
266 return log_debug_errno(r, "Error reading /sys/power/resume: %m");
267
268 r = parse_dev(resume_str, &resume);
269 if (r < 0)
270 return log_debug_errno(r, "Error parsing /sys/power/resume device: %s: %m", resume_str);
271
272 r = read_one_line_file("/sys/power/resume_offset", &resume_offset_str);
273 if (r == -ENOENT)
274 log_debug("Kernel does not support resume_offset; swap file offset detection will be skipped.");
275 else if (r < 0)
276 return log_debug_errno(r, "Error reading /sys/power/resume_offset: %m");
277 else {
278 r = safe_atou64(resume_offset_str, &resume_offset);
279 if (r < 0)
280 return log_error_errno(r, "Failed to parse value in /sys/power/resume_offset \"%s\": %m", resume_offset_str);
281 }
282
283 if (resume_offset > 0 && resume == 0)
284 log_debug("Warning: found /sys/power/resume_offset==%" PRIu64 ", but /sys/power/resume unset. Misconfiguration?",
285 resume_offset);
286
287 *ret_resume = resume;
288 *ret_resume_offset = resume_offset;
289
290 return 0;
291 }
292
293 /*
294 * Determine if the HibernateLocation matches the resume= (device) and resume_offset= (file).
295 */
296 static bool location_is_resume_device(const HibernateLocation *location, dev_t sys_resume, uint64_t sys_offset) {
297 if (!location)
298 return false;
299
300 return sys_resume > 0 &&
301 sys_resume == location->devno &&
302 (sys_offset == location->offset || (sys_offset > 0 && location->offset == UINT64_MAX));
303 }
304
305 /*
306 * Attempt to find the hibernation location by parsing /proc/swaps, /sys/power/resume, and
307 * /sys/power/resume_offset.
308 *
309 * Returns:
310 * 1 - Values are set in /sys/power/resume and /sys/power/resume_offset.
311 * ret_hibernate_location will represent matching /proc/swap entry if identified or NULL if not.
312 *
313 * 0 - No values are set in /sys/power/resume and /sys/power/resume_offset.
314 ret_hibernate_location will represent the highest priority swap with most remaining space discovered in /proc/swaps.
315 *
316 * Negative value in the case of error.
317 */
318 int find_hibernate_location(HibernateLocation **ret_hibernate_location) {
319 _cleanup_fclose_ FILE *f = NULL;
320 _cleanup_(hibernate_location_freep) HibernateLocation *hibernate_location = NULL;
321 dev_t sys_resume;
322 uint64_t sys_offset = 0;
323 bool resume_match = false;
324 int r;
325
326 /* read the /sys/power/resume & /sys/power/resume_offset values */
327 r = read_resume_files(&sys_resume, &sys_offset);
328 if (r < 0)
329 return r;
330
331 f = fopen("/proc/swaps", "re");
332 if (!f) {
333 log_full(errno == ENOENT ? LOG_DEBUG : LOG_WARNING,
334 "Failed to open /proc/swaps: %m");
335 return negative_errno();
336 }
337
338 (void) fscanf(f, "%*s %*s %*s %*s %*s\n");
339 for (unsigned i = 1;; i++) {
340 _cleanup_(swap_entry_freep) SwapEntry *swap = NULL;
341 uint64_t swap_offset = 0;
342 int k;
343
344 swap = new0(SwapEntry, 1);
345 if (!swap)
346 return log_oom();
347
348 k = fscanf(f,
349 "%ms " /* device/file */
350 "%ms " /* type of swap */
351 "%" PRIu64 /* swap size */
352 "%" PRIu64 /* used */
353 "%i\n", /* priority */
354 &swap->device, &swap->type, &swap->size, &swap->used, &swap->priority);
355 if (k == EOF)
356 break;
357 if (k != 5) {
358 log_warning("Failed to parse /proc/swaps:%u", i);
359 continue;
360 }
361
362 if (streq(swap->type, "file")) {
363 if (endswith(swap->device, "\\040(deleted)")) {
364 log_warning("Ignoring deleted swap file '%s'.", swap->device);
365 continue;
366 }
367
368 r = calculate_swap_file_offset(swap, &swap_offset);
369 if (r < 0)
370 return r;
371
372 } else if (streq(swap->type, "partition")) {
373 const char *fn;
374
375 fn = path_startswith(swap->device, "/dev/");
376 if (fn && startswith(fn, "zram")) {
377 log_debug("%s: ignoring zram swap", swap->device);
378 continue;
379 }
380
381 } else {
382 log_debug("%s: swap type %s is unsupported for hibernation, ignoring", swap->device, swap->type);
383 continue;
384 }
385
386 /* prefer resume device or highest priority swap with most remaining space */
387 if (hibernate_location && swap->priority < hibernate_location->swap->priority) {
388 log_debug("%s: ignoring device with lower priority", swap->device);
389 continue;
390 }
391 if (hibernate_location &&
392 (swap->priority == hibernate_location->swap->priority
393 && swap->size - swap->used < hibernate_location->swap->size - hibernate_location->swap->used)) {
394 log_debug("%s: ignoring device with lower usable space", swap->device);
395 continue;
396 }
397
398 dev_t swap_device;
399 r = swap_device_to_device_id(swap, &swap_device);
400 if (r < 0)
401 return log_error_errno(r, "%s: failed to query device number: %m", swap->device);
402
403 hibernate_location = hibernate_location_free(hibernate_location);
404 hibernate_location = new(HibernateLocation, 1);
405 if (!hibernate_location)
406 return log_oom();
407
408 *hibernate_location = (HibernateLocation) {
409 .devno = swap_device,
410 .offset = swap_offset,
411 .swap = TAKE_PTR(swap),
412 };
413
414 /* if the swap is the resume device, stop the loop */
415 if (location_is_resume_device(hibernate_location, sys_resume, sys_offset)) {
416 log_debug("%s: device matches configured resume settings.", hibernate_location->swap->device);
417 resume_match = true;
418 break;
419 }
420
421 log_debug("%s: is a candidate device.", hibernate_location->swap->device);
422 }
423
424 /* We found nothing at all */
425 if (!hibernate_location)
426 return log_debug_errno(SYNTHETIC_ERRNO(ENOSYS),
427 "No possible swap partitions or files suitable for hibernation were found in /proc/swaps.");
428
429 /* resume= is set but a matching /proc/swaps entry was not found */
430 if (sys_resume != 0 && !resume_match)
431 return log_debug_errno(SYNTHETIC_ERRNO(ENOSYS),
432 "No swap partitions or files matching resume config were found in /proc/swaps.");
433
434 if (hibernate_location->offset == UINT64_MAX) {
435 if (sys_offset == 0)
436 return log_debug_errno(SYNTHETIC_ERRNO(ENOSYS), "Offset detection failed and /sys/power/resume_offset is not set.");
437
438 hibernate_location->offset = sys_offset;
439 }
440
441 if (resume_match)
442 log_debug("Hibernation will attempt to use swap entry with path: %s, device: %u:%u, offset: %" PRIu64 ", priority: %i",
443 hibernate_location->swap->device, major(hibernate_location->devno), minor(hibernate_location->devno),
444 hibernate_location->offset, hibernate_location->swap->priority);
445 else
446 log_debug("/sys/power/resume is not configured; attempting to hibernate with path: %s, device: %u:%u, offset: %" PRIu64 ", priority: %i",
447 hibernate_location->swap->device, major(hibernate_location->devno), minor(hibernate_location->devno),
448 hibernate_location->offset, hibernate_location->swap->priority);
449
450 *ret_hibernate_location = TAKE_PTR(hibernate_location);
451
452 if (resume_match)
453 return 1;
454
455 return 0;
456 }
457
458 static bool enough_swap_for_hibernation(void) {
459 _cleanup_free_ char *active = NULL;
460 _cleanup_(hibernate_location_freep) HibernateLocation *hibernate_location = NULL;
461 unsigned long long act = 0;
462 int r;
463
464 if (getenv_bool("SYSTEMD_BYPASS_HIBERNATION_MEMORY_CHECK") > 0)
465 return true;
466
467 r = find_hibernate_location(&hibernate_location);
468 if (r < 0)
469 return false;
470
471 /* If /sys/power/{resume,resume_offset} is configured but a matching entry
472 * could not be identified in /proc/swaps, user is likely using Btrfs with a swapfile;
473 * return true and let the system attempt hibernation.
474 */
475 if (r > 0 && !hibernate_location) {
476 log_debug("Unable to determine remaining swap space; hibernation may fail");
477 return true;
478 }
479
480 if (!hibernate_location)
481 return false;
482
483 r = get_proc_field("/proc/meminfo", "Active(anon)", WHITESPACE, &active);
484 if (r < 0) {
485 log_debug_errno(r, "Failed to retrieve Active(anon) from /proc/meminfo: %m");
486 return false;
487 }
488
489 r = safe_atollu(active, &act);
490 if (r < 0) {
491 log_debug_errno(r, "Failed to parse Active(anon) from /proc/meminfo: %s: %m", active);
492 return false;
493 }
494
495 r = act <= (hibernate_location->swap->size - hibernate_location->swap->used) * HIBERNATION_SWAP_THRESHOLD;
496 log_debug("%s swap for hibernation, Active(anon)=%llu kB, size=%" PRIu64 " kB, used=%" PRIu64 " kB, threshold=%.2g%%",
497 r ? "Enough" : "Not enough", act, hibernate_location->swap->size, hibernate_location->swap->used, 100*HIBERNATION_SWAP_THRESHOLD);
498
499 return r;
500 }
501
502 int read_fiemap(int fd, struct fiemap **ret) {
503 _cleanup_free_ struct fiemap *fiemap = NULL, *result_fiemap = NULL;
504 struct stat statinfo;
505 uint32_t result_extents = 0;
506 uint64_t fiemap_start = 0, fiemap_length;
507 const size_t n_extra = DIV_ROUND_UP(sizeof(struct fiemap), sizeof(struct fiemap_extent));
508 size_t fiemap_allocated = n_extra, result_fiemap_allocated = n_extra;
509
510 if (fstat(fd, &statinfo) < 0)
511 return log_debug_errno(errno, "Cannot determine file size: %m");
512 if (!S_ISREG(statinfo.st_mode))
513 return -ENOTTY;
514 fiemap_length = statinfo.st_size;
515
516 /* Zero this out in case we run on a file with no extents */
517 fiemap = calloc(n_extra, sizeof(struct fiemap_extent));
518 if (!fiemap)
519 return -ENOMEM;
520
521 result_fiemap = malloc_multiply(n_extra, sizeof(struct fiemap_extent));
522 if (!result_fiemap)
523 return -ENOMEM;
524
525 /* XFS filesystem has incorrect implementation of fiemap ioctl and
526 * returns extents for only one block-group at a time, so we need
527 * to handle it manually, starting the next fiemap call from the end
528 * of the last extent
529 */
530 while (fiemap_start < fiemap_length) {
531 *fiemap = (struct fiemap) {
532 .fm_start = fiemap_start,
533 .fm_length = fiemap_length,
534 .fm_flags = FIEMAP_FLAG_SYNC,
535 };
536
537 /* Find out how many extents there are */
538 if (ioctl(fd, FS_IOC_FIEMAP, fiemap) < 0)
539 return log_debug_errno(errno, "Failed to read extents: %m");
540
541 /* Nothing to process */
542 if (fiemap->fm_mapped_extents == 0)
543 break;
544
545 /* Resize fiemap to allow us to read in the extents, result fiemap has to hold all
546 * the extents for the whole file. Add space for the initial struct fiemap. */
547 if (!greedy_realloc0((void**) &fiemap, &fiemap_allocated,
548 n_extra + fiemap->fm_mapped_extents, sizeof(struct fiemap_extent)))
549 return -ENOMEM;
550
551 fiemap->fm_extent_count = fiemap->fm_mapped_extents;
552 fiemap->fm_mapped_extents = 0;
553
554 if (ioctl(fd, FS_IOC_FIEMAP, fiemap) < 0)
555 return log_debug_errno(errno, "Failed to read extents: %m");
556
557 /* Resize result_fiemap to allow us to copy in the extents */
558 if (!greedy_realloc((void**) &result_fiemap, &result_fiemap_allocated,
559 n_extra + result_extents + fiemap->fm_mapped_extents, sizeof(struct fiemap_extent)))
560 return -ENOMEM;
561
562 memcpy(result_fiemap->fm_extents + result_extents,
563 fiemap->fm_extents,
564 sizeof(struct fiemap_extent) * fiemap->fm_mapped_extents);
565
566 result_extents += fiemap->fm_mapped_extents;
567
568 /* Highly unlikely that it is zero */
569 if (_likely_(fiemap->fm_mapped_extents > 0)) {
570 uint32_t i = fiemap->fm_mapped_extents - 1;
571
572 fiemap_start = fiemap->fm_extents[i].fe_logical +
573 fiemap->fm_extents[i].fe_length;
574
575 if (fiemap->fm_extents[i].fe_flags & FIEMAP_EXTENT_LAST)
576 break;
577 }
578 }
579
580 memcpy(result_fiemap, fiemap, sizeof(struct fiemap));
581 result_fiemap->fm_mapped_extents = result_extents;
582 *ret = TAKE_PTR(result_fiemap);
583 return 0;
584 }
585
586 static int can_sleep_internal(const char *verb, bool check_allowed, const SleepConfig *sleep_config);
587
588 static bool can_s2h(const SleepConfig *sleep_config) {
589 const char *p;
590 int r;
591
592 if (!clock_supported(CLOCK_BOOTTIME_ALARM)) {
593 log_full(errno == ENOENT ? LOG_DEBUG : LOG_WARNING,
594 "CLOCK_BOOTTIME_ALARM is not supported");
595 return false;
596 }
597
598 FOREACH_STRING(p, "suspend", "hibernate") {
599 r = can_sleep_internal(p, false, sleep_config);
600 if (IN_SET(r, 0, -ENOSPC, -EADV)) {
601 log_debug("Unable to %s system.", p);
602 return false;
603 }
604 if (r < 0)
605 return log_debug_errno(r, "Failed to check if %s is possible: %m", p);
606 }
607
608 return true;
609 }
610
611 static int can_sleep_internal(const char *verb, bool check_allowed, const SleepConfig *sleep_config) {
612 bool allow;
613 char **modes = NULL, **states = NULL;
614 int r;
615
616 assert(STR_IN_SET(verb, "suspend", "hibernate", "hybrid-sleep", "suspend-then-hibernate"));
617
618 r = sleep_settings(verb, sleep_config, &allow, &modes, &states);
619 if (r < 0)
620 return false;
621
622 if (check_allowed && !allow) {
623 log_debug("Sleep mode \"%s\" is disabled by configuration.", verb);
624 return false;
625 }
626
627 if (streq(verb, "suspend-then-hibernate"))
628 return can_s2h(sleep_config);
629
630 if (!can_sleep_state(states) || !can_sleep_disk(modes))
631 return false;
632
633 if (streq(verb, "suspend"))
634 return true;
635
636 if (!enough_swap_for_hibernation())
637 return -ENOSPC;
638
639 return true;
640 }
641
642 int can_sleep(const char *verb) {
643 _cleanup_(free_sleep_configp) SleepConfig *sleep_config = NULL;
644 int r;
645
646 r = parse_sleep_config(&sleep_config);
647 if (r < 0)
648 return r;
649
650 return can_sleep_internal(verb, true, sleep_config);
651 }
652
653 int sleep_settings(const char *verb, const SleepConfig *sleep_config, bool *ret_allow, char ***ret_modes, char ***ret_states) {
654
655 assert(verb);
656 assert(sleep_config);
657 assert(STR_IN_SET(verb, "suspend", "hibernate", "hybrid-sleep", "suspend-then-hibernate"));
658
659 if (streq(verb, "suspend")) {
660 *ret_allow = sleep_config->allow_suspend;
661 *ret_modes = sleep_config->suspend_modes;
662 *ret_states = sleep_config->suspend_states;
663 } else if (streq(verb, "hibernate")) {
664 *ret_allow = sleep_config->allow_hibernate;
665 *ret_modes = sleep_config->hibernate_modes;
666 *ret_states = sleep_config->hibernate_states;
667 } else if (streq(verb, "hybrid-sleep")) {
668 *ret_allow = sleep_config->allow_hybrid_sleep;
669 *ret_modes = sleep_config->hybrid_modes;
670 *ret_states = sleep_config->hybrid_states;
671 } else if (streq(verb, "suspend-then-hibernate")) {
672 *ret_allow = sleep_config->allow_s2h;
673 *ret_modes = *ret_states = NULL;
674 }
675
676 /* suspend modes empty by default */
677 if ((!ret_modes && !streq(verb, "suspend")) || !ret_states)
678 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No modes or states set for %s; Check sleep.conf", verb);
679
680 return 0;
681 }
682
683 void free_sleep_config(SleepConfig *sc) {
684 if (!sc)
685 return;
686
687 strv_free(sc->suspend_modes);
688 strv_free(sc->suspend_states);
689
690 strv_free(sc->hibernate_modes);
691 strv_free(sc->hibernate_states);
692
693 strv_free(sc->hybrid_modes);
694 strv_free(sc->hybrid_states);
695
696 free(sc);
697 }