1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2008-2012 Kay Sievers <kay@vrfy.org>
9 #include <linux/filter.h>
10 #include <linux/netlink.h>
16 #include <sys/socket.h>
21 #include "alloc-util.h"
24 #include "format-util.h"
25 #include "libudev-private.h"
27 #include "mount-util.h"
28 #include "socket-util.h"
29 #include "string-util.h"
32 * SECTION:libudev-monitor
33 * @short_description: device event source
35 * Connects to a device event source.
41 * Opaque object handling an event source.
47 union sockaddr_union snl
;
48 union sockaddr_union snl_trusted_sender
;
49 union sockaddr_union snl_destination
;
51 struct udev_list filter_subsystem_list
;
52 struct udev_list filter_tag_list
;
56 enum udev_monitor_netlink_group
{
62 #define UDEV_MONITOR_MAGIC 0xfeedcafe
63 struct udev_monitor_netlink_header
{
64 /* "libudev" prefix to distinguish libudev and kernel messages */
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
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
;
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
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
;
86 static struct udev_monitor
*udev_monitor_new(struct udev
*udev
) {
87 struct udev_monitor
*udev_monitor
;
89 udev_monitor
= new0(struct udev_monitor
, 1);
90 if (udev_monitor
== NULL
) {
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);
101 /* we consider udev running when /dev is on devtmpfs */
102 static bool udev_has_devtmpfs(struct udev
*udev
) {
104 _cleanup_fclose_
FILE *f
= NULL
;
105 char line
[LINE_MAX
], *e
;
108 r
= path_get_mnt_id("/dev", &mount_id
);
110 if (r
!= -EOPNOTSUPP
)
111 log_debug_errno(r
, "name_to_handle_at on /dev: %m");
116 f
= fopen("/proc/self/mountinfo", "re");
120 FOREACH_LINE(line
, f
, return false) {
123 if (sscanf(line
, "%i", &mid
) != 1)
129 e
= strstr(line
, " - ");
133 /* accept any name that starts with the currently expected type */
134 if (startswith(e
+ 3, "devtmpfs"))
141 static void monitor_set_nl_address(struct udev_monitor
*udev_monitor
) {
142 union sockaddr_union snl
;
146 assert(udev_monitor
);
148 /* get the address the kernel has assigned us
149 * it is usually, but not necessarily the pid
151 addrlen
= sizeof(struct sockaddr_nl
);
152 r
= getsockname(udev_monitor
->sock
, &snl
.sa
, &addrlen
);
154 udev_monitor
->snl
.nl
.nl_pid
= snl
.nl
.nl_pid
;
157 struct udev_monitor
*udev_monitor_new_from_netlink_fd(struct udev
*udev
, const char *name
, int fd
) {
158 struct udev_monitor
*udev_monitor
;
167 group
= UDEV_MONITOR_NONE
;
168 else if (streq(name
, "udev")) {
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
175 * Containers will currently not get any udev uevents, until
176 * a supporting infrastructure is available.
178 * We do not set a netlink multicast group here, so the socket
179 * will not receive any messages.
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
;
185 group
= UDEV_MONITOR_UDEV
;
186 } else if (streq(name
, "kernel"))
187 group
= UDEV_MONITOR_KERNEL
;
193 udev_monitor
= udev_monitor_new(udev
);
194 if (udev_monitor
== NULL
)
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
);
204 udev_monitor
->bound
= true;
205 udev_monitor
->sock
= fd
;
206 monitor_set_nl_address(udev_monitor
);
209 udev_monitor
->snl
.nl
.nl_family
= AF_NETLINK
;
210 udev_monitor
->snl
.nl
.nl_groups
= group
;
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
;
220 * udev_monitor_new_from_netlink:
221 * @udev: udev library context
222 * @name: name of event source
224 * Create new udev monitor and connect to a specified event
225 * source. Valid sources identifiers are "udev" and "kernel".
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
236 * The initial refcount is 1, and needs to be decremented to
237 * release the resources of the udev monitor.
239 * Returns: a new udev monitor, or #NULL, in case of an error
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);
245 static inline void bpf_stmt(struct sock_filter
*inss
, unsigned int *i
,
246 unsigned short code
, unsigned int data
)
248 struct sock_filter
*ins
= &inss
[*i
];
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
)
259 struct sock_filter
*ins
= &inss
[*i
];
269 * udev_monitor_filter_update:
270 * @udev_monitor: monitor
272 * Update the installed socket filter. This is only needed,
273 * if the filter was removed or changed.
275 * Returns: 0 on success, otherwise a negative error value.
277 _public_
int udev_monitor_filter_update(struct udev_monitor
*udev_monitor
)
279 struct sock_filter ins
[512];
280 struct sock_fprog filter
;
282 struct udev_list_entry
*list_entry
;
285 if (udev_list_get_entry(&udev_monitor
->filter_subsystem_list
) == NULL
&&
286 udev_list_get_entry(&udev_monitor
->filter_tag_list
) == NULL
)
289 memzero(ins
, sizeof(ins
));
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);
299 if (udev_list_get_entry(&udev_monitor
->filter_tag_list
) != NULL
) {
302 /* count tag matches, to calculate end of tag match block */
304 udev_list_entry_foreach(list_entry
, udev_list_get_entry(&udev_monitor
->filter_tag_list
))
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;
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);
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 */
326 bpf_jmp(ins
, &i
, BPF_JMP
|BPF_JEQ
|BPF_K
, tag_bloom_lo
, 1 + (tag_matches
* 6), 0);
329 /* nothing matched, drop packet */
330 bpf_stmt(ins
, &i
, BPF_RET
|BPF_K
, 0);
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
));
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);
344 /* jump if subsystem does not match */
345 bpf_jmp(ins
, &i
, BPF_JMP
|BPF_JEQ
|BPF_K
, hash
, 0, 3);
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);
354 /* matched, pass packet */
355 bpf_stmt(ins
, &i
, BPF_RET
|BPF_K
, 0xffffffff);
357 if (i
+1 >= ELEMENTSOF(ins
))
361 /* nothing matched, drop packet */
362 bpf_stmt(ins
, &i
, BPF_RET
|BPF_K
, 0);
365 /* matched, pass packet */
366 bpf_stmt(ins
, &i
, BPF_RET
|BPF_K
, 0xffffffff);
369 memzero(&filter
, sizeof(filter
));
372 err
= setsockopt(udev_monitor
->sock
, SOL_SOCKET
, SO_ATTACH_FILTER
, &filter
, sizeof(filter
));
373 return err
< 0 ? -errno
: 0;
376 int udev_monitor_allow_unicast_sender(struct udev_monitor
*udev_monitor
, struct udev_monitor
*sender
)
378 udev_monitor
->snl_trusted_sender
.nl
.nl_pid
= sender
->snl
.nl
.nl_pid
;
383 * udev_monitor_enable_receiving:
384 * @udev_monitor: the monitor which should receive events
386 * Binds the @udev_monitor socket to the event source.
388 * Returns: 0 on success, otherwise a negative error value.
390 _public_
int udev_monitor_enable_receiving(struct udev_monitor
*udev_monitor
)
395 udev_monitor_filter_update(udev_monitor
);
397 if (!udev_monitor
->bound
) {
398 err
= bind(udev_monitor
->sock
,
399 &udev_monitor
->snl
.sa
, sizeof(struct sockaddr_nl
));
401 udev_monitor
->bound
= true;
405 monitor_set_nl_address(udev_monitor
);
407 return log_debug_errno(errno
, "bind failed: %m");
409 /* enable receiving of sender credentials */
410 err
= setsockopt(udev_monitor
->sock
, SOL_SOCKET
, SO_PASSCRED
, &on
, sizeof(on
));
412 log_debug_errno(errno
, "setting SO_PASSCRED failed: %m");
418 * udev_monitor_set_receive_buffer_size:
419 * @udev_monitor: the monitor which should receive events
420 * @size: the size in bytes
422 * Set the size of the kernel socket buffer. This call needs the
423 * appropriate privileges to succeed.
425 * Returns: 0 on success, otherwise -1 on error.
427 _public_
int udev_monitor_set_receive_buffer_size(struct udev_monitor
*udev_monitor
, int size
)
429 if (udev_monitor
== NULL
)
431 if (setsockopt(udev_monitor
->sock
, SOL_SOCKET
, SO_RCVBUFFORCE
, &size
, sizeof(size
)) < 0)
437 int udev_monitor_disconnect(struct udev_monitor
*udev_monitor
)
441 err
= close(udev_monitor
->sock
);
442 udev_monitor
->sock
= -1;
443 return err
< 0 ? -errno
: 0;
448 * @udev_monitor: udev monitor
450 * Take a reference of a udev monitor.
452 * Returns: the passed udev monitor
454 _public_
struct udev_monitor
*udev_monitor_ref(struct udev_monitor
*udev_monitor
)
456 if (udev_monitor
== NULL
)
458 udev_monitor
->refcount
++;
463 * udev_monitor_unref:
464 * @udev_monitor: udev monitor
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
472 _public_
struct udev_monitor
*udev_monitor_unref(struct udev_monitor
*udev_monitor
)
474 if (udev_monitor
== NULL
)
476 udev_monitor
->refcount
--;
477 if (udev_monitor
->refcount
> 0)
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
);
487 * udev_monitor_get_udev:
488 * @udev_monitor: udev monitor
490 * Retrieve the udev library context the monitor was created with.
492 * Returns: the udev library context
494 _public_
struct udev
*udev_monitor_get_udev(struct udev_monitor
*udev_monitor
)
496 if (udev_monitor
== NULL
)
498 return udev_monitor
->udev
;
502 * udev_monitor_get_fd:
503 * @udev_monitor: udev monitor
505 * Retrieve the socket file descriptor associated with the monitor.
507 * Returns: the socket file descriptor
509 _public_
int udev_monitor_get_fd(struct udev_monitor
*udev_monitor
)
511 if (udev_monitor
== NULL
)
513 return udev_monitor
->sock
;
516 static int passes_filter(struct udev_monitor
*udev_monitor
, struct udev_device
*udev_device
)
518 struct udev_list_entry
*list_entry
;
520 if (udev_list_get_entry(&udev_monitor
->filter_subsystem_list
) == NULL
)
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
);
526 const char *ddevtype
;
528 if (!streq(dsubsys
, subsys
))
531 devtype
= udev_list_entry_get_value(list_entry
);
534 ddevtype
= udev_device_get_devtype(udev_device
);
535 if (ddevtype
== NULL
)
537 if (streq(ddevtype
, devtype
))
543 if (udev_list_get_entry(&udev_monitor
->filter_tag_list
) == NULL
)
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
);
548 if (udev_device_has_tag(udev_device
, tag
))
555 * udev_monitor_receive_device:
556 * @udev_monitor: udev monitor
558 * Receive data from the udev monitor socket, allocate a new udev
559 * device, fill in the received data, and return the device.
561 * Only socket connections with uid=0 are accepted.
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.
568 * The initial refcount is 1, and needs to be decremented to
569 * release the resources of the udev device.
571 * Returns: a new udev device, or #NULL, in case of an error
573 _public_
struct udev_device
*udev_monitor_receive_device(struct udev_monitor
*udev_monitor
)
575 struct udev_device
*udev_device
;
578 char cred_msg
[CMSG_SPACE(sizeof(struct ucred
))];
579 struct cmsghdr
*cmsg
;
580 union sockaddr_union snl
;
583 struct udev_monitor_netlink_header nlh
;
588 bool is_initialized
= false;
591 if (udev_monitor
== NULL
) {
596 iov
.iov_len
= sizeof(buf
);
597 memzero(&smsg
, sizeof(struct msghdr
));
600 smsg
.msg_control
= cred_msg
;
601 smsg
.msg_controllen
= sizeof(cred_msg
);
602 smsg
.msg_name
= &snl
;
603 smsg
.msg_namelen
= sizeof(snl
);
605 buflen
= recvmsg(udev_monitor
->sock
, &smsg
, 0);
608 log_debug("unable to receive message");
612 if (buflen
< 32 || (smsg
.msg_flags
& MSG_TRUNC
)) {
613 log_debug("invalid message length");
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");
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",
635 cmsg
= CMSG_FIRSTHDR(&smsg
);
636 if (cmsg
== NULL
|| cmsg
->cmsg_type
!= SCM_CREDENTIALS
) {
637 log_debug("no sender credentials received, message ignored");
642 cred
= (struct ucred
*)CMSG_DATA(cmsg
);
643 if (cred
->uid
!= 0) {
644 log_debug("sender uid="UID_FMT
", message ignored", cred
->uid
);
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
));
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
);
664 bufpos
= buf
.nlh
.properties_off
;
666 /* devices received from udev are always initialized */
667 is_initialized
= true;
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");
677 /* check message header */
678 if (strstr(buf
.raw
, "@/") == NULL
) {
679 log_debug("unrecognized message header");
685 udev_device
= udev_device_new_from_nulstr(udev_monitor
->udev
, &buf
.raw
[bufpos
], buflen
- bufpos
);
687 log_debug_errno(errno
, "could not create device: %m");
692 udev_device_set_is_initialized(udev_device
);
694 /* skip device, if it does not pass the current filter */
695 if (!passes_filter(udev_monitor
, udev_device
)) {
696 struct pollfd pfd
[1];
699 udev_device_unref(udev_device
);
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);
715 int udev_monitor_send_device(struct udev_monitor
*udev_monitor
,
716 struct udev_monitor
*destination
, struct udev_device
*udev_device
)
718 const char *buf
, *val
;
720 struct udev_monitor_netlink_header nlh
= {
722 .magic
= htobe32(UDEV_MONITOR_MAGIC
),
723 .header_size
= sizeof nlh
,
725 struct iovec iov
[2] = {
726 { .iov_base
= &nlh
, .iov_len
= sizeof nlh
},
728 struct msghdr smsg
= {
732 struct udev_list_entry
*list_entry
;
733 uint64_t tag_bloom_bits
;
735 blen
= udev_device_get_properties_monitor_buf(udev_device
, &buf
);
737 log_debug("device buffer is too small to contain a valid device");
741 /* fill in versioned header */
742 val
= udev_device_get_subsystem(udev_device
);
743 nlh
.filter_subsystem_hash
= htobe32(util_string_hash32(val
));
745 val
= udev_device_get_devtype(udev_device
);
747 nlh
.filter_devtype_hash
= htobe32(util_string_hash32(val
));
749 /* add tag bloom filter */
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);
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
;
765 * Use custom address for target, or the default one.
767 * If we send to a multicast group, we will get
768 * ECONNREFUSED, which is expected.
771 smsg
.msg_name
= &destination
->snl
;
773 smsg
.msg_name
= &udev_monitor
->snl_destination
;
774 smsg
.msg_namelen
= sizeof(struct sockaddr_nl
);
775 count
= sendmsg(udev_monitor
->sock
, &smsg
, 0);
777 if (!destination
&& errno
== ECONNREFUSED
) {
778 log_debug("passed device to netlink monitor %p", udev_monitor
);
784 log_debug("passed %zi byte device to netlink monitor %p", count
, udev_monitor
);
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
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.
797 * The filter must be installed before the monitor is switched to listening mode.
799 * Returns: 0 on success, otherwise a negative error value.
801 _public_
int udev_monitor_filter_add_match_subsystem_devtype(struct udev_monitor
*udev_monitor
, const char *subsystem
, const char *devtype
)
803 if (udev_monitor
== NULL
)
805 if (subsystem
== NULL
)
807 if (udev_list_entry_add(&udev_monitor
->filter_subsystem_list
, subsystem
, devtype
) == NULL
)
813 * udev_monitor_filter_add_match_tag:
814 * @udev_monitor: the monitor
815 * @tag: the name of a tag
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.
820 * The filter must be installed before the monitor is switched to listening mode.
822 * Returns: 0 on success, otherwise a negative error value.
824 _public_
int udev_monitor_filter_add_match_tag(struct udev_monitor
*udev_monitor
, const char *tag
)
826 if (udev_monitor
== NULL
)
830 if (udev_list_entry_add(&udev_monitor
->filter_tag_list
, tag
, NULL
) == NULL
)
836 * udev_monitor_filter_remove:
837 * @udev_monitor: monitor
839 * Remove all filters from monitor.
841 * Returns: 0 on success, otherwise a negative error value.
843 _public_
int udev_monitor_filter_remove(struct udev_monitor
*udev_monitor
)
845 static const struct sock_fprog filter
= { 0, NULL
};
847 udev_list_cleanup(&udev_monitor
->filter_subsystem_list
);
848 if (setsockopt(udev_monitor
->sock
, SOL_SOCKET
, SO_ATTACH_FILTER
, &filter
, sizeof(filter
)) < 0)