2 This file is part of systemd.
4 Copyright 2008-2012 Kay Sievers <kay@vrfy.org>
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 #include <linux/filter.h>
22 #include <linux/netlink.h>
28 #include <sys/socket.h>
33 #include "alloc-util.h"
36 #include "format-util.h"
37 #include "libudev-private.h"
39 #include "mount-util.h"
40 #include "socket-util.h"
41 #include "string-util.h"
44 * SECTION:libudev-monitor
45 * @short_description: device event source
47 * Connects to a device event source.
53 * Opaque object handling an event source.
59 union sockaddr_union snl
;
60 union sockaddr_union snl_trusted_sender
;
61 union sockaddr_union snl_destination
;
63 struct udev_list filter_subsystem_list
;
64 struct udev_list filter_tag_list
;
68 enum udev_monitor_netlink_group
{
74 #define UDEV_MONITOR_MAGIC 0xfeedcafe
75 struct udev_monitor_netlink_header
{
76 /* "libudev" prefix to distinguish libudev and kernel messages */
79 * magic to protect against daemon <-> library message format mismatch
80 * used in the kernel from socket filter rules; needs to be stored in network order
83 /* total length of header structure known to the sender */
84 unsigned int header_size
;
85 /* properties string buffer */
86 unsigned int properties_off
;
87 unsigned int properties_len
;
89 * hashes of primary device properties strings, to let libudev subscribers
90 * use in-kernel socket filters; values need to be stored in network order
92 unsigned int filter_subsystem_hash
;
93 unsigned int filter_devtype_hash
;
94 unsigned int filter_tag_bloom_hi
;
95 unsigned int filter_tag_bloom_lo
;
98 static struct udev_monitor
*udev_monitor_new(struct udev
*udev
)
100 struct udev_monitor
*udev_monitor
;
102 udev_monitor
= new0(struct udev_monitor
, 1);
103 if (udev_monitor
== NULL
) {
107 udev_monitor
->refcount
= 1;
108 udev_monitor
->udev
= udev
;
109 udev_list_init(udev
, &udev_monitor
->filter_subsystem_list
, false);
110 udev_list_init(udev
, &udev_monitor
->filter_tag_list
, true);
114 /* we consider udev running when /dev is on devtmpfs */
115 static bool udev_has_devtmpfs(struct udev
*udev
) {
117 union file_handle_union h
= FILE_HANDLE_INIT
;
118 _cleanup_fclose_
FILE *f
= NULL
;
119 char line
[LINE_MAX
], *e
;
123 r
= name_to_handle_at(AT_FDCWD
, "/dev", &h
.handle
, &mount_id
, 0);
125 if (errno
!= EOPNOTSUPP
)
126 log_debug_errno(errno
, "name_to_handle_at on /dev: %m");
130 f
= fopen("/proc/self/mountinfo", "re");
134 FOREACH_LINE(line
, f
, return false) {
137 if (sscanf(line
, "%i", &mid
) != 1)
143 e
= strstr(line
, " - ");
147 /* accept any name that starts with the currently expected type */
148 if (startswith(e
+ 3, "devtmpfs"))
155 static void monitor_set_nl_address(struct udev_monitor
*udev_monitor
) {
156 union sockaddr_union snl
;
160 assert(udev_monitor
);
162 /* get the address the kernel has assigned us
163 * it is usually, but not necessarily the pid
165 addrlen
= sizeof(struct sockaddr_nl
);
166 r
= getsockname(udev_monitor
->sock
, &snl
.sa
, &addrlen
);
168 udev_monitor
->snl
.nl
.nl_pid
= snl
.nl
.nl_pid
;
171 struct udev_monitor
*udev_monitor_new_from_netlink_fd(struct udev
*udev
, const char *name
, int fd
)
173 struct udev_monitor
*udev_monitor
;
182 group
= UDEV_MONITOR_NONE
;
183 else if (streq(name
, "udev")) {
185 * We do not support subscribing to uevents if no instance of
186 * udev is running. Uevents would otherwise broadcast the
187 * processing data of the host into containers, which is not
190 * Containers will currently not get any udev uevents, until
191 * a supporting infrastructure is available.
193 * We do not set a netlink multicast group here, so the socket
194 * will not receive any messages.
196 if (access("/run/udev/control", F_OK
) < 0 && !udev_has_devtmpfs(udev
)) {
197 log_debug("the udev service seems not to be active, disable the monitor");
198 group
= UDEV_MONITOR_NONE
;
200 group
= UDEV_MONITOR_UDEV
;
201 } else if (streq(name
, "kernel"))
202 group
= UDEV_MONITOR_KERNEL
;
208 udev_monitor
= udev_monitor_new(udev
);
209 if (udev_monitor
== NULL
)
213 udev_monitor
->sock
= socket(PF_NETLINK
, SOCK_RAW
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, NETLINK_KOBJECT_UEVENT
);
214 if (udev_monitor
->sock
< 0) {
215 log_debug_errno(errno
, "error getting socket: %m");
216 return mfree(udev_monitor
);
219 udev_monitor
->bound
= true;
220 udev_monitor
->sock
= fd
;
221 monitor_set_nl_address(udev_monitor
);
224 udev_monitor
->snl
.nl
.nl_family
= AF_NETLINK
;
225 udev_monitor
->snl
.nl
.nl_groups
= group
;
227 /* default destination for sending */
228 udev_monitor
->snl_destination
.nl
.nl_family
= AF_NETLINK
;
229 udev_monitor
->snl_destination
.nl
.nl_groups
= UDEV_MONITOR_UDEV
;
235 * udev_monitor_new_from_netlink:
236 * @udev: udev library context
237 * @name: name of event source
239 * Create new udev monitor and connect to a specified event
240 * source. Valid sources identifiers are "udev" and "kernel".
242 * Applications should usually not connect directly to the
243 * "kernel" events, because the devices might not be useable
244 * at that time, before udev has configured them, and created
245 * device nodes. Accessing devices at the same time as udev,
246 * might result in unpredictable behavior. The "udev" events
247 * are sent out after udev has finished its event processing,
248 * all rules have been processed, and needed device nodes are
251 * The initial refcount is 1, and needs to be decremented to
252 * release the resources of the udev monitor.
254 * Returns: a new udev monitor, or #NULL, in case of an error
256 _public_
struct udev_monitor
*udev_monitor_new_from_netlink(struct udev
*udev
, const char *name
)
258 return udev_monitor_new_from_netlink_fd(udev
, name
, -1);
261 static inline void bpf_stmt(struct sock_filter
*inss
, unsigned int *i
,
262 unsigned short code
, unsigned int data
)
264 struct sock_filter
*ins
= &inss
[*i
];
271 static inline void bpf_jmp(struct sock_filter
*inss
, unsigned int *i
,
272 unsigned short code
, unsigned int data
,
273 unsigned short jt
, unsigned short jf
)
275 struct sock_filter
*ins
= &inss
[*i
];
285 * udev_monitor_filter_update:
286 * @udev_monitor: monitor
288 * Update the installed socket filter. This is only needed,
289 * if the filter was removed or changed.
291 * Returns: 0 on success, otherwise a negative error value.
293 _public_
int udev_monitor_filter_update(struct udev_monitor
*udev_monitor
)
295 struct sock_filter ins
[512];
296 struct sock_fprog filter
;
298 struct udev_list_entry
*list_entry
;
301 if (udev_list_get_entry(&udev_monitor
->filter_subsystem_list
) == NULL
&&
302 udev_list_get_entry(&udev_monitor
->filter_tag_list
) == NULL
)
305 memzero(ins
, sizeof(ins
));
308 /* load magic in A */
309 bpf_stmt(ins
, &i
, BPF_LD
|BPF_W
|BPF_ABS
, offsetof(struct udev_monitor_netlink_header
, magic
));
310 /* jump if magic matches */
311 bpf_jmp(ins
, &i
, BPF_JMP
|BPF_JEQ
|BPF_K
, UDEV_MONITOR_MAGIC
, 1, 0);
312 /* wrong magic, pass packet */
313 bpf_stmt(ins
, &i
, BPF_RET
|BPF_K
, 0xffffffff);
315 if (udev_list_get_entry(&udev_monitor
->filter_tag_list
) != NULL
) {
318 /* count tag matches, to calculate end of tag match block */
320 udev_list_entry_foreach(list_entry
, udev_list_get_entry(&udev_monitor
->filter_tag_list
))
323 /* add all tags matches */
324 udev_list_entry_foreach(list_entry
, udev_list_get_entry(&udev_monitor
->filter_tag_list
)) {
325 uint64_t tag_bloom_bits
= util_string_bloom64(udev_list_entry_get_name(list_entry
));
326 uint32_t tag_bloom_hi
= tag_bloom_bits
>> 32;
327 uint32_t tag_bloom_lo
= tag_bloom_bits
& 0xffffffff;
329 /* load device bloom bits in A */
330 bpf_stmt(ins
, &i
, BPF_LD
|BPF_W
|BPF_ABS
, offsetof(struct udev_monitor_netlink_header
, filter_tag_bloom_hi
));
331 /* clear bits (tag bits & bloom bits) */
332 bpf_stmt(ins
, &i
, BPF_ALU
|BPF_AND
|BPF_K
, tag_bloom_hi
);
333 /* jump to next tag if it does not match */
334 bpf_jmp(ins
, &i
, BPF_JMP
|BPF_JEQ
|BPF_K
, tag_bloom_hi
, 0, 3);
336 /* load device bloom bits in A */
337 bpf_stmt(ins
, &i
, BPF_LD
|BPF_W
|BPF_ABS
, offsetof(struct udev_monitor_netlink_header
, filter_tag_bloom_lo
));
338 /* clear bits (tag bits & bloom bits) */
339 bpf_stmt(ins
, &i
, BPF_ALU
|BPF_AND
|BPF_K
, tag_bloom_lo
);
340 /* jump behind end of tag match block if tag matches */
342 bpf_jmp(ins
, &i
, BPF_JMP
|BPF_JEQ
|BPF_K
, tag_bloom_lo
, 1 + (tag_matches
* 6), 0);
345 /* nothing matched, drop packet */
346 bpf_stmt(ins
, &i
, BPF_RET
|BPF_K
, 0);
349 /* add all subsystem matches */
350 if (udev_list_get_entry(&udev_monitor
->filter_subsystem_list
) != NULL
) {
351 udev_list_entry_foreach(list_entry
, udev_list_get_entry(&udev_monitor
->filter_subsystem_list
)) {
352 unsigned int hash
= util_string_hash32(udev_list_entry_get_name(list_entry
));
354 /* load device subsystem value in A */
355 bpf_stmt(ins
, &i
, BPF_LD
|BPF_W
|BPF_ABS
, offsetof(struct udev_monitor_netlink_header
, filter_subsystem_hash
));
356 if (udev_list_entry_get_value(list_entry
) == NULL
) {
357 /* jump if subsystem does not match */
358 bpf_jmp(ins
, &i
, BPF_JMP
|BPF_JEQ
|BPF_K
, hash
, 0, 1);
360 /* jump if subsystem does not match */
361 bpf_jmp(ins
, &i
, BPF_JMP
|BPF_JEQ
|BPF_K
, hash
, 0, 3);
363 /* load device devtype value in A */
364 bpf_stmt(ins
, &i
, BPF_LD
|BPF_W
|BPF_ABS
, offsetof(struct udev_monitor_netlink_header
, filter_devtype_hash
));
365 /* jump if value does not match */
366 hash
= util_string_hash32(udev_list_entry_get_value(list_entry
));
367 bpf_jmp(ins
, &i
, BPF_JMP
|BPF_JEQ
|BPF_K
, hash
, 0, 1);
370 /* matched, pass packet */
371 bpf_stmt(ins
, &i
, BPF_RET
|BPF_K
, 0xffffffff);
373 if (i
+1 >= ELEMENTSOF(ins
))
377 /* nothing matched, drop packet */
378 bpf_stmt(ins
, &i
, BPF_RET
|BPF_K
, 0);
381 /* matched, pass packet */
382 bpf_stmt(ins
, &i
, BPF_RET
|BPF_K
, 0xffffffff);
385 memzero(&filter
, sizeof(filter
));
388 err
= setsockopt(udev_monitor
->sock
, SOL_SOCKET
, SO_ATTACH_FILTER
, &filter
, sizeof(filter
));
389 return err
< 0 ? -errno
: 0;
392 int udev_monitor_allow_unicast_sender(struct udev_monitor
*udev_monitor
, struct udev_monitor
*sender
)
394 udev_monitor
->snl_trusted_sender
.nl
.nl_pid
= sender
->snl
.nl
.nl_pid
;
399 * udev_monitor_enable_receiving:
400 * @udev_monitor: the monitor which should receive events
402 * Binds the @udev_monitor socket to the event source.
404 * Returns: 0 on success, otherwise a negative error value.
406 _public_
int udev_monitor_enable_receiving(struct udev_monitor
*udev_monitor
)
411 udev_monitor_filter_update(udev_monitor
);
413 if (!udev_monitor
->bound
) {
414 err
= bind(udev_monitor
->sock
,
415 &udev_monitor
->snl
.sa
, sizeof(struct sockaddr_nl
));
417 udev_monitor
->bound
= true;
421 monitor_set_nl_address(udev_monitor
);
423 return log_debug_errno(errno
, "bind failed: %m");
425 /* enable receiving of sender credentials */
426 err
= setsockopt(udev_monitor
->sock
, SOL_SOCKET
, SO_PASSCRED
, &on
, sizeof(on
));
428 log_debug_errno(errno
, "setting SO_PASSCRED failed: %m");
434 * udev_monitor_set_receive_buffer_size:
435 * @udev_monitor: the monitor which should receive events
436 * @size: the size in bytes
438 * Set the size of the kernel socket buffer. This call needs the
439 * appropriate privileges to succeed.
441 * Returns: 0 on success, otherwise -1 on error.
443 _public_
int udev_monitor_set_receive_buffer_size(struct udev_monitor
*udev_monitor
, int size
)
445 if (udev_monitor
== NULL
)
447 if (setsockopt(udev_monitor
->sock
, SOL_SOCKET
, SO_RCVBUFFORCE
, &size
, sizeof(size
)) < 0)
453 int udev_monitor_disconnect(struct udev_monitor
*udev_monitor
)
457 err
= close(udev_monitor
->sock
);
458 udev_monitor
->sock
= -1;
459 return err
< 0 ? -errno
: 0;
464 * @udev_monitor: udev monitor
466 * Take a reference of a udev monitor.
468 * Returns: the passed udev monitor
470 _public_
struct udev_monitor
*udev_monitor_ref(struct udev_monitor
*udev_monitor
)
472 if (udev_monitor
== NULL
)
474 udev_monitor
->refcount
++;
479 * udev_monitor_unref:
480 * @udev_monitor: udev monitor
482 * Drop a reference of a udev monitor. If the refcount reaches zero,
483 * the bound socket will be closed, and the resources of the monitor
488 _public_
struct udev_monitor
*udev_monitor_unref(struct udev_monitor
*udev_monitor
)
490 if (udev_monitor
== NULL
)
492 udev_monitor
->refcount
--;
493 if (udev_monitor
->refcount
> 0)
495 if (udev_monitor
->sock
>= 0)
496 close(udev_monitor
->sock
);
497 udev_list_cleanup(&udev_monitor
->filter_subsystem_list
);
498 udev_list_cleanup(&udev_monitor
->filter_tag_list
);
504 * udev_monitor_get_udev:
505 * @udev_monitor: udev monitor
507 * Retrieve the udev library context the monitor was created with.
509 * Returns: the udev library context
511 _public_
struct udev
*udev_monitor_get_udev(struct udev_monitor
*udev_monitor
)
513 if (udev_monitor
== NULL
)
515 return udev_monitor
->udev
;
519 * udev_monitor_get_fd:
520 * @udev_monitor: udev monitor
522 * Retrieve the socket file descriptor associated with the monitor.
524 * Returns: the socket file descriptor
526 _public_
int udev_monitor_get_fd(struct udev_monitor
*udev_monitor
)
528 if (udev_monitor
== NULL
)
530 return udev_monitor
->sock
;
533 static int passes_filter(struct udev_monitor
*udev_monitor
, struct udev_device
*udev_device
)
535 struct udev_list_entry
*list_entry
;
537 if (udev_list_get_entry(&udev_monitor
->filter_subsystem_list
) == NULL
)
539 udev_list_entry_foreach(list_entry
, udev_list_get_entry(&udev_monitor
->filter_subsystem_list
)) {
540 const char *subsys
= udev_list_entry_get_name(list_entry
);
541 const char *dsubsys
= udev_device_get_subsystem(udev_device
);
543 const char *ddevtype
;
545 if (!streq(dsubsys
, subsys
))
548 devtype
= udev_list_entry_get_value(list_entry
);
551 ddevtype
= udev_device_get_devtype(udev_device
);
552 if (ddevtype
== NULL
)
554 if (streq(ddevtype
, devtype
))
560 if (udev_list_get_entry(&udev_monitor
->filter_tag_list
) == NULL
)
562 udev_list_entry_foreach(list_entry
, udev_list_get_entry(&udev_monitor
->filter_tag_list
)) {
563 const char *tag
= udev_list_entry_get_name(list_entry
);
565 if (udev_device_has_tag(udev_device
, tag
))
572 * udev_monitor_receive_device:
573 * @udev_monitor: udev monitor
575 * Receive data from the udev monitor socket, allocate a new udev
576 * device, fill in the received data, and return the device.
578 * Only socket connections with uid=0 are accepted.
580 * The monitor socket is by default set to NONBLOCK. A variant of poll() on
581 * the file descriptor returned by udev_monitor_get_fd() should to be used to
582 * wake up when new devices arrive, or alternatively the file descriptor
583 * switched into blocking mode.
585 * The initial refcount is 1, and needs to be decremented to
586 * release the resources of the udev device.
588 * Returns: a new udev device, or #NULL, in case of an error
590 _public_
struct udev_device
*udev_monitor_receive_device(struct udev_monitor
*udev_monitor
)
592 struct udev_device
*udev_device
;
595 char cred_msg
[CMSG_SPACE(sizeof(struct ucred
))];
596 struct cmsghdr
*cmsg
;
597 union sockaddr_union snl
;
600 struct udev_monitor_netlink_header nlh
;
605 bool is_initialized
= false;
608 if (udev_monitor
== NULL
) {
613 iov
.iov_len
= sizeof(buf
);
614 memzero(&smsg
, sizeof(struct msghdr
));
617 smsg
.msg_control
= cred_msg
;
618 smsg
.msg_controllen
= sizeof(cred_msg
);
619 smsg
.msg_name
= &snl
;
620 smsg
.msg_namelen
= sizeof(snl
);
622 buflen
= recvmsg(udev_monitor
->sock
, &smsg
, 0);
625 log_debug("unable to receive message");
629 if (buflen
< 32 || (smsg
.msg_flags
& MSG_TRUNC
)) {
630 log_debug("invalid message length");
635 if (snl
.nl
.nl_groups
== 0) {
636 /* unicast message, check if we trust the sender */
637 if (udev_monitor
->snl_trusted_sender
.nl
.nl_pid
== 0 ||
638 snl
.nl
.nl_pid
!= udev_monitor
->snl_trusted_sender
.nl
.nl_pid
) {
639 log_debug("unicast netlink message ignored");
643 } else if (snl
.nl
.nl_groups
== UDEV_MONITOR_KERNEL
) {
644 if (snl
.nl
.nl_pid
> 0) {
645 log_debug("multicast kernel netlink message from PID %"PRIu32
" ignored",
652 cmsg
= CMSG_FIRSTHDR(&smsg
);
653 if (cmsg
== NULL
|| cmsg
->cmsg_type
!= SCM_CREDENTIALS
) {
654 log_debug("no sender credentials received, message ignored");
659 cred
= (struct ucred
*)CMSG_DATA(cmsg
);
660 if (cred
->uid
!= 0) {
661 log_debug("sender uid="UID_FMT
", message ignored", cred
->uid
);
666 if (memcmp(buf
.raw
, "libudev", 8) == 0) {
667 /* udev message needs proper version magic */
668 if (buf
.nlh
.magic
!= htobe32(UDEV_MONITOR_MAGIC
)) {
669 log_debug("unrecognized message signature (%x != %x)",
670 buf
.nlh
.magic
, htobe32(UDEV_MONITOR_MAGIC
));
674 if (buf
.nlh
.properties_off
+32 > (size_t)buflen
) {
675 log_debug("message smaller than expected (%u > %zd)",
676 buf
.nlh
.properties_off
+32, buflen
);
681 bufpos
= buf
.nlh
.properties_off
;
683 /* devices received from udev are always initialized */
684 is_initialized
= true;
686 /* kernel message with header */
687 bufpos
= strlen(buf
.raw
) + 1;
688 if ((size_t)bufpos
< sizeof("a@/d") || bufpos
>= buflen
) {
689 log_debug("invalid message length");
694 /* check message header */
695 if (strstr(buf
.raw
, "@/") == NULL
) {
696 log_debug("unrecognized message header");
702 udev_device
= udev_device_new_from_nulstr(udev_monitor
->udev
, &buf
.raw
[bufpos
], buflen
- bufpos
);
704 log_debug_errno(errno
, "could not create device: %m");
709 udev_device_set_is_initialized(udev_device
);
711 /* skip device, if it does not pass the current filter */
712 if (!passes_filter(udev_monitor
, udev_device
)) {
713 struct pollfd pfd
[1];
716 udev_device_unref(udev_device
);
718 /* if something is queued, get next device */
719 pfd
[0].fd
= udev_monitor
->sock
;
720 pfd
[0].events
= POLLIN
;
721 rc
= poll(pfd
, 1, 0);
732 int udev_monitor_send_device(struct udev_monitor
*udev_monitor
,
733 struct udev_monitor
*destination
, struct udev_device
*udev_device
)
735 const char *buf
, *val
;
737 struct udev_monitor_netlink_header nlh
= {
739 .magic
= htobe32(UDEV_MONITOR_MAGIC
),
740 .header_size
= sizeof nlh
,
742 struct iovec iov
[2] = {
743 { .iov_base
= &nlh
, .iov_len
= sizeof nlh
},
745 struct msghdr smsg
= {
749 struct udev_list_entry
*list_entry
;
750 uint64_t tag_bloom_bits
;
752 blen
= udev_device_get_properties_monitor_buf(udev_device
, &buf
);
754 log_debug("device buffer is too small to contain a valid device");
758 /* fill in versioned header */
759 val
= udev_device_get_subsystem(udev_device
);
760 nlh
.filter_subsystem_hash
= htobe32(util_string_hash32(val
));
762 val
= udev_device_get_devtype(udev_device
);
764 nlh
.filter_devtype_hash
= htobe32(util_string_hash32(val
));
766 /* add tag bloom filter */
768 udev_list_entry_foreach(list_entry
, udev_device_get_tags_list_entry(udev_device
))
769 tag_bloom_bits
|= util_string_bloom64(udev_list_entry_get_name(list_entry
));
770 if (tag_bloom_bits
> 0) {
771 nlh
.filter_tag_bloom_hi
= htobe32(tag_bloom_bits
>> 32);
772 nlh
.filter_tag_bloom_lo
= htobe32(tag_bloom_bits
& 0xffffffff);
775 /* add properties list */
776 nlh
.properties_off
= iov
[0].iov_len
;
777 nlh
.properties_len
= blen
;
778 iov
[1].iov_base
= (char *)buf
;
779 iov
[1].iov_len
= blen
;
782 * Use custom address for target, or the default one.
784 * If we send to a multicast group, we will get
785 * ECONNREFUSED, which is expected.
788 smsg
.msg_name
= &destination
->snl
;
790 smsg
.msg_name
= &udev_monitor
->snl_destination
;
791 smsg
.msg_namelen
= sizeof(struct sockaddr_nl
);
792 count
= sendmsg(udev_monitor
->sock
, &smsg
, 0);
794 if (!destination
&& errno
== ECONNREFUSED
) {
795 log_debug("passed device to netlink monitor %p", udev_monitor
);
801 log_debug("passed %zi byte device to netlink monitor %p", count
, udev_monitor
);
806 * udev_monitor_filter_add_match_subsystem_devtype:
807 * @udev_monitor: the monitor
808 * @subsystem: the subsystem value to match the incoming devices against
809 * @devtype: the devtype value to match the incoming devices against
811 * This filter is efficiently executed inside the kernel, and libudev subscribers
812 * will usually not be woken up for devices which do not match.
814 * The filter must be installed before the monitor is switched to listening mode.
816 * Returns: 0 on success, otherwise a negative error value.
818 _public_
int udev_monitor_filter_add_match_subsystem_devtype(struct udev_monitor
*udev_monitor
, const char *subsystem
, const char *devtype
)
820 if (udev_monitor
== NULL
)
822 if (subsystem
== NULL
)
824 if (udev_list_entry_add(&udev_monitor
->filter_subsystem_list
, subsystem
, devtype
) == NULL
)
830 * udev_monitor_filter_add_match_tag:
831 * @udev_monitor: the monitor
832 * @tag: the name of a tag
834 * This filter is efficiently executed inside the kernel, and libudev subscribers
835 * will usually not be woken up for devices which do not match.
837 * The filter must be installed before the monitor is switched to listening mode.
839 * Returns: 0 on success, otherwise a negative error value.
841 _public_
int udev_monitor_filter_add_match_tag(struct udev_monitor
*udev_monitor
, const char *tag
)
843 if (udev_monitor
== NULL
)
847 if (udev_list_entry_add(&udev_monitor
->filter_tag_list
, tag
, NULL
) == NULL
)
853 * udev_monitor_filter_remove:
854 * @udev_monitor: monitor
856 * Remove all filters from monitor.
858 * Returns: 0 on success, otherwise a negative error value.
860 _public_
int udev_monitor_filter_remove(struct udev_monitor
*udev_monitor
)
862 static const struct sock_fprog filter
= { 0, NULL
};
864 udev_list_cleanup(&udev_monitor
->filter_subsystem_list
);
865 if (setsockopt(udev_monitor
->sock
, SOL_SOCKET
, SO_ATTACH_FILTER
, &filter
, sizeof(filter
)) < 0)