]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-boot-timestamps.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / test / test-boot-timestamps.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
c51d84dc
KS
2/***
3 This file is part of systemd.
4
5 Copyright 2013 Lennart Poettering
6 Copyright 2013 Kay Sievers
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
cf0fbc49 22#include "acpi-fpdt.h"
c51d84dc
KS
23#include "boot-timestamps.h"
24#include "efivars.h"
cf0fbc49
TA
25#include "log.h"
26#include "util.h"
c51d84dc
KS
27
28static int test_acpi_fpdt(void) {
29 usec_t loader_start;
30 usec_t loader_exit;
31 char ts_start[FORMAT_TIMESPAN_MAX];
32 char ts_exit[FORMAT_TIMESPAN_MAX];
33 char ts_span[FORMAT_TIMESPAN_MAX];
34 int r;
35
36 r = acpi_get_boot_usec(&loader_start, &loader_exit);
37 if (r < 0) {
67a47c60 38 bool ok = r == -ENOENT || (getuid() != 0 && r == -EACCES) || r == -ENODATA;
a4bfedec
ZJS
39
40 log_full_errno(ok ? LOG_DEBUG : LOG_ERR,
41 r, "Failed to read ACPI FPDT: %m");
42 return ok ? 0 : r;
c51d84dc
KS
43 }
44
45 log_info("ACPI FPDT: loader start=%s exit=%s duration=%s",
46 format_timespan(ts_start, sizeof(ts_start), loader_start, USEC_PER_MSEC),
47 format_timespan(ts_exit, sizeof(ts_exit), loader_exit, USEC_PER_MSEC),
48 format_timespan(ts_span, sizeof(ts_span), loader_exit - loader_start, USEC_PER_MSEC));
a4bfedec 49 return 1;
c51d84dc
KS
50}
51
52static int test_efi_loader(void) {
53 usec_t loader_start;
54 usec_t loader_exit;
55 char ts_start[FORMAT_TIMESPAN_MAX];
56 char ts_exit[FORMAT_TIMESPAN_MAX];
57 char ts_span[FORMAT_TIMESPAN_MAX];
58 int r;
59
60 r = efi_loader_get_boot_usec(&loader_start, &loader_exit);
61 if (r < 0) {
a4bfedec
ZJS
62 bool ok = r == -ENOENT || (getuid() != 0 && r == -EACCES);
63
64 log_full_errno(ok ? LOG_DEBUG : LOG_ERR,
65 r, "Failed to read EFI loader data: %m");
66 return ok ? 0 : r;
c51d84dc
KS
67 }
68
69 log_info("EFI Loader: start=%s exit=%s duration=%s",
70 format_timespan(ts_start, sizeof(ts_start), loader_start, USEC_PER_MSEC),
71 format_timespan(ts_exit, sizeof(ts_exit), loader_exit, USEC_PER_MSEC),
72 format_timespan(ts_span, sizeof(ts_span), loader_exit - loader_start, USEC_PER_MSEC));
a4bfedec 73 return 1;
c51d84dc
KS
74}
75
a4bfedec 76static int test_boot_timestamps(void) {
c51d84dc
KS
77 char s[MAX(FORMAT_TIMESPAN_MAX, FORMAT_TIMESTAMP_MAX)];
78 int r;
79 dual_timestamp fw, l, k;
80
c51d84dc
KS
81 dual_timestamp_from_monotonic(&k, 0);
82
83 r = boot_timestamps(NULL, &fw, &l);
84 if (r < 0) {
a4bfedec
ZJS
85 bool ok = r == -ENOENT || (getuid() != 0 && r == -EACCES);
86
87 log_full_errno(ok ? LOG_DEBUG : LOG_ERR,
88 r, "Failed to read variables: %m");
89 return ok ? 0 : r;
c51d84dc
KS
90 }
91
92 log_info("Firmware began %s before kernel.", format_timespan(s, sizeof(s), fw.monotonic, 0));
93 log_info("Loader began %s before kernel.", format_timespan(s, sizeof(s), l.monotonic, 0));
94 log_info("Firmware began %s.", format_timestamp(s, sizeof(s), fw.realtime));
95 log_info("Loader began %s.", format_timestamp(s, sizeof(s), l.realtime));
96 log_info("Kernel began %s.", format_timestamp(s, sizeof(s), k.realtime));
a4bfedec
ZJS
97 return 1;
98}
99
100int main(int argc, char* argv[]) {
101 int p, q, r;
102
103 log_set_max_level(LOG_DEBUG);
104 log_parse_environment();
105
106 p = test_acpi_fpdt();
107 assert(p >= 0);
108 q = test_efi_loader();
109 assert(q >= 0);
110 r = test_boot_timestamps();
111 assert(r >= 0);
c51d84dc 112
a4bfedec 113 return (p > 0 || q > 0 || r >> 0) ? EXIT_SUCCESS : EXIT_TEST_SKIP;
c51d84dc 114}