2 * libudev - interface to udev device information
4 * Copyright (C) 2008-2010 Kay Sievers <kay.sievers@vrfy.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
21 #include <sys/socket.h>
23 #include <arpa/inet.h>
24 #include <linux/netlink.h>
25 #include <linux/filter.h>
28 #include "libudev-private.h"
31 * SECTION:libudev-monitor
32 * @short_description: device event source
34 * Connects to a device event source.
40 * Opaque object handling one event source.
46 struct sockaddr_nl snl;
47 struct sockaddr_nl snl_trusted_sender;
48 struct sockaddr_nl snl_destination;
49 struct sockaddr_un sun;
51 struct udev_list_node filter_subsystem_list;
52 struct udev_list_node filter_tag_list;
55 enum udev_monitor_netlink_group {
61 #define UDEV_MONITOR_MAGIC 0xfeedcafe
62 struct udev_monitor_netlink_header {
63 /* "libudev" prefix to distinguish libudev and kernel messages */
66 * magic to protect against daemon <-> library message format mismatch
67 * used in the kernel from socket filter rules; needs to be stored in network order
70 /* total length of header structure known to the sender */
71 unsigned int header_size;
72 /* properties string buffer */
73 unsigned int properties_off;
74 unsigned int properties_len;
76 * hashes of primary device properties strings, to let libudev subscribers
77 * use in-kernel socket filters; values need to be stored in network order
79 unsigned int filter_subsystem_hash;
80 unsigned int filter_devtype_hash;
81 unsigned int filter_tag_bloom_hi;
82 unsigned int filter_tag_bloom_lo;
85 static struct udev_monitor *udev_monitor_new(struct udev *udev)
87 struct udev_monitor *udev_monitor;
89 udev_monitor = calloc(1, sizeof(struct udev_monitor));
90 if (udev_monitor == NULL)
92 udev_monitor->refcount = 1;
93 udev_monitor->udev = udev;
94 udev_list_init(&udev_monitor->filter_subsystem_list);
95 udev_list_init(&udev_monitor->filter_tag_list);
100 * udev_monitor_new_from_socket:
101 * @udev: udev library context
102 * @socket_path: unix socket path
104 * Create new udev monitor and connect to a specified socket. The
105 * path to a socket either points to an existing socket file, or if
106 * the socket path starts with a '@' character, an abstract namespace
107 * socket will be used.
109 * A socket file will not be created. If it does not already exist,
110 * it will fall-back and connect to an abstract namespace socket with
111 * the given path. The permissions adjustment of a socket file, as
112 * well as the later cleanup, needs to be done by the caller.
114 * The initial refcount is 1, and needs to be decremented to
115 * release the resources of the udev monitor.
117 * Returns: a new udev monitor, or #NULL, in case of an error
119 struct udev_monitor *udev_monitor_new_from_socket(struct udev *udev, const char *socket_path)
121 struct udev_monitor *udev_monitor;
126 if (socket_path == NULL)
128 udev_monitor = udev_monitor_new(udev);
129 if (udev_monitor == NULL)
132 udev_monitor->sun.sun_family = AF_LOCAL;
133 if (socket_path[0] == '@') {
134 /* translate leading '@' to abstract namespace */
135 util_strscpy(udev_monitor->sun.sun_path, sizeof(udev_monitor->sun.sun_path), socket_path);
136 udev_monitor->sun.sun_path[0] = '\0';
137 udev_monitor->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(socket_path);
138 } else if (stat(socket_path, &statbuf) == 0 && S_ISSOCK(statbuf.st_mode)) {
139 /* existing socket file */
140 util_strscpy(udev_monitor->sun.sun_path, sizeof(udev_monitor->sun.sun_path), socket_path);
141 udev_monitor->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(socket_path);
143 /* no socket file, assume abstract namespace socket */
144 util_strscpy(&udev_monitor->sun.sun_path[1], sizeof(udev_monitor->sun.sun_path)-1, socket_path);
145 udev_monitor->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(socket_path)+1;
147 udev_monitor->sock = socket(AF_LOCAL, SOCK_DGRAM|SOCK_CLOEXEC, 0);
148 if (udev_monitor->sock == -1) {
149 err(udev, "error getting socket: %m\n");
154 dbg(udev, "monitor %p created with '%s'\n", udev_monitor, socket_path);
159 * udev_monitor_new_from_netlink:
160 * @udev: udev library context
161 * @name: name of event source
163 * Create new udev monitor and connect to a specified event
164 * source. Valid sources identifiers are "udev" and "kernel".
166 * Applications should usually not connect directly to the
167 * "kernel" events, because the devices might not be useable
168 * at that time, before udev has configured them, and created
171 * Accessing devices at the same time as udev, might result
172 * in unpredictable behavior.
174 * The "udev" events are sent out after udev has finished its
175 * event processing, all rules have been processed, and needed
176 * device nodes are created.
178 * The initial refcount is 1, and needs to be decremented to
179 * release the resources of the udev monitor.
181 * Returns: a new udev monitor, or #NULL, in case of an error
183 struct udev_monitor *udev_monitor_new_from_netlink(struct udev *udev, const char *name)
185 struct udev_monitor *udev_monitor;
192 group = UDEV_MONITOR_NONE;
193 else if (strcmp(name, "udev") == 0)
194 group = UDEV_MONITOR_UDEV;
195 else if (strcmp(name, "kernel") == 0)
196 group = UDEV_MONITOR_KERNEL;
200 udev_monitor = udev_monitor_new(udev);
201 if (udev_monitor == NULL)
204 udev_monitor->sock = socket(PF_NETLINK, SOCK_DGRAM|SOCK_CLOEXEC, NETLINK_KOBJECT_UEVENT);
205 if (udev_monitor->sock == -1) {
206 err(udev, "error getting socket: %m\n");
211 udev_monitor->snl.nl_family = AF_NETLINK;
212 udev_monitor->snl.nl_groups = group;
214 /* default destination for sending */
215 udev_monitor->snl_destination.nl_family = AF_NETLINK;
216 udev_monitor->snl_destination.nl_groups = UDEV_MONITOR_UDEV;
218 dbg(udev, "monitor %p created with NETLINK_KOBJECT_UEVENT (%u)\n", udev_monitor, group);
222 static inline void bpf_stmt(struct sock_filter *inss, unsigned int *i,
223 unsigned short code, unsigned int data)
225 struct sock_filter *ins = &inss[*i];
232 static inline void bpf_jmp(struct sock_filter *inss, unsigned int *i,
233 unsigned short code, unsigned int data,
234 unsigned short jt, unsigned short jf)
236 struct sock_filter *ins = &inss[*i];
246 * udev_monitor_filter_update:
247 * @udev_monitor: monitor
249 * Update the installed filter. This might only be needed, if the filter was removed or changed.
251 * Returns: 0 on success, otherwise a negative error value.
253 int udev_monitor_filter_update(struct udev_monitor *udev_monitor)
255 struct sock_filter ins[512];
256 struct sock_fprog filter;
258 struct udev_list_entry *list_entry;
261 if (udev_list_get_entry(&udev_monitor->filter_subsystem_list) == NULL &&
262 udev_list_get_entry(&udev_monitor->filter_tag_list) == NULL)
265 memset(ins, 0x00, sizeof(ins));
268 /* load magic in A */
269 bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, magic));
270 /* jump if magic matches */
271 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, UDEV_MONITOR_MAGIC, 1, 0);
272 /* wrong magic, pass packet */
273 bpf_stmt(ins, &i, BPF_RET|BPF_K, 0xffffffff);
275 if (udev_list_get_entry(&udev_monitor->filter_tag_list) != NULL) {
278 /* count tag matches, to calculate end of tag match block */
280 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_tag_list))
283 /* add all tags matches */
284 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_tag_list)) {
285 uint64_t tag_bloom_bits = util_string_bloom64(udev_list_entry_get_name(list_entry));
286 uint32_t tag_bloom_hi = tag_bloom_bits >> 32;
287 uint32_t tag_bloom_lo = tag_bloom_bits & 0xffffffff;
289 /* load device bloom bits in A */
290 bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_tag_bloom_hi));
291 /* clear bits (tag bits & bloom bits) */
292 bpf_stmt(ins, &i, BPF_ALU|BPF_AND|BPF_K, tag_bloom_hi);
293 /* jump to next tag if it does not match */
294 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, tag_bloom_hi, 0, 3);
296 /* load device bloom bits in A */
297 bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_tag_bloom_lo));
298 /* clear bits (tag bits & bloom bits) */
299 bpf_stmt(ins, &i, BPF_ALU|BPF_AND|BPF_K, tag_bloom_lo);
300 /* jump behind end of tag match block if tag matches */
302 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, tag_bloom_lo, 1 + (tag_matches * 6), 0);
305 /* nothing matched, drop packet */
306 bpf_stmt(ins, &i, BPF_RET|BPF_K, 0);
309 /* add all subsystem matches */
310 if (udev_list_get_entry(&udev_monitor->filter_subsystem_list) != NULL) {
311 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_subsystem_list)) {
312 unsigned int hash = util_string_hash32(udev_list_entry_get_name(list_entry));
314 /* load device subsystem value in A */
315 bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_subsystem_hash));
316 if (udev_list_entry_get_value(list_entry) == NULL) {
317 /* jump if subsystem does not match */
318 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 1);
320 /* jump if subsystem does not match */
321 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 3);
323 /* load device devtype value in A */
324 bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_devtype_hash));
325 /* jump if value does not match */
326 hash = util_string_hash32(udev_list_entry_get_value(list_entry));
327 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 1);
330 /* matched, pass packet */
331 bpf_stmt(ins, &i, BPF_RET|BPF_K, 0xffffffff);
333 if (i+1 >= ARRAY_SIZE(ins))
337 /* nothing matched, drop packet */
338 bpf_stmt(ins, &i, BPF_RET|BPF_K, 0);
341 /* matched, pass packet */
342 bpf_stmt(ins, &i, BPF_RET|BPF_K, 0xffffffff);
347 err = setsockopt(udev_monitor->sock, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter));
351 int udev_monitor_allow_unicast_sender(struct udev_monitor *udev_monitor, struct udev_monitor *sender)
353 udev_monitor->snl_trusted_sender.nl_pid = sender->snl.nl_pid;
357 * udev_monitor_enable_receiving:
358 * @udev_monitor: the monitor which should receive events
360 * Binds the @udev_monitor socket to the event source.
362 * Returns: 0 on success, otherwise a negative error value.
364 int udev_monitor_enable_receiving(struct udev_monitor *udev_monitor)
369 if (udev_monitor->sun.sun_family != 0) {
370 err = bind(udev_monitor->sock,
371 (struct sockaddr *)&udev_monitor->sun, udev_monitor->addrlen);
372 } else if (udev_monitor->snl.nl_family != 0) {
373 udev_monitor_filter_update(udev_monitor);
374 err = bind(udev_monitor->sock,
375 (struct sockaddr *)&udev_monitor->snl, sizeof(struct sockaddr_nl));
377 struct sockaddr_nl snl;
381 * get the address the kernel has assigned us
382 * it is usually, but not necessarily the pid
384 addrlen = sizeof(struct sockaddr_nl);
385 err = getsockname(udev_monitor->sock, (struct sockaddr *)&snl, &addrlen);
387 udev_monitor->snl.nl_pid = snl.nl_pid;
394 err(udev_monitor->udev, "bind failed: %m\n");
398 /* enable receiving of sender credentials */
399 setsockopt(udev_monitor->sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
404 * udev_monitor_set_receive_buffer_size:
405 * @udev_monitor: the monitor which should receive events
406 * @size: the size in bytes
408 * Set the size of the kernel socket buffer. This call needs the
409 * appropriate privileges to succeed.
411 * Returns: 0 on success, otherwise -1 on error.
413 int udev_monitor_set_receive_buffer_size(struct udev_monitor *udev_monitor, int size)
415 if (udev_monitor == NULL)
417 return setsockopt(udev_monitor->sock, SOL_SOCKET, SO_RCVBUFFORCE, &size, sizeof(size));
420 int udev_monitor_disconnect(struct udev_monitor *udev_monitor)
424 err = close(udev_monitor->sock);
425 udev_monitor->sock = -1;
431 * @udev_monitor: udev monitor
433 * Take a reference of a udev monitor.
435 * Returns: the passed udev monitor
437 struct udev_monitor *udev_monitor_ref(struct udev_monitor *udev_monitor)
439 if (udev_monitor == NULL)
441 udev_monitor->refcount++;
446 * udev_monitor_unref:
447 * @udev_monitor: udev monitor
449 * Drop a reference of a udev monitor. If the refcount reaches zero,
450 * the bound socket will be closed, and the resources of the monitor
454 void udev_monitor_unref(struct udev_monitor *udev_monitor)
456 if (udev_monitor == NULL)
458 udev_monitor->refcount--;
459 if (udev_monitor->refcount > 0)
461 if (udev_monitor->sock >= 0)
462 close(udev_monitor->sock);
463 udev_list_cleanup_entries(udev_monitor->udev, &udev_monitor->filter_subsystem_list);
464 udev_list_cleanup_entries(udev_monitor->udev, &udev_monitor->filter_tag_list);
465 dbg(udev_monitor->udev, "monitor %p released\n", udev_monitor);
470 * udev_monitor_get_udev:
471 * @udev_monitor: udev monitor
473 * Retrieve the udev library context the monitor was created with.
475 * Returns: the udev library context
477 struct udev *udev_monitor_get_udev(struct udev_monitor *udev_monitor)
479 if (udev_monitor == NULL)
481 return udev_monitor->udev;
485 * udev_monitor_get_fd:
486 * @udev_monitor: udev monitor
488 * Retrieve the socket file descriptor associated with the monitor.
490 * Returns: the socket file descriptor
492 int udev_monitor_get_fd(struct udev_monitor *udev_monitor)
494 if (udev_monitor == NULL)
496 return udev_monitor->sock;
499 static int passes_filter(struct udev_monitor *udev_monitor, struct udev_device *udev_device)
501 struct udev_list_entry *list_entry;
503 if (udev_list_get_entry(&udev_monitor->filter_subsystem_list) == NULL)
505 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_subsystem_list)) {
506 const char *subsys = udev_list_entry_get_name(list_entry);
507 const char *dsubsys = udev_device_get_subsystem(udev_device);
509 const char *ddevtype;
511 if (strcmp(dsubsys, subsys) != 0)
514 devtype = udev_list_entry_get_value(list_entry);
517 ddevtype = udev_device_get_devtype(udev_device);
518 if (ddevtype == NULL)
520 if (strcmp(ddevtype, devtype) == 0)
526 if (udev_list_get_entry(&udev_monitor->filter_tag_list) == NULL)
528 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_tag_list)) {
529 const char *tag = udev_list_entry_get_name(list_entry);
531 if (udev_device_has_tag(udev_device, tag))
538 * udev_monitor_receive_device:
539 * @udev_monitor: udev monitor
541 * Receive data from the udev monitor socket, allocate a new udev
542 * device, fill in the received data, and return the device.
544 * Only socket connections with uid=0 are accepted. The caller
545 * needs to make sure that there is data to read from the socket.
546 * The call will block until the socket becomes readable.
548 * The initial refcount is 1, and needs to be decremented to
549 * release the resources of the udev device.
551 * Returns: a new udev device, or #NULL, in case of an error
553 struct udev_device *udev_monitor_receive_device(struct udev_monitor *udev_monitor)
555 struct udev_device *udev_device;
558 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
559 struct cmsghdr *cmsg;
560 struct sockaddr_nl snl;
565 struct udev_monitor_netlink_header *nlh;
568 if (udev_monitor == NULL)
570 memset(buf, 0x00, sizeof(buf));
572 iov.iov_len = sizeof(buf);
573 memset (&smsg, 0x00, sizeof(struct msghdr));
576 smsg.msg_control = cred_msg;
577 smsg.msg_controllen = sizeof(cred_msg);
579 if (udev_monitor->snl.nl_family != 0) {
580 smsg.msg_name = &snl;
581 smsg.msg_namelen = sizeof(snl);
584 buflen = recvmsg(udev_monitor->sock, &smsg, 0);
587 info(udev_monitor->udev, "unable to receive message\n");
591 if (buflen < 32 || (size_t)buflen >= sizeof(buf)) {
592 info(udev_monitor->udev, "invalid message length\n");
596 if (udev_monitor->snl.nl_family != 0) {
597 if (snl.nl_groups == 0) {
598 /* unicast message, check if we trust the sender */
599 if (udev_monitor->snl_trusted_sender.nl_pid == 0 ||
600 snl.nl_pid != udev_monitor->snl_trusted_sender.nl_pid) {
601 info(udev_monitor->udev, "unicast netlink message ignored\n");
604 } else if (snl.nl_groups == UDEV_MONITOR_KERNEL) {
605 if (snl.nl_pid > 0) {
606 info(udev_monitor->udev, "multicast kernel netlink message from pid %d ignored\n",
613 cmsg = CMSG_FIRSTHDR(&smsg);
614 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
615 info(udev_monitor->udev, "no sender credentials received, message ignored\n");
619 cred = (struct ucred *)CMSG_DATA(cmsg);
620 if (cred->uid != 0) {
621 info(udev_monitor->udev, "sender uid=%d, message ignored\n", cred->uid);
625 if (memcmp(buf, "libudev", 8) == 0) {
626 /* udev message needs proper version magic */
627 nlh = (struct udev_monitor_netlink_header *) buf;
628 if (nlh->magic != htonl(UDEV_MONITOR_MAGIC))
630 if (nlh->properties_off+32 > buflen)
632 bufpos = nlh->properties_off;
634 /* kernel message with header */
635 bufpos = strlen(buf) + 1;
636 if ((size_t)bufpos < sizeof("a@/d") || bufpos >= buflen) {
637 info(udev_monitor->udev, "invalid message length\n");
641 /* check message header */
642 if (strstr(buf, "@/") == NULL) {
643 info(udev_monitor->udev, "unrecognized message header\n");
648 udev_device = udev_device_new(udev_monitor->udev);
649 if (udev_device == NULL)
651 udev_device_set_info_loaded(udev_device);
653 while (bufpos < buflen) {
658 keylen = strlen(key);
661 bufpos += keylen + 1;
662 udev_device_add_property_from_string_parse(udev_device, key);
665 if (udev_device_add_property_from_string_parse_finish(udev_device) < 0) {
666 info(udev_monitor->udev, "missing values, invalid device\n");
667 udev_device_unref(udev_device);
671 /* skip device, if it does not pass the current filter */
672 if (!passes_filter(udev_monitor, udev_device)) {
673 struct pollfd pfd[1];
676 udev_device_unref(udev_device);
678 /* if something is queued, get next device */
679 pfd[0].fd = udev_monitor->sock;
680 pfd[0].events = POLLIN;
681 rc = poll(pfd, 1, 0);
690 int udev_monitor_send_device(struct udev_monitor *udev_monitor,
691 struct udev_monitor *destination, struct udev_device *udev_device)
697 blen = udev_device_get_properties_monitor_buf(udev_device, &buf);
701 if (udev_monitor->sun.sun_family != 0) {
708 /* header <action>@<devpath> */
709 action = udev_device_get_action(udev_device);
713 if (util_strpcpyl(&s, sizeof(header), action, "@", udev_device_get_devpath(udev_device), NULL) == 0)
715 iov[0].iov_base = header;
716 iov[0].iov_len = (s - header)+1;
718 /* add properties list */
719 iov[1].iov_base = (char *)buf;
720 iov[1].iov_len = blen;
722 memset(&smsg, 0x00, sizeof(struct msghdr));
725 smsg.msg_name = &udev_monitor->sun;
726 smsg.msg_namelen = udev_monitor->addrlen;
727 count = sendmsg(udev_monitor->sock, &smsg, 0);
728 info(udev_monitor->udev, "passed %zi bytes to socket monitor %p\n", count, udev_monitor);
732 if (udev_monitor->snl.nl_family != 0) {
736 struct udev_monitor_netlink_header nlh;
737 struct udev_list_entry *list_entry;
738 uint64_t tag_bloom_bits;
740 /* add versioned header */
741 memset(&nlh, 0x00, sizeof(struct udev_monitor_netlink_header));
742 memcpy(nlh.prefix, "libudev", 8);
743 nlh.magic = htonl(UDEV_MONITOR_MAGIC);
744 nlh.header_size = sizeof(struct udev_monitor_netlink_header);
745 val = udev_device_get_subsystem(udev_device);
746 nlh.filter_subsystem_hash = htonl(util_string_hash32(val));
747 val = udev_device_get_devtype(udev_device);
749 nlh.filter_devtype_hash = htonl(util_string_hash32(val));
750 iov[0].iov_base = &nlh;
751 iov[0].iov_len = sizeof(struct udev_monitor_netlink_header);
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 = htonl(tag_bloom_bits >> 32);
759 nlh.filter_tag_bloom_lo = htonl(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;
768 memset(&smsg, 0x00, sizeof(struct msghdr));
772 * Use custom address for target, or the default one.
774 * If we send to a multicast group, we will get
775 * ECONNREFUSED, which is expected.
777 if (destination != NULL)
778 smsg.msg_name = &destination->snl;
780 smsg.msg_name = &udev_monitor->snl_destination;
781 smsg.msg_namelen = sizeof(struct sockaddr_nl);
782 count = sendmsg(udev_monitor->sock, &smsg, 0);
783 info(udev_monitor->udev, "passed %zi bytes to netlink monitor %p\n", count, udev_monitor);
791 * udev_monitor_filter_add_match_subsystem_devtype:
792 * @udev_monitor: the monitor
793 * @subsystem: the subsystem value to match the incoming devices against
794 * @devtype: the devtype value to match the incoming devices against
796 * This filer is efficiently executed inside the kernel, and libudev subscribers
797 * will usually not be woken up for devices which do not match.
799 * The filter must be installed before the monitor is switched to listening mode.
801 * Returns: 0 on success, otherwise a negative error value.
803 int udev_monitor_filter_add_match_subsystem_devtype(struct udev_monitor *udev_monitor, const char *subsystem, const char *devtype)
805 if (udev_monitor == NULL)
807 if (subsystem == NULL)
809 if (udev_list_entry_add(udev_monitor->udev,
810 &udev_monitor->filter_subsystem_list, subsystem, devtype, 0, 0) == NULL)
816 * udev_monitor_filter_add_match_tag:
817 * @udev_monitor: the monitor
818 * @tag: the name of a tag
820 * This filer is efficiently executed inside the kernel, and libudev subscribers
821 * will usually not be woken up for devices which do not match.
823 * The filter must be installed before the monitor is switched to listening mode.
825 * Returns: 0 on success, otherwise a negative error value.
827 int udev_monitor_filter_add_match_tag(struct udev_monitor *udev_monitor, const char *tag)
829 if (udev_monitor == NULL)
833 if (udev_list_entry_add(udev_monitor->udev,
834 &udev_monitor->filter_tag_list, tag, NULL, 0, 0) == 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 int udev_monitor_filter_remove(struct udev_monitor *udev_monitor)
849 static struct sock_fprog filter = { 0, NULL };
851 udev_list_cleanup_entries(udev_monitor->udev, &udev_monitor->filter_subsystem_list);
852 return setsockopt(udev_monitor->sock, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter));