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 "parse-util.h"
20 #include "sleep-config.h"
21 #include "stdio-util.h"
22 #include "string-util.h"
24 #include "terminal-util.h"
27 static char* arg_verb
= NULL
;
29 static int write_hibernate_location_info(void) {
30 _cleanup_free_
char *device
= NULL
, *type
= NULL
;
31 _cleanup_free_
struct fiemap
*fiemap
= NULL
;
32 char offset_str
[DECIMAL_STR_MAX(uint64_t)];
33 char device_str
[DECIMAL_STR_MAX(uint64_t)];
34 _cleanup_close_
int fd
= -1;
39 r
= find_hibernate_location(&device
, &type
, NULL
, NULL
);
41 return log_debug_errno(r
, "Unable to find hibernation location: %m");
43 /* if it's a swap partition, we just write the disk to /sys/power/resume */
44 if (streq(type
, "partition")) {
45 r
= write_string_file("/sys/power/resume", device
, WRITE_STRING_FILE_DISABLE_BUFFER
);
47 return log_debug_errno(r
, "Faileed to write partitoin device to /sys/power/resume: %m");
51 if (!streq(type
, "file")) {
52 log_debug("Invalid hibernate type: %s", type
);
56 /* Only available in 4.17+ */
57 if (access("/sys/power/resume_offset", W_OK
) < 0) {
58 if (errno
== ENOENT
) {
59 log_debug("Kernel too old, can't configure resume offset, ignoring.");
63 return log_debug_errno(errno
, "/sys/power/resume_offset not writeable: %m");
66 fd
= open(device
, O_RDONLY
| O_CLOEXEC
| O_NONBLOCK
);
68 return log_debug_errno(errno
, "Unable to open '%s': %m", device
);
71 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", device
);
76 if (fiemap
->fm_mapped_extents
== 0) {
77 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
, WRITE_STRING_FILE_DISABLE_BUFFER
);
85 return log_debug_errno(r
, "Failed to write offset '%s': %m", offset_str
);
87 xsprintf(device_str
, "%lx", (unsigned long)stb
.st_dev
);
88 r
= write_string_file("/sys/power/resume", device_str
, WRITE_STRING_FILE_DISABLE_BUFFER
);
90 return log_debug_errno(r
, "Failed to write device '%s': %m", device_str
);
95 static int write_mode(char **modes
) {
99 STRV_FOREACH(mode
, modes
) {
102 k
= write_string_file("/sys/power/disk", *mode
, WRITE_STRING_FILE_DISABLE_BUFFER
);
106 log_debug_errno(k
, "Failed to write '%s' to /sys/power/disk: %m", *mode
);
114 static int write_state(FILE **f
, char **states
) {
118 STRV_FOREACH(state
, states
) {
121 k
= write_string_stream(*f
, *state
, WRITE_STRING_FILE_DISABLE_BUFFER
);
124 log_debug_errno(k
, "Failed to write '%s' to /sys/power/state: %m", *state
);
129 *f
= fopen("/sys/power/state", "we");
137 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 setvbuf(f
, NULL
, _IONBF
, 0);
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
, NULL
);
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 log_struct_errno(LOG_ERR
, r
,
180 "MESSAGE_ID=" SD_MESSAGE_SLEEP_STOP_STR
,
181 LOG_MESSAGE("Failed to suspend system. System resumed again: %m"),
182 "SLEEP=%s", arg_verb
);
185 "MESSAGE_ID=" SD_MESSAGE_SLEEP_STOP_STR
,
186 LOG_MESSAGE("System resumed."),
187 "SLEEP=%s", arg_verb
);
189 arguments
[1] = (char*) "post";
190 execute_directories(dirs
, DEFAULT_TIMEOUT_USEC
, NULL
, NULL
, arguments
, NULL
);
195 static int rtc_read_time(uint64_t *ret_sec
) {
196 _cleanup_free_
char *t
= NULL
;
199 r
= read_one_line_file("/sys/class/rtc/rtc0/since_epoch", &t
);
201 return log_error_errno(r
, "Failed to read RTC time: %m");
203 r
= safe_atou64(t
, ret_sec
);
205 return log_error_errno(r
, "Failed to parse RTC time '%s': %m", t
);
210 static int rtc_write_wake_alarm(uint64_t sec
) {
211 char buf
[DECIMAL_STR_MAX(uint64_t)];
214 xsprintf(buf
, "%" PRIu64
, sec
);
216 r
= write_string_file("/sys/class/rtc/rtc0/wakealarm", buf
, WRITE_STRING_FILE_DISABLE_BUFFER
);
218 return log_error_errno(r
, "Failed to write '%s' to /sys/class/rtc/rtc0/wakealarm: %m", buf
);
223 static int execute_s2h(usec_t hibernate_delay_sec
) {
225 _cleanup_strv_free_
char **hibernate_modes
= NULL
, **hibernate_states
= NULL
,
226 **suspend_modes
= NULL
, **suspend_states
= NULL
;
227 usec_t original_time
, wake_time
, cmp_time
;
230 r
= parse_sleep_config("suspend", NULL
, &suspend_modes
, &suspend_states
, NULL
);
234 r
= parse_sleep_config("hibernate", NULL
, &hibernate_modes
, &hibernate_states
, NULL
);
238 r
= rtc_read_time(&original_time
);
242 wake_time
= original_time
+ DIV_ROUND_UP(hibernate_delay_sec
, USEC_PER_SEC
);
243 r
= rtc_write_wake_alarm(wake_time
);
247 log_debug("Set RTC wake alarm for %" PRIu64
, wake_time
);
249 r
= execute(suspend_modes
, suspend_states
);
253 /* Reset RTC right-away */
254 r
= rtc_write_wake_alarm(0);
258 r
= rtc_read_time(&cmp_time
);
262 log_debug("Woke up at %"PRIu64
, cmp_time
);
264 if (cmp_time
< wake_time
) /* We woke up before the alarm time, we are done. */
267 /* If woken up after alarm time, hibernate */
268 r
= execute(hibernate_modes
, hibernate_states
);
270 log_notice("Couldn't hibernate, will try to suspend again.");
271 r
= execute(suspend_modes
, suspend_states
);
273 log_notice("Could neither hibernate nor suspend again, giving up.");
281 static int help(void) {
282 _cleanup_free_
char *link
= NULL
;
285 r
= terminal_urlify_man("systemd-suspend.service", "8", &link
);
289 printf("%s COMMAND\n\n"
290 "Suspend the system, hibernate the system, or both.\n\n"
291 " -h --help Show this help and exit\n"
292 " --version Print version string and exit\n"
294 " suspend Suspend the system\n"
295 " hibernate Hibernate the system\n"
296 " hybrid-sleep Both hibernate and suspend the system\n"
297 " suspend-then-hibernate Initially suspend and then hibernate\n"
298 " the system after a fixed period of time\n"
299 "\nSee the %s for details.\n"
300 , program_invocation_short_name
307 static int parse_argv(int argc
, char *argv
[]) {
312 static const struct option options
[] = {
313 { "help", no_argument
, NULL
, 'h' },
314 { "version", no_argument
, NULL
, ARG_VERSION
},
323 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
335 assert_not_reached("Unhandled option");
338 if (argc
- optind
!= 1) {
339 log_error("Usage: %s COMMAND",
340 program_invocation_short_name
);
344 arg_verb
= argv
[optind
];
346 if (!STR_IN_SET(arg_verb
, "suspend", "hibernate", "hybrid-sleep", "suspend-then-hibernate")) {
347 log_error("Unknown command '%s'.", arg_verb
);
351 return 1 /* work to do */;
354 int main(int argc
, char *argv
[]) {
356 _cleanup_strv_free_
char **modes
= NULL
, **states
= NULL
;
360 log_set_target(LOG_TARGET_AUTO
);
361 log_parse_environment();
364 r
= parse_argv(argc
, argv
);
368 r
= parse_sleep_config(arg_verb
, &allow
, &modes
, &states
, &delay
);
373 log_error("Sleep mode \"%s\" is disabled by configuration, refusing.", arg_verb
);
377 if (streq(arg_verb
, "suspend-then-hibernate"))
378 r
= execute_s2h(delay
);
380 r
= execute(modes
, states
);
383 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;