]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-sleep.c
test-sleep: add more logging, show secure boot mode
[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
c550cb7f 10#include "efivars.h"
4bbccb02 11#include "errno-util.h"
17c40b3a 12#include "fd-util.h"
6524990f 13#include "log.h"
0a970718 14#include "memory-util.h"
19adb8a3
ZJS
15#include "sleep-config.h"
16#include "strv.h"
317bb217 17#include "tests.h"
ca78ad1d 18#include "util.h"
6524990f 19
db983479 20static void test_parse_sleep_config(void) {
28ca9c24 21 _cleanup_(free_sleep_configp) SleepConfig *sleep_config = NULL;
edf43e3d
ZJS
22 log_info("/* %s */", __func__);
23
20c3acfa 24 assert_se(parse_sleep_config(&sleep_config) == 0);
4a6a2092
ZJS
25
26 _cleanup_free_ char *sum, *sus, *him, *his, *hym, *hys;
27
28 sum = strv_join(sleep_config->suspend_modes, ", ");
29 sus = strv_join(sleep_config->suspend_states, ", ");
30 him = strv_join(sleep_config->hibernate_modes, ", ");
31 his = strv_join(sleep_config->hibernate_states, ", ");
32 hym = strv_join(sleep_config->hybrid_modes, ", ");
33 hys = strv_join(sleep_config->hybrid_states, ", ");
34 log_debug(" allow_suspend: %u", sleep_config->allow_suspend);
35 log_debug(" allow_hibernate: %u", sleep_config->allow_hibernate);
36 log_debug(" allow_s2h: %u", sleep_config->allow_s2h);
37 log_debug(" allow_hybrid_sleep: %u", sleep_config->allow_hybrid_sleep);
38 log_debug(" suspend modes: %s", sum);
39 log_debug(" states: %s", sus);
40 log_debug(" hibernate modes: %s", him);
41 log_debug(" states: %s", his);
42 log_debug(" hybrid modes: %s", hym);
43 log_debug(" states: %s", hys);
db983479
ZJS
44}
45
17c40b3a
ML
46static int test_fiemap(const char *path) {
47 _cleanup_free_ struct fiemap *fiemap = NULL;
48 _cleanup_close_ int fd = -1;
49 int r;
50
edf43e3d
ZJS
51 log_info("/* %s */", __func__);
52
17c40b3a
ML
53 fd = open(path, O_RDONLY | O_CLOEXEC | O_NONBLOCK);
54 if (fd < 0)
55 return log_error_errno(errno, "failed to open %s: %m", path);
56 r = read_fiemap(fd, &fiemap);
317bb217
ZJS
57 if (r == -EOPNOTSUPP)
58 exit(log_tests_skipped("Not supported"));
17c40b3a
ML
59 if (r < 0)
60 return log_error_errno(r, "Unable to read extent map for '%s': %m", path);
61 log_info("extent map information for %s:", path);
ad4bc335
FB
62 log_info("\t start: %" PRIu64, (uint64_t) fiemap->fm_start);
63 log_info("\t length: %" PRIu64, (uint64_t) fiemap->fm_length);
64 log_info("\t flags: %" PRIu32, fiemap->fm_flags);
65 log_info("\t number of mapped extents: %" PRIu32, fiemap->fm_mapped_extents);
66 log_info("\t extent count: %" PRIu32, fiemap->fm_extent_count);
17c40b3a 67 if (fiemap->fm_extent_count > 0)
d7af62d5
FB
68 log_info("\t first extent location: %" PRIu64,
69 (uint64_t) (fiemap->fm_extents[0].fe_physical / page_size()));
17c40b3a
ML
70
71 return 0;
72}
73
e9e506ed 74static void test_sleep(void) {
19adb8a3 75 _cleanup_strv_free_ char
bea1a013
LP
76 **standby = strv_new("standby"),
77 **mem = strv_new("mem"),
78 **disk = strv_new("disk"),
79 **suspend = strv_new("suspend"),
80 **reboot = strv_new("reboot"),
81 **platform = strv_new("platform"),
82 **shutdown = strv_new("shutdown"),
5238e957 83 **freeze = strv_new("freeze");
b71c9758 84 int r;
19adb8a3 85
edf43e3d
ZJS
86 log_info("/* %s */", __func__);
87
c550cb7f
ZJS
88 printf("Secure boot: %sd\n", enable_disable(is_efi_secure_boot()));
89
90 log_info("/= individual sleep modes =/");
69ab8088
ZJS
91 log_info("Standby configured: %s", yes_no(can_sleep_state(standby) > 0));
92 log_info("Suspend configured: %s", yes_no(can_sleep_state(mem) > 0));
93 log_info("Hibernate configured: %s", yes_no(can_sleep_state(disk) > 0));
94 log_info("Hibernate+Suspend (Hybrid-Sleep) configured: %s", yes_no(can_sleep_disk(suspend) > 0));
95 log_info("Hibernate+Reboot configured: %s", yes_no(can_sleep_disk(reboot) > 0));
96 log_info("Hibernate+Platform configured: %s", yes_no(can_sleep_disk(platform) > 0));
97 log_info("Hibernate+Shutdown configured: %s", yes_no(can_sleep_disk(shutdown) > 0));
5238e957 98 log_info("Freeze configured: %s", yes_no(can_sleep_state(freeze) > 0));
19adb8a3 99
c550cb7f 100 log_info("/= high-level sleep verbs =/");
b71c9758 101 r = can_sleep("suspend");
4bbccb02 102 log_info("Suspend configured and possible: %s", r >= 0 ? yes_no(r) : strerror_safe(r));
b71c9758 103 r = can_sleep("hibernate");
4bbccb02 104 log_info("Hibernation configured and possible: %s", r >= 0 ? yes_no(r) : strerror_safe(r));
b71c9758 105 r = can_sleep("hybrid-sleep");
4bbccb02 106 log_info("Hybrid-sleep configured and possible: %s", r >= 0 ? yes_no(r) : strerror_safe(r));
b71c9758 107 r = can_sleep("suspend-then-hibernate");
4bbccb02 108 log_info("Suspend-then-Hibernate configured and possible: %s", r >= 0 ? yes_no(r) : strerror_safe(r));
e9e506ed
ZJS
109}
110
111int main(int argc, char* argv[]) {
17c40b3a
ML
112 int i, r = 0, k;
113
4a6a2092 114 test_setup_logging(LOG_DEBUG);
e9e506ed
ZJS
115
116 if (getuid() != 0)
ff9b60f3 117 log_warning("This program is unlikely to work for unprivileged users");
e9e506ed 118
db983479 119 test_parse_sleep_config();
e9e506ed 120 test_sleep();
6524990f 121
17c40b3a
ML
122 if (argc <= 1)
123 assert_se(test_fiemap(argv[0]) == 0);
124 else
125 for (i = 1; i < argc; i++) {
126 k = test_fiemap(argv[i]);
127 if (r == 0)
128 r = k;
129 }
130
131 return r;
6524990f 132}