chiark / gitweb /
Prep v229: Add missing fixes from upstream [6/6] src/systemd
[elogind.git] / src / shared / sleep-config.c
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
20 #include <errno.h>
21 #include <stdbool.h>
22 #include <stddef.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <syslog.h>
26 #include <unistd.h>
27
28 #include "alloc-util.h"
29 #include "conf-parser.h"
30 #include "def.h"
31 #include "fd-util.h"
32 #include "fileio.h"
33 #include "log.h"
34 #include "macro.h"
35 #include "parse-util.h"
36 #include "sleep-config.h"
37 #include "string-util.h"
38 #include "strv.h"
39
40 #define USE(x, y) do{ (x) = (y); (y) = NULL; } while(0)
41
42 int parse_sleep_config(const char *verb, char ***_modes, char ***_states) {
43
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;
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 },
57                 {}
58         };
59
60         config_parse_many(PKGSYSCONFDIR "/sleep.conf",
61                           CONF_PATHS_NULSTR("systemd/sleep.conf.d"),
62                           "Sleep\0", config_item_table_lookup, items,
63                           false, NULL);
64
65         if (streq(verb, "suspend")) {
66                 /* empty by default */
67                 USE(modes, suspend_mode);
68
69                 if (suspend_state)
70                         USE(states, suspend_state);
71                 else
72                         states = strv_new("mem", "standby", "freeze", NULL);
73
74         } else if (streq(verb, "hibernate")) {
75                 if (hibernate_mode)
76                         USE(modes, hibernate_mode);
77                 else
78                         modes = strv_new("platform", "shutdown", NULL);
79
80                 if (hibernate_state)
81                         USE(states, hibernate_state);
82                 else
83                         states = strv_new("disk", NULL);
84
85         } else if (streq(verb, "hybrid-sleep")) {
86                 if (hybrid_mode)
87                         USE(modes, hybrid_mode);
88                 else
89                         modes = strv_new("suspend", "platform", "shutdown", NULL);
90
91                 if (hybrid_state)
92                         USE(states, hybrid_state);
93                 else
94                         states = strv_new("disk", NULL);
95
96         } else
97                 assert_not_reached("what verb");
98
99         if ((!modes && !streq(verb, "suspend")) || !states) {
100                 strv_free(modes);
101                 strv_free(states);
102                 return log_oom();
103         }
104
105         *_modes = modes;
106         *_states = states;
107         return 0;
108 }
109
110 int can_sleep_state(char **types) {
111         char **type;
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) {
127                 const char *word, *state;
128                 size_t l, k;
129
130                 k = strlen(*type);
131                 FOREACH_WORD_SEPARATOR(word, l, p, WHITESPACE, state)
132                         if (l == k && memcmp(word, *type, l) == 0)
133                                 return true;
134         }
135
136         return false;
137 }
138
139 int can_sleep_disk(char **types) {
140         char **type;
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) {
156                 const char *word, *state;
157                 size_t l, k;
158
159                 k = strlen(*type);
160                 FOREACH_WORD_SEPARATOR(word, l, p, WHITESPACE, state) {
161                         if (l == k && memcmp(word, *type, l) == 0)
162                                 return true;
163
164                         if (l == k + 2 &&
165                             word[0] == '[' &&
166                             memcmp(word + 1, *type, l - 2) == 0 &&
167                             word[l-1] == ']')
168                                 return true;
169                 }
170         }
171
172         return false;
173 }
174
175 #define HIBERNATION_SWAP_THRESHOLD 0.98
176
177 static int hibernation_partition_size(size_t *size, size_t *used) {
178         _cleanup_fclose_ FILE *f;
179         unsigned i;
180
181         assert(size);
182         assert(used);
183
184         f = fopen("/proc/swaps", "re");
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         }
191
192         (void) fscanf(f, "%*s %*s %*s %*s %*s\n");
193
194         for (i = 1;; i++) {
195                 _cleanup_free_ char *dev = NULL, *type = NULL;
196                 size_t size_field, used_field;
197                 int k;
198
199                 k = fscanf(f,
200                            "%ms "   /* device/file */
201                            "%ms "   /* type of swap */
202                            "%zu "   /* swap size */
203                            "%zu "   /* used */
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
214                 if (streq(type, "partition") && endswith(dev, "\\040(deleted)")) {
215                         log_warning("Ignoring deleted swapfile '%s'.", dev);
216                         continue;
217                 }
218
219                 *size = size_field;
220                 *used = used_field;
221                 return 0;
222         }
223
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;
230         unsigned long long act = 0;
231         size_t size = 0, used = 0;
232         int r;
233
234         r = hibernation_partition_size(&size, &used);
235         if (r < 0)
236                 return false;
237
238         r = get_proc_field("/proc/meminfo", "Active(anon)", WHITESPACE, &active);
239         if (r < 0) {
240                 log_error_errno(r, "Failed to retrieve Active(anon) from /proc/meminfo: %m");
241                 return false;
242         }
243
244         r = safe_atollu(active, &act);
245         if (r < 0) {
246                 log_error_errno(r, "Failed to parse Active(anon) from /proc/meminfo: %s: %m",
247                                 active);
248                 return false;
249         }
250
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);
254
255         return r;
256 }
257
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
270         if (!can_sleep_state(states) || !can_sleep_disk(modes))
271                 return false;
272
273         return streq(verb, "suspend") || enough_memory_for_hibernation();
274 }