]>
Commit | Line | Data |
---|---|---|
52ad194e | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
663996b3 | 2 | /*** |
b012e921 | 3 | Copyright © 2018 Dell Inc. |
663996b3 MS |
4 | ***/ |
5 | ||
4c89c718 | 6 | #include <errno.h> |
bb4f798a | 7 | #include <fcntl.h> |
b012e921 | 8 | #include <linux/fs.h> |
4c89c718 MP |
9 | #include <stdbool.h> |
10 | #include <stddef.h> | |
663996b3 | 11 | #include <stdio.h> |
4c89c718 | 12 | #include <string.h> |
6e866b33 | 13 | #include <sys/ioctl.h> |
bb4f798a MB |
14 | #include <sys/stat.h> |
15 | #include <sys/types.h> | |
4c89c718 MP |
16 | #include <syslog.h> |
17 | #include <unistd.h> | |
663996b3 | 18 | |
db2df898 | 19 | #include "alloc-util.h" |
663996b3 | 20 | #include "conf-parser.h" |
db2df898 | 21 | #include "def.h" |
aa27b158 | 22 | #include "env-util.h" |
bb4f798a | 23 | #include "errno-util.h" |
db2df898 | 24 | #include "fd-util.h" |
663996b3 MS |
25 | #include "fileio.h" |
26 | #include "log.h" | |
4c89c718 | 27 | #include "macro.h" |
db2df898 | 28 | #include "parse-util.h" |
6e866b33 | 29 | #include "path-util.h" |
db2df898 MP |
30 | #include "sleep-config.h" |
31 | #include "string-util.h" | |
663996b3 | 32 | #include "strv.h" |
663996b3 | 33 | |
6e866b33 MB |
34 | int parse_sleep_config(const char *verb, bool *ret_allow, char ***ret_modes, char ***ret_states, usec_t *ret_delay) { |
35 | int allow_suspend = -1, allow_hibernate = -1, | |
36 | allow_s2h = -1, allow_hybrid_sleep = -1; | |
37 | bool allow; | |
663996b3 MS |
38 | _cleanup_strv_free_ char |
39 | **suspend_mode = NULL, **suspend_state = NULL, | |
40 | **hibernate_mode = NULL, **hibernate_state = NULL, | |
41 | **hybrid_mode = NULL, **hybrid_state = NULL; | |
b012e921 MB |
42 | _cleanup_strv_free_ char **modes, **states; /* always initialized below */ |
43 | usec_t delay = 180 * USEC_PER_MINUTE; | |
663996b3 MS |
44 | |
45 | const ConfigTableItem items[] = { | |
6e866b33 MB |
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, &suspend_mode }, | |
52 | { "Sleep", "SuspendState", config_parse_strv, 0, &suspend_state }, | |
53 | { "Sleep", "HibernateMode", config_parse_strv, 0, &hibernate_mode }, | |
54 | { "Sleep", "HibernateState", config_parse_strv, 0, &hibernate_state }, | |
55 | { "Sleep", "HybridSleepMode", config_parse_strv, 0, &hybrid_mode }, | |
56 | { "Sleep", "HybridSleepState", config_parse_strv, 0, &hybrid_state }, | |
57 | ||
58 | { "Sleep", "HibernateDelaySec", config_parse_sec, 0, &delay}, | |
e842803a MB |
59 | {} |
60 | }; | |
663996b3 | 61 | |
52ad194e MB |
62 | (void) config_parse_many_nulstr(PKGSYSCONFDIR "/sleep.conf", |
63 | CONF_PATHS_NULSTR("systemd/sleep.conf.d"), | |
64 | "Sleep\0", config_item_table_lookup, items, | |
65 | CONFIG_PARSE_WARN, NULL); | |
663996b3 MS |
66 | |
67 | if (streq(verb, "suspend")) { | |
6e866b33 MB |
68 | allow = allow_suspend != 0; |
69 | ||
663996b3 | 70 | /* empty by default */ |
b012e921 | 71 | modes = TAKE_PTR(suspend_mode); |
663996b3 MS |
72 | |
73 | if (suspend_state) | |
b012e921 | 74 | states = TAKE_PTR(suspend_state); |
663996b3 | 75 | else |
6e866b33 | 76 | states = strv_new("mem", "standby", "freeze"); |
663996b3 | 77 | |
663996b3 | 78 | } else if (streq(verb, "hibernate")) { |
6e866b33 MB |
79 | allow = allow_hibernate != 0; |
80 | ||
663996b3 | 81 | if (hibernate_mode) |
b012e921 | 82 | modes = TAKE_PTR(hibernate_mode); |
663996b3 | 83 | else |
6e866b33 | 84 | modes = strv_new("platform", "shutdown"); |
663996b3 MS |
85 | |
86 | if (hibernate_state) | |
b012e921 | 87 | states = TAKE_PTR(hibernate_state); |
663996b3 | 88 | else |
6e866b33 | 89 | states = strv_new("disk"); |
663996b3 | 90 | |
663996b3 | 91 | } else if (streq(verb, "hybrid-sleep")) { |
6e866b33 MB |
92 | allow = allow_hybrid_sleep > 0 || |
93 | (allow_suspend != 0 && allow_hibernate != 0); | |
94 | ||
663996b3 | 95 | if (hybrid_mode) |
b012e921 | 96 | modes = TAKE_PTR(hybrid_mode); |
663996b3 | 97 | else |
6e866b33 | 98 | modes = strv_new("suspend", "platform", "shutdown"); |
663996b3 MS |
99 | |
100 | if (hybrid_state) | |
b012e921 | 101 | states = TAKE_PTR(hybrid_state); |
663996b3 | 102 | else |
6e866b33 MB |
103 | states = strv_new("disk"); |
104 | ||
105 | } else if (streq(verb, "suspend-then-hibernate")) { | |
106 | allow = allow_s2h > 0 || | |
107 | (allow_suspend != 0 && allow_hibernate != 0); | |
663996b3 | 108 | |
b012e921 | 109 | modes = states = NULL; |
6e866b33 | 110 | } else |
663996b3 MS |
111 | assert_not_reached("what verb"); |
112 | ||
b012e921 MB |
113 | if ((!modes && STR_IN_SET(verb, "hibernate", "hybrid-sleep")) || |
114 | (!states && !streq(verb, "suspend-then-hibernate"))) | |
663996b3 | 115 | return log_oom(); |
663996b3 | 116 | |
6e866b33 MB |
117 | if (ret_allow) |
118 | *ret_allow = allow; | |
119 | if (ret_modes) | |
120 | *ret_modes = TAKE_PTR(modes); | |
121 | if (ret_states) | |
122 | *ret_states = TAKE_PTR(states); | |
123 | if (ret_delay) | |
124 | *ret_delay = delay; | |
b012e921 | 125 | |
663996b3 MS |
126 | return 0; |
127 | } | |
128 | ||
129 | int can_sleep_state(char **types) { | |
5eef597e | 130 | char **type; |
663996b3 MS |
131 | int r; |
132 | _cleanup_free_ char *p = NULL; | |
133 | ||
134 | if (strv_isempty(types)) | |
135 | return true; | |
136 | ||
137 | /* If /sys is read-only we cannot sleep */ | |
138 | if (access("/sys/power/state", W_OK) < 0) | |
139 | return false; | |
140 | ||
141 | r = read_one_line_file("/sys/power/state", &p); | |
142 | if (r < 0) | |
143 | return false; | |
144 | ||
145 | STRV_FOREACH(type, types) { | |
5eef597e | 146 | const char *word, *state; |
663996b3 MS |
147 | size_t l, k; |
148 | ||
149 | k = strlen(*type); | |
5eef597e MP |
150 | FOREACH_WORD_SEPARATOR(word, l, p, WHITESPACE, state) |
151 | if (l == k && memcmp(word, *type, l) == 0) | |
663996b3 MS |
152 | return true; |
153 | } | |
154 | ||
155 | return false; | |
156 | } | |
157 | ||
158 | int can_sleep_disk(char **types) { | |
5eef597e | 159 | char **type; |
663996b3 MS |
160 | int r; |
161 | _cleanup_free_ char *p = NULL; | |
162 | ||
163 | if (strv_isempty(types)) | |
164 | return true; | |
165 | ||
166 | /* If /sys is read-only we cannot sleep */ | |
6e866b33 MB |
167 | if (access("/sys/power/disk", W_OK) < 0) { |
168 | log_debug_errno(errno, "/sys/power/disk is not writable: %m"); | |
663996b3 | 169 | return false; |
6e866b33 | 170 | } |
663996b3 MS |
171 | |
172 | r = read_one_line_file("/sys/power/disk", &p); | |
6e866b33 MB |
173 | if (r < 0) { |
174 | log_debug_errno(r, "Couldn't read /sys/power/disk: %m"); | |
663996b3 | 175 | return false; |
6e866b33 | 176 | } |
663996b3 MS |
177 | |
178 | STRV_FOREACH(type, types) { | |
5eef597e | 179 | const char *word, *state; |
663996b3 MS |
180 | size_t l, k; |
181 | ||
182 | k = strlen(*type); | |
5eef597e MP |
183 | FOREACH_WORD_SEPARATOR(word, l, p, WHITESPACE, state) { |
184 | if (l == k && memcmp(word, *type, l) == 0) | |
663996b3 MS |
185 | return true; |
186 | ||
5eef597e MP |
187 | if (l == k + 2 && |
188 | word[0] == '[' && | |
189 | memcmp(word + 1, *type, l - 2) == 0 && | |
190 | word[l-1] == ']') | |
663996b3 MS |
191 | return true; |
192 | } | |
193 | } | |
194 | ||
195 | return false; | |
196 | } | |
197 | ||
14228c0d MB |
198 | #define HIBERNATION_SWAP_THRESHOLD 0.98 |
199 | ||
b012e921 | 200 | int find_hibernate_location(char **device, char **type, size_t *size, size_t *used) { |
14228c0d | 201 | _cleanup_fclose_ FILE *f; |
e735f4d4 | 202 | unsigned i; |
14228c0d | 203 | |
60f067b4 | 204 | f = fopen("/proc/swaps", "re"); |
14228c0d MB |
205 | if (!f) { |
206 | log_full(errno == ENOENT ? LOG_DEBUG : LOG_WARNING, | |
207 | "Failed to retrieve open /proc/swaps: %m"); | |
bb4f798a | 208 | return negative_errno(); |
14228c0d MB |
209 | } |
210 | ||
211 | (void) fscanf(f, "%*s %*s %*s %*s %*s\n"); | |
212 | ||
213 | for (i = 1;; i++) { | |
b012e921 | 214 | _cleanup_free_ char *dev_field = NULL, *type_field = NULL; |
14228c0d MB |
215 | size_t size_field, used_field; |
216 | int k; | |
217 | ||
218 | k = fscanf(f, | |
219 | "%ms " /* device/file */ | |
220 | "%ms " /* type of swap */ | |
e735f4d4 MP |
221 | "%zu " /* swap size */ |
222 | "%zu " /* used */ | |
14228c0d | 223 | "%*i\n", /* priority */ |
b012e921 | 224 | &dev_field, &type_field, &size_field, &used_field); |
6e866b33 MB |
225 | if (k == EOF) |
226 | break; | |
14228c0d | 227 | if (k != 4) { |
14228c0d MB |
228 | log_warning("Failed to parse /proc/swaps:%u", i); |
229 | continue; | |
230 | } | |
231 | ||
6e866b33 MB |
232 | if (streq(type_field, "file")) { |
233 | ||
234 | if (endswith(dev_field, "\\040(deleted)")) { | |
235 | log_warning("Ignoring deleted swap file '%s'.", dev_field); | |
236 | continue; | |
237 | } | |
238 | ||
239 | } else if (streq(type_field, "partition")) { | |
240 | const char *fn; | |
241 | ||
242 | fn = path_startswith(dev_field, "/dev/"); | |
243 | if (fn && startswith(fn, "zram")) { | |
244 | log_debug("Ignoring compressed RAM swap device '%s'.", dev_field); | |
245 | continue; | |
246 | } | |
14228c0d | 247 | } |
6e866b33 | 248 | |
b012e921 MB |
249 | if (device) |
250 | *device = TAKE_PTR(dev_field); | |
251 | if (type) | |
252 | *type = TAKE_PTR(type_field); | |
253 | if (size) | |
254 | *size = size_field; | |
255 | if (used) | |
256 | *used = used_field; | |
14228c0d MB |
257 | return 0; |
258 | } | |
259 | ||
6e866b33 MB |
260 | return log_debug_errno(SYNTHETIC_ERRNO(ENOSYS), |
261 | "No swap partitions were found."); | |
14228c0d MB |
262 | } |
263 | ||
b012e921 | 264 | static bool enough_swap_for_hibernation(void) { |
14228c0d | 265 | _cleanup_free_ char *active = NULL; |
60f067b4 JS |
266 | unsigned long long act = 0; |
267 | size_t size = 0, used = 0; | |
14228c0d MB |
268 | int r; |
269 | ||
aa27b158 MP |
270 | if (getenv_bool("SYSTEMD_BYPASS_HIBERNATION_MEMORY_CHECK") > 0) |
271 | return true; | |
272 | ||
b012e921 | 273 | r = find_hibernate_location(NULL, NULL, &size, &used); |
14228c0d MB |
274 | if (r < 0) |
275 | return false; | |
276 | ||
6300502b | 277 | r = get_proc_field("/proc/meminfo", "Active(anon)", WHITESPACE, &active); |
14228c0d | 278 | if (r < 0) { |
6e866b33 | 279 | log_debug_errno(r, "Failed to retrieve Active(anon) from /proc/meminfo: %m"); |
14228c0d MB |
280 | return false; |
281 | } | |
282 | ||
283 | r = safe_atollu(active, &act); | |
284 | if (r < 0) { | |
6e866b33 | 285 | log_debug_errno(r, "Failed to parse Active(anon) from /proc/meminfo: %s: %m", active); |
14228c0d MB |
286 | return false; |
287 | } | |
288 | ||
289 | r = act <= (size - used) * HIBERNATION_SWAP_THRESHOLD; | |
6e866b33 MB |
290 | log_debug("%s swap for hibernation, Active(anon)=%llu kB, size=%zu kB, used=%zu kB, threshold=%.2g%%", |
291 | r ? "Enough" : "Not enough", act, size, used, 100*HIBERNATION_SWAP_THRESHOLD); | |
14228c0d MB |
292 | |
293 | return r; | |
294 | } | |
295 | ||
b012e921 MB |
296 | int read_fiemap(int fd, struct fiemap **ret) { |
297 | _cleanup_free_ struct fiemap *fiemap = NULL, *result_fiemap = NULL; | |
298 | struct stat statinfo; | |
299 | uint32_t result_extents = 0; | |
300 | uint64_t fiemap_start = 0, fiemap_length; | |
301 | const size_t n_extra = DIV_ROUND_UP(sizeof(struct fiemap), sizeof(struct fiemap_extent)); | |
302 | size_t fiemap_allocated = n_extra, result_fiemap_allocated = n_extra; | |
303 | ||
304 | if (fstat(fd, &statinfo) < 0) | |
305 | return log_debug_errno(errno, "Cannot determine file size: %m"); | |
306 | if (!S_ISREG(statinfo.st_mode)) | |
307 | return -ENOTTY; | |
308 | fiemap_length = statinfo.st_size; | |
309 | ||
310 | /* Zero this out in case we run on a file with no extents */ | |
311 | fiemap = calloc(n_extra, sizeof(struct fiemap_extent)); | |
312 | if (!fiemap) | |
313 | return -ENOMEM; | |
314 | ||
315 | result_fiemap = malloc_multiply(n_extra, sizeof(struct fiemap_extent)); | |
316 | if (!result_fiemap) | |
317 | return -ENOMEM; | |
318 | ||
319 | /* XFS filesystem has incorrect implementation of fiemap ioctl and | |
320 | * returns extents for only one block-group at a time, so we need | |
321 | * to handle it manually, starting the next fiemap call from the end | |
322 | * of the last extent | |
323 | */ | |
324 | while (fiemap_start < fiemap_length) { | |
325 | *fiemap = (struct fiemap) { | |
326 | .fm_start = fiemap_start, | |
327 | .fm_length = fiemap_length, | |
328 | .fm_flags = FIEMAP_FLAG_SYNC, | |
329 | }; | |
330 | ||
331 | /* Find out how many extents there are */ | |
332 | if (ioctl(fd, FS_IOC_FIEMAP, fiemap) < 0) | |
333 | return log_debug_errno(errno, "Failed to read extents: %m"); | |
334 | ||
335 | /* Nothing to process */ | |
336 | if (fiemap->fm_mapped_extents == 0) | |
337 | break; | |
338 | ||
339 | /* Resize fiemap to allow us to read in the extents, result fiemap has to hold all | |
340 | * the extents for the whole file. Add space for the initial struct fiemap. */ | |
341 | if (!greedy_realloc0((void**) &fiemap, &fiemap_allocated, | |
342 | n_extra + fiemap->fm_mapped_extents, sizeof(struct fiemap_extent))) | |
343 | return -ENOMEM; | |
344 | ||
345 | fiemap->fm_extent_count = fiemap->fm_mapped_extents; | |
346 | fiemap->fm_mapped_extents = 0; | |
347 | ||
348 | if (ioctl(fd, FS_IOC_FIEMAP, fiemap) < 0) | |
349 | return log_debug_errno(errno, "Failed to read extents: %m"); | |
350 | ||
351 | /* Resize result_fiemap to allow us to copy in the extents */ | |
352 | if (!greedy_realloc((void**) &result_fiemap, &result_fiemap_allocated, | |
353 | n_extra + result_extents + fiemap->fm_mapped_extents, sizeof(struct fiemap_extent))) | |
354 | return -ENOMEM; | |
355 | ||
356 | memcpy(result_fiemap->fm_extents + result_extents, | |
357 | fiemap->fm_extents, | |
358 | sizeof(struct fiemap_extent) * fiemap->fm_mapped_extents); | |
359 | ||
360 | result_extents += fiemap->fm_mapped_extents; | |
361 | ||
362 | /* Highly unlikely that it is zero */ | |
363 | if (_likely_(fiemap->fm_mapped_extents > 0)) { | |
364 | uint32_t i = fiemap->fm_mapped_extents - 1; | |
365 | ||
366 | fiemap_start = fiemap->fm_extents[i].fe_logical + | |
367 | fiemap->fm_extents[i].fe_length; | |
368 | ||
369 | if (fiemap->fm_extents[i].fe_flags & FIEMAP_EXTENT_LAST) | |
370 | break; | |
371 | } | |
372 | } | |
373 | ||
374 | memcpy(result_fiemap, fiemap, sizeof(struct fiemap)); | |
375 | result_fiemap->fm_mapped_extents = result_extents; | |
376 | *ret = TAKE_PTR(result_fiemap); | |
377 | return 0; | |
378 | } | |
379 | ||
6e866b33 MB |
380 | static int can_sleep_internal(const char *verb, bool check_allowed); |
381 | ||
b012e921 MB |
382 | static bool can_s2h(void) { |
383 | const char *p; | |
384 | int r; | |
385 | ||
386 | r = access("/sys/class/rtc/rtc0/wakealarm", W_OK); | |
387 | if (r < 0) { | |
388 | log_full(errno == ENOENT ? LOG_DEBUG : LOG_WARNING, | |
389 | "/sys/class/rct/rct0/wakealarm is not writable %m"); | |
390 | return false; | |
391 | } | |
392 | ||
393 | FOREACH_STRING(p, "suspend", "hibernate") { | |
6e866b33 MB |
394 | r = can_sleep_internal(p, false); |
395 | if (IN_SET(r, 0, -ENOSPC, -EADV)) { | |
b012e921 MB |
396 | log_debug("Unable to %s system.", p); |
397 | return false; | |
398 | } | |
399 | if (r < 0) | |
400 | return log_debug_errno(r, "Failed to check if %s is possible: %m", p); | |
401 | } | |
402 | ||
403 | return true; | |
404 | } | |
405 | ||
6e866b33 MB |
406 | static int can_sleep_internal(const char *verb, bool check_allowed) { |
407 | bool allow; | |
663996b3 MS |
408 | _cleanup_strv_free_ char **modes = NULL, **states = NULL; |
409 | int r; | |
410 | ||
b012e921 | 411 | assert(STR_IN_SET(verb, "suspend", "hibernate", "hybrid-sleep", "suspend-then-hibernate")); |
663996b3 | 412 | |
6e866b33 | 413 | r = parse_sleep_config(verb, &allow, &modes, &states, NULL); |
663996b3 MS |
414 | if (r < 0) |
415 | return false; | |
416 | ||
6e866b33 MB |
417 | if (check_allowed && !allow) { |
418 | log_debug("Sleep mode \"%s\" is disabled by configuration.", verb); | |
419 | return false; | |
420 | } | |
421 | ||
422 | if (streq(verb, "suspend-then-hibernate")) | |
423 | return can_s2h(); | |
424 | ||
14228c0d MB |
425 | if (!can_sleep_state(states) || !can_sleep_disk(modes)) |
426 | return false; | |
427 | ||
b012e921 MB |
428 | if (streq(verb, "suspend")) |
429 | return true; | |
430 | ||
431 | if (!enough_swap_for_hibernation()) | |
432 | return -ENOSPC; | |
433 | ||
434 | return true; | |
663996b3 | 435 | } |
6e866b33 MB |
436 | |
437 | int can_sleep(const char *verb) { | |
438 | return can_sleep_internal(verb, true); | |
439 | } |