]>
Commit | Line | Data |
---|---|---|
1 | /* SPDX-License-Identifier: LGPL-2.1+ */ | |
2 | ||
3 | #include <fcntl.h> | |
4 | #include <inttypes.h> | |
5 | #include <linux/fiemap.h> | |
6 | #include <stdio.h> | |
7 | #include <sys/types.h> | |
8 | #include <unistd.h> | |
9 | ||
10 | #include "efivars.h" | |
11 | #include "errno-util.h" | |
12 | #include "fd-util.h" | |
13 | #include "log.h" | |
14 | #include "memory-util.h" | |
15 | #include "sleep-config.h" | |
16 | #include "strv.h" | |
17 | #include "tests.h" | |
18 | #include "util.h" | |
19 | ||
20 | static void test_parse_sleep_config(void) { | |
21 | _cleanup_(free_sleep_configp) SleepConfig *sleep_config = NULL; | |
22 | log_info("/* %s */", __func__); | |
23 | ||
24 | assert_se(parse_sleep_config(&sleep_config) == 0); | |
25 | ||
26 | _cleanup_free_ char *sum, *sus, *him, *his, *hym, *hys; | |
27 | ||
28 | sum = strv_join(sleep_config->suspend_modes, ", "); | |
29 | sus = strv_join(sleep_config->suspend_states, ", "); | |
30 | him = strv_join(sleep_config->hibernate_modes, ", "); | |
31 | his = strv_join(sleep_config->hibernate_states, ", "); | |
32 | hym = strv_join(sleep_config->hybrid_modes, ", "); | |
33 | hys = strv_join(sleep_config->hybrid_states, ", "); | |
34 | log_debug(" allow_suspend: %u", sleep_config->allow_suspend); | |
35 | log_debug(" allow_hibernate: %u", sleep_config->allow_hibernate); | |
36 | log_debug(" allow_s2h: %u", sleep_config->allow_s2h); | |
37 | log_debug(" allow_hybrid_sleep: %u", sleep_config->allow_hybrid_sleep); | |
38 | log_debug(" suspend modes: %s", sum); | |
39 | log_debug(" states: %s", sus); | |
40 | log_debug(" hibernate modes: %s", him); | |
41 | log_debug(" states: %s", his); | |
42 | log_debug(" hybrid modes: %s", hym); | |
43 | log_debug(" states: %s", hys); | |
44 | } | |
45 | ||
46 | static int test_fiemap(const char *path) { | |
47 | _cleanup_free_ struct fiemap *fiemap = NULL; | |
48 | _cleanup_close_ int fd = -1; | |
49 | int r; | |
50 | ||
51 | log_info("/* %s */", __func__); | |
52 | ||
53 | fd = open(path, O_RDONLY | O_CLOEXEC | O_NONBLOCK); | |
54 | if (fd < 0) | |
55 | return log_error_errno(errno, "failed to open %s: %m", path); | |
56 | r = read_fiemap(fd, &fiemap); | |
57 | if (r == -EOPNOTSUPP) | |
58 | exit(log_tests_skipped("Not supported")); | |
59 | if (r < 0) | |
60 | return log_error_errno(r, "Unable to read extent map for '%s': %m", path); | |
61 | log_info("extent map information for %s:", path); | |
62 | log_info("\t start: %" PRIu64, (uint64_t) fiemap->fm_start); | |
63 | log_info("\t length: %" PRIu64, (uint64_t) fiemap->fm_length); | |
64 | log_info("\t flags: %" PRIu32, fiemap->fm_flags); | |
65 | log_info("\t number of mapped extents: %" PRIu32, fiemap->fm_mapped_extents); | |
66 | log_info("\t extent count: %" PRIu32, fiemap->fm_extent_count); | |
67 | if (fiemap->fm_extent_count > 0) | |
68 | log_info("\t first extent location: %" PRIu64, | |
69 | (uint64_t) (fiemap->fm_extents[0].fe_physical / page_size())); | |
70 | ||
71 | return 0; | |
72 | } | |
73 | ||
74 | static void test_sleep(void) { | |
75 | _cleanup_strv_free_ char | |
76 | **standby = strv_new("standby"), | |
77 | **mem = strv_new("mem"), | |
78 | **disk = strv_new("disk"), | |
79 | **suspend = strv_new("suspend"), | |
80 | **reboot = strv_new("reboot"), | |
81 | **platform = strv_new("platform"), | |
82 | **shutdown = strv_new("shutdown"), | |
83 | **freeze = strv_new("freeze"); | |
84 | int r; | |
85 | ||
86 | log_info("/* %s */", __func__); | |
87 | ||
88 | printf("Secure boot: %sd\n", enable_disable(is_efi_secure_boot())); | |
89 | ||
90 | log_info("/= individual sleep modes =/"); | |
91 | log_info("Standby configured: %s", yes_no(can_sleep_state(standby) > 0)); | |
92 | log_info("Suspend configured: %s", yes_no(can_sleep_state(mem) > 0)); | |
93 | log_info("Hibernate configured: %s", yes_no(can_sleep_state(disk) > 0)); | |
94 | log_info("Hibernate+Suspend (Hybrid-Sleep) configured: %s", yes_no(can_sleep_disk(suspend) > 0)); | |
95 | log_info("Hibernate+Reboot configured: %s", yes_no(can_sleep_disk(reboot) > 0)); | |
96 | log_info("Hibernate+Platform configured: %s", yes_no(can_sleep_disk(platform) > 0)); | |
97 | log_info("Hibernate+Shutdown configured: %s", yes_no(can_sleep_disk(shutdown) > 0)); | |
98 | log_info("Freeze configured: %s", yes_no(can_sleep_state(freeze) > 0)); | |
99 | ||
100 | log_info("/= high-level sleep verbs =/"); | |
101 | r = can_sleep("suspend"); | |
102 | log_info("Suspend configured and possible: %s", r >= 0 ? yes_no(r) : strerror_safe(r)); | |
103 | r = can_sleep("hibernate"); | |
104 | log_info("Hibernation configured and possible: %s", r >= 0 ? yes_no(r) : strerror_safe(r)); | |
105 | r = can_sleep("hybrid-sleep"); | |
106 | log_info("Hybrid-sleep configured and possible: %s", r >= 0 ? yes_no(r) : strerror_safe(r)); | |
107 | r = can_sleep("suspend-then-hibernate"); | |
108 | log_info("Suspend-then-Hibernate configured and possible: %s", r >= 0 ? yes_no(r) : strerror_safe(r)); | |
109 | } | |
110 | ||
111 | int main(int argc, char* argv[]) { | |
112 | int i, r = 0, k; | |
113 | ||
114 | test_setup_logging(LOG_DEBUG); | |
115 | ||
116 | if (getuid() != 0) | |
117 | log_warning("This program is unlikely to work for unprivileged users"); | |
118 | ||
119 | test_parse_sleep_config(); | |
120 | test_sleep(); | |
121 | ||
122 | if (argc <= 1) | |
123 | assert_se(test_fiemap(argv[0]) == 0); | |
124 | else | |
125 | for (i = 1; i < argc; i++) { | |
126 | k = test_fiemap(argv[i]); | |
127 | if (r == 0) | |
128 | r = k; | |
129 | } | |
130 | ||
131 | return r; | |
132 | } |