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