]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udevadm.c
Merge pull request #15891 from bluca/host_os_release
[thirdparty/systemd.git] / src / udev / udevadm.c
1 /* SPDX-License-Identifier: GPL-2.0+ */
2
3 #include <errno.h>
4 #include <getopt.h>
5 #include <stddef.h>
6 #include <stdio.h>
7
8 #include "alloc-util.h"
9 #include "main-func.h"
10 #include "pretty-print.h"
11 #include "selinux-util.h"
12 #include "string-util.h"
13 #include "udevadm.h"
14 #include "udevd.h"
15 #include "udev-util.h"
16 #include "verbs.h"
17 #include "util.h"
18
19 static int help(void) {
20 static const char *const short_descriptions[][2] = {
21 { "info", "Query sysfs or the udev database" },
22 { "trigger", "Request events from the kernel" },
23 { "settle", "Wait for pending udev events" },
24 { "control", "Control the udev daemon" },
25 { "monitor", "Listen to kernel and udev events" },
26 { "test", "Test an event run" },
27 { "test-builtin", "Test a built-in command" },
28 };
29
30 _cleanup_free_ char *link = NULL;
31 size_t i;
32 int r;
33
34 r = terminal_urlify_man("udevadm", "8", &link);
35 if (r < 0)
36 return log_oom();
37
38 printf("%s [--help] [--version] [--debug] COMMAND [COMMAND OPTIONS]\n\n"
39 "Send control commands or test the device manager.\n\n"
40 "Commands:\n"
41 , program_invocation_short_name);
42
43 for (i = 0; i < ELEMENTSOF(short_descriptions); i++)
44 printf(" %-12s %s\n", short_descriptions[i][0], short_descriptions[i][1]);
45
46 printf("\nSee the %s for details.\n", link);
47 return 0;
48 }
49
50 static int parse_argv(int argc, char *argv[]) {
51 static const struct option options[] = {
52 { "debug", no_argument, NULL, 'd' },
53 { "help", no_argument, NULL, 'h' },
54 { "version", no_argument, NULL, 'V' },
55 {}
56 };
57 int c;
58
59 assert(argc >= 0);
60 assert(argv);
61
62 while ((c = getopt_long(argc, argv, "+dhV", options, NULL)) >= 0)
63 switch (c) {
64
65 case 'd':
66 log_set_max_level(LOG_DEBUG);
67 break;
68
69 case 'h':
70 return help();
71
72 case 'V':
73 return print_version();
74
75 case '?':
76 return -EINVAL;
77
78 default:
79 assert_not_reached("Unhandled option");
80 }
81
82 return 1; /* work to do */
83 }
84
85 static int version_main(int argc, char *argv[], void *userdata) {
86 return print_version();
87 }
88
89 static int help_main(int argc, char *argv[], void *userdata) {
90 return help();
91 }
92
93 static int udevadm_main(int argc, char *argv[]) {
94 static const Verb verbs[] = {
95 { "info", VERB_ANY, VERB_ANY, 0, info_main },
96 { "trigger", VERB_ANY, VERB_ANY, 0, trigger_main },
97 { "settle", VERB_ANY, VERB_ANY, 0, settle_main },
98 { "control", VERB_ANY, VERB_ANY, 0, control_main },
99 { "monitor", VERB_ANY, VERB_ANY, 0, monitor_main },
100 { "hwdb", VERB_ANY, VERB_ANY, 0, hwdb_main },
101 { "test", VERB_ANY, VERB_ANY, 0, test_main },
102 { "test-builtin", VERB_ANY, VERB_ANY, 0, builtin_main },
103 { "version", VERB_ANY, VERB_ANY, 0, version_main },
104 { "help", VERB_ANY, VERB_ANY, 0, help_main },
105 {}
106 };
107
108 return dispatch_verb(argc, argv, verbs, NULL);
109 }
110
111 static int run(int argc, char *argv[]) {
112 int r;
113
114 if (strstr(program_invocation_short_name, "udevd"))
115 return run_udevd(argc, argv);
116
117 udev_parse_config();
118 log_parse_environment();
119 log_open();
120
121 r = parse_argv(argc, argv);
122 if (r <= 0)
123 return r;
124
125 log_set_max_level_realm(LOG_REALM_SYSTEMD, log_get_max_level());
126
127 r = mac_selinux_init();
128 if (r < 0)
129 return r;
130
131 return udevadm_main(argc, argv);
132 }
133
134 DEFINE_MAIN_FUNCTION(run);