]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/sleep/sleep.c
Merge pull request #10158 from keszybz/seccomp-log-tightening
[thirdparty/systemd.git] / src / sleep / sleep.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright © 2010-2017 Canonical
4 Copyright © 2018 Dell Inc.
5 ***/
6
7 #include <errno.h>
8 #include <getopt.h>
9 #include <linux/fiemap.h>
10 #include <stdio.h>
11
12 #include "sd-messages.h"
13
14 #include "def.h"
15 #include "exec-util.h"
16 #include "fd-util.h"
17 #include "fileio.h"
18 #include "log.h"
19 #include "parse-util.h"
20 #include "sleep-config.h"
21 #include "stdio-util.h"
22 #include "string-util.h"
23 #include "strv.h"
24 #include "terminal-util.h"
25 #include "util.h"
26
27 static char* arg_verb = NULL;
28
29 static int write_hibernate_location_info(void) {
30 _cleanup_free_ char *device = NULL, *type = NULL;
31 _cleanup_free_ struct fiemap *fiemap = NULL;
32 char offset_str[DECIMAL_STR_MAX(uint64_t)];
33 char device_str[DECIMAL_STR_MAX(uint64_t)];
34 _cleanup_close_ int fd = -1;
35 struct stat stb;
36 uint64_t offset;
37 int r;
38
39 r = find_hibernate_location(&device, &type, NULL, NULL);
40 if (r < 0)
41 return log_debug_errno(r, "Unable to find hibernation location: %m");
42
43 /* if it's a swap partition, we just write the disk to /sys/power/resume */
44 if (streq(type, "partition")) {
45 r = write_string_file("/sys/power/resume", device, 0);
46 if (r < 0)
47 return log_debug_errno(r, "Faileed to write partitoin device to /sys/power/resume: %m");
48
49 return r;
50 }
51 if (!streq(type, "file")) {
52 log_debug("Invalid hibernate type: %s", type);
53 return -EINVAL;
54 }
55
56 /* Only available in 4.17+ */
57 if (access("/sys/power/resume_offset", F_OK) < 0) {
58 if (errno == ENOENT)
59 return 0;
60
61 return log_debug_errno(errno, "/sys/power/resume_offset unavailable: %m");
62 }
63
64 if (access("/sys/power/resume_offset", W_OK) < 0)
65 return log_debug_errno(errno, "/sys/power/resume_offset not writeable: %m");
66
67 fd = open(device, O_RDONLY | O_CLOEXEC | O_NONBLOCK);
68 if (fd < 0)
69 return log_debug_errno(errno, "Unable to open '%s': %m", device);
70 r = fstat(fd, &stb);
71 if (r < 0)
72 return log_debug_errno(errno, "Unable to stat %s: %m", device);
73
74 r = read_fiemap(fd, &fiemap);
75 if (r < 0)
76 return log_debug_errno(r, "Unable to read extent map for '%s': %m", device);
77 if (fiemap->fm_mapped_extents == 0) {
78 log_debug("No extents found in '%s'", device);
79 return -EINVAL;
80 }
81
82 offset = fiemap->fm_extents[0].fe_physical / page_size();
83 xsprintf(offset_str, "%" PRIu64, offset);
84 r = write_string_file("/sys/power/resume_offset", offset_str, 0);
85 if (r < 0)
86 return log_debug_errno(r, "Failed to write offset '%s': %m", offset_str);
87
88 xsprintf(device_str, "%lx", (unsigned long)stb.st_dev);
89 r = write_string_file("/sys/power/resume", device_str, 0);
90 if (r < 0)
91 return log_debug_errno(r, "Failed to write device '%s': %m", device_str);
92
93 return 0;
94 }
95
96 static int write_mode(char **modes) {
97 int r = 0;
98 char **mode;
99
100 STRV_FOREACH(mode, modes) {
101 int k;
102
103 k = write_string_file("/sys/power/disk", *mode, 0);
104 if (k >= 0)
105 return 0;
106
107 log_debug_errno(k, "Failed to write '%s' to /sys/power/disk: %m", *mode);
108 if (r >= 0)
109 r = k;
110 }
111
112 return r;
113 }
114
115 static int write_state(FILE **f, char **states) {
116 char **state;
117 int r = 0;
118
119 STRV_FOREACH(state, states) {
120 int k;
121
122 k = write_string_stream(*f, *state, 0);
123 if (k >= 0)
124 return 0;
125 log_debug_errno(k, "Failed to write '%s' to /sys/power/state: %m", *state);
126 if (r >= 0)
127 r = k;
128
129 fclose(*f);
130 *f = fopen("/sys/power/state", "we");
131 if (!*f)
132 return -errno;
133 }
134
135 return r;
136 }
137
138 static int execute(char **modes, char **states) {
139
140 char *arguments[] = {
141 NULL,
142 (char*) "pre",
143 arg_verb,
144 NULL
145 };
146 static const char* const dirs[] = {
147 SYSTEM_SLEEP_PATH,
148 NULL
149 };
150
151 int r;
152 _cleanup_fclose_ FILE *f = NULL;
153
154 /* This file is opened first, so that if we hit an error,
155 * we can abort before modifying any state. */
156 f = fopen("/sys/power/state", "we");
157 if (!f)
158 return log_error_errno(errno, "Failed to open /sys/power/state: %m");
159
160 /* Configure the hibernation mode */
161 if (!strv_isempty(modes)) {
162 r = write_hibernate_location_info();
163 if (r < 0)
164 return log_error_errno(r, "Failed to write hibernation disk offset: %m");
165 r = write_mode(modes);
166 if (r < 0)
167 return log_error_errno(r, "Failed to write mode to /sys/power/disk: %m");;
168 }
169
170 execute_directories(dirs, DEFAULT_TIMEOUT_USEC, NULL, NULL, arguments, NULL);
171
172 log_struct(LOG_INFO,
173 "MESSAGE_ID=" SD_MESSAGE_SLEEP_START_STR,
174 LOG_MESSAGE("Suspending system..."),
175 "SLEEP=%s", arg_verb);
176
177 r = write_state(&f, states);
178 if (r < 0)
179 return log_error_errno(r, "Failed to write /sys/power/state: %m");
180
181 log_struct(LOG_INFO,
182 "MESSAGE_ID=" SD_MESSAGE_SLEEP_STOP_STR,
183 LOG_MESSAGE("System resumed."),
184 "SLEEP=%s", arg_verb);
185
186 arguments[1] = (char*) "post";
187 execute_directories(dirs, DEFAULT_TIMEOUT_USEC, NULL, NULL, arguments, NULL);
188
189 return r;
190 }
191
192 static int read_wakealarm(uint64_t *result) {
193 _cleanup_free_ char *t = NULL;
194
195 if (read_one_line_file("/sys/class/rtc/rtc0/since_epoch", &t) >= 0)
196 return safe_atou64(t, result);
197 return -EBADF;
198 }
199
200 static int write_wakealarm(const char *str) {
201
202 _cleanup_fclose_ FILE *f = NULL;
203 int r;
204
205 f = fopen("/sys/class/rtc/rtc0/wakealarm", "we");
206 if (!f)
207 return log_error_errno(errno, "Failed to open /sys/class/rtc/rtc0/wakealarm: %m");
208
209 r = write_string_stream(f, str, 0);
210 if (r < 0)
211 return log_error_errno(r, "Failed to write '%s' to /sys/class/rtc/rtc0/wakealarm: %m", str);
212
213 return 0;
214 }
215
216 static int execute_s2h(usec_t hibernate_delay_sec) {
217
218 _cleanup_strv_free_ char **hibernate_modes = NULL, **hibernate_states = NULL,
219 **suspend_modes = NULL, **suspend_states = NULL;
220 usec_t orig_time, cmp_time;
221 char time_str[DECIMAL_STR_MAX(uint64_t)];
222 int r;
223
224 r = parse_sleep_config("suspend", &suspend_modes, &suspend_states,
225 NULL);
226 if (r < 0)
227 return r;
228
229 r = parse_sleep_config("hibernate", &hibernate_modes,
230 &hibernate_states, NULL);
231 if (r < 0)
232 return r;
233
234 r = read_wakealarm(&orig_time);
235 if (r < 0)
236 return log_error_errno(errno, "Failed to read time: %d", r);
237
238 orig_time += hibernate_delay_sec / USEC_PER_SEC;
239 xsprintf(time_str, "%" PRIu64, orig_time);
240
241 r = write_wakealarm(time_str);
242 if (r < 0)
243 return r;
244
245 log_debug("Set RTC wake alarm for %s", time_str);
246
247 r = execute(suspend_modes, suspend_states);
248 if (r < 0)
249 return r;
250
251 r = read_wakealarm(&cmp_time);
252 if (r < 0)
253 return log_error_errno(errno, "Failed to read time: %d", r);
254
255 /* reset RTC */
256 r = write_wakealarm("0");
257 if (r < 0)
258 return r;
259
260 log_debug("Woke up at %"PRIu64, cmp_time);
261
262 /* if woken up after alarm time, hibernate */
263 if (cmp_time >= orig_time)
264 r = execute(hibernate_modes, hibernate_states);
265
266 return r;
267 }
268
269 static int help(void) {
270 _cleanup_free_ char *link = NULL;
271 int r;
272
273 r = terminal_urlify_man("systemd-suspend.service", "8", &link);
274 if (r < 0)
275 return log_oom();
276
277 printf("%s COMMAND\n\n"
278 "Suspend the system, hibernate the system, or both.\n\n"
279 " -h --help Show this help and exit\n"
280 " --version Print version string and exit\n"
281 "\nCommands:\n"
282 " suspend Suspend the system\n"
283 " hibernate Hibernate the system\n"
284 " hybrid-sleep Both hibernate and suspend the system\n"
285 " suspend-then-hibernate Initially suspend and then hibernate\n"
286 " the system after a fixed period of time\n"
287 "\nSee the %s for details.\n"
288 , program_invocation_short_name
289 , link
290 );
291
292 return 0;
293 }
294
295 static int parse_argv(int argc, char *argv[]) {
296 enum {
297 ARG_VERSION = 0x100,
298 };
299
300 static const struct option options[] = {
301 { "help", no_argument, NULL, 'h' },
302 { "version", no_argument, NULL, ARG_VERSION },
303 {}
304 };
305
306 int c;
307
308 assert(argc >= 0);
309 assert(argv);
310
311 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
312 switch(c) {
313 case 'h':
314 return help();
315
316 case ARG_VERSION:
317 return version();
318
319 case '?':
320 return -EINVAL;
321
322 default:
323 assert_not_reached("Unhandled option");
324 }
325
326 if (argc - optind != 1) {
327 log_error("Usage: %s COMMAND",
328 program_invocation_short_name);
329 return -EINVAL;
330 }
331
332 arg_verb = argv[optind];
333
334 if (!STR_IN_SET(arg_verb, "suspend", "hibernate", "hybrid-sleep", "suspend-then-hibernate")) {
335 log_error("Unknown command '%s'.", arg_verb);
336 return -EINVAL;
337 }
338
339 return 1 /* work to do */;
340 }
341
342 int main(int argc, char *argv[]) {
343 _cleanup_strv_free_ char **modes = NULL, **states = NULL;
344 usec_t delay = 0;
345 int r;
346
347 log_set_target(LOG_TARGET_AUTO);
348 log_parse_environment();
349 log_open();
350
351 r = parse_argv(argc, argv);
352 if (r <= 0)
353 goto finish;
354
355 r = parse_sleep_config(arg_verb, &modes, &states, &delay);
356 if (r < 0)
357 goto finish;
358
359 if (streq(arg_verb, "suspend-then-hibernate"))
360 r = execute_s2h(delay);
361 else
362 r = execute(modes, states);
363
364 finish:
365 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
366 }