]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udevadm-monitor.c
Merge pull request #11827 from keszybz/pkgconfig-variables
[thirdparty/systemd.git] / src / udev / udevadm-monitor.c
1 /* SPDX-License-Identifier: GPL-2.0+ */
2
3 #include <errno.h>
4 #include <getopt.h>
5 #include <signal.h>
6
7 #include "sd-device.h"
8 #include "sd-event.h"
9
10 #include "alloc-util.h"
11 #include "device-monitor-private.h"
12 #include "device-util.h"
13 #include "fd-util.h"
14 #include "format-util.h"
15 #include "hashmap.h"
16 #include "set.h"
17 #include "signal-util.h"
18 #include "string-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 int device_monitor_handler(sd_device_monitor *monitor, sd_device *device, void *userdata) {
29 const char *action = NULL, *devpath = NULL, *subsystem = NULL;
30 MonitorNetlinkGroup group = PTR_TO_INT(userdata);
31 struct timespec ts;
32
33 assert(device);
34 assert(IN_SET(group, MONITOR_GROUP_UDEV, MONITOR_GROUP_KERNEL));
35
36 (void) sd_device_get_property_value(device, "ACTION", &action);
37 (void) sd_device_get_devpath(device, &devpath);
38 (void) sd_device_get_subsystem(device, &subsystem);
39
40 assert_se(clock_gettime(CLOCK_MONOTONIC, &ts) == 0);
41
42 printf("%-6s[%"PRI_TIME".%06"PRI_NSEC"] %-8s %s (%s)\n",
43 group == MONITOR_GROUP_UDEV ? "UDEV" : "KERNEL",
44 ts.tv_sec, (nsec_t)ts.tv_nsec/1000,
45 action, devpath, subsystem);
46
47 if (arg_show_property) {
48 const char *key, *value;
49
50 FOREACH_DEVICE_PROPERTY(device, key, value)
51 printf("%s=%s\n", key, value);
52
53 printf("\n");
54 }
55
56 return 0;
57 }
58
59 static int setup_monitor(MonitorNetlinkGroup sender, sd_event *event, sd_device_monitor **ret) {
60 _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor = NULL;
61 const char *subsystem, *devtype, *tag;
62 Iterator i;
63 int r;
64
65 r = device_monitor_new_full(&monitor, sender, -1);
66 if (r < 0)
67 return log_error_errno(r, "Failed to create netlink socket: %m");
68
69 (void) sd_device_monitor_set_receive_buffer_size(monitor, 128*1024*1024);
70
71 r = sd_device_monitor_attach_event(monitor, event);
72 if (r < 0)
73 return log_error_errno(r, "Failed to attach event: %m");
74
75 HASHMAP_FOREACH_KEY(devtype, subsystem, arg_subsystem_filter, i) {
76 r = sd_device_monitor_filter_add_match_subsystem_devtype(monitor, subsystem, devtype);
77 if (r < 0)
78 return log_error_errno(r, "Failed to apply subsystem filter '%s%s%s': %m",
79 subsystem, devtype ? "/" : "", strempty(devtype));
80 }
81
82 SET_FOREACH(tag, arg_tag_filter, i) {
83 r = sd_device_monitor_filter_add_match_tag(monitor, tag);
84 if (r < 0)
85 return log_error_errno(r, "Failed to apply tag filter '%s': %m", tag);
86 }
87
88 r = sd_device_monitor_start(monitor, device_monitor_handler, INT_TO_PTR(sender));
89 if (r < 0)
90 return log_error_errno(r, "Failed to start device monitor: %m");
91
92 (void) sd_event_source_set_description(sd_device_monitor_get_event_source(monitor),
93 sender == MONITOR_GROUP_UDEV ? "device-monitor-udev" : "device-monitor-kernel");
94
95 *ret = TAKE_PTR(monitor);
96 return 0;
97 }
98
99 static int help(void) {
100 printf("%s monitor [OPTIONS]\n\n"
101 "Listen to kernel and udev events.\n\n"
102 " -h --help Show this help\n"
103 " -V --version Show package version\n"
104 " -p --property Print the event properties\n"
105 " -k --kernel Print kernel uevents\n"
106 " -u --udev Print udev events\n"
107 " -s --subsystem-match=SUBSYSTEM[/DEVTYPE] Filter events by subsystem\n"
108 " -t --tag-match=TAG Filter events by tag\n"
109 , program_invocation_short_name);
110
111 return 0;
112 }
113
114 static int parse_argv(int argc, char *argv[]) {
115 static const struct option options[] = {
116 { "property", no_argument, NULL, 'p' },
117 { "environment", no_argument, NULL, 'e' }, /* alias for -p */
118 { "kernel", no_argument, NULL, 'k' },
119 { "udev", no_argument, NULL, 'u' },
120 { "subsystem-match", required_argument, NULL, 's' },
121 { "tag-match", required_argument, NULL, 't' },
122 { "version", no_argument, NULL, 'V' },
123 { "help", no_argument, NULL, 'h' },
124 {}
125 };
126
127 int r, c;
128
129 while ((c = getopt_long(argc, argv, "pekus:t:Vh", options, NULL)) >= 0)
130 switch (c) {
131 case 'p':
132 case 'e':
133 arg_show_property = true;
134 break;
135 case 'k':
136 arg_print_kernel = true;
137 break;
138 case 'u':
139 arg_print_udev = true;
140 break;
141 case 's': {
142 _cleanup_free_ char *subsystem = NULL, *devtype = NULL;
143 const char *slash;
144
145 slash = strchr(optarg, '/');
146 if (slash) {
147 devtype = strdup(slash + 1);
148 if (!devtype)
149 return -ENOMEM;
150
151 subsystem = strndup(optarg, slash - optarg);
152 } else
153 subsystem = strdup(optarg);
154
155 if (!subsystem)
156 return -ENOMEM;
157
158 r = hashmap_ensure_allocated(&arg_subsystem_filter, NULL);
159 if (r < 0)
160 return r;
161
162 r = hashmap_put(arg_subsystem_filter, subsystem, devtype);
163 if (r < 0)
164 return r;
165
166 subsystem = devtype = NULL;
167 break;
168 }
169 case 't': {
170 _cleanup_free_ char *tag = NULL;
171
172 r = set_ensure_allocated(&arg_tag_filter, &string_hash_ops);
173 if (r < 0)
174 return r;
175
176 tag = strdup(optarg);
177 if (!tag)
178 return -ENOMEM;
179
180 r = set_put(arg_tag_filter, tag);
181 if (r < 0)
182 return r;
183
184 tag = NULL;
185 break;
186 }
187 case 'V':
188 return print_version();
189 case 'h':
190 return help();
191 case '?':
192 return -EINVAL;
193 default:
194 assert_not_reached("Unknown option.");
195 }
196
197 if (!arg_print_kernel && !arg_print_udev) {
198 arg_print_kernel = true;
199 arg_print_udev = true;
200 }
201
202 return 1;
203 }
204
205 int monitor_main(int argc, char *argv[], void *userdata) {
206 _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *kernel_monitor = NULL, *udev_monitor = NULL;
207 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
208 int r;
209
210 r = parse_argv(argc, argv);
211 if (r <= 0)
212 goto finalize;
213
214 if (running_in_chroot() > 0) {
215 log_info("Running in chroot, ignoring request.");
216 return 0;
217 }
218
219 /* Callers are expecting to see events as they happen: Line buffering */
220 setlinebuf(stdout);
221
222 r = sd_event_default(&event);
223 if (r < 0) {
224 log_error_errno(r, "Failed to initialize event: %m");
225 goto finalize;
226 }
227
228 assert_se(sigprocmask_many(SIG_UNBLOCK, NULL, SIGTERM, SIGINT, -1) >= 0);
229 (void) sd_event_add_signal(event, NULL, SIGTERM, NULL, NULL);
230 (void) sd_event_add_signal(event, NULL, SIGINT, NULL, NULL);
231
232 printf("monitor will print the received events for:\n");
233 if (arg_print_udev) {
234 r = setup_monitor(MONITOR_GROUP_UDEV, event, &udev_monitor);
235 if (r < 0)
236 goto finalize;
237
238 printf("UDEV - the event which udev sends out after rule processing\n");
239 }
240
241 if (arg_print_kernel) {
242 r = setup_monitor(MONITOR_GROUP_KERNEL, event, &kernel_monitor);
243 if (r < 0)
244 goto finalize;
245
246 printf("KERNEL - the kernel uevent\n");
247 }
248 printf("\n");
249
250 r = sd_event_loop(event);
251 if (r < 0) {
252 log_error_errno(r, "Failed to run event loop: %m");
253 goto finalize;
254 }
255
256 r = 0;
257
258 finalize:
259 hashmap_free_free_free(arg_subsystem_filter);
260 set_free_free(arg_tag_filter);
261
262 return r;
263 }