1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2008-2012 Kay Sievers <kay@vrfy.org>
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <linux/filter.h>
23 #include <linux/netlink.h>
29 #include <sys/socket.h>
34 #include "alloc-util.h"
37 #include "format-util.h"
38 #include "libudev-private.h"
40 #include "mount-util.h"
41 #include "socket-util.h"
42 #include "string-util.h"
45 * SECTION:libudev-monitor
46 * @short_description: device event source
48 * Connects to a device event source.
54 * Opaque object handling an event source.
60 union sockaddr_union snl
;
61 union sockaddr_union snl_trusted_sender
;
62 union sockaddr_union snl_destination
;
64 struct udev_list filter_subsystem_list
;
65 struct udev_list filter_tag_list
;
69 enum udev_monitor_netlink_group
{
75 #define UDEV_MONITOR_MAGIC 0xfeedcafe
76 struct udev_monitor_netlink_header
{
77 /* "libudev" prefix to distinguish libudev and kernel messages */
80 * magic to protect against daemon <-> library message format mismatch
81 * used in the kernel from socket filter rules; needs to be stored in network order
84 /* total length of header structure known to the sender */
85 unsigned int header_size
;
86 /* properties string buffer */
87 unsigned int properties_off
;
88 unsigned int properties_len
;
90 * hashes of primary device properties strings, to let libudev subscribers
91 * use in-kernel socket filters; values need to be stored in network order
93 unsigned int filter_subsystem_hash
;
94 unsigned int filter_devtype_hash
;
95 unsigned int filter_tag_bloom_hi
;
96 unsigned int filter_tag_bloom_lo
;
99 static struct udev_monitor
*udev_monitor_new(struct udev
*udev
)
101 struct udev_monitor
*udev_monitor
;
103 udev_monitor
= new0(struct udev_monitor
, 1);
104 if (udev_monitor
== NULL
) {
108 udev_monitor
->refcount
= 1;
109 udev_monitor
->udev
= udev
;
110 udev_list_init(udev
, &udev_monitor
->filter_subsystem_list
, false);
111 udev_list_init(udev
, &udev_monitor
->filter_tag_list
, true);
115 /* we consider udev running when /dev is on devtmpfs */
116 static bool udev_has_devtmpfs(struct udev
*udev
) {
118 union file_handle_union h
= FILE_HANDLE_INIT
;
119 _cleanup_fclose_
FILE *f
= NULL
;
120 char line
[LINE_MAX
], *e
;
124 r
= name_to_handle_at(AT_FDCWD
, "/dev", &h
.handle
, &mount_id
, 0);
126 if (errno
!= EOPNOTSUPP
)
127 log_debug_errno(errno
, "name_to_handle_at on /dev: %m");
131 f
= fopen("/proc/self/mountinfo", "re");
135 FOREACH_LINE(line
, f
, return false) {
138 if (sscanf(line
, "%i", &mid
) != 1)
144 e
= strstr(line
, " - ");
148 /* accept any name that starts with the currently expected type */
149 if (startswith(e
+ 3, "devtmpfs"))
156 static void monitor_set_nl_address(struct udev_monitor
*udev_monitor
) {
157 union sockaddr_union snl
;
161 assert(udev_monitor
);
163 /* get the address the kernel has assigned us
164 * it is usually, but not necessarily the pid
166 addrlen
= sizeof(struct sockaddr_nl
);
167 r
= getsockname(udev_monitor
->sock
, &snl
.sa
, &addrlen
);
169 udev_monitor
->snl
.nl
.nl_pid
= snl
.nl
.nl_pid
;
172 struct udev_monitor
*udev_monitor_new_from_netlink_fd(struct udev
*udev
, const char *name
, int fd
)
174 struct udev_monitor
*udev_monitor
;
183 group
= UDEV_MONITOR_NONE
;
184 else if (streq(name
, "udev")) {
186 * We do not support subscribing to uevents if no instance of
187 * udev is running. Uevents would otherwise broadcast the
188 * processing data of the host into containers, which is not
191 * Containers will currently not get any udev uevents, until
192 * a supporting infrastructure is available.
194 * We do not set a netlink multicast group here, so the socket
195 * will not receive any messages.
197 if (access("/run/udev/control", F_OK
) < 0 && !udev_has_devtmpfs(udev
)) {
198 log_debug("the udev service seems not to be active, disable the monitor");
199 group
= UDEV_MONITOR_NONE
;
201 group
= UDEV_MONITOR_UDEV
;
202 } else if (streq(name
, "kernel"))
203 group
= UDEV_MONITOR_KERNEL
;
209 udev_monitor
= udev_monitor_new(udev
);
210 if (udev_monitor
== NULL
)
214 udev_monitor
->sock
= socket(PF_NETLINK
, SOCK_RAW
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, NETLINK_KOBJECT_UEVENT
);
215 if (udev_monitor
->sock
< 0) {
216 log_debug_errno(errno
, "error getting socket: %m");
217 return mfree(udev_monitor
);
220 udev_monitor
->bound
= true;
221 udev_monitor
->sock
= fd
;
222 monitor_set_nl_address(udev_monitor
);
225 udev_monitor
->snl
.nl
.nl_family
= AF_NETLINK
;
226 udev_monitor
->snl
.nl
.nl_groups
= group
;
228 /* default destination for sending */
229 udev_monitor
->snl_destination
.nl
.nl_family
= AF_NETLINK
;
230 udev_monitor
->snl_destination
.nl
.nl_groups
= UDEV_MONITOR_UDEV
;
236 * udev_monitor_new_from_netlink:
237 * @udev: udev library context
238 * @name: name of event source
240 * Create new udev monitor and connect to a specified event
241 * source. Valid sources identifiers are "udev" and "kernel".
243 * Applications should usually not connect directly to the
244 * "kernel" events, because the devices might not be useable
245 * at that time, before udev has configured them, and created
246 * device nodes. Accessing devices at the same time as udev,
247 * might result in unpredictable behavior. The "udev" events
248 * are sent out after udev has finished its event processing,
249 * all rules have been processed, and needed device nodes are
252 * The initial refcount is 1, and needs to be decremented to
253 * release the resources of the udev monitor.
255 * Returns: a new udev monitor, or #NULL, in case of an error
257 _public_
struct udev_monitor
*udev_monitor_new_from_netlink(struct udev
*udev
, const char *name
)
259 return udev_monitor_new_from_netlink_fd(udev
, name
, -1);
262 static inline void bpf_stmt(struct sock_filter
*inss
, unsigned int *i
,
263 unsigned short code
, unsigned int data
)
265 struct sock_filter
*ins
= &inss
[*i
];
272 static inline void bpf_jmp(struct sock_filter
*inss
, unsigned int *i
,
273 unsigned short code
, unsigned int data
,
274 unsigned short jt
, unsigned short jf
)
276 struct sock_filter
*ins
= &inss
[*i
];
286 * udev_monitor_filter_update:
287 * @udev_monitor: monitor
289 * Update the installed socket filter. This is only needed,
290 * if the filter was removed or changed.
292 * Returns: 0 on success, otherwise a negative error value.
294 _public_
int udev_monitor_filter_update(struct udev_monitor
*udev_monitor
)
296 struct sock_filter ins
[512];
297 struct sock_fprog filter
;
299 struct udev_list_entry
*list_entry
;
302 if (udev_list_get_entry(&udev_monitor
->filter_subsystem_list
) == NULL
&&
303 udev_list_get_entry(&udev_monitor
->filter_tag_list
) == NULL
)
306 memzero(ins
, sizeof(ins
));
309 /* load magic in A */
310 bpf_stmt(ins
, &i
, BPF_LD
|BPF_W
|BPF_ABS
, offsetof(struct udev_monitor_netlink_header
, magic
));
311 /* jump if magic matches */
312 bpf_jmp(ins
, &i
, BPF_JMP
|BPF_JEQ
|BPF_K
, UDEV_MONITOR_MAGIC
, 1, 0);
313 /* wrong magic, pass packet */
314 bpf_stmt(ins
, &i
, BPF_RET
|BPF_K
, 0xffffffff);
316 if (udev_list_get_entry(&udev_monitor
->filter_tag_list
) != NULL
) {
319 /* count tag matches, to calculate end of tag match block */
321 udev_list_entry_foreach(list_entry
, udev_list_get_entry(&udev_monitor
->filter_tag_list
))
324 /* add all tags matches */
325 udev_list_entry_foreach(list_entry
, udev_list_get_entry(&udev_monitor
->filter_tag_list
)) {
326 uint64_t tag_bloom_bits
= util_string_bloom64(udev_list_entry_get_name(list_entry
));
327 uint32_t tag_bloom_hi
= tag_bloom_bits
>> 32;
328 uint32_t tag_bloom_lo
= tag_bloom_bits
& 0xffffffff;
330 /* load device bloom bits in A */
331 bpf_stmt(ins
, &i
, BPF_LD
|BPF_W
|BPF_ABS
, offsetof(struct udev_monitor_netlink_header
, filter_tag_bloom_hi
));
332 /* clear bits (tag bits & bloom bits) */
333 bpf_stmt(ins
, &i
, BPF_ALU
|BPF_AND
|BPF_K
, tag_bloom_hi
);
334 /* jump to next tag if it does not match */
335 bpf_jmp(ins
, &i
, BPF_JMP
|BPF_JEQ
|BPF_K
, tag_bloom_hi
, 0, 3);
337 /* load device bloom bits in A */
338 bpf_stmt(ins
, &i
, BPF_LD
|BPF_W
|BPF_ABS
, offsetof(struct udev_monitor_netlink_header
, filter_tag_bloom_lo
));
339 /* clear bits (tag bits & bloom bits) */
340 bpf_stmt(ins
, &i
, BPF_ALU
|BPF_AND
|BPF_K
, tag_bloom_lo
);
341 /* jump behind end of tag match block if tag matches */
343 bpf_jmp(ins
, &i
, BPF_JMP
|BPF_JEQ
|BPF_K
, tag_bloom_lo
, 1 + (tag_matches
* 6), 0);
346 /* nothing matched, drop packet */
347 bpf_stmt(ins
, &i
, BPF_RET
|BPF_K
, 0);
350 /* add all subsystem matches */
351 if (udev_list_get_entry(&udev_monitor
->filter_subsystem_list
) != NULL
) {
352 udev_list_entry_foreach(list_entry
, udev_list_get_entry(&udev_monitor
->filter_subsystem_list
)) {
353 unsigned int hash
= util_string_hash32(udev_list_entry_get_name(list_entry
));
355 /* load device subsystem value in A */
356 bpf_stmt(ins
, &i
, BPF_LD
|BPF_W
|BPF_ABS
, offsetof(struct udev_monitor_netlink_header
, filter_subsystem_hash
));
357 if (udev_list_entry_get_value(list_entry
) == NULL
) {
358 /* jump if subsystem does not match */
359 bpf_jmp(ins
, &i
, BPF_JMP
|BPF_JEQ
|BPF_K
, hash
, 0, 1);
361 /* jump if subsystem does not match */
362 bpf_jmp(ins
, &i
, BPF_JMP
|BPF_JEQ
|BPF_K
, hash
, 0, 3);
364 /* load device devtype value in A */
365 bpf_stmt(ins
, &i
, BPF_LD
|BPF_W
|BPF_ABS
, offsetof(struct udev_monitor_netlink_header
, filter_devtype_hash
));
366 /* jump if value does not match */
367 hash
= util_string_hash32(udev_list_entry_get_value(list_entry
));
368 bpf_jmp(ins
, &i
, BPF_JMP
|BPF_JEQ
|BPF_K
, hash
, 0, 1);
371 /* matched, pass packet */
372 bpf_stmt(ins
, &i
, BPF_RET
|BPF_K
, 0xffffffff);
374 if (i
+1 >= ELEMENTSOF(ins
))
378 /* nothing matched, drop packet */
379 bpf_stmt(ins
, &i
, BPF_RET
|BPF_K
, 0);
382 /* matched, pass packet */
383 bpf_stmt(ins
, &i
, BPF_RET
|BPF_K
, 0xffffffff);
386 memzero(&filter
, sizeof(filter
));
389 err
= setsockopt(udev_monitor
->sock
, SOL_SOCKET
, SO_ATTACH_FILTER
, &filter
, sizeof(filter
));
390 return err
< 0 ? -errno
: 0;
393 int udev_monitor_allow_unicast_sender(struct udev_monitor
*udev_monitor
, struct udev_monitor
*sender
)
395 udev_monitor
->snl_trusted_sender
.nl
.nl_pid
= sender
->snl
.nl
.nl_pid
;
400 * udev_monitor_enable_receiving:
401 * @udev_monitor: the monitor which should receive events
403 * Binds the @udev_monitor socket to the event source.
405 * Returns: 0 on success, otherwise a negative error value.
407 _public_
int udev_monitor_enable_receiving(struct udev_monitor
*udev_monitor
)
412 udev_monitor_filter_update(udev_monitor
);
414 if (!udev_monitor
->bound
) {
415 err
= bind(udev_monitor
->sock
,
416 &udev_monitor
->snl
.sa
, sizeof(struct sockaddr_nl
));
418 udev_monitor
->bound
= true;
422 monitor_set_nl_address(udev_monitor
);
424 return log_debug_errno(errno
, "bind failed: %m");
426 /* enable receiving of sender credentials */
427 err
= setsockopt(udev_monitor
->sock
, SOL_SOCKET
, SO_PASSCRED
, &on
, sizeof(on
));
429 log_debug_errno(errno
, "setting SO_PASSCRED failed: %m");
435 * udev_monitor_set_receive_buffer_size:
436 * @udev_monitor: the monitor which should receive events
437 * @size: the size in bytes
439 * Set the size of the kernel socket buffer. This call needs the
440 * appropriate privileges to succeed.
442 * Returns: 0 on success, otherwise -1 on error.
444 _public_
int udev_monitor_set_receive_buffer_size(struct udev_monitor
*udev_monitor
, int size
)
446 if (udev_monitor
== NULL
)
448 if (setsockopt(udev_monitor
->sock
, SOL_SOCKET
, SO_RCVBUFFORCE
, &size
, sizeof(size
)) < 0)
454 int udev_monitor_disconnect(struct udev_monitor
*udev_monitor
)
458 err
= close(udev_monitor
->sock
);
459 udev_monitor
->sock
= -1;
460 return err
< 0 ? -errno
: 0;
465 * @udev_monitor: udev monitor
467 * Take a reference of a udev monitor.
469 * Returns: the passed udev monitor
471 _public_
struct udev_monitor
*udev_monitor_ref(struct udev_monitor
*udev_monitor
)
473 if (udev_monitor
== NULL
)
475 udev_monitor
->refcount
++;
480 * udev_monitor_unref:
481 * @udev_monitor: udev monitor
483 * Drop a reference of a udev monitor. If the refcount reaches zero,
484 * the bound socket will be closed, and the resources of the monitor
489 _public_
struct udev_monitor
*udev_monitor_unref(struct udev_monitor
*udev_monitor
)
491 if (udev_monitor
== NULL
)
493 udev_monitor
->refcount
--;
494 if (udev_monitor
->refcount
> 0)
496 if (udev_monitor
->sock
>= 0)
497 close(udev_monitor
->sock
);
498 udev_list_cleanup(&udev_monitor
->filter_subsystem_list
);
499 udev_list_cleanup(&udev_monitor
->filter_tag_list
);
505 * udev_monitor_get_udev:
506 * @udev_monitor: udev monitor
508 * Retrieve the udev library context the monitor was created with.
510 * Returns: the udev library context
512 _public_
struct udev
*udev_monitor_get_udev(struct udev_monitor
*udev_monitor
)
514 if (udev_monitor
== NULL
)
516 return udev_monitor
->udev
;
520 * udev_monitor_get_fd:
521 * @udev_monitor: udev monitor
523 * Retrieve the socket file descriptor associated with the monitor.
525 * Returns: the socket file descriptor
527 _public_
int udev_monitor_get_fd(struct udev_monitor
*udev_monitor
)
529 if (udev_monitor
== NULL
)
531 return udev_monitor
->sock
;
534 static int passes_filter(struct udev_monitor
*udev_monitor
, struct udev_device
*udev_device
)
536 struct udev_list_entry
*list_entry
;
538 if (udev_list_get_entry(&udev_monitor
->filter_subsystem_list
) == NULL
)
540 udev_list_entry_foreach(list_entry
, udev_list_get_entry(&udev_monitor
->filter_subsystem_list
)) {
541 const char *subsys
= udev_list_entry_get_name(list_entry
);
542 const char *dsubsys
= udev_device_get_subsystem(udev_device
);
544 const char *ddevtype
;
546 if (!streq(dsubsys
, subsys
))
549 devtype
= udev_list_entry_get_value(list_entry
);
552 ddevtype
= udev_device_get_devtype(udev_device
);
553 if (ddevtype
== NULL
)
555 if (streq(ddevtype
, devtype
))
561 if (udev_list_get_entry(&udev_monitor
->filter_tag_list
) == NULL
)
563 udev_list_entry_foreach(list_entry
, udev_list_get_entry(&udev_monitor
->filter_tag_list
)) {
564 const char *tag
= udev_list_entry_get_name(list_entry
);
566 if (udev_device_has_tag(udev_device
, tag
))
573 * udev_monitor_receive_device:
574 * @udev_monitor: udev monitor
576 * Receive data from the udev monitor socket, allocate a new udev
577 * device, fill in the received data, and return the device.
579 * Only socket connections with uid=0 are accepted.
581 * The monitor socket is by default set to NONBLOCK. A variant of poll() on
582 * the file descriptor returned by udev_monitor_get_fd() should to be used to
583 * wake up when new devices arrive, or alternatively the file descriptor
584 * switched into blocking mode.
586 * The initial refcount is 1, and needs to be decremented to
587 * release the resources of the udev device.
589 * Returns: a new udev device, or #NULL, in case of an error
591 _public_
struct udev_device
*udev_monitor_receive_device(struct udev_monitor
*udev_monitor
)
593 struct udev_device
*udev_device
;
596 char cred_msg
[CMSG_SPACE(sizeof(struct ucred
))];
597 struct cmsghdr
*cmsg
;
598 union sockaddr_union snl
;
601 struct udev_monitor_netlink_header nlh
;
606 bool is_initialized
= false;
609 if (udev_monitor
== NULL
) {
614 iov
.iov_len
= sizeof(buf
);
615 memzero(&smsg
, sizeof(struct msghdr
));
618 smsg
.msg_control
= cred_msg
;
619 smsg
.msg_controllen
= sizeof(cred_msg
);
620 smsg
.msg_name
= &snl
;
621 smsg
.msg_namelen
= sizeof(snl
);
623 buflen
= recvmsg(udev_monitor
->sock
, &smsg
, 0);
626 log_debug("unable to receive message");
630 if (buflen
< 32 || (smsg
.msg_flags
& MSG_TRUNC
)) {
631 log_debug("invalid message length");
636 if (snl
.nl
.nl_groups
== 0) {
637 /* unicast message, check if we trust the sender */
638 if (udev_monitor
->snl_trusted_sender
.nl
.nl_pid
== 0 ||
639 snl
.nl
.nl_pid
!= udev_monitor
->snl_trusted_sender
.nl
.nl_pid
) {
640 log_debug("unicast netlink message ignored");
644 } else if (snl
.nl
.nl_groups
== UDEV_MONITOR_KERNEL
) {
645 if (snl
.nl
.nl_pid
> 0) {
646 log_debug("multicast kernel netlink message from PID %"PRIu32
" ignored",
653 cmsg
= CMSG_FIRSTHDR(&smsg
);
654 if (cmsg
== NULL
|| cmsg
->cmsg_type
!= SCM_CREDENTIALS
) {
655 log_debug("no sender credentials received, message ignored");
660 cred
= (struct ucred
*)CMSG_DATA(cmsg
);
661 if (cred
->uid
!= 0) {
662 log_debug("sender uid="UID_FMT
", message ignored", cred
->uid
);
667 if (memcmp(buf
.raw
, "libudev", 8) == 0) {
668 /* udev message needs proper version magic */
669 if (buf
.nlh
.magic
!= htobe32(UDEV_MONITOR_MAGIC
)) {
670 log_debug("unrecognized message signature (%x != %x)",
671 buf
.nlh
.magic
, htobe32(UDEV_MONITOR_MAGIC
));
675 if (buf
.nlh
.properties_off
+32 > (size_t)buflen
) {
676 log_debug("message smaller than expected (%u > %zd)",
677 buf
.nlh
.properties_off
+32, buflen
);
682 bufpos
= buf
.nlh
.properties_off
;
684 /* devices received from udev are always initialized */
685 is_initialized
= true;
687 /* kernel message with header */
688 bufpos
= strlen(buf
.raw
) + 1;
689 if ((size_t)bufpos
< sizeof("a@/d") || bufpos
>= buflen
) {
690 log_debug("invalid message length");
695 /* check message header */
696 if (strstr(buf
.raw
, "@/") == NULL
) {
697 log_debug("unrecognized message header");
703 udev_device
= udev_device_new_from_nulstr(udev_monitor
->udev
, &buf
.raw
[bufpos
], buflen
- bufpos
);
705 log_debug_errno(errno
, "could not create device: %m");
710 udev_device_set_is_initialized(udev_device
);
712 /* skip device, if it does not pass the current filter */
713 if (!passes_filter(udev_monitor
, udev_device
)) {
714 struct pollfd pfd
[1];
717 udev_device_unref(udev_device
);
719 /* if something is queued, get next device */
720 pfd
[0].fd
= udev_monitor
->sock
;
721 pfd
[0].events
= POLLIN
;
722 rc
= poll(pfd
, 1, 0);
733 int udev_monitor_send_device(struct udev_monitor
*udev_monitor
,
734 struct udev_monitor
*destination
, struct udev_device
*udev_device
)
736 const char *buf
, *val
;
738 struct udev_monitor_netlink_header nlh
= {
740 .magic
= htobe32(UDEV_MONITOR_MAGIC
),
741 .header_size
= sizeof nlh
,
743 struct iovec iov
[2] = {
744 { .iov_base
= &nlh
, .iov_len
= sizeof nlh
},
746 struct msghdr smsg
= {
750 struct udev_list_entry
*list_entry
;
751 uint64_t tag_bloom_bits
;
753 blen
= udev_device_get_properties_monitor_buf(udev_device
, &buf
);
755 log_debug("device buffer is too small to contain a valid device");
759 /* fill in versioned header */
760 val
= udev_device_get_subsystem(udev_device
);
761 nlh
.filter_subsystem_hash
= htobe32(util_string_hash32(val
));
763 val
= udev_device_get_devtype(udev_device
);
765 nlh
.filter_devtype_hash
= htobe32(util_string_hash32(val
));
767 /* add tag bloom filter */
769 udev_list_entry_foreach(list_entry
, udev_device_get_tags_list_entry(udev_device
))
770 tag_bloom_bits
|= util_string_bloom64(udev_list_entry_get_name(list_entry
));
771 if (tag_bloom_bits
> 0) {
772 nlh
.filter_tag_bloom_hi
= htobe32(tag_bloom_bits
>> 32);
773 nlh
.filter_tag_bloom_lo
= htobe32(tag_bloom_bits
& 0xffffffff);
776 /* add properties list */
777 nlh
.properties_off
= iov
[0].iov_len
;
778 nlh
.properties_len
= blen
;
779 iov
[1].iov_base
= (char *)buf
;
780 iov
[1].iov_len
= blen
;
783 * Use custom address for target, or the default one.
785 * If we send to a multicast group, we will get
786 * ECONNREFUSED, which is expected.
789 smsg
.msg_name
= &destination
->snl
;
791 smsg
.msg_name
= &udev_monitor
->snl_destination
;
792 smsg
.msg_namelen
= sizeof(struct sockaddr_nl
);
793 count
= sendmsg(udev_monitor
->sock
, &smsg
, 0);
795 if (!destination
&& errno
== ECONNREFUSED
) {
796 log_debug("passed device to netlink monitor %p", udev_monitor
);
802 log_debug("passed %zi byte device to netlink monitor %p", count
, udev_monitor
);
807 * udev_monitor_filter_add_match_subsystem_devtype:
808 * @udev_monitor: the monitor
809 * @subsystem: the subsystem value to match the incoming devices against
810 * @devtype: the devtype value to match the incoming devices against
812 * This filter is efficiently executed inside the kernel, and libudev subscribers
813 * will usually not be woken up for devices which do not match.
815 * The filter must be installed before the monitor is switched to listening mode.
817 * Returns: 0 on success, otherwise a negative error value.
819 _public_
int udev_monitor_filter_add_match_subsystem_devtype(struct udev_monitor
*udev_monitor
, const char *subsystem
, const char *devtype
)
821 if (udev_monitor
== NULL
)
823 if (subsystem
== NULL
)
825 if (udev_list_entry_add(&udev_monitor
->filter_subsystem_list
, subsystem
, devtype
) == NULL
)
831 * udev_monitor_filter_add_match_tag:
832 * @udev_monitor: the monitor
833 * @tag: the name of a tag
835 * This filter is efficiently executed inside the kernel, and libudev subscribers
836 * will usually not be woken up for devices which do not match.
838 * The filter must be installed before the monitor is switched to listening mode.
840 * Returns: 0 on success, otherwise a negative error value.
842 _public_
int udev_monitor_filter_add_match_tag(struct udev_monitor
*udev_monitor
, const char *tag
)
844 if (udev_monitor
== NULL
)
848 if (udev_list_entry_add(&udev_monitor
->filter_tag_list
, tag
, NULL
) == NULL
)
854 * udev_monitor_filter_remove:
855 * @udev_monitor: monitor
857 * Remove all filters from monitor.
859 * Returns: 0 on success, otherwise a negative error value.
861 _public_
int udev_monitor_filter_remove(struct udev_monitor
*udev_monitor
)
863 static const struct sock_fprog filter
= { 0, NULL
};
865 udev_list_cleanup(&udev_monitor
->filter_subsystem_list
);
866 if (setsockopt(udev_monitor
->sock
, SOL_SOCKET
, SO_ATTACH_FILTER
, &filter
, sizeof(filter
)) < 0)