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 static int execute(char **modes, char **states) {
138 char *arguments[] = {
144 static const char* const dirs[] = {
150 _cleanup_fclose_ FILE *f = NULL;
152 /* This file is opened first, so that if we hit an error,
153 * we can abort before modifying any state. */
154 f = fopen("/sys/power/state", "we");
156 return log_error_errno(errno, "Failed to open /sys/power/state: %m");
158 /* Configure the hibernation mode */
159 if (!strv_isempty(modes)) {
160 r = write_hibernate_location_info();
162 return log_error_errno(r, "Failed to write hibernation disk offset: %m");
163 r = write_mode(modes);
165 return log_error_errno(r, "Failed to write mode to /sys/power/disk: %m");;
168 execute_directories(dirs, DEFAULT_TIMEOUT_USEC, NULL, NULL, arguments);
171 "MESSAGE_ID=" SD_MESSAGE_SLEEP_START_STR,
172 LOG_MESSAGE("Suspending system..."),
173 "SLEEP=%s", arg_verb);
175 r = write_state(&f, states);
177 return log_error_errno(r, "Failed to write /sys/power/state: %m");
180 "MESSAGE_ID=" SD_MESSAGE_SLEEP_STOP_STR,
181 LOG_MESSAGE("System resumed."),
182 "SLEEP=%s", arg_verb);
184 arguments[1] = (char*) "post";
185 execute_directories(dirs, DEFAULT_TIMEOUT_USEC, NULL, NULL, arguments);
190 static int read_wakealarm(uint64_t *result) {
191 _cleanup_free_ char *t = NULL;
193 if (read_one_line_file("/sys/class/rtc/rtc0/since_epoch", &t) >= 0)
194 return safe_atou64(t, result);
198 static int write_wakealarm(const char *str) {
200 _cleanup_fclose_ FILE *f = NULL;
203 f = fopen("/sys/class/rtc/rtc0/wakealarm", "we");
205 return log_error_errno(errno, "Failed to open /sys/class/rtc/rtc0/wakealarm: %m");
207 r = write_string_stream(f, str, 0);
209 return log_error_errno(r, "Failed to write '%s' to /sys/class/rtc/rtc0/wakealarm: %m", str);
214 static int execute_s2h(usec_t hibernate_delay_sec) {
216 _cleanup_strv_free_ char **hibernate_modes = NULL, **hibernate_states = NULL,
217 **suspend_modes = NULL, **suspend_states = NULL;
218 usec_t orig_time, cmp_time;
219 char time_str[DECIMAL_STR_MAX(uint64_t)];
222 #if 0 /// Already parsed by elogind config
223 r = parse_sleep_config("suspend", &suspend_modes, &suspend_states,
228 r = parse_sleep_config("hibernate", &hibernate_modes,
229 &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);
353 int do_sleep(const char *verb, char **modes, char **states, usec_t delay) {
357 arg_verb = (char*)verb;
359 if (streq(arg_verb, "suspend-then-hibernate"))
360 r = execute_s2h(delay);
362 r = execute(modes, states);
363 #if 0 /// In elogind we give the result back, no interpretation here.
365 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;