]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-sleep.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2012 Lennart Poettering
6 #include <linux/fiemap.h>
11 #include "sleep-config.h"
15 static void test_parse_sleep_config(void) {
18 FOREACH_STRING(verb
, "suspend", "hibernate", "hybrid-sleep", "suspend-then-hibernate")
19 assert_se(parse_sleep_config(verb
, NULL
, NULL
, NULL
) == 0);
22 static int test_fiemap(const char *path
) {
23 _cleanup_free_
struct fiemap
*fiemap
= NULL
;
24 _cleanup_close_
int fd
= -1;
27 fd
= open(path
, O_RDONLY
| O_CLOEXEC
| O_NONBLOCK
);
29 return log_error_errno(errno
, "failed to open %s: %m", path
);
30 r
= read_fiemap(fd
, &fiemap
);
31 if (r
== -EOPNOTSUPP
) {
32 log_info("Skipping test, not supported");
36 return log_error_errno(r
, "Unable to read extent map for '%s': %m", path
);
37 log_info("extent map information for %s:", path
);
38 log_info("\t start: %llu", fiemap
->fm_start
);
39 log_info("\t length: %llu", fiemap
->fm_length
);
40 log_info("\t flags: %u", fiemap
->fm_flags
);
41 log_info("\t number of mapped extents: %u", fiemap
->fm_mapped_extents
);
42 log_info("\t extent count: %u", fiemap
->fm_extent_count
);
43 if (fiemap
->fm_extent_count
> 0)
44 log_info("\t first extent location: %llu",
45 fiemap
->fm_extents
[0].fe_physical
/ page_size());
50 static void test_sleep(void) {
51 _cleanup_strv_free_
char
52 **standby
= strv_new("standby", NULL
),
53 **mem
= strv_new("mem", NULL
),
54 **disk
= strv_new("disk", NULL
),
55 **suspend
= strv_new("suspend", NULL
),
56 **reboot
= strv_new("reboot", NULL
),
57 **platform
= strv_new("platform", NULL
),
58 **shutdown
= strv_new("shutdown", NULL
),
59 **freez
= strv_new("freeze", NULL
);
62 log_info("/* configuration */");
63 log_info("Standby configured: %s", yes_no(can_sleep_state(standby
) > 0));
64 log_info("Suspend configured: %s", yes_no(can_sleep_state(mem
) > 0));
65 log_info("Hibernate configured: %s", yes_no(can_sleep_state(disk
) > 0));
66 log_info("Hibernate+Suspend (Hybrid-Sleep) configured: %s", yes_no(can_sleep_disk(suspend
) > 0));
67 log_info("Hibernate+Reboot configured: %s", yes_no(can_sleep_disk(reboot
) > 0));
68 log_info("Hibernate+Platform configured: %s", yes_no(can_sleep_disk(platform
) > 0));
69 log_info("Hibernate+Shutdown configured: %s", yes_no(can_sleep_disk(shutdown
) > 0));
70 log_info("Freeze configured: %s", yes_no(can_sleep_state(freez
) > 0));
72 log_info("/* running system */");
73 r
= can_sleep("suspend");
74 log_info("Suspend configured and possible: %s", r
>= 0 ? yes_no(r
) : strerror(-r
));
75 r
= can_sleep("hibernate");
76 log_info("Hibernation configured and possible: %s", r
>= 0 ? yes_no(r
) : strerror(-r
));
77 r
= can_sleep("hybrid-sleep");
78 log_info("Hybrid-sleep configured and possible: %s", r
>= 0 ? yes_no(r
) : strerror(-r
));
79 r
= can_sleep("suspend-then-hibernate");
80 log_info("Suspend-then-Hibernate configured and possible: %s", r
>= 0 ? yes_no(r
) : strerror(-r
));
83 int main(int argc
, char* argv
[]) {
86 log_parse_environment();
90 log_warning("This program is unlikely to work for unprivileged users");
92 test_parse_sleep_config();
96 assert_se(test_fiemap(argv
[0]) == 0);
98 for (i
= 1; i
< argc
; i
++) {
99 k
= test_fiemap(argv
[i
]);