]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-sleep.c
grypt-util: drop two emacs modelines
[thirdparty/systemd.git] / src / test / test-sleep.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright 2012 Lennart Poettering
4 ***/
5
6 #include <linux/fiemap.h>
7 #include <stdio.h>
8
9 #include "fd-util.h"
10 #include "log.h"
11 #include "sleep-config.h"
12 #include "strv.h"
13 #include "util.h"
14
15 static void test_parse_sleep_config(void) {
16 const char *verb;
17
18 FOREACH_STRING(verb, "suspend", "hibernate", "hybrid-sleep", "suspend-then-hibernate")
19 assert_se(parse_sleep_config(verb, NULL, NULL, NULL) == 0);
20 }
21
22 static int test_fiemap(const char *path) {
23 _cleanup_free_ struct fiemap *fiemap = NULL;
24 _cleanup_close_ int fd = -1;
25 int r;
26
27 fd = open(path, O_RDONLY | O_CLOEXEC | O_NONBLOCK);
28 if (fd < 0)
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");
33 exit(EXIT_TEST_SKIP);
34 }
35 if (r < 0)
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());
46
47 return 0;
48 }
49
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);
60 int r;
61
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));
71
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));
81 }
82
83 int main(int argc, char* argv[]) {
84 int i, r = 0, k;
85
86 log_parse_environment();
87 log_open();
88
89 if (getuid() != 0)
90 log_warning("This program is unlikely to work for unprivileged users");
91
92 test_parse_sleep_config();
93 test_sleep();
94
95 if (argc <= 1)
96 assert_se(test_fiemap(argv[0]) == 0);
97 else
98 for (i = 1; i < argc; i++) {
99 k = test_fiemap(argv[i]);
100 if (r == 0)
101 r = k;
102 }
103
104 return r;
105 }