| 1 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 |
|
| 3 |
#include <stdio.h> |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "ansi-color.h" |
| 7 |
#include "conf-files.h" |
| 8 |
#include "errno-util.h" |
| 9 |
#include "format-table.h" |
| 10 |
#include "help-util.h" |
| 11 |
#include "log.h" |
| 12 |
#include "options.h" |
| 13 |
#include "parse-argument.h" |
| 14 |
#include "static-destruct.h" |
| 15 |
#include "udev-rules.h" |
| 16 |
#include "udevadm.h" |
| 17 |
#include "udevadm-util.h" |
| 18 |
|
| 19 |
static ResolveNameTiming arg_resolve_name_timing = RESOLVE_NAME_EARLY; |
| 20 |
static char *arg_root = NULL; |
| 21 |
static bool arg_summary = true; |
| 22 |
static bool arg_style = true; |
| 23 |
|
| 24 |
STATIC_DESTRUCTOR_REGISTER(arg_root, freep); |
| 25 |
|
| 26 |
static int help(void) { |
| 27 |
_cleanup_(table_unrefp) Table *options = NULL; |
| 28 |
int r; |
| 29 |
|
| 30 |
r = option_parser_get_help_table_ns("udevadm-verify", &options); |
| 31 |
if (r < 0) |
| 32 |
return r; |
| 33 |
|
| 34 |
help_cmdline("verify [OPTIONS] [FILE...]"); |
| 35 |
help_abstract("Verify udev rules files."); |
| 36 |
help_section("Options"); |
| 37 |
r = table_print_or_warn(options); |
| 38 |
if (r < 0) |
| 39 |
return r; |
| 40 |
|
| 41 |
help_man_page_reference("udevadm", "8"); |
| 42 |
return 0; |
| 43 |
} |
| 44 |
|
| 45 |
static int parse_argv(int argc, char *argv[], char ***remaining_args) { |
| 46 |
int r; |
| 47 |
|
| 48 |
assert(argc >= 0); |
| 49 |
assert(argv); |
| 50 |
assert(remaining_args); |
| 51 |
|
| 52 |
OptionParser opts = { argc, argv, .namespace = "udevadm-verify" }; |
| 53 |
|
| 54 |
FOREACH_OPTION_OR_RETURN(c, &opts) |
| 55 |
switch (c) { |
| 56 |
|
| 57 |
OPTION_NAMESPACE("udevadm-verify"): {} |
| 58 |
|
| 59 |
OPTION_COMMON_HELP: |
| 60 |
return help(); |
| 61 |
|
| 62 |
OPTION('V', "version", NULL, "Show package version"): |
| 63 |
return print_version(); |
| 64 |
|
| 65 |
OPTION_COMMON_RESOLVE_NAMES: |
| 66 |
r = parse_resolve_name_timing(opts.arg, &arg_resolve_name_timing); |
| 67 |
if (r <= 0) |
| 68 |
return r; |
| 69 |
break; |
| 70 |
|
| 71 |
OPTION_LONG("root", "PATH", "Operate on an alternate filesystem root"): |
| 72 |
r = parse_path_argument(opts.arg, /* suppress_root= */ true, &arg_root); |
| 73 |
if (r < 0) |
| 74 |
return r; |
| 75 |
break; |
| 76 |
|
| 77 |
OPTION_LONG("no-summary", NULL, "Do not show summary"): |
| 78 |
arg_summary = false; |
| 79 |
break; |
| 80 |
|
| 81 |
OPTION_LONG("no-style", NULL, "Ignore style issues"): |
| 82 |
arg_style = false; |
| 83 |
break; |
| 84 |
} |
| 85 |
|
| 86 |
*remaining_args = option_parser_get_args(&opts); |
| 87 |
return 1; |
| 88 |
} |
| 89 |
|
| 90 |
static int verify_rules_file(UdevRules *rules, const ConfFile *c) { |
| 91 |
UdevRuleFile *file; |
| 92 |
int r; |
| 93 |
|
| 94 |
assert(rules); |
| 95 |
assert(c); |
| 96 |
|
| 97 |
r = udev_rules_parse_file(rules, c, /* extra_checks= */ true, &file); |
| 98 |
if (r < 0) |
| 99 |
return log_error_errno(r, "Failed to parse rules file %s: %m", c->original_path); |
| 100 |
|
| 101 |
unsigned issues = udev_rule_file_get_issues(file); |
| 102 |
unsigned mask = (1U << LOG_ERR) | (1U << LOG_WARNING); |
| 103 |
if (issues & mask) |
| 104 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 105 |
"%s: udev rules check failed.", c->original_path); |
| 106 |
|
| 107 |
if (arg_style && (issues & (1U << LOG_NOTICE))) |
| 108 |
return log_warning_errno(SYNTHETIC_ERRNO(EINVAL), |
| 109 |
"%s: udev rules have style issues.", c->original_path); |
| 110 |
|
| 111 |
return 0; |
| 112 |
} |
| 113 |
|
| 114 |
static int verify_rules(UdevRules *rules, ConfFile * const *files, size_t n_files) { |
| 115 |
size_t fail_count = 0, success_count = 0; |
| 116 |
int r, ret = 0; |
| 117 |
|
| 118 |
assert(rules); |
| 119 |
assert(files || n_files == 0); |
| 120 |
|
| 121 |
FOREACH_ARRAY(i, files, n_files) { |
| 122 |
r = verify_rules_file(rules, *i); |
| 123 |
if (r < 0) |
| 124 |
++fail_count; |
| 125 |
else |
| 126 |
++success_count; |
| 127 |
RET_GATHER(ret, r); |
| 128 |
} |
| 129 |
|
| 130 |
if (arg_summary) |
| 131 |
printf("\n%s%zu udev rules files have been checked.%s\n" |
| 132 |
" Success: %zu\n" |
| 133 |
"%s Fail: %zu%s\n", |
| 134 |
ansi_highlight(), |
| 135 |
fail_count + success_count, |
| 136 |
ansi_normal(), |
| 137 |
success_count, |
| 138 |
fail_count > 0 ? ansi_highlight_red() : "", |
| 139 |
fail_count, |
| 140 |
fail_count > 0 ? ansi_normal() : ""); |
| 141 |
|
| 142 |
return ret; |
| 143 |
} |
| 144 |
|
| 145 |
int verb_verify_main(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 146 |
_cleanup_(udev_rules_freep) UdevRules *rules = NULL; |
| 147 |
char **args = NULL; |
| 148 |
int r; |
| 149 |
|
| 150 |
r = parse_argv(argc, argv, &args); |
| 151 |
if (r <= 0) |
| 152 |
return r; |
| 153 |
|
| 154 |
rules = udev_rules_new(arg_resolve_name_timing); |
| 155 |
if (!rules) |
| 156 |
return -ENOMEM; |
| 157 |
|
| 158 |
ConfFile **files = NULL; |
| 159 |
size_t n_files = 0; |
| 160 |
|
| 161 |
CLEANUP_ARRAY(files, n_files, conf_file_free_array); |
| 162 |
|
| 163 |
r = search_rules_files(args, arg_root, &files, &n_files); |
| 164 |
if (r < 0) |
| 165 |
return r; |
| 166 |
|
| 167 |
return verify_rules(rules, files, n_files); |
| 168 |
} |