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)
113 struct udev_monitor *udev_monitor_new_from_netlink_fd(struct udev *udev, const char *name, int fd)
115 struct udev_monitor *udev_monitor;
122 group = UDEV_MONITOR_NONE;
123 else if (strcmp(name, "udev") == 0)
124 group = UDEV_MONITOR_UDEV;
125 else if (strcmp(name, "kernel") == 0)
126 group = UDEV_MONITOR_KERNEL;
130 udev_monitor = udev_monitor_new(udev);
131 if (udev_monitor == NULL)
135 udev_monitor->sock = socket(PF_NETLINK, SOCK_RAW|SOCK_CLOEXEC|SOCK_NONBLOCK, NETLINK_KOBJECT_UEVENT);
136 if (udev_monitor->sock == -1) {
137 err(udev, "error getting socket: %m\n");
142 udev_monitor->bound = true;
143 udev_monitor->sock = fd;
146 udev_monitor->snl.nl_family = AF_NETLINK;
147 udev_monitor->snl.nl_groups = group;
149 /* default destination for sending */
150 udev_monitor->snl_destination.nl_family = AF_NETLINK;
151 udev_monitor->snl_destination.nl_groups = UDEV_MONITOR_UDEV;
157 * udev_monitor_new_from_netlink:
158 * @udev: udev library context
159 * @name: name of event source
161 * Create new udev monitor and connect to a specified event
162 * source. Valid sources identifiers are "udev" and "kernel".
164 * Applications should usually not connect directly to the
165 * "kernel" events, because the devices might not be useable
166 * at that time, before udev has configured them, and created
167 * device nodes. Accessing devices at the same time as udev,
168 * might result in unpredictable behavior. The "udev" events
169 * are sent out after udev has finished its event processing,
170 * all rules have been processed, and needed device nodes are
173 * The initial refcount is 1, and needs to be decremented to
174 * release the resources of the udev monitor.
176 * Returns: a new udev monitor, or #NULL, in case of an error
178 _public_ struct udev_monitor *udev_monitor_new_from_netlink(struct udev *udev, const char *name)
180 return udev_monitor_new_from_netlink_fd(udev, name, -1);
183 static inline void bpf_stmt(struct sock_filter *inss, unsigned int *i,
184 unsigned short code, unsigned int data)
186 struct sock_filter *ins = &inss[*i];
193 static inline void bpf_jmp(struct sock_filter *inss, unsigned int *i,
194 unsigned short code, unsigned int data,
195 unsigned short jt, unsigned short jf)
197 struct sock_filter *ins = &inss[*i];
207 * udev_monitor_filter_update:
208 * @udev_monitor: monitor
210 * Update the installed socket filter. This is only needed,
211 * if the filter was removed or changed.
213 * Returns: 0 on success, otherwise a negative error value.
215 _public_ int udev_monitor_filter_update(struct udev_monitor *udev_monitor)
217 struct sock_filter ins[512];
218 struct sock_fprog filter;
220 struct udev_list_entry *list_entry;
223 if (udev_list_get_entry(&udev_monitor->filter_subsystem_list) == NULL &&
224 udev_list_get_entry(&udev_monitor->filter_tag_list) == NULL)
227 memset(ins, 0x00, sizeof(ins));
230 /* load magic in A */
231 bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, magic));
232 /* jump if magic matches */
233 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, UDEV_MONITOR_MAGIC, 1, 0);
234 /* wrong magic, pass packet */
235 bpf_stmt(ins, &i, BPF_RET|BPF_K, 0xffffffff);
237 if (udev_list_get_entry(&udev_monitor->filter_tag_list) != NULL) {
240 /* count tag matches, to calculate end of tag match block */
242 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_tag_list))
245 /* add all tags matches */
246 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_tag_list)) {
247 uint64_t tag_bloom_bits = util_string_bloom64(udev_list_entry_get_name(list_entry));
248 uint32_t tag_bloom_hi = tag_bloom_bits >> 32;
249 uint32_t tag_bloom_lo = tag_bloom_bits & 0xffffffff;
251 /* load device bloom bits in A */
252 bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_tag_bloom_hi));
253 /* clear bits (tag bits & bloom bits) */
254 bpf_stmt(ins, &i, BPF_ALU|BPF_AND|BPF_K, tag_bloom_hi);
255 /* jump to next tag if it does not match */
256 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, tag_bloom_hi, 0, 3);
258 /* load device bloom bits in A */
259 bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_tag_bloom_lo));
260 /* clear bits (tag bits & bloom bits) */
261 bpf_stmt(ins, &i, BPF_ALU|BPF_AND|BPF_K, tag_bloom_lo);
262 /* jump behind end of tag match block if tag matches */
264 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, tag_bloom_lo, 1 + (tag_matches * 6), 0);
267 /* nothing matched, drop packet */
268 bpf_stmt(ins, &i, BPF_RET|BPF_K, 0);
271 /* add all subsystem matches */
272 if (udev_list_get_entry(&udev_monitor->filter_subsystem_list) != NULL) {
273 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_subsystem_list)) {
274 unsigned int hash = util_string_hash32(udev_list_entry_get_name(list_entry));
276 /* load device subsystem value in A */
277 bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_subsystem_hash));
278 if (udev_list_entry_get_value(list_entry) == NULL) {
279 /* jump if subsystem does not match */
280 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 1);
282 /* jump if subsystem does not match */
283 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 3);
285 /* load device devtype value in A */
286 bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_devtype_hash));
287 /* jump if value does not match */
288 hash = util_string_hash32(udev_list_entry_get_value(list_entry));
289 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 1);
292 /* matched, pass packet */
293 bpf_stmt(ins, &i, BPF_RET|BPF_K, 0xffffffff);
295 if (i+1 >= ARRAY_SIZE(ins))
299 /* nothing matched, drop packet */
300 bpf_stmt(ins, &i, BPF_RET|BPF_K, 0);
303 /* matched, pass packet */
304 bpf_stmt(ins, &i, BPF_RET|BPF_K, 0xffffffff);
307 memset(&filter, 0x00, sizeof(filter));
310 err = setsockopt(udev_monitor->sock, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter));
314 int udev_monitor_allow_unicast_sender(struct udev_monitor *udev_monitor, struct udev_monitor *sender)
316 udev_monitor->snl_trusted_sender.nl_pid = sender->snl.nl_pid;
320 * udev_monitor_enable_receiving:
321 * @udev_monitor: the monitor which should receive events
323 * Binds the @udev_monitor socket to the event source.
325 * Returns: 0 on success, otherwise a negative error value.
327 _public_ int udev_monitor_enable_receiving(struct udev_monitor *udev_monitor)
332 if (udev_monitor->snl.nl_family == 0)
335 udev_monitor_filter_update(udev_monitor);
337 if (!udev_monitor->bound) {
338 err = bind(udev_monitor->sock,
339 (struct sockaddr *)&udev_monitor->snl, sizeof(struct sockaddr_nl));
341 udev_monitor->bound = true;
345 struct sockaddr_nl snl;
349 * get the address the kernel has assigned us
350 * it is usually, but not necessarily the pid
352 addrlen = sizeof(struct sockaddr_nl);
353 err = getsockname(udev_monitor->sock, (struct sockaddr *)&snl, &addrlen);
355 udev_monitor->snl.nl_pid = snl.nl_pid;
357 err(udev_monitor->udev, "bind failed: %m\n");
361 /* enable receiving of sender credentials */
362 setsockopt(udev_monitor->sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
367 * udev_monitor_set_receive_buffer_size:
368 * @udev_monitor: the monitor which should receive events
369 * @size: the size in bytes
371 * Set the size of the kernel socket buffer. This call needs the
372 * appropriate privileges to succeed.
374 * Returns: 0 on success, otherwise -1 on error.
376 _public_ int udev_monitor_set_receive_buffer_size(struct udev_monitor *udev_monitor, int size)
378 if (udev_monitor == NULL)
380 return setsockopt(udev_monitor->sock, SOL_SOCKET, SO_RCVBUFFORCE, &size, sizeof(size));
383 int udev_monitor_disconnect(struct udev_monitor *udev_monitor)
387 err = close(udev_monitor->sock);
388 udev_monitor->sock = -1;
394 * @udev_monitor: udev monitor
396 * Take a reference of a udev monitor.
398 * Returns: the passed udev monitor
400 _public_ struct udev_monitor *udev_monitor_ref(struct udev_monitor *udev_monitor)
402 if (udev_monitor == NULL)
404 udev_monitor->refcount++;
409 * udev_monitor_unref:
410 * @udev_monitor: udev monitor
412 * Drop a reference of a udev monitor. If the refcount reaches zero,
413 * the bound socket will be closed, and the resources of the monitor
417 _public_ void udev_monitor_unref(struct udev_monitor *udev_monitor)
419 if (udev_monitor == NULL)
421 udev_monitor->refcount--;
422 if (udev_monitor->refcount > 0)
424 if (udev_monitor->sock >= 0)
425 close(udev_monitor->sock);
426 udev_list_cleanup(&udev_monitor->filter_subsystem_list);
427 udev_list_cleanup(&udev_monitor->filter_tag_list);
432 * udev_monitor_get_udev:
433 * @udev_monitor: udev monitor
435 * Retrieve the udev library context the monitor was created with.
437 * Returns: the udev library context
439 _public_ struct udev *udev_monitor_get_udev(struct udev_monitor *udev_monitor)
441 if (udev_monitor == NULL)
443 return udev_monitor->udev;
447 * udev_monitor_get_fd:
448 * @udev_monitor: udev monitor
450 * Retrieve the socket file descriptor associated with the monitor.
452 * Returns: the socket file descriptor
454 _public_ int udev_monitor_get_fd(struct udev_monitor *udev_monitor)
456 if (udev_monitor == NULL)
458 return udev_monitor->sock;
461 static int passes_filter(struct udev_monitor *udev_monitor, struct udev_device *udev_device)
463 struct udev_list_entry *list_entry;
465 if (udev_list_get_entry(&udev_monitor->filter_subsystem_list) == NULL)
467 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_subsystem_list)) {
468 const char *subsys = udev_list_entry_get_name(list_entry);
469 const char *dsubsys = udev_device_get_subsystem(udev_device);
471 const char *ddevtype;
473 if (strcmp(dsubsys, subsys) != 0)
476 devtype = udev_list_entry_get_value(list_entry);
479 ddevtype = udev_device_get_devtype(udev_device);
480 if (ddevtype == NULL)
482 if (strcmp(ddevtype, devtype) == 0)
488 if (udev_list_get_entry(&udev_monitor->filter_tag_list) == NULL)
490 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_tag_list)) {
491 const char *tag = udev_list_entry_get_name(list_entry);
493 if (udev_device_has_tag(udev_device, tag))
500 * udev_monitor_receive_device:
501 * @udev_monitor: udev monitor
503 * Receive data from the udev monitor socket, allocate a new udev
504 * device, fill in the received data, and return the device.
506 * Only socket connections with uid=0 are accepted.
508 * The initial refcount is 1, and needs to be decremented to
509 * release the resources of the udev device.
511 * Returns: a new udev device, or #NULL, in case of an error
513 _public_ struct udev_device *udev_monitor_receive_device(struct udev_monitor *udev_monitor)
515 struct udev_device *udev_device;
518 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
519 struct cmsghdr *cmsg;
520 struct sockaddr_nl snl;
525 struct udev_monitor_netlink_header *nlh;
528 if (udev_monitor == NULL)
531 iov.iov_len = sizeof(buf);
532 memset (&smsg, 0x00, sizeof(struct msghdr));
535 smsg.msg_control = cred_msg;
536 smsg.msg_controllen = sizeof(cred_msg);
538 if (udev_monitor->snl.nl_family != 0) {
539 smsg.msg_name = &snl;
540 smsg.msg_namelen = sizeof(snl);
543 buflen = recvmsg(udev_monitor->sock, &smsg, 0);
546 dbg(udev_monitor->udev, "unable to receive message\n");
550 if (buflen < 32 || (size_t)buflen >= sizeof(buf)) {
551 dbg(udev_monitor->udev, "invalid message length\n");
555 if (udev_monitor->snl.nl_family != 0) {
556 if (snl.nl_groups == 0) {
557 /* unicast message, check if we trust the sender */
558 if (udev_monitor->snl_trusted_sender.nl_pid == 0 ||
559 snl.nl_pid != udev_monitor->snl_trusted_sender.nl_pid) {
560 dbg(udev_monitor->udev, "unicast netlink message ignored\n");
563 } else if (snl.nl_groups == UDEV_MONITOR_KERNEL) {
564 if (snl.nl_pid > 0) {
565 dbg(udev_monitor->udev, "multicast kernel netlink message from pid %d ignored\n",
572 cmsg = CMSG_FIRSTHDR(&smsg);
573 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
574 dbg(udev_monitor->udev, "no sender credentials received, message ignored\n");
578 cred = (struct ucred *)CMSG_DATA(cmsg);
579 if (cred->uid != 0) {
580 dbg(udev_monitor->udev, "sender uid=%d, message ignored\n", cred->uid);
584 if (memcmp(buf, "libudev", 8) == 0) {
585 /* udev message needs proper version magic */
586 nlh = (struct udev_monitor_netlink_header *) buf;
587 if (nlh->magic != htonl(UDEV_MONITOR_MAGIC)) {
588 err(udev_monitor->udev, "unrecognized message signature (%x != %x)\n",
589 nlh->magic, htonl(UDEV_MONITOR_MAGIC));
592 if (nlh->properties_off+32 > buflen)
594 bufpos = nlh->properties_off;
596 /* kernel message with header */
597 bufpos = strlen(buf) + 1;
598 if ((size_t)bufpos < sizeof("a@/d") || bufpos >= buflen) {
599 dbg(udev_monitor->udev, "invalid message length\n");
603 /* check message header */
604 if (strstr(buf, "@/") == NULL) {
605 dbg(udev_monitor->udev, "unrecognized message header\n");
610 udev_device = udev_device_new(udev_monitor->udev);
611 if (udev_device == NULL)
613 udev_device_set_info_loaded(udev_device);
615 while (bufpos < buflen) {
620 keylen = strlen(key);
623 bufpos += keylen + 1;
624 udev_device_add_property_from_string_parse(udev_device, key);
627 if (udev_device_add_property_from_string_parse_finish(udev_device) < 0) {
628 dbg(udev_monitor->udev, "missing values, invalid device\n");
629 udev_device_unref(udev_device);
633 /* skip device, if it does not pass the current filter */
634 if (!passes_filter(udev_monitor, udev_device)) {
635 struct pollfd pfd[1];
638 udev_device_unref(udev_device);
640 /* if something is queued, get next device */
641 pfd[0].fd = udev_monitor->sock;
642 pfd[0].events = POLLIN;
643 rc = poll(pfd, 1, 0);
652 int udev_monitor_send_device(struct udev_monitor *udev_monitor,
653 struct udev_monitor *destination, struct udev_device *udev_device)
661 struct udev_monitor_netlink_header nlh;
662 struct udev_list_entry *list_entry;
663 uint64_t tag_bloom_bits;
665 if (udev_monitor->snl.nl_family == 0)
668 blen = udev_device_get_properties_monitor_buf(udev_device, &buf);
672 /* add versioned header */
673 memset(&nlh, 0x00, sizeof(struct udev_monitor_netlink_header));
674 memcpy(nlh.prefix, "libudev", 8);
675 nlh.magic = htonl(UDEV_MONITOR_MAGIC);
676 nlh.header_size = sizeof(struct udev_monitor_netlink_header);
677 val = udev_device_get_subsystem(udev_device);
678 nlh.filter_subsystem_hash = htonl(util_string_hash32(val));
679 val = udev_device_get_devtype(udev_device);
681 nlh.filter_devtype_hash = htonl(util_string_hash32(val));
682 iov[0].iov_base = &nlh;
683 iov[0].iov_len = sizeof(struct udev_monitor_netlink_header);
685 /* add tag bloom filter */
687 udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(udev_device))
688 tag_bloom_bits |= util_string_bloom64(udev_list_entry_get_name(list_entry));
689 if (tag_bloom_bits > 0) {
690 nlh.filter_tag_bloom_hi = htonl(tag_bloom_bits >> 32);
691 nlh.filter_tag_bloom_lo = htonl(tag_bloom_bits & 0xffffffff);
694 /* add properties list */
695 nlh.properties_off = iov[0].iov_len;
696 nlh.properties_len = blen;
697 iov[1].iov_base = (char *)buf;
698 iov[1].iov_len = blen;
700 memset(&smsg, 0x00, sizeof(struct msghdr));
704 * Use custom address for target, or the default one.
706 * If we send to a multicast group, we will get
707 * ECONNREFUSED, which is expected.
709 if (destination != NULL)
710 smsg.msg_name = &destination->snl;
712 smsg.msg_name = &udev_monitor->snl_destination;
713 smsg.msg_namelen = sizeof(struct sockaddr_nl);
714 count = sendmsg(udev_monitor->sock, &smsg, 0);
715 dbg(udev_monitor->udev, "passed %zi bytes to netlink monitor %p\n", count, udev_monitor);
720 * udev_monitor_filter_add_match_subsystem_devtype:
721 * @udev_monitor: the monitor
722 * @subsystem: the subsystem value to match the incoming devices against
723 * @devtype: the devtype value to match the incoming devices against
725 * This filter is efficiently executed inside the kernel, and libudev subscribers
726 * will usually not be woken up for devices which do not match.
728 * The filter must be installed before the monitor is switched to listening mode.
730 * Returns: 0 on success, otherwise a negative error value.
732 _public_ int udev_monitor_filter_add_match_subsystem_devtype(struct udev_monitor *udev_monitor, const char *subsystem, const char *devtype)
734 if (udev_monitor == NULL)
736 if (subsystem == NULL)
738 if (udev_list_entry_add(&udev_monitor->filter_subsystem_list, subsystem, devtype) == NULL)
744 * udev_monitor_filter_add_match_tag:
745 * @udev_monitor: the monitor
746 * @tag: the name of a tag
748 * This filter is efficiently executed inside the kernel, and libudev subscribers
749 * will usually not be woken up for devices which do not match.
751 * The filter must be installed before the monitor is switched to listening mode.
753 * Returns: 0 on success, otherwise a negative error value.
755 _public_ int udev_monitor_filter_add_match_tag(struct udev_monitor *udev_monitor, const char *tag)
757 if (udev_monitor == NULL)
761 if (udev_list_entry_add(&udev_monitor->filter_tag_list, tag, NULL) == NULL)
767 * udev_monitor_filter_remove:
768 * @udev_monitor: monitor
770 * Remove all filters from monitor.
772 * Returns: 0 on success, otherwise a negative error value.
774 _public_ int udev_monitor_filter_remove(struct udev_monitor *udev_monitor)
776 static struct sock_fprog filter = { 0, NULL };
778 udev_list_cleanup(&udev_monitor->filter_subsystem_list);
779 return setsockopt(udev_monitor->sock, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter));