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"
29 #include "socket-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)
88 struct udev_monitor *udev_monitor;
90 udev_monitor = calloc(1, sizeof(struct udev_monitor));
91 if (udev_monitor == NULL)
93 udev_monitor->refcount = 1;
94 udev_monitor->udev = udev;
95 udev_list_init(udev, &udev_monitor->filter_subsystem_list, false);
96 udev_list_init(udev, &udev_monitor->filter_tag_list, true);
101 * udev_monitor_new_from_socket:
102 * @udev: udev library context
103 * @socket_path: unix socket path
105 * This function is removed from libudev and will not do anything.
109 struct udev_monitor *udev_monitor_new_from_socket(struct udev *udev, const char *socket_path);
110 _public_ struct udev_monitor *udev_monitor_new_from_socket(struct udev *udev, const char *socket_path)
112 udev_err(udev, "udev_monitor_new_from_socket() does not do anything; please migrate to netlink\n");
117 struct udev_monitor *udev_monitor_new_from_netlink_fd(struct udev *udev, const char *name, int fd)
119 struct udev_monitor *udev_monitor;
126 group = UDEV_MONITOR_NONE;
127 else if (strcmp(name, "udev") == 0)
128 group = UDEV_MONITOR_UDEV;
129 else if (strcmp(name, "kernel") == 0)
130 group = UDEV_MONITOR_KERNEL;
134 udev_monitor = udev_monitor_new(udev);
135 if (udev_monitor == NULL)
139 udev_monitor->sock = socket(PF_NETLINK, SOCK_RAW|SOCK_CLOEXEC|SOCK_NONBLOCK, NETLINK_KOBJECT_UEVENT);
140 if (udev_monitor->sock == -1) {
141 udev_err(udev, "error getting socket: %m\n");
146 udev_monitor->bound = true;
147 udev_monitor->sock = fd;
150 udev_monitor->snl.nl.nl_family = AF_NETLINK;
151 udev_monitor->snl.nl.nl_groups = group;
153 /* default destination for sending */
154 udev_monitor->snl_destination.nl.nl_family = AF_NETLINK;
155 udev_monitor->snl_destination.nl.nl_groups = UDEV_MONITOR_UDEV;
161 * udev_monitor_new_from_netlink:
162 * @udev: udev library context
163 * @name: name of event source
165 * Create new udev monitor and connect to a specified event
166 * source. Valid sources identifiers are "udev" and "kernel".
168 * Applications should usually not connect directly to the
169 * "kernel" events, because the devices might not be useable
170 * at that time, before udev has configured them, and created
171 * device nodes. Accessing devices at the same time as udev,
172 * might result in unpredictable behavior. The "udev" events
173 * are sent out after udev has finished its event processing,
174 * all rules have been processed, and needed device nodes are
177 * The initial refcount is 1, and needs to be decremented to
178 * release the resources of the udev monitor.
180 * Returns: a new udev monitor, or #NULL, in case of an error
182 _public_ struct udev_monitor *udev_monitor_new_from_netlink(struct udev *udev, const char *name)
184 return udev_monitor_new_from_netlink_fd(udev, name, -1);
187 static inline void bpf_stmt(struct sock_filter *inss, unsigned int *i,
188 unsigned short code, unsigned int data)
190 struct sock_filter *ins = &inss[*i];
197 static inline void bpf_jmp(struct sock_filter *inss, unsigned int *i,
198 unsigned short code, unsigned int data,
199 unsigned short jt, unsigned short jf)
201 struct sock_filter *ins = &inss[*i];
211 * udev_monitor_filter_update:
212 * @udev_monitor: monitor
214 * Update the installed socket filter. This is only needed,
215 * if the filter was removed or changed.
217 * Returns: 0 on success, otherwise a negative error value.
219 _public_ int udev_monitor_filter_update(struct udev_monitor *udev_monitor)
221 struct sock_filter ins[512];
222 struct sock_fprog filter;
224 struct udev_list_entry *list_entry;
227 if (udev_list_get_entry(&udev_monitor->filter_subsystem_list) == NULL &&
228 udev_list_get_entry(&udev_monitor->filter_tag_list) == NULL)
231 memset(ins, 0x00, sizeof(ins));
234 /* load magic in A */
235 bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, magic));
236 /* jump if magic matches */
237 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, UDEV_MONITOR_MAGIC, 1, 0);
238 /* wrong magic, pass packet */
239 bpf_stmt(ins, &i, BPF_RET|BPF_K, 0xffffffff);
241 if (udev_list_get_entry(&udev_monitor->filter_tag_list) != NULL) {
244 /* count tag matches, to calculate end of tag match block */
246 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_tag_list))
249 /* add all tags matches */
250 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_tag_list)) {
251 uint64_t tag_bloom_bits = util_string_bloom64(udev_list_entry_get_name(list_entry));
252 uint32_t tag_bloom_hi = tag_bloom_bits >> 32;
253 uint32_t tag_bloom_lo = tag_bloom_bits & 0xffffffff;
255 /* load device bloom bits in A */
256 bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_tag_bloom_hi));
257 /* clear bits (tag bits & bloom bits) */
258 bpf_stmt(ins, &i, BPF_ALU|BPF_AND|BPF_K, tag_bloom_hi);
259 /* jump to next tag if it does not match */
260 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, tag_bloom_hi, 0, 3);
262 /* load device bloom bits in A */
263 bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_tag_bloom_lo));
264 /* clear bits (tag bits & bloom bits) */
265 bpf_stmt(ins, &i, BPF_ALU|BPF_AND|BPF_K, tag_bloom_lo);
266 /* jump behind end of tag match block if tag matches */
268 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, tag_bloom_lo, 1 + (tag_matches * 6), 0);
271 /* nothing matched, drop packet */
272 bpf_stmt(ins, &i, BPF_RET|BPF_K, 0);
275 /* add all subsystem matches */
276 if (udev_list_get_entry(&udev_monitor->filter_subsystem_list) != NULL) {
277 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_subsystem_list)) {
278 unsigned int hash = util_string_hash32(udev_list_entry_get_name(list_entry));
280 /* load device subsystem value in A */
281 bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_subsystem_hash));
282 if (udev_list_entry_get_value(list_entry) == NULL) {
283 /* jump if subsystem does not match */
284 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 1);
286 /* jump if subsystem does not match */
287 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 3);
289 /* load device devtype value in A */
290 bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_devtype_hash));
291 /* jump if value does not match */
292 hash = util_string_hash32(udev_list_entry_get_value(list_entry));
293 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 1);
296 /* matched, pass packet */
297 bpf_stmt(ins, &i, BPF_RET|BPF_K, 0xffffffff);
299 if (i+1 >= ELEMENTSOF(ins))
303 /* nothing matched, drop packet */
304 bpf_stmt(ins, &i, BPF_RET|BPF_K, 0);
307 /* matched, pass packet */
308 bpf_stmt(ins, &i, BPF_RET|BPF_K, 0xffffffff);
311 memset(&filter, 0x00, sizeof(filter));
314 err = setsockopt(udev_monitor->sock, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter));
318 int udev_monitor_allow_unicast_sender(struct udev_monitor *udev_monitor, struct udev_monitor *sender)
320 udev_monitor->snl_trusted_sender.nl.nl_pid = sender->snl.nl.nl_pid;
324 * udev_monitor_enable_receiving:
325 * @udev_monitor: the monitor which should receive events
327 * Binds the @udev_monitor socket to the event source.
329 * Returns: 0 on success, otherwise a negative error value.
331 _public_ int udev_monitor_enable_receiving(struct udev_monitor *udev_monitor)
336 if (udev_monitor->snl.nl.nl_family == 0)
339 udev_monitor_filter_update(udev_monitor);
341 if (!udev_monitor->bound) {
342 err = bind(udev_monitor->sock,
343 &udev_monitor->snl.sa, sizeof(struct sockaddr_nl));
345 udev_monitor->bound = true;
349 union sockaddr_union snl;
353 * get the address the kernel has assigned us
354 * it is usually, but not necessarily the pid
356 addrlen = sizeof(struct sockaddr_nl);
357 err = getsockname(udev_monitor->sock, &snl.sa, &addrlen);
359 udev_monitor->snl.nl.nl_pid = snl.nl.nl_pid;
361 udev_err(udev_monitor->udev, "bind failed: %m\n");
365 /* enable receiving of sender credentials */
366 setsockopt(udev_monitor->sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
371 * udev_monitor_set_receive_buffer_size:
372 * @udev_monitor: the monitor which should receive events
373 * @size: the size in bytes
375 * Set the size of the kernel socket buffer. This call needs the
376 * appropriate privileges to succeed.
378 * Returns: 0 on success, otherwise -1 on error.
380 _public_ int udev_monitor_set_receive_buffer_size(struct udev_monitor *udev_monitor, int size)
382 if (udev_monitor == NULL)
384 return setsockopt(udev_monitor->sock, SOL_SOCKET, SO_RCVBUFFORCE, &size, sizeof(size));
387 int udev_monitor_disconnect(struct udev_monitor *udev_monitor)
391 err = close(udev_monitor->sock);
392 udev_monitor->sock = -1;
398 * @udev_monitor: udev monitor
400 * Take a reference of a udev monitor.
402 * Returns: the passed udev monitor
404 _public_ struct udev_monitor *udev_monitor_ref(struct udev_monitor *udev_monitor)
406 if (udev_monitor == NULL)
408 udev_monitor->refcount++;
413 * udev_monitor_unref:
414 * @udev_monitor: udev monitor
416 * Drop a reference of a udev monitor. If the refcount reaches zero,
417 * the bound socket will be closed, and the resources of the monitor
421 _public_ void udev_monitor_unref(struct udev_monitor *udev_monitor)
423 if (udev_monitor == NULL)
425 udev_monitor->refcount--;
426 if (udev_monitor->refcount > 0)
428 if (udev_monitor->sock >= 0)
429 close(udev_monitor->sock);
430 udev_list_cleanup(&udev_monitor->filter_subsystem_list);
431 udev_list_cleanup(&udev_monitor->filter_tag_list);
436 * udev_monitor_get_udev:
437 * @udev_monitor: udev monitor
439 * Retrieve the udev library context the monitor was created with.
441 * Returns: the udev library context
443 _public_ struct udev *udev_monitor_get_udev(struct udev_monitor *udev_monitor)
445 if (udev_monitor == NULL)
447 return udev_monitor->udev;
451 * udev_monitor_get_fd:
452 * @udev_monitor: udev monitor
454 * Retrieve the socket file descriptor associated with the monitor.
456 * Returns: the socket file descriptor
458 _public_ int udev_monitor_get_fd(struct udev_monitor *udev_monitor)
460 if (udev_monitor == NULL)
462 return udev_monitor->sock;
465 static int passes_filter(struct udev_monitor *udev_monitor, struct udev_device *udev_device)
467 struct udev_list_entry *list_entry;
469 if (udev_list_get_entry(&udev_monitor->filter_subsystem_list) == NULL)
471 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_subsystem_list)) {
472 const char *subsys = udev_list_entry_get_name(list_entry);
473 const char *dsubsys = udev_device_get_subsystem(udev_device);
475 const char *ddevtype;
477 if (strcmp(dsubsys, subsys) != 0)
480 devtype = udev_list_entry_get_value(list_entry);
483 ddevtype = udev_device_get_devtype(udev_device);
484 if (ddevtype == NULL)
486 if (strcmp(ddevtype, devtype) == 0)
492 if (udev_list_get_entry(&udev_monitor->filter_tag_list) == NULL)
494 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_tag_list)) {
495 const char *tag = udev_list_entry_get_name(list_entry);
497 if (udev_device_has_tag(udev_device, tag))
504 * udev_monitor_receive_device:
505 * @udev_monitor: udev monitor
507 * Receive data from the udev monitor socket, allocate a new udev
508 * device, fill in the received data, and return the device.
510 * Only socket connections with uid=0 are accepted.
512 * The initial refcount is 1, and needs to be decremented to
513 * release the resources of the udev device.
515 * Returns: a new udev device, or #NULL, in case of an error
517 _public_ struct udev_device *udev_monitor_receive_device(struct udev_monitor *udev_monitor)
519 struct udev_device *udev_device;
522 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
523 struct cmsghdr *cmsg;
524 union sockaddr_union snl;
529 struct udev_monitor_netlink_header *nlh;
532 if (udev_monitor == NULL)
535 iov.iov_len = sizeof(buf);
536 memset (&smsg, 0x00, sizeof(struct msghdr));
539 smsg.msg_control = cred_msg;
540 smsg.msg_controllen = sizeof(cred_msg);
542 if (udev_monitor->snl.nl.nl_family != 0) {
543 smsg.msg_name = &snl;
544 smsg.msg_namelen = sizeof(snl);
547 buflen = recvmsg(udev_monitor->sock, &smsg, 0);
550 udev_dbg(udev_monitor->udev, "unable to receive message\n");
554 if (buflen < 32 || (size_t)buflen >= sizeof(buf)) {
555 udev_dbg(udev_monitor->udev, "invalid message length\n");
559 if (udev_monitor->snl.nl.nl_family != 0) {
560 if (snl.nl.nl_groups == 0) {
561 /* unicast message, check if we trust the sender */
562 if (udev_monitor->snl_trusted_sender.nl.nl_pid == 0 ||
563 snl.nl.nl_pid != udev_monitor->snl_trusted_sender.nl.nl_pid) {
564 udev_dbg(udev_monitor->udev, "unicast netlink message ignored\n");
567 } else if (snl.nl.nl_groups == UDEV_MONITOR_KERNEL) {
568 if (snl.nl.nl_pid > 0) {
569 udev_dbg(udev_monitor->udev, "multicast kernel netlink message from pid %d ignored\n",
576 cmsg = CMSG_FIRSTHDR(&smsg);
577 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
578 udev_dbg(udev_monitor->udev, "no sender credentials received, message ignored\n");
582 cred = (struct ucred *)CMSG_DATA(cmsg);
583 if (cred->uid != 0) {
584 udev_dbg(udev_monitor->udev, "sender uid=%d, message ignored\n", cred->uid);
588 if (memcmp(buf, "libudev", 8) == 0) {
589 /* udev message needs proper version magic */
590 nlh = (struct udev_monitor_netlink_header *) buf;
591 if (nlh->magic != htonl(UDEV_MONITOR_MAGIC)) {
592 udev_err(udev_monitor->udev, "unrecognized message signature (%x != %x)\n",
593 nlh->magic, htonl(UDEV_MONITOR_MAGIC));
596 if (nlh->properties_off+32 > buflen)
598 bufpos = nlh->properties_off;
600 /* kernel message with header */
601 bufpos = strlen(buf) + 1;
602 if ((size_t)bufpos < sizeof("a@/d") || bufpos >= buflen) {
603 udev_dbg(udev_monitor->udev, "invalid message length\n");
607 /* check message header */
608 if (strstr(buf, "@/") == NULL) {
609 udev_dbg(udev_monitor->udev, "unrecognized message header\n");
614 udev_device = udev_device_new(udev_monitor->udev);
615 if (udev_device == NULL)
617 udev_device_set_info_loaded(udev_device);
619 while (bufpos < buflen) {
624 keylen = strlen(key);
627 bufpos += keylen + 1;
628 udev_device_add_property_from_string_parse(udev_device, key);
631 if (udev_device_add_property_from_string_parse_finish(udev_device) < 0) {
632 udev_dbg(udev_monitor->udev, "missing values, invalid device\n");
633 udev_device_unref(udev_device);
637 /* skip device, if it does not pass the current filter */
638 if (!passes_filter(udev_monitor, udev_device)) {
639 struct pollfd pfd[1];
642 udev_device_unref(udev_device);
644 /* if something is queued, get next device */
645 pfd[0].fd = udev_monitor->sock;
646 pfd[0].events = POLLIN;
647 rc = poll(pfd, 1, 0);
656 int udev_monitor_send_device(struct udev_monitor *udev_monitor,
657 struct udev_monitor *destination, struct udev_device *udev_device)
665 struct udev_monitor_netlink_header nlh;
666 struct udev_list_entry *list_entry;
667 uint64_t tag_bloom_bits;
669 if (udev_monitor->snl.nl.nl_family == 0)
672 blen = udev_device_get_properties_monitor_buf(udev_device, &buf);
676 /* add versioned header */
677 memset(&nlh, 0x00, sizeof(struct udev_monitor_netlink_header));
678 memcpy(nlh.prefix, "libudev", 8);
679 nlh.magic = htonl(UDEV_MONITOR_MAGIC);
680 nlh.header_size = sizeof(struct udev_monitor_netlink_header);
681 val = udev_device_get_subsystem(udev_device);
682 nlh.filter_subsystem_hash = htonl(util_string_hash32(val));
683 val = udev_device_get_devtype(udev_device);
685 nlh.filter_devtype_hash = htonl(util_string_hash32(val));
686 iov[0].iov_base = &nlh;
687 iov[0].iov_len = sizeof(struct udev_monitor_netlink_header);
689 /* add tag bloom filter */
691 udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(udev_device))
692 tag_bloom_bits |= util_string_bloom64(udev_list_entry_get_name(list_entry));
693 if (tag_bloom_bits > 0) {
694 nlh.filter_tag_bloom_hi = htonl(tag_bloom_bits >> 32);
695 nlh.filter_tag_bloom_lo = htonl(tag_bloom_bits & 0xffffffff);
698 /* add properties list */
699 nlh.properties_off = iov[0].iov_len;
700 nlh.properties_len = blen;
701 iov[1].iov_base = (char *)buf;
702 iov[1].iov_len = blen;
704 memset(&smsg, 0x00, sizeof(struct msghdr));
708 * Use custom address for target, or the default one.
710 * If we send to a multicast group, we will get
711 * ECONNREFUSED, which is expected.
713 if (destination != NULL)
714 smsg.msg_name = &destination->snl;
716 smsg.msg_name = &udev_monitor->snl_destination;
717 smsg.msg_namelen = sizeof(struct sockaddr_nl);
718 count = sendmsg(udev_monitor->sock, &smsg, 0);
719 udev_dbg(udev_monitor->udev, "passed %zi bytes to netlink monitor %p\n", count, udev_monitor);
724 * udev_monitor_filter_add_match_subsystem_devtype:
725 * @udev_monitor: the monitor
726 * @subsystem: the subsystem value to match the incoming devices against
727 * @devtype: the devtype value to match the incoming devices against
729 * This filter is efficiently executed inside the kernel, and libudev subscribers
730 * will usually not be woken up for devices which do not match.
732 * The filter must be installed before the monitor is switched to listening mode.
734 * Returns: 0 on success, otherwise a negative error value.
736 _public_ int udev_monitor_filter_add_match_subsystem_devtype(struct udev_monitor *udev_monitor, const char *subsystem, const char *devtype)
738 if (udev_monitor == NULL)
740 if (subsystem == NULL)
742 if (udev_list_entry_add(&udev_monitor->filter_subsystem_list, subsystem, devtype) == NULL)
748 * udev_monitor_filter_add_match_tag:
749 * @udev_monitor: the monitor
750 * @tag: the name of a tag
752 * This filter is efficiently executed inside the kernel, and libudev subscribers
753 * will usually not be woken up for devices which do not match.
755 * The filter must be installed before the monitor is switched to listening mode.
757 * Returns: 0 on success, otherwise a negative error value.
759 _public_ int udev_monitor_filter_add_match_tag(struct udev_monitor *udev_monitor, const char *tag)
761 if (udev_monitor == NULL)
765 if (udev_list_entry_add(&udev_monitor->filter_tag_list, tag, NULL) == NULL)
771 * udev_monitor_filter_remove:
772 * @udev_monitor: monitor
774 * Remove all filters from monitor.
776 * Returns: 0 on success, otherwise a negative error value.
778 _public_ int udev_monitor_filter_remove(struct udev_monitor *udev_monitor)
780 static struct sock_fprog filter = { 0, NULL };
782 udev_list_cleanup(&udev_monitor->filter_subsystem_list);
783 return setsockopt(udev_monitor->sock, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter));