]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-sleep.c
Merge pull request #12414 from keszybz/detect-podman
[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 "fd-util.h"
11 #include "log.h"
12 #include "memory-util.h"
13 #include "sleep-config.h"
14 #include "strv.h"
15 #include "tests.h"
16 #include "util.h"
17
18 static void test_parse_sleep_config(void) {
19 const char *verb;
20
21 log_info("/* %s */", __func__);
22
23 FOREACH_STRING(verb, "suspend", "hibernate", "hybrid-sleep", "suspend-then-hibernate")
24 assert_se(parse_sleep_config(verb, NULL, NULL, NULL, NULL) == 0);
25 }
26
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
32 log_info("/* %s */", __func__);
33
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);
38 if (r == -EOPNOTSUPP)
39 exit(log_tests_skipped("Not supported"));
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);
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);
48 if (fiemap->fm_extent_count > 0)
49 log_info("\t first extent location: %" PRIu64,
50 (uint64_t) (fiemap->fm_extents[0].fe_physical / page_size()));
51
52 return 0;
53 }
54
55 static void test_sleep(void) {
56 _cleanup_strv_free_ char
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"),
64 **freeze = strv_new("freeze");
65 int r;
66
67 log_info("/* %s */", __func__);
68
69 log_info("/= configuration =/");
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));
77 log_info("Freeze configured: %s", yes_no(can_sleep_state(freeze) > 0));
78
79 log_info("/= running system =/");
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));
88 }
89
90 int main(int argc, char* argv[]) {
91 int i, r = 0, k;
92
93 test_setup_logging(LOG_INFO);
94
95 if (getuid() != 0)
96 log_warning("This program is unlikely to work for unprivileged users");
97
98 test_parse_sleep_config();
99 test_sleep();
100
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;
111 }