]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-sleep.c
util: split out some stuff into a new file limits-util.[ch]
[thirdparty/systemd.git] / src / test / test-sleep.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
6524990f 2
ad4bc335 3#include <inttypes.h>
17c40b3a 4#include <linux/fiemap.h>
6524990f 5#include <stdio.h>
6524990f 6
17c40b3a 7#include "fd-util.h"
6524990f 8#include "log.h"
19adb8a3
ZJS
9#include "sleep-config.h"
10#include "strv.h"
317bb217 11#include "tests.h"
cf0fbc49 12#include "util.h"
6524990f 13
db983479
ZJS
14static void test_parse_sleep_config(void) {
15 const char *verb;
16
edf43e3d
ZJS
17 log_info("/* %s */", __func__);
18
db983479 19 FOREACH_STRING(verb, "suspend", "hibernate", "hybrid-sleep", "suspend-then-hibernate")
e8f1d00d 20 assert_se(parse_sleep_config(verb, NULL, NULL, NULL, NULL) == 0);
db983479
ZJS
21}
22
17c40b3a
ML
23static int test_fiemap(const char *path) {
24 _cleanup_free_ struct fiemap *fiemap = NULL;
25 _cleanup_close_ int fd = -1;
26 int r;
27
edf43e3d
ZJS
28 log_info("/* %s */", __func__);
29
17c40b3a
ML
30 fd = open(path, O_RDONLY | O_CLOEXEC | O_NONBLOCK);
31 if (fd < 0)
32 return log_error_errno(errno, "failed to open %s: %m", path);
33 r = read_fiemap(fd, &fiemap);
317bb217
ZJS
34 if (r == -EOPNOTSUPP)
35 exit(log_tests_skipped("Not supported"));
17c40b3a
ML
36 if (r < 0)
37 return log_error_errno(r, "Unable to read extent map for '%s': %m", path);
38 log_info("extent map information for %s:", path);
ad4bc335
FB
39 log_info("\t start: %" PRIu64, (uint64_t) fiemap->fm_start);
40 log_info("\t length: %" PRIu64, (uint64_t) fiemap->fm_length);
41 log_info("\t flags: %" PRIu32, fiemap->fm_flags);
42 log_info("\t number of mapped extents: %" PRIu32, fiemap->fm_mapped_extents);
43 log_info("\t extent count: %" PRIu32, fiemap->fm_extent_count);
17c40b3a 44 if (fiemap->fm_extent_count > 0)
d7af62d5
FB
45 log_info("\t first extent location: %" PRIu64,
46 (uint64_t) (fiemap->fm_extents[0].fe_physical / page_size()));
17c40b3a
ML
47
48 return 0;
49}
50
e9e506ed 51static void test_sleep(void) {
19adb8a3 52 _cleanup_strv_free_ char
bea1a013
LP
53 **standby = strv_new("standby"),
54 **mem = strv_new("mem"),
55 **disk = strv_new("disk"),
56 **suspend = strv_new("suspend"),
57 **reboot = strv_new("reboot"),
58 **platform = strv_new("platform"),
59 **shutdown = strv_new("shutdown"),
60 **freez = strv_new("freeze");
b71c9758 61 int r;
19adb8a3 62
edf43e3d
ZJS
63 log_info("/* %s */", __func__);
64
65 log_info("/= configuration =/");
69ab8088
ZJS
66 log_info("Standby configured: %s", yes_no(can_sleep_state(standby) > 0));
67 log_info("Suspend configured: %s", yes_no(can_sleep_state(mem) > 0));
68 log_info("Hibernate configured: %s", yes_no(can_sleep_state(disk) > 0));
69 log_info("Hibernate+Suspend (Hybrid-Sleep) configured: %s", yes_no(can_sleep_disk(suspend) > 0));
70 log_info("Hibernate+Reboot configured: %s", yes_no(can_sleep_disk(reboot) > 0));
71 log_info("Hibernate+Platform configured: %s", yes_no(can_sleep_disk(platform) > 0));
72 log_info("Hibernate+Shutdown configured: %s", yes_no(can_sleep_disk(shutdown) > 0));
73 log_info("Freeze configured: %s", yes_no(can_sleep_state(freez) > 0));
19adb8a3 74
edf43e3d 75 log_info("/= running system =/");
b71c9758
ZJS
76 r = can_sleep("suspend");
77 log_info("Suspend configured and possible: %s", r >= 0 ? yes_no(r) : strerror(-r));
78 r = can_sleep("hibernate");
79 log_info("Hibernation configured and possible: %s", r >= 0 ? yes_no(r) : strerror(-r));
80 r = can_sleep("hybrid-sleep");
81 log_info("Hybrid-sleep configured and possible: %s", r >= 0 ? yes_no(r) : strerror(-r));
82 r = can_sleep("suspend-then-hibernate");
83 log_info("Suspend-then-Hibernate configured and possible: %s", r >= 0 ? yes_no(r) : strerror(-r));
e9e506ed
ZJS
84}
85
86int main(int argc, char* argv[]) {
17c40b3a
ML
87 int i, r = 0, k;
88
6d7c4033 89 test_setup_logging(LOG_INFO);
e9e506ed
ZJS
90
91 if (getuid() != 0)
ff9b60f3 92 log_warning("This program is unlikely to work for unprivileged users");
e9e506ed 93
db983479 94 test_parse_sleep_config();
e9e506ed 95 test_sleep();
6524990f 96
17c40b3a
ML
97 if (argc <= 1)
98 assert_se(test_fiemap(argv[0]) == 0);
99 else
100 for (i = 1; i < argc; i++) {
101 k = test_fiemap(argv[i]);
102 if (r == 0)
103 r = k;
104 }
105
106 return r;
6524990f 107}