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