chiark / gitweb /
logind: introduce LockedHint and SetLockedHint (#3238)
[elogind.git] / src / login / logind-sleep.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 "sd-messages.h"
29
30 //#include "alloc-util.h"
31 //#include "conf-parser.h"
32 //#include "def.h"
33 #include "fd-util.h"
34 #include "fileio.h"
35 #include "log.h"
36 #include "logind-sleep.h"
37 //#include "macro.h"
38 #include "parse-util.h"
39 #include "string-util.h"
40 #include "strv.h"
41
42 static int can_sleep_state(char **types) {
43         char **type;
44         int r;
45         _cleanup_free_ char *p = NULL;
46
47         if (strv_isempty(types))
48                 return true;
49
50         /* If /sys is read-only we cannot sleep */
51         if (access("/sys/power/state", W_OK) < 0)
52                 return false;
53
54         r = read_one_line_file("/sys/power/state", &p);
55         if (r < 0)
56                 return false;
57
58         STRV_FOREACH(type, types) {
59                 const char *word, *state;
60                 size_t l, k;
61
62                 k = strlen(*type);
63                 FOREACH_WORD_SEPARATOR(word, l, p, WHITESPACE, state)
64                         if (l == k && memcmp(word, *type, l) == 0)
65                                 return true;
66         }
67
68         return false;
69 }
70
71 static int can_sleep_disk(char **types) {
72         char **type;
73         int r;
74         _cleanup_free_ char *p = NULL;
75
76         if (strv_isempty(types))
77                 return true;
78
79         /* If /sys is read-only we cannot sleep */
80         if (access("/sys/power/disk", W_OK) < 0)
81                 return false;
82
83         r = read_one_line_file("/sys/power/disk", &p);
84         if (r < 0)
85                 return false;
86
87         STRV_FOREACH(type, types) {
88                 const char *word, *state;
89                 size_t l, k;
90
91                 k = strlen(*type);
92                 FOREACH_WORD_SEPARATOR(word, l, p, WHITESPACE, state) {
93                         if (l == k && memcmp(word, *type, l) == 0)
94                                 return true;
95
96                         if (l == k + 2 &&
97                             word[0] == '[' &&
98                             memcmp(word + 1, *type, l - 2) == 0 &&
99                             word[l-1] == ']')
100                                 return true;
101                 }
102         }
103
104         return false;
105 }
106
107 #define HIBERNATION_SWAP_THRESHOLD 0.98
108
109 static int hibernation_partition_size(size_t *size, size_t *used) {
110         _cleanup_fclose_ FILE *f;
111         unsigned i;
112
113         assert(size);
114         assert(used);
115
116         f = fopen("/proc/swaps", "re");
117         if (!f) {
118                 log_full(errno == ENOENT ? LOG_DEBUG : LOG_WARNING,
119                          "Failed to retrieve open /proc/swaps: %m");
120                 assert(errno > 0);
121                 return -errno;
122         }
123
124         (void) fscanf(f, "%*s %*s %*s %*s %*s\n");
125
126         for (i = 1;; i++) {
127                 _cleanup_free_ char *dev = NULL, *type = NULL;
128                 size_t size_field, used_field;
129                 int k;
130
131                 k = fscanf(f,
132                            "%ms "   /* device/file */
133                            "%ms "   /* type of swap */
134                            "%zu "   /* swap size */
135                            "%zu "   /* used */
136                            "%*i\n", /* priority */
137                            &dev, &type, &size_field, &used_field);
138                 if (k != 4) {
139                         if (k == EOF)
140                                 break;
141
142                         log_warning("Failed to parse /proc/swaps:%u", i);
143                         continue;
144                 }
145
146                 if (streq(type, "partition") && endswith(dev, "\\040(deleted)")) {
147                         log_warning("Ignoring deleted swapfile '%s'.", dev);
148                         continue;
149                 }
150
151                 *size = size_field;
152                 *used = used_field;
153                 return 0;
154         }
155
156         log_debug("No swap partitions were found.");
157         return -ENOSYS;
158 }
159
160 static bool enough_memory_for_hibernation(void) {
161         _cleanup_free_ char *active = NULL;
162         unsigned long long act = 0;
163         size_t size = 0, used = 0;
164         int r;
165
166         r = hibernation_partition_size(&size, &used);
167         if (r < 0)
168                 return false;
169
170         r = get_proc_field("/proc/meminfo", "Active(anon)", WHITESPACE, &active);
171         if (r < 0) {
172                 log_error_errno(r, "Failed to retrieve Active(anon) from /proc/meminfo: %m");
173                 return false;
174         }
175
176         r = safe_atollu(active, &act);
177         if (r < 0) {
178                 log_error_errno(r, "Failed to parse Active(anon) from /proc/meminfo: %s: %m",
179                                 active);
180                 return false;
181         }
182
183         r = act <= (size - used) * HIBERNATION_SWAP_THRESHOLD;
184         log_debug("Hibernation is %spossible, Active(anon)=%llu kB, size=%zu kB, used=%zu kB, threshold=%.2g%%",
185                   r ? "" : "im", act, size, used, 100*HIBERNATION_SWAP_THRESHOLD);
186
187         return r;
188 }
189
190 int can_sleep(Manager *m, const char *verb) {
191
192         assert(streq(verb, "suspend") ||
193                streq(verb, "hibernate") ||
194                streq(verb, "hybrid-sleep"));
195
196         if ( streq(verb, "suspend")
197           && ( !can_sleep_state(m->suspend_state)
198             || !can_sleep_disk(m->suspend_mode) ) )
199                 return false;
200
201         if ( streq(verb, "hibernate")
202           && ( !can_sleep_state(m->hibernate_state)
203             || !can_sleep_disk(m->hibernate_mode) ) )
204                 return false;
205
206         if ( streq(verb, "hybrid-sleep")
207           && ( !can_sleep_state(m->hybrid_sleep_state)
208             || !can_sleep_disk(m->hybrid_sleep_mode) ) )
209                 return false;
210
211
212         return streq(verb, "suspend") || enough_memory_for_hibernation();
213 }
214
215 static int write_mode(char **modes) {
216         int r = 0;
217         char **mode;
218
219         STRV_FOREACH(mode, modes) {
220                 int k;
221
222                 k = write_string_file("/sys/power/disk", *mode, 0);
223                 if (k == 0)
224                         return 0;
225
226                 log_debug_errno(k, "Failed to write '%s' to /sys/power/disk: %m",
227                                 *mode);
228                 if (r == 0)
229                         r = k;
230         }
231
232         if (r < 0)
233                 log_error_errno(r, "Failed to write mode to /sys/power/disk: %m");
234
235         return r;
236 }
237
238 static int write_state(FILE **f, char **states) {
239         char **state;
240         int r = 0;
241
242         STRV_FOREACH(state, states) {
243                 int k;
244
245                 k = write_string_stream(*f, *state, true);
246                 if (k == 0)
247                         return 0;
248                 log_debug_errno(k, "Failed to write '%s' to /sys/power/state: %m",
249                                 *state);
250                 if (r == 0)
251                         r = k;
252
253                 fclose(*f);
254                 *f = fopen("/sys/power/state", "we");
255                 if (!*f)
256                         return log_error_errno(errno, "Failed to open /sys/power/state: %m");
257         }
258
259         return r;
260 }
261
262 int do_sleep(const char *arg_verb, char **modes, char **states) {
263
264         char *arguments[] = {
265                 NULL,
266                 (char*) "pre",
267                 (char*) arg_verb,
268                 NULL
269         };
270         static const char* const dirs[] = {SYSTEM_SLEEP_PATH, NULL};
271
272         int r;
273         _cleanup_fclose_ FILE *f = NULL;
274
275         /* This file is opened first, so that if we hit an error,
276          * we can abort before modifying any state. */
277         f = fopen("/sys/power/state", "we");
278         if (!f)
279                 return log_error_errno(errno, "Failed to open /sys/power/state: %m");
280
281         /* Configure the hibernation mode */
282         r = write_mode(modes);
283         if (r < 0)
284                 return r;
285
286         execute_directories(dirs, DEFAULT_TIMEOUT_USEC, arguments);
287
288         log_struct(LOG_INFO,
289                    LOG_MESSAGE_ID(SD_MESSAGE_SLEEP_START),
290                    LOG_MESSAGE("Suspending system..."),
291                    "SLEEP=%s", arg_verb,
292                    NULL);
293
294         r = write_state(&f, states);
295         if (r < 0)
296                 return r;
297
298         log_struct(LOG_INFO,
299                    LOG_MESSAGE_ID(SD_MESSAGE_SLEEP_STOP),
300                    LOG_MESSAGE("System resumed."),
301                    "SLEEP=%s", arg_verb,
302                    NULL);
303
304         arguments[1] = (char*) "post";
305         execute_directories(dirs, DEFAULT_TIMEOUT_USEC, arguments);
306
307         return r;
308 }
309