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 static char* arg_verb
= NULL
;
32 static int write_hibernate_location_info(void) {
33 _cleanup_free_
char *device
= NULL
, *type
= NULL
;
34 _cleanup_free_
struct fiemap
*fiemap
= NULL
;
35 char offset_str
[DECIMAL_STR_MAX(uint64_t)];
36 char device_str
[DECIMAL_STR_MAX(uint64_t)];
37 _cleanup_close_
int fd
= -1;
42 r
= find_hibernate_location(&device
, &type
, NULL
, NULL
);
44 return log_debug_errno(r
, "Unable to find hibernation location: %m");
46 /* if it's a swap partition, we just write the disk to /sys/power/resume */
47 if (streq(type
, "partition"))
48 return write_string_file("/sys/power/resume", device
, 0);
49 else if (!streq(type
, "file"))
50 return log_debug_errno(EINVAL
, "Invalid hibernate type %s: %m",
53 /* Only available in 4.17+ */
54 if (access("/sys/power/resume_offset", F_OK
) < 0) {
57 return log_debug_errno(errno
, "/sys/power/resume_offset unavailable: %m");
60 r
= access("/sys/power/resume_offset", W_OK
);
62 return log_debug_errno(errno
, "/sys/power/resume_offset not writeable: %m");
64 fd
= open(device
, O_RDONLY
| O_CLOEXEC
| O_NONBLOCK
);
66 return log_debug_errno(errno
, "Unable to open '%s': %m", device
);
69 return log_debug_errno(errno
, "Unable to stat %s: %m", device
);
70 r
= read_fiemap(fd
, &fiemap
);
72 return log_debug_errno(r
, "Unable to read extent map for '%s': %m",
74 if (fiemap
->fm_mapped_extents
== 0) {
75 log_debug("No extents found in '%s'", device
);
78 offset
= fiemap
->fm_extents
[0].fe_physical
/ page_size();
79 xsprintf(offset_str
, "%" PRIu64
, offset
);
80 r
= write_string_file("/sys/power/resume_offset", offset_str
, 0);
82 return log_debug_errno(r
, "Failed to write offset '%s': %m",
85 xsprintf(device_str
, "%lx", (unsigned long)stb
.st_dev
);
86 r
= write_string_file("/sys/power/resume", device_str
, 0);
88 return log_debug_errno(r
, "Failed to write device '%s': %m",
93 static int write_mode(char **modes
) {
97 STRV_FOREACH(mode
, modes
) {
100 k
= write_string_file("/sys/power/disk", *mode
, 0);
104 log_debug_errno(k
, "Failed to write '%s' to /sys/power/disk: %m",
113 static int write_state(FILE **f
, char **states
) {
117 STRV_FOREACH(state
, states
) {
120 k
= write_string_stream(*f
, *state
, 0);
123 log_debug_errno(k
, "Failed to write '%s' to /sys/power/state: %m",
129 *f
= fopen("/sys/power/state", "we");
137 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 /* Configure the hibernation mode */
160 if (!strv_isempty(modes
)) {
161 r
= write_hibernate_location_info();
163 return log_error_errno(r
, "Failed to write hibernation disk offset: %m");
164 r
= write_mode(modes
);
166 return log_error_errno(r
, "Failed to write mode to /sys/power/disk: %m");;
169 execute_directories(dirs
, DEFAULT_TIMEOUT_USEC
, NULL
, NULL
, arguments
);
172 "MESSAGE_ID=" SD_MESSAGE_SLEEP_START_STR
,
173 LOG_MESSAGE("Suspending system..."),
174 "SLEEP=%s", arg_verb
);
176 r
= write_state(&f
, states
);
178 return log_error_errno(r
, "Failed to write /sys/power/state: %m");
181 "MESSAGE_ID=" SD_MESSAGE_SLEEP_STOP_STR
,
182 LOG_MESSAGE("System resumed."),
183 "SLEEP=%s", arg_verb
);
185 arguments
[1] = (char*) "post";
186 execute_directories(dirs
, DEFAULT_TIMEOUT_USEC
, NULL
, NULL
, arguments
);
191 static int read_wakealarm(uint64_t *result
) {
192 _cleanup_free_
char *t
= NULL
;
194 if (read_one_line_file("/sys/class/rtc/rtc0/since_epoch", &t
) >= 0)
195 return safe_atou64(t
, result
);
199 static int write_wakealarm(const char *str
) {
201 _cleanup_fclose_
FILE *f
= NULL
;
204 f
= fopen("/sys/class/rtc/rtc0/wakealarm", "we");
206 return log_error_errno(errno
, "Failed to open /sys/class/rtc/rtc0/wakealarm: %m");
208 r
= write_string_stream(f
, str
, 0);
210 return log_error_errno(r
, "Failed to write '%s' to /sys/class/rtc/rtc0/wakealarm: %m", str
);
215 static int execute_s2h(usec_t hibernate_delay_sec
) {
217 _cleanup_strv_free_
char **hibernate_modes
= NULL
, **hibernate_states
= NULL
,
218 **suspend_modes
= NULL
, **suspend_states
= NULL
;
219 usec_t orig_time
, cmp_time
;
220 char time_str
[DECIMAL_STR_MAX(uint64_t)];
223 r
= parse_sleep_config("suspend", &suspend_modes
, &suspend_states
,
228 r
= parse_sleep_config("hibernate", &hibernate_modes
,
229 &hibernate_states
, NULL
);
233 r
= read_wakealarm(&orig_time
);
235 return log_error_errno(errno
, "Failed to read time: %d", r
);
237 orig_time
+= hibernate_delay_sec
/ USEC_PER_SEC
;
238 xsprintf(time_str
, "%" PRIu64
, orig_time
);
240 r
= write_wakealarm(time_str
);
244 log_debug("Set RTC wake alarm for %s", time_str
);
246 r
= execute(suspend_modes
, suspend_states
);
250 r
= read_wakealarm(&cmp_time
);
252 return log_error_errno(errno
, "Failed to read time: %d", r
);
255 r
= write_wakealarm("0");
259 log_debug("Woke up at %"PRIu64
, cmp_time
);
261 /* if woken up after alarm time, hibernate */
262 if (cmp_time
>= orig_time
)
263 r
= execute(hibernate_modes
, hibernate_states
);
268 static void help(void) {
269 printf("%s COMMAND\n\n"
270 "Suspend the system, hibernate the system, or both.\n\n"
272 " -h --help Show this help and exit\n"
273 " --version Print version string and exit\n"
274 " suspend Suspend the system\n"
275 " hibernate Hibernate the system\n"
276 " hybrid-sleep Both hibernate and suspend the system\n"
277 " suspend-then-hibernate Initially suspend and then hibernate\n"
278 " the system after a fixed period of time\n"
279 , program_invocation_short_name
);
282 static int parse_argv(int argc
, char *argv
[]) {
287 static const struct option options
[] = {
288 { "help", no_argument
, NULL
, 'h' },
289 { "version", no_argument
, NULL
, ARG_VERSION
},
298 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
311 assert_not_reached("Unhandled option");
314 if (argc
- optind
!= 1) {
315 log_error("Usage: %s COMMAND",
316 program_invocation_short_name
);
320 arg_verb
= argv
[optind
];
322 if (!streq(arg_verb
, "suspend") &&
323 !streq(arg_verb
, "hibernate") &&
324 !streq(arg_verb
, "hybrid-sleep") &&
325 !streq(arg_verb
, "suspend-then-hibernate")) {
326 log_error("Unknown command '%s'.", arg_verb
);
330 return 1 /* work to do */;
333 int main(int argc
, char *argv
[]) {
334 _cleanup_strv_free_
char **modes
= NULL
, **states
= NULL
;
338 log_set_target(LOG_TARGET_AUTO
);
339 log_parse_environment();
342 r
= parse_argv(argc
, argv
);
346 r
= parse_sleep_config(arg_verb
, &modes
, &states
, &delay
);
350 if (streq(arg_verb
, "suspend-then-hibernate"))
351 r
= execute_s2h(delay
);
353 r
= execute(modes
, states
);
355 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;