1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2012 Lennart Poettering
4 Copyright 2013 Zbigniew Jędrzejewski-Szmek
5 Copyright 2010-2017 Canonical
6 Copyright 2018 Dell Inc.
11 //#include <linux/fiemap.h>
14 #include "sd-messages.h"
16 //#include "parse-util.h"
18 #include "exec-util.h"
22 //#include "sleep-config.h"
23 //#include "stdio-util.h"
24 //#include "string-util.h"
28 /// Additional includes needed by elogind
31 static char* arg_verb = NULL;
33 static int write_hibernate_location_info(void) {
34 _cleanup_free_ char *device = NULL, *type = NULL;
35 _cleanup_free_ struct fiemap *fiemap = NULL;
36 char offset_str[DECIMAL_STR_MAX(uint64_t)];
37 char device_str[DECIMAL_STR_MAX(uint64_t)];
38 _cleanup_close_ int fd = -1;
43 r = find_hibernate_location(&device, &type, NULL, NULL);
45 return log_debug_errno(r, "Unable to find hibernation location: %m");
47 /* if it's a swap partition, we just write the disk to /sys/power/resume */
48 if (streq(type, "partition"))
49 return write_string_file("/sys/power/resume", device, 0);
50 else if (!streq(type, "file"))
51 return log_debug_errno(EINVAL, "Invalid hibernate type %s: %m",
54 /* Only available in 4.17+ */
55 if (access("/sys/power/resume_offset", F_OK) < 0) {
58 return log_debug_errno(errno, "/sys/power/resume_offset unavailable: %m");
61 r = access("/sys/power/resume_offset", W_OK);
63 return log_debug_errno(errno, "/sys/power/resume_offset not writeable: %m");
65 fd = open(device, O_RDONLY | O_CLOEXEC | O_NONBLOCK);
67 return log_debug_errno(errno, "Unable to open '%s': %m", device);
70 return log_debug_errno(errno, "Unable to stat %s: %m", device);
71 r = read_fiemap(fd, &fiemap);
73 return log_debug_errno(r, "Unable to read extent map for '%s': %m",
75 if (fiemap->fm_mapped_extents == 0) {
76 log_debug("No extents found in '%s'", device);
79 offset = fiemap->fm_extents[0].fe_physical / page_size();
80 xsprintf(offset_str, "%" PRIu64, offset);
81 r = write_string_file("/sys/power/resume_offset", offset_str, 0);
83 return log_debug_errno(r, "Failed to write offset '%s': %m",
86 xsprintf(device_str, "%lx", (unsigned long)stb.st_dev);
87 r = write_string_file("/sys/power/resume", device_str, 0);
89 return log_debug_errno(r, "Failed to write device '%s': %m",
94 static int write_mode(char **modes) {
98 STRV_FOREACH(mode, modes) {
101 k = write_string_file("/sys/power/disk", *mode, 0);
105 log_debug_errno(k, "Failed to write '%s' to /sys/power/disk: %m",
114 static int write_state(FILE **f, char **states) {
118 STRV_FOREACH(state, states) {
121 k = write_string_stream(*f, *state, 0);
124 log_debug_errno(k, "Failed to write '%s' to /sys/power/state: %m",
130 *f = fopen("/sys/power/state", "we");
138 static int execute(char **modes, char **states) {
140 char *arguments[] = {
146 static const char* const dirs[] = {
152 _cleanup_fclose_ FILE *f = NULL;
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");
158 return log_error_errno(errno, "Failed to open /sys/power/state: %m");
160 /* Configure the hibernation mode */
161 if (!strv_isempty(modes)) {
162 r = write_hibernate_location_info();
164 return log_error_errno(r, "Failed to write hibernation disk offset: %m");
165 r = write_mode(modes);
167 return log_error_errno(r, "Failed to write mode to /sys/power/disk: %m");;
170 execute_directories(dirs, DEFAULT_TIMEOUT_USEC, NULL, NULL, arguments);
173 "MESSAGE_ID=" SD_MESSAGE_SLEEP_START_STR,
174 LOG_MESSAGE("Suspending system..."),
175 "SLEEP=%s", arg_verb);
177 r = write_state(&f, states);
179 return log_error_errno(r, "Failed to write /sys/power/state: %m");
182 "MESSAGE_ID=" SD_MESSAGE_SLEEP_STOP_STR,
183 LOG_MESSAGE("System resumed."),
184 "SLEEP=%s", arg_verb);
186 arguments[1] = (char*) "post";
187 execute_directories(dirs, DEFAULT_TIMEOUT_USEC, NULL, NULL, arguments);
192 static int read_wakealarm(uint64_t *result) {
193 _cleanup_free_ char *t = NULL;
195 if (read_one_line_file("/sys/class/rtc/rtc0/since_epoch", &t) >= 0)
196 return safe_atou64(t, result);
200 static int write_wakealarm(const char *str) {
202 _cleanup_fclose_ FILE *f = NULL;
205 f = fopen("/sys/class/rtc/rtc0/wakealarm", "we");
207 return log_error_errno(errno, "Failed to open /sys/class/rtc/rtc0/wakealarm: %m");
209 r = write_string_stream(f, str, 0);
211 return log_error_errno(r, "Failed to write '%s' to /sys/class/rtc/rtc0/wakealarm: %m", str);
216 static int execute_s2h(usec_t hibernate_delay_sec) {
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)];
224 r = parse_sleep_config("suspend", &suspend_modes, &suspend_states,
229 r = parse_sleep_config("hibernate", &hibernate_modes,
230 &hibernate_states, NULL);
234 r = read_wakealarm(&orig_time);
236 return log_error_errno(errno, "Failed to read time: %d", r);
238 orig_time += hibernate_delay_sec / USEC_PER_SEC;
239 xsprintf(time_str, "%" PRIu64, orig_time);
241 r = write_wakealarm(time_str);
245 log_debug("Set RTC wake alarm for %s", time_str);
247 r = execute(suspend_modes, suspend_states);
251 r = read_wakealarm(&cmp_time);
253 return log_error_errno(errno, "Failed to read time: %d", r);
256 r = write_wakealarm("0");
260 log_debug("Woke up at %"PRIu64, cmp_time);
262 /* if woken up after alarm time, hibernate */
263 if (cmp_time >= orig_time)
264 r = execute(hibernate_modes, hibernate_states);
269 #if 0 /// elogind calls execute() by itself and does not need another binary
270 static void help(void) {
271 printf("%s COMMAND\n\n"
272 "Suspend the system, hibernate the system, or both.\n\n"
274 " -h --help Show this help and exit\n"
275 " --version Print version string and exit\n"
276 " suspend Suspend the system\n"
277 " hibernate Hibernate the system\n"
278 " hybrid-sleep Both hibernate and suspend the system\n"
279 " suspend-then-hibernate Initially suspend and then hibernate\n"
280 " the system after a fixed period of time\n"
281 , program_invocation_short_name);
284 static int parse_argv(int argc, char *argv[]) {
289 static const struct option options[] = {
290 { "help", no_argument, NULL, 'h' },
291 { "version", no_argument, NULL, ARG_VERSION },
300 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
313 assert_not_reached("Unhandled option");
316 if (argc - optind != 1) {
317 log_error("Usage: %s COMMAND",
318 program_invocation_short_name);
322 arg_verb = argv[optind];
324 if (!streq(arg_verb, "suspend") &&
325 !streq(arg_verb, "hibernate") &&
326 !streq(arg_verb, "hybrid-sleep") &&
327 !streq(arg_verb, "suspend-then-hibernate")) {
328 log_error("Unknown command '%s'.", arg_verb);
332 return 1 /* work to do */;
335 int main(int argc, char *argv[]) {
336 _cleanup_strv_free_ char **modes = NULL, **states = NULL;
340 log_set_target(LOG_TARGET_AUTO);
341 log_parse_environment();
344 r = parse_argv(argc, argv);
348 r = parse_sleep_config(arg_verb, &modes, &states, &delay);
352 if (streq(arg_verb, "suspend-then-hibernate"))
353 r = execute_s2h(delay);
355 r = execute(modes, states);
357 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
360 int do_sleep(const char *verb, char **modes, char **states) {
362 arg_verb = (char*)verb;
363 return execute(modes, states);