linux_udev: Log error code on pthread_create failure
[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 "libusb.h"
43 #include "libusbi.h"
44 #include "linux_usbfs.h"
45
46 /* udev context */
47 static struct udev *udev_ctx = NULL;
48 static int udev_monitor_fd = -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                 return LIBUSB_ERROR_OTHER;
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", 0);
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         r = pthread_create(&linux_event_thread, NULL, linux_udev_event_thread_main, NULL);
86         if (r) {
87                 usbi_err(NULL, "creating hotplug event thread (%d)", r);
88                 goto err_free_monitor;
89         }
90
91         return LIBUSB_SUCCESS;
92
93 err_free_monitor:
94         udev_monitor_unref(udev_monitor);
95         udev_monitor = NULL;
96         udev_monitor_fd = -1;
97 err_free_ctx:
98         udev_unref(udev_ctx);
99         udev_ctx = NULL;
100         return LIBUSB_ERROR_OTHER;
101 }
102
103 int linux_udev_stop_event_monitor(void)
104 {
105         assert(udev_ctx != NULL);
106         assert(udev_monitor != NULL);
107         assert(udev_monitor_fd != -1);
108
109         /* Cancel the event thread. This is the only way to garauntee the thread
110            exits since closing the monitor fd won't necessarily cause poll
111            to return. */
112         pthread_cancel(linux_event_thread);
113
114         /* Release the udev monitor */
115         udev_monitor_unref(udev_monitor);
116         udev_monitor = NULL;
117         udev_monitor_fd = -1;
118
119         /* Clean up the udev context */
120         udev_unref(udev_ctx);
121         udev_ctx = NULL;
122
123         return LIBUSB_SUCCESS;
124 }
125
126 static void *linux_udev_event_thread_main(void *arg)
127 {
128         struct udev_device* udev_dev;
129         struct pollfd fds = {.fd = udev_monitor_fd,
130                              .events = POLLIN};
131
132         usbi_dbg("udev event thread entering.");
133
134         while (1 == poll(&fds, 1, -1)) {
135                 if (NULL == udev_monitor || POLLIN != fds.revents) {
136                         break;
137                 }
138
139                 usbi_mutex_static_lock(&linux_hotplug_lock);
140                 udev_dev = udev_monitor_receive_device(udev_monitor);
141                 if (udev_dev)
142                         udev_hotplug_event(udev_dev);
143                 usbi_mutex_static_unlock(&linux_hotplug_lock);
144         }
145
146         usbi_dbg("udev event thread exiting");
147
148         return NULL;
149 }
150
151 static int udev_device_info(struct libusb_context *ctx, int detached,
152                             struct udev_device *udev_dev, uint8_t *busnum,
153                             uint8_t *devaddr, const char **sys_name) {
154         const char *dev_node;
155
156         dev_node = udev_device_get_devnode(udev_dev);
157         if (!dev_node) {
158                 return LIBUSB_ERROR_OTHER;
159         }
160
161         *sys_name = udev_device_get_sysname(udev_dev);
162         if (!*sys_name) {
163                 return LIBUSB_ERROR_OTHER;
164         }
165
166         return linux_get_device_address(ctx, detached, busnum, devaddr,
167                                         dev_node, *sys_name);
168 }
169
170 static void udev_hotplug_event(struct udev_device* udev_dev)
171 {
172         const char* udev_action;
173         const char* sys_name = NULL;
174         uint8_t busnum = 0, devaddr = 0;
175         int detached;
176         int r;
177
178         do {
179                 udev_action = udev_device_get_action(udev_dev);
180                 if (!udev_action) {
181                         break;
182                 }
183
184                 detached = !strncmp(udev_action, "remove", 6);
185
186                 r = udev_device_info(NULL, detached, udev_dev, &busnum, &devaddr, &sys_name);
187                 if (LIBUSB_SUCCESS != r) {
188                         break;
189                 }
190
191                 usbi_dbg("udev hotplug event. action: %s.", udev_action);
192
193                 if (strncmp(udev_action, "add", 3) == 0) {
194                         linux_hotplug_enumerate(busnum, devaddr, sys_name);
195                 } else if (detached) {
196                         linux_hotplug_disconnected(busnum, devaddr, sys_name);
197                 } else {
198                         usbi_err(NULL, "ignoring udev action %s", udev_action);
199                 }
200         } while (0);
201
202         udev_device_unref(udev_dev);
203 }
204
205 int linux_udev_scan_devices(struct libusb_context *ctx)
206 {
207         struct udev_enumerate *enumerator;
208         struct udev_list_entry *devices, *entry;
209         struct udev_device *udev_dev;
210         const char *sys_name;
211         int r;
212
213         assert(udev_ctx != NULL);
214
215         enumerator = udev_enumerate_new(udev_ctx);
216         if (NULL == enumerator) {
217                 usbi_err(ctx, "error creating udev enumerator");
218                 return LIBUSB_ERROR_OTHER;
219         }
220
221         udev_enumerate_add_match_subsystem(enumerator, "usb");
222         udev_enumerate_scan_devices(enumerator);
223         devices = udev_enumerate_get_list_entry(enumerator);
224
225         udev_list_entry_foreach(entry, devices) {
226                 const char *path = udev_list_entry_get_name(entry);
227                 uint8_t busnum = 0, devaddr = 0;
228
229                 udev_dev = udev_device_new_from_syspath(udev_ctx, path);
230
231                 r = udev_device_info(ctx, 0, udev_dev, &busnum, &devaddr, &sys_name);
232                 if (r) {
233                         udev_device_unref(udev_dev);
234                         continue;
235                 }
236
237                 linux_enumerate_device(ctx, busnum, devaddr, sys_name);
238                 udev_device_unref(udev_dev);
239         }
240
241         udev_enumerate_unref(enumerator);
242
243         return LIBUSB_SUCCESS;
244 }
245
246 void linux_udev_hotplug_poll(void)
247 {
248         struct udev_device* udev_dev;
249
250         usbi_mutex_static_lock(&linux_hotplug_lock);
251         do {
252                 udev_dev = udev_monitor_receive_device(udev_monitor);
253                 if (udev_dev) {
254                         usbi_dbg("Handling hotplug event from hotplug_poll");
255                         udev_hotplug_event(udev_dev);
256                 }
257         } while (udev_dev);
258         usbi_mutex_static_unlock(&linux_hotplug_lock);
259 }