]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-sleep.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 #include <linux/fiemap.h>
12 #include "memory-util.h"
13 #include "sleep-config.h"
18 static void test_parse_sleep_config(void) {
19 _cleanup_(free_sleep_configp
) SleepConfig
*sleep_config
= NULL
;
20 log_info("/* %s */", __func__
);
22 assert(parse_sleep_config(&sleep_config
) == 0);
24 _cleanup_free_
char *sum
, *sus
, *him
, *his
, *hym
, *hys
;
26 sum
= strv_join(sleep_config
->suspend_modes
, ", ");
27 sus
= strv_join(sleep_config
->suspend_states
, ", ");
28 him
= strv_join(sleep_config
->hibernate_modes
, ", ");
29 his
= strv_join(sleep_config
->hibernate_states
, ", ");
30 hym
= strv_join(sleep_config
->hybrid_modes
, ", ");
31 hys
= strv_join(sleep_config
->hybrid_states
, ", ");
32 log_debug(" allow_suspend: %u", sleep_config
->allow_suspend
);
33 log_debug(" allow_hibernate: %u", sleep_config
->allow_hibernate
);
34 log_debug(" allow_s2h: %u", sleep_config
->allow_s2h
);
35 log_debug(" allow_hybrid_sleep: %u", sleep_config
->allow_hybrid_sleep
);
36 log_debug(" suspend modes: %s", sum
);
37 log_debug(" states: %s", sus
);
38 log_debug(" hibernate modes: %s", him
);
39 log_debug(" states: %s", his
);
40 log_debug(" hybrid modes: %s", hym
);
41 log_debug(" states: %s", hys
);
44 static int test_fiemap(const char *path
) {
45 _cleanup_free_
struct fiemap
*fiemap
= NULL
;
46 _cleanup_close_
int fd
= -1;
49 log_info("/* %s */", __func__
);
51 fd
= open(path
, O_RDONLY
| O_CLOEXEC
| O_NONBLOCK
);
53 return log_error_errno(errno
, "failed to open %s: %m", path
);
54 r
= read_fiemap(fd
, &fiemap
);
56 exit(log_tests_skipped("Not supported"));
58 return log_error_errno(r
, "Unable to read extent map for '%s': %m", path
);
59 log_info("extent map information for %s:", path
);
60 log_info("\t start: %" PRIu64
, (uint64_t) fiemap
->fm_start
);
61 log_info("\t length: %" PRIu64
, (uint64_t) fiemap
->fm_length
);
62 log_info("\t flags: %" PRIu32
, fiemap
->fm_flags
);
63 log_info("\t number of mapped extents: %" PRIu32
, fiemap
->fm_mapped_extents
);
64 log_info("\t extent count: %" PRIu32
, fiemap
->fm_extent_count
);
65 if (fiemap
->fm_extent_count
> 0)
66 log_info("\t first extent location: %" PRIu64
,
67 (uint64_t) (fiemap
->fm_extents
[0].fe_physical
/ page_size()));
72 static void test_sleep(void) {
73 _cleanup_strv_free_
char
74 **standby
= strv_new("standby"),
75 **mem
= strv_new("mem"),
76 **disk
= strv_new("disk"),
77 **suspend
= strv_new("suspend"),
78 **reboot
= strv_new("reboot"),
79 **platform
= strv_new("platform"),
80 **shutdown
= strv_new("shutdown"),
81 **freeze
= strv_new("freeze");
84 log_info("/* %s */", __func__
);
86 log_info("/= configuration =/");
87 log_info("Standby configured: %s", yes_no(can_sleep_state(standby
) > 0));
88 log_info("Suspend configured: %s", yes_no(can_sleep_state(mem
) > 0));
89 log_info("Hibernate configured: %s", yes_no(can_sleep_state(disk
) > 0));
90 log_info("Hibernate+Suspend (Hybrid-Sleep) configured: %s", yes_no(can_sleep_disk(suspend
) > 0));
91 log_info("Hibernate+Reboot configured: %s", yes_no(can_sleep_disk(reboot
) > 0));
92 log_info("Hibernate+Platform configured: %s", yes_no(can_sleep_disk(platform
) > 0));
93 log_info("Hibernate+Shutdown configured: %s", yes_no(can_sleep_disk(shutdown
) > 0));
94 log_info("Freeze configured: %s", yes_no(can_sleep_state(freeze
) > 0));
96 log_info("/= running system =/");
97 r
= can_sleep("suspend");
98 log_info("Suspend configured and possible: %s", r
>= 0 ? yes_no(r
) : strerror(-r
));
99 r
= can_sleep("hibernate");
100 log_info("Hibernation configured and possible: %s", r
>= 0 ? yes_no(r
) : strerror(-r
));
101 r
= can_sleep("hybrid-sleep");
102 log_info("Hybrid-sleep configured and possible: %s", r
>= 0 ? yes_no(r
) : strerror(-r
));
103 r
= can_sleep("suspend-then-hibernate");
104 log_info("Suspend-then-Hibernate configured and possible: %s", r
>= 0 ? yes_no(r
) : strerror(-r
));
107 int main(int argc
, char* argv
[]) {
110 test_setup_logging(LOG_DEBUG
);
113 log_warning("This program is unlikely to work for unprivileged users");
115 test_parse_sleep_config();
119 assert_se(test_fiemap(argv
[0]) == 0);
121 for (i
= 1; i
< argc
; i
++) {
122 k
= test_fiemap(argv
[i
]);