1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright © 2018 Dell Inc.
7 //#include <linux/fs.h>
15 #include "alloc-util.h"
16 //#include "conf-parser.h"
18 //#include "env-util.h"
23 #include "parse-util.h"
24 #include "sleep-config.h"
25 #include "string-util.h"
28 #if 0 /// UNNEEDED by elogind
29 int parse_sleep_config(const char *verb, char ***_modes, char ***_states, usec_t *_delay) {
31 _cleanup_strv_free_ char
32 **suspend_mode = NULL, **suspend_state = NULL,
33 **hibernate_mode = NULL, **hibernate_state = NULL,
34 **hybrid_mode = NULL, **hybrid_state = NULL;
35 _cleanup_strv_free_ char **modes, **states; /* always initialized below */
36 usec_t delay = 180 * USEC_PER_MINUTE;
38 const ConfigTableItem items[] = {
39 { "Sleep", "SuspendMode", config_parse_strv, 0, &suspend_mode },
40 { "Sleep", "SuspendState", config_parse_strv, 0, &suspend_state },
41 { "Sleep", "HibernateMode", config_parse_strv, 0, &hibernate_mode },
42 { "Sleep", "HibernateState", config_parse_strv, 0, &hibernate_state },
43 { "Sleep", "HybridSleepMode", config_parse_strv, 0, &hybrid_mode },
44 { "Sleep", "HybridSleepState", config_parse_strv, 0, &hybrid_state },
45 { "Sleep", "HibernateDelaySec", config_parse_sec, 0, &delay},
49 (void) config_parse_many_nulstr(PKGSYSCONFDIR "/sleep.conf",
50 CONF_PATHS_NULSTR("elogind/sleep.conf.d"),
51 "Sleep\0", config_item_table_lookup, items,
52 CONFIG_PARSE_WARN, NULL);
54 if (streq(verb, "suspend")) {
55 /* empty by default */
56 modes = TAKE_PTR(suspend_mode);
59 states = TAKE_PTR(suspend_state);
61 states = strv_new("mem", "standby", "freeze", NULL);
63 } else if (streq(verb, "hibernate")) {
65 modes = TAKE_PTR(hibernate_mode);
67 modes = strv_new("platform", "shutdown", NULL);
70 states = TAKE_PTR(hibernate_state);
72 states = strv_new("disk", NULL);
74 } else if (streq(verb, "hybrid-sleep")) {
76 modes = TAKE_PTR(hybrid_mode);
78 modes = strv_new("suspend", "platform", "shutdown", NULL);
81 states = TAKE_PTR(hybrid_state);
83 states = strv_new("disk", NULL);
85 } else if (streq(verb, "suspend-then-hibernate"))
86 modes = states = NULL;
88 assert_not_reached("what verb");
90 if ((!modes && STR_IN_SET(verb, "hibernate", "hybrid-sleep")) ||
91 (!states && !streq(verb, "suspend-then-hibernate")))
95 *_modes = TAKE_PTR(modes);
97 *_states = TAKE_PTR(states);
105 #if 1 /// Only available in this file for elogind
108 int can_sleep_state(char **types) {
111 _cleanup_free_ char *p = NULL;
113 if (strv_isempty(types))
116 /* If /sys is read-only we cannot sleep */
117 if (access("/sys/power/state", W_OK) < 0)
120 r = read_one_line_file("/sys/power/state", &p);
124 STRV_FOREACH(type, types) {
125 const char *word, *state;
129 FOREACH_WORD_SEPARATOR(word, l, p, WHITESPACE, state)
130 if (l == k && memcmp(word, *type, l) == 0)
137 #if 1 /// Only available in this file for elogind
140 int can_sleep_disk(char **types) {
143 _cleanup_free_ char *p = NULL;
145 if (strv_isempty(types))
148 /* If /sys is read-only we cannot sleep */
149 if (access("/sys/power/disk", W_OK) < 0)
152 r = read_one_line_file("/sys/power/disk", &p);
156 STRV_FOREACH(type, types) {
157 const char *word, *state;
161 FOREACH_WORD_SEPARATOR(word, l, p, WHITESPACE, state) {
162 if (l == k && memcmp(word, *type, l) == 0)
167 memcmp(word + 1, *type, l - 2) == 0 &&
176 #define HIBERNATION_SWAP_THRESHOLD 0.98
178 int find_hibernate_location(char **device, char **type, size_t *size, size_t *used) {
179 _cleanup_fclose_ FILE *f;
182 f = fopen("/proc/swaps", "re");
184 log_full(errno == ENOENT ? LOG_DEBUG : LOG_WARNING,
185 "Failed to retrieve open /proc/swaps: %m");
190 (void) fscanf(f, "%*s %*s %*s %*s %*s\n");
193 _cleanup_free_ char *dev_field = NULL, *type_field = NULL;
194 size_t size_field, used_field;
198 "%ms " /* device/file */
199 "%ms " /* type of swap */
200 "%zu " /* swap size */
202 "%*i\n", /* priority */
203 &dev_field, &type_field, &size_field, &used_field);
208 log_warning("Failed to parse /proc/swaps:%u", i);
212 if (streq(type_field, "partition") && endswith(dev_field, "\\040(deleted)")) {
213 log_warning("Ignoring deleted swapfile '%s'.", dev_field);
217 *device = TAKE_PTR(dev_field);
219 *type = TAKE_PTR(type_field);
227 log_debug("No swap partitions were found.");
231 static bool enough_swap_for_hibernation(void) {
232 _cleanup_free_ char *active = NULL;
233 unsigned long long act = 0;
234 size_t size = 0, used = 0;
237 #if 0 /// elogind does not allow any bypass, we are never init!
238 if (getenv_bool("SYSTEMD_BYPASS_HIBERNATION_MEMORY_CHECK") > 0)
242 r = find_hibernate_location(NULL, NULL, &size, &used);
246 r = get_proc_field("/proc/meminfo", "Active(anon)", WHITESPACE, &active);
248 log_error_errno(r, "Failed to retrieve Active(anon) from /proc/meminfo: %m");
252 r = safe_atollu(active, &act);
254 log_error_errno(r, "Failed to parse Active(anon) from /proc/meminfo: %s: %m",
259 r = act <= (size - used) * HIBERNATION_SWAP_THRESHOLD;
260 log_debug("Hibernation is %spossible, Active(anon)=%llu kB, size=%zu kB, used=%zu kB, threshold=%.2g%%",
261 r ? "" : "im", act, size, used, 100*HIBERNATION_SWAP_THRESHOLD);
266 int read_fiemap(int fd, struct fiemap **ret) {
267 _cleanup_free_ struct fiemap *fiemap = NULL, *result_fiemap = NULL;
268 struct stat statinfo;
269 uint32_t result_extents = 0;
270 uint64_t fiemap_start = 0, fiemap_length;
271 const size_t n_extra = DIV_ROUND_UP(sizeof(struct fiemap), sizeof(struct fiemap_extent));
272 size_t fiemap_allocated = n_extra, result_fiemap_allocated = n_extra;
274 if (fstat(fd, &statinfo) < 0)
275 return log_debug_errno(errno, "Cannot determine file size: %m");
276 if (!S_ISREG(statinfo.st_mode))
278 fiemap_length = statinfo.st_size;
280 /* Zero this out in case we run on a file with no extents */
281 fiemap = calloc(n_extra, sizeof(struct fiemap_extent));
285 result_fiemap = malloc_multiply(n_extra, sizeof(struct fiemap_extent));
289 /* XFS filesystem has incorrect implementation of fiemap ioctl and
290 * returns extents for only one block-group at a time, so we need
291 * to handle it manually, starting the next fiemap call from the end
294 while (fiemap_start < fiemap_length) {
295 *fiemap = (struct fiemap) {
296 .fm_start = fiemap_start,
297 .fm_length = fiemap_length,
298 .fm_flags = FIEMAP_FLAG_SYNC,
301 /* Find out how many extents there are */
302 if (ioctl(fd, FS_IOC_FIEMAP, fiemap) < 0)
303 return log_debug_errno(errno, "Failed to read extents: %m");
305 /* Nothing to process */
306 if (fiemap->fm_mapped_extents == 0)
309 /* Resize fiemap to allow us to read in the extents, result fiemap has to hold all
310 * the extents for the whole file. Add space for the initial struct fiemap. */
311 if (!greedy_realloc0((void**) &fiemap, &fiemap_allocated,
312 n_extra + fiemap->fm_mapped_extents, sizeof(struct fiemap_extent)))
315 fiemap->fm_extent_count = fiemap->fm_mapped_extents;
316 fiemap->fm_mapped_extents = 0;
318 if (ioctl(fd, FS_IOC_FIEMAP, fiemap) < 0)
319 return log_debug_errno(errno, "Failed to read extents: %m");
321 /* Resize result_fiemap to allow us to copy in the extents */
322 if (!greedy_realloc((void**) &result_fiemap, &result_fiemap_allocated,
323 n_extra + result_extents + fiemap->fm_mapped_extents, sizeof(struct fiemap_extent)))
326 memcpy(result_fiemap->fm_extents + result_extents,
328 sizeof(struct fiemap_extent) * fiemap->fm_mapped_extents);
330 result_extents += fiemap->fm_mapped_extents;
332 /* Highly unlikely that it is zero */
333 if (_likely_(fiemap->fm_mapped_extents > 0)) {
334 uint32_t i = fiemap->fm_mapped_extents - 1;
336 fiemap_start = fiemap->fm_extents[i].fe_logical +
337 fiemap->fm_extents[i].fe_length;
339 if (fiemap->fm_extents[i].fe_flags & FIEMAP_EXTENT_LAST)
344 memcpy(result_fiemap, fiemap, sizeof(struct fiemap));
345 result_fiemap->fm_mapped_extents = result_extents;
346 *ret = TAKE_PTR(result_fiemap);
350 #if 0 /// elogind has to do, or better, *can* do it differently
351 static bool can_s2h(void) {
355 r = access("/sys/class/rtc/rtc0/wakealarm", W_OK);
357 log_full(errno == ENOENT ? LOG_DEBUG : LOG_WARNING,
358 "/sys/class/rct/rct0/wakealarm is not writable %m");
362 FOREACH_STRING(p, "suspend", "hibernate") {
364 if (IN_SET(r, 0, -ENOSPC)) {
365 log_debug("Unable to %s system.", p);
369 return log_debug_errno(r, "Failed to check if %s is possible: %m", p);
375 int can_sleep(Manager *m, const char *verb) {
377 assert(streq(verb, "suspend") ||
378 streq(verb, "hibernate") ||
379 streq(verb, "hybrid-sleep"));
380 int can_sleep(const char *verb) {
381 _cleanup_strv_free_ char **modes = NULL, **states = NULL;
384 if ( streq(verb, "suspend")
385 && ( !can_sleep_state(m->suspend_state)
386 || !can_sleep_disk(m->suspend_mode) ) )
388 assert(STR_IN_SET(verb, "suspend", "hibernate", "hybrid-sleep", "suspend-to-hibernate"));
389 assert(STR_IN_SET(verb, "suspend", "hibernate", "hybrid-sleep", "suspend-then-hibernate"));
391 if ( streq(verb, "hibernate")
392 && ( !can_sleep_state(m->hibernate_state)
393 || !can_sleep_disk(m->hibernate_mode) ) )
395 if (streq(verb, "suspend-to-hibernate"))
396 if (streq(verb, "suspend-then-hibernate"))
399 if ( streq(verb, "hybrid-sleep")
400 && ( !can_sleep_state(m->hybrid_sleep_state)
401 || !can_sleep_disk(m->hybrid_sleep_mode) ) )
402 r = parse_sleep_config(verb, &modes, &states, NULL);
406 if (!can_sleep_state(states) || !can_sleep_disk(modes))
409 return streq(verb, "suspend") || enough_memory_for_hibernation();
410 if (streq(verb, "suspend"))
413 if (!enough_memory_for_hibernation())
414 if (!enough_swap_for_hibernation())