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