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 an event source.
46 struct sockaddr_nl snl;
47 struct sockaddr_nl snl_trusted_sender;
48 struct sockaddr_nl snl_destination;
50 struct udev_list filter_subsystem_list;
51 struct udev_list 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, &udev_monitor->filter_subsystem_list, false);
95 udev_list_init(udev, &udev_monitor->filter_tag_list, true);
100 * udev_monitor_new_from_socket:
101 * @udev: udev library context
102 * @socket_path: unix socket path
104 * This function is removed from libudev and will not do anything.
108 _public_ struct udev_monitor *udev_monitor_new_from_socket(struct udev *udev, const char *socket_path)
114 struct udev_monitor *udev_monitor_new_from_netlink_fd(struct udev *udev, const char *name, int fd)
116 struct udev_monitor *udev_monitor;
123 group = UDEV_MONITOR_NONE;
124 else if (strcmp(name, "udev") == 0)
125 group = UDEV_MONITOR_UDEV;
126 else if (strcmp(name, "kernel") == 0)
127 group = UDEV_MONITOR_KERNEL;
131 udev_monitor = udev_monitor_new(udev);
132 if (udev_monitor == NULL)
136 udev_monitor->sock = socket(PF_NETLINK, SOCK_RAW|SOCK_CLOEXEC|SOCK_NONBLOCK, NETLINK_KOBJECT_UEVENT);
137 if (udev_monitor->sock == -1) {
138 err(udev, "error getting socket: %m\n");
143 udev_monitor->bound = true;
144 udev_monitor->sock = fd;
147 udev_monitor->snl.nl_family = AF_NETLINK;
148 udev_monitor->snl.nl_groups = group;
150 /* default destination for sending */
151 udev_monitor->snl_destination.nl_family = AF_NETLINK;
152 udev_monitor->snl_destination.nl_groups = UDEV_MONITOR_UDEV;
158 * udev_monitor_new_from_netlink:
159 * @udev: udev library context
160 * @name: name of event source
162 * Create new udev monitor and connect to a specified event
163 * source. Valid sources identifiers are "udev" and "kernel".
165 * Applications should usually not connect directly to the
166 * "kernel" events, because the devices might not be useable
167 * at that time, before udev has configured them, and created
168 * device nodes. Accessing devices at the same time as udev,
169 * might result in unpredictable behavior. The "udev" events
170 * are sent out after udev has finished its event processing,
171 * all rules have been processed, and needed device nodes are
174 * The initial refcount is 1, and needs to be decremented to
175 * release the resources of the udev monitor.
177 * Returns: a new udev monitor, or #NULL, in case of an error
179 _public_ struct udev_monitor *udev_monitor_new_from_netlink(struct udev *udev, const char *name)
181 return udev_monitor_new_from_netlink_fd(udev, name, -1);
184 static inline void bpf_stmt(struct sock_filter *inss, unsigned int *i,
185 unsigned short code, unsigned int data)
187 struct sock_filter *ins = &inss[*i];
194 static inline void bpf_jmp(struct sock_filter *inss, unsigned int *i,
195 unsigned short code, unsigned int data,
196 unsigned short jt, unsigned short jf)
198 struct sock_filter *ins = &inss[*i];
208 * udev_monitor_filter_update:
209 * @udev_monitor: monitor
211 * Update the installed socket filter. This is only needed,
212 * if the filter was removed or changed.
214 * Returns: 0 on success, otherwise a negative error value.
216 _public_ int udev_monitor_filter_update(struct udev_monitor *udev_monitor)
218 struct sock_filter ins[512];
219 struct sock_fprog filter;
221 struct udev_list_entry *list_entry;
224 if (udev_list_get_entry(&udev_monitor->filter_subsystem_list) == NULL &&
225 udev_list_get_entry(&udev_monitor->filter_tag_list) == NULL)
228 memset(ins, 0x00, sizeof(ins));
231 /* load magic in A */
232 bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, magic));
233 /* jump if magic matches */
234 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, UDEV_MONITOR_MAGIC, 1, 0);
235 /* wrong magic, pass packet */
236 bpf_stmt(ins, &i, BPF_RET|BPF_K, 0xffffffff);
238 if (udev_list_get_entry(&udev_monitor->filter_tag_list) != NULL) {
241 /* count tag matches, to calculate end of tag match block */
243 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_tag_list))
246 /* add all tags matches */
247 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_tag_list)) {
248 uint64_t tag_bloom_bits = util_string_bloom64(udev_list_entry_get_name(list_entry));
249 uint32_t tag_bloom_hi = tag_bloom_bits >> 32;
250 uint32_t tag_bloom_lo = tag_bloom_bits & 0xffffffff;
252 /* load device bloom bits in A */
253 bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_tag_bloom_hi));
254 /* clear bits (tag bits & bloom bits) */
255 bpf_stmt(ins, &i, BPF_ALU|BPF_AND|BPF_K, tag_bloom_hi);
256 /* jump to next tag if it does not match */
257 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, tag_bloom_hi, 0, 3);
259 /* load device bloom bits in A */
260 bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_tag_bloom_lo));
261 /* clear bits (tag bits & bloom bits) */
262 bpf_stmt(ins, &i, BPF_ALU|BPF_AND|BPF_K, tag_bloom_lo);
263 /* jump behind end of tag match block if tag matches */
265 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, tag_bloom_lo, 1 + (tag_matches * 6), 0);
268 /* nothing matched, drop packet */
269 bpf_stmt(ins, &i, BPF_RET|BPF_K, 0);
272 /* add all subsystem matches */
273 if (udev_list_get_entry(&udev_monitor->filter_subsystem_list) != NULL) {
274 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_subsystem_list)) {
275 unsigned int hash = util_string_hash32(udev_list_entry_get_name(list_entry));
277 /* load device subsystem value in A */
278 bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_subsystem_hash));
279 if (udev_list_entry_get_value(list_entry) == NULL) {
280 /* jump if subsystem does not match */
281 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 1);
283 /* jump if subsystem does not match */
284 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 3);
286 /* load device devtype value in A */
287 bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_devtype_hash));
288 /* jump if value does not match */
289 hash = util_string_hash32(udev_list_entry_get_value(list_entry));
290 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 1);
293 /* matched, pass packet */
294 bpf_stmt(ins, &i, BPF_RET|BPF_K, 0xffffffff);
296 if (i+1 >= ARRAY_SIZE(ins))
300 /* nothing matched, drop packet */
301 bpf_stmt(ins, &i, BPF_RET|BPF_K, 0);
304 /* matched, pass packet */
305 bpf_stmt(ins, &i, BPF_RET|BPF_K, 0xffffffff);
308 memset(&filter, 0x00, sizeof(filter));
311 err = setsockopt(udev_monitor->sock, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter));
315 int udev_monitor_allow_unicast_sender(struct udev_monitor *udev_monitor, struct udev_monitor *sender)
317 udev_monitor->snl_trusted_sender.nl_pid = sender->snl.nl_pid;
321 * udev_monitor_enable_receiving:
322 * @udev_monitor: the monitor which should receive events
324 * Binds the @udev_monitor socket to the event source.
326 * Returns: 0 on success, otherwise a negative error value.
328 _public_ int udev_monitor_enable_receiving(struct udev_monitor *udev_monitor)
333 if (udev_monitor->snl.nl_family == 0)
336 udev_monitor_filter_update(udev_monitor);
338 if (!udev_monitor->bound) {
339 err = bind(udev_monitor->sock,
340 (struct sockaddr *)&udev_monitor->snl, sizeof(struct sockaddr_nl));
342 udev_monitor->bound = true;
346 struct sockaddr_nl snl;
350 * get the address the kernel has assigned us
351 * it is usually, but not necessarily the pid
353 addrlen = sizeof(struct sockaddr_nl);
354 err = getsockname(udev_monitor->sock, (struct sockaddr *)&snl, &addrlen);
356 udev_monitor->snl.nl_pid = snl.nl_pid;
358 err(udev_monitor->udev, "bind failed: %m\n");
362 /* enable receiving of sender credentials */
363 setsockopt(udev_monitor->sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
368 * udev_monitor_set_receive_buffer_size:
369 * @udev_monitor: the monitor which should receive events
370 * @size: the size in bytes
372 * Set the size of the kernel socket buffer. This call needs the
373 * appropriate privileges to succeed.
375 * Returns: 0 on success, otherwise -1 on error.
377 _public_ int udev_monitor_set_receive_buffer_size(struct udev_monitor *udev_monitor, int size)
379 if (udev_monitor == NULL)
381 return setsockopt(udev_monitor->sock, SOL_SOCKET, SO_RCVBUFFORCE, &size, sizeof(size));
384 int udev_monitor_disconnect(struct udev_monitor *udev_monitor)
388 err = close(udev_monitor->sock);
389 udev_monitor->sock = -1;
395 * @udev_monitor: udev monitor
397 * Take a reference of a udev monitor.
399 * Returns: the passed udev monitor
401 _public_ struct udev_monitor *udev_monitor_ref(struct udev_monitor *udev_monitor)
403 if (udev_monitor == NULL)
405 udev_monitor->refcount++;
410 * udev_monitor_unref:
411 * @udev_monitor: udev monitor
413 * Drop a reference of a udev monitor. If the refcount reaches zero,
414 * the bound socket will be closed, and the resources of the monitor
418 _public_ void udev_monitor_unref(struct udev_monitor *udev_monitor)
420 if (udev_monitor == NULL)
422 udev_monitor->refcount--;
423 if (udev_monitor->refcount > 0)
425 if (udev_monitor->sock >= 0)
426 close(udev_monitor->sock);
427 udev_list_cleanup(&udev_monitor->filter_subsystem_list);
428 udev_list_cleanup(&udev_monitor->filter_tag_list);
433 * udev_monitor_get_udev:
434 * @udev_monitor: udev monitor
436 * Retrieve the udev library context the monitor was created with.
438 * Returns: the udev library context
440 _public_ struct udev *udev_monitor_get_udev(struct udev_monitor *udev_monitor)
442 if (udev_monitor == NULL)
444 return udev_monitor->udev;
448 * udev_monitor_get_fd:
449 * @udev_monitor: udev monitor
451 * Retrieve the socket file descriptor associated with the monitor.
453 * Returns: the socket file descriptor
455 _public_ int udev_monitor_get_fd(struct udev_monitor *udev_monitor)
457 if (udev_monitor == NULL)
459 return udev_monitor->sock;
462 static int passes_filter(struct udev_monitor *udev_monitor, struct udev_device *udev_device)
464 struct udev_list_entry *list_entry;
466 if (udev_list_get_entry(&udev_monitor->filter_subsystem_list) == NULL)
468 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_subsystem_list)) {
469 const char *subsys = udev_list_entry_get_name(list_entry);
470 const char *dsubsys = udev_device_get_subsystem(udev_device);
472 const char *ddevtype;
474 if (strcmp(dsubsys, subsys) != 0)
477 devtype = udev_list_entry_get_value(list_entry);
480 ddevtype = udev_device_get_devtype(udev_device);
481 if (ddevtype == NULL)
483 if (strcmp(ddevtype, devtype) == 0)
489 if (udev_list_get_entry(&udev_monitor->filter_tag_list) == NULL)
491 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_tag_list)) {
492 const char *tag = udev_list_entry_get_name(list_entry);
494 if (udev_device_has_tag(udev_device, tag))
501 * udev_monitor_receive_device:
502 * @udev_monitor: udev monitor
504 * Receive data from the udev monitor socket, allocate a new udev
505 * device, fill in the received data, and return the device.
507 * Only socket connections with uid=0 are accepted.
509 * The initial refcount is 1, and needs to be decremented to
510 * release the resources of the udev device.
512 * Returns: a new udev device, or #NULL, in case of an error
514 _public_ struct udev_device *udev_monitor_receive_device(struct udev_monitor *udev_monitor)
516 struct udev_device *udev_device;
519 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
520 struct cmsghdr *cmsg;
521 struct sockaddr_nl snl;
526 struct udev_monitor_netlink_header *nlh;
529 if (udev_monitor == NULL)
532 iov.iov_len = sizeof(buf);
533 memset (&smsg, 0x00, sizeof(struct msghdr));
536 smsg.msg_control = cred_msg;
537 smsg.msg_controllen = sizeof(cred_msg);
539 if (udev_monitor->snl.nl_family != 0) {
540 smsg.msg_name = &snl;
541 smsg.msg_namelen = sizeof(snl);
544 buflen = recvmsg(udev_monitor->sock, &smsg, 0);
547 dbg(udev_monitor->udev, "unable to receive message\n");
551 if (buflen < 32 || (size_t)buflen >= sizeof(buf)) {
552 dbg(udev_monitor->udev, "invalid message length\n");
556 if (udev_monitor->snl.nl_family != 0) {
557 if (snl.nl_groups == 0) {
558 /* unicast message, check if we trust the sender */
559 if (udev_monitor->snl_trusted_sender.nl_pid == 0 ||
560 snl.nl_pid != udev_monitor->snl_trusted_sender.nl_pid) {
561 dbg(udev_monitor->udev, "unicast netlink message ignored\n");
564 } else if (snl.nl_groups == UDEV_MONITOR_KERNEL) {
565 if (snl.nl_pid > 0) {
566 dbg(udev_monitor->udev, "multicast kernel netlink message from pid %d ignored\n",
573 cmsg = CMSG_FIRSTHDR(&smsg);
574 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
575 dbg(udev_monitor->udev, "no sender credentials received, message ignored\n");
579 cred = (struct ucred *)CMSG_DATA(cmsg);
580 if (cred->uid != 0) {
581 dbg(udev_monitor->udev, "sender uid=%d, message ignored\n", cred->uid);
585 if (memcmp(buf, "libudev", 8) == 0) {
586 /* udev message needs proper version magic */
587 nlh = (struct udev_monitor_netlink_header *) buf;
588 if (nlh->magic != htonl(UDEV_MONITOR_MAGIC)) {
589 err(udev_monitor->udev, "unrecognized message signature (%x != %x)\n",
590 nlh->magic, htonl(UDEV_MONITOR_MAGIC));
593 if (nlh->properties_off+32 > buflen)
595 bufpos = nlh->properties_off;
597 /* kernel message with header */
598 bufpos = strlen(buf) + 1;
599 if ((size_t)bufpos < sizeof("a@/d") || bufpos >= buflen) {
600 dbg(udev_monitor->udev, "invalid message length\n");
604 /* check message header */
605 if (strstr(buf, "@/") == NULL) {
606 dbg(udev_monitor->udev, "unrecognized message header\n");
611 udev_device = udev_device_new(udev_monitor->udev);
612 if (udev_device == NULL)
614 udev_device_set_info_loaded(udev_device);
616 while (bufpos < buflen) {
621 keylen = strlen(key);
624 bufpos += keylen + 1;
625 udev_device_add_property_from_string_parse(udev_device, key);
628 if (udev_device_add_property_from_string_parse_finish(udev_device) < 0) {
629 dbg(udev_monitor->udev, "missing values, invalid device\n");
630 udev_device_unref(udev_device);
634 /* skip device, if it does not pass the current filter */
635 if (!passes_filter(udev_monitor, udev_device)) {
636 struct pollfd pfd[1];
639 udev_device_unref(udev_device);
641 /* if something is queued, get next device */
642 pfd[0].fd = udev_monitor->sock;
643 pfd[0].events = POLLIN;
644 rc = poll(pfd, 1, 0);
653 int udev_monitor_send_device(struct udev_monitor *udev_monitor,
654 struct udev_monitor *destination, struct udev_device *udev_device)
662 struct udev_monitor_netlink_header nlh;
663 struct udev_list_entry *list_entry;
664 uint64_t tag_bloom_bits;
666 if (udev_monitor->snl.nl_family == 0)
669 blen = udev_device_get_properties_monitor_buf(udev_device, &buf);
673 /* add versioned header */
674 memset(&nlh, 0x00, sizeof(struct udev_monitor_netlink_header));
675 memcpy(nlh.prefix, "libudev", 8);
676 nlh.magic = htonl(UDEV_MONITOR_MAGIC);
677 nlh.header_size = sizeof(struct udev_monitor_netlink_header);
678 val = udev_device_get_subsystem(udev_device);
679 nlh.filter_subsystem_hash = htonl(util_string_hash32(val));
680 val = udev_device_get_devtype(udev_device);
682 nlh.filter_devtype_hash = htonl(util_string_hash32(val));
683 iov[0].iov_base = &nlh;
684 iov[0].iov_len = sizeof(struct udev_monitor_netlink_header);
686 /* add tag bloom filter */
688 udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(udev_device))
689 tag_bloom_bits |= util_string_bloom64(udev_list_entry_get_name(list_entry));
690 if (tag_bloom_bits > 0) {
691 nlh.filter_tag_bloom_hi = htonl(tag_bloom_bits >> 32);
692 nlh.filter_tag_bloom_lo = htonl(tag_bloom_bits & 0xffffffff);
695 /* add properties list */
696 nlh.properties_off = iov[0].iov_len;
697 nlh.properties_len = blen;
698 iov[1].iov_base = (char *)buf;
699 iov[1].iov_len = blen;
701 memset(&smsg, 0x00, sizeof(struct msghdr));
705 * Use custom address for target, or the default one.
707 * If we send to a multicast group, we will get
708 * ECONNREFUSED, which is expected.
710 if (destination != NULL)
711 smsg.msg_name = &destination->snl;
713 smsg.msg_name = &udev_monitor->snl_destination;
714 smsg.msg_namelen = sizeof(struct sockaddr_nl);
715 count = sendmsg(udev_monitor->sock, &smsg, 0);
716 dbg(udev_monitor->udev, "passed %zi bytes to netlink monitor %p\n", count, udev_monitor);
721 * udev_monitor_filter_add_match_subsystem_devtype:
722 * @udev_monitor: the monitor
723 * @subsystem: the subsystem value to match the incoming devices against
724 * @devtype: the devtype value to match the incoming devices against
726 * This filter is efficiently executed inside the kernel, and libudev subscribers
727 * will usually not be woken up for devices which do not match.
729 * The filter must be installed before the monitor is switched to listening mode.
731 * Returns: 0 on success, otherwise a negative error value.
733 _public_ int udev_monitor_filter_add_match_subsystem_devtype(struct udev_monitor *udev_monitor, const char *subsystem, const char *devtype)
735 if (udev_monitor == NULL)
737 if (subsystem == NULL)
739 if (udev_list_entry_add(&udev_monitor->filter_subsystem_list, subsystem, devtype) == NULL)
745 * udev_monitor_filter_add_match_tag:
746 * @udev_monitor: the monitor
747 * @tag: the name of a tag
749 * This filter is efficiently executed inside the kernel, and libudev subscribers
750 * will usually not be woken up for devices which do not match.
752 * The filter must be installed before the monitor is switched to listening mode.
754 * Returns: 0 on success, otherwise a negative error value.
756 _public_ int udev_monitor_filter_add_match_tag(struct udev_monitor *udev_monitor, const char *tag)
758 if (udev_monitor == NULL)
762 if (udev_list_entry_add(&udev_monitor->filter_tag_list, tag, NULL) == NULL)
768 * udev_monitor_filter_remove:
769 * @udev_monitor: monitor
771 * Remove all filters from monitor.
773 * Returns: 0 on success, otherwise a negative error value.
775 _public_ int udev_monitor_filter_remove(struct udev_monitor *udev_monitor)
777 static struct sock_fprog filter = { 0, NULL };
779 udev_list_cleanup(&udev_monitor->filter_subsystem_list);
780 return setsockopt(udev_monitor->sock, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter));