| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <stdio.h> |
| 4 |
|
| 5 |
#include "sd-bus.h" |
| 6 |
|
| 7 |
#include "alloc-util.h" |
| 8 |
#include "build.h" |
| 9 |
#include "bus-error.h" |
| 10 |
#include "format-table.h" |
| 11 |
#include "log.h" |
| 12 |
#include "main-func.h" |
| 13 |
#include "options.h" |
| 14 |
#include "pretty-print.h" |
| 15 |
|
| 16 |
static int help(void) { |
| 17 |
_cleanup_free_ char *link = NULL; |
| 18 |
_cleanup_(table_unrefp) Table *options = NULL; |
| 19 |
int r; |
| 20 |
|
| 21 |
r = terminal_urlify_man("systemd-boot-check-no-failures.service", "8", &link); |
| 22 |
if (r < 0) |
| 23 |
return log_oom(); |
| 24 |
|
| 25 |
r = option_parser_get_help_table(&options); |
| 26 |
if (r < 0) |
| 27 |
return r; |
| 28 |
|
| 29 |
printf("%s [OPTIONS...]\n" |
| 30 |
"\n%sVerify system operational state.%s\n\n", |
| 31 |
program_invocation_short_name, |
| 32 |
ansi_highlight(), |
| 33 |
ansi_normal()); |
| 34 |
|
| 35 |
r = table_print_or_warn(options); |
| 36 |
if (r < 0) |
| 37 |
return r; |
| 38 |
|
| 39 |
printf("\nSee the %s for details.\n", link); |
| 40 |
return 0; |
| 41 |
} |
| 42 |
|
| 43 |
static int parse_argv(int argc, char *argv[]) { |
| 44 |
assert(argc >= 0); |
| 45 |
assert(argv); |
| 46 |
|
| 47 |
OptionParser opts = { argc, argv }; |
| 48 |
|
| 49 |
FOREACH_OPTION_OR_RETURN(c, &opts) |
| 50 |
switch (c) { |
| 51 |
|
| 52 |
OPTION_COMMON_HELP: |
| 53 |
return help(); |
| 54 |
|
| 55 |
OPTION_COMMON_VERSION: |
| 56 |
return version(); |
| 57 |
} |
| 58 |
|
| 59 |
return 1; |
| 60 |
} |
| 61 |
|
| 62 |
static int run(int argc, char *argv[]) { |
| 63 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 64 |
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
| 65 |
uint32_t n; |
| 66 |
int r; |
| 67 |
|
| 68 |
log_setup(); |
| 69 |
|
| 70 |
r = parse_argv(argc, argv); |
| 71 |
if (r <= 0) |
| 72 |
return r; |
| 73 |
|
| 74 |
r = sd_bus_open_system(&bus); |
| 75 |
if (r < 0) |
| 76 |
return log_error_errno(r, "Failed to connect to system bus: %m"); |
| 77 |
|
| 78 |
r = sd_bus_get_property_trivial( |
| 79 |
bus, |
| 80 |
"org.freedesktop.systemd1", |
| 81 |
"/org/freedesktop/systemd1", |
| 82 |
"org.freedesktop.systemd1.Manager", |
| 83 |
"NFailedUnits", |
| 84 |
&error, |
| 85 |
'u', |
| 86 |
&n); |
| 87 |
if (r < 0) |
| 88 |
return log_error_errno(r, "Failed to get failed units counter: %s", bus_error_message(&error, r)); |
| 89 |
|
| 90 |
if (n > 0) |
| 91 |
log_notice("Health check: %" PRIu32 " units have failed.", n); |
| 92 |
else |
| 93 |
log_info("Health check: no failed units."); |
| 94 |
|
| 95 |
return n > 0; |
| 96 |
} |
| 97 |
|
| 98 |
DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run); |