]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/udev/udevadm.c
Merge pull request #16504 from poettering/read-file-ipc
[thirdparty/systemd.git] / src / udev / udevadm.c
CommitLineData
e7145211 1/* SPDX-License-Identifier: GPL-2.0+ */
225cb03b 2
225cb03b
KS
3#include <errno.h>
4#include <getopt.h>
07630cea
LP
5#include <stddef.h>
6#include <stdio.h>
225cb03b 7
3d05193e 8#include "alloc-util.h"
138715dc 9#include "main-func.h"
294bf0c3 10#include "pretty-print.h"
d7b8eec7 11#include "selinux-util.h"
07630cea 12#include "string-util.h"
3d05193e 13#include "udevadm.h"
63e2d171 14#include "udevd.h"
b237a168 15#include "udev-util.h"
3d05193e
YW
16#include "verbs.h"
17#include "util.h"
18
19static int help(void) {
b82f71c7 20 static const char *const short_descriptions[][2] = {
3d05193e
YW
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 };
baa30fbc 29
37ec0fdd
LP
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();
912541b0 37
5ac0162c
LP
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
3d05193e
YW
43 for (i = 0; i < ELEMENTSOF(short_descriptions); i++)
44 printf(" %-12s %s\n", short_descriptions[i][0], short_descriptions[i][1]);
37ec0fdd
LP
45
46 printf("\nSee the %s for details.\n", link);
912541b0 47 return 0;
225cb03b
KS
48}
49
3d05193e 50static int parse_argv(int argc, char *argv[]) {
912541b0 51 static const struct option options[] = {
3d05193e
YW
52 { "debug", no_argument, NULL, 'd' },
53 { "help", no_argument, NULL, 'h' },
912541b0
KS
54 { "version", no_argument, NULL, 'V' },
55 {}
56 };
3d05193e 57 int c;
b237a168 58
3d05193e
YW
59 assert(argc >= 0);
60 assert(argv);
912541b0 61
601185b4
ZJS
62 while ((c = getopt_long(argc, argv, "+dhV", options, NULL)) >= 0)
63 switch (c) {
912541b0 64
912541b0 65 case 'd':
baa30fbc 66 log_set_max_level(LOG_DEBUG);
912541b0 67 break;
601185b4 68
912541b0 69 case 'h':
3d05193e 70 return help();
601185b4 71
912541b0 72 case 'V':
51b006e1 73 return print_version();
3d05193e
YW
74
75 case '?':
76 return -EINVAL;
601185b4 77
912541b0 78 default:
3d05193e 79 assert_not_reached("Unhandled option");
912541b0 80 }
601185b4 81
3d05193e
YW
82 return 1; /* work to do */
83}
84
85static int version_main(int argc, char *argv[], void *userdata) {
51b006e1 86 return print_version();
3d05193e
YW
87}
88
89static int help_main(int argc, char *argv[], void *userdata) {
90 return help();
91}
92
93static 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
138715dc 111static int run(int argc, char *argv[]) {
3d05193e
YW
112 int r;
113
63e2d171
NL
114 if (strstr(program_invocation_short_name, "udevd"))
115 return run_udevd(argc, argv);
116
3d05193e
YW
117 udev_parse_config();
118 log_parse_environment();
119 log_open();
3d05193e
YW
120
121 r = parse_argv(argc, argv);
122 if (r <= 0)
138715dc 123 return r;
3d05193e 124
b1d1cb5b
YW
125 log_set_max_level_realm(LOG_REALM_SYSTEMD, log_get_max_level());
126
a9ba0e32
CG
127 r = mac_selinux_init();
128 if (r < 0)
129 return r;
130
138715dc 131 return udevadm_main(argc, argv);
225cb03b 132}
138715dc
ZJS
133
134DEFINE_MAIN_FUNCTION(run);