1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright © 2010-2017 Canonical
4 Copyright © 2018 Dell Inc.
10 #include <linux/fiemap.h>
14 #include <sys/types.h>
15 #include <sys/timerfd.h>
18 #include "sd-messages.h"
21 #include "exec-util.h"
23 #include "format-util.h"
26 #include "main-func.h"
27 #include "parse-util.h"
28 #include "pretty-print.h"
29 #include "sleep-config.h"
30 #include "stdio-util.h"
31 #include "string-util.h"
33 #include "time-util.h"
36 static char* arg_verb
= NULL
;
38 static int write_hibernate_location_info(void) {
39 _cleanup_free_
char *device
= NULL
, *type
= NULL
;
40 _cleanup_free_
struct fiemap
*fiemap
= NULL
;
41 char offset_str
[DECIMAL_STR_MAX(uint64_t)];
42 char device_str
[DECIMAL_STR_MAX(uint64_t)];
43 _cleanup_close_
int fd
= -1;
48 r
= find_hibernate_location(&device
, &type
, NULL
, NULL
);
50 return log_debug_errno(r
, "Unable to find hibernation location: %m");
52 /* if it's a swap partition, we just write the disk to /sys/power/resume */
53 if (streq(type
, "partition")) {
54 r
= write_string_file("/sys/power/resume", device
, WRITE_STRING_FILE_DISABLE_BUFFER
);
56 return log_debug_errno(r
, "Failed to write partition device to /sys/power/resume: %m");
60 if (!streq(type
, "file"))
61 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
62 "Invalid hibernate type: %s", type
);
64 /* Only available in 4.17+ */
65 if (access("/sys/power/resume_offset", W_OK
) < 0) {
66 if (errno
== ENOENT
) {
67 log_debug("Kernel too old, can't configure resume offset, ignoring.");
71 return log_debug_errno(errno
, "/sys/power/resume_offset not writeable: %m");
74 fd
= open(device
, O_RDONLY
| O_CLOEXEC
| O_NONBLOCK
);
76 return log_debug_errno(errno
, "Unable to open '%s': %m", device
);
79 return log_debug_errno(errno
, "Unable to stat %s: %m", device
);
81 r
= read_fiemap(fd
, &fiemap
);
83 return log_debug_errno(r
, "Unable to read extent map for '%s': %m", device
);
84 if (fiemap
->fm_mapped_extents
== 0)
85 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
86 "No extents found in '%s'", device
);
88 offset
= fiemap
->fm_extents
[0].fe_physical
/ page_size();
89 xsprintf(offset_str
, "%" PRIu64
, offset
);
90 r
= write_string_file("/sys/power/resume_offset", offset_str
, WRITE_STRING_FILE_DISABLE_BUFFER
);
92 return log_debug_errno(r
, "Failed to write offset '%s': %m", offset_str
);
94 xsprintf(device_str
, "%lx", (unsigned long)stb
.st_dev
);
95 r
= write_string_file("/sys/power/resume", device_str
, WRITE_STRING_FILE_DISABLE_BUFFER
);
97 return log_debug_errno(r
, "Failed to write device '%s': %m", device_str
);
102 static int write_mode(char **modes
) {
106 STRV_FOREACH(mode
, modes
) {
109 k
= write_string_file("/sys/power/disk", *mode
, WRITE_STRING_FILE_DISABLE_BUFFER
);
113 log_debug_errno(k
, "Failed to write '%s' to /sys/power/disk: %m", *mode
);
121 static int write_state(FILE **f
, char **states
) {
125 STRV_FOREACH(state
, states
) {
128 k
= write_string_stream(*f
, *state
, WRITE_STRING_FILE_DISABLE_BUFFER
);
131 log_debug_errno(k
, "Failed to write '%s' to /sys/power/state: %m", *state
);
136 *f
= fopen("/sys/power/state", "we");
144 static int execute(char **modes
, char **states
) {
145 char *arguments
[] = {
151 static const char* const dirs
[] = {
157 _cleanup_fclose_
FILE *f
= NULL
;
159 /* This file is opened first, so that if we hit an error,
160 * we can abort before modifying any state. */
161 f
= fopen("/sys/power/state", "we");
163 return log_error_errno(errno
, "Failed to open /sys/power/state: %m");
165 setvbuf(f
, NULL
, _IONBF
, 0);
167 /* Configure the hibernation mode */
168 if (!strv_isempty(modes
)) {
169 r
= write_hibernate_location_info();
171 return log_error_errno(r
, "Failed to write hibernation disk offset: %m");
172 r
= write_mode(modes
);
174 return log_error_errno(r
, "Failed to write mode to /sys/power/disk: %m");;
177 (void) execute_directories(dirs
, DEFAULT_TIMEOUT_USEC
, NULL
, NULL
, arguments
, NULL
, EXEC_DIR_PARALLEL
| EXEC_DIR_IGNORE_ERRORS
);
180 "MESSAGE_ID=" SD_MESSAGE_SLEEP_START_STR
,
181 LOG_MESSAGE("Suspending system..."),
182 "SLEEP=%s", arg_verb
);
184 r
= write_state(&f
, states
);
186 log_struct_errno(LOG_ERR
, r
,
187 "MESSAGE_ID=" SD_MESSAGE_SLEEP_STOP_STR
,
188 LOG_MESSAGE("Failed to suspend system. System resumed again: %m"),
189 "SLEEP=%s", arg_verb
);
192 "MESSAGE_ID=" SD_MESSAGE_SLEEP_STOP_STR
,
193 LOG_MESSAGE("System resumed."),
194 "SLEEP=%s", arg_verb
);
196 arguments
[1] = (char*) "post";
197 (void) execute_directories(dirs
, DEFAULT_TIMEOUT_USEC
, NULL
, NULL
, arguments
, NULL
, EXEC_DIR_PARALLEL
| EXEC_DIR_IGNORE_ERRORS
);
202 static int execute_s2h(const SleepConfig
*sleep_config
) {
203 _cleanup_close_
int tfd
= -1;
204 char buf
[FORMAT_TIMESPAN_MAX
];
205 struct itimerspec ts
= {};
209 assert(sleep_config
);
211 tfd
= timerfd_create(CLOCK_BOOTTIME_ALARM
, TFD_NONBLOCK
|TFD_CLOEXEC
);
213 return log_error_errno(errno
, "Error creating timerfd: %m");
215 log_debug("Set timerfd wake alarm for %s",
216 format_timespan(buf
, sizeof(buf
), sleep_config
->hibernate_delay_sec
, USEC_PER_SEC
));
218 timespec_store(&ts
.it_value
, sleep_config
->hibernate_delay_sec
);
220 r
= timerfd_settime(tfd
, 0, &ts
, NULL
);
222 return log_error_errno(errno
, "Error setting hibernate timer: %m");
224 r
= execute(sleep_config
->suspend_modes
, sleep_config
->suspend_states
);
228 fds
= (struct pollfd
) {
232 r
= poll(&fds
, 1, 0);
234 return log_error_errno(errno
, "Error polling timerfd: %m");
236 tfd
= safe_close(tfd
);
238 if (!FLAGS_SET(fds
.revents
, POLLIN
)) /* We woke up before the alarm time, we are done. */
241 /* If woken up after alarm time, hibernate */
242 log_debug("Attempting to hibernate after waking from %s timer",
243 format_timespan(buf
, sizeof(buf
), sleep_config
->hibernate_delay_sec
, USEC_PER_SEC
));
245 r
= execute(sleep_config
->hibernate_modes
, sleep_config
->hibernate_states
);
247 log_notice("Couldn't hibernate, will try to suspend again.");
248 r
= execute(sleep_config
->suspend_modes
, sleep_config
->suspend_states
);
250 log_notice("Could neither hibernate nor suspend again, giving up.");
258 static int help(void) {
259 _cleanup_free_
char *link
= NULL
;
262 r
= terminal_urlify_man("systemd-suspend.service", "8", &link
);
266 printf("%s COMMAND\n\n"
267 "Suspend the system, hibernate the system, or both.\n\n"
268 " -h --help Show this help and exit\n"
269 " --version Print version string and exit\n"
271 " suspend Suspend the system\n"
272 " hibernate Hibernate the system\n"
273 " hybrid-sleep Both hibernate and suspend the system\n"
274 " suspend-then-hibernate Initially suspend and then hibernate\n"
275 " the system after a fixed period of time\n"
276 "\nSee the %s for details.\n"
277 , 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)
312 assert_not_reached("Unhandled option");
315 if (argc
- optind
!= 1)
316 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
318 program_invocation_short_name
);
320 arg_verb
= argv
[optind
];
322 if (!STR_IN_SET(arg_verb
, "suspend", "hibernate", "hybrid-sleep", "suspend-then-hibernate"))
323 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
324 "Unknown command '%s'.", arg_verb
);
326 return 1 /* work to do */;
329 static int run(int argc
, char *argv
[]) {
331 char **modes
= NULL
, **states
= NULL
;
332 _cleanup_(free_sleep_configp
) SleepConfig
*sleep_config
= NULL
;
337 r
= parse_argv(argc
, argv
);
341 r
= parse_sleep_config(&sleep_config
);
345 r
= sleep_settings(arg_verb
, sleep_config
, &allow
, &modes
, &states
);
350 return log_error_errno(SYNTHETIC_ERRNO(EACCES
),
351 "Sleep mode \"%s\" is disabled by configuration, refusing.",
354 if (streq(arg_verb
, "suspend-then-hibernate"))
355 return execute_s2h(sleep_config
);
357 return execute(modes
, states
);
360 DEFINE_MAIN_FUNCTION(run
);