]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/boot/boot-check-no-failures.c
pkgconfig: define variables relative to ${prefix}/${rootprefix}/${sysconfdir}
[thirdparty/systemd.git] / src / boot / boot-check-no-failures.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <errno.h>
4 #include <getopt.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7
8 #include "sd-bus.h"
9
10 #include "alloc-util.h"
11 #include "bus-error.h"
12 #include "log.h"
13 #include "util.h"
14
15 static int help(void) {
16
17 printf("%s [COMMAND] [OPTIONS...]\n"
18 "\n"
19 "Verify system operational state.\n\n"
20 " -h --help Show this help\n"
21 " --version Print version\n",
22 program_invocation_short_name);
23
24 return 0;
25 }
26
27 static int parse_argv(int argc, char *argv[]) {
28 enum {
29 ARG_PATH = 0x100,
30 ARG_VERSION,
31 };
32
33 static const struct option options[] = {
34 { "help", no_argument, NULL, 'h' },
35 { "version", no_argument, NULL, ARG_VERSION },
36 {}
37 };
38
39 int c;
40
41 assert(argc >= 0);
42 assert(argv);
43
44 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
45 switch (c) {
46
47 case 'h':
48 help();
49 return 0;
50
51 case ARG_VERSION:
52 return version();
53
54 case '?':
55 return -EINVAL;
56
57 default:
58 assert_not_reached("Unknown option");
59 }
60
61 return 1;
62 }
63
64 int main(int argc, char *argv[]) {
65 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
66 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
67 uint32_t n;
68 int r;
69
70 log_parse_environment();
71 log_open();
72
73 r = parse_argv(argc, argv);
74 if (r <= 0)
75 goto finish;
76
77 r = sd_bus_open_system(&bus);
78 if (r < 0) {
79 log_error_errno(r, "Failed to connect to system bus: %m");
80 goto finish;
81 }
82
83 r = sd_bus_get_property_trivial(
84 bus,
85 "org.freedesktop.systemd1",
86 "/org/freedesktop/systemd1",
87 "org.freedesktop.systemd1.Manager",
88 "NFailedUnits",
89 &error,
90 'u',
91 &n);
92 if (r < 0) {
93 log_error_errno(r, "Failed to get failed units counter: %s", bus_error_message(&error, r));
94 goto finish;
95 }
96
97 if (n > 0)
98 log_notice("Health check: %" PRIu32 " units have failed.", n);
99 else
100 log_info("Health check: no failed units.");
101
102 r = n > 0 ? EXIT_FAILURE : EXIT_SUCCESS;
103
104 finish:
105 return r < 0 ? EXIT_FAILURE : r;
106 }