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_DESTRUCTOR_REGISTER(arg_verb
, freep
);
40 static int write_hibernate_location_info(void) {
41 _cleanup_free_
char *device
= NULL
, *type
= NULL
;
42 _cleanup_free_
struct fiemap
*fiemap
= NULL
;
43 char offset_str
[DECIMAL_STR_MAX(uint64_t)];
44 char device_str
[DECIMAL_STR_MAX(uint64_t)];
45 _cleanup_close_
int fd
= -1;
50 r
= find_hibernate_location(&device
, &type
, NULL
, NULL
);
52 return log_debug_errno(r
, "Unable to find hibernation location: %m");
54 /* if it's a swap partition, we just write the disk to /sys/power/resume */
55 if (streq(type
, "partition")) {
56 r
= write_string_file("/sys/power/resume", device
, WRITE_STRING_FILE_DISABLE_BUFFER
);
58 return log_debug_errno(r
, "Failed to write partition device to /sys/power/resume: %m");
62 if (!streq(type
, "file"))
63 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
64 "Invalid hibernate type: %s", type
);
66 /* Only available in 4.17+ */
67 if (access("/sys/power/resume_offset", W_OK
) < 0) {
68 if (errno
== ENOENT
) {
69 log_debug("Kernel too old, can't configure resume offset, ignoring.");
73 return log_debug_errno(errno
, "/sys/power/resume_offset not writeable: %m");
76 fd
= open(device
, O_RDONLY
| O_CLOEXEC
| O_NONBLOCK
);
78 return log_debug_errno(errno
, "Unable to open '%s': %m", device
);
81 return log_debug_errno(errno
, "Unable to stat %s: %m", device
);
83 r
= read_fiemap(fd
, &fiemap
);
85 return log_debug_errno(r
, "Unable to read extent map for '%s': %m", device
);
86 if (fiemap
->fm_mapped_extents
== 0)
87 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
88 "No extents found in '%s'", device
);
90 offset
= fiemap
->fm_extents
[0].fe_physical
/ page_size();
91 xsprintf(offset_str
, "%" PRIu64
, offset
);
92 r
= write_string_file("/sys/power/resume_offset", offset_str
, WRITE_STRING_FILE_DISABLE_BUFFER
);
94 return log_debug_errno(r
, "Failed to write offset '%s': %m", offset_str
);
96 xsprintf(device_str
, "%lx", (unsigned long)stb
.st_dev
);
97 r
= write_string_file("/sys/power/resume", device_str
, WRITE_STRING_FILE_DISABLE_BUFFER
);
99 return log_debug_errno(r
, "Failed to write device '%s': %m", device_str
);
104 static int write_mode(char **modes
) {
108 STRV_FOREACH(mode
, modes
) {
111 k
= write_string_file("/sys/power/disk", *mode
, WRITE_STRING_FILE_DISABLE_BUFFER
);
115 log_debug_errno(k
, "Failed to write '%s' to /sys/power/disk: %m", *mode
);
123 static int write_state(FILE **f
, char **states
) {
127 STRV_FOREACH(state
, states
) {
130 k
= write_string_stream(*f
, *state
, WRITE_STRING_FILE_DISABLE_BUFFER
);
133 log_debug_errno(k
, "Failed to write '%s' to /sys/power/state: %m", *state
);
138 *f
= fopen("/sys/power/state", "we");
146 static int execute(char **modes
, char **states
) {
147 char *arguments
[] = {
153 static const char* const dirs
[] = {
159 _cleanup_fclose_
FILE *f
= NULL
;
161 /* This file is opened first, so that if we hit an error,
162 * we can abort before modifying any state. */
163 f
= fopen("/sys/power/state", "we");
165 return log_error_errno(errno
, "Failed to open /sys/power/state: %m");
167 setvbuf(f
, NULL
, _IONBF
, 0);
169 /* Configure the hibernation mode */
170 if (!strv_isempty(modes
)) {
171 r
= write_hibernate_location_info();
173 return log_error_errno(r
, "Failed to write hibernation disk offset: %m");
174 r
= write_mode(modes
);
176 return log_error_errno(r
, "Failed to write mode to /sys/power/disk: %m");;
179 (void) execute_directories(dirs
, DEFAULT_TIMEOUT_USEC
, NULL
, NULL
, arguments
, NULL
, EXEC_DIR_PARALLEL
| EXEC_DIR_IGNORE_ERRORS
);
182 "MESSAGE_ID=" SD_MESSAGE_SLEEP_START_STR
,
183 LOG_MESSAGE("Suspending system..."),
184 "SLEEP=%s", arg_verb
);
186 r
= write_state(&f
, states
);
188 log_struct_errno(LOG_ERR
, r
,
189 "MESSAGE_ID=" SD_MESSAGE_SLEEP_STOP_STR
,
190 LOG_MESSAGE("Failed to suspend system. System resumed again: %m"),
191 "SLEEP=%s", arg_verb
);
194 "MESSAGE_ID=" SD_MESSAGE_SLEEP_STOP_STR
,
195 LOG_MESSAGE("System resumed."),
196 "SLEEP=%s", arg_verb
);
198 arguments
[1] = (char*) "post";
199 (void) execute_directories(dirs
, DEFAULT_TIMEOUT_USEC
, NULL
, NULL
, arguments
, NULL
, EXEC_DIR_PARALLEL
| EXEC_DIR_IGNORE_ERRORS
);
204 static int execute_s2h(const SleepConfig
*sleep_config
) {
205 _cleanup_close_
int tfd
= -1;
206 char buf
[FORMAT_TIMESPAN_MAX
];
207 struct itimerspec ts
= {};
211 assert(sleep_config
);
213 tfd
= timerfd_create(CLOCK_BOOTTIME_ALARM
, TFD_NONBLOCK
|TFD_CLOEXEC
);
215 return log_error_errno(errno
, "Error creating timerfd: %m");
217 log_debug("Set timerfd wake alarm for %s",
218 format_timespan(buf
, sizeof(buf
), sleep_config
->hibernate_delay_sec
, USEC_PER_SEC
));
220 timespec_store(&ts
.it_value
, sleep_config
->hibernate_delay_sec
);
222 r
= timerfd_settime(tfd
, 0, &ts
, NULL
);
224 return log_error_errno(errno
, "Error setting hibernate timer: %m");
226 r
= execute(sleep_config
->suspend_modes
, sleep_config
->suspend_states
);
230 fds
= (struct pollfd
) {
234 r
= poll(&fds
, 1, 0);
236 return log_error_errno(errno
, "Error polling timerfd: %m");
238 tfd
= safe_close(tfd
);
240 if (!FLAGS_SET(fds
.revents
, POLLIN
)) /* We woke up before the alarm time, we are done. */
243 /* If woken up after alarm time, hibernate */
244 log_debug("Attempting to hibernate after waking from %s timer",
245 format_timespan(buf
, sizeof(buf
), sleep_config
->hibernate_delay_sec
, USEC_PER_SEC
));
247 r
= execute(sleep_config
->hibernate_modes
, sleep_config
->hibernate_states
);
249 log_notice("Couldn't hibernate, will try to suspend again.");
250 r
= execute(sleep_config
->suspend_modes
, sleep_config
->suspend_states
);
252 log_notice("Could neither hibernate nor suspend again, giving up.");
260 static int help(void) {
261 _cleanup_free_
char *link
= NULL
;
264 r
= terminal_urlify_man("systemd-suspend.service", "8", &link
);
268 printf("%s COMMAND\n\n"
269 "Suspend the system, hibernate the system, or both.\n\n"
270 " -h --help Show this help and exit\n"
271 " --version Print version string and exit\n"
273 " suspend Suspend the system\n"
274 " hibernate Hibernate the system\n"
275 " hybrid-sleep Both hibernate and suspend the system\n"
276 " suspend-then-hibernate Initially suspend and then hibernate\n"
277 " the system after a fixed period of time\n"
278 "\nSee the %s for details.\n"
279 , program_invocation_short_name
286 static int parse_argv(int argc
, char *argv
[]) {
291 static const struct option options
[] = {
292 { "help", no_argument
, NULL
, 'h' },
293 { "version", no_argument
, NULL
, ARG_VERSION
},
302 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
314 assert_not_reached("Unhandled option");
317 if (argc
- optind
!= 1)
318 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
320 program_invocation_short_name
);
322 arg_verb
= strdup(argv
[optind
]);
326 if (!STR_IN_SET(arg_verb
, "suspend", "hibernate", "hybrid-sleep", "suspend-then-hibernate"))
327 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
328 "Unknown command '%s'.", arg_verb
);
330 return 1 /* work to do */;
333 static int run(int argc
, char *argv
[]) {
335 char **modes
= NULL
, **states
= NULL
;
336 _cleanup_(free_sleep_configp
) SleepConfig
*sleep_config
= NULL
;
341 r
= parse_argv(argc
, argv
);
345 r
= parse_sleep_config(&sleep_config
);
349 r
= sleep_settings(arg_verb
, sleep_config
, &allow
, &modes
, &states
);
354 return log_error_errno(SYNTHETIC_ERRNO(EACCES
),
355 "Sleep mode \"%s\" is disabled by configuration, refusing.",
358 if (streq(arg_verb
, "suspend-then-hibernate"))
359 return execute_s2h(sleep_config
);
361 return execute(modes
, states
);
364 DEFINE_MAIN_FUNCTION(run
);