]>
Commit | Line | Data |
---|---|---|
1 | /* SPDX-License-Identifier: LGPL-2.1+ */ | |
2 | /*** | |
3 | This file is part of systemd. | |
4 | ||
5 | Copyright 2008-2012 Kay Sievers <kay@vrfy.org> | |
6 | ||
7 | systemd is free software; you can redistribute it and/or modify it | |
8 | under the terms of the GNU Lesser General Public License as published by | |
9 | the Free Software Foundation; either version 2.1 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | systemd is distributed in the hope that it will be useful, but | |
13 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | Lesser General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU Lesser General Public License | |
18 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
19 | ***/ | |
20 | ||
21 | #include <errno.h> | |
22 | #include <linux/filter.h> | |
23 | #include <linux/netlink.h> | |
24 | #include <poll.h> | |
25 | #include <stddef.h> | |
26 | #include <stdio.h> | |
27 | #include <stdlib.h> | |
28 | #include <string.h> | |
29 | #include <sys/socket.h> | |
30 | #include <unistd.h> | |
31 | ||
32 | #include "libudev.h" | |
33 | ||
34 | #include "alloc-util.h" | |
35 | #include "fd-util.h" | |
36 | #include "fileio.h" | |
37 | #include "format-util.h" | |
38 | #include "libudev-private.h" | |
39 | #include "missing.h" | |
40 | #include "mount-util.h" | |
41 | #include "socket-util.h" | |
42 | #include "string-util.h" | |
43 | ||
44 | /** | |
45 | * SECTION:libudev-monitor | |
46 | * @short_description: device event source | |
47 | * | |
48 | * Connects to a device event source. | |
49 | */ | |
50 | ||
51 | /** | |
52 | * udev_monitor: | |
53 | * | |
54 | * Opaque object handling an event source. | |
55 | */ | |
56 | struct udev_monitor { | |
57 | struct udev *udev; | |
58 | int refcount; | |
59 | int sock; | |
60 | union sockaddr_union snl; | |
61 | union sockaddr_union snl_trusted_sender; | |
62 | union sockaddr_union snl_destination; | |
63 | socklen_t addrlen; | |
64 | struct udev_list filter_subsystem_list; | |
65 | struct udev_list filter_tag_list; | |
66 | bool bound; | |
67 | }; | |
68 | ||
69 | enum udev_monitor_netlink_group { | |
70 | UDEV_MONITOR_NONE, | |
71 | UDEV_MONITOR_KERNEL, | |
72 | UDEV_MONITOR_UDEV, | |
73 | }; | |
74 | ||
75 | #define UDEV_MONITOR_MAGIC 0xfeedcafe | |
76 | struct udev_monitor_netlink_header { | |
77 | /* "libudev" prefix to distinguish libudev and kernel messages */ | |
78 | char prefix[8]; | |
79 | /* | |
80 | * magic to protect against daemon <-> library message format mismatch | |
81 | * used in the kernel from socket filter rules; needs to be stored in network order | |
82 | */ | |
83 | unsigned int magic; | |
84 | /* total length of header structure known to the sender */ | |
85 | unsigned int header_size; | |
86 | /* properties string buffer */ | |
87 | unsigned int properties_off; | |
88 | unsigned int properties_len; | |
89 | /* | |
90 | * hashes of primary device properties strings, to let libudev subscribers | |
91 | * use in-kernel socket filters; values need to be stored in network order | |
92 | */ | |
93 | unsigned int filter_subsystem_hash; | |
94 | unsigned int filter_devtype_hash; | |
95 | unsigned int filter_tag_bloom_hi; | |
96 | unsigned int filter_tag_bloom_lo; | |
97 | }; | |
98 | ||
99 | static struct udev_monitor *udev_monitor_new(struct udev *udev) | |
100 | { | |
101 | struct udev_monitor *udev_monitor; | |
102 | ||
103 | udev_monitor = new0(struct udev_monitor, 1); | |
104 | if (udev_monitor == NULL) { | |
105 | errno = ENOMEM; | |
106 | return NULL; | |
107 | } | |
108 | udev_monitor->refcount = 1; | |
109 | udev_monitor->udev = udev; | |
110 | udev_list_init(udev, &udev_monitor->filter_subsystem_list, false); | |
111 | udev_list_init(udev, &udev_monitor->filter_tag_list, true); | |
112 | return udev_monitor; | |
113 | } | |
114 | ||
115 | /* we consider udev running when /dev is on devtmpfs */ | |
116 | static bool udev_has_devtmpfs(struct udev *udev) { | |
117 | ||
118 | union file_handle_union h = FILE_HANDLE_INIT; | |
119 | _cleanup_fclose_ FILE *f = NULL; | |
120 | char line[LINE_MAX], *e; | |
121 | int mount_id; | |
122 | int r; | |
123 | ||
124 | r = name_to_handle_at(AT_FDCWD, "/dev", &h.handle, &mount_id, 0); | |
125 | if (r < 0) { | |
126 | if (errno != EOPNOTSUPP) | |
127 | log_debug_errno(errno, "name_to_handle_at on /dev: %m"); | |
128 | return false; | |
129 | } | |
130 | ||
131 | f = fopen("/proc/self/mountinfo", "re"); | |
132 | if (!f) | |
133 | return false; | |
134 | ||
135 | FOREACH_LINE(line, f, return false) { | |
136 | int mid; | |
137 | ||
138 | if (sscanf(line, "%i", &mid) != 1) | |
139 | continue; | |
140 | ||
141 | if (mid != mount_id) | |
142 | continue; | |
143 | ||
144 | e = strstr(line, " - "); | |
145 | if (!e) | |
146 | continue; | |
147 | ||
148 | /* accept any name that starts with the currently expected type */ | |
149 | if (startswith(e + 3, "devtmpfs")) | |
150 | return true; | |
151 | } | |
152 | ||
153 | return false; | |
154 | } | |
155 | ||
156 | static void monitor_set_nl_address(struct udev_monitor *udev_monitor) { | |
157 | union sockaddr_union snl; | |
158 | socklen_t addrlen; | |
159 | int r; | |
160 | ||
161 | assert(udev_monitor); | |
162 | ||
163 | /* get the address the kernel has assigned us | |
164 | * it is usually, but not necessarily the pid | |
165 | */ | |
166 | addrlen = sizeof(struct sockaddr_nl); | |
167 | r = getsockname(udev_monitor->sock, &snl.sa, &addrlen); | |
168 | if (r >= 0) | |
169 | udev_monitor->snl.nl.nl_pid = snl.nl.nl_pid; | |
170 | } | |
171 | ||
172 | struct udev_monitor *udev_monitor_new_from_netlink_fd(struct udev *udev, const char *name, int fd) | |
173 | { | |
174 | struct udev_monitor *udev_monitor; | |
175 | unsigned int group; | |
176 | ||
177 | if (udev == NULL) { | |
178 | errno = EINVAL; | |
179 | return NULL; | |
180 | } | |
181 | ||
182 | if (name == NULL) | |
183 | group = UDEV_MONITOR_NONE; | |
184 | else if (streq(name, "udev")) { | |
185 | /* | |
186 | * We do not support subscribing to uevents if no instance of | |
187 | * udev is running. Uevents would otherwise broadcast the | |
188 | * processing data of the host into containers, which is not | |
189 | * desired. | |
190 | * | |
191 | * Containers will currently not get any udev uevents, until | |
192 | * a supporting infrastructure is available. | |
193 | * | |
194 | * We do not set a netlink multicast group here, so the socket | |
195 | * will not receive any messages. | |
196 | */ | |
197 | if (access("/run/udev/control", F_OK) < 0 && !udev_has_devtmpfs(udev)) { | |
198 | log_debug("the udev service seems not to be active, disable the monitor"); | |
199 | group = UDEV_MONITOR_NONE; | |
200 | } else | |
201 | group = UDEV_MONITOR_UDEV; | |
202 | } else if (streq(name, "kernel")) | |
203 | group = UDEV_MONITOR_KERNEL; | |
204 | else { | |
205 | errno = EINVAL; | |
206 | return NULL; | |
207 | } | |
208 | ||
209 | udev_monitor = udev_monitor_new(udev); | |
210 | if (udev_monitor == NULL) | |
211 | return NULL; | |
212 | ||
213 | if (fd < 0) { | |
214 | udev_monitor->sock = socket(PF_NETLINK, SOCK_RAW|SOCK_CLOEXEC|SOCK_NONBLOCK, NETLINK_KOBJECT_UEVENT); | |
215 | if (udev_monitor->sock < 0) { | |
216 | log_debug_errno(errno, "error getting socket: %m"); | |
217 | return mfree(udev_monitor); | |
218 | } | |
219 | } else { | |
220 | udev_monitor->bound = true; | |
221 | udev_monitor->sock = fd; | |
222 | monitor_set_nl_address(udev_monitor); | |
223 | } | |
224 | ||
225 | udev_monitor->snl.nl.nl_family = AF_NETLINK; | |
226 | udev_monitor->snl.nl.nl_groups = group; | |
227 | ||
228 | /* default destination for sending */ | |
229 | udev_monitor->snl_destination.nl.nl_family = AF_NETLINK; | |
230 | udev_monitor->snl_destination.nl.nl_groups = UDEV_MONITOR_UDEV; | |
231 | ||
232 | return udev_monitor; | |
233 | } | |
234 | ||
235 | /** | |
236 | * udev_monitor_new_from_netlink: | |
237 | * @udev: udev library context | |
238 | * @name: name of event source | |
239 | * | |
240 | * Create new udev monitor and connect to a specified event | |
241 | * source. Valid sources identifiers are "udev" and "kernel". | |
242 | * | |
243 | * Applications should usually not connect directly to the | |
244 | * "kernel" events, because the devices might not be useable | |
245 | * at that time, before udev has configured them, and created | |
246 | * device nodes. Accessing devices at the same time as udev, | |
247 | * might result in unpredictable behavior. The "udev" events | |
248 | * are sent out after udev has finished its event processing, | |
249 | * all rules have been processed, and needed device nodes are | |
250 | * created. | |
251 | * | |
252 | * The initial refcount is 1, and needs to be decremented to | |
253 | * release the resources of the udev monitor. | |
254 | * | |
255 | * Returns: a new udev monitor, or #NULL, in case of an error | |
256 | **/ | |
257 | _public_ struct udev_monitor *udev_monitor_new_from_netlink(struct udev *udev, const char *name) | |
258 | { | |
259 | return udev_monitor_new_from_netlink_fd(udev, name, -1); | |
260 | } | |
261 | ||
262 | static inline void bpf_stmt(struct sock_filter *inss, unsigned int *i, | |
263 | unsigned short code, unsigned int data) | |
264 | { | |
265 | struct sock_filter *ins = &inss[*i]; | |
266 | ||
267 | ins->code = code; | |
268 | ins->k = data; | |
269 | (*i)++; | |
270 | } | |
271 | ||
272 | static inline void bpf_jmp(struct sock_filter *inss, unsigned int *i, | |
273 | unsigned short code, unsigned int data, | |
274 | unsigned short jt, unsigned short jf) | |
275 | { | |
276 | struct sock_filter *ins = &inss[*i]; | |
277 | ||
278 | ins->code = code; | |
279 | ins->jt = jt; | |
280 | ins->jf = jf; | |
281 | ins->k = data; | |
282 | (*i)++; | |
283 | } | |
284 | ||
285 | /** | |
286 | * udev_monitor_filter_update: | |
287 | * @udev_monitor: monitor | |
288 | * | |
289 | * Update the installed socket filter. This is only needed, | |
290 | * if the filter was removed or changed. | |
291 | * | |
292 | * Returns: 0 on success, otherwise a negative error value. | |
293 | */ | |
294 | _public_ int udev_monitor_filter_update(struct udev_monitor *udev_monitor) | |
295 | { | |
296 | struct sock_filter ins[512]; | |
297 | struct sock_fprog filter; | |
298 | unsigned int i; | |
299 | struct udev_list_entry *list_entry; | |
300 | int err; | |
301 | ||
302 | if (udev_list_get_entry(&udev_monitor->filter_subsystem_list) == NULL && | |
303 | udev_list_get_entry(&udev_monitor->filter_tag_list) == NULL) | |
304 | return 0; | |
305 | ||
306 | memzero(ins, sizeof(ins)); | |
307 | i = 0; | |
308 | ||
309 | /* load magic in A */ | |
310 | bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, magic)); | |
311 | /* jump if magic matches */ | |
312 | bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, UDEV_MONITOR_MAGIC, 1, 0); | |
313 | /* wrong magic, pass packet */ | |
314 | bpf_stmt(ins, &i, BPF_RET|BPF_K, 0xffffffff); | |
315 | ||
316 | if (udev_list_get_entry(&udev_monitor->filter_tag_list) != NULL) { | |
317 | int tag_matches; | |
318 | ||
319 | /* count tag matches, to calculate end of tag match block */ | |
320 | tag_matches = 0; | |
321 | udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_tag_list)) | |
322 | tag_matches++; | |
323 | ||
324 | /* add all tags matches */ | |
325 | udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_tag_list)) { | |
326 | uint64_t tag_bloom_bits = util_string_bloom64(udev_list_entry_get_name(list_entry)); | |
327 | uint32_t tag_bloom_hi = tag_bloom_bits >> 32; | |
328 | uint32_t tag_bloom_lo = tag_bloom_bits & 0xffffffff; | |
329 | ||
330 | /* load device bloom bits in A */ | |
331 | bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_tag_bloom_hi)); | |
332 | /* clear bits (tag bits & bloom bits) */ | |
333 | bpf_stmt(ins, &i, BPF_ALU|BPF_AND|BPF_K, tag_bloom_hi); | |
334 | /* jump to next tag if it does not match */ | |
335 | bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, tag_bloom_hi, 0, 3); | |
336 | ||
337 | /* load device bloom bits in A */ | |
338 | bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_tag_bloom_lo)); | |
339 | /* clear bits (tag bits & bloom bits) */ | |
340 | bpf_stmt(ins, &i, BPF_ALU|BPF_AND|BPF_K, tag_bloom_lo); | |
341 | /* jump behind end of tag match block if tag matches */ | |
342 | tag_matches--; | |
343 | bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, tag_bloom_lo, 1 + (tag_matches * 6), 0); | |
344 | } | |
345 | ||
346 | /* nothing matched, drop packet */ | |
347 | bpf_stmt(ins, &i, BPF_RET|BPF_K, 0); | |
348 | } | |
349 | ||
350 | /* add all subsystem matches */ | |
351 | if (udev_list_get_entry(&udev_monitor->filter_subsystem_list) != NULL) { | |
352 | udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_subsystem_list)) { | |
353 | unsigned int hash = util_string_hash32(udev_list_entry_get_name(list_entry)); | |
354 | ||
355 | /* load device subsystem value in A */ | |
356 | bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_subsystem_hash)); | |
357 | if (udev_list_entry_get_value(list_entry) == NULL) { | |
358 | /* jump if subsystem does not match */ | |
359 | bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 1); | |
360 | } else { | |
361 | /* jump if subsystem does not match */ | |
362 | bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 3); | |
363 | ||
364 | /* load device devtype value in A */ | |
365 | bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_devtype_hash)); | |
366 | /* jump if value does not match */ | |
367 | hash = util_string_hash32(udev_list_entry_get_value(list_entry)); | |
368 | bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 1); | |
369 | } | |
370 | ||
371 | /* matched, pass packet */ | |
372 | bpf_stmt(ins, &i, BPF_RET|BPF_K, 0xffffffff); | |
373 | ||
374 | if (i+1 >= ELEMENTSOF(ins)) | |
375 | return -E2BIG; | |
376 | } | |
377 | ||
378 | /* nothing matched, drop packet */ | |
379 | bpf_stmt(ins, &i, BPF_RET|BPF_K, 0); | |
380 | } | |
381 | ||
382 | /* matched, pass packet */ | |
383 | bpf_stmt(ins, &i, BPF_RET|BPF_K, 0xffffffff); | |
384 | ||
385 | /* install filter */ | |
386 | memzero(&filter, sizeof(filter)); | |
387 | filter.len = i; | |
388 | filter.filter = ins; | |
389 | err = setsockopt(udev_monitor->sock, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter)); | |
390 | return err < 0 ? -errno : 0; | |
391 | } | |
392 | ||
393 | int udev_monitor_allow_unicast_sender(struct udev_monitor *udev_monitor, struct udev_monitor *sender) | |
394 | { | |
395 | udev_monitor->snl_trusted_sender.nl.nl_pid = sender->snl.nl.nl_pid; | |
396 | return 0; | |
397 | } | |
398 | ||
399 | /** | |
400 | * udev_monitor_enable_receiving: | |
401 | * @udev_monitor: the monitor which should receive events | |
402 | * | |
403 | * Binds the @udev_monitor socket to the event source. | |
404 | * | |
405 | * Returns: 0 on success, otherwise a negative error value. | |
406 | */ | |
407 | _public_ int udev_monitor_enable_receiving(struct udev_monitor *udev_monitor) | |
408 | { | |
409 | int err = 0; | |
410 | const int on = 1; | |
411 | ||
412 | udev_monitor_filter_update(udev_monitor); | |
413 | ||
414 | if (!udev_monitor->bound) { | |
415 | err = bind(udev_monitor->sock, | |
416 | &udev_monitor->snl.sa, sizeof(struct sockaddr_nl)); | |
417 | if (err == 0) | |
418 | udev_monitor->bound = true; | |
419 | } | |
420 | ||
421 | if (err >= 0) | |
422 | monitor_set_nl_address(udev_monitor); | |
423 | else | |
424 | return log_debug_errno(errno, "bind failed: %m"); | |
425 | ||
426 | /* enable receiving of sender credentials */ | |
427 | err = setsockopt(udev_monitor->sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)); | |
428 | if (err < 0) | |
429 | log_debug_errno(errno, "setting SO_PASSCRED failed: %m"); | |
430 | ||
431 | return 0; | |
432 | } | |
433 | ||
434 | /** | |
435 | * udev_monitor_set_receive_buffer_size: | |
436 | * @udev_monitor: the monitor which should receive events | |
437 | * @size: the size in bytes | |
438 | * | |
439 | * Set the size of the kernel socket buffer. This call needs the | |
440 | * appropriate privileges to succeed. | |
441 | * | |
442 | * Returns: 0 on success, otherwise -1 on error. | |
443 | */ | |
444 | _public_ int udev_monitor_set_receive_buffer_size(struct udev_monitor *udev_monitor, int size) | |
445 | { | |
446 | if (udev_monitor == NULL) | |
447 | return -EINVAL; | |
448 | if (setsockopt(udev_monitor->sock, SOL_SOCKET, SO_RCVBUFFORCE, &size, sizeof(size)) < 0) | |
449 | return -errno; | |
450 | ||
451 | return 0; | |
452 | } | |
453 | ||
454 | int udev_monitor_disconnect(struct udev_monitor *udev_monitor) | |
455 | { | |
456 | int err; | |
457 | ||
458 | err = close(udev_monitor->sock); | |
459 | udev_monitor->sock = -1; | |
460 | return err < 0 ? -errno : 0; | |
461 | } | |
462 | ||
463 | /** | |
464 | * udev_monitor_ref: | |
465 | * @udev_monitor: udev monitor | |
466 | * | |
467 | * Take a reference of a udev monitor. | |
468 | * | |
469 | * Returns: the passed udev monitor | |
470 | **/ | |
471 | _public_ struct udev_monitor *udev_monitor_ref(struct udev_monitor *udev_monitor) | |
472 | { | |
473 | if (udev_monitor == NULL) | |
474 | return NULL; | |
475 | udev_monitor->refcount++; | |
476 | return udev_monitor; | |
477 | } | |
478 | ||
479 | /** | |
480 | * udev_monitor_unref: | |
481 | * @udev_monitor: udev monitor | |
482 | * | |
483 | * Drop a reference of a udev monitor. If the refcount reaches zero, | |
484 | * the bound socket will be closed, and the resources of the monitor | |
485 | * will be released. | |
486 | * | |
487 | * Returns: #NULL | |
488 | **/ | |
489 | _public_ struct udev_monitor *udev_monitor_unref(struct udev_monitor *udev_monitor) | |
490 | { | |
491 | if (udev_monitor == NULL) | |
492 | return NULL; | |
493 | udev_monitor->refcount--; | |
494 | if (udev_monitor->refcount > 0) | |
495 | return NULL; | |
496 | if (udev_monitor->sock >= 0) | |
497 | close(udev_monitor->sock); | |
498 | udev_list_cleanup(&udev_monitor->filter_subsystem_list); | |
499 | udev_list_cleanup(&udev_monitor->filter_tag_list); | |
500 | free(udev_monitor); | |
501 | return NULL; | |
502 | } | |
503 | ||
504 | /** | |
505 | * udev_monitor_get_udev: | |
506 | * @udev_monitor: udev monitor | |
507 | * | |
508 | * Retrieve the udev library context the monitor was created with. | |
509 | * | |
510 | * Returns: the udev library context | |
511 | **/ | |
512 | _public_ struct udev *udev_monitor_get_udev(struct udev_monitor *udev_monitor) | |
513 | { | |
514 | if (udev_monitor == NULL) | |
515 | return NULL; | |
516 | return udev_monitor->udev; | |
517 | } | |
518 | ||
519 | /** | |
520 | * udev_monitor_get_fd: | |
521 | * @udev_monitor: udev monitor | |
522 | * | |
523 | * Retrieve the socket file descriptor associated with the monitor. | |
524 | * | |
525 | * Returns: the socket file descriptor | |
526 | **/ | |
527 | _public_ int udev_monitor_get_fd(struct udev_monitor *udev_monitor) | |
528 | { | |
529 | if (udev_monitor == NULL) | |
530 | return -EINVAL; | |
531 | return udev_monitor->sock; | |
532 | } | |
533 | ||
534 | static int passes_filter(struct udev_monitor *udev_monitor, struct udev_device *udev_device) | |
535 | { | |
536 | struct udev_list_entry *list_entry; | |
537 | ||
538 | if (udev_list_get_entry(&udev_monitor->filter_subsystem_list) == NULL) | |
539 | goto tag; | |
540 | udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_subsystem_list)) { | |
541 | const char *subsys = udev_list_entry_get_name(list_entry); | |
542 | const char *dsubsys = udev_device_get_subsystem(udev_device); | |
543 | const char *devtype; | |
544 | const char *ddevtype; | |
545 | ||
546 | if (!streq(dsubsys, subsys)) | |
547 | continue; | |
548 | ||
549 | devtype = udev_list_entry_get_value(list_entry); | |
550 | if (devtype == NULL) | |
551 | goto tag; | |
552 | ddevtype = udev_device_get_devtype(udev_device); | |
553 | if (ddevtype == NULL) | |
554 | continue; | |
555 | if (streq(ddevtype, devtype)) | |
556 | goto tag; | |
557 | } | |
558 | return 0; | |
559 | ||
560 | tag: | |
561 | if (udev_list_get_entry(&udev_monitor->filter_tag_list) == NULL) | |
562 | return 1; | |
563 | udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_tag_list)) { | |
564 | const char *tag = udev_list_entry_get_name(list_entry); | |
565 | ||
566 | if (udev_device_has_tag(udev_device, tag)) | |
567 | return 1; | |
568 | } | |
569 | return 0; | |
570 | } | |
571 | ||
572 | /** | |
573 | * udev_monitor_receive_device: | |
574 | * @udev_monitor: udev monitor | |
575 | * | |
576 | * Receive data from the udev monitor socket, allocate a new udev | |
577 | * device, fill in the received data, and return the device. | |
578 | * | |
579 | * Only socket connections with uid=0 are accepted. | |
580 | * | |
581 | * The monitor socket is by default set to NONBLOCK. A variant of poll() on | |
582 | * the file descriptor returned by udev_monitor_get_fd() should to be used to | |
583 | * wake up when new devices arrive, or alternatively the file descriptor | |
584 | * switched into blocking mode. | |
585 | * | |
586 | * The initial refcount is 1, and needs to be decremented to | |
587 | * release the resources of the udev device. | |
588 | * | |
589 | * Returns: a new udev device, or #NULL, in case of an error | |
590 | **/ | |
591 | _public_ struct udev_device *udev_monitor_receive_device(struct udev_monitor *udev_monitor) | |
592 | { | |
593 | struct udev_device *udev_device; | |
594 | struct msghdr smsg; | |
595 | struct iovec iov; | |
596 | char cred_msg[CMSG_SPACE(sizeof(struct ucred))]; | |
597 | struct cmsghdr *cmsg; | |
598 | union sockaddr_union snl; | |
599 | struct ucred *cred; | |
600 | union { | |
601 | struct udev_monitor_netlink_header nlh; | |
602 | char raw[8192]; | |
603 | } buf; | |
604 | ssize_t buflen; | |
605 | ssize_t bufpos; | |
606 | bool is_initialized = false; | |
607 | ||
608 | retry: | |
609 | if (udev_monitor == NULL) { | |
610 | errno = EINVAL; | |
611 | return NULL; | |
612 | } | |
613 | iov.iov_base = &buf; | |
614 | iov.iov_len = sizeof(buf); | |
615 | memzero(&smsg, sizeof(struct msghdr)); | |
616 | smsg.msg_iov = &iov; | |
617 | smsg.msg_iovlen = 1; | |
618 | smsg.msg_control = cred_msg; | |
619 | smsg.msg_controllen = sizeof(cred_msg); | |
620 | smsg.msg_name = &snl; | |
621 | smsg.msg_namelen = sizeof(snl); | |
622 | ||
623 | buflen = recvmsg(udev_monitor->sock, &smsg, 0); | |
624 | if (buflen < 0) { | |
625 | if (errno != EINTR) | |
626 | log_debug("unable to receive message"); | |
627 | return NULL; | |
628 | } | |
629 | ||
630 | if (buflen < 32 || (smsg.msg_flags & MSG_TRUNC)) { | |
631 | log_debug("invalid message length"); | |
632 | errno = EINVAL; | |
633 | return NULL; | |
634 | } | |
635 | ||
636 | if (snl.nl.nl_groups == 0) { | |
637 | /* unicast message, check if we trust the sender */ | |
638 | if (udev_monitor->snl_trusted_sender.nl.nl_pid == 0 || | |
639 | snl.nl.nl_pid != udev_monitor->snl_trusted_sender.nl.nl_pid) { | |
640 | log_debug("unicast netlink message ignored"); | |
641 | errno = EAGAIN; | |
642 | return NULL; | |
643 | } | |
644 | } else if (snl.nl.nl_groups == UDEV_MONITOR_KERNEL) { | |
645 | if (snl.nl.nl_pid > 0) { | |
646 | log_debug("multicast kernel netlink message from PID %"PRIu32" ignored", | |
647 | snl.nl.nl_pid); | |
648 | errno = EAGAIN; | |
649 | return NULL; | |
650 | } | |
651 | } | |
652 | ||
653 | cmsg = CMSG_FIRSTHDR(&smsg); | |
654 | if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) { | |
655 | log_debug("no sender credentials received, message ignored"); | |
656 | errno = EAGAIN; | |
657 | return NULL; | |
658 | } | |
659 | ||
660 | cred = (struct ucred *)CMSG_DATA(cmsg); | |
661 | if (cred->uid != 0) { | |
662 | log_debug("sender uid="UID_FMT", message ignored", cred->uid); | |
663 | errno = EAGAIN; | |
664 | return NULL; | |
665 | } | |
666 | ||
667 | if (memcmp(buf.raw, "libudev", 8) == 0) { | |
668 | /* udev message needs proper version magic */ | |
669 | if (buf.nlh.magic != htobe32(UDEV_MONITOR_MAGIC)) { | |
670 | log_debug("unrecognized message signature (%x != %x)", | |
671 | buf.nlh.magic, htobe32(UDEV_MONITOR_MAGIC)); | |
672 | errno = EAGAIN; | |
673 | return NULL; | |
674 | } | |
675 | if (buf.nlh.properties_off+32 > (size_t)buflen) { | |
676 | log_debug("message smaller than expected (%u > %zd)", | |
677 | buf.nlh.properties_off+32, buflen); | |
678 | errno = EAGAIN; | |
679 | return NULL; | |
680 | } | |
681 | ||
682 | bufpos = buf.nlh.properties_off; | |
683 | ||
684 | /* devices received from udev are always initialized */ | |
685 | is_initialized = true; | |
686 | } else { | |
687 | /* kernel message with header */ | |
688 | bufpos = strlen(buf.raw) + 1; | |
689 | if ((size_t)bufpos < sizeof("a@/d") || bufpos >= buflen) { | |
690 | log_debug("invalid message length"); | |
691 | errno = EAGAIN; | |
692 | return NULL; | |
693 | } | |
694 | ||
695 | /* check message header */ | |
696 | if (strstr(buf.raw, "@/") == NULL) { | |
697 | log_debug("unrecognized message header"); | |
698 | errno = EAGAIN; | |
699 | return NULL; | |
700 | } | |
701 | } | |
702 | ||
703 | udev_device = udev_device_new_from_nulstr(udev_monitor->udev, &buf.raw[bufpos], buflen - bufpos); | |
704 | if (!udev_device) { | |
705 | log_debug_errno(errno, "could not create device: %m"); | |
706 | return NULL; | |
707 | } | |
708 | ||
709 | if (is_initialized) | |
710 | udev_device_set_is_initialized(udev_device); | |
711 | ||
712 | /* skip device, if it does not pass the current filter */ | |
713 | if (!passes_filter(udev_monitor, udev_device)) { | |
714 | struct pollfd pfd[1]; | |
715 | int rc; | |
716 | ||
717 | udev_device_unref(udev_device); | |
718 | ||
719 | /* if something is queued, get next device */ | |
720 | pfd[0].fd = udev_monitor->sock; | |
721 | pfd[0].events = POLLIN; | |
722 | rc = poll(pfd, 1, 0); | |
723 | if (rc > 0) | |
724 | goto retry; | |
725 | ||
726 | errno = EAGAIN; | |
727 | return NULL; | |
728 | } | |
729 | ||
730 | return udev_device; | |
731 | } | |
732 | ||
733 | int udev_monitor_send_device(struct udev_monitor *udev_monitor, | |
734 | struct udev_monitor *destination, struct udev_device *udev_device) | |
735 | { | |
736 | const char *buf, *val; | |
737 | ssize_t blen, count; | |
738 | struct udev_monitor_netlink_header nlh = { | |
739 | .prefix = "libudev", | |
740 | .magic = htobe32(UDEV_MONITOR_MAGIC), | |
741 | .header_size = sizeof nlh, | |
742 | }; | |
743 | struct iovec iov[2] = { | |
744 | { .iov_base = &nlh, .iov_len = sizeof nlh }, | |
745 | }; | |
746 | struct msghdr smsg = { | |
747 | .msg_iov = iov, | |
748 | .msg_iovlen = 2, | |
749 | }; | |
750 | struct udev_list_entry *list_entry; | |
751 | uint64_t tag_bloom_bits; | |
752 | ||
753 | blen = udev_device_get_properties_monitor_buf(udev_device, &buf); | |
754 | if (blen < 32) { | |
755 | log_debug("device buffer is too small to contain a valid device"); | |
756 | return -EINVAL; | |
757 | } | |
758 | ||
759 | /* fill in versioned header */ | |
760 | val = udev_device_get_subsystem(udev_device); | |
761 | nlh.filter_subsystem_hash = htobe32(util_string_hash32(val)); | |
762 | ||
763 | val = udev_device_get_devtype(udev_device); | |
764 | if (val != NULL) | |
765 | nlh.filter_devtype_hash = htobe32(util_string_hash32(val)); | |
766 | ||
767 | /* add tag bloom filter */ | |
768 | tag_bloom_bits = 0; | |
769 | udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(udev_device)) | |
770 | tag_bloom_bits |= util_string_bloom64(udev_list_entry_get_name(list_entry)); | |
771 | if (tag_bloom_bits > 0) { | |
772 | nlh.filter_tag_bloom_hi = htobe32(tag_bloom_bits >> 32); | |
773 | nlh.filter_tag_bloom_lo = htobe32(tag_bloom_bits & 0xffffffff); | |
774 | } | |
775 | ||
776 | /* add properties list */ | |
777 | nlh.properties_off = iov[0].iov_len; | |
778 | nlh.properties_len = blen; | |
779 | iov[1].iov_base = (char *)buf; | |
780 | iov[1].iov_len = blen; | |
781 | ||
782 | /* | |
783 | * Use custom address for target, or the default one. | |
784 | * | |
785 | * If we send to a multicast group, we will get | |
786 | * ECONNREFUSED, which is expected. | |
787 | */ | |
788 | if (destination) | |
789 | smsg.msg_name = &destination->snl; | |
790 | else | |
791 | smsg.msg_name = &udev_monitor->snl_destination; | |
792 | smsg.msg_namelen = sizeof(struct sockaddr_nl); | |
793 | count = sendmsg(udev_monitor->sock, &smsg, 0); | |
794 | if (count < 0) { | |
795 | if (!destination && errno == ECONNREFUSED) { | |
796 | log_debug("passed device to netlink monitor %p", udev_monitor); | |
797 | return 0; | |
798 | } else | |
799 | return -errno; | |
800 | } | |
801 | ||
802 | log_debug("passed %zi byte device to netlink monitor %p", count, udev_monitor); | |
803 | return count; | |
804 | } | |
805 | ||
806 | /** | |
807 | * udev_monitor_filter_add_match_subsystem_devtype: | |
808 | * @udev_monitor: the monitor | |
809 | * @subsystem: the subsystem value to match the incoming devices against | |
810 | * @devtype: the devtype value to match the incoming devices against | |
811 | * | |
812 | * This filter is efficiently executed inside the kernel, and libudev subscribers | |
813 | * will usually not be woken up for devices which do not match. | |
814 | * | |
815 | * The filter must be installed before the monitor is switched to listening mode. | |
816 | * | |
817 | * Returns: 0 on success, otherwise a negative error value. | |
818 | */ | |
819 | _public_ int udev_monitor_filter_add_match_subsystem_devtype(struct udev_monitor *udev_monitor, const char *subsystem, const char *devtype) | |
820 | { | |
821 | if (udev_monitor == NULL) | |
822 | return -EINVAL; | |
823 | if (subsystem == NULL) | |
824 | return -EINVAL; | |
825 | if (udev_list_entry_add(&udev_monitor->filter_subsystem_list, subsystem, devtype) == NULL) | |
826 | return -ENOMEM; | |
827 | return 0; | |
828 | } | |
829 | ||
830 | /** | |
831 | * udev_monitor_filter_add_match_tag: | |
832 | * @udev_monitor: the monitor | |
833 | * @tag: the name of a tag | |
834 | * | |
835 | * This filter is efficiently executed inside the kernel, and libudev subscribers | |
836 | * will usually not be woken up for devices which do not match. | |
837 | * | |
838 | * The filter must be installed before the monitor is switched to listening mode. | |
839 | * | |
840 | * Returns: 0 on success, otherwise a negative error value. | |
841 | */ | |
842 | _public_ int udev_monitor_filter_add_match_tag(struct udev_monitor *udev_monitor, const char *tag) | |
843 | { | |
844 | if (udev_monitor == NULL) | |
845 | return -EINVAL; | |
846 | if (tag == NULL) | |
847 | return -EINVAL; | |
848 | if (udev_list_entry_add(&udev_monitor->filter_tag_list, tag, NULL) == NULL) | |
849 | return -ENOMEM; | |
850 | return 0; | |
851 | } | |
852 | ||
853 | /** | |
854 | * udev_monitor_filter_remove: | |
855 | * @udev_monitor: monitor | |
856 | * | |
857 | * Remove all filters from monitor. | |
858 | * | |
859 | * Returns: 0 on success, otherwise a negative error value. | |
860 | */ | |
861 | _public_ int udev_monitor_filter_remove(struct udev_monitor *udev_monitor) | |
862 | { | |
863 | static const struct sock_fprog filter = { 0, NULL }; | |
864 | ||
865 | udev_list_cleanup(&udev_monitor->filter_subsystem_list); | |
866 | if (setsockopt(udev_monitor->sock, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter)) < 0) | |
867 | return -errno; | |
868 | ||
869 | return 0; | |
870 | } |