]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/sleep-config.c
Merge pull request #4061 from dm0-/coreos-1545
[thirdparty/systemd.git] / src / shared / sleep-config.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2013 Zbigniew Jędrzejewski-Szmek
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <errno.h>
21 #include <stdbool.h>
22 #include <stddef.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <syslog.h>
26 #include <unistd.h>
27
28 #include "alloc-util.h"
29 #include "conf-parser.h"
30 #include "def.h"
31 #include "env-util.h"
32 #include "fd-util.h"
33 #include "fileio.h"
34 #include "log.h"
35 #include "macro.h"
36 #include "parse-util.h"
37 #include "sleep-config.h"
38 #include "string-util.h"
39 #include "strv.h"
40
41 #define USE(x, y) do { (x) = (y); (y) = NULL; } while (0)
42
43 int parse_sleep_config(const char *verb, char ***_modes, char ***_states) {
44
45 _cleanup_strv_free_ char
46 **suspend_mode = NULL, **suspend_state = NULL,
47 **hibernate_mode = NULL, **hibernate_state = NULL,
48 **hybrid_mode = NULL, **hybrid_state = NULL;
49 char **modes, **states;
50
51 const ConfigTableItem items[] = {
52 { "Sleep", "SuspendMode", config_parse_strv, 0, &suspend_mode },
53 { "Sleep", "SuspendState", config_parse_strv, 0, &suspend_state },
54 { "Sleep", "HibernateMode", config_parse_strv, 0, &hibernate_mode },
55 { "Sleep", "HibernateState", config_parse_strv, 0, &hibernate_state },
56 { "Sleep", "HybridSleepMode", config_parse_strv, 0, &hybrid_mode },
57 { "Sleep", "HybridSleepState", config_parse_strv, 0, &hybrid_state },
58 {}
59 };
60
61 config_parse_many_nulstr(PKGSYSCONFDIR "/sleep.conf",
62 CONF_PATHS_NULSTR("systemd/sleep.conf.d"),
63 "Sleep\0", config_item_table_lookup, items,
64 false, NULL);
65
66 if (streq(verb, "suspend")) {
67 /* empty by default */
68 USE(modes, suspend_mode);
69
70 if (suspend_state)
71 USE(states, suspend_state);
72 else
73 states = strv_new("mem", "standby", "freeze", NULL);
74
75 } else if (streq(verb, "hibernate")) {
76 if (hibernate_mode)
77 USE(modes, hibernate_mode);
78 else
79 modes = strv_new("platform", "shutdown", NULL);
80
81 if (hibernate_state)
82 USE(states, hibernate_state);
83 else
84 states = strv_new("disk", NULL);
85
86 } else if (streq(verb, "hybrid-sleep")) {
87 if (hybrid_mode)
88 USE(modes, hybrid_mode);
89 else
90 modes = strv_new("suspend", "platform", "shutdown", NULL);
91
92 if (hybrid_state)
93 USE(states, hybrid_state);
94 else
95 states = strv_new("disk", NULL);
96
97 } else
98 assert_not_reached("what verb");
99
100 if ((!modes && !streq(verb, "suspend")) || !states) {
101 strv_free(modes);
102 strv_free(states);
103 return log_oom();
104 }
105
106 *_modes = modes;
107 *_states = states;
108 return 0;
109 }
110
111 int can_sleep_state(char **types) {
112 char **type;
113 int r;
114 _cleanup_free_ char *p = NULL;
115
116 if (strv_isempty(types))
117 return true;
118
119 /* If /sys is read-only we cannot sleep */
120 if (access("/sys/power/state", W_OK) < 0)
121 return false;
122
123 r = read_one_line_file("/sys/power/state", &p);
124 if (r < 0)
125 return false;
126
127 STRV_FOREACH(type, types) {
128 const char *word, *state;
129 size_t l, k;
130
131 k = strlen(*type);
132 FOREACH_WORD_SEPARATOR(word, l, p, WHITESPACE, state)
133 if (l == k && memcmp(word, *type, l) == 0)
134 return true;
135 }
136
137 return false;
138 }
139
140 int can_sleep_disk(char **types) {
141 char **type;
142 int r;
143 _cleanup_free_ char *p = NULL;
144
145 if (strv_isempty(types))
146 return true;
147
148 /* If /sys is read-only we cannot sleep */
149 if (access("/sys/power/disk", W_OK) < 0)
150 return false;
151
152 r = read_one_line_file("/sys/power/disk", &p);
153 if (r < 0)
154 return false;
155
156 STRV_FOREACH(type, types) {
157 const char *word, *state;
158 size_t l, k;
159
160 k = strlen(*type);
161 FOREACH_WORD_SEPARATOR(word, l, p, WHITESPACE, state) {
162 if (l == k && memcmp(word, *type, l) == 0)
163 return true;
164
165 if (l == k + 2 &&
166 word[0] == '[' &&
167 memcmp(word + 1, *type, l - 2) == 0 &&
168 word[l-1] == ']')
169 return true;
170 }
171 }
172
173 return false;
174 }
175
176 #define HIBERNATION_SWAP_THRESHOLD 0.98
177
178 static int hibernation_partition_size(size_t *size, size_t *used) {
179 _cleanup_fclose_ FILE *f;
180 unsigned i;
181
182 assert(size);
183 assert(used);
184
185 f = fopen("/proc/swaps", "re");
186 if (!f) {
187 log_full(errno == ENOENT ? LOG_DEBUG : LOG_WARNING,
188 "Failed to retrieve open /proc/swaps: %m");
189 assert(errno > 0);
190 return -errno;
191 }
192
193 (void) fscanf(f, "%*s %*s %*s %*s %*s\n");
194
195 for (i = 1;; i++) {
196 _cleanup_free_ char *dev = NULL, *type = NULL;
197 size_t size_field, used_field;
198 int k;
199
200 k = fscanf(f,
201 "%ms " /* device/file */
202 "%ms " /* type of swap */
203 "%zu " /* swap size */
204 "%zu " /* used */
205 "%*i\n", /* priority */
206 &dev, &type, &size_field, &used_field);
207 if (k != 4) {
208 if (k == EOF)
209 break;
210
211 log_warning("Failed to parse /proc/swaps:%u", i);
212 continue;
213 }
214
215 if (streq(type, "partition") && endswith(dev, "\\040(deleted)")) {
216 log_warning("Ignoring deleted swapfile '%s'.", dev);
217 continue;
218 }
219
220 *size = size_field;
221 *used = used_field;
222 return 0;
223 }
224
225 log_debug("No swap partitions were found.");
226 return -ENOSYS;
227 }
228
229 static bool enough_memory_for_hibernation(void) {
230 _cleanup_free_ char *active = NULL;
231 unsigned long long act = 0;
232 size_t size = 0, used = 0;
233 int r;
234
235 if (getenv_bool("SYSTEMD_BYPASS_HIBERNATION_MEMORY_CHECK") > 0)
236 return true;
237
238 r = hibernation_partition_size(&size, &used);
239 if (r < 0)
240 return false;
241
242 r = get_proc_field("/proc/meminfo", "Active(anon)", WHITESPACE, &active);
243 if (r < 0) {
244 log_error_errno(r, "Failed to retrieve Active(anon) from /proc/meminfo: %m");
245 return false;
246 }
247
248 r = safe_atollu(active, &act);
249 if (r < 0) {
250 log_error_errno(r, "Failed to parse Active(anon) from /proc/meminfo: %s: %m",
251 active);
252 return false;
253 }
254
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);
258
259 return r;
260 }
261
262 int can_sleep(const char *verb) {
263 _cleanup_strv_free_ char **modes = NULL, **states = NULL;
264 int r;
265
266 assert(streq(verb, "suspend") ||
267 streq(verb, "hibernate") ||
268 streq(verb, "hybrid-sleep"));
269
270 r = parse_sleep_config(verb, &modes, &states);
271 if (r < 0)
272 return false;
273
274 if (!can_sleep_state(states) || !can_sleep_disk(modes))
275 return false;
276
277 return streq(verb, "suspend") || enough_memory_for_hibernation();
278 }