1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2012 Lennart Poettering
6 Copyright 2013 Zbigniew Jędrzejewski-Szmek
7 Copyright 2010-2017 Canonical
8 Copyright 2018 Dell Inc.
13 //#include <linux/fiemap.h>
16 #include "sd-messages.h"
18 //#include "parse-util.h"
20 #include "exec-util.h"
24 //#include "sleep-config.h"
25 //#include "stdio-util.h"
26 //#include "string-util.h"
30 /// Additional includes needed by elogind
33 static char* arg_verb = NULL;
35 static int write_hibernate_location_info(void) {
36 _cleanup_free_ char *device = NULL, *type = NULL;
37 _cleanup_free_ struct fiemap *fiemap = NULL;
38 char offset_str[DECIMAL_STR_MAX(uint64_t)];
39 char device_str[DECIMAL_STR_MAX(uint64_t)];
40 _cleanup_close_ int fd = -1;
45 r = find_hibernate_location(&device, &type, NULL, NULL);
47 return log_debug_errno(r, "Unable to find hibernation location: %m");
49 /* if it's a swap partition, we just write the disk to /sys/power/resume */
50 if (streq(type, "partition"))
51 return write_string_file("/sys/power/resume", device, 0);
52 else if (!streq(type, "file"))
53 return log_debug_errno(EINVAL, "Invalid hibernate type %s: %m",
56 /* Only available in 4.17+ */
57 if (access("/sys/power/resume_offset", F_OK) < 0) {
60 return log_debug_errno(errno, "/sys/power/resume_offset unavailable: %m");
63 r = access("/sys/power/resume_offset", W_OK);
65 return log_debug_errno(errno, "/sys/power/resume_offset not writeable: %m");
67 fd = open(device, O_RDONLY | O_CLOEXEC | O_NONBLOCK);
69 return log_debug_errno(errno, "Unable to open '%s': %m", device);
72 return log_debug_errno(errno, "Unable to stat %s: %m", device);
73 r = read_fiemap(fd, &fiemap);
75 return log_debug_errno(r, "Unable to read extent map for '%s': %m",
77 if (fiemap->fm_mapped_extents == 0) {
78 log_debug("No extents found in '%s'", device);
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);
85 return log_debug_errno(r, "Failed to write offset '%s': %m",
88 xsprintf(device_str, "%lx", (unsigned long)stb.st_dev);
89 r = write_string_file("/sys/power/resume", device_str, 0);
91 return log_debug_errno(r, "Failed to write device '%s': %m",
96 static int write_mode(char **modes) {
100 STRV_FOREACH(mode, modes) {
103 k = write_string_file("/sys/power/disk", *mode, 0);
107 log_debug_errno(k, "Failed to write '%s' to /sys/power/disk: %m",
116 static int write_state(FILE **f, char **states) {
120 STRV_FOREACH(state, states) {
123 k = write_string_stream(*f, *state, 0);
126 log_debug_errno(k, "Failed to write '%s' to /sys/power/state: %m",
132 *f = fopen("/sys/power/state", "we");
140 static int execute(char **modes, char **states) {
142 char *arguments[] = {
148 static const char* const dirs[] = {
154 _cleanup_fclose_ FILE *f = NULL;
156 /* This file is opened first, so that if we hit an error,
157 * we can abort before modifying any state. */
158 f = fopen("/sys/power/state", "we");
160 return log_error_errno(errno, "Failed to open /sys/power/state: %m");
162 /* Configure the hibernation mode */
163 if (!strv_isempty(modes)) {
164 r = write_hibernate_location_info();
166 return log_error_errno(r, "Failed to write hibernation disk offset: %m");
167 r = write_mode(modes);
169 return log_error_errno(r, "Failed to write mode to /sys/power/disk: %m");;
172 execute_directories(dirs, DEFAULT_TIMEOUT_USEC, NULL, NULL, arguments);
175 "MESSAGE_ID=" SD_MESSAGE_SLEEP_START_STR,
176 LOG_MESSAGE("Suspending system..."),
177 "SLEEP=%s", arg_verb);
179 r = write_state(&f, states);
181 return log_error_errno(r, "Failed to write /sys/power/state: %m");
184 "MESSAGE_ID=" SD_MESSAGE_SLEEP_STOP_STR,
185 LOG_MESSAGE("System resumed."),
186 "SLEEP=%s", arg_verb);
188 arguments[1] = (char*) "post";
189 execute_directories(dirs, DEFAULT_TIMEOUT_USEC, NULL, NULL, arguments);
194 static int read_wakealarm(uint64_t *result) {
195 _cleanup_free_ char *t = NULL;
197 if (read_one_line_file("/sys/class/rtc/rtc0/since_epoch", &t) >= 0)
198 return safe_atou64(t, result);
202 static int write_wakealarm(const char *str) {
204 _cleanup_fclose_ FILE *f = NULL;
207 f = fopen("/sys/class/rtc/rtc0/wakealarm", "we");
209 return log_error_errno(errno, "Failed to open /sys/class/rtc/rtc0/wakealarm: %m");
211 r = write_string_stream(f, str, 0);
213 return log_error_errno(r, "Failed to write '%s' to /sys/class/rtc/rtc0/wakealarm: %m", str);
218 static int execute_s2h(usec_t hibernate_delay_sec) {
220 _cleanup_strv_free_ char **hibernate_modes = NULL, **hibernate_states = NULL,
221 **suspend_modes = NULL, **suspend_states = NULL;
222 usec_t orig_time, cmp_time;
223 char time_str[DECIMAL_STR_MAX(uint64_t)];
226 r = parse_sleep_config("suspend", &suspend_modes, &suspend_states,
231 r = parse_sleep_config("hibernate", &hibernate_modes,
232 &hibernate_states, NULL);
236 r = read_wakealarm(&orig_time);
238 return log_error_errno(errno, "Failed to read time: %d", r);
240 orig_time += hibernate_delay_sec / USEC_PER_SEC;
241 xsprintf(time_str, "%" PRIu64, orig_time);
243 r = write_wakealarm(time_str);
247 log_debug("Set RTC wake alarm for %s", time_str);
249 r = execute(suspend_modes, suspend_states);
253 r = read_wakealarm(&cmp_time);
255 return log_error_errno(errno, "Failed to read time: %d", r);
258 r = write_wakealarm("0");
262 log_debug("Woke up at %"PRIu64, cmp_time);
264 /* if woken up after alarm time, hibernate */
265 if (cmp_time >= orig_time)
266 r = execute(hibernate_modes, hibernate_states);
271 #if 0 /// elogind calls execute() by itself and does not need another binary
272 static void help(void) {
273 printf("%s COMMAND\n\n"
274 "Suspend the system, hibernate the system, or both.\n\n"
276 " -h --help Show this help and exit\n"
277 " --version Print version string and exit\n"
278 " suspend Suspend the system\n"
279 " hibernate Hibernate the system\n"
280 " hybrid-sleep Both hibernate and suspend the system\n"
281 " suspend-then-hibernate Initially suspend and then hibernate\n"
282 " the system after a fixed period of time\n"
283 , program_invocation_short_name);
286 static int parse_argv(int argc, char *argv[]) {
291 static const struct option options[] = {
292 { "help", no_argument, NULL, 'h' },
293 { "version", no_argument, NULL, ARG_VERSION },
302 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
315 assert_not_reached("Unhandled option");
318 if (argc - optind != 1) {
319 log_error("Usage: %s COMMAND",
320 program_invocation_short_name);
324 arg_verb = argv[optind];
326 if (!streq(arg_verb, "suspend") &&
327 !streq(arg_verb, "hibernate") &&
328 !streq(arg_verb, "hybrid-sleep") &&
329 !streq(arg_verb, "suspend-then-hibernate")) {
330 log_error("Unknown command '%s'.", arg_verb);
334 return 1 /* work to do */;
337 int main(int argc, char *argv[]) {
338 _cleanup_strv_free_ char **modes = NULL, **states = NULL;
342 log_set_target(LOG_TARGET_AUTO);
343 log_parse_environment();
346 r = parse_argv(argc, argv);
350 r = parse_sleep_config(arg_verb, &modes, &states, &delay);
354 if (streq(arg_verb, "suspend-then-hibernate"))
355 r = execute_s2h(delay);
357 r = execute(modes, states);
359 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
362 int do_sleep(const char *verb, char **modes, char **states) {
364 arg_verb = (char*)verb;
365 return execute(modes, states);