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