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