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/>.
28 #include "alloc-util.h"
29 #include "conf-parser.h"
35 #include "parse-util.h"
36 #include "sleep-config.h"
37 #include "string-util.h"
40 #define USE(x, y) do{ (x) = (y); (y) = NULL; } while(0)
42 #if 0 /// UNNEEDED by elogind
43 int parse_sleep_config(const char *verb, char ***_modes, char ***_states) {
45 /// really only used in here.
46 static int parse_sleep_config(const char *verb, char ***_modes, char ***_states) {
48 _cleanup_strv_free_ char
49 **suspend_mode = NULL, **suspend_state = NULL,
50 **hibernate_mode = NULL, **hibernate_state = NULL,
51 **hybrid_mode = NULL, **hybrid_state = NULL;
52 char **modes, **states;
54 const ConfigTableItem items[] = {
55 { "Sleep", "SuspendMode", config_parse_strv, 0, &suspend_mode },
56 { "Sleep", "SuspendState", config_parse_strv, 0, &suspend_state },
57 { "Sleep", "HibernateMode", config_parse_strv, 0, &hibernate_mode },
58 { "Sleep", "HibernateState", config_parse_strv, 0, &hibernate_state },
59 { "Sleep", "HybridSleepMode", config_parse_strv, 0, &hybrid_mode },
60 { "Sleep", "HybridSleepState", config_parse_strv, 0, &hybrid_state },
64 #if 0 /// elogind has its own config file
65 config_parse_many(PKGSYSCONFDIR "/sleep.conf",
66 CONF_PATHS_NULSTR("systemd/sleep.conf.d"),
67 "Sleep\0", config_item_table_lookup, items,
70 const char* logind_conf = getenv("ELOGIND_CONF_FILE");
72 logind_conf = PKGSYSCONFDIR "/logind.conf";
73 config_parse(NULL, logind_conf, NULL, "Sleep\0",
74 config_item_table_lookup, items, false,
78 if (streq(verb, "suspend")) {
79 /* empty by default */
80 USE(modes, suspend_mode);
83 USE(states, suspend_state);
85 states = strv_new("mem", "standby", "freeze", NULL);
87 } else if (streq(verb, "hibernate")) {
89 USE(modes, hibernate_mode);
91 modes = strv_new("platform", "shutdown", NULL);
94 USE(states, hibernate_state);
96 states = strv_new("disk", NULL);
98 } else if (streq(verb, "hybrid-sleep")) {
100 USE(modes, hybrid_mode);
102 modes = strv_new("suspend", "platform", "shutdown", NULL);
105 USE(states, hybrid_state);
107 states = strv_new("disk", NULL);
110 assert_not_reached("what verb");
112 if ((!modes && !streq(verb, "suspend")) || !states) {
123 int can_sleep_state(char **types) {
126 _cleanup_free_ char *p = NULL;
128 if (strv_isempty(types))
131 /* If /sys is read-only we cannot sleep */
132 if (access("/sys/power/state", W_OK) < 0)
135 r = read_one_line_file("/sys/power/state", &p);
139 STRV_FOREACH(type, types) {
140 const char *word, *state;
144 FOREACH_WORD_SEPARATOR(word, l, p, WHITESPACE, state)
145 if (l == k && memcmp(word, *type, l) == 0)
152 int can_sleep_disk(char **types) {
155 _cleanup_free_ char *p = NULL;
157 if (strv_isempty(types))
160 /* If /sys is read-only we cannot sleep */
161 if (access("/sys/power/disk", W_OK) < 0)
164 r = read_one_line_file("/sys/power/disk", &p);
168 STRV_FOREACH(type, types) {
169 const char *word, *state;
173 FOREACH_WORD_SEPARATOR(word, l, p, WHITESPACE, state) {
174 if (l == k && memcmp(word, *type, l) == 0)
179 memcmp(word + 1, *type, l - 2) == 0 &&
188 #define HIBERNATION_SWAP_THRESHOLD 0.98
190 static int hibernation_partition_size(size_t *size, size_t *used) {
191 _cleanup_fclose_ FILE *f;
197 f = fopen("/proc/swaps", "re");
199 log_full(errno == ENOENT ? LOG_DEBUG : LOG_WARNING,
200 "Failed to retrieve open /proc/swaps: %m");
205 (void) fscanf(f, "%*s %*s %*s %*s %*s\n");
208 _cleanup_free_ char *dev = NULL, *type = NULL;
209 size_t size_field, used_field;
213 "%ms " /* device/file */
214 "%ms " /* type of swap */
215 "%zu " /* swap size */
217 "%*i\n", /* priority */
218 &dev, &type, &size_field, &used_field);
223 log_warning("Failed to parse /proc/swaps:%u", i);
227 if (streq(type, "partition") && endswith(dev, "\\040(deleted)")) {
228 log_warning("Ignoring deleted swapfile '%s'.", dev);
237 log_debug("No swap partitions were found.");
241 static bool enough_memory_for_hibernation(void) {
242 _cleanup_free_ char *active = NULL;
243 unsigned long long act = 0;
244 size_t size = 0, used = 0;
247 r = hibernation_partition_size(&size, &used);
251 r = get_proc_field("/proc/meminfo", "Active(anon)", WHITESPACE, &active);
253 log_error_errno(r, "Failed to retrieve Active(anon) from /proc/meminfo: %m");
257 r = safe_atollu(active, &act);
259 log_error_errno(r, "Failed to parse Active(anon) from /proc/meminfo: %s: %m",
264 r = act <= (size - used) * HIBERNATION_SWAP_THRESHOLD;
265 log_debug("Hibernation is %spossible, Active(anon)=%llu kB, size=%zu kB, used=%zu kB, threshold=%.2g%%",
266 r ? "" : "im", act, size, used, 100*HIBERNATION_SWAP_THRESHOLD);
271 int can_sleep(const char *verb) {
272 _cleanup_strv_free_ char **modes = NULL, **states = NULL;
275 assert(streq(verb, "suspend") ||
276 streq(verb, "hibernate") ||
277 streq(verb, "hybrid-sleep"));
279 r = parse_sleep_config(verb, &modes, &states);
283 if (!can_sleep_state(states) || !can_sleep_disk(modes))
286 return streq(verb, "suspend") || enough_memory_for_hibernation();