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"
15 #include "exec-util.h"
19 #include "main-func.h"
20 #include "parse-util.h"
21 #include "pretty-print.h"
22 #include "sleep-config.h"
23 #include "stdio-util.h"
24 #include "string-util.h"
28 static char* arg_verb
= NULL
;
30 static int write_hibernate_location_info(void) {
31 _cleanup_free_
char *device
= NULL
, *type
= NULL
;
32 _cleanup_free_
struct fiemap
*fiemap
= NULL
;
33 char offset_str
[DECIMAL_STR_MAX(uint64_t)];
34 char device_str
[DECIMAL_STR_MAX(uint64_t)];
35 _cleanup_close_
int fd
= -1;
40 r
= find_hibernate_location(&device
, &type
, NULL
, NULL
);
42 return log_debug_errno(r
, "Unable to find hibernation location: %m");
44 /* if it's a swap partition, we just write the disk to /sys/power/resume */
45 if (streq(type
, "partition")) {
46 r
= write_string_file("/sys/power/resume", device
, WRITE_STRING_FILE_DISABLE_BUFFER
);
48 return log_debug_errno(r
, "Faileed to write partitoin device to /sys/power/resume: %m");
52 if (!streq(type
, "file")) {
53 log_debug("Invalid hibernate type: %s", type
);
57 /* Only available in 4.17+ */
58 if (access("/sys/power/resume_offset", W_OK
) < 0) {
59 if (errno
== ENOENT
) {
60 log_debug("Kernel too old, can't configure resume offset, ignoring.");
64 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
);
74 r
= read_fiemap(fd
, &fiemap
);
76 return log_debug_errno(r
, "Unable to read extent map for '%s': %m", device
);
77 if (fiemap
->fm_mapped_extents
== 0) {
78 log_debug("No extents found in '%s'", device
);
82 offset
= fiemap
->fm_extents
[0].fe_physical
/ page_size();
83 xsprintf(offset_str
, "%" PRIu64
, offset
);
84 r
= write_string_file("/sys/power/resume_offset", offset_str
, WRITE_STRING_FILE_DISABLE_BUFFER
);
86 return log_debug_errno(r
, "Failed to write offset '%s': %m", offset_str
);
88 xsprintf(device_str
, "%lx", (unsigned long)stb
.st_dev
);
89 r
= write_string_file("/sys/power/resume", device_str
, WRITE_STRING_FILE_DISABLE_BUFFER
);
91 return log_debug_errno(r
, "Failed to write device '%s': %m", device_str
);
96 static int write_mode(char **modes
) {
100 STRV_FOREACH(mode
, modes
) {
103 k
= write_string_file("/sys/power/disk", *mode
, WRITE_STRING_FILE_DISABLE_BUFFER
);
107 log_debug_errno(k
, "Failed to write '%s' to /sys/power/disk: %m", *mode
);
115 static int write_state(FILE **f
, char **states
) {
119 STRV_FOREACH(state
, states
) {
122 k
= write_string_stream(*f
, *state
, WRITE_STRING_FILE_DISABLE_BUFFER
);
125 log_debug_errno(k
, "Failed to write '%s' to /sys/power/state: %m", *state
);
130 *f
= fopen("/sys/power/state", "we");
138 static int execute(char **modes
, char **states
) {
139 char *arguments
[] = {
145 static const char* const dirs
[] = {
151 _cleanup_fclose_
FILE *f
= NULL
;
153 /* This file is opened first, so that if we hit an error,
154 * we can abort before modifying any state. */
155 f
= fopen("/sys/power/state", "we");
157 return log_error_errno(errno
, "Failed to open /sys/power/state: %m");
159 setvbuf(f
, NULL
, _IONBF
, 0);
161 /* Configure the hibernation mode */
162 if (!strv_isempty(modes
)) {
163 r
= write_hibernate_location_info();
165 return log_error_errno(r
, "Failed to write hibernation disk offset: %m");
166 r
= write_mode(modes
);
168 return log_error_errno(r
, "Failed to write mode to /sys/power/disk: %m");;
171 execute_directories(dirs
, DEFAULT_TIMEOUT_USEC
, NULL
, NULL
, arguments
, NULL
);
174 "MESSAGE_ID=" SD_MESSAGE_SLEEP_START_STR
,
175 LOG_MESSAGE("Suspending system..."),
176 "SLEEP=%s", arg_verb
);
178 r
= write_state(&f
, states
);
180 log_struct_errno(LOG_ERR
, r
,
181 "MESSAGE_ID=" SD_MESSAGE_SLEEP_STOP_STR
,
182 LOG_MESSAGE("Failed to suspend system. System resumed again: %m"),
183 "SLEEP=%s", arg_verb
);
186 "MESSAGE_ID=" SD_MESSAGE_SLEEP_STOP_STR
,
187 LOG_MESSAGE("System resumed."),
188 "SLEEP=%s", arg_verb
);
190 arguments
[1] = (char*) "post";
191 execute_directories(dirs
, DEFAULT_TIMEOUT_USEC
, NULL
, NULL
, arguments
, NULL
);
196 static int rtc_read_time(uint64_t *ret_sec
) {
197 _cleanup_free_
char *t
= NULL
;
200 r
= read_one_line_file("/sys/class/rtc/rtc0/since_epoch", &t
);
202 return log_error_errno(r
, "Failed to read RTC time: %m");
204 r
= safe_atou64(t
, ret_sec
);
206 return log_error_errno(r
, "Failed to parse RTC time '%s': %m", t
);
211 static int rtc_write_wake_alarm(uint64_t sec
) {
212 char buf
[DECIMAL_STR_MAX(uint64_t)];
215 xsprintf(buf
, "%" PRIu64
, sec
);
217 r
= write_string_file("/sys/class/rtc/rtc0/wakealarm", buf
, WRITE_STRING_FILE_DISABLE_BUFFER
);
219 return log_error_errno(r
, "Failed to write '%s' to /sys/class/rtc/rtc0/wakealarm: %m", buf
);
224 static int execute_s2h(usec_t hibernate_delay_sec
) {
226 _cleanup_strv_free_
char **hibernate_modes
= NULL
, **hibernate_states
= NULL
,
227 **suspend_modes
= NULL
, **suspend_states
= NULL
;
228 usec_t original_time
, wake_time
, cmp_time
;
231 r
= parse_sleep_config("suspend", NULL
, &suspend_modes
, &suspend_states
, NULL
);
235 r
= parse_sleep_config("hibernate", NULL
, &hibernate_modes
, &hibernate_states
, NULL
);
239 r
= rtc_read_time(&original_time
);
243 wake_time
= original_time
+ DIV_ROUND_UP(hibernate_delay_sec
, USEC_PER_SEC
);
244 r
= rtc_write_wake_alarm(wake_time
);
248 log_debug("Set RTC wake alarm for %" PRIu64
, wake_time
);
250 r
= execute(suspend_modes
, suspend_states
);
254 /* Reset RTC right-away */
255 r
= rtc_write_wake_alarm(0);
259 r
= rtc_read_time(&cmp_time
);
263 log_debug("Woke up at %"PRIu64
, cmp_time
);
265 if (cmp_time
< wake_time
) /* We woke up before the alarm time, we are done. */
268 /* If woken up after alarm time, hibernate */
269 r
= execute(hibernate_modes
, hibernate_states
);
271 log_notice("Couldn't hibernate, will try to suspend again.");
272 r
= execute(suspend_modes
, suspend_states
);
274 log_notice("Could neither hibernate nor suspend again, giving up.");
282 static int help(void) {
283 _cleanup_free_
char *link
= NULL
;
286 r
= terminal_urlify_man("systemd-suspend.service", "8", &link
);
290 printf("%s COMMAND\n\n"
291 "Suspend the system, hibernate the system, or both.\n\n"
292 " -h --help Show this help and exit\n"
293 " --version Print version string and exit\n"
295 " suspend Suspend the system\n"
296 " hibernate Hibernate the system\n"
297 " hybrid-sleep Both hibernate and suspend the system\n"
298 " suspend-then-hibernate Initially suspend and then hibernate\n"
299 " the system after a fixed period of time\n"
300 "\nSee the %s for details.\n"
301 , program_invocation_short_name
308 static int parse_argv(int argc
, char *argv
[]) {
313 static const struct option options
[] = {
314 { "help", no_argument
, NULL
, 'h' },
315 { "version", no_argument
, NULL
, ARG_VERSION
},
324 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
336 assert_not_reached("Unhandled option");
339 if (argc
- optind
!= 1) {
340 log_error("Usage: %s COMMAND",
341 program_invocation_short_name
);
345 arg_verb
= argv
[optind
];
347 if (!STR_IN_SET(arg_verb
, "suspend", "hibernate", "hybrid-sleep", "suspend-then-hibernate")) {
348 log_error("Unknown command '%s'.", arg_verb
);
352 return 1 /* work to do */;
355 static int run(int argc
, char *argv
[]) {
357 _cleanup_strv_free_
char **modes
= NULL
, **states
= NULL
;
363 r
= parse_argv(argc
, argv
);
367 r
= parse_sleep_config(arg_verb
, &allow
, &modes
, &states
, &delay
);
372 log_error("Sleep mode \"%s\" is disabled by configuration, refusing.", arg_verb
);
376 if (streq(arg_verb
, "suspend-then-hibernate"))
377 return execute_s2h(delay
);
379 return execute(modes
, states
);
382 DEFINE_MAIN_FUNCTION(run
);