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