]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/boot/boot-check-no-failures.c
license: LGPL-2.1+ -> LGPL-2.1-or-later
[thirdparty/systemd.git] / src / boot / boot-check-no-failures.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
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 "main-func.h"
14 #include "pretty-print.h"
15 #include "terminal-util.h"
16 #include "util.h"
17
18 static int help(void) {
19 _cleanup_free_ char *link = NULL;
20 int r;
21
22 r = terminal_urlify_man("systemd-boot-check-no-failures.service", "8", &link);
23 if (r < 0)
24 return log_oom();
25
26 printf("%s [OPTIONS...]\n"
27 "\n%sVerify system operational state.%s\n\n"
28 " -h --help Show this help\n"
29 " --version Print version\n"
30 "\nSee the %s for details.\n"
31 , program_invocation_short_name
32 , ansi_highlight()
33 , ansi_normal()
34 , link
35 );
36
37 return 0;
38 }
39
40 static int parse_argv(int argc, char *argv[]) {
41 enum {
42 ARG_PATH = 0x100,
43 ARG_VERSION,
44 };
45
46 static const struct option options[] = {
47 { "help", no_argument, NULL, 'h' },
48 { "version", no_argument, NULL, ARG_VERSION },
49 {}
50 };
51
52 int c;
53
54 assert(argc >= 0);
55 assert(argv);
56
57 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
58 switch (c) {
59
60 case 'h':
61 help();
62 return 0;
63
64 case ARG_VERSION:
65 return version();
66
67 case '?':
68 return -EINVAL;
69
70 default:
71 assert_not_reached("Unknown option");
72 }
73
74 return 1;
75 }
76
77 static int run(int argc, char *argv[]) {
78 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
79 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
80 uint32_t n;
81 int r;
82
83 log_parse_environment();
84 log_open();
85
86 r = parse_argv(argc, argv);
87 if (r <= 0)
88 return r;
89
90 r = sd_bus_open_system(&bus);
91 if (r < 0)
92 return log_error_errno(r, "Failed to connect to system bus: %m");
93
94 r = sd_bus_get_property_trivial(
95 bus,
96 "org.freedesktop.systemd1",
97 "/org/freedesktop/systemd1",
98 "org.freedesktop.systemd1.Manager",
99 "NFailedUnits",
100 &error,
101 'u',
102 &n);
103 if (r < 0)
104 return log_error_errno(r, "Failed to get failed units counter: %s", bus_error_message(&error, r));
105
106 if (n > 0)
107 log_notice("Health check: %" PRIu32 " units have failed.", n);
108 else
109 log_info("Health check: no failed units.");
110
111 return n > 0;
112 }
113
114 DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);