2 This file is part of systemd.
4 Copyright 2013 Zbigniew Jędrzejewski-Szmek
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.
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.
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/>.
21 //#include <stdbool.h>
28 #include "alloc-util.h"
29 //#include "conf-parser.h"
31 //#include "env-util.h"
36 #include "parse-util.h"
37 #include "sleep-config.h"
38 #include "string-util.h"
41 #if 0 /// UNNEEDED by elogind
42 #define USE(x, y) do { (x) = (y); (y) = NULL; } while (0)
44 int parse_sleep_config(const char *verb, char ***_modes, char ***_states) {
46 _cleanup_strv_free_ char
47 **suspend_mode = NULL, **suspend_state = NULL,
48 **hibernate_mode = NULL, **hibernate_state = NULL,
49 **hybrid_mode = NULL, **hybrid_state = NULL;
50 char **modes, **states;
52 const ConfigTableItem items[] = {
53 { "Sleep", "SuspendMode", config_parse_strv, 0, &suspend_mode },
54 { "Sleep", "SuspendState", config_parse_strv, 0, &suspend_state },
55 { "Sleep", "HibernateMode", config_parse_strv, 0, &hibernate_mode },
56 { "Sleep", "HibernateState", config_parse_strv, 0, &hibernate_state },
57 { "Sleep", "HybridSleepMode", config_parse_strv, 0, &hybrid_mode },
58 { "Sleep", "HybridSleepState", config_parse_strv, 0, &hybrid_state },
62 config_parse_many_nulstr(PKGSYSCONFDIR "/sleep.conf",
63 CONF_PATHS_NULSTR("systemd/sleep.conf.d"),
64 "Sleep\0", config_item_table_lookup, items,
67 if (streq(verb, "suspend")) {
68 /* empty by default */
69 USE(modes, suspend_mode);
72 USE(states, suspend_state);
74 states = strv_new("mem", "standby", "freeze", NULL);
76 } else if (streq(verb, "hibernate")) {
78 USE(modes, hibernate_mode);
80 modes = strv_new("platform", "shutdown", NULL);
83 USE(states, hibernate_state);
85 states = strv_new("disk", NULL);
87 } else if (streq(verb, "hybrid-sleep")) {
89 USE(modes, hybrid_mode);
91 modes = strv_new("suspend", "platform", "shutdown", NULL);
94 USE(states, hybrid_state);
96 states = strv_new("disk", NULL);
99 assert_not_reached("what verb");
101 if ((!modes && !streq(verb, "suspend")) || !states) {
113 #if 1 /// Only available in this file for elogind
116 int can_sleep_state(char **types) {
119 _cleanup_free_ char *p = NULL;
121 if (strv_isempty(types))
124 /* If /sys is read-only we cannot sleep */
125 if (access("/sys/power/state", W_OK) < 0)
128 r = read_one_line_file("/sys/power/state", &p);
132 STRV_FOREACH(type, types) {
133 const char *word, *state;
137 FOREACH_WORD_SEPARATOR(word, l, p, WHITESPACE, state)
138 if (l == k && memcmp(word, *type, l) == 0)
145 #if 1 /// Only available in this file for elogind
148 int can_sleep_disk(char **types) {
151 _cleanup_free_ char *p = NULL;
153 if (strv_isempty(types))
156 /* If /sys is read-only we cannot sleep */
157 if (access("/sys/power/disk", W_OK) < 0)
160 r = read_one_line_file("/sys/power/disk", &p);
164 STRV_FOREACH(type, types) {
165 const char *word, *state;
169 FOREACH_WORD_SEPARATOR(word, l, p, WHITESPACE, state) {
170 if (l == k && memcmp(word, *type, l) == 0)
175 memcmp(word + 1, *type, l - 2) == 0 &&
184 #define HIBERNATION_SWAP_THRESHOLD 0.98
186 static int hibernation_partition_size(size_t *size, size_t *used) {
187 _cleanup_fclose_ FILE *f;
193 f = fopen("/proc/swaps", "re");
195 log_full(errno == ENOENT ? LOG_DEBUG : LOG_WARNING,
196 "Failed to retrieve open /proc/swaps: %m");
201 (void) fscanf(f, "%*s %*s %*s %*s %*s\n");
204 _cleanup_free_ char *dev = NULL, *type = NULL;
205 size_t size_field, used_field;
209 "%ms " /* device/file */
210 "%ms " /* type of swap */
211 "%zu " /* swap size */
213 "%*i\n", /* priority */
214 &dev, &type, &size_field, &used_field);
219 log_warning("Failed to parse /proc/swaps:%u", i);
223 if (streq(type, "partition") && endswith(dev, "\\040(deleted)")) {
224 log_warning("Ignoring deleted swapfile '%s'.", dev);
233 log_debug("No swap partitions were found.");
237 static bool enough_memory_for_hibernation(void) {
238 _cleanup_free_ char *active = NULL;
239 unsigned long long act = 0;
240 size_t size = 0, used = 0;
243 #if 0 /// elogind does not allow any bypass, we are never init!
244 if (getenv_bool("SYSTEMD_BYPASS_HIBERNATION_MEMORY_CHECK") > 0)
248 r = hibernation_partition_size(&size, &used);
252 r = get_proc_field("/proc/meminfo", "Active(anon)", WHITESPACE, &active);
254 log_error_errno(r, "Failed to retrieve Active(anon) from /proc/meminfo: %m");
258 r = safe_atollu(active, &act);
260 log_error_errno(r, "Failed to parse Active(anon) from /proc/meminfo: %s: %m",
265 r = act <= (size - used) * HIBERNATION_SWAP_THRESHOLD;
266 log_debug("Hibernation is %spossible, Active(anon)=%llu kB, size=%zu kB, used=%zu kB, threshold=%.2g%%",
267 r ? "" : "im", act, size, used, 100*HIBERNATION_SWAP_THRESHOLD);
272 #if 0 /// elogind has to do, or better, *can* do it differently
273 int can_sleep(const char *verb) {
274 _cleanup_strv_free_ char **modes = NULL, **states = NULL;
277 assert(streq(verb, "suspend") ||
278 streq(verb, "hibernate") ||
279 streq(verb, "hybrid-sleep"));
281 r = parse_sleep_config(verb, &modes, &states);
285 if (!can_sleep_state(states) || !can_sleep_disk(modes))
288 return streq(verb, "suspend") || enough_memory_for_hibernation();
291 int can_sleep(Manager *m, const char *verb) {
293 assert(streq(verb, "suspend") ||
294 streq(verb, "hibernate") ||
295 streq(verb, "hybrid-sleep"));
297 if ( streq(verb, "suspend")
298 && ( !can_sleep_state(m->suspend_state)
299 || !can_sleep_disk(m->suspend_mode) ) )
302 if ( streq(verb, "hibernate")
303 && ( !can_sleep_state(m->hibernate_state)
304 || !can_sleep_disk(m->hibernate_mode) ) )
307 if ( streq(verb, "hybrid-sleep")
308 && ( !can_sleep_state(m->hybrid_sleep_state)
309 || !can_sleep_disk(m->hybrid_sleep_mode) ) )
313 return streq(verb, "suspend") || enough_memory_for_hibernation();