2 This file is part of systemd.
4 Copyright 2008-2012 Kay Sievers <kay@vrfy.org>
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
29 #include <sys/socket.h>
31 #include <arpa/inet.h>
32 #include <linux/netlink.h>
33 #include <linux/filter.h>
36 #include "libudev-private.h"
37 #include "socket-util.h"
40 * SECTION:libudev-monitor
41 * @short_description: device event source
43 * Connects to a device event source.
49 * Opaque object handling an event source.
55 union sockaddr_union snl;
56 union sockaddr_union snl_trusted_sender;
57 union sockaddr_union snl_destination;
59 struct udev_list filter_subsystem_list;
60 struct udev_list filter_tag_list;
64 enum udev_monitor_netlink_group {
70 #define UDEV_MONITOR_MAGIC 0xfeedcafe
71 struct udev_monitor_netlink_header {
72 /* "libudev" prefix to distinguish libudev and kernel messages */
75 * magic to protect against daemon <-> library message format mismatch
76 * used in the kernel from socket filter rules; needs to be stored in network order
79 /* total length of header structure known to the sender */
80 unsigned int header_size;
81 /* properties string buffer */
82 unsigned int properties_off;
83 unsigned int properties_len;
85 * hashes of primary device properties strings, to let libudev subscribers
86 * use in-kernel socket filters; values need to be stored in network order
88 unsigned int filter_subsystem_hash;
89 unsigned int filter_devtype_hash;
90 unsigned int filter_tag_bloom_hi;
91 unsigned int filter_tag_bloom_lo;
94 static struct udev_monitor *udev_monitor_new(struct udev *udev)
96 struct udev_monitor *udev_monitor;
98 udev_monitor = calloc(1, sizeof(struct udev_monitor));
99 if (udev_monitor == NULL)
101 udev_monitor->refcount = 1;
102 udev_monitor->udev = udev;
103 udev_list_init(udev, &udev_monitor->filter_subsystem_list, false);
104 udev_list_init(udev, &udev_monitor->filter_tag_list, true);
108 struct udev_monitor *udev_monitor_new_from_netlink_fd(struct udev *udev, const char *name, int fd)
110 struct udev_monitor *udev_monitor;
117 group = UDEV_MONITOR_NONE;
118 else if (streq(name, "udev"))
119 group = UDEV_MONITOR_UDEV;
120 else if (streq(name, "kernel"))
121 group = UDEV_MONITOR_KERNEL;
126 * We do not support subscribing to uevents if no instance of udev
127 * is running. Uevents would otherwise broadcast the processing data
128 * of the host into containers, which is not acceptable. Containers
129 * will currently just not get any uevents.
133 udev_monitor = udev_monitor_new(udev);
134 if (udev_monitor == NULL)
138 udev_monitor->sock = socket(PF_NETLINK, SOCK_RAW|SOCK_CLOEXEC|SOCK_NONBLOCK, NETLINK_KOBJECT_UEVENT);
139 if (udev_monitor->sock == -1) {
140 udev_err(udev, "error getting socket: %m\n");
145 udev_monitor->bound = true;
146 udev_monitor->sock = fd;
149 udev_monitor->snl.nl.nl_family = AF_NETLINK;
150 udev_monitor->snl.nl.nl_groups = group;
152 /* default destination for sending */
153 udev_monitor->snl_destination.nl.nl_family = AF_NETLINK;
154 udev_monitor->snl_destination.nl.nl_groups = UDEV_MONITOR_UDEV;
160 * udev_monitor_new_from_netlink:
161 * @udev: udev library context
162 * @name: name of event source
164 * Create new udev monitor and connect to a specified event
165 * source. Valid sources identifiers are "udev" and "kernel".
167 * Applications should usually not connect directly to the
168 * "kernel" events, because the devices might not be useable
169 * at that time, before udev has configured them, and created
170 * device nodes. Accessing devices at the same time as udev,
171 * might result in unpredictable behavior. The "udev" events
172 * are sent out after udev has finished its event processing,
173 * all rules have been processed, and needed device nodes are
176 * The initial refcount is 1, and needs to be decremented to
177 * release the resources of the udev monitor.
179 * Returns: a new udev monitor, or #NULL, in case of an error
181 _public_ struct udev_monitor *udev_monitor_new_from_netlink(struct udev *udev, const char *name)
183 return udev_monitor_new_from_netlink_fd(udev, name, -1);
186 static inline void bpf_stmt(struct sock_filter *inss, unsigned int *i,
187 unsigned short code, unsigned int data)
189 struct sock_filter *ins = &inss[*i];
196 static inline void bpf_jmp(struct sock_filter *inss, unsigned int *i,
197 unsigned short code, unsigned int data,
198 unsigned short jt, unsigned short jf)
200 struct sock_filter *ins = &inss[*i];
210 * udev_monitor_filter_update:
211 * @udev_monitor: monitor
213 * Update the installed socket filter. This is only needed,
214 * if the filter was removed or changed.
216 * Returns: 0 on success, otherwise a negative error value.
218 _public_ int udev_monitor_filter_update(struct udev_monitor *udev_monitor)
220 struct sock_filter ins[512];
221 struct sock_fprog filter;
223 struct udev_list_entry *list_entry;
226 if (udev_list_get_entry(&udev_monitor->filter_subsystem_list) == NULL &&
227 udev_list_get_entry(&udev_monitor->filter_tag_list) == NULL)
230 memset(ins, 0x00, sizeof(ins));
233 /* load magic in A */
234 bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, magic));
235 /* jump if magic matches */
236 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, UDEV_MONITOR_MAGIC, 1, 0);
237 /* wrong magic, pass packet */
238 bpf_stmt(ins, &i, BPF_RET|BPF_K, 0xffffffff);
240 if (udev_list_get_entry(&udev_monitor->filter_tag_list) != NULL) {
243 /* count tag matches, to calculate end of tag match block */
245 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_tag_list))
248 /* add all tags matches */
249 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_tag_list)) {
250 uint64_t tag_bloom_bits = util_string_bloom64(udev_list_entry_get_name(list_entry));
251 uint32_t tag_bloom_hi = tag_bloom_bits >> 32;
252 uint32_t tag_bloom_lo = tag_bloom_bits & 0xffffffff;
254 /* load device bloom bits in A */
255 bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_tag_bloom_hi));
256 /* clear bits (tag bits & bloom bits) */
257 bpf_stmt(ins, &i, BPF_ALU|BPF_AND|BPF_K, tag_bloom_hi);
258 /* jump to next tag if it does not match */
259 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, tag_bloom_hi, 0, 3);
261 /* load device bloom bits in A */
262 bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_tag_bloom_lo));
263 /* clear bits (tag bits & bloom bits) */
264 bpf_stmt(ins, &i, BPF_ALU|BPF_AND|BPF_K, tag_bloom_lo);
265 /* jump behind end of tag match block if tag matches */
267 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, tag_bloom_lo, 1 + (tag_matches * 6), 0);
270 /* nothing matched, drop packet */
271 bpf_stmt(ins, &i, BPF_RET|BPF_K, 0);
274 /* add all subsystem matches */
275 if (udev_list_get_entry(&udev_monitor->filter_subsystem_list) != NULL) {
276 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_subsystem_list)) {
277 unsigned int hash = util_string_hash32(udev_list_entry_get_name(list_entry));
279 /* load device subsystem value in A */
280 bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_subsystem_hash));
281 if (udev_list_entry_get_value(list_entry) == NULL) {
282 /* jump if subsystem does not match */
283 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 1);
285 /* jump if subsystem does not match */
286 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 3);
288 /* load device devtype value in A */
289 bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_devtype_hash));
290 /* jump if value does not match */
291 hash = util_string_hash32(udev_list_entry_get_value(list_entry));
292 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 1);
295 /* matched, pass packet */
296 bpf_stmt(ins, &i, BPF_RET|BPF_K, 0xffffffff);
298 if (i+1 >= ELEMENTSOF(ins))
302 /* nothing matched, drop packet */
303 bpf_stmt(ins, &i, BPF_RET|BPF_K, 0);
306 /* matched, pass packet */
307 bpf_stmt(ins, &i, BPF_RET|BPF_K, 0xffffffff);
310 memset(&filter, 0x00, sizeof(filter));
313 err = setsockopt(udev_monitor->sock, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter));
314 return err < 0 ? -errno : 0;
317 int udev_monitor_allow_unicast_sender(struct udev_monitor *udev_monitor, struct udev_monitor *sender)
319 udev_monitor->snl_trusted_sender.nl.nl_pid = sender->snl.nl.nl_pid;
323 * udev_monitor_enable_receiving:
324 * @udev_monitor: the monitor which should receive events
326 * Binds the @udev_monitor socket to the event source.
328 * Returns: 0 on success, otherwise a negative error value.
330 _public_ int udev_monitor_enable_receiving(struct udev_monitor *udev_monitor)
335 udev_monitor_filter_update(udev_monitor);
337 if (!udev_monitor->bound) {
338 err = bind(udev_monitor->sock,
339 &udev_monitor->snl.sa, sizeof(struct sockaddr_nl));
341 udev_monitor->bound = true;
345 union sockaddr_union 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, &snl.sa, &addrlen);
355 udev_monitor->snl.nl.nl_pid = snl.nl.nl_pid;
357 udev_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;
389 return err < 0 ? -errno : 0;
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
418 _public_ struct udev_monitor *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);
434 * udev_monitor_get_udev:
435 * @udev_monitor: udev monitor
437 * Retrieve the udev library context the monitor was created with.
439 * Returns: the udev library context
441 _public_ struct udev *udev_monitor_get_udev(struct udev_monitor *udev_monitor)
443 if (udev_monitor == NULL)
445 return udev_monitor->udev;
449 * udev_monitor_get_fd:
450 * @udev_monitor: udev monitor
452 * Retrieve the socket file descriptor associated with the monitor.
454 * Returns: the socket file descriptor
456 _public_ int udev_monitor_get_fd(struct udev_monitor *udev_monitor)
458 if (udev_monitor == NULL)
460 return udev_monitor->sock;
463 static int passes_filter(struct udev_monitor *udev_monitor, struct udev_device *udev_device)
465 struct udev_list_entry *list_entry;
467 if (udev_list_get_entry(&udev_monitor->filter_subsystem_list) == NULL)
469 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_subsystem_list)) {
470 const char *subsys = udev_list_entry_get_name(list_entry);
471 const char *dsubsys = udev_device_get_subsystem(udev_device);
473 const char *ddevtype;
475 if (!streq(dsubsys, subsys))
478 devtype = udev_list_entry_get_value(list_entry);
481 ddevtype = udev_device_get_devtype(udev_device);
482 if (ddevtype == NULL)
484 if (streq(ddevtype, devtype))
490 if (udev_list_get_entry(&udev_monitor->filter_tag_list) == NULL)
492 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_tag_list)) {
493 const char *tag = udev_list_entry_get_name(list_entry);
495 if (udev_device_has_tag(udev_device, tag))
502 * udev_monitor_receive_device:
503 * @udev_monitor: udev monitor
505 * Receive data from the udev monitor socket, allocate a new udev
506 * device, fill in the received data, and return the device.
508 * Only socket connections with uid=0 are accepted.
510 * The monitor socket is by default set to NONBLOCK. A variant of poll() on
511 * the file descriptor returned by udev_monitor_get_fd() should to be used to
512 * wake up when new devices arrive, or alternatively the file descriptor
513 * switched into blocking mode.
515 * The initial refcount is 1, and needs to be decremented to
516 * release the resources of the udev device.
518 * Returns: a new udev device, or #NULL, in case of an error
520 _public_ struct udev_device *udev_monitor_receive_device(struct udev_monitor *udev_monitor)
522 struct udev_device *udev_device;
525 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
526 struct cmsghdr *cmsg;
527 union sockaddr_union snl;
534 if (udev_monitor == NULL)
537 iov.iov_len = sizeof(buf);
538 memset (&smsg, 0x00, sizeof(struct msghdr));
541 smsg.msg_control = cred_msg;
542 smsg.msg_controllen = sizeof(cred_msg);
543 smsg.msg_name = &snl;
544 smsg.msg_namelen = sizeof(snl);
546 buflen = recvmsg(udev_monitor->sock, &smsg, 0);
549 udev_dbg(udev_monitor->udev, "unable to receive message\n");
553 if (buflen < 32 || (size_t)buflen >= sizeof(buf)) {
554 udev_dbg(udev_monitor->udev, "invalid message length\n");
558 if (snl.nl.nl_groups == 0) {
559 /* unicast message, check if we trust the sender */
560 if (udev_monitor->snl_trusted_sender.nl.nl_pid == 0 ||
561 snl.nl.nl_pid != udev_monitor->snl_trusted_sender.nl.nl_pid) {
562 udev_dbg(udev_monitor->udev, "unicast netlink message ignored\n");
565 } else if (snl.nl.nl_groups == UDEV_MONITOR_KERNEL) {
566 if (snl.nl.nl_pid > 0) {
567 udev_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 udev_dbg(udev_monitor->udev, "no sender credentials received, message ignored\n");
579 cred = (struct ucred *)CMSG_DATA(cmsg);
580 if (cred->uid != 0) {
581 udev_dbg(udev_monitor->udev, "sender uid=%d, message ignored\n", cred->uid);
585 udev_device = udev_device_new(udev_monitor->udev);
586 if (udev_device == NULL)
589 if (memcmp(buf, "libudev", 8) == 0) {
590 struct udev_monitor_netlink_header *nlh;
592 /* udev message needs proper version magic */
593 nlh = (struct udev_monitor_netlink_header *) buf;
594 if (nlh->magic != htonl(UDEV_MONITOR_MAGIC)) {
595 udev_err(udev_monitor->udev, "unrecognized message signature (%x != %x)\n",
596 nlh->magic, htonl(UDEV_MONITOR_MAGIC));
597 udev_device_unref(udev_device);
600 if (nlh->properties_off+32 > (size_t)buflen) {
601 udev_device_unref(udev_device);
605 bufpos = nlh->properties_off;
607 /* devices received from udev are always initialized */
608 udev_device_set_is_initialized(udev_device);
610 /* kernel message with header */
611 bufpos = strlen(buf) + 1;
612 if ((size_t)bufpos < sizeof("a@/d") || bufpos >= buflen) {
613 udev_dbg(udev_monitor->udev, "invalid message length\n");
614 udev_device_unref(udev_device);
618 /* check message header */
619 if (strstr(buf, "@/") == NULL) {
620 udev_dbg(udev_monitor->udev, "unrecognized message header\n");
621 udev_device_unref(udev_device);
626 udev_device_set_info_loaded(udev_device);
628 while (bufpos < buflen) {
633 keylen = strlen(key);
636 bufpos += keylen + 1;
637 udev_device_add_property_from_string_parse(udev_device, key);
640 if (udev_device_add_property_from_string_parse_finish(udev_device) < 0) {
641 udev_dbg(udev_monitor->udev, "missing values, invalid device\n");
642 udev_device_unref(udev_device);
646 /* skip device, if it does not pass the current filter */
647 if (!passes_filter(udev_monitor, udev_device)) {
648 struct pollfd pfd[1];
651 udev_device_unref(udev_device);
653 /* if something is queued, get next device */
654 pfd[0].fd = udev_monitor->sock;
655 pfd[0].events = POLLIN;
656 rc = poll(pfd, 1, 0);
665 int udev_monitor_send_device(struct udev_monitor *udev_monitor,
666 struct udev_monitor *destination, struct udev_device *udev_device)
674 struct udev_monitor_netlink_header nlh;
675 struct udev_list_entry *list_entry;
676 uint64_t tag_bloom_bits;
678 blen = udev_device_get_properties_monitor_buf(udev_device, &buf);
682 /* add versioned header */
683 memset(&nlh, 0x00, sizeof(struct udev_monitor_netlink_header));
684 memcpy(nlh.prefix, "libudev", 8);
685 nlh.magic = htonl(UDEV_MONITOR_MAGIC);
686 nlh.header_size = sizeof(struct udev_monitor_netlink_header);
687 val = udev_device_get_subsystem(udev_device);
688 nlh.filter_subsystem_hash = htonl(util_string_hash32(val));
689 val = udev_device_get_devtype(udev_device);
691 nlh.filter_devtype_hash = htonl(util_string_hash32(val));
692 iov[0].iov_base = &nlh;
693 iov[0].iov_len = sizeof(struct udev_monitor_netlink_header);
695 /* add tag bloom filter */
697 udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(udev_device))
698 tag_bloom_bits |= util_string_bloom64(udev_list_entry_get_name(list_entry));
699 if (tag_bloom_bits > 0) {
700 nlh.filter_tag_bloom_hi = htonl(tag_bloom_bits >> 32);
701 nlh.filter_tag_bloom_lo = htonl(tag_bloom_bits & 0xffffffff);
704 /* add properties list */
705 nlh.properties_off = iov[0].iov_len;
706 nlh.properties_len = blen;
707 iov[1].iov_base = (char *)buf;
708 iov[1].iov_len = blen;
710 memset(&smsg, 0x00, sizeof(struct msghdr));
714 * Use custom address for target, or the default one.
716 * If we send to a multicast group, we will get
717 * ECONNREFUSED, which is expected.
719 if (destination != NULL)
720 smsg.msg_name = &destination->snl;
722 smsg.msg_name = &udev_monitor->snl_destination;
723 smsg.msg_namelen = sizeof(struct sockaddr_nl);
724 count = sendmsg(udev_monitor->sock, &smsg, 0);
725 udev_dbg(udev_monitor->udev, "passed %zi bytes to netlink monitor %p\n", count, udev_monitor);
730 * udev_monitor_filter_add_match_subsystem_devtype:
731 * @udev_monitor: the monitor
732 * @subsystem: the subsystem value to match the incoming devices against
733 * @devtype: the devtype value to match the incoming devices against
735 * This filter is efficiently executed inside the kernel, and libudev subscribers
736 * will usually not be woken up for devices which do not match.
738 * The filter must be installed before the monitor is switched to listening mode.
740 * Returns: 0 on success, otherwise a negative error value.
742 _public_ int udev_monitor_filter_add_match_subsystem_devtype(struct udev_monitor *udev_monitor, const char *subsystem, const char *devtype)
744 if (udev_monitor == NULL)
746 if (subsystem == NULL)
748 if (udev_list_entry_add(&udev_monitor->filter_subsystem_list, subsystem, devtype) == NULL)
754 * udev_monitor_filter_add_match_tag:
755 * @udev_monitor: the monitor
756 * @tag: the name of a tag
758 * This filter is efficiently executed inside the kernel, and libudev subscribers
759 * will usually not be woken up for devices which do not match.
761 * The filter must be installed before the monitor is switched to listening mode.
763 * Returns: 0 on success, otherwise a negative error value.
765 _public_ int udev_monitor_filter_add_match_tag(struct udev_monitor *udev_monitor, const char *tag)
767 if (udev_monitor == NULL)
771 if (udev_list_entry_add(&udev_monitor->filter_tag_list, tag, NULL) == NULL)
777 * udev_monitor_filter_remove:
778 * @udev_monitor: monitor
780 * Remove all filters from monitor.
782 * Returns: 0 on success, otherwise a negative error value.
784 _public_ int udev_monitor_filter_remove(struct udev_monitor *udev_monitor)
786 static struct sock_fprog filter = { 0, NULL };
788 udev_list_cleanup(&udev_monitor->filter_subsystem_list);
789 return setsockopt(udev_monitor->sock, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter));