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