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