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