Darwin: Fix a SIGFPE
[libusbx.git] / libusb / os / linux_udev.c
blob99ac943410557de1a5cdd54082932db972040c2c
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) 2012-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
23 #include "config.h"
25 #include <assert.h>
26 #include <ctype.h>
27 #include <dirent.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <poll.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <sys/ioctl.h>
35 #include <sys/stat.h>
36 #include <sys/types.h>
37 #include <sys/utsname.h>
38 #include <sys/socket.h>
39 #include <unistd.h>
40 #include <libudev.h>
42 #include "libusb.h"
43 #include "libusbi.h"
44 #include "linux_usbfs.h"
46 /* udev context */
47 static struct udev *udev_ctx = NULL;
48 static int udev_monitor_fd = -1;
49 static int udev_control_pipe[2] = {-1, -1};
50 static struct udev_monitor *udev_monitor = NULL;
51 static pthread_t linux_event_thread;
53 static void udev_hotplug_event(struct udev_device* udev_dev);
54 static void *linux_udev_event_thread_main(void *arg);
56 int linux_udev_start_event_monitor(void)
58 int r;
60 assert(udev_ctx == NULL);
61 udev_ctx = udev_new();
62 if (!udev_ctx) {
63 usbi_err(NULL, "could not create udev context");
64 return LIBUSB_ERROR_OTHER;
67 udev_monitor = udev_monitor_new_from_netlink(udev_ctx, "udev");
68 if (!udev_monitor) {
69 usbi_err(NULL, "could not initialize udev monitor");
70 goto err_free_ctx;
73 r = udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "usb", 0);
74 if (r) {
75 usbi_err(NULL, "could not initialize udev monitor filter for \"usb\" subsystem");
76 goto err_free_monitor;
79 if (udev_monitor_enable_receiving(udev_monitor)) {
80 usbi_err(NULL, "failed to enable the udev monitor");
81 goto err_free_monitor;
84 udev_monitor_fd = udev_monitor_get_fd(udev_monitor);
86 /* Some older versions of udev are not non-blocking by default,
87 * so make sure this is set */
88 r = fcntl(udev_monitor_fd, F_GETFL);
89 if (r == -1) {
90 usbi_err(NULL, "getting udev monitor fd flags (%d)", errno);
91 goto err_free_monitor;
93 r = fcntl(udev_monitor_fd, F_SETFL, r | O_NONBLOCK);
94 if (r) {
95 usbi_err(NULL, "setting udev monitor fd flags (%d)", errno);
96 goto err_free_monitor;
99 r = usbi_pipe(udev_control_pipe);
100 if (r) {
101 usbi_err(NULL, "could not create udev control pipe");
102 goto err_free_monitor;
105 r = pthread_create(&linux_event_thread, NULL, linux_udev_event_thread_main, NULL);
106 if (r) {
107 usbi_err(NULL, "creating hotplug event thread (%d)", r);
108 goto err_close_pipe;
111 return LIBUSB_SUCCESS;
113 err_close_pipe:
114 close(udev_control_pipe[0]);
115 close(udev_control_pipe[1]);
116 err_free_monitor:
117 udev_monitor_unref(udev_monitor);
118 udev_monitor = NULL;
119 udev_monitor_fd = -1;
120 err_free_ctx:
121 udev_unref(udev_ctx);
122 udev_ctx = NULL;
123 return LIBUSB_ERROR_OTHER;
126 int linux_udev_stop_event_monitor(void)
128 char dummy = 1;
129 int r;
131 assert(udev_ctx != NULL);
132 assert(udev_monitor != NULL);
133 assert(udev_monitor_fd != -1);
135 /* Write some dummy data to the control pipe and
136 * wait for the thread to exit */
137 r = usbi_write(udev_control_pipe[1], &dummy, sizeof(dummy));
138 if (r <= 0) {
139 usbi_warn(NULL, "udev control pipe signal failed");
141 pthread_join(linux_event_thread, NULL);
143 /* Release the udev monitor */
144 udev_monitor_unref(udev_monitor);
145 udev_monitor = NULL;
146 udev_monitor_fd = -1;
148 /* Clean up the udev context */
149 udev_unref(udev_ctx);
150 udev_ctx = NULL;
152 /* close and reset control pipe */
153 close(udev_control_pipe[0]);
154 close(udev_control_pipe[1]);
155 udev_control_pipe[0] = -1;
156 udev_control_pipe[1] = -1;
158 return LIBUSB_SUCCESS;
161 static void *linux_udev_event_thread_main(void *arg)
163 char dummy;
164 int r;
165 struct udev_device* udev_dev;
166 struct pollfd fds[] = {
167 {.fd = udev_control_pipe[0],
168 .events = POLLIN},
169 {.fd = udev_monitor_fd,
170 .events = POLLIN},
173 usbi_dbg("udev event thread entering.");
175 while (poll(fds, 2, -1) >= 0) {
176 if (fds[0].revents & POLLIN) {
177 /* activity on control pipe, read the byte and exit */
178 r = usbi_read(udev_control_pipe[0], &dummy, sizeof(dummy));
179 if (r <= 0) {
180 usbi_warn(NULL, "udev control pipe read failed");
182 break;
184 if (fds[1].revents & POLLIN) {
185 usbi_mutex_static_lock(&linux_hotplug_lock);
186 udev_dev = udev_monitor_receive_device(udev_monitor);
187 if (udev_dev)
188 udev_hotplug_event(udev_dev);
189 usbi_mutex_static_unlock(&linux_hotplug_lock);
193 usbi_dbg("udev event thread exiting");
195 return NULL;
198 static int udev_device_info(struct libusb_context *ctx, int detached,
199 struct udev_device *udev_dev, uint8_t *busnum,
200 uint8_t *devaddr, const char **sys_name) {
201 const char *dev_node;
203 dev_node = udev_device_get_devnode(udev_dev);
204 if (!dev_node) {
205 return LIBUSB_ERROR_OTHER;
208 *sys_name = udev_device_get_sysname(udev_dev);
209 if (!*sys_name) {
210 return LIBUSB_ERROR_OTHER;
213 return linux_get_device_address(ctx, detached, busnum, devaddr,
214 dev_node, *sys_name);
217 static void udev_hotplug_event(struct udev_device* udev_dev)
219 const char* udev_action;
220 const char* sys_name = NULL;
221 uint8_t busnum = 0, devaddr = 0;
222 int detached;
223 int r;
225 do {
226 udev_action = udev_device_get_action(udev_dev);
227 if (!udev_action) {
228 break;
231 detached = !strncmp(udev_action, "remove", 6);
233 r = udev_device_info(NULL, detached, udev_dev, &busnum, &devaddr, &sys_name);
234 if (LIBUSB_SUCCESS != r) {
235 break;
238 usbi_dbg("udev hotplug event. action: %s.", udev_action);
240 if (strncmp(udev_action, "add", 3) == 0) {
241 linux_hotplug_enumerate(busnum, devaddr, sys_name);
242 } else if (detached) {
243 linux_device_disconnected(busnum, devaddr, sys_name);
244 } else {
245 usbi_err(NULL, "ignoring udev action %s", udev_action);
247 } while (0);
249 udev_device_unref(udev_dev);
252 int linux_udev_scan_devices(struct libusb_context *ctx)
254 struct udev_enumerate *enumerator;
255 struct udev_list_entry *devices, *entry;
256 struct udev_device *udev_dev;
257 const char *sys_name;
258 int r;
260 assert(udev_ctx != NULL);
262 enumerator = udev_enumerate_new(udev_ctx);
263 if (NULL == enumerator) {
264 usbi_err(ctx, "error creating udev enumerator");
265 return LIBUSB_ERROR_OTHER;
268 udev_enumerate_add_match_subsystem(enumerator, "usb");
269 udev_enumerate_scan_devices(enumerator);
270 devices = udev_enumerate_get_list_entry(enumerator);
272 udev_list_entry_foreach(entry, devices) {
273 const char *path = udev_list_entry_get_name(entry);
274 uint8_t busnum = 0, devaddr = 0;
276 udev_dev = udev_device_new_from_syspath(udev_ctx, path);
278 r = udev_device_info(ctx, 0, udev_dev, &busnum, &devaddr, &sys_name);
279 if (r) {
280 udev_device_unref(udev_dev);
281 continue;
284 linux_enumerate_device(ctx, busnum, devaddr, sys_name);
285 udev_device_unref(udev_dev);
288 udev_enumerate_unref(enumerator);
290 return LIBUSB_SUCCESS;
293 void linux_udev_hotplug_poll(void)
295 struct udev_device* udev_dev;
297 usbi_mutex_static_lock(&linux_hotplug_lock);
298 do {
299 udev_dev = udev_monitor_receive_device(udev_monitor);
300 if (udev_dev) {
301 usbi_dbg("Handling hotplug event from hotplug_poll");
302 udev_hotplug_event(udev_dev);
304 } while (udev_dev);
305 usbi_mutex_static_unlock(&linux_hotplug_lock);