]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/sleep-config.c
conf-parser: turn three bool function params into a flags fields
[thirdparty/systemd.git] / src / shared / sleep-config.c
CommitLineData
19adb8a3
ZJS
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
a8fbdf54
TA
20#include <errno.h>
21#include <stdbool.h>
22#include <stddef.h>
19adb8a3 23#include <stdio.h>
a8fbdf54
TA
24#include <string.h>
25#include <syslog.h>
26#include <unistd.h>
19adb8a3 27
b5efdb8a 28#include "alloc-util.h"
19adb8a3 29#include "conf-parser.h"
a0f29c76 30#include "def.h"
490d20e6 31#include "env-util.h"
3ffd4af2 32#include "fd-util.h"
19adb8a3
ZJS
33#include "fileio.h"
34#include "log.h"
a8fbdf54 35#include "macro.h"
a0f29c76 36#include "parse-util.h"
3ffd4af2 37#include "sleep-config.h"
07630cea 38#include "string-util.h"
19adb8a3 39#include "strv.h"
19adb8a3 40
9ed794a3 41#define USE(x, y) do { (x) = (y); (y) = NULL; } while (0)
dabeaa46
ZJS
42
43int parse_sleep_config(const char *verb, char ***_modes, char ***_states) {
34c10968 44
19adb8a3
ZJS
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;
dabeaa46 49 char **modes, **states;
19adb8a3
ZJS
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 },
34c10968
LP
58 {}
59 };
19adb8a3 60
bcde742e
LP
61 (void) config_parse_many_nulstr(PKGSYSCONFDIR "/sleep.conf",
62 CONF_PATHS_NULSTR("systemd/sleep.conf.d"),
63 "Sleep\0", config_item_table_lookup, items,
64 CONFIG_PARSE_WARN, NULL);
19adb8a3 65
19adb8a3
ZJS
66 if (streq(verb, "suspend")) {
67 /* empty by default */
dabeaa46 68 USE(modes, suspend_mode);
19adb8a3
ZJS
69
70 if (suspend_state)
dabeaa46 71 USE(states, suspend_state);
19adb8a3 72 else
dabeaa46 73 states = strv_new("mem", "standby", "freeze", NULL);
19adb8a3 74
19adb8a3
ZJS
75 } else if (streq(verb, "hibernate")) {
76 if (hibernate_mode)
dabeaa46 77 USE(modes, hibernate_mode);
19adb8a3 78 else
dabeaa46 79 modes = strv_new("platform", "shutdown", NULL);
19adb8a3
ZJS
80
81 if (hibernate_state)
dabeaa46 82 USE(states, hibernate_state);
19adb8a3 83 else
dabeaa46 84 states = strv_new("disk", NULL);
19adb8a3 85
19adb8a3
ZJS
86 } else if (streq(verb, "hybrid-sleep")) {
87 if (hybrid_mode)
dabeaa46 88 USE(modes, hybrid_mode);
19adb8a3 89 else
dabeaa46 90 modes = strv_new("suspend", "platform", "shutdown", NULL);
19adb8a3
ZJS
91
92 if (hybrid_state)
dabeaa46 93 USE(states, hybrid_state);
19adb8a3 94 else
dabeaa46 95 states = strv_new("disk", NULL);
19adb8a3 96
19adb8a3
ZJS
97 } else
98 assert_not_reached("what verb");
99
dabeaa46
ZJS
100 if ((!modes && !streq(verb, "suspend")) || !states) {
101 strv_free(modes);
102 strv_free(states);
19adb8a3
ZJS
103 return log_oom();
104 }
105
dabeaa46
ZJS
106 *_modes = modes;
107 *_states = states;
19adb8a3
ZJS
108 return 0;
109}
110
111int can_sleep_state(char **types) {
a2a5291b 112 char **type;
19adb8a3
ZJS
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) {
a2a5291b 128 const char *word, *state;
19adb8a3
ZJS
129 size_t l, k;
130
131 k = strlen(*type);
a2a5291b
ZJS
132 FOREACH_WORD_SEPARATOR(word, l, p, WHITESPACE, state)
133 if (l == k && memcmp(word, *type, l) == 0)
19adb8a3
ZJS
134 return true;
135 }
136
137 return false;
138}
139
140int can_sleep_disk(char **types) {
a2a5291b 141 char **type;
19adb8a3
ZJS
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) {
a2a5291b 157 const char *word, *state;
19adb8a3
ZJS
158 size_t l, k;
159
160 k = strlen(*type);
a2a5291b
ZJS
161 FOREACH_WORD_SEPARATOR(word, l, p, WHITESPACE, state) {
162 if (l == k && memcmp(word, *type, l) == 0)
19adb8a3
ZJS
163 return true;
164
a2a5291b
ZJS
165 if (l == k + 2 &&
166 word[0] == '[' &&
167 memcmp(word + 1, *type, l - 2) == 0 &&
168 word[l-1] == ']')
19adb8a3
ZJS
169 return true;
170 }
171 }
172
173 return false;
174}
175
69ab8088
ZJS
176#define HIBERNATION_SWAP_THRESHOLD 0.98
177
9fb3675e
ZJS
178static int hibernation_partition_size(size_t *size, size_t *used) {
179 _cleanup_fclose_ FILE *f;
1fa2f38f 180 unsigned i;
9fb3675e
ZJS
181
182 assert(size);
183 assert(used);
184
c8a202b7 185 f = fopen("/proc/swaps", "re");
9fb3675e
ZJS
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 }
69ab8088 192
9fb3675e
ZJS
193 (void) fscanf(f, "%*s %*s %*s %*s %*s\n");
194
195 for (i = 1;; i++) {
db69869f 196 _cleanup_free_ char *dev = NULL, *type = NULL;
9fb3675e
ZJS
197 size_t size_field, used_field;
198 int k;
199
200 k = fscanf(f,
201 "%ms " /* device/file */
202 "%ms " /* type of swap */
1fa2f38f
ZJS
203 "%zu " /* swap size */
204 "%zu " /* used */
9fb3675e
ZJS
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
db69869f
DR
215 if (streq(type, "partition") && endswith(dev, "\\040(deleted)")) {
216 log_warning("Ignoring deleted swapfile '%s'.", dev);
9fb3675e
ZJS
217 continue;
218 }
219
220 *size = size_field;
221 *used = used_field;
222 return 0;
69ab8088
ZJS
223 }
224
9fb3675e
ZJS
225 log_debug("No swap partitions were found.");
226 return -ENOSYS;
227}
228
229static bool enough_memory_for_hibernation(void) {
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
9fb3675e
ZJS
238 r = hibernation_partition_size(&size, &used);
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
19adb8a3
ZJS
262int 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
69ab8088
ZJS
274 if (!can_sleep_state(states) || !can_sleep_disk(modes))
275 return false;
276
277 return streq(verb, "suspend") || enough_memory_for_hibernation();
19adb8a3 278}