1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright © 2018 Dell Inc.
12 #include <sys/ioctl.h>
16 #include "alloc-util.h"
17 #include "conf-parser.h"
24 #include "parse-util.h"
25 #include "path-util.h"
26 #include "sleep-config.h"
27 #include "string-util.h"
30 int parse_sleep_config(const char *verb
, bool *ret_allow
, char ***ret_modes
, char ***ret_states
, usec_t
*ret_delay
) {
31 int allow_suspend
= -1, allow_hibernate
= -1,
32 allow_s2h
= -1, allow_hybrid_sleep
= -1;
34 _cleanup_strv_free_
char
35 **suspend_mode
= NULL
, **suspend_state
= NULL
,
36 **hibernate_mode
= NULL
, **hibernate_state
= NULL
,
37 **hybrid_mode
= NULL
, **hybrid_state
= NULL
;
38 _cleanup_strv_free_
char **modes
, **states
; /* always initialized below */
39 usec_t delay
= 180 * USEC_PER_MINUTE
;
41 const ConfigTableItem items
[] = {
42 { "Sleep", "AllowSuspend", config_parse_tristate
, 0, &allow_suspend
},
43 { "Sleep", "AllowHibernation", config_parse_tristate
, 0, &allow_hibernate
},
44 { "Sleep", "AllowSuspendThenHibernate", config_parse_tristate
, 0, &allow_s2h
},
45 { "Sleep", "AllowHybridSleep", config_parse_tristate
, 0, &allow_hybrid_sleep
},
47 { "Sleep", "SuspendMode", config_parse_strv
, 0, &suspend_mode
},
48 { "Sleep", "SuspendState", config_parse_strv
, 0, &suspend_state
},
49 { "Sleep", "HibernateMode", config_parse_strv
, 0, &hibernate_mode
},
50 { "Sleep", "HibernateState", config_parse_strv
, 0, &hibernate_state
},
51 { "Sleep", "HybridSleepMode", config_parse_strv
, 0, &hybrid_mode
},
52 { "Sleep", "HybridSleepState", config_parse_strv
, 0, &hybrid_state
},
54 { "Sleep", "HibernateDelaySec", config_parse_sec
, 0, &delay
},
58 (void) config_parse_many_nulstr(PKGSYSCONFDIR
"/sleep.conf",
59 CONF_PATHS_NULSTR("systemd/sleep.conf.d"),
60 "Sleep\0", config_item_table_lookup
, items
,
61 CONFIG_PARSE_WARN
, NULL
);
63 if (streq(verb
, "suspend")) {
64 allow
= allow_suspend
!= 0;
66 /* empty by default */
67 modes
= TAKE_PTR(suspend_mode
);
70 states
= TAKE_PTR(suspend_state
);
72 states
= strv_new("mem", "standby", "freeze");
74 } else if (streq(verb
, "hibernate")) {
75 allow
= allow_hibernate
!= 0;
78 modes
= TAKE_PTR(hibernate_mode
);
80 modes
= strv_new("platform", "shutdown");
83 states
= TAKE_PTR(hibernate_state
);
85 states
= strv_new("disk");
87 } else if (streq(verb
, "hybrid-sleep")) {
88 allow
= allow_hybrid_sleep
> 0 ||
89 (allow_suspend
!= 0 && allow_hibernate
!= 0);
92 modes
= TAKE_PTR(hybrid_mode
);
94 modes
= strv_new("suspend", "platform", "shutdown");
97 states
= TAKE_PTR(hybrid_state
);
99 states
= strv_new("disk");
101 } else if (streq(verb
, "suspend-then-hibernate")) {
102 allow
= allow_s2h
> 0 ||
103 (allow_suspend
!= 0 && allow_hibernate
!= 0);
105 modes
= states
= NULL
;
107 assert_not_reached("what verb");
109 if ((!modes
&& STR_IN_SET(verb
, "hibernate", "hybrid-sleep")) ||
110 (!states
&& !streq(verb
, "suspend-then-hibernate")))
116 *ret_modes
= TAKE_PTR(modes
);
118 *ret_states
= TAKE_PTR(states
);
125 int can_sleep_state(char **types
) {
128 _cleanup_free_
char *p
= NULL
;
130 if (strv_isempty(types
))
133 /* If /sys is read-only we cannot sleep */
134 if (access("/sys/power/state", W_OK
) < 0)
137 r
= read_one_line_file("/sys/power/state", &p
);
141 STRV_FOREACH(type
, types
) {
142 const char *word
, *state
;
146 FOREACH_WORD_SEPARATOR(word
, l
, p
, WHITESPACE
, state
)
147 if (l
== k
&& memcmp(word
, *type
, l
) == 0)
154 int can_sleep_disk(char **types
) {
157 _cleanup_free_
char *p
= NULL
;
159 if (strv_isempty(types
))
162 /* If /sys is read-only we cannot sleep */
163 if (access("/sys/power/disk", W_OK
) < 0) {
164 log_debug_errno(errno
, "/sys/power/disk is not writable: %m");
168 r
= read_one_line_file("/sys/power/disk", &p
);
170 log_debug_errno(r
, "Couldn't read /sys/power/disk: %m");
174 STRV_FOREACH(type
, types
) {
175 const char *word
, *state
;
179 FOREACH_WORD_SEPARATOR(word
, l
, p
, WHITESPACE
, state
) {
180 if (l
== k
&& memcmp(word
, *type
, l
) == 0)
185 memcmp(word
+ 1, *type
, l
- 2) == 0 &&
194 #define HIBERNATION_SWAP_THRESHOLD 0.98
196 int find_hibernate_location(char **device
, char **type
, size_t *size
, size_t *used
) {
197 _cleanup_fclose_
FILE *f
;
200 f
= fopen("/proc/swaps", "re");
202 log_full(errno
== ENOENT
? LOG_DEBUG
: LOG_WARNING
,
203 "Failed to retrieve open /proc/swaps: %m");
208 (void) fscanf(f
, "%*s %*s %*s %*s %*s\n");
211 _cleanup_free_
char *dev_field
= NULL
, *type_field
= NULL
;
212 size_t size_field
, used_field
;
216 "%ms " /* device/file */
217 "%ms " /* type of swap */
218 "%zu " /* swap size */
220 "%*i\n", /* priority */
221 &dev_field
, &type_field
, &size_field
, &used_field
);
225 log_warning("Failed to parse /proc/swaps:%u", i
);
229 if (streq(type_field
, "file")) {
231 if (endswith(dev_field
, "\\040(deleted)")) {
232 log_warning("Ignoring deleted swap file '%s'.", dev_field
);
236 } else if (streq(type_field
, "partition")) {
239 fn
= path_startswith(dev_field
, "/dev/");
240 if (fn
&& startswith(fn
, "zram")) {
241 log_debug("Ignoring compressed RAM swap device '%s'.", dev_field
);
247 *device
= TAKE_PTR(dev_field
);
249 *type
= TAKE_PTR(type_field
);
257 return log_debug_errno(SYNTHETIC_ERRNO(ENOSYS
),
258 "No swap partitions were found.");
261 static bool enough_swap_for_hibernation(void) {
262 _cleanup_free_
char *active
= NULL
;
263 unsigned long long act
= 0;
264 size_t size
= 0, used
= 0;
267 if (getenv_bool("SYSTEMD_BYPASS_HIBERNATION_MEMORY_CHECK") > 0)
270 r
= find_hibernate_location(NULL
, NULL
, &size
, &used
);
274 r
= get_proc_field("/proc/meminfo", "Active(anon)", WHITESPACE
, &active
);
276 log_debug_errno(r
, "Failed to retrieve Active(anon) from /proc/meminfo: %m");
280 r
= safe_atollu(active
, &act
);
282 log_debug_errno(r
, "Failed to parse Active(anon) from /proc/meminfo: %s: %m", active
);
286 r
= act
<= (size
- used
) * HIBERNATION_SWAP_THRESHOLD
;
287 log_debug("%s swap for hibernation, Active(anon)=%llu kB, size=%zu kB, used=%zu kB, threshold=%.2g%%",
288 r
? "Enough" : "Not enough", act
, size
, used
, 100*HIBERNATION_SWAP_THRESHOLD
);
293 int read_fiemap(int fd
, struct fiemap
**ret
) {
294 _cleanup_free_
struct fiemap
*fiemap
= NULL
, *result_fiemap
= NULL
;
295 struct stat statinfo
;
296 uint32_t result_extents
= 0;
297 uint64_t fiemap_start
= 0, fiemap_length
;
298 const size_t n_extra
= DIV_ROUND_UP(sizeof(struct fiemap
), sizeof(struct fiemap_extent
));
299 size_t fiemap_allocated
= n_extra
, result_fiemap_allocated
= n_extra
;
301 if (fstat(fd
, &statinfo
) < 0)
302 return log_debug_errno(errno
, "Cannot determine file size: %m");
303 if (!S_ISREG(statinfo
.st_mode
))
305 fiemap_length
= statinfo
.st_size
;
307 /* Zero this out in case we run on a file with no extents */
308 fiemap
= calloc(n_extra
, sizeof(struct fiemap_extent
));
312 result_fiemap
= malloc_multiply(n_extra
, sizeof(struct fiemap_extent
));
316 /* XFS filesystem has incorrect implementation of fiemap ioctl and
317 * returns extents for only one block-group at a time, so we need
318 * to handle it manually, starting the next fiemap call from the end
321 while (fiemap_start
< fiemap_length
) {
322 *fiemap
= (struct fiemap
) {
323 .fm_start
= fiemap_start
,
324 .fm_length
= fiemap_length
,
325 .fm_flags
= FIEMAP_FLAG_SYNC
,
328 /* Find out how many extents there are */
329 if (ioctl(fd
, FS_IOC_FIEMAP
, fiemap
) < 0)
330 return log_debug_errno(errno
, "Failed to read extents: %m");
332 /* Nothing to process */
333 if (fiemap
->fm_mapped_extents
== 0)
336 /* Resize fiemap to allow us to read in the extents, result fiemap has to hold all
337 * the extents for the whole file. Add space for the initial struct fiemap. */
338 if (!greedy_realloc0((void**) &fiemap
, &fiemap_allocated
,
339 n_extra
+ fiemap
->fm_mapped_extents
, sizeof(struct fiemap_extent
)))
342 fiemap
->fm_extent_count
= fiemap
->fm_mapped_extents
;
343 fiemap
->fm_mapped_extents
= 0;
345 if (ioctl(fd
, FS_IOC_FIEMAP
, fiemap
) < 0)
346 return log_debug_errno(errno
, "Failed to read extents: %m");
348 /* Resize result_fiemap to allow us to copy in the extents */
349 if (!greedy_realloc((void**) &result_fiemap
, &result_fiemap_allocated
,
350 n_extra
+ result_extents
+ fiemap
->fm_mapped_extents
, sizeof(struct fiemap_extent
)))
353 memcpy(result_fiemap
->fm_extents
+ result_extents
,
355 sizeof(struct fiemap_extent
) * fiemap
->fm_mapped_extents
);
357 result_extents
+= fiemap
->fm_mapped_extents
;
359 /* Highly unlikely that it is zero */
360 if (_likely_(fiemap
->fm_mapped_extents
> 0)) {
361 uint32_t i
= fiemap
->fm_mapped_extents
- 1;
363 fiemap_start
= fiemap
->fm_extents
[i
].fe_logical
+
364 fiemap
->fm_extents
[i
].fe_length
;
366 if (fiemap
->fm_extents
[i
].fe_flags
& FIEMAP_EXTENT_LAST
)
371 memcpy(result_fiemap
, fiemap
, sizeof(struct fiemap
));
372 result_fiemap
->fm_mapped_extents
= result_extents
;
373 *ret
= TAKE_PTR(result_fiemap
);
377 static int can_sleep_internal(const char *verb
, bool check_allowed
);
379 static bool can_s2h(void) {
383 r
= access("/sys/class/rtc/rtc0/wakealarm", W_OK
);
385 log_full(errno
== ENOENT
? LOG_DEBUG
: LOG_WARNING
,
386 "/sys/class/rct/rct0/wakealarm is not writable %m");
390 FOREACH_STRING(p
, "suspend", "hibernate") {
391 r
= can_sleep_internal(p
, false);
392 if (IN_SET(r
, 0, -ENOSPC
, -EADV
)) {
393 log_debug("Unable to %s system.", p
);
397 return log_debug_errno(r
, "Failed to check if %s is possible: %m", p
);
403 static int can_sleep_internal(const char *verb
, bool check_allowed
) {
405 _cleanup_strv_free_
char **modes
= NULL
, **states
= NULL
;
408 assert(STR_IN_SET(verb
, "suspend", "hibernate", "hybrid-sleep", "suspend-then-hibernate"));
410 r
= parse_sleep_config(verb
, &allow
, &modes
, &states
, NULL
);
414 if (check_allowed
&& !allow
) {
415 log_debug("Sleep mode \"%s\" is disabled by configuration.", verb
);
419 if (streq(verb
, "suspend-then-hibernate"))
422 if (!can_sleep_state(states
) || !can_sleep_disk(modes
))
425 if (streq(verb
, "suspend"))
428 if (!enough_swap_for_hibernation())
434 int can_sleep(const char *verb
) {
435 return can_sleep_internal(verb
, true);