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