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 #include <sys/socket.h>
40 #include <linux/netlink.h>
43 #include "linux_usbfs.h"
47 static int linux_netlink_socket = -1;
48 static int netlink_control_pipe[2] = { -1, -1 };
49 static pthread_t libusb_linux_event_thread;
51 static void *linux_netlink_event_thread_main(void *arg);
53 static struct sockaddr_nl snl = { .nl_family = AF_NETLINK, .nl_groups = KERNEL };
55 static int set_fd_cloexec_nb(int fd)
59 #if defined(FD_CLOEXEC)
60 flags = fcntl(fd, F_GETFD);
62 usbi_err(NULL, "failed to get netlink fd flags (%d)", errno);
66 if (!(flags & FD_CLOEXEC)) {
67 if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1) {
68 usbi_err(NULL, "failed to set netlink fd flags (%d)", errno);
74 flags = fcntl(fd, F_GETFL);
76 usbi_err(NULL, "failed to get netlink fd status flags (%d)", errno);
80 if (!(flags & O_NONBLOCK)) {
81 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) {
82 usbi_err(NULL, "failed to set netlink fd status flags (%d)", errno);
90 int linux_netlink_start_event_monitor(void)
92 int socktype = SOCK_RAW;
95 #if defined(SOCK_CLOEXEC)
96 socktype |= SOCK_CLOEXEC;
98 #if defined(SOCK_NONBLOCK)
99 socktype |= SOCK_NONBLOCK;
102 linux_netlink_socket = socket(PF_NETLINK, socktype, NETLINK_KOBJECT_UEVENT);
103 if (linux_netlink_socket == -1 && errno == EINVAL) {
104 usbi_dbg("failed to create netlink socket of type %d, attempting SOCK_RAW", socktype);
105 linux_netlink_socket = socket(PF_NETLINK, SOCK_RAW, NETLINK_KOBJECT_UEVENT);
108 if (linux_netlink_socket == -1) {
109 usbi_err(NULL, "failed to create netlink socket (%d)", errno);
113 ret = set_fd_cloexec_nb(linux_netlink_socket);
115 goto err_close_socket;
117 ret = bind(linux_netlink_socket, (struct sockaddr *)&snl, sizeof(snl));
119 usbi_err(NULL, "failed to bind netlink socket (%d)", errno);
120 goto err_close_socket;
123 /* TODO -- add authentication */
124 /* setsockopt(linux_netlink_socket, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)); */
126 ret = usbi_pipe(netlink_control_pipe);
128 usbi_err(NULL, "failed to create netlink control pipe");
129 goto err_close_socket;
132 ret = pthread_create(&libusb_linux_event_thread, NULL, linux_netlink_event_thread_main, NULL);
134 usbi_err(NULL, "failed to create netlink event thread (%d)", ret);
138 return LIBUSB_SUCCESS;
141 close(netlink_control_pipe[0]);
142 close(netlink_control_pipe[1]);
143 netlink_control_pipe[0] = -1;
144 netlink_control_pipe[1] = -1;
146 close(linux_netlink_socket);
147 linux_netlink_socket = -1;
149 return LIBUSB_ERROR_OTHER;
152 int linux_netlink_stop_event_monitor(void)
157 assert(linux_netlink_socket != -1);
159 /* Write some dummy data to the control pipe and
160 * wait for the thread to exit */
161 r = usbi_write(netlink_control_pipe[1], &dummy, sizeof(dummy));
163 usbi_warn(NULL, "netlink control pipe signal failed");
165 pthread_join(libusb_linux_event_thread, NULL);
167 close(linux_netlink_socket);
168 linux_netlink_socket = -1;
170 /* close and reset control pipe */
171 close(netlink_control_pipe[0]);
172 close(netlink_control_pipe[1]);
173 netlink_control_pipe[0] = -1;
174 netlink_control_pipe[1] = -1;
176 return LIBUSB_SUCCESS;
179 static const char *netlink_message_parse(const char *buffer, size_t len, const char *key)
181 size_t keylen = strlen(key);
184 for (offset = 0; offset < len && buffer[offset] != '\0'; offset += strlen(buffer + offset) + 1) {
185 if (strncmp(buffer + offset, key, keylen) == 0 &&
186 buffer[offset + keylen] == '=') {
187 return buffer + offset + keylen + 1;
194 /* parse parts of netlink message common to both libudev and the kernel */
195 static int linux_netlink_parse(const char *buffer, size_t len, int *detached,
196 const char **sys_name, uint8_t *busnum, uint8_t *devaddr)
208 tmp = netlink_message_parse(buffer, len, "ACTION");
211 } else if (strcmp(tmp, "remove") == 0) {
213 } else if (strcmp(tmp, "add") != 0) {
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 (!tmp || strcmp(tmp, "usb") != 0) {
221 /* not usb. ignore */
225 /* check that this is an actual usb device */
226 tmp = netlink_message_parse(buffer, len, "DEVTYPE");
227 if (!tmp || strcmp(tmp, "usb_device") != 0) {
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 const char *pLastSlash = strrchr(tmp, '/');
246 *busnum = (uint8_t)(strtoul(pLastSlash - 3, NULL, 10) & 0xff);
252 *devaddr = (uint8_t)(strtoul(pLastSlash + 1, NULL, 10) & 0xff);
261 *busnum = (uint8_t)(strtoul(tmp, NULL, 10) & 0xff);
267 tmp = netlink_message_parse(buffer, len, "DEVNUM");
271 *devaddr = (uint8_t)(strtoul(tmp, NULL, 10) & 0xff);
277 tmp = netlink_message_parse(buffer, len, "DEVPATH");
281 for (i = strlen(tmp) - 1; i; --i) {
283 *sys_name = tmp + i + 1;
288 /* found a usb device */
292 static int linux_netlink_read_message(void)
295 const char *sys_name = NULL;
296 uint8_t busnum, devaddr;
299 struct iovec iov = { .iov_base = buffer, .iov_len = sizeof(buffer) };
300 struct msghdr meh = {
301 .msg_iov = &iov, .msg_iovlen = 1,
302 .msg_name = &snl, .msg_namelen = sizeof(snl)
305 /* read netlink message */
306 memset(buffer, 0, sizeof(buffer));
307 len = recvmsg(linux_netlink_socket, &meh, 0);
310 usbi_dbg("error receiving message from netlink");
314 /* TODO -- authenticate this message is from the kernel or udevd */
316 r = linux_netlink_parse(buffer, (size_t)len, &detached, &sys_name, &busnum, &devaddr);
320 usbi_dbg("netlink hotplug found device busnum: %hhu, devaddr: %hhu, sys_name: %s, removed: %s",
321 busnum, devaddr, sys_name, detached ? "yes" : "no");
323 /* signal device is available (or not) to all contexts */
325 linux_device_disconnected(busnum, devaddr);
327 linux_hotplug_enumerate(busnum, devaddr, sys_name);
332 static void *linux_netlink_event_thread_main(void *arg)
336 struct pollfd fds[] = {
337 { .fd = netlink_control_pipe[0],
339 { .fd = linux_netlink_socket,
345 usbi_dbg("netlink event thread entering");
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");
355 if (fds[1].revents & POLLIN) {
356 usbi_mutex_static_lock(&linux_hotplug_lock);
357 linux_netlink_read_message();
358 usbi_mutex_static_unlock(&linux_hotplug_lock);
362 usbi_dbg("netlink event thread exiting");
367 void linux_netlink_hotplug_poll(void)
371 usbi_mutex_static_lock(&linux_hotplug_lock);
373 r = linux_netlink_read_message();
375 usbi_mutex_static_unlock(&linux_hotplug_lock);