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