| 1 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 |
|
| 3 |
#include <stdio.h> |
| 4 |
|
| 5 |
#include "argv-util.h" |
| 6 |
#include "format-table.h" |
| 7 |
#include "help-util.h" |
| 8 |
#include "label-util.h" |
| 9 |
#include "main-func.h" |
| 10 |
#include "options.h" |
| 11 |
#include "udev-util.h" |
| 12 |
#include "udevadm.h" |
| 13 |
#include "udevd.h" |
| 14 |
#include "verbs.h" |
| 15 |
|
| 16 |
static int help(void) { |
| 17 |
_cleanup_(table_unrefp) Table *verbs = NULL, *options = NULL; |
| 18 |
int r; |
| 19 |
|
| 20 |
r = verbs_get_help_table(&verbs); |
| 21 |
if (r < 0) |
| 22 |
return r; |
| 23 |
|
| 24 |
r = option_parser_get_help_table_ns("udevadm", &options); |
| 25 |
if (r < 0) |
| 26 |
return r; |
| 27 |
|
| 28 |
(void) table_sync_column_widths(0, verbs, options); |
| 29 |
|
| 30 |
help_cmdline("[OPTIONS…] COMMAND [COMMAND OPTIONS…]"); |
| 31 |
help_abstract("Send control commands or test the device manager."); |
| 32 |
|
| 33 |
help_section("Commands"); |
| 34 |
r = table_print_or_warn(verbs); |
| 35 |
if (r < 0) |
| 36 |
return r; |
| 37 |
|
| 38 |
help_section("Options"); |
| 39 |
r = table_print_or_warn(options); |
| 40 |
if (r < 0) |
| 41 |
return r; |
| 42 |
|
| 43 |
help_man_page_reference("udevadm", "8"); |
| 44 |
return 0; |
| 45 |
} |
| 46 |
|
| 47 |
VERB_COMMON_HELP(help); |
| 48 |
|
| 49 |
VERB_SCOPE(, verb_info_main, "info", "[DEVPATH|FILE]", VERB_ANY, VERB_ANY, 0, "Query sysfs or the udev database"); |
| 50 |
VERB_SCOPE(, verb_trigger_main, "trigger", "DEVPATH", VERB_ANY, VERB_ANY, 0, "Request events from the kernel"); |
| 51 |
VERB_SCOPE(, verb_settle_main, "settle", NULL, VERB_ANY, VERB_ANY, 0, "Wait for pending udev events"); |
| 52 |
VERB_SCOPE(, verb_control_main, "control", "OPTION", VERB_ANY, VERB_ANY, 0, "Control the udev daemon"); |
| 53 |
VERB_SCOPE(, verb_monitor_main, "monitor", NULL, VERB_ANY, VERB_ANY, 0, "Listen to kernel and udev events"); |
| 54 |
VERB_SCOPE(, verb_test_main, "test", "DEVPATH", VERB_ANY, VERB_ANY, 0, "Test an event run"); |
| 55 |
VERB_SCOPE(, verb_builtin_main, "test-builtin", "COMMAND DEVPATH", VERB_ANY, VERB_ANY, 0, "Test a built-in command"); |
| 56 |
VERB_SCOPE(, verb_verify_main, "verify", "[FILE…]", VERB_ANY, VERB_ANY, 0, "Verify udev rules files"); |
| 57 |
VERB_SCOPE(, verb_cat_main, "cat", "[FILE…]", VERB_ANY, VERB_ANY, 0, "Show udev rules files"); |
| 58 |
VERB_SCOPE(, verb_wait_main, "wait", "DEVICE [DEVICE…]", VERB_ANY, VERB_ANY, 0, "Wait for device or device symlink"); |
| 59 |
VERB_SCOPE(, verb_lock_main, "lock", "[OPTIONS…] COMMAND", VERB_ANY, VERB_ANY, 0, "Lock a block device"); |
| 60 |
VERB_SCOPE(, verb_hwdb_main, "hwdb", NULL, VERB_ANY, VERB_ANY, 0, /* help= */ NULL); /* deprecated */ |
| 61 |
|
| 62 |
VERB_NOARG(verb_version_main, "version", /* help= */ NULL); |
| 63 |
static int verb_version_main(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 64 |
return print_version(); |
| 65 |
} |
| 66 |
|
| 67 |
static int parse_argv(int argc, char *argv[], char ***remaining_args) { |
| 68 |
assert(argc >= 0); |
| 69 |
assert(argv); |
| 70 |
assert(remaining_args); |
| 71 |
|
| 72 |
OptionParser opts = { argc, argv, OPTION_PARSER_STOP_AT_FIRST_NONOPTION, "udevadm" }; |
| 73 |
|
| 74 |
FOREACH_OPTION_OR_RETURN(c, &opts) |
| 75 |
switch (c) { |
| 76 |
|
| 77 |
OPTION_NAMESPACE("udevadm"): {} |
| 78 |
|
| 79 |
OPTION_COMMON_HELP: |
| 80 |
return help(); |
| 81 |
|
| 82 |
OPTION_COMMON_VERSION_WITH_HIDDEN_V: |
| 83 |
return print_version(); |
| 84 |
|
| 85 |
OPTION('d', "debug", NULL, "Enable debug logging"): |
| 86 |
log_set_max_level(LOG_DEBUG); |
| 87 |
break; |
| 88 |
} |
| 89 |
|
| 90 |
*remaining_args = option_parser_get_args(&opts); |
| 91 |
return 1; /* work to do */ |
| 92 |
} |
| 93 |
|
| 94 |
int print_version(void) { |
| 95 |
/* Dracut relies on the version being a single integer */ |
| 96 |
puts(PROJECT_VERSION_STR); |
| 97 |
return 0; |
| 98 |
} |
| 99 |
|
| 100 |
static int run(int argc, char *argv[]) { |
| 101 |
char **args = NULL; |
| 102 |
int r; |
| 103 |
|
| 104 |
if (invoked_as(argv, "udevd")) |
| 105 |
return run_udevd(argc, argv); |
| 106 |
|
| 107 |
(void) udev_parse_config(); |
| 108 |
log_setup(); |
| 109 |
|
| 110 |
r = parse_argv(argc, argv, &args); |
| 111 |
if (r <= 0) |
| 112 |
return r; |
| 113 |
|
| 114 |
r = mac_init(); |
| 115 |
if (r < 0) |
| 116 |
return r; |
| 117 |
|
| 118 |
return dispatch_verb(args, NULL); |
| 119 |
} |
| 120 |
|
| 121 |
DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run); |