1 /* -*- Mode: C; c-basic-offset:8 ; indent-tabs-mode:t -*- */
3 * Linux usbfs backend for libusb
4 * Copyright (C) 2007-2009 Daniel Drake <dsd@gentoo.org>
5 * Copyright (c) 2001 Johannes Erdfelt <johannes@erdfelt.com>
6 * Copyright (c) 2013 Nathan Hjelm <hjelmn@mac.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <arpa/inet.h>
38 #include "linux_usbfs.h"
40 #include <linux/netlink.h>
41 #include <linux/filter.h>
45 static int linux_netlink_socket = -1;
46 static pthread_t libusb_linux_event_thread;
48 static void *linux_netlink_event_thread_main(void *arg);
50 struct sockaddr_nl snl = { .nl_family=AF_NETLINK, .nl_groups=KERNEL };
52 int linux_netlink_start_event_monitor(void)
56 snl.nl_groups = KERNEL;
58 linux_netlink_socket = socket(PF_NETLINK, SOCK_RAW|SOCK_CLOEXEC|SOCK_NONBLOCK, NETLINK_KOBJECT_UEVENT);
59 if (-1 == linux_netlink_socket) {
60 return LIBUSB_ERROR_OTHER;
63 ret = bind(linux_netlink_socket, (struct sockaddr *) &snl, sizeof(snl));
65 return LIBUSB_ERROR_OTHER;
68 /* TODO -- add authentication */
69 /* setsockopt(linux_netlink_socket, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)); */
71 ret = pthread_create(&libusb_linux_event_thread, NULL, linux_netlink_event_thread_main, NULL);
73 return LIBUSB_ERROR_OTHER;
76 return LIBUSB_SUCCESS;
79 int linux_netlink_stop_event_monitor(void)
83 if (-1 == linux_netlink_socket) {
84 /* already closed. nothing to do */
85 return LIBUSB_SUCCESS;
88 r = close(linux_netlink_socket);
90 usbi_err(NULL, "error closing netlink socket. %s", strerror(errno));
91 return LIBUSB_ERROR_OTHER;
94 pthread_cancel(libusb_linux_event_thread);
96 linux_netlink_socket = -1;
98 return LIBUSB_SUCCESS;
101 static const char *netlink_message_parse (const char *buffer, size_t len, const char *key)
103 size_t keylen = strlen(key);
106 for (offset = 0 ; offset < len && '\0' != buffer[offset] ; offset += strlen(buffer + offset) + 1) {
107 if (0 == strncmp(buffer + offset, key, keylen) &&
108 '=' == buffer[offset + keylen]) {
109 return buffer + offset + keylen + 1;
116 /* parse parts of netlink message common to both libudev and the kernel */
117 static int linux_netlink_parse(char *buffer, size_t len, int *detached, const char **sys_name,
118 uint8_t *busnum, uint8_t *devaddr) {
129 tmp = netlink_message_parse((const char *) buffer, len, "ACTION");
130 if (0 == strcmp(tmp, "remove")) {
132 } else if (0 != strcmp(tmp, "add")) {
133 usbi_dbg("unknown device action");
137 /* check that this is a usb message */
138 tmp = netlink_message_parse(buffer, len, "SUBSYSTEM");
139 if (NULL == tmp || 0 != strcmp(tmp, "usb")) {
140 /* not usb. ignore */
144 tmp = netlink_message_parse(buffer, len, "BUSNUM");
146 /* no bus number (likely a usb interface). ignore*/
150 *busnum = (uint8_t)(strtoul(tmp, NULL, 10) & 0xff);
156 tmp = netlink_message_parse(buffer, len, "DEVNUM");
161 *devaddr = (uint8_t)(strtoul(tmp, NULL, 10) & 0xff);
167 tmp = netlink_message_parse(buffer, len, "DEVPATH");
172 for (i = strlen(tmp) - 1 ; i ; --i) {
174 *sys_name = tmp + i + 1;
179 /* found a usb device */
183 static void *linux_netlink_event_thread_main(void *arg)
185 struct pollfd fds = {.fd = linux_netlink_socket,
187 unsigned char buffer[1024];
188 struct iovec iov = {.iov_base = buffer, .iov_len = sizeof(buffer)};
189 struct msghdr meh = { .msg_iov=&iov, .msg_iovlen=1,
190 .msg_name=&snl, .msg_namelen=sizeof(snl) };
191 uint8_t busnum, devaddr;
195 /* silence compiler warning */
198 while (1 == poll(&fds, 1, -1)) {
199 const char *sys_name = NULL;
201 if (POLLIN != fds.revents) {
205 /* read netlink message */
206 memset(buffer, 0, sizeof(buffer));
207 len = recvmsg(linux_netlink_socket, &meh, 0);
209 usbi_dbg("error recieving message from netlink");
213 /* TODO -- authenticate this message is from the kernel or udevd */
215 r = linux_netlink_parse(buffer, len, &detached, &sys_name,
220 usbi_dbg("netlink hotplug found device busnum: %hhu, devaddr: %hhu, sys_name: %s, removed: %s",
221 busnum, devaddr, sys_name, detached ? "yes" : "no");
223 /* signal device is available (or not) to all contexts */
225 linux_hotplug_disconnected(busnum, devaddr, sys_name);
227 linux_hotplug_enumerate(busnum, devaddr, sys_name);