]>
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) {
21 log_info("/* %s */", __func__
);
23 FOREACH_STRING(verb
, "suspend", "hibernate", "hybrid-sleep", "suspend-then-hibernate")
24 assert_se(parse_sleep_config(verb
, NULL
, NULL
, NULL
, NULL
) == 0);
27 static int test_fiemap(const char *path
) {
28 _cleanup_free_
struct fiemap
*fiemap
= NULL
;
29 _cleanup_close_
int fd
= -1;
32 log_info("/* %s */", __func__
);
34 fd
= open(path
, O_RDONLY
| O_CLOEXEC
| O_NONBLOCK
);
36 return log_error_errno(errno
, "failed to open %s: %m", path
);
37 r
= read_fiemap(fd
, &fiemap
);
39 exit(log_tests_skipped("Not supported"));
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()));
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");
67 log_info("/* %s */", __func__
);
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));
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
));
90 int main(int argc
, char* argv
[]) {
93 test_setup_logging(LOG_INFO
);
96 log_warning("This program is unlikely to work for unprivileged users");
98 test_parse_sleep_config();
102 assert_se(test_fiemap(argv
[0]) == 0);
104 for (i
= 1; i
< argc
; i
++) {
105 k
= test_fiemap(argv
[i
]);