]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/boot/boot-check-no-failures.c
Merge pull request #11827 from keszybz/pkgconfig-variables
[thirdparty/systemd.git] / src / boot / boot-check-no-failures.c
CommitLineData
f876f537
LP
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"
5e332028 13#include "main-func.h"
f876f537
LP
14#include "util.h"
15
16static int help(void) {
17
18 printf("%s [COMMAND] [OPTIONS...]\n"
19 "\n"
20 "Verify system operational state.\n\n"
21 " -h --help Show this help\n"
22 " --version Print version\n",
23 program_invocation_short_name);
24
25 return 0;
26}
27
28static int parse_argv(int argc, char *argv[]) {
29 enum {
30 ARG_PATH = 0x100,
31 ARG_VERSION,
32 };
33
34 static const struct option options[] = {
35 { "help", no_argument, NULL, 'h' },
36 { "version", no_argument, NULL, ARG_VERSION },
37 {}
38 };
39
40 int c;
41
42 assert(argc >= 0);
43 assert(argv);
44
45 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
46 switch (c) {
47
48 case 'h':
49 help();
50 return 0;
51
52 case ARG_VERSION:
53 return version();
54
55 case '?':
56 return -EINVAL;
57
58 default:
59 assert_not_reached("Unknown option");
60 }
61
62 return 1;
63}
64
02434ea2 65static int run(int argc, char *argv[]) {
f876f537
LP
66 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
67 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
68 uint32_t n;
69 int r;
70
71 log_parse_environment();
72 log_open();
73
74 r = parse_argv(argc, argv);
75 if (r <= 0)
02434ea2 76 return r;
f876f537
LP
77
78 r = sd_bus_open_system(&bus);
02434ea2
ZJS
79 if (r < 0)
80 return log_error_errno(r, "Failed to connect to system bus: %m");
f876f537
LP
81
82 r = sd_bus_get_property_trivial(
83 bus,
84 "org.freedesktop.systemd1",
85 "/org/freedesktop/systemd1",
86 "org.freedesktop.systemd1.Manager",
87 "NFailedUnits",
88 &error,
89 'u',
90 &n);
02434ea2
ZJS
91 if (r < 0)
92 return log_error_errno(r, "Failed to get failed units counter: %s", bus_error_message(&error, r));
f876f537
LP
93
94 if (n > 0)
95 log_notice("Health check: %" PRIu32 " units have failed.", n);
96 else
97 log_info("Health check: no failed units.");
98
02434ea2 99 return n > 0;
f876f537 100}
02434ea2
ZJS
101
102DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);