]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-sleep.c
Merge pull request #8575 from keszybz/non-absolute-paths
[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
8 #include <linux/fiemap.h>
9 #include <stdio.h>
10
11 #include "fd-util.h"
12 #include "log.h"
13 #include "sleep-config.h"
14 #include "strv.h"
15 #include "util.h"
16
17 static int test_fiemap(const char *path) {
18 _cleanup_free_ struct fiemap *fiemap = NULL;
19 _cleanup_close_ int fd = -1;
20 int r;
21
22 fd = open(path, O_RDONLY | O_CLOEXEC | O_NONBLOCK);
23 if (fd < 0)
24 return log_error_errno(errno, "failed to open %s: %m", path);
25 r = read_fiemap(fd, &fiemap);
26 if (r == -ENOTSUP) {
27 log_info("Skipping test, not supported");
28 exit(EXIT_TEST_SKIP);
29 }
30 if (r < 0)
31 return log_error_errno(r, "Unable to read extent map for '%s': %m", path);
32 log_info("extent map information for %s:", path);
33 log_info("\t start: %llu", fiemap->fm_start);
34 log_info("\t length: %llu", fiemap->fm_length);
35 log_info("\t flags: %u", fiemap->fm_flags);
36 log_info("\t number of mapped extents: %u", fiemap->fm_mapped_extents);
37 log_info("\t extent count: %u", fiemap->fm_extent_count);
38 if (fiemap->fm_extent_count > 0)
39 log_info("\t first extent location: %llu",
40 fiemap->fm_extents[0].fe_physical / page_size());
41
42 return 0;
43 }
44
45 static void test_sleep(void) {
46 _cleanup_strv_free_ char
47 **standby = strv_new("standby", NULL),
48 **mem = strv_new("mem", NULL),
49 **disk = strv_new("disk", NULL),
50 **suspend = strv_new("suspend", NULL),
51 **reboot = strv_new("reboot", NULL),
52 **platform = strv_new("platform", NULL),
53 **shutdown = strv_new("shutdown", NULL),
54 **freez = strv_new("freeze", NULL);
55 int r;
56
57 log_info("/* configuration */");
58 log_info("Standby configured: %s", yes_no(can_sleep_state(standby) > 0));
59 log_info("Suspend configured: %s", yes_no(can_sleep_state(mem) > 0));
60 log_info("Hibernate configured: %s", yes_no(can_sleep_state(disk) > 0));
61 log_info("Hibernate+Suspend (Hybrid-Sleep) configured: %s", yes_no(can_sleep_disk(suspend) > 0));
62 log_info("Hibernate+Reboot configured: %s", yes_no(can_sleep_disk(reboot) > 0));
63 log_info("Hibernate+Platform configured: %s", yes_no(can_sleep_disk(platform) > 0));
64 log_info("Hibernate+Shutdown configured: %s", yes_no(can_sleep_disk(shutdown) > 0));
65 log_info("Freeze configured: %s", yes_no(can_sleep_state(freez) > 0));
66
67 log_info("/* running system */");
68 r = can_sleep("suspend");
69 log_info("Suspend configured and possible: %s", r >= 0 ? yes_no(r) : strerror(-r));
70 r = can_sleep("hibernate");
71 log_info("Hibernation configured and possible: %s", r >= 0 ? yes_no(r) : strerror(-r));
72 r = can_sleep("hybrid-sleep");
73 log_info("Hybrid-sleep configured and possible: %s", r >= 0 ? yes_no(r) : strerror(-r));
74 r = can_sleep("suspend-then-hibernate");
75 log_info("Suspend-then-Hibernate configured and possible: %s", r >= 0 ? yes_no(r) : strerror(-r));
76 }
77
78 int main(int argc, char* argv[]) {
79 int i, r = 0, k;
80
81 log_parse_environment();
82 log_open();
83
84 if (getuid() != 0)
85 log_warning("This program is unlikely to work for unprivileged users");
86
87 test_sleep();
88
89 if (argc <= 1)
90 assert_se(test_fiemap(argv[0]) == 0);
91 else
92 for (i = 1; i < argc; i++) {
93 k = test_fiemap(argv[i]);
94 if (r == 0)
95 r = k;
96 }
97
98 return r;
99 }