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>
7 * Copyright (c) 2016 Chris Dickens <christopher.a.dickens@gmail.com>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "linux_usbfs.h"
34 #ifdef HAVE_ASM_TYPES_H
35 #include <asm/types.h>
37 #include <sys/socket.h>
38 #include <linux/netlink.h>
40 #define NL_GROUP_KERNEL 1
43 #define SOCK_CLOEXEC 0
47 #define SOCK_NONBLOCK 0
50 static int linux_netlink_socket = -1;
51 static usbi_event_t netlink_control_event = USBI_INVALID_EVENT;
52 static pthread_t libusb_linux_event_thread;
54 static void *linux_netlink_event_thread_main(void *arg);
56 static int set_fd_cloexec_nb(int fd, int socktype)
60 #if defined(FD_CLOEXEC)
61 /* Make sure the netlink socket file descriptor is marked as CLOEXEC */
62 if (!(socktype & SOCK_CLOEXEC)) {
63 flags = fcntl(fd, F_GETFD);
65 usbi_err(NULL, "failed to get netlink fd flags, errno=%d", errno);
69 if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1) {
70 usbi_err(NULL, "failed to set netlink fd flags, errno=%d", errno);
76 /* Make sure the netlink socket is non-blocking */
77 if (!(socktype & SOCK_NONBLOCK)) {
78 flags = fcntl(fd, F_GETFL);
80 usbi_err(NULL, "failed to get netlink fd status flags, errno=%d", errno);
84 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) {
85 usbi_err(NULL, "failed to set netlink fd status flags, errno=%d", errno);
93 int linux_netlink_start_event_monitor(void)
95 struct sockaddr_nl sa_nl = { .nl_family = AF_NETLINK, .nl_groups = NL_GROUP_KERNEL };
96 int socktype = SOCK_RAW | SOCK_NONBLOCK | SOCK_CLOEXEC;
100 linux_netlink_socket = socket(PF_NETLINK, socktype, NETLINK_KOBJECT_UEVENT);
101 if (linux_netlink_socket == -1 && errno == EINVAL) {
102 usbi_dbg(NULL, "failed to create netlink socket of type %d, attempting SOCK_RAW", socktype);
104 linux_netlink_socket = socket(PF_NETLINK, socktype, NETLINK_KOBJECT_UEVENT);
107 if (linux_netlink_socket == -1) {
108 usbi_err(NULL, "failed to create netlink socket, errno=%d", errno);
112 ret = set_fd_cloexec_nb(linux_netlink_socket, socktype);
114 goto err_close_socket;
116 ret = bind(linux_netlink_socket, (struct sockaddr *)&sa_nl, sizeof(sa_nl));
118 usbi_err(NULL, "failed to bind netlink socket, errno=%d", errno);
119 goto err_close_socket;
122 ret = setsockopt(linux_netlink_socket, SOL_SOCKET, SO_PASSCRED, &opt, sizeof(opt));
124 usbi_err(NULL, "failed to set netlink socket SO_PASSCRED option, errno=%d", errno);
125 goto err_close_socket;
128 ret = usbi_create_event(&netlink_control_event);
130 usbi_err(NULL, "failed to create netlink control event");
131 goto err_close_socket;
134 ret = pthread_create(&libusb_linux_event_thread, NULL, linux_netlink_event_thread_main, NULL);
136 usbi_err(NULL, "failed to create netlink event thread (%d)", ret);
137 goto err_destroy_event;
140 return LIBUSB_SUCCESS;
143 usbi_destroy_event(&netlink_control_event);
144 netlink_control_event = (usbi_event_t)USBI_INVALID_EVENT;
146 close(linux_netlink_socket);
147 linux_netlink_socket = -1;
149 return LIBUSB_ERROR_OTHER;
152 int linux_netlink_stop_event_monitor(void)
156 assert(linux_netlink_socket != -1);
158 /* Signal the control event and wait for the thread to exit */
159 usbi_signal_event(&netlink_control_event);
161 ret = pthread_join(libusb_linux_event_thread, NULL);
163 usbi_warn(NULL, "failed to join netlink event thread (%d)", ret);
165 usbi_destroy_event(&netlink_control_event);
166 netlink_control_event = (usbi_event_t)USBI_INVALID_EVENT;
168 close(linux_netlink_socket);
169 linux_netlink_socket = -1;
171 return LIBUSB_SUCCESS;
174 static const char *netlink_message_parse(const char *buffer, size_t len, const char *key)
176 const char *end = buffer + len;
177 size_t keylen = strlen(key);
179 while (buffer < end && *buffer) {
180 if (strncmp(buffer, key, keylen) == 0 && buffer[keylen] == '=')
181 return buffer + keylen + 1;
182 buffer += strlen(buffer) + 1;
188 /* parse parts of netlink message common to both libudev and the kernel */
189 static int linux_netlink_parse(const char *buffer, size_t len, int *detached,
190 const char **sys_name, uint8_t *busnum, uint8_t *devaddr)
192 const char *tmp, *slash;
201 tmp = netlink_message_parse(buffer, len, "ACTION");
204 } else if (strcmp(tmp, "remove") == 0) {
206 } else if (strcmp(tmp, "add") != 0) {
207 usbi_dbg(NULL, "unknown device action %s", tmp);
211 /* check that this is a usb message */
212 tmp = netlink_message_parse(buffer, len, "SUBSYSTEM");
213 if (!tmp || strcmp(tmp, "usb") != 0) {
214 /* not usb. ignore */
218 /* check that this is an actual usb device */
219 tmp = netlink_message_parse(buffer, len, "DEVTYPE");
220 if (!tmp || strcmp(tmp, "usb_device") != 0) {
221 /* not usb. ignore */
225 tmp = netlink_message_parse(buffer, len, "BUSNUM");
227 *busnum = (uint8_t)(strtoul(tmp, NULL, 10) & 0xff);
233 tmp = netlink_message_parse(buffer, len, "DEVNUM");
237 *devaddr = (uint8_t)(strtoul(tmp, NULL, 10) & 0xff);
243 /* no bus number. try "DEVICE" */
244 tmp = netlink_message_parse(buffer, len, "DEVICE");
246 /* not usb. ignore */
250 /* Parse a device path such as /dev/bus/usb/003/004 */
251 slash = strrchr(tmp, '/');
255 *busnum = (uint8_t)(strtoul(slash - 3, NULL, 10) & 0xff);
261 *devaddr = (uint8_t)(strtoul(slash + 1, NULL, 10) & 0xff);
270 tmp = netlink_message_parse(buffer, len, "DEVPATH");
274 slash = strrchr(tmp, '/');
276 *sys_name = slash + 1;
278 /* found a usb device */
282 static int linux_netlink_read_message(void)
284 char cred_buffer[CMSG_SPACE(sizeof(struct ucred))];
285 char msg_buffer[2048];
286 const char *sys_name = NULL;
287 uint8_t busnum, devaddr;
290 struct cmsghdr *cmsg;
292 struct sockaddr_nl sa_nl;
293 struct iovec iov = { .iov_base = msg_buffer, .iov_len = sizeof(msg_buffer) };
294 struct msghdr msg = {
295 .msg_iov = &iov, .msg_iovlen = 1,
296 .msg_control = cred_buffer, .msg_controllen = sizeof(cred_buffer),
297 .msg_name = &sa_nl, .msg_namelen = sizeof(sa_nl)
300 /* read netlink message */
301 len = recvmsg(linux_netlink_socket, &msg, 0);
303 if (errno != EAGAIN && errno != EINTR)
304 usbi_err(NULL, "error receiving message from netlink, errno=%d", errno);
308 if (len < 32 || (msg.msg_flags & MSG_TRUNC)) {
309 usbi_err(NULL, "invalid netlink message length");
313 if (sa_nl.nl_groups != NL_GROUP_KERNEL || sa_nl.nl_pid != 0) {
314 usbi_dbg(NULL, "ignoring netlink message from unknown group/PID (%u/%u)",
315 (unsigned int)sa_nl.nl_groups, (unsigned int)sa_nl.nl_pid);
319 cmsg = CMSG_FIRSTHDR(&msg);
320 if (!cmsg || cmsg->cmsg_type != SCM_CREDENTIALS) {
321 usbi_dbg(NULL, "ignoring netlink message with no sender credentials");
325 cred = (struct ucred *)CMSG_DATA(cmsg);
326 if (cred->uid != 0) {
327 usbi_dbg(NULL, "ignoring netlink message with non-zero sender UID %u", (unsigned int)cred->uid);
331 r = linux_netlink_parse(msg_buffer, (size_t)len, &detached, &sys_name, &busnum, &devaddr);
335 usbi_dbg(NULL, "netlink hotplug found device busnum: %hhu, devaddr: %hhu, sys_name: %s, removed: %s",
336 busnum, devaddr, sys_name, detached ? "yes" : "no");
338 /* signal device is available (or not) to all contexts */
340 linux_device_disconnected(busnum, devaddr);
342 linux_hotplug_enumerate(busnum, devaddr, sys_name);
347 static void *linux_netlink_event_thread_main(void *arg)
349 struct pollfd fds[] = {
350 { .fd = USBI_EVENT_OS_HANDLE(&netlink_control_event),
351 .events = USBI_EVENT_POLL_EVENTS },
352 { .fd = linux_netlink_socket,
359 #if defined(HAVE_PTHREAD_SETNAME_NP)
360 r = pthread_setname_np(pthread_self(), "libusb_event");
362 usbi_warn(NULL, "failed to set hotplug event thread name, error=%d", r);
365 usbi_dbg(NULL, "netlink event thread entering");
368 r = poll(fds, 2, -1);
370 /* check for temporary failure */
373 usbi_err(NULL, "poll() failed, errno=%d", errno);
376 if (fds[0].revents) {
377 /* activity on control event, exit */
380 if (fds[1].revents) {
381 usbi_mutex_static_lock(&linux_hotplug_lock);
382 linux_netlink_read_message();
383 usbi_mutex_static_unlock(&linux_hotplug_lock);
387 usbi_dbg(NULL, "netlink event thread exiting");
392 void linux_netlink_hotplug_poll(void)
396 usbi_mutex_static_lock(&linux_hotplug_lock);
398 r = linux_netlink_read_message();
400 usbi_mutex_static_unlock(&linux_hotplug_lock);