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