]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-sleep.c
Merge pull request #16271 from yuwata/network-cleanups-around-link-get
[thirdparty/systemd.git] / src / test / test-sleep.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
6524990f 2
ca78ad1d 3#include <fcntl.h>
ad4bc335 4#include <inttypes.h>
17c40b3a 5#include <linux/fiemap.h>
6524990f 6#include <stdio.h>
ca78ad1d
ZJS
7#include <sys/types.h>
8#include <unistd.h>
6524990f 9
4bbccb02 10#include "errno-util.h"
17c40b3a 11#include "fd-util.h"
6524990f 12#include "log.h"
0a970718 13#include "memory-util.h"
19adb8a3
ZJS
14#include "sleep-config.h"
15#include "strv.h"
317bb217 16#include "tests.h"
ca78ad1d 17#include "util.h"
6524990f 18
db983479 19static void test_parse_sleep_config(void) {
28ca9c24 20 _cleanup_(free_sleep_configp) SleepConfig *sleep_config = NULL;
edf43e3d
ZJS
21 log_info("/* %s */", __func__);
22
20c3acfa 23 assert_se(parse_sleep_config(&sleep_config) == 0);
4a6a2092
ZJS
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);
db983479
ZJS
43}
44
17c40b3a
ML
45static int test_fiemap(const char *path) {
46 _cleanup_free_ struct fiemap *fiemap = NULL;
47 _cleanup_close_ int fd = -1;
48 int r;
49
edf43e3d
ZJS
50 log_info("/* %s */", __func__);
51
17c40b3a
ML
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);
317bb217
ZJS
56 if (r == -EOPNOTSUPP)
57 exit(log_tests_skipped("Not supported"));
17c40b3a
ML
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);
ad4bc335
FB
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);
17c40b3a 66 if (fiemap->fm_extent_count > 0)
d7af62d5
FB
67 log_info("\t first extent location: %" PRIu64,
68 (uint64_t) (fiemap->fm_extents[0].fe_physical / page_size()));
17c40b3a
ML
69
70 return 0;
71}
72
e9e506ed 73static void test_sleep(void) {
19adb8a3 74 _cleanup_strv_free_ char
bea1a013
LP
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"),
5238e957 82 **freeze = strv_new("freeze");
b71c9758 83 int r;
19adb8a3 84
edf43e3d
ZJS
85 log_info("/* %s */", __func__);
86
87 log_info("/= configuration =/");
69ab8088
ZJS
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));
5238e957 95 log_info("Freeze configured: %s", yes_no(can_sleep_state(freeze) > 0));
19adb8a3 96
edf43e3d 97 log_info("/= running system =/");
b71c9758 98 r = can_sleep("suspend");
4bbccb02 99 log_info("Suspend configured and possible: %s", r >= 0 ? yes_no(r) : strerror_safe(r));
b71c9758 100 r = can_sleep("hibernate");
4bbccb02 101 log_info("Hibernation configured and possible: %s", r >= 0 ? yes_no(r) : strerror_safe(r));
b71c9758 102 r = can_sleep("hybrid-sleep");
4bbccb02 103 log_info("Hybrid-sleep configured and possible: %s", r >= 0 ? yes_no(r) : strerror_safe(r));
b71c9758 104 r = can_sleep("suspend-then-hibernate");
4bbccb02 105 log_info("Suspend-then-Hibernate configured and possible: %s", r >= 0 ? yes_no(r) : strerror_safe(r));
e9e506ed
ZJS
106}
107
108int main(int argc, char* argv[]) {
17c40b3a
ML
109 int i, r = 0, k;
110
4a6a2092 111 test_setup_logging(LOG_DEBUG);
e9e506ed
ZJS
112
113 if (getuid() != 0)
ff9b60f3 114 log_warning("This program is unlikely to work for unprivileged users");
e9e506ed 115
db983479 116 test_parse_sleep_config();
e9e506ed 117 test_sleep();
6524990f 118
17c40b3a
ML
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;
6524990f 129}