Debian Codegraph @

src/udev/udevadm-monitor.c

raw
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2
3 #include "sd-device.h"
4 #include "sd-event.h"
5
6 #include "alloc-util.h"
7 #include "device-monitor-private.h"
8 #include "device-private.h"
9 #include "device-util.h"
10 #include "format-table.h"
11 #include "format-util.h"
12 #include "hashmap.h"
13 #include "help-util.h"
14 #include "options.h"
15 #include "set.h"
16 #include "static-destruct.h"
17 #include "string-util.h"
18 #include "time-util.h"
19 #include "udevadm.h"
20 #include "virt.h"
21
22 static bool arg_show_property = false;
23 static bool arg_print_kernel = false;
24 static bool arg_print_udev = false;
25 static Set *arg_tag_filter = NULL;
26 static Hashmap *arg_subsystem_filter = NULL;
27
28 STATIC_DESTRUCTOR_REGISTER(arg_tag_filter, set_freep);
29 STATIC_DESTRUCTOR_REGISTER(arg_subsystem_filter, hashmap_freep);
30
31 static int device_monitor_handler(sd_device_monitor *monitor, sd_device *device, void *userdata) {
32 sd_device_action_t action = _SD_DEVICE_ACTION_INVALID;
33 const char *devpath = NULL, *subsystem = NULL;
34 MonitorNetlinkGroup group = PTR_TO_INT(userdata);
35 struct timespec ts;
36
37 assert(device);
38 assert(IN_SET(group, MONITOR_GROUP_UDEV, MONITOR_GROUP_KERNEL));
39
40 (void) sd_device_get_action(device, &action);
41 (void) sd_device_get_devpath(device, &devpath);
42 (void) sd_device_get_subsystem(device, &subsystem);
43
44 assert_se(clock_gettime(CLOCK_MONOTONIC, &ts) == 0);
45
46 printf("%-6s[%"PRI_TIME".%06"PRI_NSEC"] %-8s %s (%s)\n",
47 group == MONITOR_GROUP_UDEV ? "UDEV" : "KERNEL",
48 ts.tv_sec, (nsec_t)ts.tv_nsec/1000,
49 strna(device_action_to_string(action)),
50 devpath, subsystem);
51
52 if (arg_show_property) {
53 FOREACH_DEVICE_PROPERTY(device, key, value)
54 printf("%s=%s\n", key, value);
55
56 printf("\n");
57 }
58
59 fflush(stdout);
60
61 return 0;
62 }
63
64 static int setup_monitor(MonitorNetlinkGroup sender, sd_event *event, sd_device_monitor **ret) {
65 _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor = NULL;
66 const char *subsystem, *devtype, *tag;
67 int r;
68
69 assert(ret);
70
71 r = device_monitor_new_full(&monitor, sender, -EBADF);
72 if (r < 0)
73 return log_error_errno(r, "Failed to create netlink socket: %m");
74
75 r = sd_device_monitor_attach_event(monitor, event);
76 if (r < 0)
77 return log_error_errno(r, "Failed to attach event: %m");
78
79 HASHMAP_FOREACH_KEY(devtype, subsystem, arg_subsystem_filter) {
80 r = sd_device_monitor_filter_add_match_subsystem_devtype(monitor, subsystem, devtype);
81 if (r < 0)
82 return log_error_errno(r, "Failed to apply subsystem filter '%s%s%s': %m",
83 subsystem, devtype ? "/" : "", strempty(devtype));
84 }
85
86 SET_FOREACH(tag, arg_tag_filter) {
87 r = sd_device_monitor_filter_add_match_tag(monitor, tag);
88 if (r < 0)
89 return log_error_errno(r, "Failed to apply tag filter '%s': %m", tag);
90 }
91
92 r = sd_device_monitor_start(monitor, device_monitor_handler, INT_TO_PTR(sender));
93 if (r < 0)
94 return log_error_errno(r, "Failed to start device monitor: %m");
95
96 (void) sd_device_monitor_set_description(monitor, sender == MONITOR_GROUP_UDEV ? "udev" : "kernel");
97
98 *ret = TAKE_PTR(monitor);
99 return 0;
100 }
101
102 static int help(void) {
103 _cleanup_(table_unrefp) Table *options = NULL;
104 int r;
105
106 r = option_parser_get_help_table_ns("udevadm-monitor", &options);
107 if (r < 0)
108 return r;
109
110 help_cmdline("monitor [OPTIONS]");
111 help_abstract("Listen to kernel and udev events.");
112 help_section("Options");
113 r = table_print_or_warn(options);
114 if (r < 0)
115 return r;
116
117 help_man_page_reference("udevadm", "8");
118 return 0;
119 }
120
121 static int parse_argv(int argc, char *argv[]) {
122 int r;
123
124 assert(argc >= 0);
125 assert(argv);
126
127 OptionParser opts = { argc, argv, .namespace = "udevadm-monitor" };
128
129 FOREACH_OPTION_OR_RETURN(c, &opts)
130 switch (c) {
131
132 OPTION_NAMESPACE("udevadm-monitor"): {}
133
134 OPTION_COMMON_HELP:
135 return help();
136
137 OPTION('V', "version", NULL, "Show package version"):
138 return print_version();
139
140 OPTION('e', "environment", NULL, /* help= */ NULL): {} /* hidden alias for -p */
141 OPTION('p', "property", NULL, "Print the event properties"):
142 arg_show_property = true;
143 break;
144
145 OPTION('k', "kernel", NULL, "Print kernel uevents"):
146 arg_print_kernel = true;
147 break;
148
149 OPTION('u', "udev", NULL, "Print udev events"):
150 arg_print_udev = true;
151 break;
152
153 OPTION('s', "subsystem-match", "SUBSYSTEM[/DEVTYPE]",
154 "Filter events by subsystem"): {
155 _cleanup_free_ char *subsystem = NULL, *devtype = NULL;
156 const char *slash;
157
158 slash = strchr(opts.arg, '/');
159 if (slash) {
160 devtype = strdup(slash + 1);
161 if (!devtype)
162 return log_oom();
163
164 subsystem = strndup(opts.arg, slash - opts.arg);
165 } else
166 subsystem = strdup(opts.arg);
167
168 if (!subsystem)
169 return log_oom();
170
171 r = hashmap_ensure_put(&arg_subsystem_filter, &trivial_hash_ops_free_free, subsystem, devtype);
172 if (r < 0)
173 return log_oom();
174
175 TAKE_PTR(subsystem);
176 TAKE_PTR(devtype);
177 break;
178 }
179
180 OPTION('t', "tag-match", "TAG", "Filter events by tag"):
181 r = set_put_strdup(&arg_tag_filter, opts.arg);
182 if (r < 0)
183 return log_oom();
184 break;
185 }
186
187 if (!arg_print_kernel && !arg_print_udev) {
188 arg_print_kernel = true;
189 arg_print_udev = true;
190 }
191
192 return 1;
193 }
194
195 int verb_monitor_main(int argc, char *argv[], uintptr_t _data, void *userdata) {
196 _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *kernel_monitor = NULL, *udev_monitor = NULL;
197 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
198 int r;
199
200 r = parse_argv(argc, argv);
201 if (r <= 0)
202 return r;
203
204 if (running_in_chroot() > 0) {
205 log_info("Running in chroot, ignoring request.");
206 return 0;
207 }
208
209 /* Callers are expecting to see events as they happen: Line buffering */
210 setlinebuf(stdout);
211
212 r = sd_event_default(&event);
213 if (r < 0)
214 return log_error_errno(r, "Failed to initialize event: %m");
215
216 r = sd_event_set_signal_exit(event, true);
217 if (r < 0)
218 return log_error_errno(r, "Failed to install SIGINT/SIGTERM handling: %m");
219
220 printf("monitor will print the received events for:\n");
221 if (arg_print_udev) {
222 r = setup_monitor(MONITOR_GROUP_UDEV, event, &udev_monitor);
223 if (r < 0)
224 return r;
225
226 printf("UDEV - the event which udev sends out after rule processing\n");
227 }
228
229 if (arg_print_kernel) {
230 r = setup_monitor(MONITOR_GROUP_KERNEL, event, &kernel_monitor);
231 if (r < 0)
232 return r;
233
234 printf("KERNEL - the kernel uevent\n");
235 }
236 printf("\n");
237
238 r = sd_event_loop(event);
239 if (r < 0)
240 return log_error_errno(r, "Failed to run event loop: %m");
241
242 return 0;
243 }