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