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
404 _public_ struct udev_monitor *udev_monitor_unref(struct udev_monitor *udev_monitor)
406 if (udev_monitor == NULL)
408 udev_monitor->refcount--;
409 if (udev_monitor->refcount > 0)
411 if (udev_monitor->sock >= 0)
412 close(udev_monitor->sock);
413 udev_list_cleanup(&udev_monitor->filter_subsystem_list);
414 udev_list_cleanup(&udev_monitor->filter_tag_list);
420 * udev_monitor_get_udev:
421 * @udev_monitor: udev monitor
423 * Retrieve the udev library context the monitor was created with.
425 * Returns: the udev library context
427 _public_ struct udev *udev_monitor_get_udev(struct udev_monitor *udev_monitor)
429 if (udev_monitor == NULL)
431 return udev_monitor->udev;
435 * udev_monitor_get_fd:
436 * @udev_monitor: udev monitor
438 * Retrieve the socket file descriptor associated with the monitor.
440 * Returns: the socket file descriptor
442 _public_ int udev_monitor_get_fd(struct udev_monitor *udev_monitor)
444 if (udev_monitor == NULL)
446 return udev_monitor->sock;
449 static int passes_filter(struct udev_monitor *udev_monitor, struct udev_device *udev_device)
451 struct udev_list_entry *list_entry;
453 if (udev_list_get_entry(&udev_monitor->filter_subsystem_list) == NULL)
455 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_subsystem_list)) {
456 const char *subsys = udev_list_entry_get_name(list_entry);
457 const char *dsubsys = udev_device_get_subsystem(udev_device);
459 const char *ddevtype;
461 if (strcmp(dsubsys, subsys) != 0)
464 devtype = udev_list_entry_get_value(list_entry);
467 ddevtype = udev_device_get_devtype(udev_device);
468 if (ddevtype == NULL)
470 if (strcmp(ddevtype, devtype) == 0)
476 if (udev_list_get_entry(&udev_monitor->filter_tag_list) == NULL)
478 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_tag_list)) {
479 const char *tag = udev_list_entry_get_name(list_entry);
481 if (udev_device_has_tag(udev_device, tag))
488 * udev_monitor_receive_device:
489 * @udev_monitor: udev monitor
491 * Receive data from the udev monitor socket, allocate a new udev
492 * device, fill in the received data, and return the device.
494 * Only socket connections with uid=0 are accepted.
496 * The initial refcount is 1, and needs to be decremented to
497 * release the resources of the udev device.
499 * Returns: a new udev device, or #NULL, in case of an error
501 _public_ struct udev_device *udev_monitor_receive_device(struct udev_monitor *udev_monitor)
503 struct udev_device *udev_device;
506 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
507 struct cmsghdr *cmsg;
508 union sockaddr_union snl;
513 struct udev_monitor_netlink_header *nlh;
516 if (udev_monitor == NULL)
519 iov.iov_len = sizeof(buf);
520 memset (&smsg, 0x00, sizeof(struct msghdr));
523 smsg.msg_control = cred_msg;
524 smsg.msg_controllen = sizeof(cred_msg);
526 if (udev_monitor->snl.nl.nl_family != 0) {
527 smsg.msg_name = &snl;
528 smsg.msg_namelen = sizeof(snl);
531 buflen = recvmsg(udev_monitor->sock, &smsg, 0);
534 udev_dbg(udev_monitor->udev, "unable to receive message\n");
538 if (buflen < 32 || (size_t)buflen >= sizeof(buf)) {
539 udev_dbg(udev_monitor->udev, "invalid message length\n");
543 if (udev_monitor->snl.nl.nl_family != 0) {
544 if (snl.nl.nl_groups == 0) {
545 /* unicast message, check if we trust the sender */
546 if (udev_monitor->snl_trusted_sender.nl.nl_pid == 0 ||
547 snl.nl.nl_pid != udev_monitor->snl_trusted_sender.nl.nl_pid) {
548 udev_dbg(udev_monitor->udev, "unicast netlink message ignored\n");
551 } else if (snl.nl.nl_groups == UDEV_MONITOR_KERNEL) {
552 if (snl.nl.nl_pid > 0) {
553 udev_dbg(udev_monitor->udev, "multicast kernel netlink message from pid %d ignored\n",
560 cmsg = CMSG_FIRSTHDR(&smsg);
561 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
562 udev_dbg(udev_monitor->udev, "no sender credentials received, message ignored\n");
566 cred = (struct ucred *)CMSG_DATA(cmsg);
567 if (cred->uid != 0) {
568 udev_dbg(udev_monitor->udev, "sender uid=%d, message ignored\n", cred->uid);
572 if (memcmp(buf, "libudev", 8) == 0) {
573 /* udev message needs proper version magic */
574 nlh = (struct udev_monitor_netlink_header *) buf;
575 if (nlh->magic != htonl(UDEV_MONITOR_MAGIC)) {
576 udev_err(udev_monitor->udev, "unrecognized message signature (%x != %x)\n",
577 nlh->magic, htonl(UDEV_MONITOR_MAGIC));
580 if (nlh->properties_off+32 > buflen)
582 bufpos = nlh->properties_off;
584 /* kernel message with header */
585 bufpos = strlen(buf) + 1;
586 if ((size_t)bufpos < sizeof("a@/d") || bufpos >= buflen) {
587 udev_dbg(udev_monitor->udev, "invalid message length\n");
591 /* check message header */
592 if (strstr(buf, "@/") == NULL) {
593 udev_dbg(udev_monitor->udev, "unrecognized message header\n");
598 udev_device = udev_device_new(udev_monitor->udev);
599 if (udev_device == NULL)
601 udev_device_set_info_loaded(udev_device);
603 while (bufpos < buflen) {
608 keylen = strlen(key);
611 bufpos += keylen + 1;
612 udev_device_add_property_from_string_parse(udev_device, key);
615 if (udev_device_add_property_from_string_parse_finish(udev_device) < 0) {
616 udev_dbg(udev_monitor->udev, "missing values, invalid device\n");
617 udev_device_unref(udev_device);
621 /* skip device, if it does not pass the current filter */
622 if (!passes_filter(udev_monitor, udev_device)) {
623 struct pollfd pfd[1];
626 udev_device_unref(udev_device);
628 /* if something is queued, get next device */
629 pfd[0].fd = udev_monitor->sock;
630 pfd[0].events = POLLIN;
631 rc = poll(pfd, 1, 0);
640 int udev_monitor_send_device(struct udev_monitor *udev_monitor,
641 struct udev_monitor *destination, struct udev_device *udev_device)
649 struct udev_monitor_netlink_header nlh;
650 struct udev_list_entry *list_entry;
651 uint64_t tag_bloom_bits;
653 if (udev_monitor->snl.nl.nl_family == 0)
656 blen = udev_device_get_properties_monitor_buf(udev_device, &buf);
660 /* add versioned header */
661 memset(&nlh, 0x00, sizeof(struct udev_monitor_netlink_header));
662 memcpy(nlh.prefix, "libudev", 8);
663 nlh.magic = htonl(UDEV_MONITOR_MAGIC);
664 nlh.header_size = sizeof(struct udev_monitor_netlink_header);
665 val = udev_device_get_subsystem(udev_device);
666 nlh.filter_subsystem_hash = htonl(util_string_hash32(val));
667 val = udev_device_get_devtype(udev_device);
669 nlh.filter_devtype_hash = htonl(util_string_hash32(val));
670 iov[0].iov_base = &nlh;
671 iov[0].iov_len = sizeof(struct udev_monitor_netlink_header);
673 /* add tag bloom filter */
675 udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(udev_device))
676 tag_bloom_bits |= util_string_bloom64(udev_list_entry_get_name(list_entry));
677 if (tag_bloom_bits > 0) {
678 nlh.filter_tag_bloom_hi = htonl(tag_bloom_bits >> 32);
679 nlh.filter_tag_bloom_lo = htonl(tag_bloom_bits & 0xffffffff);
682 /* add properties list */
683 nlh.properties_off = iov[0].iov_len;
684 nlh.properties_len = blen;
685 iov[1].iov_base = (char *)buf;
686 iov[1].iov_len = blen;
688 memset(&smsg, 0x00, sizeof(struct msghdr));
692 * Use custom address for target, or the default one.
694 * If we send to a multicast group, we will get
695 * ECONNREFUSED, which is expected.
697 if (destination != NULL)
698 smsg.msg_name = &destination->snl;
700 smsg.msg_name = &udev_monitor->snl_destination;
701 smsg.msg_namelen = sizeof(struct sockaddr_nl);
702 count = sendmsg(udev_monitor->sock, &smsg, 0);
703 udev_dbg(udev_monitor->udev, "passed %zi bytes to netlink monitor %p\n", count, udev_monitor);
708 * udev_monitor_filter_add_match_subsystem_devtype:
709 * @udev_monitor: the monitor
710 * @subsystem: the subsystem value to match the incoming devices against
711 * @devtype: the devtype value to match the incoming devices against
713 * This filter is efficiently executed inside the kernel, and libudev subscribers
714 * will usually not be woken up for devices which do not match.
716 * The filter must be installed before the monitor is switched to listening mode.
718 * Returns: 0 on success, otherwise a negative error value.
720 _public_ int udev_monitor_filter_add_match_subsystem_devtype(struct udev_monitor *udev_monitor, const char *subsystem, const char *devtype)
722 if (udev_monitor == NULL)
724 if (subsystem == NULL)
726 if (udev_list_entry_add(&udev_monitor->filter_subsystem_list, subsystem, devtype) == NULL)
732 * udev_monitor_filter_add_match_tag:
733 * @udev_monitor: the monitor
734 * @tag: the name of a tag
736 * This filter is efficiently executed inside the kernel, and libudev subscribers
737 * will usually not be woken up for devices which do not match.
739 * The filter must be installed before the monitor is switched to listening mode.
741 * Returns: 0 on success, otherwise a negative error value.
743 _public_ int udev_monitor_filter_add_match_tag(struct udev_monitor *udev_monitor, const char *tag)
745 if (udev_monitor == NULL)
749 if (udev_list_entry_add(&udev_monitor->filter_tag_list, tag, NULL) == NULL)
755 * udev_monitor_filter_remove:
756 * @udev_monitor: monitor
758 * Remove all filters from monitor.
760 * Returns: 0 on success, otherwise a negative error value.
762 _public_ int udev_monitor_filter_remove(struct udev_monitor *udev_monitor)
764 static struct sock_fprog filter = { 0, NULL };
766 udev_list_cleanup(&udev_monitor->filter_subsystem_list);
767 return setsockopt(udev_monitor->sock, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter));