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 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
54 "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 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
78 "No extents found in '%s'", device
);
80 offset
= fiemap
->fm_extents
[0].fe_physical
/ page_size();
81 xsprintf(offset_str
, "%" PRIu64
, offset
);
82 r
= write_string_file("/sys/power/resume_offset", offset_str
, WRITE_STRING_FILE_DISABLE_BUFFER
);
84 return log_debug_errno(r
, "Failed to write offset '%s': %m", offset_str
);
86 xsprintf(device_str
, "%lx", (unsigned long)stb
.st_dev
);
87 r
= write_string_file("/sys/power/resume", device_str
, WRITE_STRING_FILE_DISABLE_BUFFER
);
89 return log_debug_errno(r
, "Failed to write device '%s': %m", device_str
);
94 static int write_mode(char **modes
) {
98 STRV_FOREACH(mode
, modes
) {
101 k
= write_string_file("/sys/power/disk", *mode
, WRITE_STRING_FILE_DISABLE_BUFFER
);
105 log_debug_errno(k
, "Failed to write '%s' to /sys/power/disk: %m", *mode
);
113 static int write_state(FILE **f
, char **states
) {
117 STRV_FOREACH(state
, states
) {
120 k
= write_string_stream(*f
, *state
, WRITE_STRING_FILE_DISABLE_BUFFER
);
123 log_debug_errno(k
, "Failed to write '%s' to /sys/power/state: %m", *state
);
128 *f
= fopen("/sys/power/state", "we");
136 static int execute(char **modes
, char **states
) {
137 char *arguments
[] = {
143 static const char* const dirs
[] = {
149 _cleanup_fclose_
FILE *f
= NULL
;
151 /* This file is opened first, so that if we hit an error,
152 * we can abort before modifying any state. */
153 f
= fopen("/sys/power/state", "we");
155 return log_error_errno(errno
, "Failed to open /sys/power/state: %m");
157 setvbuf(f
, NULL
, _IONBF
, 0);
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
, NULL
, EXEC_DIR_PARALLEL
| EXEC_DIR_IGNORE_ERRORS
);
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 log_struct_errno(LOG_ERR
, r
,
179 "MESSAGE_ID=" SD_MESSAGE_SLEEP_STOP_STR
,
180 LOG_MESSAGE("Failed to suspend system. System resumed again: %m"),
181 "SLEEP=%s", arg_verb
);
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
, NULL
, EXEC_DIR_PARALLEL
| EXEC_DIR_IGNORE_ERRORS
);
194 static int rtc_read_time(uint64_t *ret_sec
) {
195 _cleanup_free_
char *t
= NULL
;
198 r
= read_one_line_file("/sys/class/rtc/rtc0/since_epoch", &t
);
200 return log_error_errno(r
, "Failed to read RTC time: %m");
202 r
= safe_atou64(t
, ret_sec
);
204 return log_error_errno(r
, "Failed to parse RTC time '%s': %m", t
);
209 static int rtc_write_wake_alarm(uint64_t sec
) {
210 char buf
[DECIMAL_STR_MAX(uint64_t)];
213 xsprintf(buf
, "%" PRIu64
, sec
);
215 r
= write_string_file("/sys/class/rtc/rtc0/wakealarm", buf
, WRITE_STRING_FILE_DISABLE_BUFFER
);
217 return log_error_errno(r
, "Failed to write '%s' to /sys/class/rtc/rtc0/wakealarm: %m", buf
);
222 static int execute_s2h(usec_t hibernate_delay_sec
) {
224 _cleanup_strv_free_
char **hibernate_modes
= NULL
, **hibernate_states
= NULL
,
225 **suspend_modes
= NULL
, **suspend_states
= NULL
;
226 usec_t original_time
, wake_time
, cmp_time
;
229 r
= parse_sleep_config("suspend", NULL
, &suspend_modes
, &suspend_states
, NULL
);
233 r
= parse_sleep_config("hibernate", NULL
, &hibernate_modes
, &hibernate_states
, NULL
);
237 r
= rtc_read_time(&original_time
);
241 wake_time
= original_time
+ DIV_ROUND_UP(hibernate_delay_sec
, USEC_PER_SEC
);
242 r
= rtc_write_wake_alarm(wake_time
);
246 log_debug("Set RTC wake alarm for %" PRIu64
, wake_time
);
248 r
= execute(suspend_modes
, suspend_states
);
252 /* Reset RTC right-away */
253 r
= rtc_write_wake_alarm(0);
257 r
= rtc_read_time(&cmp_time
);
261 log_debug("Woke up at %"PRIu64
, cmp_time
);
263 if (cmp_time
< wake_time
) /* We woke up before the alarm time, we are done. */
266 /* If woken up after alarm time, hibernate */
267 r
= execute(hibernate_modes
, hibernate_states
);
269 log_notice("Couldn't hibernate, will try to suspend again.");
270 r
= execute(suspend_modes
, suspend_states
);
272 log_notice("Could neither hibernate nor suspend again, giving up.");
280 static int help(void) {
281 _cleanup_free_
char *link
= NULL
;
284 r
= terminal_urlify_man("systemd-suspend.service", "8", &link
);
288 printf("%s COMMAND\n\n"
289 "Suspend the system, hibernate the system, or both.\n\n"
290 " -h --help Show this help and exit\n"
291 " --version Print version string and exit\n"
293 " suspend Suspend the system\n"
294 " hibernate Hibernate the system\n"
295 " hybrid-sleep Both hibernate and suspend the system\n"
296 " suspend-then-hibernate Initially suspend and then hibernate\n"
297 " the system after a fixed period of time\n"
298 "\nSee the %s for details.\n"
299 , program_invocation_short_name
306 static int parse_argv(int argc
, char *argv
[]) {
311 static const struct option options
[] = {
312 { "help", no_argument
, NULL
, 'h' },
313 { "version", no_argument
, NULL
, ARG_VERSION
},
322 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
334 assert_not_reached("Unhandled option");
337 if (argc
- optind
!= 1)
338 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
340 program_invocation_short_name
);
342 arg_verb
= argv
[optind
];
344 if (!STR_IN_SET(arg_verb
, "suspend", "hibernate", "hybrid-sleep", "suspend-then-hibernate"))
345 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
346 "Unknown command '%s'.", arg_verb
);
348 return 1 /* work to do */;
351 static int run(int argc
, char *argv
[]) {
353 _cleanup_strv_free_
char **modes
= NULL
, **states
= NULL
;
359 r
= parse_argv(argc
, argv
);
363 r
= parse_sleep_config(arg_verb
, &allow
, &modes
, &states
, &delay
);
368 return log_error_errno(SYNTHETIC_ERRNO(EACCES
),
369 "Sleep mode \"%s\" is disabled by configuration, refusing.",
372 if (streq(arg_verb
, "suspend-then-hibernate"))
373 return execute_s2h(delay
);
375 return execute(modes
, states
);
378 DEFINE_MAIN_FUNCTION(run
);