]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
6524990f | 2 | |
17c40b3a | 3 | #include <linux/fiemap.h> |
6524990f | 4 | #include <stdio.h> |
6524990f | 5 | |
17c40b3a | 6 | #include "fd-util.h" |
6524990f | 7 | #include "log.h" |
19adb8a3 ZJS |
8 | #include "sleep-config.h" |
9 | #include "strv.h" | |
cf0fbc49 | 10 | #include "util.h" |
6524990f | 11 | |
db983479 ZJS |
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 | ||
17c40b3a ML |
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); | |
2f0726c0 | 28 | if (r == -EOPNOTSUPP) { |
17c40b3a ML |
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 | ||
e9e506ed | 47 | static void test_sleep(void) { |
19adb8a3 ZJS |
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); | |
b71c9758 | 57 | int r; |
19adb8a3 | 58 | |
b71c9758 | 59 | log_info("/* configuration */"); |
69ab8088 ZJS |
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)); | |
19adb8a3 | 68 | |
b71c9758 ZJS |
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)); | |
e9e506ed ZJS |
78 | } |
79 | ||
80 | int main(int argc, char* argv[]) { | |
17c40b3a ML |
81 | int i, r = 0, k; |
82 | ||
e9e506ed ZJS |
83 | log_parse_environment(); |
84 | log_open(); | |
85 | ||
86 | if (getuid() != 0) | |
ff9b60f3 | 87 | log_warning("This program is unlikely to work for unprivileged users"); |
e9e506ed | 88 | |
db983479 | 89 | test_parse_sleep_config(); |
e9e506ed | 90 | test_sleep(); |
6524990f | 91 | |
17c40b3a ML |
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; | |
6524990f | 102 | } |