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
33 #include <sys/types.h>
35 #ifdef HAVE_ASM_TYPES_H
36 #include <asm/types.h>
39 #ifdef HAVE_SYS_SOCKET_H
40 #include <sys/socket.h>
43 #include <arpa/inet.h>
45 #ifdef HAVE_LINUX_NETLINK_H
46 #include <linux/netlink.h>
49 #ifdef HAVE_LINUX_FILTER_H
50 #include <linux/filter.h>
54 #include "linux_usbfs.h"
58 static int linux_netlink_socket = -1;
59 static int netlink_control_pipe[2] = { -1, -1 };
60 static pthread_t libusb_linux_event_thread;
62 static void *linux_netlink_event_thread_main(void *arg);
64 static struct sockaddr_nl snl = { .nl_family=AF_NETLINK, .nl_groups=KERNEL };
66 static int set_fd_cloexec_nb(int fd)
70 #if defined(FD_CLOEXEC)
71 flags = fcntl(fd, F_GETFD);
76 if (!(flags & FD_CLOEXEC)) {
77 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
81 flags = fcntl(fd, F_GETFL);
86 if (!(flags & O_NONBLOCK)) {
87 fcntl(fd, F_SETFL, flags | O_NONBLOCK);
93 int linux_netlink_start_event_monitor(void)
95 int socktype = SOCK_RAW;
98 snl.nl_groups = KERNEL;
100 #if defined(SOCK_CLOEXEC)
101 socktype |= SOCK_CLOEXEC;
103 #if defined(SOCK_NONBLOCK)
104 socktype |= SOCK_NONBLOCK;
107 linux_netlink_socket = socket(PF_NETLINK, socktype, NETLINK_KOBJECT_UEVENT);
108 if (-1 == linux_netlink_socket && EINVAL == errno) {
109 linux_netlink_socket = socket(PF_NETLINK, SOCK_RAW, NETLINK_KOBJECT_UEVENT);
112 if (-1 == linux_netlink_socket) {
113 return LIBUSB_ERROR_OTHER;
116 ret = set_fd_cloexec_nb (linux_netlink_socket);
118 close (linux_netlink_socket);
119 linux_netlink_socket = -1;
120 return LIBUSB_ERROR_OTHER;
123 ret = bind(linux_netlink_socket, (struct sockaddr *) &snl, sizeof(snl));
125 close(linux_netlink_socket);
126 return LIBUSB_ERROR_OTHER;
129 /* TODO -- add authentication */
130 /* setsockopt(linux_netlink_socket, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)); */
132 ret = usbi_pipe(netlink_control_pipe);
134 usbi_err(NULL, "could not create netlink control pipe");
135 close(linux_netlink_socket);
136 return LIBUSB_ERROR_OTHER;
139 ret = pthread_create(&libusb_linux_event_thread, NULL, linux_netlink_event_thread_main, NULL);
141 close(netlink_control_pipe[0]);
142 close(netlink_control_pipe[1]);
143 close(linux_netlink_socket);
144 return LIBUSB_ERROR_OTHER;
147 return LIBUSB_SUCCESS;
150 int linux_netlink_stop_event_monitor(void)
155 if (-1 == linux_netlink_socket) {
156 /* already closed. nothing to do */
157 return LIBUSB_SUCCESS;
160 /* Write some dummy data to the control pipe and
161 * wait for the thread to exit */
162 r = usbi_write(netlink_control_pipe[1], &dummy, sizeof(dummy));
164 usbi_warn(NULL, "netlink control pipe signal failed");
166 pthread_join(libusb_linux_event_thread, NULL);
168 close(linux_netlink_socket);
169 linux_netlink_socket = -1;
171 /* close and reset control pipe */
172 close(netlink_control_pipe[0]);
173 close(netlink_control_pipe[1]);
174 netlink_control_pipe[0] = -1;
175 netlink_control_pipe[1] = -1;
177 return LIBUSB_SUCCESS;
180 static const char *netlink_message_parse (const char *buffer, size_t len, const char *key)
182 size_t keylen = strlen(key);
185 for (offset = 0 ; offset < len && '\0' != buffer[offset] ; offset += strlen(buffer + offset) + 1) {
186 if (0 == strncmp(buffer + offset, key, keylen) &&
187 '=' == buffer[offset + keylen]) {
188 return buffer + offset + keylen + 1;
195 /* parse parts of netlink message common to both libudev and the kernel */
196 static int linux_netlink_parse(char *buffer, size_t len, int *detached, const char **sys_name,
197 uint8_t *busnum, uint8_t *devaddr) {
208 tmp = netlink_message_parse((const char *) buffer, len, "ACTION");
211 if (0 == strcmp(tmp, "remove")) {
213 } else if (0 != strcmp(tmp, "add")) {
214 usbi_dbg("unknown device action %s", tmp);
218 /* check that this is a usb message */
219 tmp = netlink_message_parse(buffer, len, "SUBSYSTEM");
220 if (NULL == tmp || 0 != strcmp(tmp, "usb")) {
221 /* not usb. ignore */
225 /* check that this is an actual usb device */
226 tmp = netlink_message_parse(buffer, len, "DEVTYPE");
227 if (NULL == tmp || 0 != strcmp(tmp, "usb_device")) {
228 /* not usb. ignore */
232 tmp = netlink_message_parse(buffer, len, "BUSNUM");
234 /* no bus number. try "DEVICE" */
235 tmp = netlink_message_parse(buffer, len, "DEVICE");
237 /* not usb. ignore */
241 /* Parse a device path such as /dev/bus/usb/003/004 */
242 char *pLastSlash = (char*)strrchr(tmp,'/');
243 if(NULL == pLastSlash) {
247 *devaddr = strtoul(pLastSlash + 1, NULL, 10);
253 *busnum = strtoul(pLastSlash - 3, NULL, 10);
262 *busnum = (uint8_t)(strtoul(tmp, NULL, 10) & 0xff);
268 tmp = netlink_message_parse(buffer, len, "DEVNUM");
273 *devaddr = (uint8_t)(strtoul(tmp, NULL, 10) & 0xff);
279 tmp = netlink_message_parse(buffer, len, "DEVPATH");
284 for (i = strlen(tmp) - 1 ; i ; --i) {
286 *sys_name = tmp + i + 1;
291 /* found a usb device */
295 static int linux_netlink_read_message(void)
297 unsigned char buffer[1024];
298 struct iovec iov = {.iov_base = buffer, .iov_len = sizeof(buffer)};
299 struct msghdr meh = { .msg_iov=&iov, .msg_iovlen=1,
300 .msg_name=&snl, .msg_namelen=sizeof(snl) };
301 const char *sys_name = NULL;
302 uint8_t busnum, devaddr;
306 /* read netlink message */
307 memset(buffer, 0, sizeof(buffer));
308 len = recvmsg(linux_netlink_socket, &meh, 0);
311 usbi_dbg("error recieving message from netlink");
315 /* TODO -- authenticate this message is from the kernel or udevd */
317 r = linux_netlink_parse(buffer, len, &detached, &sys_name,
322 usbi_dbg("netlink hotplug found device busnum: %hhu, devaddr: %hhu, sys_name: %s, removed: %s",
323 busnum, devaddr, sys_name, detached ? "yes" : "no");
325 /* signal device is available (or not) to all contexts */
327 linux_device_disconnected(busnum, devaddr);
329 linux_hotplug_enumerate(busnum, devaddr, sys_name);
334 static void *linux_netlink_event_thread_main(void *arg)
338 struct pollfd fds[] = {
339 { .fd = netlink_control_pipe[0],
341 { .fd = linux_netlink_socket,
347 while (poll(fds, 2, -1) >= 0) {
348 if (fds[0].revents & POLLIN) {
349 /* activity on control pipe, read the byte and exit */
350 r = usbi_read(netlink_control_pipe[0], &dummy, sizeof(dummy));
352 usbi_warn(NULL, "netlink control pipe read failed");
356 if (fds[1].revents & POLLIN) {
357 usbi_mutex_static_lock(&linux_hotplug_lock);
358 linux_netlink_read_message();
359 usbi_mutex_static_unlock(&linux_hotplug_lock);
366 void linux_netlink_hotplug_poll(void)
370 usbi_mutex_static_lock(&linux_hotplug_lock);
372 r = linux_netlink_read_message();
374 usbi_mutex_static_unlock(&linux_hotplug_lock);