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"
36 #include "parse-util.h"
37 #include "sleep-config.h"
38 #include "string-util.h"
41 #define USE(x, y) do { (x) = (y); (y) = NULL; } while (0)
43 int parse_sleep_config(const char *verb
, char ***_modes
, char ***_states
) {
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
;
49 char **modes
, **states
;
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
},
61 config_parse_many(PKGSYSCONFDIR
"/sleep.conf",
62 CONF_PATHS_NULSTR("systemd/sleep.conf.d"),
63 "Sleep\0", config_item_table_lookup
, items
,
66 if (streq(verb
, "suspend")) {
67 /* empty by default */
68 USE(modes
, suspend_mode
);
71 USE(states
, suspend_state
);
73 states
= strv_new("mem", "standby", "freeze", NULL
);
75 } else if (streq(verb
, "hibernate")) {
77 USE(modes
, hibernate_mode
);
79 modes
= strv_new("platform", "shutdown", NULL
);
82 USE(states
, hibernate_state
);
84 states
= strv_new("disk", NULL
);
86 } else if (streq(verb
, "hybrid-sleep")) {
88 USE(modes
, hybrid_mode
);
90 modes
= strv_new("suspend", "platform", "shutdown", NULL
);
93 USE(states
, hybrid_state
);
95 states
= strv_new("disk", NULL
);
98 assert_not_reached("what verb");
100 if ((!modes
&& !streq(verb
, "suspend")) || !states
) {
111 int can_sleep_state(char **types
) {
114 _cleanup_free_
char *p
= NULL
;
116 if (strv_isempty(types
))
119 /* If /sys is read-only we cannot sleep */
120 if (access("/sys/power/state", W_OK
) < 0)
123 r
= read_one_line_file("/sys/power/state", &p
);
127 STRV_FOREACH(type
, types
) {
128 const char *word
, *state
;
132 FOREACH_WORD_SEPARATOR(word
, l
, p
, WHITESPACE
, state
)
133 if (l
== k
&& memcmp(word
, *type
, l
) == 0)
140 int can_sleep_disk(char **types
) {
143 _cleanup_free_
char *p
= NULL
;
145 if (strv_isempty(types
))
148 /* If /sys is read-only we cannot sleep */
149 if (access("/sys/power/disk", W_OK
) < 0)
152 r
= read_one_line_file("/sys/power/disk", &p
);
156 STRV_FOREACH(type
, types
) {
157 const char *word
, *state
;
161 FOREACH_WORD_SEPARATOR(word
, l
, p
, WHITESPACE
, state
) {
162 if (l
== k
&& memcmp(word
, *type
, l
) == 0)
167 memcmp(word
+ 1, *type
, l
- 2) == 0 &&
176 #define HIBERNATION_SWAP_THRESHOLD 0.98
178 static int hibernation_partition_size(size_t *size
, size_t *used
) {
179 _cleanup_fclose_
FILE *f
;
185 f
= fopen("/proc/swaps", "re");
187 log_full(errno
== ENOENT
? LOG_DEBUG
: LOG_WARNING
,
188 "Failed to retrieve open /proc/swaps: %m");
193 (void) fscanf(f
, "%*s %*s %*s %*s %*s\n");
196 _cleanup_free_
char *dev
= NULL
, *type
= NULL
;
197 size_t size_field
, used_field
;
201 "%ms " /* device/file */
202 "%ms " /* type of swap */
203 "%zu " /* swap size */
205 "%*i\n", /* priority */
206 &dev
, &type
, &size_field
, &used_field
);
211 log_warning("Failed to parse /proc/swaps:%u", i
);
215 if (streq(type
, "partition") && endswith(dev
, "\\040(deleted)")) {
216 log_warning("Ignoring deleted swapfile '%s'.", dev
);
225 log_debug("No swap partitions were found.");
229 static bool enough_memory_for_hibernation(void) {
230 _cleanup_free_
char *active
= NULL
;
231 unsigned long long act
= 0;
232 size_t size
= 0, used
= 0;
235 if (getenv_bool("SYSTEMD_BYPASS_HIBERNATION_MEMORY_CHECK") > 0)
238 r
= hibernation_partition_size(&size
, &used
);
242 r
= get_proc_field("/proc/meminfo", "Active(anon)", WHITESPACE
, &active
);
244 log_error_errno(r
, "Failed to retrieve Active(anon) from /proc/meminfo: %m");
248 r
= safe_atollu(active
, &act
);
250 log_error_errno(r
, "Failed to parse Active(anon) from /proc/meminfo: %s: %m",
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
);
262 int can_sleep(const char *verb
) {
263 _cleanup_strv_free_
char **modes
= NULL
, **states
= NULL
;
266 assert(streq(verb
, "suspend") ||
267 streq(verb
, "hibernate") ||
268 streq(verb
, "hybrid-sleep"));
270 r
= parse_sleep_config(verb
, &modes
, &states
);
274 if (!can_sleep_state(states
) || !can_sleep_disk(modes
))
277 return streq(verb
, "suspend") || enough_memory_for_hibernation();