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