linux_udev: Retry poll() on EINTR
[platform/upstream/libusb.git] / libusb / os / linux_udev.c
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>
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
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>
41
42 #include "libusbi.h"
43 #include "linux_usbfs.h"
44
45 /* udev context */
46 static struct udev *udev_ctx = NULL;
47 static int udev_monitor_fd = -1;
48 static int udev_control_pipe[2] = {-1, -1};
49 static struct udev_monitor *udev_monitor = NULL;
50 static pthread_t linux_event_thread;
51
52 static void udev_hotplug_event(struct udev_device* udev_dev);
53 static void *linux_udev_event_thread_main(void *arg);
54
55 int linux_udev_start_event_monitor(void)
56 {
57         int r;
58
59         assert(udev_ctx == NULL);
60         udev_ctx = udev_new();
61         if (!udev_ctx) {
62                 usbi_err(NULL, "could not create udev context");
63                 goto err;
64         }
65
66         udev_monitor = udev_monitor_new_from_netlink(udev_ctx, "udev");
67         if (!udev_monitor) {
68                 usbi_err(NULL, "could not initialize udev monitor");
69                 goto err_free_ctx;
70         }
71
72         r = udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "usb", "usb_device");
73         if (r) {
74                 usbi_err(NULL, "could not initialize udev monitor filter for \"usb\" subsystem");
75                 goto err_free_monitor;
76         }
77
78         if (udev_monitor_enable_receiving(udev_monitor)) {
79                 usbi_err(NULL, "failed to enable the udev monitor");
80                 goto err_free_monitor;
81         }
82
83         udev_monitor_fd = udev_monitor_get_fd(udev_monitor);
84
85         /* Some older versions of udev are not non-blocking by default,
86          * so make sure this is set */
87         r = fcntl(udev_monitor_fd, F_GETFL);
88         if (r == -1) {
89                 usbi_err(NULL, "getting udev monitor fd flags (%d)", errno);
90                 goto err_free_monitor;
91         }
92         r = fcntl(udev_monitor_fd, F_SETFL, r | O_NONBLOCK);
93         if (r) {
94                 usbi_err(NULL, "setting udev monitor fd flags (%d)", errno);
95                 goto err_free_monitor;
96         }
97
98         r = usbi_pipe(udev_control_pipe);
99         if (r) {
100                 usbi_err(NULL, "could not create udev control pipe");
101                 goto err_free_monitor;
102         }
103
104         r = pthread_create(&linux_event_thread, NULL, linux_udev_event_thread_main, NULL);
105         if (r) {
106                 usbi_err(NULL, "creating hotplug event thread (%d)", r);
107                 goto err_close_pipe;
108         }
109
110         return LIBUSB_SUCCESS;
111
112 err_close_pipe:
113         close(udev_control_pipe[0]);
114         close(udev_control_pipe[1]);
115 err_free_monitor:
116         udev_monitor_unref(udev_monitor);
117         udev_monitor = NULL;
118         udev_monitor_fd = -1;
119 err_free_ctx:
120         udev_unref(udev_ctx);
121 err:
122         udev_ctx = NULL;
123         return LIBUSB_ERROR_OTHER;
124 }
125
126 int linux_udev_stop_event_monitor(void)
127 {
128         char dummy = 1;
129         int r;
130
131         assert(udev_ctx != NULL);
132         assert(udev_monitor != NULL);
133         assert(udev_monitor_fd != -1);
134
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");
140         }
141         pthread_join(linux_event_thread, NULL);
142
143         /* Release the udev monitor */
144         udev_monitor_unref(udev_monitor);
145         udev_monitor = NULL;
146         udev_monitor_fd = -1;
147
148         /* Clean up the udev context */
149         udev_unref(udev_ctx);
150         udev_ctx = NULL;
151
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;
157
158         return LIBUSB_SUCCESS;
159 }
160
161 static void *linux_udev_event_thread_main(void *arg)
162 {
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},
171         };
172
173         usbi_dbg("udev event thread entering.");
174
175         while ((r = poll(fds, 2, -1)) >= 0 || errno == EINTR) {
176                 if (r < 0) {
177                         /* temporary failure */
178                         continue;
179                 }
180                 if (fds[0].revents & POLLIN) {
181                         /* activity on control pipe, read the byte and exit */
182                         r = usbi_read(udev_control_pipe[0], &dummy, sizeof(dummy));
183                         if (r <= 0) {
184                                 usbi_warn(NULL, "udev control pipe read failed");
185                         }
186                         break;
187                 }
188                 if (fds[1].revents & POLLIN) {
189                         usbi_mutex_static_lock(&linux_hotplug_lock);
190                         udev_dev = udev_monitor_receive_device(udev_monitor);
191                         if (udev_dev)
192                                 udev_hotplug_event(udev_dev);
193                         usbi_mutex_static_unlock(&linux_hotplug_lock);
194                 }
195         }
196
197         usbi_dbg("udev event thread exiting");
198
199         return NULL;
200 }
201
202 static int udev_device_info(struct libusb_context *ctx, int detached,
203                             struct udev_device *udev_dev, uint8_t *busnum,
204                             uint8_t *devaddr, const char **sys_name) {
205         const char *dev_node;
206
207         dev_node = udev_device_get_devnode(udev_dev);
208         if (!dev_node) {
209                 return LIBUSB_ERROR_OTHER;
210         }
211
212         *sys_name = udev_device_get_sysname(udev_dev);
213         if (!*sys_name) {
214                 return LIBUSB_ERROR_OTHER;
215         }
216
217         return linux_get_device_address(ctx, detached, busnum, devaddr,
218                                         dev_node, *sys_name);
219 }
220
221 static void udev_hotplug_event(struct udev_device* udev_dev)
222 {
223         const char* udev_action;
224         const char* sys_name = NULL;
225         uint8_t busnum = 0, devaddr = 0;
226         int detached;
227         int r;
228
229         do {
230                 udev_action = udev_device_get_action(udev_dev);
231                 if (!udev_action) {
232                         break;
233                 }
234
235                 detached = !strncmp(udev_action, "remove", 6);
236
237                 r = udev_device_info(NULL, detached, udev_dev, &busnum, &devaddr, &sys_name);
238                 if (LIBUSB_SUCCESS != r) {
239                         break;
240                 }
241
242                 usbi_dbg("udev hotplug event. action: %s.", udev_action);
243
244                 if (strncmp(udev_action, "add", 3) == 0) {
245                         linux_hotplug_enumerate(busnum, devaddr, sys_name);
246                 } else if (detached) {
247                         linux_device_disconnected(busnum, devaddr);
248                 } else {
249                         usbi_err(NULL, "ignoring udev action %s", udev_action);
250                 }
251         } while (0);
252
253         udev_device_unref(udev_dev);
254 }
255
256 int linux_udev_scan_devices(struct libusb_context *ctx)
257 {
258         struct udev_enumerate *enumerator;
259         struct udev_list_entry *devices, *entry;
260         struct udev_device *udev_dev;
261         const char *sys_name;
262         int r;
263
264         assert(udev_ctx != NULL);
265
266         enumerator = udev_enumerate_new(udev_ctx);
267         if (NULL == enumerator) {
268                 usbi_err(ctx, "error creating udev enumerator");
269                 return LIBUSB_ERROR_OTHER;
270         }
271
272         udev_enumerate_add_match_subsystem(enumerator, "usb");
273         udev_enumerate_add_match_property(enumerator, "DEVTYPE", "usb_device");
274         udev_enumerate_scan_devices(enumerator);
275         devices = udev_enumerate_get_list_entry(enumerator);
276
277         udev_list_entry_foreach(entry, devices) {
278                 const char *path = udev_list_entry_get_name(entry);
279                 uint8_t busnum = 0, devaddr = 0;
280
281                 udev_dev = udev_device_new_from_syspath(udev_ctx, path);
282
283                 r = udev_device_info(ctx, 0, udev_dev, &busnum, &devaddr, &sys_name);
284                 if (r) {
285                         udev_device_unref(udev_dev);
286                         continue;
287                 }
288
289                 linux_enumerate_device(ctx, busnum, devaddr, sys_name);
290                 udev_device_unref(udev_dev);
291         }
292
293         udev_enumerate_unref(enumerator);
294
295         return LIBUSB_SUCCESS;
296 }
297
298 void linux_udev_hotplug_poll(void)
299 {
300         struct udev_device* udev_dev;
301
302         usbi_mutex_static_lock(&linux_hotplug_lock);
303         do {
304                 udev_dev = udev_monitor_receive_device(udev_monitor);
305                 if (udev_dev) {
306                         usbi_dbg("Handling hotplug event from hotplug_poll");
307                         udev_hotplug_event(udev_dev);
308                 }
309         } while (udev_dev);
310         usbi_mutex_static_unlock(&linux_hotplug_lock);
311 }