1 /* SPDX-License-Identifier: LGPL-2.1+ */
4 #include <linux/filter.h>
5 #include <linux/netlink.h>
11 #include <sys/socket.h>
16 #include "alloc-util.h"
19 #include "format-util.h"
20 #include "libudev-private.h"
21 #include "libudev-device-internal.h"
23 #include "mount-util.h"
24 #include "socket-util.h"
25 #include "string-util.h"
28 * SECTION:libudev-monitor
29 * @short_description: device event source
31 * Connects to a device event source.
37 * Opaque object handling an event source.
43 union sockaddr_union snl
;
44 union sockaddr_union snl_trusted_sender
;
45 union sockaddr_union snl_destination
;
47 struct udev_list filter_subsystem_list
;
48 struct udev_list filter_tag_list
;
52 enum udev_monitor_netlink_group
{
58 #define UDEV_MONITOR_MAGIC 0xfeedcafe
59 struct udev_monitor_netlink_header
{
60 /* "libudev" prefix to distinguish libudev and kernel messages */
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
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
;
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
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
;
82 static struct udev_monitor
*udev_monitor_new(struct udev
*udev
) {
83 struct udev_monitor
*udev_monitor
;
85 udev_monitor
= new0(struct udev_monitor
, 1);
86 if (udev_monitor
== NULL
) {
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);
97 /* we consider udev running when /dev is on devtmpfs */
98 static bool udev_has_devtmpfs(struct udev
*udev
) {
100 _cleanup_fclose_
FILE *f
= NULL
;
101 char line
[LINE_MAX
], *e
;
104 r
= path_get_mnt_id("/dev", &mount_id
);
106 if (r
!= -EOPNOTSUPP
)
107 log_debug_errno(r
, "name_to_handle_at on /dev: %m");
112 f
= fopen("/proc/self/mountinfo", "re");
116 FOREACH_LINE(line
, f
, return false) {
119 if (sscanf(line
, "%i", &mid
) != 1)
125 e
= strstr(line
, " - ");
129 /* accept any name that starts with the currently expected type */
130 if (startswith(e
+ 3, "devtmpfs"))
137 static void monitor_set_nl_address(struct udev_monitor
*udev_monitor
) {
138 union sockaddr_union snl
;
142 assert(udev_monitor
);
144 /* get the address the kernel has assigned us
145 * it is usually, but not necessarily the pid
147 addrlen
= sizeof(struct sockaddr_nl
);
148 r
= getsockname(udev_monitor
->sock
, &snl
.sa
, &addrlen
);
150 udev_monitor
->snl
.nl
.nl_pid
= snl
.nl
.nl_pid
;
153 struct udev_monitor
*udev_monitor_new_from_netlink_fd(struct udev
*udev
, const char *name
, int fd
) {
154 struct udev_monitor
*udev_monitor
;
158 group
= UDEV_MONITOR_NONE
;
159 else if (streq(name
, "udev")) {
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
166 * Containers will currently not get any udev uevents, until
167 * a supporting infrastructure is available.
169 * We do not set a netlink multicast group here, so the socket
170 * will not receive any messages.
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
;
176 group
= UDEV_MONITOR_UDEV
;
177 } else if (streq(name
, "kernel"))
178 group
= UDEV_MONITOR_KERNEL
;
184 udev_monitor
= udev_monitor_new(udev
);
185 if (udev_monitor
== NULL
)
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
);
195 udev_monitor
->bound
= true;
196 udev_monitor
->sock
= fd
;
197 monitor_set_nl_address(udev_monitor
);
200 udev_monitor
->snl
.nl
.nl_family
= AF_NETLINK
;
201 udev_monitor
->snl
.nl
.nl_groups
= group
;
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
;
211 * udev_monitor_new_from_netlink:
212 * @udev: udev library context
213 * @name: name of event source
215 * Create new udev monitor and connect to a specified event
216 * source. Valid sources identifiers are "udev" and "kernel".
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
227 * The initial refcount is 1, and needs to be decremented to
228 * release the resources of the udev monitor.
230 * Returns: a new udev monitor, or #NULL, in case of an error
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);
236 static inline void bpf_stmt(struct sock_filter
*inss
, unsigned int *i
,
237 unsigned short code
, unsigned int data
)
239 struct sock_filter
*ins
= &inss
[*i
];
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
)
250 struct sock_filter
*ins
= &inss
[*i
];
260 * udev_monitor_filter_update:
261 * @udev_monitor: monitor
263 * Update the installed socket filter. This is only needed,
264 * if the filter was removed or changed.
266 * Returns: 0 on success, otherwise a negative error value.
268 _public_
int udev_monitor_filter_update(struct udev_monitor
*udev_monitor
)
270 struct sock_filter ins
[512];
271 struct sock_fprog filter
;
273 struct udev_list_entry
*list_entry
;
276 if (udev_list_get_entry(&udev_monitor
->filter_subsystem_list
) == NULL
&&
277 udev_list_get_entry(&udev_monitor
->filter_tag_list
) == NULL
)
280 memzero(ins
, sizeof(ins
));
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);
290 if (udev_list_get_entry(&udev_monitor
->filter_tag_list
) != NULL
) {
293 /* count tag matches, to calculate end of tag match block */
295 udev_list_entry_foreach(list_entry
, udev_list_get_entry(&udev_monitor
->filter_tag_list
))
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;
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);
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 */
317 bpf_jmp(ins
, &i
, BPF_JMP
|BPF_JEQ
|BPF_K
, tag_bloom_lo
, 1 + (tag_matches
* 6), 0);
320 /* nothing matched, drop packet */
321 bpf_stmt(ins
, &i
, BPF_RET
|BPF_K
, 0);
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
));
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);
335 /* jump if subsystem does not match */
336 bpf_jmp(ins
, &i
, BPF_JMP
|BPF_JEQ
|BPF_K
, hash
, 0, 3);
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);
345 /* matched, pass packet */
346 bpf_stmt(ins
, &i
, BPF_RET
|BPF_K
, 0xffffffff);
348 if (i
+1 >= ELEMENTSOF(ins
))
352 /* nothing matched, drop packet */
353 bpf_stmt(ins
, &i
, BPF_RET
|BPF_K
, 0);
356 /* matched, pass packet */
357 bpf_stmt(ins
, &i
, BPF_RET
|BPF_K
, 0xffffffff);
360 memzero(&filter
, sizeof(filter
));
363 err
= setsockopt(udev_monitor
->sock
, SOL_SOCKET
, SO_ATTACH_FILTER
, &filter
, sizeof(filter
));
364 return err
< 0 ? -errno
: 0;
367 int udev_monitor_allow_unicast_sender(struct udev_monitor
*udev_monitor
, struct udev_monitor
*sender
)
369 udev_monitor
->snl_trusted_sender
.nl
.nl_pid
= sender
->snl
.nl
.nl_pid
;
374 * udev_monitor_enable_receiving:
375 * @udev_monitor: the monitor which should receive events
377 * Binds the @udev_monitor socket to the event source.
379 * Returns: 0 on success, otherwise a negative error value.
381 _public_
int udev_monitor_enable_receiving(struct udev_monitor
*udev_monitor
)
386 udev_monitor_filter_update(udev_monitor
);
388 if (!udev_monitor
->bound
) {
389 err
= bind(udev_monitor
->sock
,
390 &udev_monitor
->snl
.sa
, sizeof(struct sockaddr_nl
));
392 udev_monitor
->bound
= true;
396 monitor_set_nl_address(udev_monitor
);
398 return log_debug_errno(errno
, "bind failed: %m");
400 /* enable receiving of sender credentials */
401 err
= setsockopt(udev_monitor
->sock
, SOL_SOCKET
, SO_PASSCRED
, &on
, sizeof(on
));
403 log_debug_errno(errno
, "setting SO_PASSCRED failed: %m");
409 * udev_monitor_set_receive_buffer_size:
410 * @udev_monitor: the monitor which should receive events
411 * @size: the size in bytes
413 * Set the size of the kernel socket buffer. This call needs the
414 * appropriate privileges to succeed.
416 * Returns: 0 on success, otherwise -1 on error.
418 _public_
int udev_monitor_set_receive_buffer_size(struct udev_monitor
*udev_monitor
, int size
)
420 if (udev_monitor
== NULL
)
422 if (setsockopt(udev_monitor
->sock
, SOL_SOCKET
, SO_RCVBUFFORCE
, &size
, sizeof(size
)) < 0)
428 int udev_monitor_disconnect(struct udev_monitor
*udev_monitor
)
432 err
= close(udev_monitor
->sock
);
433 udev_monitor
->sock
= -1;
434 return err
< 0 ? -errno
: 0;
439 * @udev_monitor: udev monitor
441 * Take a reference of a udev monitor.
443 * Returns: the passed udev monitor
445 _public_
struct udev_monitor
*udev_monitor_ref(struct udev_monitor
*udev_monitor
)
447 if (udev_monitor
== NULL
)
449 udev_monitor
->refcount
++;
454 * udev_monitor_unref:
455 * @udev_monitor: udev monitor
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
463 _public_
struct udev_monitor
*udev_monitor_unref(struct udev_monitor
*udev_monitor
)
465 if (udev_monitor
== NULL
)
467 udev_monitor
->refcount
--;
468 if (udev_monitor
->refcount
> 0)
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
);
478 * udev_monitor_get_udev:
479 * @udev_monitor: udev monitor
481 * Retrieve the udev library context the monitor was created with.
483 * Returns: the udev library context
485 _public_
struct udev
*udev_monitor_get_udev(struct udev_monitor
*udev_monitor
)
487 if (udev_monitor
== NULL
)
489 return udev_monitor
->udev
;
493 * udev_monitor_get_fd:
494 * @udev_monitor: udev monitor
496 * Retrieve the socket file descriptor associated with the monitor.
498 * Returns: the socket file descriptor
500 _public_
int udev_monitor_get_fd(struct udev_monitor
*udev_monitor
)
502 if (udev_monitor
== NULL
)
504 return udev_monitor
->sock
;
507 static int passes_filter(struct udev_monitor
*udev_monitor
, struct udev_device
*udev_device
)
509 struct udev_list_entry
*list_entry
;
511 if (udev_list_get_entry(&udev_monitor
->filter_subsystem_list
) == NULL
)
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
);
517 const char *ddevtype
;
519 if (!streq(dsubsys
, subsys
))
522 devtype
= udev_list_entry_get_value(list_entry
);
525 ddevtype
= udev_device_get_devtype(udev_device
);
526 if (ddevtype
== NULL
)
528 if (streq(ddevtype
, devtype
))
534 if (udev_list_get_entry(&udev_monitor
->filter_tag_list
) == NULL
)
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
);
539 if (udev_device_has_tag(udev_device
, tag
))
546 * udev_monitor_receive_device:
547 * @udev_monitor: udev monitor
549 * Receive data from the udev monitor socket, allocate a new udev
550 * device, fill in the received data, and return the device.
552 * Only socket connections with uid=0 are accepted.
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.
559 * The initial refcount is 1, and needs to be decremented to
560 * release the resources of the udev device.
562 * Returns: a new udev device, or #NULL, in case of an error
564 _public_
struct udev_device
*udev_monitor_receive_device(struct udev_monitor
*udev_monitor
)
566 struct udev_device
*udev_device
;
569 char cred_msg
[CMSG_SPACE(sizeof(struct ucred
))];
570 struct cmsghdr
*cmsg
;
571 union sockaddr_union snl
;
574 struct udev_monitor_netlink_header nlh
;
579 bool is_initialized
= false;
582 if (udev_monitor
== NULL
) {
587 iov
.iov_len
= sizeof(buf
);
588 memzero(&smsg
, sizeof(struct msghdr
));
591 smsg
.msg_control
= cred_msg
;
592 smsg
.msg_controllen
= sizeof(cred_msg
);
593 smsg
.msg_name
= &snl
;
594 smsg
.msg_namelen
= sizeof(snl
);
596 buflen
= recvmsg(udev_monitor
->sock
, &smsg
, 0);
599 log_debug("unable to receive message");
603 if (buflen
< 32 || (smsg
.msg_flags
& MSG_TRUNC
)) {
604 log_debug("invalid message length");
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");
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",
626 cmsg
= CMSG_FIRSTHDR(&smsg
);
627 if (cmsg
== NULL
|| cmsg
->cmsg_type
!= SCM_CREDENTIALS
) {
628 log_debug("no sender credentials received, message ignored");
633 cred
= (struct ucred
*)CMSG_DATA(cmsg
);
634 if (cred
->uid
!= 0) {
635 log_debug("sender uid="UID_FMT
", message ignored", cred
->uid
);
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
));
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
);
655 bufpos
= buf
.nlh
.properties_off
;
657 /* devices received from udev are always initialized */
658 is_initialized
= true;
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");
668 /* check message header */
669 if (strstr(buf
.raw
, "@/") == NULL
) {
670 log_debug("unrecognized message header");
676 udev_device
= udev_device_new_from_nulstr(udev_monitor
->udev
, &buf
.raw
[bufpos
], buflen
- bufpos
);
678 log_debug_errno(errno
, "could not create device: %m");
683 udev_device_set_is_initialized(udev_device
);
685 /* skip device, if it does not pass the current filter */
686 if (!passes_filter(udev_monitor
, udev_device
)) {
687 struct pollfd pfd
[1];
690 udev_device_unref(udev_device
);
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);
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
;
711 udev_device
= udev_monitor_receive_device(udev_monitor
);
715 *ret
= sd_device_ref(udev_device
->device
);
719 int udev_monitor_send_device(struct udev_monitor
*udev_monitor
,
720 struct udev_monitor
*destination
, struct udev_device
*udev_device
)
722 const char *buf
, *val
;
724 struct udev_monitor_netlink_header nlh
= {
726 .magic
= htobe32(UDEV_MONITOR_MAGIC
),
727 .header_size
= sizeof nlh
,
729 struct iovec iov
[2] = {
730 { .iov_base
= &nlh
, .iov_len
= sizeof nlh
},
732 struct msghdr smsg
= {
736 struct udev_list_entry
*list_entry
;
737 uint64_t tag_bloom_bits
;
739 blen
= udev_device_get_properties_monitor_buf(udev_device
, &buf
);
741 log_debug("device buffer is too small to contain a valid device");
745 /* fill in versioned header */
746 val
= udev_device_get_subsystem(udev_device
);
747 nlh
.filter_subsystem_hash
= htobe32(util_string_hash32(val
));
749 val
= udev_device_get_devtype(udev_device
);
751 nlh
.filter_devtype_hash
= htobe32(util_string_hash32(val
));
753 /* add tag bloom filter */
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);
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
;
769 * Use custom address for target, or the default one.
771 * If we send to a multicast group, we will get
772 * ECONNREFUSED, which is expected.
775 smsg
.msg_name
= &destination
->snl
;
777 smsg
.msg_name
= &udev_monitor
->snl_destination
;
778 smsg
.msg_namelen
= sizeof(struct sockaddr_nl
);
779 count
= sendmsg(udev_monitor
->sock
, &smsg
, 0);
781 if (!destination
&& errno
== ECONNREFUSED
) {
782 log_debug("passed device to netlink monitor %p", udev_monitor
);
788 log_debug("passed %zi byte device to netlink monitor %p", count
, udev_monitor
);
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
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.
801 * The filter must be installed before the monitor is switched to listening mode.
803 * Returns: 0 on success, otherwise a negative error value.
805 _public_
int udev_monitor_filter_add_match_subsystem_devtype(struct udev_monitor
*udev_monitor
, const char *subsystem
, const char *devtype
)
807 if (udev_monitor
== NULL
)
809 if (subsystem
== NULL
)
811 if (udev_list_entry_add(&udev_monitor
->filter_subsystem_list
, subsystem
, devtype
) == NULL
)
817 * udev_monitor_filter_add_match_tag:
818 * @udev_monitor: the monitor
819 * @tag: the name of a tag
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.
824 * The filter must be installed before the monitor is switched to listening mode.
826 * Returns: 0 on success, otherwise a negative error value.
828 _public_
int udev_monitor_filter_add_match_tag(struct udev_monitor
*udev_monitor
, const char *tag
)
830 if (udev_monitor
== NULL
)
834 if (udev_list_entry_add(&udev_monitor
->filter_tag_list
, tag
, NULL
) == NULL
)
840 * udev_monitor_filter_remove:
841 * @udev_monitor: monitor
843 * Remove all filters from monitor.
845 * Returns: 0 on success, otherwise a negative error value.
847 _public_
int udev_monitor_filter_remove(struct udev_monitor
*udev_monitor
)
849 static const struct sock_fprog filter
= { 0, NULL
};
851 udev_list_cleanup(&udev_monitor
->filter_subsystem_list
);
852 if (setsockopt(udev_monitor
->sock
, SOL_SOCKET
, SO_ATTACH_FILTER
, &filter
, sizeof(filter
)) < 0)