Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5)

Side by Side Diff: third_party/libusb/src/libusb/os/linux_netlink.c

Issue 19490008: Recommit: Update libusb 1.0.9 to libusbx 1.0.16 (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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 *
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.
12 *
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.
17 *
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
21 */
22
23 #include "config.h"
24 #include <ctype.h>
25 #include <dirent.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <poll.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <arpa/inet.h>
35
36 #include "libusb.h"
37 #include "libusbi.h"
38 #include "linux_usbfs.h"
39
40 #include <linux/netlink.h>
41 #include <linux/filter.h>
42
43 #define KERNEL 1
44
45 static int linux_netlink_socket = -1;
46 static pthread_t libusb_linux_event_thread;
47
48 static void *linux_netlink_event_thread_main(void *arg);
49
50 struct sockaddr_nl snl = { .nl_family=AF_NETLINK, .nl_groups=KERNEL };
51
52 int linux_netlink_start_event_monitor(void)
53 {
54 int ret;
55
56 snl.nl_groups = KERNEL;
57
58 linux_netlink_socket = socket(PF_NETLINK, SOCK_RAW|SOCK_CLOEXEC|SOCK_NON BLOCK, NETLINK_KOBJECT_UEVENT);
59 if (-1 == linux_netlink_socket) {
60 return LIBUSB_ERROR_OTHER;
61 }
62
63 ret = bind(linux_netlink_socket, (struct sockaddr *) &snl, sizeof(snl));
64 if (0 != ret) {
65 return LIBUSB_ERROR_OTHER;
66 }
67
68 /* TODO -- add authentication */
69 /* setsockopt(linux_netlink_socket, SOL_SOCKET, SO_PASSCRED, &one, sizeo f(one)); */
70
71 ret = pthread_create(&libusb_linux_event_thread, NULL, linux_netlink_eve nt_thread_main, NULL);
72 if (0 != ret) {
73 return LIBUSB_ERROR_OTHER;
74 }
75
76 return LIBUSB_SUCCESS;
77 }
78
79 int linux_netlink_stop_event_monitor(void)
80 {
81 int r;
82
83 if (-1 == linux_netlink_socket) {
84 /* already closed. nothing to do */
85 return LIBUSB_SUCCESS;
86 }
87
88 r = close(linux_netlink_socket);
89 if (0 > r) {
90 usbi_err(NULL, "error closing netlink socket. %s", strerror(errn o));
91 return LIBUSB_ERROR_OTHER;
92 }
93
94 pthread_cancel(libusb_linux_event_thread);
95
96 linux_netlink_socket = -1;
97
98 return LIBUSB_SUCCESS;
99 }
100
101 static const char *netlink_message_parse (const char *buffer, size_t len, const char *key)
102 {
103 size_t keylen = strlen(key);
104 size_t offset;
105
106 for (offset = 0 ; offset < len && '\0' != buffer[offset] ; offset += str len(buffer + offset) + 1) {
107 if (0 == strncmp(buffer + offset, key, keylen) &&
108 '=' == buffer[offset + keylen]) {
109 return buffer + offset + keylen + 1;
110 }
111 }
112
113 return NULL;
114 }
115
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 ch ar **sys_name,
118 uint8_t *busnum, uint8_t *devaddr) {
119 const char *tmp;
120 int i;
121
122 errno = 0;
123
124 *sys_name = NULL;
125 *detached = 0;
126 *busnum = 0;
127 *devaddr = 0;
128
129 tmp = netlink_message_parse((const char *) buffer, len, "ACTION");
130 if (tmp == NULL)
131 return -1;
132 if (0 == strcmp(tmp, "remove")) {
133 *detached = 1;
134 } else if (0 != strcmp(tmp, "add")) {
135 usbi_dbg("unknown device action %s", tmp);
136 return -1;
137 }
138
139 /* check that this is a usb message */
140 tmp = netlink_message_parse(buffer, len, "SUBSYSTEM");
141 if (NULL == tmp || 0 != strcmp(tmp, "usb")) {
142 /* not usb. ignore */
143 return -1;
144 }
145
146 tmp = netlink_message_parse(buffer, len, "BUSNUM");
147 if (NULL == tmp) {
148 /* no bus number (likely a usb interface). ignore*/
149 return -1;
150 }
151
152 *busnum = (uint8_t)(strtoul(tmp, NULL, 10) & 0xff);
153 if (errno) {
154 errno = 0;
155 return -1;
156 }
157
158 tmp = netlink_message_parse(buffer, len, "DEVNUM");
159 if (NULL == tmp) {
160 return -1;
161 }
162
163 *devaddr = (uint8_t)(strtoul(tmp, NULL, 10) & 0xff);
164 if (errno) {
165 errno = 0;
166 return -1;
167 }
168
169 tmp = netlink_message_parse(buffer, len, "DEVPATH");
170 if (NULL == tmp) {
171 return -1;
172 }
173
174 for (i = strlen(tmp) - 1 ; i ; --i) {
175 if ('/' ==tmp[i]) {
176 *sys_name = tmp + i + 1;
177 break;
178 }
179 }
180
181 /* found a usb device */
182 return 0;
183 }
184
185 static int linux_netlink_read_message(void)
186 {
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 const char *sys_name = NULL;
192 uint8_t busnum, devaddr;
193 int detached, r;
194 size_t len;
195
196 /* read netlink message */
197 memset(buffer, 0, sizeof(buffer));
198 len = recvmsg(linux_netlink_socket, &meh, 0);
199 if (len < 32) {
200 if (errno != EAGAIN)
201 usbi_dbg("error recieving message from netlink");
202 return -1;
203 }
204
205 /* TODO -- authenticate this message is from the kernel or udevd */
206
207 r = linux_netlink_parse(buffer, len, &detached, &sys_name,
208 &busnum, &devaddr);
209 if (r)
210 return r;
211
212 usbi_dbg("netlink hotplug found device busnum: %hhu, devaddr: %hhu, sys_ name: %s, removed: %s",
213 busnum, devaddr, sys_name, detached ? "yes" : "no");
214
215 /* signal device is available (or not) to all contexts */
216 if (detached)
217 linux_hotplug_disconnected(busnum, devaddr, sys_name);
218 else
219 linux_hotplug_enumerate(busnum, devaddr, sys_name);
220
221 return 0;
222 }
223
224 static void *linux_netlink_event_thread_main(void *arg)
225 {
226 struct pollfd fds = {.fd = linux_netlink_socket,
227 .events = POLLIN};
228
229 /* silence compiler warning */
230 (void) arg;
231
232 while (1 == poll(&fds, 1, -1)) {
233 if (POLLIN != fds.revents) {
234 break;
235 }
236
237 usbi_mutex_static_lock(&linux_hotplug_lock);
238 linux_netlink_read_message();
239 usbi_mutex_static_unlock(&linux_hotplug_lock);
240 }
241
242 return NULL;
243 }
244
245 void linux_netlink_hotplug_poll(void)
246 {
247 int r;
248
249 usbi_mutex_static_lock(&linux_hotplug_lock);
250 do {
251 r = linux_netlink_read_message();
252 } while (r == 0);
253 usbi_mutex_static_unlock(&linux_hotplug_lock);
254 }
OLDNEW
« no previous file with comments | « third_party/libusb/src/libusb/os/darwin_usb.c ('k') | third_party/libusb/src/libusb/os/linux_udev.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698