1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 
3 #include <fcntl.h>
4 #include <inttypes.h>
5 #include <linux/fiemap.h>
6 #include <stdio.h>
7 #include <sys/types.h>
8 #include <unistd.h>
9 
10 #include "efivars.h"
11 #include "errno-util.h"
12 #include "fd-util.h"
13 #include "log.h"
14 #include "memory-util.h"
15 #include "sleep-config.h"
16 #include "strv.h"
17 #include "tests.h"
18 #include "util.h"
19 
TEST(parse_sleep_config)20 TEST(parse_sleep_config) {
21         _cleanup_(free_sleep_configp) SleepConfig *sleep_config = NULL;
22 
23         assert_se(parse_sleep_config(&sleep_config) == 0);
24 
25         _cleanup_free_ char *sum, *sus, *him, *his, *hym, *hys;
26 
27         sum = strv_join(sleep_config->modes[SLEEP_SUSPEND], ", ");
28         sus = strv_join(sleep_config->states[SLEEP_SUSPEND], ", ");
29         him = strv_join(sleep_config->modes[SLEEP_HIBERNATE], ", ");
30         his = strv_join(sleep_config->states[SLEEP_HIBERNATE], ", ");
31         hym = strv_join(sleep_config->modes[SLEEP_HYBRID_SLEEP], ", ");
32         hys = strv_join(sleep_config->states[SLEEP_HYBRID_SLEEP], ", ");
33         log_debug("  allow_suspend: %u", sleep_config->allow[SLEEP_SUSPEND]);
34         log_debug("  allow_hibernate: %u", sleep_config->allow[SLEEP_HIBERNATE]);
35         log_debug("  allow_s2h: %u", sleep_config->allow[SLEEP_SUSPEND_THEN_HIBERNATE]);
36         log_debug("  allow_hybrid_sleep: %u", sleep_config->allow[SLEEP_HYBRID_SLEEP]);
37         log_debug("  suspend modes: %s", sum);
38         log_debug("         states: %s", sus);
39         log_debug("  hibernate modes: %s", him);
40         log_debug("           states: %s", his);
41         log_debug("  hybrid modes: %s", hym);
42         log_debug("        states: %s", hys);
43 }
44 
test_fiemap_one(const char * path)45 static int test_fiemap_one(const char *path) {
46         _cleanup_free_ struct fiemap *fiemap = NULL;
47         _cleanup_close_ int fd = -1;
48         int r;
49 
50         log_info("/* %s */", __func__);
51 
52         fd = open(path, O_RDONLY | O_CLOEXEC | O_NONBLOCK);
53         if (fd < 0)
54                 return log_error_errno(errno, "failed to open %s: %m", path);
55         r = read_fiemap(fd, &fiemap);
56         if (r == -EOPNOTSUPP)
57                 exit(log_tests_skipped("Not supported"));
58         if (r < 0)
59                 return log_error_errno(r, "Unable to read extent map for '%s': %m", path);
60         log_info("extent map information for %s:", path);
61         log_info("\t start: %" PRIu64, (uint64_t) fiemap->fm_start);
62         log_info("\t length: %" PRIu64, (uint64_t) fiemap->fm_length);
63         log_info("\t flags: %" PRIu32, fiemap->fm_flags);
64         log_info("\t number of mapped extents: %" PRIu32, fiemap->fm_mapped_extents);
65         log_info("\t extent count: %" PRIu32, fiemap->fm_extent_count);
66         if (fiemap->fm_extent_count > 0)
67                 log_info("\t first extent location: %" PRIu64,
68                          (uint64_t) (fiemap->fm_extents[0].fe_physical / page_size()));
69 
70         return 0;
71 }
72 
TEST_RET(fiemap)73 TEST_RET(fiemap) {
74         int r = 0;
75 
76         assert_se(test_fiemap_one(saved_argv[0]) == 0);
77         for (int i = 1; i < saved_argc; i++) {
78                 int k = test_fiemap_one(saved_argv[i]);
79                 if (r == 0)
80                         r = k;
81         }
82 
83         return r;
84 }
85 
TEST(sleep)86 TEST(sleep) {
87         _cleanup_strv_free_ char
88                 **standby = strv_new("standby"),
89                 **mem = strv_new("mem"),
90                 **disk = strv_new("disk"),
91                 **suspend = strv_new("suspend"),
92                 **reboot = strv_new("reboot"),
93                 **platform = strv_new("platform"),
94                 **shutdown = strv_new("shutdown"),
95                 **freeze = strv_new("freeze");
96         int r;
97 
98         printf("Secure boot: %sd\n", enable_disable(is_efi_secure_boot()));
99 
100         log_info("/= individual sleep modes =/");
101         log_info("Standby configured: %s", yes_no(can_sleep_state(standby) > 0));
102         log_info("Suspend configured: %s", yes_no(can_sleep_state(mem) > 0));
103         log_info("Hibernate configured: %s", yes_no(can_sleep_state(disk) > 0));
104         log_info("Hibernate+Suspend (Hybrid-Sleep) configured: %s", yes_no(can_sleep_disk(suspend) > 0));
105         log_info("Hibernate+Reboot configured: %s", yes_no(can_sleep_disk(reboot) > 0));
106         log_info("Hibernate+Platform configured: %s", yes_no(can_sleep_disk(platform) > 0));
107         log_info("Hibernate+Shutdown configured: %s", yes_no(can_sleep_disk(shutdown) > 0));
108         log_info("Freeze configured: %s", yes_no(can_sleep_state(freeze) > 0));
109 
110         log_info("/= high-level sleep verbs =/");
111         r = can_sleep(SLEEP_SUSPEND);
112         log_info("Suspend configured and possible: %s", r >= 0 ? yes_no(r) : strerror_safe(r));
113         r = can_sleep(SLEEP_HIBERNATE);
114         log_info("Hibernation configured and possible: %s", r >= 0 ? yes_no(r) : strerror_safe(r));
115         r = can_sleep(SLEEP_HYBRID_SLEEP);
116         log_info("Hybrid-sleep configured and possible: %s", r >= 0 ? yes_no(r) : strerror_safe(r));
117         r = can_sleep(SLEEP_SUSPEND_THEN_HIBERNATE);
118         log_info("Suspend-then-Hibernate configured and possible: %s", r >= 0 ? yes_no(r) : strerror_safe(r));
119 }
120 
intro(void)121 static int intro(void) {
122         if (getuid() != 0)
123                 log_warning("This program is unlikely to work for unprivileged users");
124 
125         return EXIT_SUCCESS;
126 }
127 
128 DEFINE_TEST_MAIN_WITH_INTRO(LOG_DEBUG, intro);
129