2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
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>
|
2013-05-04 18:31:28 +02:00
|
|
|
#include <getopt.h>
|
2018-03-08 09:41:50 +01:00
|
|
|
#include <linux/fiemap.h>
|
2015-09-23 03:01:06 +02:00
|
|
|
#include <stdio.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
|
|
|
|
|
|
|
#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"
|
2015-09-23 03:01:06 +02:00
|
|
|
#include "log.h"
|
2018-07-16 12:33:18 +02:00
|
|
|
#include "parse-util.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"
|
2018-08-09 10:32:31 +02:00
|
|
|
#include "terminal-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;
|
|
|
|
|
2018-03-08 09:41:50 +01:00
|
|
|
static int write_hibernate_location_info(void) {
|
|
|
|
_cleanup_free_ char *device = NULL, *type = NULL;
|
|
|
|
_cleanup_free_ struct fiemap *fiemap = NULL;
|
|
|
|
char offset_str[DECIMAL_STR_MAX(uint64_t)];
|
|
|
|
char device_str[DECIMAL_STR_MAX(uint64_t)];
|
|
|
|
_cleanup_close_ int fd = -1;
|
|
|
|
struct stat stb;
|
|
|
|
uint64_t offset;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
r = find_hibernate_location(&device, &type, NULL, NULL);
|
|
|
|
if (r < 0)
|
|
|
|
return log_debug_errno(r, "Unable to find hibernation location: %m");
|
|
|
|
|
|
|
|
/* if it's a swap partition, we just write the disk to /sys/power/resume */
|
2018-07-16 12:33:18 +02:00
|
|
|
if (streq(type, "partition")) {
|
|
|
|
r = write_string_file("/sys/power/resume", device, 0);
|
|
|
|
if (r < 0)
|
|
|
|
return log_debug_errno(r, "Faileed to write partitoin device to /sys/power/resume: %m");
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
if (!streq(type, "file")) {
|
|
|
|
log_debug("Invalid hibernate type: %s", type);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2018-03-08 09:41:50 +01:00
|
|
|
|
|
|
|
/* Only available in 4.17+ */
|
|
|
|
if (access("/sys/power/resume_offset", F_OK) < 0) {
|
|
|
|
if (errno == ENOENT)
|
|
|
|
return 0;
|
2018-07-16 12:33:18 +02:00
|
|
|
|
2018-03-08 09:41:50 +01:00
|
|
|
return log_debug_errno(errno, "/sys/power/resume_offset unavailable: %m");
|
|
|
|
}
|
|
|
|
|
2018-07-16 12:33:18 +02:00
|
|
|
if (access("/sys/power/resume_offset", W_OK) < 0)
|
2018-03-08 09:41:50 +01:00
|
|
|
return log_debug_errno(errno, "/sys/power/resume_offset not writeable: %m");
|
|
|
|
|
|
|
|
fd = open(device, O_RDONLY | O_CLOEXEC | O_NONBLOCK);
|
|
|
|
if (fd < 0)
|
|
|
|
return log_debug_errno(errno, "Unable to open '%s': %m", device);
|
|
|
|
r = fstat(fd, &stb);
|
|
|
|
if (r < 0)
|
|
|
|
return log_debug_errno(errno, "Unable to stat %s: %m", device);
|
2018-07-16 12:33:18 +02:00
|
|
|
|
2018-03-08 09:41:50 +01:00
|
|
|
r = read_fiemap(fd, &fiemap);
|
|
|
|
if (r < 0)
|
2018-07-16 12:33:18 +02:00
|
|
|
return log_debug_errno(r, "Unable to read extent map for '%s': %m", device);
|
2018-03-08 09:41:50 +01:00
|
|
|
if (fiemap->fm_mapped_extents == 0) {
|
|
|
|
log_debug("No extents found in '%s'", device);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2018-07-16 12:33:18 +02:00
|
|
|
|
2018-03-08 09:41:50 +01:00
|
|
|
offset = fiemap->fm_extents[0].fe_physical / page_size();
|
|
|
|
xsprintf(offset_str, "%" PRIu64, offset);
|
|
|
|
r = write_string_file("/sys/power/resume_offset", offset_str, 0);
|
|
|
|
if (r < 0)
|
2018-07-16 12:33:18 +02:00
|
|
|
return log_debug_errno(r, "Failed to write offset '%s': %m", offset_str);
|
2018-03-08 09:41:50 +01:00
|
|
|
|
|
|
|
xsprintf(device_str, "%lx", (unsigned long)stb.st_dev);
|
|
|
|
r = write_string_file("/sys/power/resume", device_str, 0);
|
|
|
|
if (r < 0)
|
2018-07-16 12:33:18 +02:00
|
|
|
return log_debug_errno(r, "Failed to write device '%s': %m", device_str);
|
|
|
|
|
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;
|
|
|
|
|
2015-07-07 01:19:25 +02:00
|
|
|
k = write_string_file("/sys/power/disk", *mode, 0);
|
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;
|
|
|
|
|
|
|
|
STRV_FOREACH(state, states) {
|
|
|
|
int k;
|
|
|
|
|
2017-09-22 20:55:34 +02:00
|
|
|
k = write_string_stream(*f, *state, 0);
|
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
|
|
|
|
2013-05-04 18:31:28 +02:00
|
|
|
static int execute(char **modes, char **states) {
|
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-05-04 18:31:28 +02:00
|
|
|
int r;
|
2013-12-15 22:25:04 +01:00
|
|
|
_cleanup_fclose_ FILE *f = NULL;
|
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
|
|
|
|
2013-05-04 18:31:28 +02:00
|
|
|
/* Configure the hibernation mode */
|
2018-03-08 09:41:50 +01:00
|
|
|
if (!strv_isempty(modes)) {
|
|
|
|
r = write_hibernate_location_info();
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to write hibernation disk offset: %m");
|
|
|
|
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
|
|
|
|
2018-09-12 19:19:13 +02:00
|
|
|
execute_directories(dirs, DEFAULT_TIMEOUT_USEC, NULL, NULL, arguments, NULL);
|
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-03-29 15:56:23 +02:00
|
|
|
return log_error_errno(r, "Failed to write /sys/power/state: %m");
|
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_STOP_STR,
|
2014-12-09 03:58:20 +01:00
|
|
|
LOG_MESSAGE("System resumed."),
|
2018-06-04 12:59:22 +02:00
|
|
|
"SLEEP=%s", arg_verb);
|
2012-05-30 15:04:39 +02:00
|
|
|
|
2012-05-05 02:06:58 +02:00
|
|
|
arguments[1] = (char*) "post";
|
2018-09-12 19:19:13 +02:00
|
|
|
execute_directories(dirs, DEFAULT_TIMEOUT_USEC, NULL, NULL, arguments, NULL);
|
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
|
|
|
|
2018-03-08 14:17:33 +01:00
|
|
|
static int read_wakealarm(uint64_t *result) {
|
|
|
|
_cleanup_free_ char *t = NULL;
|
|
|
|
|
|
|
|
if (read_one_line_file("/sys/class/rtc/rtc0/since_epoch", &t) >= 0)
|
|
|
|
return safe_atou64(t, result);
|
|
|
|
return -EBADF;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int write_wakealarm(const char *str) {
|
|
|
|
|
|
|
|
_cleanup_fclose_ FILE *f = NULL;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
f = fopen("/sys/class/rtc/rtc0/wakealarm", "we");
|
|
|
|
if (!f)
|
|
|
|
return log_error_errno(errno, "Failed to open /sys/class/rtc/rtc0/wakealarm: %m");
|
|
|
|
|
|
|
|
r = write_string_stream(f, str, 0);
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to write '%s' to /sys/class/rtc/rtc0/wakealarm: %m", str);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int execute_s2h(usec_t hibernate_delay_sec) {
|
|
|
|
|
|
|
|
_cleanup_strv_free_ char **hibernate_modes = NULL, **hibernate_states = NULL,
|
|
|
|
**suspend_modes = NULL, **suspend_states = NULL;
|
|
|
|
usec_t orig_time, cmp_time;
|
|
|
|
char time_str[DECIMAL_STR_MAX(uint64_t)];
|
|
|
|
int r;
|
|
|
|
|
2018-09-26 11:17:36 +02:00
|
|
|
r = parse_sleep_config("suspend", NULL, &suspend_modes, &suspend_states, NULL);
|
2018-03-08 14:17:33 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2018-09-26 11:17:36 +02:00
|
|
|
r = parse_sleep_config("hibernate", NULL, &hibernate_modes, &hibernate_states, NULL);
|
2018-03-08 14:17:33 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = read_wakealarm(&orig_time);
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(errno, "Failed to read time: %d", r);
|
|
|
|
|
|
|
|
orig_time += hibernate_delay_sec / USEC_PER_SEC;
|
|
|
|
xsprintf(time_str, "%" PRIu64, orig_time);
|
|
|
|
|
|
|
|
r = write_wakealarm(time_str);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
log_debug("Set RTC wake alarm for %s", time_str);
|
|
|
|
|
|
|
|
r = execute(suspend_modes, suspend_states);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = read_wakealarm(&cmp_time);
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(errno, "Failed to read time: %d", r);
|
|
|
|
|
|
|
|
/* reset RTC */
|
|
|
|
r = write_wakealarm("0");
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
log_debug("Woke up at %"PRIu64, cmp_time);
|
|
|
|
|
|
|
|
/* if woken up after alarm time, hibernate */
|
|
|
|
if (cmp_time >= orig_time)
|
|
|
|
r = execute(hibernate_modes, hibernate_states);
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
if (argc - optind != 1) {
|
|
|
|
log_error("Usage: %s COMMAND",
|
|
|
|
program_invocation_short_name);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
arg_verb = argv[optind];
|
2012-05-05 02:06:58 +02:00
|
|
|
|
2018-07-16 12:33:18 +02:00
|
|
|
if (!STR_IN_SET(arg_verb, "suspend", "hibernate", "hybrid-sleep", "suspend-then-hibernate")) {
|
2013-05-04 18:31:28 +02:00
|
|
|
log_error("Unknown command '%s'.", arg_verb);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1 /* work to do */;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
2018-09-26 11:17:36 +02:00
|
|
|
bool allow;
|
2013-05-04 18:31:28 +02:00
|
|
|
_cleanup_strv_free_ char **modes = NULL, **states = NULL;
|
2018-03-08 14:17:33 +01:00
|
|
|
usec_t delay = 0;
|
2013-05-04 18:31:28 +02:00
|
|
|
int r;
|
|
|
|
|
|
|
|
log_set_target(LOG_TARGET_AUTO);
|
|
|
|
log_parse_environment();
|
|
|
|
log_open();
|
|
|
|
|
|
|
|
r = parse_argv(argc, argv);
|
|
|
|
if (r <= 0)
|
|
|
|
goto finish;
|
|
|
|
|
2018-09-26 11:17:36 +02:00
|
|
|
r = parse_sleep_config(arg_verb, &allow, &modes, &states, &delay);
|
2013-05-04 18:31:28 +02:00
|
|
|
if (r < 0)
|
|
|
|
goto finish;
|
|
|
|
|
2018-09-26 11:17:36 +02:00
|
|
|
if (!allow) {
|
|
|
|
log_error("Sleep mode \"%s\" is disabled by configuration, refusing.", arg_verb);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2018-03-28 18:00:06 +02:00
|
|
|
if (streq(arg_verb, "suspend-then-hibernate"))
|
2018-03-08 14:17:33 +01:00
|
|
|
r = execute_s2h(delay);
|
|
|
|
else
|
|
|
|
r = execute(modes, states);
|
2018-07-16 12:33:18 +02:00
|
|
|
|
2013-05-04 18:31:28 +02:00
|
|
|
finish:
|
|
|
|
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
|
2012-05-05 02:06:58 +02:00
|
|
|
}
|