2020-11-09 05:23:58 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
2012-05-05 02:06:58 +02:00
|
|
|
/***
|
2018-06-12 19:00:24 +02:00
|
|
|
Copyright © 2010-2017 Canonical
|
|
|
|
Copyright © 2018 Dell Inc.
|
2012-05-05 02:06:58 +02:00
|
|
|
***/
|
|
|
|
|
|
|
|
#include <errno.h>
|
2019-03-27 11:32:41 +01:00
|
|
|
#include <fcntl.h>
|
2013-05-04 18:31:28 +02:00
|
|
|
#include <getopt.h>
|
2018-03-08 09:41:50 +01:00
|
|
|
#include <linux/fiemap.h>
|
2019-05-16 18:12:41 +02:00
|
|
|
#include <poll.h>
|
2019-03-27 11:32:41 +01:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2019-05-16 18:12:41 +02:00
|
|
|
#include <sys/timerfd.h>
|
2019-03-27 11:32:41 +01:00
|
|
|
#include <unistd.h>
|
2012-05-05 02:06:58 +02:00
|
|
|
|
2014-03-06 02:19:06 +01:00
|
|
|
#include "sd-messages.h"
|
2015-09-23 03:01:06 +02:00
|
|
|
|
2019-06-26 15:55:37 +02:00
|
|
|
#include "btrfs-util.h"
|
2019-08-19 18:56:11 +02:00
|
|
|
#include "bus-error.h"
|
2015-09-23 03:01:06 +02:00
|
|
|
#include "def.h"
|
2017-01-22 18:35:08 +01:00
|
|
|
#include "exec-util.h"
|
2015-10-25 13:14:12 +01:00
|
|
|
#include "fd-util.h"
|
2013-02-14 12:26:13 +01:00
|
|
|
#include "fileio.h"
|
2019-08-19 18:56:11 +02:00
|
|
|
#include "format-util.h"
|
2020-06-10 11:43:40 +02:00
|
|
|
#include "io-util.h"
|
2015-09-23 03:01:06 +02:00
|
|
|
#include "log.h"
|
2018-11-19 20:26:37 +01:00
|
|
|
#include "main-func.h"
|
2018-07-16 12:33:18 +02:00
|
|
|
#include "parse-util.h"
|
2018-11-20 15:42:57 +01:00
|
|
|
#include "pretty-print.h"
|
2013-05-04 18:31:28 +02:00
|
|
|
#include "sleep-config.h"
|
2018-03-08 14:17:33 +01:00
|
|
|
#include "stdio-util.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "string-util.h"
|
2015-09-23 03:01:06 +02:00
|
|
|
#include "strv.h"
|
2019-05-16 18:12:41 +02:00
|
|
|
#include "time-util.h"
|
2015-09-23 03:01:06 +02:00
|
|
|
#include "util.h"
|
2013-05-04 18:31:28 +02:00
|
|
|
|
|
|
|
static char* arg_verb = NULL;
|
|
|
|
|
2019-06-17 11:31:06 +02:00
|
|
|
STATIC_DESTRUCTOR_REGISTER(arg_verb, freep);
|
|
|
|
|
2019-09-27 06:02:28 +02:00
|
|
|
static int write_hibernate_location_info(const HibernateLocation *hibernate_location) {
|
2018-03-08 09:41:50 +01:00
|
|
|
char offset_str[DECIMAL_STR_MAX(uint64_t)];
|
2019-12-07 01:37:22 +01:00
|
|
|
char resume_str[DECIMAL_STR_MAX(unsigned) * 2 + STRLEN(":")];
|
2018-03-08 09:41:50 +01:00
|
|
|
int r;
|
|
|
|
|
2019-09-27 06:02:28 +02:00
|
|
|
assert(hibernate_location);
|
|
|
|
assert(hibernate_location->swap);
|
|
|
|
|
2019-12-07 01:37:22 +01:00
|
|
|
xsprintf(resume_str, "%u:%u", major(hibernate_location->devno), minor(hibernate_location->devno));
|
|
|
|
r = write_string_file("/sys/power/resume", resume_str, WRITE_STRING_FILE_DISABLE_BUFFER);
|
2018-03-08 09:41:50 +01:00
|
|
|
if (r < 0)
|
2019-09-27 06:02:28 +02:00
|
|
|
return log_debug_errno(r, "Failed to write partition device to /sys/power/resume for '%s': '%s': %m",
|
2019-12-07 01:37:22 +01:00
|
|
|
hibernate_location->swap->device, resume_str);
|
2018-03-08 09:41:50 +01:00
|
|
|
|
2019-12-07 01:37:22 +01:00
|
|
|
log_debug("Wrote resume= value for %s to /sys/power/resume: %s", hibernate_location->swap->device, resume_str);
|
2018-07-16 12:33:18 +02:00
|
|
|
|
2019-09-27 06:02:28 +02:00
|
|
|
/* if it's a swap partition, we're done */
|
|
|
|
if (streq(hibernate_location->swap->type, "partition"))
|
2018-07-16 12:33:18 +02:00
|
|
|
return r;
|
2019-09-27 06:02:28 +02:00
|
|
|
|
|
|
|
if (!streq(hibernate_location->swap->type, "file"))
|
2018-11-20 23:40:44 +01:00
|
|
|
return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
|
2019-09-27 06:02:28 +02:00
|
|
|
"Invalid hibernate type: %s", hibernate_location->swap->type);
|
2018-03-08 09:41:50 +01:00
|
|
|
|
|
|
|
/* Only available in 4.17+ */
|
2019-12-07 01:37:22 +01:00
|
|
|
if (hibernate_location->offset > 0 && access("/sys/power/resume_offset", W_OK) < 0) {
|
2018-10-24 12:57:37 +02:00
|
|
|
if (errno == ENOENT) {
|
2019-09-27 06:02:28 +02:00
|
|
|
log_debug("Kernel too old, can't configure resume_offset for %s, ignoring: %" PRIu64,
|
2019-12-07 01:37:22 +01:00
|
|
|
hibernate_location->swap->device, hibernate_location->offset);
|
2018-03-08 09:41:50 +01:00
|
|
|
return 0;
|
2018-10-24 12:57:37 +02:00
|
|
|
}
|
2018-07-16 12:33:18 +02:00
|
|
|
|
2020-07-07 10:02:02 +02:00
|
|
|
return log_debug_errno(errno, "/sys/power/resume_offset not writable: %m");
|
2018-10-24 12:57:37 +02:00
|
|
|
}
|
2018-03-08 09:41:50 +01:00
|
|
|
|
2019-12-07 01:37:22 +01:00
|
|
|
xsprintf(offset_str, "%" PRIu64, hibernate_location->offset);
|
2018-11-06 13:00:07 +01:00
|
|
|
r = write_string_file("/sys/power/resume_offset", offset_str, WRITE_STRING_FILE_DISABLE_BUFFER);
|
2018-03-08 09:41:50 +01:00
|
|
|
if (r < 0)
|
2019-09-27 06:02:28 +02:00
|
|
|
return log_debug_errno(r, "Failed to write swap file offset to /sys/power/resume_offset for '%s': '%s': %m",
|
|
|
|
hibernate_location->swap->device, offset_str);
|
2018-03-08 09:41:50 +01:00
|
|
|
|
2019-09-27 06:02:28 +02:00
|
|
|
log_debug("Wrote resume_offset= value for %s to /sys/power/resume_offset: %s", hibernate_location->swap->device, offset_str);
|
2019-06-09 05:44:34 +02:00
|
|
|
|
2018-03-08 09:41:50 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-05-04 18:31:28 +02:00
|
|
|
static int write_mode(char **modes) {
|
|
|
|
int r = 0;
|
|
|
|
char **mode;
|
|
|
|
|
|
|
|
STRV_FOREACH(mode, modes) {
|
2014-03-06 02:19:06 +01:00
|
|
|
int k;
|
|
|
|
|
2018-11-06 13:00:07 +01:00
|
|
|
k = write_string_file("/sys/power/disk", *mode, WRITE_STRING_FILE_DISABLE_BUFFER);
|
2018-07-16 12:33:18 +02:00
|
|
|
if (k >= 0)
|
2013-05-04 18:31:28 +02:00
|
|
|
return 0;
|
2014-03-06 02:19:06 +01:00
|
|
|
|
2018-07-16 12:33:18 +02:00
|
|
|
log_debug_errno(k, "Failed to write '%s' to /sys/power/disk: %m", *mode);
|
|
|
|
if (r >= 0)
|
2013-05-04 18:31:28 +02:00
|
|
|
r = k;
|
|
|
|
}
|
2012-05-05 02:06:58 +02:00
|
|
|
|
2013-05-04 18:31:28 +02:00
|
|
|
return r;
|
|
|
|
}
|
2012-05-05 02:06:58 +02:00
|
|
|
|
2013-12-15 22:25:04 +01:00
|
|
|
static int write_state(FILE **f, char **states) {
|
2013-05-04 18:31:28 +02:00
|
|
|
char **state;
|
|
|
|
int r = 0;
|
|
|
|
|
2020-06-10 16:54:10 +02:00
|
|
|
assert(f);
|
|
|
|
assert(*f);
|
|
|
|
|
2013-05-04 18:31:28 +02:00
|
|
|
STRV_FOREACH(state, states) {
|
|
|
|
int k;
|
|
|
|
|
2018-11-06 13:00:07 +01:00
|
|
|
k = write_string_stream(*f, *state, WRITE_STRING_FILE_DISABLE_BUFFER);
|
2018-07-16 12:33:18 +02:00
|
|
|
if (k >= 0)
|
2013-05-04 18:31:28 +02:00
|
|
|
return 0;
|
2018-07-16 12:33:18 +02:00
|
|
|
log_debug_errno(k, "Failed to write '%s' to /sys/power/state: %m", *state);
|
|
|
|
if (r >= 0)
|
2013-05-04 18:31:28 +02:00
|
|
|
r = k;
|
|
|
|
|
2013-12-15 22:25:04 +01:00
|
|
|
fclose(*f);
|
|
|
|
*f = fopen("/sys/power/state", "we");
|
2014-11-28 19:57:32 +01:00
|
|
|
if (!*f)
|
2018-03-29 15:56:23 +02:00
|
|
|
return -errno;
|
2012-05-05 02:06:58 +02:00
|
|
|
}
|
|
|
|
|
2013-05-04 18:31:28 +02:00
|
|
|
return r;
|
|
|
|
}
|
2012-05-05 02:06:58 +02:00
|
|
|
|
2019-08-19 18:56:11 +02:00
|
|
|
static int lock_all_homes(void) {
|
|
|
|
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
|
|
|
|
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
|
|
|
|
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
/* Let's synchronously lock all home directories managed by homed that have been marked for it. This
|
|
|
|
* way the key material required to access these volumes is hopefully removed from memory. */
|
|
|
|
|
|
|
|
r = sd_bus_open_system(&bus);
|
|
|
|
if (r < 0)
|
|
|
|
return log_warning_errno(r, "Failed to connect to system bus, ignoring: %m");
|
|
|
|
|
|
|
|
r = sd_bus_message_new_method_call(
|
|
|
|
bus,
|
|
|
|
&m,
|
|
|
|
"org.freedesktop.home1",
|
|
|
|
"/org/freedesktop/home1",
|
|
|
|
"org.freedesktop.home1.Manager",
|
|
|
|
"LockAllHomes");
|
|
|
|
if (r < 0)
|
|
|
|
return bus_log_create_error(r);
|
|
|
|
|
|
|
|
/* If homed is not running it can't have any home directories active either. */
|
|
|
|
r = sd_bus_message_set_auto_start(m, false);
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to disable auto-start of LockAllHomes() message: %m");
|
|
|
|
|
|
|
|
r = sd_bus_call(bus, m, DEFAULT_TIMEOUT_USEC, &error, NULL);
|
|
|
|
if (r < 0) {
|
2020-09-08 13:19:53 +02:00
|
|
|
if (!bus_error_is_unknown_service(&error))
|
|
|
|
return log_error_errno(r, "Failed to lock home directories: %s", bus_error_message(&error, r));
|
2019-08-19 18:56:11 +02:00
|
|
|
|
2020-09-08 13:19:53 +02:00
|
|
|
return log_debug("systemd-homed is not running, locking of home directories skipped.");
|
2019-08-19 18:56:11 +02:00
|
|
|
}
|
|
|
|
|
2020-09-08 13:19:53 +02:00
|
|
|
return log_debug("Successfully requested locking of all home directories.");
|
2019-08-19 18:56:11 +02:00
|
|
|
}
|
|
|
|
|
2021-01-08 03:06:30 +01:00
|
|
|
static int execute(char **modes, char **states, const char *action) {
|
2014-11-28 02:05:14 +01:00
|
|
|
char *arguments[] = {
|
|
|
|
NULL,
|
|
|
|
(char*) "pre",
|
|
|
|
arg_verb,
|
|
|
|
NULL
|
|
|
|
};
|
2017-09-12 16:57:33 +02:00
|
|
|
static const char* const dirs[] = {
|
|
|
|
SYSTEM_SLEEP_PATH,
|
|
|
|
NULL
|
|
|
|
};
|
2014-11-28 02:05:14 +01:00
|
|
|
|
2013-12-15 22:25:04 +01:00
|
|
|
_cleanup_fclose_ FILE *f = NULL;
|
2019-09-27 06:02:28 +02:00
|
|
|
_cleanup_(hibernate_location_freep) HibernateLocation *hibernate_location = NULL;
|
|
|
|
int r;
|
2012-10-28 00:49:04 +02:00
|
|
|
|
2013-05-04 18:31:28 +02:00
|
|
|
/* This file is opened first, so that if we hit an error,
|
2014-01-07 17:51:41 +01:00
|
|
|
* we can abort before modifying any state. */
|
2012-05-05 02:06:58 +02:00
|
|
|
f = fopen("/sys/power/state", "we");
|
2014-11-28 19:57:32 +01:00
|
|
|
if (!f)
|
|
|
|
return log_error_errno(errno, "Failed to open /sys/power/state: %m");
|
2012-05-05 02:06:58 +02:00
|
|
|
|
2018-11-06 13:00:07 +01:00
|
|
|
setvbuf(f, NULL, _IONBF, 0);
|
|
|
|
|
2020-06-10 16:47:38 +02:00
|
|
|
/* Configure hibernation settings if we are supposed to hibernate */
|
2018-03-08 09:41:50 +01:00
|
|
|
if (!strv_isempty(modes)) {
|
2019-09-27 06:02:28 +02:00
|
|
|
r = find_hibernate_location(&hibernate_location);
|
2018-03-08 09:41:50 +01:00
|
|
|
if (r < 0)
|
2020-06-10 16:47:38 +02:00
|
|
|
return log_error_errno(r, "Failed to find location to hibernate to: %m");
|
|
|
|
if (r == 0) { /* 0 means: no hibernation location was configured in the kernel so far, let's
|
|
|
|
* do it ourselves then. > 0 means: kernel already had a configured hibernation
|
|
|
|
* location which we shouldn't touch. */
|
2019-09-27 06:02:28 +02:00
|
|
|
r = write_hibernate_location_info(hibernate_location);
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to prepare for hibernation: %m");
|
|
|
|
}
|
2019-06-09 05:44:34 +02:00
|
|
|
|
2018-03-08 09:41:50 +01:00
|
|
|
r = write_mode(modes);
|
|
|
|
if (r < 0)
|
2018-03-29 15:56:23 +02:00
|
|
|
return log_error_errno(r, "Failed to write mode to /sys/power/disk: %m");;
|
2018-03-08 09:41:50 +01:00
|
|
|
}
|
2013-05-04 18:31:28 +02:00
|
|
|
|
2021-01-08 03:06:30 +01:00
|
|
|
r = setenv("SYSTEMD_SLEEP_ACTION", action, 1);
|
|
|
|
if (r != 0)
|
|
|
|
log_warning_errno(errno, "Error setting SYSTEMD_SLEEP_ACTION=%s: %m", action);
|
|
|
|
|
2019-03-26 09:59:11 +01:00
|
|
|
(void) execute_directories(dirs, DEFAULT_TIMEOUT_USEC, NULL, NULL, arguments, NULL, EXEC_DIR_PARALLEL | EXEC_DIR_IGNORE_ERRORS);
|
2019-08-19 18:56:11 +02:00
|
|
|
(void) lock_all_homes();
|
2012-05-05 02:06:58 +02:00
|
|
|
|
2013-05-04 18:31:28 +02:00
|
|
|
log_struct(LOG_INFO,
|
tree-wide: add SD_ID128_MAKE_STR, remove LOG_MESSAGE_ID
Embedding sd_id128_t's in constant strings was rather cumbersome. We had
SD_ID128_CONST_STR which returned a const char[], but it had two problems:
- it wasn't possible to statically concatanate this array with a normal string
- gcc wasn't really able to optimize this, and generated code to perform the
"conversion" at runtime.
Because of this, even our own code in coredumpctl wasn't using
SD_ID128_CONST_STR.
Add a new macro to generate a constant string: SD_ID128_MAKE_STR.
It is not as elegant as SD_ID128_CONST_STR, because it requires a repetition
of the numbers, but in practice it is more convenient to use, and allows gcc
to generate smarter code:
$ size .libs/systemd{,-logind,-journald}{.old,}
text data bss dec hex filename
1265204 149564 4808 1419576 15a938 .libs/systemd.old
1260268 149564 4808 1414640 1595f0 .libs/systemd
246805 13852 209 260866 3fb02 .libs/systemd-logind.old
240973 13852 209 255034 3e43a .libs/systemd-logind
146839 4984 34 151857 25131 .libs/systemd-journald.old
146391 4984 34 151409 24f71 .libs/systemd-journald
It is also much easier to check if a certain binary uses a certain MESSAGE_ID:
$ strings .libs/systemd.old|grep MESSAGE_ID
MESSAGE_ID=%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x
MESSAGE_ID=%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x
MESSAGE_ID=%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x
MESSAGE_ID=%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x
$ strings .libs/systemd|grep MESSAGE_ID
MESSAGE_ID=c7a787079b354eaaa9e77b371893cd27
MESSAGE_ID=b07a249cd024414a82dd00cd181378ff
MESSAGE_ID=641257651c1b4ec9a8624d7a40a9e1e7
MESSAGE_ID=de5b426a63be47a7b6ac3eaac82e2f6f
MESSAGE_ID=d34d037fff1847e6ae669a370e694725
MESSAGE_ID=7d4958e842da4a758f6c1cdc7b36dcc5
MESSAGE_ID=1dee0369c7fc4736b7099b38ecb46ee7
MESSAGE_ID=39f53479d3a045ac8e11786248231fbf
MESSAGE_ID=be02cf6855d2428ba40df7e9d022f03d
MESSAGE_ID=7b05ebc668384222baa8881179cfda54
MESSAGE_ID=9d1aaa27d60140bd96365438aad20286
2016-11-06 18:48:23 +01:00
|
|
|
"MESSAGE_ID=" SD_MESSAGE_SLEEP_START_STR,
|
2014-11-28 02:05:14 +01:00
|
|
|
LOG_MESSAGE("Suspending system..."),
|
2018-06-04 12:59:22 +02:00
|
|
|
"SLEEP=%s", arg_verb);
|
2013-05-04 18:31:28 +02:00
|
|
|
|
2013-12-15 22:25:04 +01:00
|
|
|
r = write_state(&f, states);
|
2013-05-04 18:31:28 +02:00
|
|
|
if (r < 0)
|
2018-10-24 13:04:30 +02:00
|
|
|
log_struct_errno(LOG_ERR, r,
|
|
|
|
"MESSAGE_ID=" SD_MESSAGE_SLEEP_STOP_STR,
|
|
|
|
LOG_MESSAGE("Failed to suspend system. System resumed again: %m"),
|
|
|
|
"SLEEP=%s", arg_verb);
|
|
|
|
else
|
|
|
|
log_struct(LOG_INFO,
|
|
|
|
"MESSAGE_ID=" SD_MESSAGE_SLEEP_STOP_STR,
|
|
|
|
LOG_MESSAGE("System resumed."),
|
|
|
|
"SLEEP=%s", arg_verb);
|
2012-05-30 15:04:39 +02:00
|
|
|
|
2012-05-05 02:06:58 +02:00
|
|
|
arguments[1] = (char*) "post";
|
2019-03-26 09:59:11 +01:00
|
|
|
(void) execute_directories(dirs, DEFAULT_TIMEOUT_USEC, NULL, NULL, arguments, NULL, EXEC_DIR_PARALLEL | EXEC_DIR_IGNORE_ERRORS);
|
2012-05-05 02:06:58 +02:00
|
|
|
|
2013-05-04 18:31:28 +02:00
|
|
|
return r;
|
|
|
|
}
|
2012-05-05 02:06:58 +02:00
|
|
|
|
2019-05-20 07:43:29 +02:00
|
|
|
static int execute_s2h(const SleepConfig *sleep_config) {
|
2019-05-16 18:12:41 +02:00
|
|
|
_cleanup_close_ int tfd = -1;
|
|
|
|
char buf[FORMAT_TIMESPAN_MAX];
|
|
|
|
struct itimerspec ts = {};
|
2018-03-08 14:17:33 +01:00
|
|
|
int r;
|
|
|
|
|
2019-05-20 07:43:29 +02:00
|
|
|
assert(sleep_config);
|
2018-03-08 14:17:33 +01:00
|
|
|
|
2019-05-16 18:12:41 +02:00
|
|
|
tfd = timerfd_create(CLOCK_BOOTTIME_ALARM, TFD_NONBLOCK|TFD_CLOEXEC);
|
|
|
|
if (tfd < 0)
|
|
|
|
return log_error_errno(errno, "Error creating timerfd: %m");
|
2018-03-08 14:17:33 +01:00
|
|
|
|
2019-05-16 18:12:41 +02:00
|
|
|
log_debug("Set timerfd wake alarm for %s",
|
2019-05-20 07:43:29 +02:00
|
|
|
format_timespan(buf, sizeof(buf), sleep_config->hibernate_delay_sec, USEC_PER_SEC));
|
2018-03-08 14:17:33 +01:00
|
|
|
|
2019-05-20 07:43:29 +02:00
|
|
|
timespec_store(&ts.it_value, sleep_config->hibernate_delay_sec);
|
2018-03-08 14:17:33 +01:00
|
|
|
|
2019-05-16 18:12:41 +02:00
|
|
|
r = timerfd_settime(tfd, 0, &ts, NULL);
|
2018-03-08 14:17:33 +01:00
|
|
|
if (r < 0)
|
2019-05-16 18:12:41 +02:00
|
|
|
return log_error_errno(errno, "Error setting hibernate timer: %m");
|
2018-03-08 14:17:33 +01:00
|
|
|
|
2021-01-08 03:06:30 +01:00
|
|
|
r = execute(sleep_config->suspend_modes, sleep_config->suspend_states, "suspend");
|
2018-03-08 14:17:33 +01:00
|
|
|
if (r < 0)
|
2018-10-24 12:47:46 +02:00
|
|
|
return r;
|
2018-03-08 14:17:33 +01:00
|
|
|
|
2020-06-10 11:43:40 +02:00
|
|
|
r = fd_wait_for_event(tfd, POLLIN, 0);
|
2018-03-08 14:17:33 +01:00
|
|
|
if (r < 0)
|
2020-06-10 11:43:40 +02:00
|
|
|
return log_error_errno(r, "Error polling timerfd: %m");
|
|
|
|
if (!FLAGS_SET(r, POLLIN)) /* We woke up before the alarm time, we are done. */
|
|
|
|
return 0;
|
2018-03-08 14:17:33 +01:00
|
|
|
|
2019-05-16 18:12:41 +02:00
|
|
|
tfd = safe_close(tfd);
|
2018-03-08 14:17:33 +01:00
|
|
|
|
2018-10-24 12:52:11 +02:00
|
|
|
/* If woken up after alarm time, hibernate */
|
2019-05-16 18:12:41 +02:00
|
|
|
log_debug("Attempting to hibernate after waking from %s timer",
|
2019-05-20 07:43:29 +02:00
|
|
|
format_timespan(buf, sizeof(buf), sleep_config->hibernate_delay_sec, USEC_PER_SEC));
|
|
|
|
|
2021-01-08 03:06:30 +01:00
|
|
|
r = execute(sleep_config->hibernate_modes, sleep_config->hibernate_states, "hibernate");
|
2018-10-24 13:15:24 +02:00
|
|
|
if (r < 0) {
|
2020-06-10 16:54:10 +02:00
|
|
|
log_notice_errno(r, "Couldn't hibernate, will try to suspend again: %m");
|
2020-06-10 11:43:40 +02:00
|
|
|
|
2021-01-08 03:06:30 +01:00
|
|
|
r = execute(sleep_config->suspend_modes, sleep_config->suspend_states, "suspend-after-failed-hibernate");
|
2020-06-10 11:43:40 +02:00
|
|
|
if (r < 0)
|
2020-06-10 16:54:10 +02:00
|
|
|
return log_error_errno(r, "Could neither hibernate nor suspend, giving up: %m");
|
2018-10-24 13:15:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2018-03-08 14:17:33 +01:00
|
|
|
}
|
|
|
|
|
2018-08-09 10:32:31 +02:00
|
|
|
static int help(void) {
|
|
|
|
_cleanup_free_ char *link = NULL;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
r = terminal_urlify_man("systemd-suspend.service", "8", &link);
|
|
|
|
if (r < 0)
|
|
|
|
return log_oom();
|
|
|
|
|
2013-05-04 18:31:28 +02:00
|
|
|
printf("%s COMMAND\n\n"
|
|
|
|
"Suspend the system, hibernate the system, or both.\n\n"
|
2018-08-09 10:32:31 +02:00
|
|
|
" -h --help Show this help and exit\n"
|
|
|
|
" --version Print version string and exit\n"
|
|
|
|
"\nCommands:\n"
|
|
|
|
" suspend Suspend the system\n"
|
|
|
|
" hibernate Hibernate the system\n"
|
|
|
|
" hybrid-sleep Both hibernate and suspend the system\n"
|
2018-03-28 18:00:06 +02:00
|
|
|
" suspend-then-hibernate Initially suspend and then hibernate\n"
|
2018-08-09 10:32:31 +02:00
|
|
|
" the system after a fixed period of time\n"
|
|
|
|
"\nSee the %s for details.\n"
|
|
|
|
, program_invocation_short_name
|
|
|
|
, link
|
|
|
|
);
|
|
|
|
|
|
|
|
return 0;
|
2013-05-04 18:31:28 +02:00
|
|
|
}
|
2012-05-05 02:06:58 +02:00
|
|
|
|
2013-05-04 18:31:28 +02:00
|
|
|
static int parse_argv(int argc, char *argv[]) {
|
|
|
|
enum {
|
|
|
|
ARG_VERSION = 0x100,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct option options[] = {
|
|
|
|
{ "help", no_argument, NULL, 'h' },
|
|
|
|
{ "version", no_argument, NULL, ARG_VERSION },
|
2013-11-06 18:28:39 +01:00
|
|
|
{}
|
2013-05-04 18:31:28 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
int c;
|
|
|
|
|
|
|
|
assert(argc >= 0);
|
|
|
|
assert(argv);
|
|
|
|
|
2014-08-02 17:12:21 +02:00
|
|
|
while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
|
2013-05-04 18:31:28 +02:00
|
|
|
switch(c) {
|
|
|
|
case 'h':
|
2018-08-09 10:32:31 +02:00
|
|
|
return help();
|
2013-05-04 18:31:28 +02:00
|
|
|
|
|
|
|
case ARG_VERSION:
|
2015-09-23 03:01:06 +02:00
|
|
|
return version();
|
2013-05-04 18:31:28 +02:00
|
|
|
|
|
|
|
case '?':
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
default:
|
2013-11-06 18:28:39 +01:00
|
|
|
assert_not_reached("Unhandled option");
|
2013-05-04 18:31:28 +02:00
|
|
|
}
|
|
|
|
|
2018-11-20 23:40:44 +01:00
|
|
|
if (argc - optind != 1)
|
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
|
|
|
"Usage: %s COMMAND",
|
|
|
|
program_invocation_short_name);
|
2013-05-04 18:31:28 +02:00
|
|
|
|
2019-06-17 11:31:06 +02:00
|
|
|
arg_verb = strdup(argv[optind]);
|
|
|
|
if (!arg_verb)
|
|
|
|
return log_oom();
|
2012-05-05 02:06:58 +02:00
|
|
|
|
2018-11-20 23:40:44 +01:00
|
|
|
if (!STR_IN_SET(arg_verb, "suspend", "hibernate", "hybrid-sleep", "suspend-then-hibernate"))
|
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
|
|
|
"Unknown command '%s'.", arg_verb);
|
2013-05-04 18:31:28 +02:00
|
|
|
|
|
|
|
return 1 /* work to do */;
|
|
|
|
}
|
|
|
|
|
2018-11-16 15:15:22 +01:00
|
|
|
static int run(int argc, char *argv[]) {
|
2018-09-26 11:17:36 +02:00
|
|
|
bool allow;
|
2019-05-20 07:43:29 +02:00
|
|
|
char **modes = NULL, **states = NULL;
|
|
|
|
_cleanup_(free_sleep_configp) SleepConfig *sleep_config = NULL;
|
2013-05-04 18:31:28 +02:00
|
|
|
int r;
|
|
|
|
|
2018-11-20 11:18:22 +01:00
|
|
|
log_setup_service();
|
2013-05-04 18:31:28 +02:00
|
|
|
|
|
|
|
r = parse_argv(argc, argv);
|
|
|
|
if (r <= 0)
|
2018-11-16 15:15:22 +01:00
|
|
|
return r;
|
2013-05-04 18:31:28 +02:00
|
|
|
|
2019-05-20 07:43:29 +02:00
|
|
|
r = parse_sleep_config(&sleep_config);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = sleep_settings(arg_verb, sleep_config, &allow, &modes, &states);
|
2013-05-04 18:31:28 +02:00
|
|
|
if (r < 0)
|
2018-11-16 15:15:22 +01:00
|
|
|
return r;
|
2013-05-04 18:31:28 +02:00
|
|
|
|
2018-11-20 23:40:44 +01:00
|
|
|
if (!allow)
|
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(EACCES),
|
|
|
|
"Sleep mode \"%s\" is disabled by configuration, refusing.",
|
|
|
|
arg_verb);
|
2018-09-26 11:17:36 +02:00
|
|
|
|
2018-03-28 18:00:06 +02:00
|
|
|
if (streq(arg_verb, "suspend-then-hibernate"))
|
2019-05-20 07:43:29 +02:00
|
|
|
return execute_s2h(sleep_config);
|
2018-03-08 14:17:33 +01:00
|
|
|
else
|
2021-01-08 03:06:30 +01:00
|
|
|
return execute(modes, states, arg_verb);
|
2012-05-05 02:06:58 +02:00
|
|
|
}
|
2018-11-16 15:15:22 +01:00
|
|
|
|
|
|
|
DEFINE_MAIN_FUNCTION(run);
|