]>
Commit | Line | Data |
---|---|---|
19adb8a3 ZJS |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2013 Zbigniew Jędrzejewski-Szmek | |
5 | ||
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. | |
10 | ||
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. | |
15 | ||
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/>. | |
18 | ***/ | |
19 | ||
a8fbdf54 TA |
20 | #include <errno.h> |
21 | #include <stdbool.h> | |
22 | #include <stddef.h> | |
19adb8a3 | 23 | #include <stdio.h> |
a8fbdf54 TA |
24 | #include <string.h> |
25 | #include <syslog.h> | |
26 | #include <unistd.h> | |
19adb8a3 | 27 | |
b5efdb8a | 28 | #include "alloc-util.h" |
19adb8a3 | 29 | #include "conf-parser.h" |
a0f29c76 | 30 | #include "def.h" |
3ffd4af2 | 31 | #include "fd-util.h" |
19adb8a3 ZJS |
32 | #include "fileio.h" |
33 | #include "log.h" | |
a8fbdf54 | 34 | #include "macro.h" |
a0f29c76 | 35 | #include "parse-util.h" |
3ffd4af2 | 36 | #include "sleep-config.h" |
07630cea | 37 | #include "string-util.h" |
19adb8a3 | 38 | #include "strv.h" |
19adb8a3 | 39 | |
9ed794a3 | 40 | #define USE(x, y) do { (x) = (y); (y) = NULL; } while (0) |
dabeaa46 ZJS |
41 | |
42 | int parse_sleep_config(const char *verb, char ***_modes, char ***_states) { | |
34c10968 | 43 | |
19adb8a3 ZJS |
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; | |
dabeaa46 | 48 | char **modes, **states; |
19adb8a3 ZJS |
49 | |
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 }, | |
34c10968 LP |
57 | {} |
58 | }; | |
19adb8a3 | 59 | |
778b6a3f | 60 | config_parse_many(PKGSYSCONFDIR "/sleep.conf", |
75eb6154 | 61 | CONF_PATHS_NULSTR("systemd/sleep.conf.d"), |
778b6a3f JT |
62 | "Sleep\0", config_item_table_lookup, items, |
63 | false, NULL); | |
19adb8a3 | 64 | |
19adb8a3 ZJS |
65 | if (streq(verb, "suspend")) { |
66 | /* empty by default */ | |
dabeaa46 | 67 | USE(modes, suspend_mode); |
19adb8a3 ZJS |
68 | |
69 | if (suspend_state) | |
dabeaa46 | 70 | USE(states, suspend_state); |
19adb8a3 | 71 | else |
dabeaa46 | 72 | states = strv_new("mem", "standby", "freeze", NULL); |
19adb8a3 | 73 | |
19adb8a3 ZJS |
74 | } else if (streq(verb, "hibernate")) { |
75 | if (hibernate_mode) | |
dabeaa46 | 76 | USE(modes, hibernate_mode); |
19adb8a3 | 77 | else |
dabeaa46 | 78 | modes = strv_new("platform", "shutdown", NULL); |
19adb8a3 ZJS |
79 | |
80 | if (hibernate_state) | |
dabeaa46 | 81 | USE(states, hibernate_state); |
19adb8a3 | 82 | else |
dabeaa46 | 83 | states = strv_new("disk", NULL); |
19adb8a3 | 84 | |
19adb8a3 ZJS |
85 | } else if (streq(verb, "hybrid-sleep")) { |
86 | if (hybrid_mode) | |
dabeaa46 | 87 | USE(modes, hybrid_mode); |
19adb8a3 | 88 | else |
dabeaa46 | 89 | modes = strv_new("suspend", "platform", "shutdown", NULL); |
19adb8a3 ZJS |
90 | |
91 | if (hybrid_state) | |
dabeaa46 | 92 | USE(states, hybrid_state); |
19adb8a3 | 93 | else |
dabeaa46 | 94 | states = strv_new("disk", NULL); |
19adb8a3 | 95 | |
19adb8a3 ZJS |
96 | } else |
97 | assert_not_reached("what verb"); | |
98 | ||
dabeaa46 ZJS |
99 | if ((!modes && !streq(verb, "suspend")) || !states) { |
100 | strv_free(modes); | |
101 | strv_free(states); | |
19adb8a3 ZJS |
102 | return log_oom(); |
103 | } | |
104 | ||
dabeaa46 ZJS |
105 | *_modes = modes; |
106 | *_states = states; | |
19adb8a3 ZJS |
107 | return 0; |
108 | } | |
109 | ||
110 | int can_sleep_state(char **types) { | |
a2a5291b | 111 | char **type; |
19adb8a3 ZJS |
112 | int r; |
113 | _cleanup_free_ char *p = NULL; | |
114 | ||
115 | if (strv_isempty(types)) | |
116 | return true; | |
117 | ||
118 | /* If /sys is read-only we cannot sleep */ | |
119 | if (access("/sys/power/state", W_OK) < 0) | |
120 | return false; | |
121 | ||
122 | r = read_one_line_file("/sys/power/state", &p); | |
123 | if (r < 0) | |
124 | return false; | |
125 | ||
126 | STRV_FOREACH(type, types) { | |
a2a5291b | 127 | const char *word, *state; |
19adb8a3 ZJS |
128 | size_t l, k; |
129 | ||
130 | k = strlen(*type); | |
a2a5291b ZJS |
131 | FOREACH_WORD_SEPARATOR(word, l, p, WHITESPACE, state) |
132 | if (l == k && memcmp(word, *type, l) == 0) | |
19adb8a3 ZJS |
133 | return true; |
134 | } | |
135 | ||
136 | return false; | |
137 | } | |
138 | ||
139 | int can_sleep_disk(char **types) { | |
a2a5291b | 140 | char **type; |
19adb8a3 ZJS |
141 | int r; |
142 | _cleanup_free_ char *p = NULL; | |
143 | ||
144 | if (strv_isempty(types)) | |
145 | return true; | |
146 | ||
147 | /* If /sys is read-only we cannot sleep */ | |
148 | if (access("/sys/power/disk", W_OK) < 0) | |
149 | return false; | |
150 | ||
151 | r = read_one_line_file("/sys/power/disk", &p); | |
152 | if (r < 0) | |
153 | return false; | |
154 | ||
155 | STRV_FOREACH(type, types) { | |
a2a5291b | 156 | const char *word, *state; |
19adb8a3 ZJS |
157 | size_t l, k; |
158 | ||
159 | k = strlen(*type); | |
a2a5291b ZJS |
160 | FOREACH_WORD_SEPARATOR(word, l, p, WHITESPACE, state) { |
161 | if (l == k && memcmp(word, *type, l) == 0) | |
19adb8a3 ZJS |
162 | return true; |
163 | ||
a2a5291b ZJS |
164 | if (l == k + 2 && |
165 | word[0] == '[' && | |
166 | memcmp(word + 1, *type, l - 2) == 0 && | |
167 | word[l-1] == ']') | |
19adb8a3 ZJS |
168 | return true; |
169 | } | |
170 | } | |
171 | ||
172 | return false; | |
173 | } | |
174 | ||
69ab8088 ZJS |
175 | #define HIBERNATION_SWAP_THRESHOLD 0.98 |
176 | ||
9fb3675e ZJS |
177 | static int hibernation_partition_size(size_t *size, size_t *used) { |
178 | _cleanup_fclose_ FILE *f; | |
1fa2f38f | 179 | unsigned i; |
9fb3675e ZJS |
180 | |
181 | assert(size); | |
182 | assert(used); | |
183 | ||
c8a202b7 | 184 | f = fopen("/proc/swaps", "re"); |
9fb3675e ZJS |
185 | if (!f) { |
186 | log_full(errno == ENOENT ? LOG_DEBUG : LOG_WARNING, | |
187 | "Failed to retrieve open /proc/swaps: %m"); | |
188 | assert(errno > 0); | |
189 | return -errno; | |
190 | } | |
69ab8088 | 191 | |
9fb3675e ZJS |
192 | (void) fscanf(f, "%*s %*s %*s %*s %*s\n"); |
193 | ||
194 | for (i = 1;; i++) { | |
db69869f | 195 | _cleanup_free_ char *dev = NULL, *type = NULL; |
9fb3675e ZJS |
196 | size_t size_field, used_field; |
197 | int k; | |
198 | ||
199 | k = fscanf(f, | |
200 | "%ms " /* device/file */ | |
201 | "%ms " /* type of swap */ | |
1fa2f38f ZJS |
202 | "%zu " /* swap size */ |
203 | "%zu " /* used */ | |
9fb3675e ZJS |
204 | "%*i\n", /* priority */ |
205 | &dev, &type, &size_field, &used_field); | |
206 | if (k != 4) { | |
207 | if (k == EOF) | |
208 | break; | |
209 | ||
210 | log_warning("Failed to parse /proc/swaps:%u", i); | |
211 | continue; | |
212 | } | |
213 | ||
db69869f DR |
214 | if (streq(type, "partition") && endswith(dev, "\\040(deleted)")) { |
215 | log_warning("Ignoring deleted swapfile '%s'.", dev); | |
9fb3675e ZJS |
216 | continue; |
217 | } | |
218 | ||
219 | *size = size_field; | |
220 | *used = used_field; | |
221 | return 0; | |
69ab8088 ZJS |
222 | } |
223 | ||
9fb3675e ZJS |
224 | log_debug("No swap partitions were found."); |
225 | return -ENOSYS; | |
226 | } | |
227 | ||
228 | static bool enough_memory_for_hibernation(void) { | |
229 | _cleanup_free_ char *active = NULL; | |
39883f62 LP |
230 | unsigned long long act = 0; |
231 | size_t size = 0, used = 0; | |
9fb3675e ZJS |
232 | int r; |
233 | ||
234 | r = hibernation_partition_size(&size, &used); | |
235 | if (r < 0) | |
69ab8088 | 236 | return false; |
69ab8088 | 237 | |
c4cd1d4d | 238 | r = get_proc_field("/proc/meminfo", "Active(anon)", WHITESPACE, &active); |
69ab8088 | 239 | if (r < 0) { |
da927ba9 | 240 | log_error_errno(r, "Failed to retrieve Active(anon) from /proc/meminfo: %m"); |
69ab8088 ZJS |
241 | return false; |
242 | } | |
243 | ||
244 | r = safe_atollu(active, &act); | |
245 | if (r < 0) { | |
c33b3297 MS |
246 | log_error_errno(r, "Failed to parse Active(anon) from /proc/meminfo: %s: %m", |
247 | active); | |
69ab8088 ZJS |
248 | return false; |
249 | } | |
250 | ||
9fb3675e ZJS |
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); | |
69ab8088 ZJS |
254 | |
255 | return r; | |
256 | } | |
257 | ||
19adb8a3 ZJS |
258 | int can_sleep(const char *verb) { |
259 | _cleanup_strv_free_ char **modes = NULL, **states = NULL; | |
260 | int r; | |
261 | ||
262 | assert(streq(verb, "suspend") || | |
263 | streq(verb, "hibernate") || | |
264 | streq(verb, "hybrid-sleep")); | |
265 | ||
266 | r = parse_sleep_config(verb, &modes, &states); | |
267 | if (r < 0) | |
268 | return false; | |
269 | ||
69ab8088 ZJS |
270 | if (!can_sleep_state(states) || !can_sleep_disk(modes)) |
271 | return false; | |
272 | ||
273 | return streq(verb, "suspend") || enough_memory_for_hibernation(); | |
19adb8a3 | 274 | } |