2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2012-10-28 00:49:04 +02:00
|
|
|
|
2018-06-26 18:43:49 +02:00
|
|
|
#include <inttypes.h>
|
2018-03-08 09:41:50 +01:00
|
|
|
#include <linux/fiemap.h>
|
2012-10-28 00:49:04 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2018-03-08 09:41:50 +01:00
|
|
|
#include "fd-util.h"
|
2012-10-28 00:49:04 +02:00
|
|
|
#include "log.h"
|
2013-05-04 18:31:28 +02:00
|
|
|
#include "sleep-config.h"
|
|
|
|
#include "strv.h"
|
2018-09-13 13:34:12 +02:00
|
|
|
#include "tests.h"
|
2015-11-16 22:09:36 +01:00
|
|
|
#include "util.h"
|
2012-10-28 00:49:04 +02:00
|
|
|
|
2018-05-10 01:04:53 +02:00
|
|
|
static void test_parse_sleep_config(void) {
|
|
|
|
const char *verb;
|
|
|
|
|
|
|
|
FOREACH_STRING(verb, "suspend", "hibernate", "hybrid-sleep", "suspend-then-hibernate")
|
|
|
|
assert_se(parse_sleep_config(verb, NULL, NULL, NULL) == 0);
|
|
|
|
}
|
|
|
|
|
2018-03-08 09:41:50 +01:00
|
|
|
static int test_fiemap(const char *path) {
|
|
|
|
_cleanup_free_ struct fiemap *fiemap = NULL;
|
|
|
|
_cleanup_close_ int fd = -1;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
fd = open(path, O_RDONLY | O_CLOEXEC | O_NONBLOCK);
|
|
|
|
if (fd < 0)
|
|
|
|
return log_error_errno(errno, "failed to open %s: %m", path);
|
|
|
|
r = read_fiemap(fd, &fiemap);
|
2018-09-13 13:34:12 +02:00
|
|
|
if (r == -EOPNOTSUPP)
|
|
|
|
exit(log_tests_skipped("Not supported"));
|
2018-03-08 09:41:50 +01:00
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Unable to read extent map for '%s': %m", path);
|
|
|
|
log_info("extent map information for %s:", path);
|
2018-06-26 18:43:49 +02:00
|
|
|
log_info("\t start: %" PRIu64, (uint64_t) fiemap->fm_start);
|
|
|
|
log_info("\t length: %" PRIu64, (uint64_t) fiemap->fm_length);
|
|
|
|
log_info("\t flags: %" PRIu32, fiemap->fm_flags);
|
|
|
|
log_info("\t number of mapped extents: %" PRIu32, fiemap->fm_mapped_extents);
|
|
|
|
log_info("\t extent count: %" PRIu32, fiemap->fm_extent_count);
|
2018-03-08 09:41:50 +01:00
|
|
|
if (fiemap->fm_extent_count > 0)
|
2018-06-27 18:19:33 +02:00
|
|
|
log_info("\t first extent location: %" PRIu64,
|
|
|
|
(uint64_t) (fiemap->fm_extents[0].fe_physical / page_size()));
|
2018-03-08 09:41:50 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-09-17 22:13:18 +02:00
|
|
|
static void test_sleep(void) {
|
2013-05-04 18:31:28 +02:00
|
|
|
_cleanup_strv_free_ char
|
|
|
|
**standby = strv_new("standby", NULL),
|
|
|
|
**mem = strv_new("mem", NULL),
|
|
|
|
**disk = strv_new("disk", NULL),
|
|
|
|
**suspend = strv_new("suspend", NULL),
|
|
|
|
**reboot = strv_new("reboot", NULL),
|
|
|
|
**platform = strv_new("platform", NULL),
|
|
|
|
**shutdown = strv_new("shutdown", NULL),
|
|
|
|
**freez = strv_new("freeze", NULL);
|
2018-04-11 08:51:06 +02:00
|
|
|
int r;
|
2013-05-04 18:31:28 +02:00
|
|
|
|
2018-04-11 08:51:06 +02:00
|
|
|
log_info("/* configuration */");
|
2013-09-14 01:41:52 +02:00
|
|
|
log_info("Standby configured: %s", yes_no(can_sleep_state(standby) > 0));
|
|
|
|
log_info("Suspend configured: %s", yes_no(can_sleep_state(mem) > 0));
|
|
|
|
log_info("Hibernate configured: %s", yes_no(can_sleep_state(disk) > 0));
|
|
|
|
log_info("Hibernate+Suspend (Hybrid-Sleep) configured: %s", yes_no(can_sleep_disk(suspend) > 0));
|
|
|
|
log_info("Hibernate+Reboot configured: %s", yes_no(can_sleep_disk(reboot) > 0));
|
|
|
|
log_info("Hibernate+Platform configured: %s", yes_no(can_sleep_disk(platform) > 0));
|
|
|
|
log_info("Hibernate+Shutdown configured: %s", yes_no(can_sleep_disk(shutdown) > 0));
|
|
|
|
log_info("Freeze configured: %s", yes_no(can_sleep_state(freez) > 0));
|
2013-05-04 18:31:28 +02:00
|
|
|
|
2018-04-11 08:51:06 +02:00
|
|
|
log_info("/* running system */");
|
|
|
|
r = can_sleep("suspend");
|
|
|
|
log_info("Suspend configured and possible: %s", r >= 0 ? yes_no(r) : strerror(-r));
|
|
|
|
r = can_sleep("hibernate");
|
|
|
|
log_info("Hibernation configured and possible: %s", r >= 0 ? yes_no(r) : strerror(-r));
|
|
|
|
r = can_sleep("hybrid-sleep");
|
|
|
|
log_info("Hybrid-sleep configured and possible: %s", r >= 0 ? yes_no(r) : strerror(-r));
|
|
|
|
r = can_sleep("suspend-then-hibernate");
|
|
|
|
log_info("Suspend-then-Hibernate configured and possible: %s", r >= 0 ? yes_no(r) : strerror(-r));
|
2013-09-17 22:13:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char* argv[]) {
|
2018-03-08 09:41:50 +01:00
|
|
|
int i, r = 0, k;
|
|
|
|
|
2018-09-13 14:31:13 +02:00
|
|
|
test_setup_logging(LOG_INFO);
|
2013-09-17 22:13:18 +02:00
|
|
|
|
|
|
|
if (getuid() != 0)
|
2015-05-11 13:49:29 +02:00
|
|
|
log_warning("This program is unlikely to work for unprivileged users");
|
2013-09-17 22:13:18 +02:00
|
|
|
|
2018-05-10 01:04:53 +02:00
|
|
|
test_parse_sleep_config();
|
2013-09-17 22:13:18 +02:00
|
|
|
test_sleep();
|
2012-10-28 00:49:04 +02:00
|
|
|
|
2018-03-08 09:41:50 +01:00
|
|
|
if (argc <= 1)
|
|
|
|
assert_se(test_fiemap(argv[0]) == 0);
|
|
|
|
else
|
|
|
|
for (i = 1; i < argc; i++) {
|
|
|
|
k = test_fiemap(argv[i]);
|
|
|
|
if (r == 0)
|
|
|
|
r = k;
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
2012-10-28 00:49:04 +02:00
|
|
|
}
|