/src/libusb/libusb/os/linux_netlink.c
Line | Count | Source |
1 | | /* -*- Mode: C; c-basic-offset:8 ; indent-tabs-mode:t -*- */ |
2 | | /* |
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> |
8 | | * |
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. |
13 | | * |
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. |
18 | | * |
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 |
22 | | */ |
23 | | |
24 | | #include "libusbi.h" |
25 | | #include "linux_usbfs.h" |
26 | | |
27 | | #include <errno.h> |
28 | | #include <fcntl.h> |
29 | | #include <poll.h> |
30 | | #include <pthread.h> |
31 | | #include <string.h> |
32 | | #include <unistd.h> |
33 | | |
34 | | #ifdef HAVE_ASM_TYPES_H |
35 | | #include <asm/types.h> |
36 | | #endif |
37 | | #include <sys/socket.h> |
38 | | #include <linux/netlink.h> |
39 | | |
40 | 0 | #define NL_GROUP_KERNEL 1 |
41 | | |
42 | | #ifndef SOCK_CLOEXEC |
43 | | #define SOCK_CLOEXEC 0 |
44 | | #endif |
45 | | |
46 | | #ifndef SOCK_NONBLOCK |
47 | | #define SOCK_NONBLOCK 0 |
48 | | #endif |
49 | | |
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; |
53 | | |
54 | | static void *linux_netlink_event_thread_main(void *arg); |
55 | | |
56 | | static int set_fd_cloexec_nb(int fd, int socktype) |
57 | 0 | { |
58 | 0 | int flags; |
59 | |
|
60 | 0 | #if defined(FD_CLOEXEC) |
61 | | /* Make sure the netlink socket file descriptor is marked as CLOEXEC */ |
62 | 0 | if (!(socktype & SOCK_CLOEXEC)) { |
63 | 0 | flags = fcntl(fd, F_GETFD); |
64 | 0 | if (flags == -1) { |
65 | 0 | usbi_err(NULL, "failed to get netlink fd flags, errno=%d", errno); |
66 | 0 | return -1; |
67 | 0 | } |
68 | | |
69 | 0 | if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1) { |
70 | 0 | usbi_err(NULL, "failed to set netlink fd flags, errno=%d", errno); |
71 | 0 | return -1; |
72 | 0 | } |
73 | 0 | } |
74 | 0 | #endif |
75 | | |
76 | | /* Make sure the netlink socket is non-blocking */ |
77 | 0 | if (!(socktype & SOCK_NONBLOCK)) { |
78 | 0 | flags = fcntl(fd, F_GETFL); |
79 | 0 | if (flags == -1) { |
80 | 0 | usbi_err(NULL, "failed to get netlink fd status flags, errno=%d", errno); |
81 | 0 | return -1; |
82 | 0 | } |
83 | | |
84 | 0 | if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) { |
85 | 0 | usbi_err(NULL, "failed to set netlink fd status flags, errno=%d", errno); |
86 | 0 | return -1; |
87 | 0 | } |
88 | 0 | } |
89 | | |
90 | 0 | return 0; |
91 | 0 | } |
92 | | |
93 | | int linux_netlink_start_event_monitor(void) |
94 | 0 | { |
95 | 0 | struct sockaddr_nl sa_nl = { .nl_family = AF_NETLINK, .nl_groups = NL_GROUP_KERNEL }; |
96 | 0 | int socktype = SOCK_RAW | SOCK_NONBLOCK | SOCK_CLOEXEC; |
97 | 0 | int opt = 1; |
98 | 0 | int ret; |
99 | |
|
100 | 0 | linux_netlink_socket = socket(PF_NETLINK, socktype, NETLINK_KOBJECT_UEVENT); |
101 | 0 | if (linux_netlink_socket == -1 && errno == EINVAL) { |
102 | 0 | usbi_dbg(NULL, "failed to create netlink socket of type %d, attempting SOCK_RAW", socktype); |
103 | 0 | socktype = SOCK_RAW; |
104 | 0 | linux_netlink_socket = socket(PF_NETLINK, socktype, NETLINK_KOBJECT_UEVENT); |
105 | 0 | } |
106 | |
|
107 | 0 | if (linux_netlink_socket == -1) { |
108 | 0 | usbi_err(NULL, "failed to create netlink socket, errno=%d", errno); |
109 | 0 | goto err; |
110 | 0 | } |
111 | | |
112 | 0 | ret = set_fd_cloexec_nb(linux_netlink_socket, socktype); |
113 | 0 | if (ret == -1) |
114 | 0 | goto err_close_socket; |
115 | | |
116 | 0 | ret = bind(linux_netlink_socket, (struct sockaddr *)&sa_nl, sizeof(sa_nl)); |
117 | 0 | if (ret == -1) { |
118 | 0 | usbi_err(NULL, "failed to bind netlink socket, errno=%d", errno); |
119 | 0 | goto err_close_socket; |
120 | 0 | } |
121 | | |
122 | 0 | ret = setsockopt(linux_netlink_socket, SOL_SOCKET, SO_PASSCRED, &opt, sizeof(opt)); |
123 | 0 | if (ret == -1) { |
124 | 0 | usbi_err(NULL, "failed to set netlink socket SO_PASSCRED option, errno=%d", errno); |
125 | 0 | goto err_close_socket; |
126 | 0 | } |
127 | | |
128 | 0 | ret = usbi_create_event(&netlink_control_event); |
129 | 0 | if (ret) { |
130 | 0 | usbi_err(NULL, "failed to create netlink control event"); |
131 | 0 | goto err_close_socket; |
132 | 0 | } |
133 | | |
134 | 0 | ret = pthread_create(&libusb_linux_event_thread, NULL, linux_netlink_event_thread_main, NULL); |
135 | 0 | if (ret != 0) { |
136 | 0 | usbi_err(NULL, "failed to create netlink event thread (%d)", ret); |
137 | 0 | goto err_destroy_event; |
138 | 0 | } |
139 | | |
140 | 0 | return LIBUSB_SUCCESS; |
141 | | |
142 | 0 | err_destroy_event: |
143 | 0 | usbi_destroy_event(&netlink_control_event); |
144 | 0 | netlink_control_event = (usbi_event_t)USBI_INVALID_EVENT; |
145 | 0 | err_close_socket: |
146 | 0 | close(linux_netlink_socket); |
147 | 0 | linux_netlink_socket = -1; |
148 | 0 | err: |
149 | 0 | return LIBUSB_ERROR_OTHER; |
150 | 0 | } |
151 | | |
152 | | int linux_netlink_stop_event_monitor(void) |
153 | 0 | { |
154 | 0 | int ret; |
155 | |
|
156 | 0 | assert(linux_netlink_socket != -1); |
157 | | |
158 | | /* Signal the control event and wait for the thread to exit */ |
159 | 0 | usbi_signal_event(&netlink_control_event); |
160 | |
|
161 | 0 | ret = pthread_join(libusb_linux_event_thread, NULL); |
162 | 0 | if (ret) |
163 | 0 | usbi_warn(NULL, "failed to join netlink event thread (%d)", ret); |
164 | |
|
165 | 0 | usbi_destroy_event(&netlink_control_event); |
166 | 0 | netlink_control_event = (usbi_event_t)USBI_INVALID_EVENT; |
167 | |
|
168 | 0 | close(linux_netlink_socket); |
169 | 0 | linux_netlink_socket = -1; |
170 | |
|
171 | 0 | return LIBUSB_SUCCESS; |
172 | 0 | } |
173 | | |
174 | | static const char *netlink_message_parse(const char *buffer, size_t len, const char *key) |
175 | 0 | { |
176 | 0 | const char *end = buffer + len; |
177 | 0 | size_t keylen = strlen(key); |
178 | |
|
179 | 0 | while (buffer < end && *buffer) { |
180 | 0 | if (strncmp(buffer, key, keylen) == 0 && buffer[keylen] == '=') |
181 | 0 | return buffer + keylen + 1; |
182 | 0 | buffer += strlen(buffer) + 1; |
183 | 0 | } |
184 | | |
185 | 0 | return NULL; |
186 | 0 | } |
187 | | |
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) |
191 | 0 | { |
192 | 0 | const char *tmp, *slash; |
193 | |
|
194 | 0 | errno = 0; |
195 | |
|
196 | 0 | *sys_name = NULL; |
197 | 0 | *detached = 0; |
198 | 0 | *busnum = 0; |
199 | 0 | *devaddr = 0; |
200 | |
|
201 | 0 | tmp = netlink_message_parse(buffer, len, "ACTION"); |
202 | 0 | if (!tmp) { |
203 | 0 | return -1; |
204 | 0 | } else if (strcmp(tmp, "remove") == 0) { |
205 | 0 | *detached = 1; |
206 | 0 | } else if (strcmp(tmp, "add") != 0) { |
207 | 0 | usbi_dbg(NULL, "unknown device action %s", tmp); |
208 | 0 | return -1; |
209 | 0 | } |
210 | | |
211 | | /* check that this is a usb message */ |
212 | 0 | tmp = netlink_message_parse(buffer, len, "SUBSYSTEM"); |
213 | 0 | if (!tmp || strcmp(tmp, "usb") != 0) { |
214 | | /* not usb. ignore */ |
215 | 0 | return -1; |
216 | 0 | } |
217 | | |
218 | | /* check that this is an actual usb device */ |
219 | 0 | tmp = netlink_message_parse(buffer, len, "DEVTYPE"); |
220 | 0 | if (!tmp || strcmp(tmp, "usb_device") != 0) { |
221 | | /* not usb. ignore */ |
222 | 0 | return -1; |
223 | 0 | } |
224 | | |
225 | 0 | tmp = netlink_message_parse(buffer, len, "BUSNUM"); |
226 | 0 | if (tmp) { |
227 | 0 | *busnum = (uint8_t)(strtoul(tmp, NULL, 10) & 0xff); |
228 | 0 | if (errno) { |
229 | 0 | errno = 0; |
230 | 0 | return -1; |
231 | 0 | } |
232 | | |
233 | 0 | tmp = netlink_message_parse(buffer, len, "DEVNUM"); |
234 | 0 | if (NULL == tmp) |
235 | 0 | return -1; |
236 | | |
237 | 0 | *devaddr = (uint8_t)(strtoul(tmp, NULL, 10) & 0xff); |
238 | 0 | if (errno) { |
239 | 0 | errno = 0; |
240 | 0 | return -1; |
241 | 0 | } |
242 | 0 | } else { |
243 | | /* no bus number. try "DEVICE" */ |
244 | 0 | tmp = netlink_message_parse(buffer, len, "DEVICE"); |
245 | 0 | if (!tmp) { |
246 | | /* not usb. ignore */ |
247 | 0 | return -1; |
248 | 0 | } |
249 | | |
250 | | /* Parse a device path such as /dev/bus/usb/003/004 */ |
251 | 0 | slash = strrchr(tmp, '/'); |
252 | 0 | if (!slash) |
253 | 0 | return -1; |
254 | | |
255 | 0 | *busnum = (uint8_t)(strtoul(slash - 3, NULL, 10) & 0xff); |
256 | 0 | if (errno) { |
257 | 0 | errno = 0; |
258 | 0 | return -1; |
259 | 0 | } |
260 | | |
261 | 0 | *devaddr = (uint8_t)(strtoul(slash + 1, NULL, 10) & 0xff); |
262 | 0 | if (errno) { |
263 | 0 | errno = 0; |
264 | 0 | return -1; |
265 | 0 | } |
266 | | |
267 | 0 | return 0; |
268 | 0 | } |
269 | | |
270 | 0 | tmp = netlink_message_parse(buffer, len, "DEVPATH"); |
271 | 0 | if (!tmp) |
272 | 0 | return -1; |
273 | | |
274 | 0 | slash = strrchr(tmp, '/'); |
275 | 0 | if (slash) |
276 | 0 | *sys_name = slash + 1; |
277 | | |
278 | | /* found a usb device */ |
279 | 0 | return 0; |
280 | 0 | } |
281 | | |
282 | | static int linux_netlink_read_message(void) |
283 | 0 | { |
284 | 0 | char cred_buffer[CMSG_SPACE(sizeof(struct ucred))]; |
285 | 0 | char msg_buffer[2048]; |
286 | 0 | const char *sys_name = NULL; |
287 | 0 | uint8_t busnum, devaddr; |
288 | 0 | int detached, r; |
289 | 0 | ssize_t len; |
290 | 0 | struct cmsghdr *cmsg; |
291 | 0 | struct ucred *cred; |
292 | 0 | struct sockaddr_nl sa_nl; |
293 | 0 | struct iovec iov = { .iov_base = msg_buffer, .iov_len = sizeof(msg_buffer) }; |
294 | 0 | struct msghdr msg = { |
295 | 0 | .msg_iov = &iov, .msg_iovlen = 1, |
296 | 0 | .msg_control = cred_buffer, .msg_controllen = sizeof(cred_buffer), |
297 | 0 | .msg_name = &sa_nl, .msg_namelen = sizeof(sa_nl) |
298 | 0 | }; |
299 | | |
300 | | /* read netlink message */ |
301 | 0 | len = recvmsg(linux_netlink_socket, &msg, 0); |
302 | 0 | if (len == -1) { |
303 | 0 | if (errno != EAGAIN && errno != EINTR) |
304 | 0 | usbi_err(NULL, "error receiving message from netlink, errno=%d", errno); |
305 | 0 | return -1; |
306 | 0 | } |
307 | | |
308 | 0 | if (len < 32 || (msg.msg_flags & MSG_TRUNC)) { |
309 | 0 | usbi_err(NULL, "invalid netlink message length"); |
310 | 0 | return -1; |
311 | 0 | } |
312 | | |
313 | 0 | if (sa_nl.nl_groups != NL_GROUP_KERNEL || sa_nl.nl_pid != 0) { |
314 | 0 | usbi_dbg(NULL, "ignoring netlink message from unknown group/PID (%u/%u)", |
315 | 0 | (unsigned int)sa_nl.nl_groups, (unsigned int)sa_nl.nl_pid); |
316 | 0 | return -1; |
317 | 0 | } |
318 | | |
319 | 0 | cmsg = CMSG_FIRSTHDR(&msg); |
320 | 0 | if (!cmsg || cmsg->cmsg_type != SCM_CREDENTIALS) { |
321 | 0 | usbi_dbg(NULL, "ignoring netlink message with no sender credentials"); |
322 | 0 | return -1; |
323 | 0 | } |
324 | | |
325 | 0 | cred = (struct ucred *)CMSG_DATA(cmsg); |
326 | 0 | if (cred->uid != 0) { |
327 | 0 | usbi_dbg(NULL, "ignoring netlink message with non-zero sender UID %u", (unsigned int)cred->uid); |
328 | 0 | return -1; |
329 | 0 | } |
330 | | |
331 | 0 | r = linux_netlink_parse(msg_buffer, (size_t)len, &detached, &sys_name, &busnum, &devaddr); |
332 | 0 | if (r) |
333 | 0 | return r; |
334 | | |
335 | 0 | usbi_dbg(NULL, "netlink hotplug found device busnum: %hhu, devaddr: %hhu, sys_name: %s, removed: %s", |
336 | 0 | busnum, devaddr, sys_name, detached ? "yes" : "no"); |
337 | | |
338 | | /* signal device is available (or not) to all contexts */ |
339 | 0 | if (detached) |
340 | 0 | linux_device_disconnected(busnum, devaddr); |
341 | 0 | else |
342 | 0 | linux_hotplug_enumerate(busnum, devaddr, sys_name); |
343 | |
|
344 | 0 | return 0; |
345 | 0 | } |
346 | | |
347 | | static void *linux_netlink_event_thread_main(void *arg) |
348 | 0 | { |
349 | 0 | struct pollfd fds[] = { |
350 | 0 | { .fd = USBI_EVENT_OS_HANDLE(&netlink_control_event), |
351 | 0 | .events = USBI_EVENT_POLL_EVENTS }, |
352 | 0 | { .fd = linux_netlink_socket, |
353 | 0 | .events = POLLIN }, |
354 | 0 | }; |
355 | 0 | int r; |
356 | |
|
357 | 0 | UNUSED(arg); |
358 | |
|
359 | 0 | #if defined(HAVE_PTHREAD_SETNAME_NP) |
360 | 0 | r = pthread_setname_np(pthread_self(), "libusb_event"); |
361 | 0 | if (r) |
362 | 0 | usbi_warn(NULL, "failed to set hotplug event thread name, error=%d", r); |
363 | 0 | #endif |
364 | |
|
365 | 0 | usbi_dbg(NULL, "netlink event thread entering"); |
366 | |
|
367 | 0 | while (1) { |
368 | 0 | r = poll(fds, 2, -1); |
369 | 0 | if (r == -1) { |
370 | | /* check for temporary failure */ |
371 | 0 | if (errno == EINTR) |
372 | 0 | continue; |
373 | 0 | usbi_err(NULL, "poll() failed, errno=%d", errno); |
374 | 0 | break; |
375 | 0 | } |
376 | 0 | if (fds[0].revents) { |
377 | | /* activity on control event, exit */ |
378 | 0 | break; |
379 | 0 | } |
380 | 0 | if (fds[1].revents) { |
381 | 0 | usbi_mutex_static_lock(&linux_hotplug_lock); |
382 | 0 | linux_netlink_read_message(); |
383 | 0 | usbi_mutex_static_unlock(&linux_hotplug_lock); |
384 | 0 | } |
385 | 0 | } |
386 | |
|
387 | 0 | usbi_dbg(NULL, "netlink event thread exiting"); |
388 | |
|
389 | 0 | return NULL; |
390 | 0 | } |
391 | | |
392 | | void linux_netlink_hotplug_poll(void) |
393 | 0 | { |
394 | 0 | int r; |
395 | |
|
396 | 0 | usbi_mutex_static_lock(&linux_hotplug_lock); |
397 | 0 | do { |
398 | 0 | r = linux_netlink_read_message(); |
399 | 0 | } while (r == 0); |
400 | 0 | usbi_mutex_static_unlock(&linux_hotplug_lock); |
401 | 0 | } |