]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/boot/boot-check-no-failures.c
tree-wide: clean up --help texts a bit
[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"
353b2baa
LP
14#include "pretty-print.h"
15#include "terminal-util.h"
f876f537
LP
16#include "util.h"
17
18static int help(void) {
353b2baa
LP
19 _cleanup_free_ char *link = NULL;
20 int r;
f876f537 21
353b2baa
LP
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"
f876f537 28 " -h --help Show this help\n"
353b2baa
LP
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 );
f876f537
LP
36
37 return 0;
38}
39
40static 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
02434ea2 77static int run(int argc, char *argv[]) {
f876f537
LP
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)
02434ea2 88 return r;
f876f537
LP
89
90 r = sd_bus_open_system(&bus);
02434ea2
ZJS
91 if (r < 0)
92 return log_error_errno(r, "Failed to connect to system bus: %m");
f876f537
LP
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);
02434ea2
ZJS
103 if (r < 0)
104 return log_error_errno(r, "Failed to get failed units counter: %s", bus_error_message(&error, r));
f876f537
LP
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
02434ea2 111 return n > 0;
f876f537 112}
02434ea2
ZJS
113
114DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);