]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-sleep.c
5286442e268aaf21fdecf1812b3e42a4cc3fe8f3
[thirdparty/systemd.git] / src / test / test-sleep.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <linux/fiemap.h>
4 #include <stdio.h>
5
6 #include "fd-util.h"
7 #include "log.h"
8 #include "sleep-config.h"
9 #include "strv.h"
10 #include "util.h"
11
12 static void test_parse_sleep_config(void) {
13 const char *verb;
14
15 FOREACH_STRING(verb, "suspend", "hibernate", "hybrid-sleep", "suspend-then-hibernate")
16 assert_se(parse_sleep_config(verb, NULL, NULL, NULL) == 0);
17 }
18
19 static int test_fiemap(const char *path) {
20 _cleanup_free_ struct fiemap *fiemap = NULL;
21 _cleanup_close_ int fd = -1;
22 int r;
23
24 fd = open(path, O_RDONLY | O_CLOEXEC | O_NONBLOCK);
25 if (fd < 0)
26 return log_error_errno(errno, "failed to open %s: %m", path);
27 r = read_fiemap(fd, &fiemap);
28 if (r == -EOPNOTSUPP) {
29 log_info("Skipping test, not supported");
30 exit(EXIT_TEST_SKIP);
31 }
32 if (r < 0)
33 return log_error_errno(r, "Unable to read extent map for '%s': %m", path);
34 log_info("extent map information for %s:", path);
35 log_info("\t start: %llu", fiemap->fm_start);
36 log_info("\t length: %llu", fiemap->fm_length);
37 log_info("\t flags: %u", fiemap->fm_flags);
38 log_info("\t number of mapped extents: %u", fiemap->fm_mapped_extents);
39 log_info("\t extent count: %u", fiemap->fm_extent_count);
40 if (fiemap->fm_extent_count > 0)
41 log_info("\t first extent location: %llu",
42 fiemap->fm_extents[0].fe_physical / page_size());
43
44 return 0;
45 }
46
47 static void test_sleep(void) {
48 _cleanup_strv_free_ char
49 **standby = strv_new("standby", NULL),
50 **mem = strv_new("mem", NULL),
51 **disk = strv_new("disk", NULL),
52 **suspend = strv_new("suspend", NULL),
53 **reboot = strv_new("reboot", NULL),
54 **platform = strv_new("platform", NULL),
55 **shutdown = strv_new("shutdown", NULL),
56 **freez = strv_new("freeze", NULL);
57 int r;
58
59 log_info("/* configuration */");
60 log_info("Standby configured: %s", yes_no(can_sleep_state(standby) > 0));
61 log_info("Suspend configured: %s", yes_no(can_sleep_state(mem) > 0));
62 log_info("Hibernate configured: %s", yes_no(can_sleep_state(disk) > 0));
63 log_info("Hibernate+Suspend (Hybrid-Sleep) configured: %s", yes_no(can_sleep_disk(suspend) > 0));
64 log_info("Hibernate+Reboot configured: %s", yes_no(can_sleep_disk(reboot) > 0));
65 log_info("Hibernate+Platform configured: %s", yes_no(can_sleep_disk(platform) > 0));
66 log_info("Hibernate+Shutdown configured: %s", yes_no(can_sleep_disk(shutdown) > 0));
67 log_info("Freeze configured: %s", yes_no(can_sleep_state(freez) > 0));
68
69 log_info("/* running system */");
70 r = can_sleep("suspend");
71 log_info("Suspend configured and possible: %s", r >= 0 ? yes_no(r) : strerror(-r));
72 r = can_sleep("hibernate");
73 log_info("Hibernation configured and possible: %s", r >= 0 ? yes_no(r) : strerror(-r));
74 r = can_sleep("hybrid-sleep");
75 log_info("Hybrid-sleep configured and possible: %s", r >= 0 ? yes_no(r) : strerror(-r));
76 r = can_sleep("suspend-then-hibernate");
77 log_info("Suspend-then-Hibernate configured and possible: %s", r >= 0 ? yes_no(r) : strerror(-r));
78 }
79
80 int main(int argc, char* argv[]) {
81 int i, r = 0, k;
82
83 log_parse_environment();
84 log_open();
85
86 if (getuid() != 0)
87 log_warning("This program is unlikely to work for unprivileged users");
88
89 test_parse_sleep_config();
90 test_sleep();
91
92 if (argc <= 1)
93 assert_se(test_fiemap(argv[0]) == 0);
94 else
95 for (i = 1; i < argc; i++) {
96 k = test_fiemap(argv[i]);
97 if (r == 0)
98 r = k;
99 }
100
101 return r;
102 }