]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-sleep.c
Merge pull request #8406 from dell/hibernate-disk-offset
[thirdparty/systemd.git] / src / test / test-sleep.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2012 Lennart Poettering
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include <linux/fiemap.h>
22 #include <stdio.h>
23
24 #include "fd-util.h"
25 #include "log.h"
26 #include "sleep-config.h"
27 #include "strv.h"
28 #include "util.h"
29
30 static int test_fiemap(const char *path) {
31 _cleanup_free_ struct fiemap *fiemap = NULL;
32 _cleanup_close_ int fd = -1;
33 int r;
34
35 fd = open(path, O_RDONLY | O_CLOEXEC | O_NONBLOCK);
36 if (fd < 0)
37 return log_error_errno(errno, "failed to open %s: %m", path);
38 r = read_fiemap(fd, &fiemap);
39 if (r == -ENOTSUP) {
40 log_info("Skipping test, not supported");
41 exit(EXIT_TEST_SKIP);
42 }
43 if (r < 0)
44 return log_error_errno(r, "Unable to read extent map for '%s': %m", path);
45 log_info("extent map information for %s:", path);
46 log_info("\t start: %llu", fiemap->fm_start);
47 log_info("\t length: %llu", fiemap->fm_length);
48 log_info("\t flags: %u", fiemap->fm_flags);
49 log_info("\t number of mapped extents: %u", fiemap->fm_mapped_extents);
50 log_info("\t extent count: %u", fiemap->fm_extent_count);
51 if (fiemap->fm_extent_count > 0)
52 log_info("\t first extent location: %llu",
53 fiemap->fm_extents[0].fe_physical / page_size());
54
55 return 0;
56 }
57
58 static void test_sleep(void) {
59 _cleanup_strv_free_ char
60 **standby = strv_new("standby", NULL),
61 **mem = strv_new("mem", NULL),
62 **disk = strv_new("disk", NULL),
63 **suspend = strv_new("suspend", NULL),
64 **reboot = strv_new("reboot", NULL),
65 **platform = strv_new("platform", NULL),
66 **shutdown = strv_new("shutdown", NULL),
67 **freez = strv_new("freeze", NULL);
68
69 log_info("Standby configured: %s", yes_no(can_sleep_state(standby) > 0));
70 log_info("Suspend configured: %s", yes_no(can_sleep_state(mem) > 0));
71 log_info("Hibernate configured: %s", yes_no(can_sleep_state(disk) > 0));
72 log_info("Hibernate+Suspend (Hybrid-Sleep) configured: %s", yes_no(can_sleep_disk(suspend) > 0));
73 log_info("Hibernate+Reboot configured: %s", yes_no(can_sleep_disk(reboot) > 0));
74 log_info("Hibernate+Platform configured: %s", yes_no(can_sleep_disk(platform) > 0));
75 log_info("Hibernate+Shutdown configured: %s", yes_no(can_sleep_disk(shutdown) > 0));
76 log_info("Freeze configured: %s", yes_no(can_sleep_state(freez) > 0));
77
78 log_info("Suspend configured and possible: %s", yes_no(can_sleep("suspend") > 0));
79 log_info("Hibernation configured and possible: %s", yes_no(can_sleep("hibernate") > 0));
80 log_info("Hybrid-sleep configured and possible: %s", yes_no(can_sleep("hybrid-sleep") > 0));
81 log_info("Suspend-then-Hibernate configured and possible: %s", yes_no(can_sleep("suspend-then-hibernate") > 0));
82 }
83
84 int main(int argc, char* argv[]) {
85 int i, r = 0, k;
86
87 log_parse_environment();
88 log_open();
89
90 if (getuid() != 0)
91 log_warning("This program is unlikely to work for unprivileged users");
92
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 }