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 int parse_sleep_config(const char *verb, char ***_modes, char ***_states) {
44 _cleanup_strv_free_ char
45 **suspend_mode = NULL, **suspend_state = NULL,
46 **hibernate_mode = NULL, **hibernate_state = NULL,
47 **hybrid_mode = NULL, **hybrid_state = NULL;
48 char **modes, **states;
50 const ConfigTableItem items[] = {
51 { "Sleep", "SuspendMode", config_parse_strv, 0, &suspend_mode },
52 { "Sleep", "SuspendState", config_parse_strv, 0, &suspend_state },
53 { "Sleep", "HibernateMode", config_parse_strv, 0, &hibernate_mode },
54 { "Sleep", "HibernateState", config_parse_strv, 0, &hibernate_state },
55 { "Sleep", "HybridSleepMode", config_parse_strv, 0, &hybrid_mode },
56 { "Sleep", "HybridSleepState", config_parse_strv, 0, &hybrid_state },
60 config_parse_many(PKGSYSCONFDIR "/sleep.conf",
61 CONF_PATHS_NULSTR("systemd/sleep.conf.d"),
62 "Sleep\0", config_item_table_lookup, items,
65 if (streq(verb, "suspend")) {
66 /* empty by default */
67 USE(modes, suspend_mode);
70 USE(states, suspend_state);
72 states = strv_new("mem", "standby", "freeze", NULL);
74 } else if (streq(verb, "hibernate")) {
76 USE(modes, hibernate_mode);
78 modes = strv_new("platform", "shutdown", NULL);
81 USE(states, hibernate_state);
83 states = strv_new("disk", NULL);
85 } else if (streq(verb, "hybrid-sleep")) {
87 USE(modes, hybrid_mode);
89 modes = strv_new("suspend", "platform", "shutdown", NULL);
92 USE(states, hybrid_state);
94 states = strv_new("disk", NULL);
97 assert_not_reached("what verb");
99 if ((!modes && !streq(verb, "suspend")) || !states) {
110 int can_sleep_state(char **types) {
113 _cleanup_free_ char *p = NULL;
115 if (strv_isempty(types))
118 /* If /sys is read-only we cannot sleep */
119 if (access("/sys/power/state", W_OK) < 0)
122 r = read_one_line_file("/sys/power/state", &p);
126 STRV_FOREACH(type, types) {
127 const char *word, *state;
131 FOREACH_WORD_SEPARATOR(word, l, p, WHITESPACE, state)
132 if (l == k && memcmp(word, *type, l) == 0)
139 int can_sleep_disk(char **types) {
142 _cleanup_free_ char *p = NULL;
144 if (strv_isempty(types))
147 /* If /sys is read-only we cannot sleep */
148 if (access("/sys/power/disk", W_OK) < 0)
151 r = read_one_line_file("/sys/power/disk", &p);
155 STRV_FOREACH(type, types) {
156 const char *word, *state;
160 FOREACH_WORD_SEPARATOR(word, l, p, WHITESPACE, state) {
161 if (l == k && memcmp(word, *type, l) == 0)
166 memcmp(word + 1, *type, l - 2) == 0 &&
175 #define HIBERNATION_SWAP_THRESHOLD 0.98
177 static int hibernation_partition_size(size_t *size, size_t *used) {
178 _cleanup_fclose_ FILE *f;
184 f = fopen("/proc/swaps", "re");
186 log_full(errno == ENOENT ? LOG_DEBUG : LOG_WARNING,
187 "Failed to retrieve open /proc/swaps: %m");
192 (void) fscanf(f, "%*s %*s %*s %*s %*s\n");
195 _cleanup_free_ char *dev = NULL, *type = NULL;
196 size_t size_field, used_field;
200 "%ms " /* device/file */
201 "%ms " /* type of swap */
202 "%zu " /* swap size */
204 "%*i\n", /* priority */
205 &dev, &type, &size_field, &used_field);
210 log_warning("Failed to parse /proc/swaps:%u", i);
214 if (streq(type, "partition") && endswith(dev, "\\040(deleted)")) {
215 log_warning("Ignoring deleted swapfile '%s'.", dev);
224 log_debug("No swap partitions were found.");
228 static bool enough_memory_for_hibernation(void) {
229 _cleanup_free_ char *active = NULL;
230 unsigned long long act = 0;
231 size_t size = 0, used = 0;
234 r = hibernation_partition_size(&size, &used);
238 r = get_proc_field("/proc/meminfo", "Active(anon)", WHITESPACE, &active);
240 log_error_errno(r, "Failed to retrieve Active(anon) from /proc/meminfo: %m");
244 r = safe_atollu(active, &act);
246 log_error_errno(r, "Failed to parse Active(anon) from /proc/meminfo: %s: %m",
251 r = act <= (size - used) * HIBERNATION_SWAP_THRESHOLD;
252 log_debug("Hibernation is %spossible, Active(anon)=%llu kB, size=%zu kB, used=%zu kB, threshold=%.2g%%",
253 r ? "" : "im", act, size, used, 100*HIBERNATION_SWAP_THRESHOLD);
258 int can_sleep(const char *verb) {
259 _cleanup_strv_free_ char **modes = NULL, **states = NULL;
262 assert(streq(verb, "suspend") ||
263 streq(verb, "hibernate") ||
264 streq(verb, "hybrid-sleep"));
266 r = parse_sleep_config(verb, &modes, &states);
270 if (!can_sleep_state(states) || !can_sleep_disk(modes))
273 return streq(verb, "suspend") || enough_memory_for_hibernation();