1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2013 Zbigniew Jędrzejewski-Szmek
4 Copyright 2018 Dell Inc.
16 #include "alloc-util.h"
17 #include "conf-parser.h"
24 #include "parse-util.h"
25 #include "sleep-config.h"
26 #include "string-util.h"
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("systemd/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
);
104 int can_sleep_state(char **types
) {
107 _cleanup_free_
char *p
= NULL
;
109 if (strv_isempty(types
))
112 /* If /sys is read-only we cannot sleep */
113 if (access("/sys/power/state", W_OK
) < 0)
116 r
= read_one_line_file("/sys/power/state", &p
);
120 STRV_FOREACH(type
, types
) {
121 const char *word
, *state
;
125 FOREACH_WORD_SEPARATOR(word
, l
, p
, WHITESPACE
, state
)
126 if (l
== k
&& memcmp(word
, *type
, l
) == 0)
133 int can_sleep_disk(char **types
) {
136 _cleanup_free_
char *p
= NULL
;
138 if (strv_isempty(types
))
141 /* If /sys is read-only we cannot sleep */
142 if (access("/sys/power/disk", W_OK
) < 0)
145 r
= read_one_line_file("/sys/power/disk", &p
);
149 STRV_FOREACH(type
, types
) {
150 const char *word
, *state
;
154 FOREACH_WORD_SEPARATOR(word
, l
, p
, WHITESPACE
, state
) {
155 if (l
== k
&& memcmp(word
, *type
, l
) == 0)
160 memcmp(word
+ 1, *type
, l
- 2) == 0 &&
169 #define HIBERNATION_SWAP_THRESHOLD 0.98
171 int find_hibernate_location(char **device
, char **type
, size_t *size
, size_t *used
) {
172 _cleanup_fclose_
FILE *f
;
175 f
= fopen("/proc/swaps", "re");
177 log_full(errno
== ENOENT
? LOG_DEBUG
: LOG_WARNING
,
178 "Failed to retrieve open /proc/swaps: %m");
183 (void) fscanf(f
, "%*s %*s %*s %*s %*s\n");
186 _cleanup_free_
char *dev_field
= NULL
, *type_field
= NULL
;
187 size_t size_field
, used_field
;
191 "%ms " /* device/file */
192 "%ms " /* type of swap */
193 "%zu " /* swap size */
195 "%*i\n", /* priority */
196 &dev_field
, &type_field
, &size_field
, &used_field
);
201 log_warning("Failed to parse /proc/swaps:%u", i
);
205 if (streq(type_field
, "partition") && endswith(dev_field
, "\\040(deleted)")) {
206 log_warning("Ignoring deleted swapfile '%s'.", dev_field
);
210 *device
= TAKE_PTR(dev_field
);
212 *type
= TAKE_PTR(type_field
);
220 log_debug("No swap partitions were found.");
224 static bool enough_swap_for_hibernation(void) {
225 _cleanup_free_
char *active
= NULL
;
226 unsigned long long act
= 0;
227 size_t size
= 0, used
= 0;
230 if (getenv_bool("SYSTEMD_BYPASS_HIBERNATION_MEMORY_CHECK") > 0)
233 r
= find_hibernate_location(NULL
, NULL
, &size
, &used
);
237 r
= get_proc_field("/proc/meminfo", "Active(anon)", WHITESPACE
, &active
);
239 log_error_errno(r
, "Failed to retrieve Active(anon) from /proc/meminfo: %m");
243 r
= safe_atollu(active
, &act
);
245 log_error_errno(r
, "Failed to parse Active(anon) from /proc/meminfo: %s: %m",
250 r
= act
<= (size
- used
) * HIBERNATION_SWAP_THRESHOLD
;
251 log_debug("Hibernation is %spossible, Active(anon)=%llu kB, size=%zu kB, used=%zu kB, threshold=%.2g%%",
252 r
? "" : "im", act
, size
, used
, 100*HIBERNATION_SWAP_THRESHOLD
);
257 int read_fiemap(int fd
, struct fiemap
**ret
) {
258 _cleanup_free_
struct fiemap
*fiemap
= NULL
, *result_fiemap
= NULL
;
259 struct stat statinfo
;
260 uint32_t result_extents
= 0;
261 uint64_t fiemap_start
= 0, fiemap_length
;
262 const size_t n_extra
= DIV_ROUND_UP(sizeof(struct fiemap
), sizeof(struct fiemap_extent
));
263 size_t fiemap_allocated
= n_extra
, result_fiemap_allocated
= n_extra
;
265 if (fstat(fd
, &statinfo
) < 0)
266 return log_debug_errno(errno
, "Cannot determine file size: %m");
267 if (!S_ISREG(statinfo
.st_mode
))
269 fiemap_length
= statinfo
.st_size
;
271 /* Zero this out in case we run on a file with no extents */
272 fiemap
= calloc(n_extra
, sizeof(struct fiemap_extent
));
276 result_fiemap
= malloc_multiply(n_extra
, sizeof(struct fiemap_extent
));
280 /* XFS filesystem has incorrect implementation of fiemap ioctl and
281 * returns extents for only one block-group at a time, so we need
282 * to handle it manually, starting the next fiemap call from the end
285 while (fiemap_start
< fiemap_length
) {
286 *fiemap
= (struct fiemap
) {
287 .fm_start
= fiemap_start
,
288 .fm_length
= fiemap_length
,
289 .fm_flags
= FIEMAP_FLAG_SYNC
,
292 /* Find out how many extents there are */
293 if (ioctl(fd
, FS_IOC_FIEMAP
, fiemap
) < 0)
294 return log_debug_errno(errno
, "Failed to read extents: %m");
296 /* Nothing to process */
297 if (fiemap
->fm_mapped_extents
== 0)
300 /* Resize fiemap to allow us to read in the extents, result fiemap has to hold all
301 * the extents for the whole file. Add space for the initial struct fiemap. */
302 if (!greedy_realloc0((void**) &fiemap
, &fiemap_allocated
,
303 n_extra
+ fiemap
->fm_mapped_extents
, sizeof(struct fiemap_extent
)))
306 fiemap
->fm_extent_count
= fiemap
->fm_mapped_extents
;
307 fiemap
->fm_mapped_extents
= 0;
309 if (ioctl(fd
, FS_IOC_FIEMAP
, fiemap
) < 0)
310 return log_debug_errno(errno
, "Failed to read extents: %m");
312 /* Resize result_fiemap to allow us to copy in the extents */
313 if (!greedy_realloc((void**) &result_fiemap
, &result_fiemap_allocated
,
314 n_extra
+ result_extents
+ fiemap
->fm_mapped_extents
, sizeof(struct fiemap_extent
)))
317 memcpy(result_fiemap
->fm_extents
+ result_extents
,
319 sizeof(struct fiemap_extent
) * fiemap
->fm_mapped_extents
);
321 result_extents
+= fiemap
->fm_mapped_extents
;
323 /* Highly unlikely that it is zero */
324 if (_likely_(fiemap
->fm_mapped_extents
> 0)) {
325 uint32_t i
= fiemap
->fm_mapped_extents
- 1;
327 fiemap_start
= fiemap
->fm_extents
[i
].fe_logical
+
328 fiemap
->fm_extents
[i
].fe_length
;
330 if (fiemap
->fm_extents
[i
].fe_flags
& FIEMAP_EXTENT_LAST
)
335 memcpy(result_fiemap
, fiemap
, sizeof(struct fiemap
));
336 result_fiemap
->fm_mapped_extents
= result_extents
;
337 *ret
= TAKE_PTR(result_fiemap
);
341 static bool can_s2h(void) {
345 r
= access("/sys/class/rtc/rtc0/wakealarm", W_OK
);
347 log_full(errno
== ENOENT
? LOG_DEBUG
: LOG_WARNING
,
348 "/sys/class/rct/rct0/wakealarm is not writable %m");
352 FOREACH_STRING(p
, "suspend", "hibernate") {
354 if (IN_SET(r
, 0, -ENOSPC
)) {
355 log_debug("Unable to %s system.", p
);
359 return log_debug_errno(r
, "Failed to check if %s is possible: %m", p
);
365 int can_sleep(const char *verb
) {
366 _cleanup_strv_free_
char **modes
= NULL
, **states
= NULL
;
369 assert(STR_IN_SET(verb
, "suspend", "hibernate", "hybrid-sleep", "suspend-then-hibernate"));
371 if (streq(verb
, "suspend-then-hibernate"))
374 r
= parse_sleep_config(verb
, &modes
, &states
, NULL
);
378 if (!can_sleep_state(states
) || !can_sleep_disk(modes
))
381 if (streq(verb
, "suspend"))
384 if (!enough_swap_for_hibernation())