2 * libudev - interface to udev device information
4 * Copyright (C) 2008-2009 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 one event source.
46 struct sockaddr_nl snl;
47 struct sockaddr_nl snl_trusted_sender;
48 struct sockaddr_nl snl_destination;
49 struct sockaddr_un sun;
51 struct udev_list_node filter_subsystem_list;
54 enum udev_monitor_netlink_group {
60 #define UDEV_MONITOR_MAGIC 0xcafe1dea
61 struct udev_monitor_netlink_header {
62 /* udev version text */
65 * magic to protect against daemon <-> library message format mismatch
66 * used in the kernel from socket filter rules; needs to be stored in network order
69 /* properties buffer */
70 unsigned short properties_off;
71 unsigned short properties_len;
73 * hashes of some common device properties strings to filter with socket filters in
74 * the client used in the kernel from socket filter rules; needs to be stored in
77 unsigned int filter_subsystem;
78 unsigned int filter_devtype;
81 static struct udev_monitor *udev_monitor_new(struct udev *udev)
83 struct udev_monitor *udev_monitor;
85 udev_monitor = calloc(1, sizeof(struct udev_monitor));
86 if (udev_monitor == NULL)
88 udev_monitor->refcount = 1;
89 udev_monitor->udev = udev;
90 udev_list_init(&udev_monitor->filter_subsystem_list);
95 * udev_monitor_new_from_socket:
96 * @udev: udev library context
97 * @socket_path: unix socket path
99 * Create new udev monitor and connect to a specified socket. The
100 * path to a socket either points to an existing socket file, or if
101 * the socket path starts with a '@' character, an abstract namespace
102 * socket will be used.
104 * A socket file will not be created. If it does not already exist,
105 * it will fall-back and connect to an abstract namespace socket with
106 * the given path. The permissions adjustment of a socket file, as
107 * well as the later cleanup, needs to be done by the caller.
109 * The initial refcount is 1, and needs to be decremented to
110 * release the resources of the udev monitor.
112 * Returns: a new udev monitor, or #NULL, in case of an error
114 struct udev_monitor *udev_monitor_new_from_socket(struct udev *udev, const char *socket_path)
116 struct udev_monitor *udev_monitor;
121 if (socket_path == NULL)
123 udev_monitor = udev_monitor_new(udev);
124 if (udev_monitor == NULL)
127 udev_monitor->sun.sun_family = AF_LOCAL;
128 if (socket_path[0] == '@') {
129 /* translate leading '@' to abstract namespace */
130 util_strscpy(udev_monitor->sun.sun_path, sizeof(udev_monitor->sun.sun_path), socket_path);
131 udev_monitor->sun.sun_path[0] = '\0';
132 udev_monitor->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(socket_path);
133 } else if (stat(socket_path, &statbuf) == 0 && S_ISSOCK(statbuf.st_mode)) {
134 /* existing socket file */
135 util_strscpy(udev_monitor->sun.sun_path, sizeof(udev_monitor->sun.sun_path), socket_path);
136 udev_monitor->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(socket_path);
138 /* no socket file, assume abstract namespace socket */
139 util_strscpy(&udev_monitor->sun.sun_path[1], sizeof(udev_monitor->sun.sun_path)-1, socket_path);
140 udev_monitor->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(socket_path)+1;
142 udev_monitor->sock = socket(AF_LOCAL, SOCK_DGRAM|SOCK_CLOEXEC, 0);
143 if (udev_monitor->sock == -1) {
144 err(udev, "error getting socket: %m\n");
149 dbg(udev, "monitor %p created with '%s'\n", udev_monitor, socket_path);
154 * udev_monitor_new_from_netlink:
155 * @udev: udev library context
156 * @name: name of event source
158 * Create new udev monitor and connect to a specified event
159 * source. Valid sources identifiers are "udev" and "kernel".
161 * Applications should usually not connect directly to the
162 * "kernel" events, because the devices might not be useable
163 * at that time, before udev has configured them, and created
166 * Accessing devices at the same time as udev, might result
167 * in unpredictable behavior.
169 * The "udev" events are sent out after udev has finished its
170 * event processing, all rules have been processed, and needed
171 * device nodes are created.
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 struct udev_monitor *udev_monitor_new_from_netlink(struct udev *udev, const char *name)
180 struct udev_monitor *udev_monitor;
187 group = UDEV_MONITOR_NONE;
188 else if (strcmp(name, "udev") == 0)
189 group = UDEV_MONITOR_UDEV;
190 else if (strcmp(name, "kernel") == 0)
191 group = UDEV_MONITOR_KERNEL;
195 udev_monitor = udev_monitor_new(udev);
196 if (udev_monitor == NULL)
199 udev_monitor->sock = socket(PF_NETLINK, SOCK_DGRAM|SOCK_CLOEXEC, NETLINK_KOBJECT_UEVENT);
200 if (udev_monitor->sock == -1) {
201 err(udev, "error getting socket: %m\n");
206 udev_monitor->snl.nl_family = AF_NETLINK;
207 udev_monitor->snl.nl_groups = group;
209 /* default destination for sending */
210 udev_monitor->snl_destination.nl_family = AF_NETLINK;
211 udev_monitor->snl_destination.nl_groups = UDEV_MONITOR_UDEV;
213 dbg(udev, "monitor %p created with NETLINK_KOBJECT_UEVENT (%u)\n", udev_monitor, group);
217 static inline void bpf_stmt(struct sock_filter *inss, unsigned int *i,
218 unsigned short code, unsigned int data)
220 struct sock_filter *ins = &inss[*i];
227 static inline void bpf_jmp(struct sock_filter *inss, unsigned int *i,
228 unsigned short code, unsigned int data,
229 unsigned short jt, unsigned short jf)
231 struct sock_filter *ins = &inss[*i];
241 * udev_monitor_filter_update:
242 * @udev_monitor: monitor
244 * Update the installed filter. This might only be needed, if the filter was removed or changed.
246 * Returns: 0 on success, otherwise a negative error value.
248 int udev_monitor_filter_update(struct udev_monitor *udev_monitor)
250 static struct sock_filter ins[256];
251 static struct sock_fprog filter;
253 struct udev_list_entry *list_entry;
256 if (udev_list_get_entry(&udev_monitor->filter_subsystem_list) == NULL)
259 memset(ins, 0x00, sizeof(ins));
262 /* load magic in A */
263 bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, magic));
264 /* jump if magic matches */
265 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, UDEV_MONITOR_MAGIC, 1, 0);
266 /* wrong magic, pass packet */
267 bpf_stmt(ins, &i, BPF_RET|BPF_K, 0xffffffff);
269 /* add all subsystem match values */
270 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_subsystem_list)) {
273 /* load filter_subsystem value in A */
274 bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_subsystem));
275 hash = util_string_hash32(udev_list_entry_get_name(list_entry));
276 if (udev_list_entry_get_value(list_entry) == NULL) {
277 /* jump if subsystem does not match */
278 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 1);
280 /* jump if subsystem does not match */
281 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 3);
283 /* load filter_devtype value in A */
284 bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_devtype));
285 /* jump if value does not match */
286 hash = util_string_hash32(udev_list_entry_get_value(list_entry));
287 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 1);
290 /* matched, pass packet */
291 bpf_stmt(ins, &i, BPF_RET|BPF_K, 0xffffffff);
293 if (i+1 >= ARRAY_SIZE(ins))
296 /* nothing matched, drop packet */
297 bpf_stmt(ins, &i, BPF_RET|BPF_K, 0);
302 err = setsockopt(udev_monitor->sock, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter));
306 int udev_monitor_allow_unicast_sender(struct udev_monitor *udev_monitor, struct udev_monitor *sender)
308 udev_monitor->snl_trusted_sender.nl_pid = sender->snl.nl_pid;
312 * udev_monitor_enable_receiving:
313 * @udev_monitor: the monitor which should receive events
315 * Binds the @udev_monitor socket to the event source.
317 * Returns: 0 on success, otherwise a negative error value.
319 int udev_monitor_enable_receiving(struct udev_monitor *udev_monitor)
324 if (udev_monitor->sun.sun_family != 0) {
325 err = bind(udev_monitor->sock,
326 (struct sockaddr *)&udev_monitor->sun, udev_monitor->addrlen);
327 } else if (udev_monitor->snl.nl_family != 0) {
328 udev_monitor_filter_update(udev_monitor);
329 err = bind(udev_monitor->sock,
330 (struct sockaddr *)&udev_monitor->snl, sizeof(struct sockaddr_nl));
332 struct sockaddr_nl 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, (struct sockaddr *)&snl, &addrlen);
342 udev_monitor->snl.nl_pid = snl.nl_pid;
349 err(udev_monitor->udev, "bind failed: %m\n");
353 /* enable receiving of sender credentials */
354 setsockopt(udev_monitor->sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
358 int udev_monitor_set_receive_buffer_size(struct udev_monitor *udev_monitor, int size)
360 if (udev_monitor == NULL)
362 return setsockopt(udev_monitor->sock, SOL_SOCKET, SO_RCVBUFFORCE, &size, sizeof(size));
365 int udev_monitor_disconnect(struct udev_monitor *udev_monitor)
369 err = close(udev_monitor->sock);
370 udev_monitor->sock = -1;
376 * @udev_monitor: udev monitor
378 * Take a reference of a udev monitor.
380 * Returns: the passed udev monitor
382 struct udev_monitor *udev_monitor_ref(struct udev_monitor *udev_monitor)
384 if (udev_monitor == NULL)
386 udev_monitor->refcount++;
391 * udev_monitor_unref:
392 * @udev_monitor: udev monitor
394 * Drop a reference of a udev monitor. If the refcount reaches zero,
395 * the bound socket will be closed, and the resources of the monitor
399 void udev_monitor_unref(struct udev_monitor *udev_monitor)
401 if (udev_monitor == NULL)
403 udev_monitor->refcount--;
404 if (udev_monitor->refcount > 0)
406 if (udev_monitor->sock >= 0)
407 close(udev_monitor->sock);
408 udev_list_cleanup_entries(udev_monitor->udev, &udev_monitor->filter_subsystem_list);
409 dbg(udev_monitor->udev, "monitor %p released\n", udev_monitor);
414 * udev_monitor_get_udev:
415 * @udev_monitor: udev monitor
417 * Retrieve the udev library context the monitor was created with.
419 * Returns: the udev library context
421 struct udev *udev_monitor_get_udev(struct udev_monitor *udev_monitor)
423 if (udev_monitor == NULL)
425 return udev_monitor->udev;
429 * udev_monitor_get_fd:
430 * @udev_monitor: udev monitor
432 * Retrieve the socket file descriptor associated with the monitor.
434 * Returns: the socket file descriptor
436 int udev_monitor_get_fd(struct udev_monitor *udev_monitor)
438 if (udev_monitor == NULL)
440 return udev_monitor->sock;
443 static int passes_filter(struct udev_monitor *udev_monitor, struct udev_device *udev_device)
445 struct udev_list_entry *list_entry;
447 if (udev_list_get_entry(&udev_monitor->filter_subsystem_list) == NULL)
450 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_subsystem_list)) {
451 const char *subsys = udev_list_entry_get_name(list_entry);
452 const char *dsubsys = udev_device_get_subsystem(udev_device);
454 const char *ddevtype;
456 if (strcmp(dsubsys, subsys) != 0)
459 devtype = udev_list_entry_get_value(list_entry);
462 ddevtype = udev_device_get_devtype(udev_device);
463 if (ddevtype == NULL)
465 if (strcmp(ddevtype, devtype) == 0)
472 * udev_monitor_receive_device:
473 * @udev_monitor: udev monitor
475 * Receive data from the udev monitor socket, allocate a new udev
476 * device, fill in the received data, and return the device.
478 * Only socket connections with uid=0 are accepted. The caller
479 * needs to make sure that there is data to read from the socket.
480 * The call will block until the socket becomes readable.
482 * The initial refcount is 1, and needs to be decremented to
483 * release the resources of the udev device.
485 * Returns: a new udev device, or #NULL, in case of an error
487 struct udev_device *udev_monitor_receive_device(struct udev_monitor *udev_monitor)
489 struct udev_device *udev_device;
492 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
493 struct cmsghdr *cmsg;
494 struct sockaddr_nl snl;
499 struct udev_monitor_netlink_header *nlh;
502 if (udev_monitor == NULL)
504 memset(buf, 0x00, sizeof(buf));
506 iov.iov_len = sizeof(buf);
507 memset (&smsg, 0x00, sizeof(struct msghdr));
510 smsg.msg_control = cred_msg;
511 smsg.msg_controllen = sizeof(cred_msg);
513 if (udev_monitor->snl.nl_family != 0) {
514 smsg.msg_name = &snl;
515 smsg.msg_namelen = sizeof(snl);
518 buflen = recvmsg(udev_monitor->sock, &smsg, 0);
521 info(udev_monitor->udev, "unable to receive message\n");
525 if (buflen < 32 || (size_t)buflen >= sizeof(buf)) {
526 info(udev_monitor->udev, "invalid message length\n");
530 if (udev_monitor->snl.nl_family != 0) {
531 if (snl.nl_groups == 0) {
532 /* unicast message, check if we trust the sender */
533 if (udev_monitor->snl_trusted_sender.nl_pid == 0 ||
534 snl.nl_pid != udev_monitor->snl_trusted_sender.nl_pid) {
535 info(udev_monitor->udev, "unicast netlink message ignored\n");
538 } else if (snl.nl_groups == UDEV_MONITOR_KERNEL) {
539 if (snl.nl_pid > 0) {
540 info(udev_monitor->udev, "multicast kernel netlink message from pid %d ignored\n",
547 cmsg = CMSG_FIRSTHDR(&smsg);
548 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
549 info(udev_monitor->udev, "no sender credentials received, message ignored\n");
553 cred = (struct ucred *)CMSG_DATA(cmsg);
554 if (cred->uid != 0) {
555 info(udev_monitor->udev, "sender uid=%d, message ignored\n", cred->uid);
559 if (strncmp(buf, "udev-", 5) == 0) {
560 /* udev message needs proper version magic */
561 nlh = (struct udev_monitor_netlink_header *) buf;
562 if (nlh->magic != htonl(UDEV_MONITOR_MAGIC))
564 if (nlh->properties_off < sizeof(struct udev_monitor_netlink_header))
566 if (nlh->properties_off+32 > buflen)
568 bufpos = nlh->properties_off;
570 /* kernel message with header */
571 bufpos = strlen(buf) + 1;
572 if ((size_t)bufpos < sizeof("a@/d") || bufpos >= buflen) {
573 info(udev_monitor->udev, "invalid message length\n");
577 /* check message header */
578 if (strstr(buf, "@/") == NULL) {
579 info(udev_monitor->udev, "unrecognized message header\n");
584 udev_device = udev_device_new(udev_monitor->udev);
585 if (udev_device == NULL)
587 udev_device_set_info_loaded(udev_device);
589 while (bufpos < buflen) {
594 keylen = strlen(key);
597 bufpos += keylen + 1;
598 udev_device_add_property_from_string_parse(udev_device, key);
601 if (udev_device_add_property_from_string_parse_finish(udev_device) < 0) {
602 info(udev_monitor->udev, "missing values, invalid device\n");
603 udev_device_unref(udev_device);
607 /* skip device, if it does not pass the current filter */
608 if (!passes_filter(udev_monitor, udev_device)) {
609 struct pollfd pfd[1];
612 udev_device_unref(udev_device);
614 /* if something is queued, get next device */
615 pfd[0].fd = udev_monitor->sock;
616 pfd[0].events = POLLIN;
617 rc = poll(pfd, 1, 0);
626 int udev_monitor_send_device(struct udev_monitor *udev_monitor,
627 struct udev_monitor *destination, struct udev_device *udev_device)
635 blen = udev_device_get_properties_monitor_buf(udev_device, &buf);
639 if (udev_monitor->sun.sun_family != 0) {
644 /* header <action>@<devpath> */
645 action = udev_device_get_action(udev_device);
649 if (util_strpcpyl(&s, sizeof(header), action, "@", udev_device_get_devpath(udev_device), NULL) == 0)
651 iov[0].iov_base = header;
652 iov[0].iov_len = (s - header)+1;
654 /* add properties list */
655 iov[1].iov_base = (char *)buf;
656 iov[1].iov_len = blen;
658 memset(&smsg, 0x00, sizeof(struct msghdr));
661 smsg.msg_name = &udev_monitor->sun;
662 smsg.msg_namelen = udev_monitor->addrlen;
663 } else if (udev_monitor->snl.nl_family != 0) {
665 struct udev_monitor_netlink_header nlh;
668 /* add versioned header */
669 memset(&nlh, 0x00, sizeof(struct udev_monitor_netlink_header));
670 util_strscpy(nlh.version, sizeof(nlh.version), "udev-" VERSION);
671 nlh.magic = htonl(UDEV_MONITOR_MAGIC);
672 val = udev_device_get_subsystem(udev_device);
673 nlh.filter_subsystem = htonl(util_string_hash32(val));
674 val = udev_device_get_devtype(udev_device);
676 nlh.filter_devtype = htonl(util_string_hash32(val));
677 iov[0].iov_base = &nlh;
678 iov[0].iov_len = sizeof(struct udev_monitor_netlink_header);
680 /* add properties list */
681 nlh.properties_off = iov[0].iov_len;
682 nlh.properties_len = blen;
683 iov[1].iov_base = (char *)buf;
684 iov[1].iov_len = blen;
686 memset(&smsg, 0x00, sizeof(struct msghdr));
690 * Use custom address for target, or the default one.
692 * If we send to a multicast group, we will get
693 * ECONNREFUSED, which is expected.
695 if (destination != NULL)
696 smsg.msg_name = &destination->snl;
698 smsg.msg_name = &udev_monitor->snl_destination;
699 smsg.msg_namelen = sizeof(struct sockaddr_nl);
704 count = sendmsg(udev_monitor->sock, &smsg, 0);
705 info(udev_monitor->udev, "passed %zi bytes to monitor %p\n", count, udev_monitor);
710 * udev_monitor_filter_add_match_subsystem_devtype:
711 * @udev_monitor: the monitor
712 * @subsystem: the subsystem value to match the incoming devices against
713 * @devtype: the devtype value to match the incoming devices against
715 * The filter must be installed before the monitor is switched to listening mode.
717 * Returns: 0 on success, otherwise a negative error value.
719 int udev_monitor_filter_add_match_subsystem_devtype(struct udev_monitor *udev_monitor, const char *subsystem, const char *devtype)
721 if (udev_monitor == NULL)
723 if (subsystem == NULL)
725 if (udev_list_entry_add(udev_monitor->udev,
726 &udev_monitor->filter_subsystem_list, subsystem, devtype, 0, 0) == NULL)
732 * udev_monitor_filter_remove:
733 * @udev_monitor: monitor
735 * Remove all filters from monitor.
737 * Returns: 0 on success, otherwise a negative error value.
739 int udev_monitor_filter_remove(struct udev_monitor *udev_monitor)
741 static struct sock_fprog filter = { 0, NULL };
743 udev_list_cleanup_entries(udev_monitor->udev, &udev_monitor->filter_subsystem_list);
744 return setsockopt(udev_monitor->sock, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter));