1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <unistd.h>
#include "alloc-util.h"
#include "conf-parser.h"
#include "constants.h"
#include "device-util.h"
#include "devnum-util.h"
#include "errno-util.h"
#include "fd-util.h"
#include "fileio.h"
#include "hibernate-util.h"
#include "log.h"
#include "macro.h"
#include "path-util.h"
#include "sleep-config.h"
#include "stat-util.h"
#include "stdio-util.h"
#include "string-table.h"
#include "string-util.h"
#include "strv.h"
#include "time-util.h"
#define DEFAULT_SUSPEND_ESTIMATION_USEC (1 * USEC_PER_HOUR)
static const char* const sleep_operation_table[_SLEEP_OPERATION_MAX] = {
[SLEEP_SUSPEND] = "suspend",
[SLEEP_HIBERNATE] = "hibernate",
[SLEEP_HYBRID_SLEEP] = "hybrid-sleep",
[SLEEP_SUSPEND_THEN_HIBERNATE] = "suspend-then-hibernate",
};
DEFINE_STRING_TABLE_LOOKUP(sleep_operation, SleepOperation);
static char* const* const sleep_default_state_table[_SLEEP_OPERATION_CONFIG_MAX] = {
[SLEEP_SUSPEND] = STRV_MAKE("mem", "standby", "freeze"),
[SLEEP_HIBERNATE] = STRV_MAKE("disk"),
[SLEEP_HYBRID_SLEEP] = STRV_MAKE("disk"),
};
static char* const* const sleep_default_mode_table[_SLEEP_OPERATION_CONFIG_MAX] = {
/* Not used by SLEEP_SUSPEND */
[SLEEP_HIBERNATE] = STRV_MAKE("platform", "shutdown"),
[SLEEP_HYBRID_SLEEP] = STRV_MAKE("suspend"),
};
SleepConfig* sleep_config_free(SleepConfig *sc) {
if (!sc)
return NULL;
for (SleepOperation i = 0; i < _SLEEP_OPERATION_CONFIG_MAX; i++) {
strv_free(sc->states[i]);
strv_free(sc->modes[i]);
}
strv_free(sc->mem_modes);
return mfree(sc);
}
static int config_parse_sleep_mode(
const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
char ***sv = ASSERT_PTR(data);
_cleanup_strv_free_ char **modes = NULL;
int r;
assert(filename);
assert(lvalue);
assert(rvalue);
if (isempty(rvalue)) {
modes = strv_new(NULL);
if (!modes)
return log_oom();
} else {
r = strv_split_full(&modes, rvalue, NULL, EXTRACT_UNQUOTE|EXTRACT_RETAIN_ESCAPE);
if (r < 0)
return log_oom();
}
return strv_free_and_replace(*sv, modes);
}
static void sleep_config_validate_state_and_mode(SleepConfig *sc) {
assert(sc);
/* So we should really not allow setting SuspendState= to 'disk', which means hibernation. We have
* SLEEP_HIBERNATE for proper hibernation support, which includes checks for resume support (through
* EFI variable or resume= kernel command line option). It's simply not sensible to call the suspend
* operation but eventually do an unsafe hibernation. */
if (strv_contains(sc->states[SLEEP_SUSPEND], "disk")) {
strv_remove(sc->states[SLEEP_SUSPEND], "disk");
log_warning("Sleep state 'disk' is not supported by operation %s, ignoring.",
sleep_operation_to_string(SLEEP_SUSPEND));
}
assert(!sc->modes[SLEEP_SUSPEND]);
/* People should use hybrid-sleep instead of setting HibernateMode=suspend. Warn about it but don't
* drop it in this case. */
if (strv_contains(sc->modes[SLEEP_HIBERNATE], "suspend"))
log_warning("Sleep mode 'suspend' should not be used by operation %s. Please use %s instead.",
sleep_operation_to_string(SLEEP_HIBERNATE), sleep_operation_to_string(SLEEP_HYBRID_SLEEP));
}
int parse_sleep_config(SleepConfig **ret) {
_cleanup_(sleep_config_freep) SleepConfig *sc = NULL;
int allow_suspend = -1, allow_hibernate = -1, allow_s2h = -1, allow_hybrid_sleep = -1;
assert(ret);
sc = new(SleepConfig, 1);
if (!sc)
return log_oom();
*sc = (SleepConfig) {
.hibernate_delay_usec = USEC_INFINITY,
};
const ConfigTableItem items[] = {
{ "Sleep", "AllowSuspend", config_parse_tristate, 0, &allow_suspend },
{ "Sleep", "AllowHibernation", config_parse_tristate, 0, &allow_hibernate },
{ "Sleep", "AllowSuspendThenHibernate", config_parse_tristate, 0, &allow_s2h },
{ "Sleep", "AllowHybridSleep", config_parse_tristate, 0, &allow_hybrid_sleep },
{ "Sleep", "SuspendState", config_parse_strv, 0, sc->states + SLEEP_SUSPEND },
{ "Sleep", "SuspendMode", config_parse_warn_compat, DISABLED_LEGACY, NULL },
{ "Sleep", "HibernateState", config_parse_warn_compat, DISABLED_LEGACY, NULL },
{ "Sleep", "HibernateMode", config_parse_sleep_mode, 0, sc->modes + SLEEP_HIBERNATE },
{ "Sleep", "HybridSleepState", config_parse_warn_compat, DISABLED_LEGACY, NULL },
{ "Sleep", "HybridSleepMode", config_parse_warn_compat, DISABLED_LEGACY, NULL },
{ "Sleep", "MemorySleepMode", config_parse_sleep_mode, 0, &sc->mem_modes },
{ "Sleep", "HibernateDelaySec", config_parse_sec, 0, &sc->hibernate_delay_usec },
{ "Sleep", "SuspendEstimationSec", config_parse_sec, 0, &sc->suspend_estimation_usec },
{}
};
(void) config_parse_standard_file_with_dropins(
"systemd/sleep.conf",
"Sleep\0",
config_item_table_lookup, items,
CONFIG_PARSE_WARN,
/* userdata= */ NULL);
/* use default values unless set */
sc->allow[SLEEP_SUSPEND] = allow_suspend != 0;
sc->allow[SLEEP_HIBERNATE] = allow_hibernate != 0;
sc->allow[SLEEP_HYBRID_SLEEP] = allow_hybrid_sleep >= 0 ? allow_hybrid_sleep
: (allow_suspend != 0 && allow_hibernate != 0);
sc->allow[SLEEP_SUSPEND_THEN_HIBERNATE] = allow_s2h >= 0 ? allow_s2h
: (allow_suspend != 0 && allow_hibernate != 0);
for (SleepOperation i = 0; i < _SLEEP_OPERATION_CONFIG_MAX; i++) {
if (!sc->states[i] && sleep_default_state_table[i]) {
sc->states[i] = strv_copy(sleep_default_state_table[i]);
if (!sc->states[i])
return log_oom();
}
if (!sc->modes[i] && sleep_default_mode_table[i]) {
sc->modes[i] = strv_copy(sleep_default_mode_table[i]);
if (!sc->modes[i])
return log_oom();
}
}
if (sc->suspend_estimation_usec == 0)
sc->suspend_estimation_usec = DEFAULT_SUSPEND_ESTIMATION_USEC;
sleep_config_validate_state_and_mode(sc);
*ret = TAKE_PTR(sc);
return 0;
}
int sleep_state_supported(char * const *states) {
_cleanup_free_ char *supported_sysfs = NULL;
const char *found;
int r;
if (strv_isempty(states))
return log_debug_errno(SYNTHETIC_ERRNO(ENOMSG), "No sleep state configured.");
if (access("/sys/power/state", W_OK) < 0)
return log_debug_errno(errno, "/sys/power/state is not writable: %m");
r = read_one_line_file("/sys/power/state", &supported_sysfs);
if (r < 0)
return log_debug_errno(r, "Failed to read /sys/power/state: %m");
r = string_contains_word_strv(supported_sysfs, NULL, states, &found);
if (r < 0)
return log_debug_errno(r, "Failed to parse /sys/power/state: %m");
if (r > 0) {
log_debug("Sleep state '%s' is supported by kernel.", found);
return true;
}
if (DEBUG_LOGGING) {
_cleanup_free_ char *joined = strv_join(states, " ");
log_debug("None of the configured sleep states are supported by kernel: %s", strnull(joined));
}
return false;
}
int sleep_mode_supported(const char *path, char * const *modes) {
_cleanup_free_ char *supported_sysfs = NULL;
int r;
assert(path);
/* Unlike state, kernel has its own default choice if not configured */
if (strv_isempty(modes)) {
log_debug("No sleep mode configured, using kernel default for %s.", path);
return true;
}
if (access(path, W_OK) < 0)
return log_debug_errno(errno, "%s is not writable: %m", path);
r = read_one_line_file(path, &supported_sysfs);
if (r < 0)
return log_debug_errno(r, "Failed to read %s: %m", path);
for (const char *p = supported_sysfs;;) {
_cleanup_free_ char *word = NULL;
char *mode;
size_t l;
r = extract_first_word(&p, &word, NULL, 0);
if (r < 0)
return log_debug_errno(r, "Failed to parse %s: %m", path);
if (r == 0)
break;
mode = word;
l = strlen(word);
if (mode[0] == '[' && mode[l - 1] == ']') {
mode[l - 1] = '\0';
mode++;
}
if (strv_contains(modes, mode)) {
log_debug("Sleep mode '%s' is supported by kernel (%s).", mode, path);
return true;
}
}
if (DEBUG_LOGGING) {
_cleanup_free_ char *joined = strv_join(modes, " ");
log_debug("None of the configured modes are supported by kernel (%s): %s",
path, strnull(joined));
}
return false;
}
static int sleep_supported_internal(
const SleepConfig *sleep_config,
SleepOperation operation,
bool check_allowed,
SleepSupport *ret_support);
static int s2h_supported(const SleepConfig *sleep_config, SleepSupport *ret_support) {
static const SleepOperation operations[] = {
SLEEP_SUSPEND,
SLEEP_HIBERNATE,
};
SleepSupport support;
int r;
assert(sleep_config);
assert(ret_support);
if (!clock_supported(CLOCK_BOOTTIME_ALARM)) {
log_debug("CLOCK_BOOTTIME_ALARM is not supported, can't perform %s.", sleep_operation_to_string(SLEEP_SUSPEND_THEN_HIBERNATE));
*ret_support = SLEEP_ALARM_NOT_SUPPORTED;
return false;
}
FOREACH_ELEMENT(i, operations) {
r = sleep_supported_internal(sleep_config, *i, /* check_allowed = */ false, &support);
if (r < 0)
return r;
if (r == 0) {
log_debug("Sleep operation %s is not supported, can't perform %s.",
sleep_operation_to_string(*i), sleep_operation_to_string(SLEEP_SUSPEND_THEN_HIBERNATE));
*ret_support = support;
return false;
}
}
assert(support == SLEEP_SUPPORTED);
*ret_support = support;
return true;
}
static int sleep_supported_internal(
const SleepConfig *sleep_config,
SleepOperation operation,
bool check_allowed,
SleepSupport *ret_support) {
int r;
assert(sleep_config);
assert(operation >= 0);
assert(operation < _SLEEP_OPERATION_MAX);
assert(ret_support);
if (check_allowed && !sleep_config->allow[operation]) {
log_debug("Sleep operation %s is disabled by configuration.", sleep_operation_to_string(operation));
*ret_support = SLEEP_DISABLED;
return false;
}
if (operation == SLEEP_SUSPEND_THEN_HIBERNATE)
return s2h_supported(sleep_config, ret_support);
assert(operation < _SLEEP_OPERATION_CONFIG_MAX);
r = sleep_state_supported(sleep_config->states[operation]);
if (r == -ENOMSG) {
*ret_support = SLEEP_NOT_CONFIGURED;
return false;
}
if (r < 0)
return r;
if (r == 0) {
*ret_support = SLEEP_STATE_OR_MODE_NOT_SUPPORTED;
return false;
}
if (SLEEP_NEEDS_MEM_SLEEP(sleep_config, operation)) {
r = sleep_mode_supported("/sys/power/mem_sleep", sleep_config->mem_modes);
if (r < 0)
return r;
if (r == 0) {
*ret_support = SLEEP_STATE_OR_MODE_NOT_SUPPORTED;
return false;
}
}
if (SLEEP_OPERATION_IS_HIBERNATION(operation)) {
r = sleep_mode_supported("/sys/power/disk", sleep_config->modes[operation]);
if (r < 0)
return r;
if (r == 0) {
*ret_support = SLEEP_STATE_OR_MODE_NOT_SUPPORTED;
return false;
}
r = hibernation_is_safe();
if (r == -ENOTRECOVERABLE) {
*ret_support = SLEEP_RESUME_NOT_SUPPORTED;
return false;
}
if (r == -ENOSPC) {
*ret_support = SLEEP_NOT_ENOUGH_SWAP_SPACE;
return false;
}
if (r < 0)
return r;
} else
assert(!sleep_config->modes[operation]);
*ret_support = SLEEP_SUPPORTED;
return true;
}
int sleep_supported_full(SleepOperation operation, SleepSupport *ret_support) {
_cleanup_(sleep_config_freep) SleepConfig *sleep_config = NULL;
SleepSupport support;
int r;
assert(operation >= 0);
assert(operation < _SLEEP_OPERATION_MAX);
r = parse_sleep_config(&sleep_config);
if (r < 0)
return r;
r = sleep_supported_internal(sleep_config, operation, /* check_allowed = */ true, &support);
if (r < 0)
return r;
assert((r > 0) == (support == SLEEP_SUPPORTED));
if (ret_support)
*ret_support = support;
return r;
}
|