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);
100 struct udev_monitor *udev_monitor_new_from_netlink_fd(struct udev *udev, const char *name, int fd)
102 struct udev_monitor *udev_monitor;
109 group = UDEV_MONITOR_NONE;
110 else if (strcmp(name, "udev") == 0)
111 group = UDEV_MONITOR_UDEV;
112 else if (strcmp(name, "kernel") == 0)
113 group = UDEV_MONITOR_KERNEL;
117 udev_monitor = udev_monitor_new(udev);
118 if (udev_monitor == NULL)
122 udev_monitor->sock = socket(PF_NETLINK, SOCK_RAW|SOCK_CLOEXEC|SOCK_NONBLOCK, NETLINK_KOBJECT_UEVENT);
123 if (udev_monitor->sock == -1) {
124 udev_err(udev, "error getting socket: %m\n");
129 udev_monitor->bound = true;
130 udev_monitor->sock = fd;
133 udev_monitor->snl.nl.nl_family = AF_NETLINK;
134 udev_monitor->snl.nl.nl_groups = group;
136 /* default destination for sending */
137 udev_monitor->snl_destination.nl.nl_family = AF_NETLINK;
138 udev_monitor->snl_destination.nl.nl_groups = UDEV_MONITOR_UDEV;
144 * udev_monitor_new_from_netlink:
145 * @udev: udev library context
146 * @name: name of event source
148 * Create new udev monitor and connect to a specified event
149 * source. Valid sources identifiers are "udev" and "kernel".
151 * Applications should usually not connect directly to the
152 * "kernel" events, because the devices might not be useable
153 * at that time, before udev has configured them, and created
154 * device nodes. Accessing devices at the same time as udev,
155 * might result in unpredictable behavior. The "udev" events
156 * are sent out after udev has finished its event processing,
157 * all rules have been processed, and needed device nodes are
160 * The initial refcount is 1, and needs to be decremented to
161 * release the resources of the udev monitor.
163 * Returns: a new udev monitor, or #NULL, in case of an error
165 _public_ struct udev_monitor *udev_monitor_new_from_netlink(struct udev *udev, const char *name)
167 return udev_monitor_new_from_netlink_fd(udev, name, -1);
170 static inline void bpf_stmt(struct sock_filter *inss, unsigned int *i,
171 unsigned short code, unsigned int data)
173 struct sock_filter *ins = &inss[*i];
180 static inline void bpf_jmp(struct sock_filter *inss, unsigned int *i,
181 unsigned short code, unsigned int data,
182 unsigned short jt, unsigned short jf)
184 struct sock_filter *ins = &inss[*i];
194 * udev_monitor_filter_update:
195 * @udev_monitor: monitor
197 * Update the installed socket filter. This is only needed,
198 * if the filter was removed or changed.
200 * Returns: 0 on success, otherwise a negative error value.
202 _public_ int udev_monitor_filter_update(struct udev_monitor *udev_monitor)
204 struct sock_filter ins[512];
205 struct sock_fprog filter;
207 struct udev_list_entry *list_entry;
210 if (udev_list_get_entry(&udev_monitor->filter_subsystem_list) == NULL &&
211 udev_list_get_entry(&udev_monitor->filter_tag_list) == NULL)
214 memset(ins, 0x00, sizeof(ins));
217 /* load magic in A */
218 bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, magic));
219 /* jump if magic matches */
220 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, UDEV_MONITOR_MAGIC, 1, 0);
221 /* wrong magic, pass packet */
222 bpf_stmt(ins, &i, BPF_RET|BPF_K, 0xffffffff);
224 if (udev_list_get_entry(&udev_monitor->filter_tag_list) != NULL) {
227 /* count tag matches, to calculate end of tag match block */
229 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_tag_list))
232 /* add all tags matches */
233 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_tag_list)) {
234 uint64_t tag_bloom_bits = util_string_bloom64(udev_list_entry_get_name(list_entry));
235 uint32_t tag_bloom_hi = tag_bloom_bits >> 32;
236 uint32_t tag_bloom_lo = tag_bloom_bits & 0xffffffff;
238 /* load device bloom bits in A */
239 bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_tag_bloom_hi));
240 /* clear bits (tag bits & bloom bits) */
241 bpf_stmt(ins, &i, BPF_ALU|BPF_AND|BPF_K, tag_bloom_hi);
242 /* jump to next tag if it does not match */
243 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, tag_bloom_hi, 0, 3);
245 /* load device bloom bits in A */
246 bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_tag_bloom_lo));
247 /* clear bits (tag bits & bloom bits) */
248 bpf_stmt(ins, &i, BPF_ALU|BPF_AND|BPF_K, tag_bloom_lo);
249 /* jump behind end of tag match block if tag matches */
251 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, tag_bloom_lo, 1 + (tag_matches * 6), 0);
254 /* nothing matched, drop packet */
255 bpf_stmt(ins, &i, BPF_RET|BPF_K, 0);
258 /* add all subsystem matches */
259 if (udev_list_get_entry(&udev_monitor->filter_subsystem_list) != NULL) {
260 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_subsystem_list)) {
261 unsigned int hash = util_string_hash32(udev_list_entry_get_name(list_entry));
263 /* load device subsystem value in A */
264 bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_subsystem_hash));
265 if (udev_list_entry_get_value(list_entry) == NULL) {
266 /* jump if subsystem does not match */
267 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 1);
269 /* jump if subsystem does not match */
270 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 3);
272 /* load device devtype value in A */
273 bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_devtype_hash));
274 /* jump if value does not match */
275 hash = util_string_hash32(udev_list_entry_get_value(list_entry));
276 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 1);
279 /* matched, pass packet */
280 bpf_stmt(ins, &i, BPF_RET|BPF_K, 0xffffffff);
282 if (i+1 >= ELEMENTSOF(ins))
286 /* nothing matched, drop packet */
287 bpf_stmt(ins, &i, BPF_RET|BPF_K, 0);
290 /* matched, pass packet */
291 bpf_stmt(ins, &i, BPF_RET|BPF_K, 0xffffffff);
294 memset(&filter, 0x00, sizeof(filter));
297 err = setsockopt(udev_monitor->sock, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter));
301 int udev_monitor_allow_unicast_sender(struct udev_monitor *udev_monitor, struct udev_monitor *sender)
303 udev_monitor->snl_trusted_sender.nl.nl_pid = sender->snl.nl.nl_pid;
307 * udev_monitor_enable_receiving:
308 * @udev_monitor: the monitor which should receive events
310 * Binds the @udev_monitor socket to the event source.
312 * Returns: 0 on success, otherwise a negative error value.
314 _public_ int udev_monitor_enable_receiving(struct udev_monitor *udev_monitor)
319 if (udev_monitor->snl.nl.nl_family == 0)
322 udev_monitor_filter_update(udev_monitor);
324 if (!udev_monitor->bound) {
325 err = bind(udev_monitor->sock,
326 &udev_monitor->snl.sa, sizeof(struct sockaddr_nl));
328 udev_monitor->bound = true;
332 union sockaddr_union snl;
336 * get the address the kernel has assigned us
337 * it is usually, but not necessarily the pid
339 addrlen = sizeof(struct sockaddr_nl);
340 err = getsockname(udev_monitor->sock, &snl.sa, &addrlen);
342 udev_monitor->snl.nl.nl_pid = snl.nl.nl_pid;
344 udev_err(udev_monitor->udev, "bind failed: %m\n");
348 /* enable receiving of sender credentials */
349 setsockopt(udev_monitor->sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
354 * udev_monitor_set_receive_buffer_size:
355 * @udev_monitor: the monitor which should receive events
356 * @size: the size in bytes
358 * Set the size of the kernel socket buffer. This call needs the
359 * appropriate privileges to succeed.
361 * Returns: 0 on success, otherwise -1 on error.
363 _public_ int udev_monitor_set_receive_buffer_size(struct udev_monitor *udev_monitor, int size)
365 if (udev_monitor == NULL)
367 return setsockopt(udev_monitor->sock, SOL_SOCKET, SO_RCVBUFFORCE, &size, sizeof(size));
370 int udev_monitor_disconnect(struct udev_monitor *udev_monitor)
374 err = close(udev_monitor->sock);
375 udev_monitor->sock = -1;
381 * @udev_monitor: udev monitor
383 * Take a reference of a udev monitor.
385 * Returns: the passed udev monitor
387 _public_ struct udev_monitor *udev_monitor_ref(struct udev_monitor *udev_monitor)
389 if (udev_monitor == NULL)
391 udev_monitor->refcount++;
396 * udev_monitor_unref:
397 * @udev_monitor: udev monitor
399 * Drop a reference of a udev monitor. If the refcount reaches zero,
400 * the bound socket will be closed, and the resources of the monitor
403 * Returns: the passed udev monitor if it has still an active reference, or #NULL otherwise.
405 _public_ struct udev_monitor *udev_monitor_unref(struct udev_monitor *udev_monitor)
407 if (udev_monitor == NULL)
409 udev_monitor->refcount--;
410 if (udev_monitor->refcount > 0)
412 if (udev_monitor->sock >= 0)
413 close(udev_monitor->sock);
414 udev_list_cleanup(&udev_monitor->filter_subsystem_list);
415 udev_list_cleanup(&udev_monitor->filter_tag_list);
421 * udev_monitor_get_udev:
422 * @udev_monitor: udev monitor
424 * Retrieve the udev library context the monitor was created with.
426 * Returns: the udev library context
428 _public_ struct udev *udev_monitor_get_udev(struct udev_monitor *udev_monitor)
430 if (udev_monitor == NULL)
432 return udev_monitor->udev;
436 * udev_monitor_get_fd:
437 * @udev_monitor: udev monitor
439 * Retrieve the socket file descriptor associated with the monitor.
441 * Returns: the socket file descriptor
443 _public_ int udev_monitor_get_fd(struct udev_monitor *udev_monitor)
445 if (udev_monitor == NULL)
447 return udev_monitor->sock;
450 static int passes_filter(struct udev_monitor *udev_monitor, struct udev_device *udev_device)
452 struct udev_list_entry *list_entry;
454 if (udev_list_get_entry(&udev_monitor->filter_subsystem_list) == NULL)
456 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_subsystem_list)) {
457 const char *subsys = udev_list_entry_get_name(list_entry);
458 const char *dsubsys = udev_device_get_subsystem(udev_device);
460 const char *ddevtype;
462 if (strcmp(dsubsys, subsys) != 0)
465 devtype = udev_list_entry_get_value(list_entry);
468 ddevtype = udev_device_get_devtype(udev_device);
469 if (ddevtype == NULL)
471 if (strcmp(ddevtype, devtype) == 0)
477 if (udev_list_get_entry(&udev_monitor->filter_tag_list) == NULL)
479 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_tag_list)) {
480 const char *tag = udev_list_entry_get_name(list_entry);
482 if (udev_device_has_tag(udev_device, tag))
489 * udev_monitor_receive_device:
490 * @udev_monitor: udev monitor
492 * Receive data from the udev monitor socket, allocate a new udev
493 * device, fill in the received data, and return the device.
495 * Only socket connections with uid=0 are accepted.
497 * The monitor socket is by default set to NONBLOCK. A variant of poll() on
498 * the file descriptor returned by udev_monitor_get_fd() should to be used to
499 * wake up when new devices arrive, or alternatively the file descriptor
500 * switched into blocking mode.
502 * The initial refcount is 1, and needs to be decremented to
503 * release the resources of the udev device.
505 * Returns: a new udev device, or #NULL, in case of an error
507 _public_ struct udev_device *udev_monitor_receive_device(struct udev_monitor *udev_monitor)
509 struct udev_device *udev_device;
512 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
513 struct cmsghdr *cmsg;
514 union sockaddr_union snl;
519 struct udev_monitor_netlink_header *nlh;
522 if (udev_monitor == NULL)
525 iov.iov_len = sizeof(buf);
526 memset (&smsg, 0x00, sizeof(struct msghdr));
529 smsg.msg_control = cred_msg;
530 smsg.msg_controllen = sizeof(cred_msg);
532 if (udev_monitor->snl.nl.nl_family != 0) {
533 smsg.msg_name = &snl;
534 smsg.msg_namelen = sizeof(snl);
537 buflen = recvmsg(udev_monitor->sock, &smsg, 0);
540 udev_dbg(udev_monitor->udev, "unable to receive message\n");
544 if (buflen < 32 || (size_t)buflen >= sizeof(buf)) {
545 udev_dbg(udev_monitor->udev, "invalid message length\n");
549 if (udev_monitor->snl.nl.nl_family != 0) {
550 if (snl.nl.nl_groups == 0) {
551 /* unicast message, check if we trust the sender */
552 if (udev_monitor->snl_trusted_sender.nl.nl_pid == 0 ||
553 snl.nl.nl_pid != udev_monitor->snl_trusted_sender.nl.nl_pid) {
554 udev_dbg(udev_monitor->udev, "unicast netlink message ignored\n");
557 } else if (snl.nl.nl_groups == UDEV_MONITOR_KERNEL) {
558 if (snl.nl.nl_pid > 0) {
559 udev_dbg(udev_monitor->udev, "multicast kernel netlink message from pid %d ignored\n",
566 cmsg = CMSG_FIRSTHDR(&smsg);
567 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
568 udev_dbg(udev_monitor->udev, "no sender credentials received, message ignored\n");
572 cred = (struct ucred *)CMSG_DATA(cmsg);
573 if (cred->uid != 0) {
574 udev_dbg(udev_monitor->udev, "sender uid=%d, message ignored\n", cred->uid);
578 if (memcmp(buf, "libudev", 8) == 0) {
579 /* udev message needs proper version magic */
580 nlh = (struct udev_monitor_netlink_header *) buf;
581 if (nlh->magic != htonl(UDEV_MONITOR_MAGIC)) {
582 udev_err(udev_monitor->udev, "unrecognized message signature (%x != %x)\n",
583 nlh->magic, htonl(UDEV_MONITOR_MAGIC));
586 if (nlh->properties_off+32 > (size_t)buflen)
588 bufpos = nlh->properties_off;
590 /* kernel message with header */
591 bufpos = strlen(buf) + 1;
592 if ((size_t)bufpos < sizeof("a@/d") || bufpos >= buflen) {
593 udev_dbg(udev_monitor->udev, "invalid message length\n");
597 /* check message header */
598 if (strstr(buf, "@/") == NULL) {
599 udev_dbg(udev_monitor->udev, "unrecognized message header\n");
604 udev_device = udev_device_new(udev_monitor->udev);
605 if (udev_device == NULL)
607 udev_device_set_info_loaded(udev_device);
609 while (bufpos < buflen) {
614 keylen = strlen(key);
617 bufpos += keylen + 1;
618 udev_device_add_property_from_string_parse(udev_device, key);
621 if (udev_device_add_property_from_string_parse_finish(udev_device) < 0) {
622 udev_dbg(udev_monitor->udev, "missing values, invalid device\n");
623 udev_device_unref(udev_device);
627 /* skip device, if it does not pass the current filter */
628 if (!passes_filter(udev_monitor, udev_device)) {
629 struct pollfd pfd[1];
632 udev_device_unref(udev_device);
634 /* if something is queued, get next device */
635 pfd[0].fd = udev_monitor->sock;
636 pfd[0].events = POLLIN;
637 rc = poll(pfd, 1, 0);
646 int udev_monitor_send_device(struct udev_monitor *udev_monitor,
647 struct udev_monitor *destination, struct udev_device *udev_device)
655 struct udev_monitor_netlink_header nlh;
656 struct udev_list_entry *list_entry;
657 uint64_t tag_bloom_bits;
659 if (udev_monitor->snl.nl.nl_family == 0)
662 blen = udev_device_get_properties_monitor_buf(udev_device, &buf);
666 /* add versioned header */
667 memset(&nlh, 0x00, sizeof(struct udev_monitor_netlink_header));
668 memcpy(nlh.prefix, "libudev", 8);
669 nlh.magic = htonl(UDEV_MONITOR_MAGIC);
670 nlh.header_size = sizeof(struct udev_monitor_netlink_header);
671 val = udev_device_get_subsystem(udev_device);
672 nlh.filter_subsystem_hash = htonl(util_string_hash32(val));
673 val = udev_device_get_devtype(udev_device);
675 nlh.filter_devtype_hash = htonl(util_string_hash32(val));
676 iov[0].iov_base = &nlh;
677 iov[0].iov_len = sizeof(struct udev_monitor_netlink_header);
679 /* add tag bloom filter */
681 udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(udev_device))
682 tag_bloom_bits |= util_string_bloom64(udev_list_entry_get_name(list_entry));
683 if (tag_bloom_bits > 0) {
684 nlh.filter_tag_bloom_hi = htonl(tag_bloom_bits >> 32);
685 nlh.filter_tag_bloom_lo = htonl(tag_bloom_bits & 0xffffffff);
688 /* add properties list */
689 nlh.properties_off = iov[0].iov_len;
690 nlh.properties_len = blen;
691 iov[1].iov_base = (char *)buf;
692 iov[1].iov_len = blen;
694 memset(&smsg, 0x00, sizeof(struct msghdr));
698 * Use custom address for target, or the default one.
700 * If we send to a multicast group, we will get
701 * ECONNREFUSED, which is expected.
703 if (destination != NULL)
704 smsg.msg_name = &destination->snl;
706 smsg.msg_name = &udev_monitor->snl_destination;
707 smsg.msg_namelen = sizeof(struct sockaddr_nl);
708 count = sendmsg(udev_monitor->sock, &smsg, 0);
709 udev_dbg(udev_monitor->udev, "passed %zi bytes to netlink monitor %p\n", count, udev_monitor);
714 * udev_monitor_filter_add_match_subsystem_devtype:
715 * @udev_monitor: the monitor
716 * @subsystem: the subsystem value to match the incoming devices against
717 * @devtype: the devtype value to match the incoming devices against
719 * This filter is efficiently executed inside the kernel, and libudev subscribers
720 * will usually not be woken up for devices which do not match.
722 * The filter must be installed before the monitor is switched to listening mode.
724 * Returns: 0 on success, otherwise a negative error value.
726 _public_ int udev_monitor_filter_add_match_subsystem_devtype(struct udev_monitor *udev_monitor, const char *subsystem, const char *devtype)
728 if (udev_monitor == NULL)
730 if (subsystem == NULL)
732 if (udev_list_entry_add(&udev_monitor->filter_subsystem_list, subsystem, devtype) == NULL)
738 * udev_monitor_filter_add_match_tag:
739 * @udev_monitor: the monitor
740 * @tag: the name of a tag
742 * This filter is efficiently executed inside the kernel, and libudev subscribers
743 * will usually not be woken up for devices which do not match.
745 * The filter must be installed before the monitor is switched to listening mode.
747 * Returns: 0 on success, otherwise a negative error value.
749 _public_ int udev_monitor_filter_add_match_tag(struct udev_monitor *udev_monitor, const char *tag)
751 if (udev_monitor == NULL)
755 if (udev_list_entry_add(&udev_monitor->filter_tag_list, tag, NULL) == NULL)
761 * udev_monitor_filter_remove:
762 * @udev_monitor: monitor
764 * Remove all filters from monitor.
766 * Returns: 0 on success, otherwise a negative error value.
768 _public_ int udev_monitor_filter_remove(struct udev_monitor *udev_monitor)
770 static struct sock_fprog filter = { 0, NULL };
772 udev_list_cleanup(&udev_monitor->filter_subsystem_list);
773 return setsockopt(udev_monitor->sock, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter));