1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright © 2010-2017 Canonical
4 Copyright © 2018 Dell Inc.
9 #include <linux/fiemap.h>
12 #include "sd-messages.h"
14 #include "parse-util.h"
16 #include "exec-util.h"
20 #include "sleep-config.h"
21 #include "stdio-util.h"
22 #include "string-util.h"
26 /// Additional includes needed by elogind
29 static char* arg_verb = NULL;
31 static int write_hibernate_location_info(void) {
32 _cleanup_free_ char *device = NULL, *type = NULL;
33 _cleanup_free_ struct fiemap *fiemap = NULL;
34 char offset_str[DECIMAL_STR_MAX(uint64_t)];
35 char device_str[DECIMAL_STR_MAX(uint64_t)];
36 _cleanup_close_ int fd = -1;
41 r = find_hibernate_location(&device, &type, NULL, NULL);
43 return log_debug_errno(r, "Unable to find hibernation location: %m");
45 /* if it's a swap partition, we just write the disk to /sys/power/resume */
46 if (streq(type, "partition"))
47 return write_string_file("/sys/power/resume", device, 0);
48 else if (!streq(type, "file"))
49 return log_debug_errno(EINVAL, "Invalid hibernate type %s: %m",
52 /* Only available in 4.17+ */
53 if (access("/sys/power/resume_offset", F_OK) < 0) {
56 return log_debug_errno(errno, "/sys/power/resume_offset unavailable: %m");
59 r = access("/sys/power/resume_offset", W_OK);
61 return log_debug_errno(errno, "/sys/power/resume_offset not writeable: %m");
63 fd = open(device, O_RDONLY | O_CLOEXEC | O_NONBLOCK);
65 return log_debug_errno(errno, "Unable to open '%s': %m", device);
68 return log_debug_errno(errno, "Unable to stat %s: %m", device);
69 r = read_fiemap(fd, &fiemap);
71 return log_debug_errno(r, "Unable to read extent map for '%s': %m",
73 if (fiemap->fm_mapped_extents == 0) {
74 log_debug("No extents found in '%s'", device);
77 offset = fiemap->fm_extents[0].fe_physical / page_size();
78 xsprintf(offset_str, "%" PRIu64, offset);
79 r = write_string_file("/sys/power/resume_offset", offset_str, 0);
81 return log_debug_errno(r, "Failed to write offset '%s': %m",
84 xsprintf(device_str, "%lx", (unsigned long)stb.st_dev);
85 r = write_string_file("/sys/power/resume", device_str, 0);
87 return log_debug_errno(r, "Failed to write device '%s': %m",
92 static int write_mode(char **modes) {
96 STRV_FOREACH(mode, modes) {
99 k = write_string_file("/sys/power/disk", *mode, 0);
103 log_debug_errno(k, "Failed to write '%s' to /sys/power/disk: %m",
112 static int write_state(FILE **f, char **states) {
116 STRV_FOREACH(state, states) {
119 k = write_string_stream(*f, *state, 0);
122 log_debug_errno(k, "Failed to write '%s' to /sys/power/state: %m",
128 *f = fopen("/sys/power/state", "we");
136 #if 0 /// elogind uses the values stored in its manager instance
137 static int execute(char **modes, char **states) {
139 static int execute(Manager *m, const char *verb) {
143 arg_verb = (char*)verb;
145 char **modes = streq(arg_verb, "suspend") ? m->suspend_mode :
146 streq(arg_verb, "hibernate") ? m->hibernate_mode :
147 m->hybrid_sleep_mode;
148 char **states = streq(arg_verb, "suspend") ? m->suspend_state :
149 streq(arg_verb, "hibernate") ? m->hibernate_state :
150 m->hybrid_sleep_state;
153 char *arguments[] = {
159 static const char* const dirs[] = {
165 _cleanup_fclose_ FILE *f = NULL;
167 /* This file is opened first, so that if we hit an error,
168 * we can abort before modifying any state. */
169 f = fopen("/sys/power/state", "we");
171 return log_error_errno(errno, "Failed to open /sys/power/state: %m");
173 /* Configure the hibernation mode */
174 if (!strv_isempty(modes)) {
175 r = write_hibernate_location_info();
177 return log_error_errno(r, "Failed to write hibernation disk offset: %m");
178 r = write_mode(modes);
180 return log_error_errno(r, "Failed to write mode to /sys/power/disk: %m");;
183 execute_directories(dirs, DEFAULT_TIMEOUT_USEC, NULL, NULL, arguments);
186 "MESSAGE_ID=" SD_MESSAGE_SLEEP_START_STR,
187 LOG_MESSAGE("Suspending system..."),
188 "SLEEP=%s", arg_verb);
190 r = write_state(&f, states);
192 return log_error_errno(r, "Failed to write /sys/power/state: %m");
195 "MESSAGE_ID=" SD_MESSAGE_SLEEP_STOP_STR,
196 LOG_MESSAGE("System resumed."),
197 "SLEEP=%s", arg_verb);
199 arguments[1] = (char*) "post";
200 execute_directories(dirs, DEFAULT_TIMEOUT_USEC, NULL, NULL, arguments);
205 static int read_wakealarm(uint64_t *result) {
206 _cleanup_free_ char *t = NULL;
208 if (read_one_line_file("/sys/class/rtc/rtc0/since_epoch", &t) >= 0)
209 return safe_atou64(t, result);
213 static int write_wakealarm(const char *str) {
215 _cleanup_fclose_ FILE *f = NULL;
218 f = fopen("/sys/class/rtc/rtc0/wakealarm", "we");
220 return log_error_errno(errno, "Failed to open /sys/class/rtc/rtc0/wakealarm: %m");
222 r = write_string_stream(f, str, 0);
224 return log_error_errno(r, "Failed to write '%s' to /sys/class/rtc/rtc0/wakealarm: %m", str);
229 #if 0 /// elogind uses the values stored in its manager instance
230 static int execute_s2h(usec_t hibernate_delay_sec) {
232 _cleanup_strv_free_ char **hibernate_modes = NULL, **hibernate_states = NULL,
233 **suspend_modes = NULL, **suspend_states = NULL;
235 static int execute_s2h(Manager *m) {
238 usec_t hibernate_delay_sec = m->hibernate_delay_sec;
240 usec_t orig_time, cmp_time;
241 char time_str[DECIMAL_STR_MAX(uint64_t)];
244 #if 0 /// Already parsed by elogind config
245 r = parse_sleep_config("suspend", &suspend_modes, &suspend_states,
250 r = parse_sleep_config("hibernate", &hibernate_modes,
251 &hibernate_states, NULL);
256 r = read_wakealarm(&orig_time);
258 return log_error_errno(errno, "Failed to read time: %d", r);
260 orig_time += hibernate_delay_sec / USEC_PER_SEC;
261 xsprintf(time_str, "%" PRIu64, orig_time);
263 r = write_wakealarm(time_str);
267 log_debug("Set RTC wake alarm for %s", time_str);
269 #if 0 /// elogind uses its manager instance values
270 r = execute(suspend_modes, suspend_states);
272 r = execute(m, "suspend");
277 r = read_wakealarm(&cmp_time);
279 return log_error_errno(errno, "Failed to read time: %d", r);
282 r = write_wakealarm("0");
286 log_debug("Woke up at %"PRIu64, cmp_time);
288 /* if woken up after alarm time, hibernate */
289 if (cmp_time >= orig_time)
290 #if 0 /// elogind uses its manager instance values
291 r = execute(hibernate_modes, hibernate_states);
293 r = execute(m, "hibernate");
299 #if 0 /// elogind calls execute() by itself and does not need another binary
300 static void help(void) {
301 printf("%s COMMAND\n\n"
302 "Suspend the system, hibernate the system, or both.\n\n"
304 " -h --help Show this help and exit\n"
305 " --version Print version string and exit\n"
306 " suspend Suspend the system\n"
307 " hibernate Hibernate the system\n"
308 " hybrid-sleep Both hibernate and suspend the system\n"
309 " suspend-then-hibernate Initially suspend and then hibernate\n"
310 " the system after a fixed period of time\n"
311 , program_invocation_short_name);
314 static int parse_argv(int argc, char *argv[]) {
319 static const struct option options[] = {
320 { "help", no_argument, NULL, 'h' },
321 { "version", no_argument, NULL, ARG_VERSION },
330 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
343 assert_not_reached("Unhandled option");
346 if (argc - optind != 1) {
347 log_error("Usage: %s COMMAND",
348 program_invocation_short_name);
352 arg_verb = argv[optind];
354 if (!streq(arg_verb, "suspend") &&
355 !streq(arg_verb, "hibernate") &&
356 !streq(arg_verb, "hybrid-sleep") &&
357 !streq(arg_verb, "suspend-then-hibernate")) {
358 log_error("Unknown command '%s'.", arg_verb);
362 return 1 /* work to do */;
365 int main(int argc, char *argv[]) {
366 _cleanup_strv_free_ char **modes = NULL, **states = NULL;
370 log_set_target(LOG_TARGET_AUTO);
371 log_parse_environment();
374 r = parse_argv(argc, argv);
378 r = parse_sleep_config(arg_verb, &modes, &states, &delay);
382 if (streq(arg_verb, "suspend-then-hibernate"))
383 r = execute_s2h(delay);
385 r = execute(modes, states);
387 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
390 int do_sleep(Manager *m, const char *verb) {
394 arg_verb = (char*)verb;
396 if (streq(arg_verb, "suspend-then-hibernate"))
397 return execute_s2h(m);
399 return execute(m, NULL);