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