linux_udev.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. #include "libusbi.h"
  23. #include "linux_usbfs.h"
  24. #include <errno.h>
  25. #include <fcntl.h>
  26. #include <libudev.h>
  27. #include <poll.h>
  28. #include <pthread.h>
  29. #include <string.h>
  30. #include <unistd.h>
  31. /* udev context */
  32. static struct udev *udev_ctx = NULL;
  33. static int udev_monitor_fd = -1;
  34. static usbi_event_t udev_control_event = USBI_INVALID_EVENT;
  35. static struct udev_monitor *udev_monitor = NULL;
  36. static pthread_t linux_event_thread;
  37. static void udev_hotplug_event(struct udev_device *udev_dev);
  38. static void *linux_udev_event_thread_main(void *arg);
  39. int linux_udev_start_event_monitor(void)
  40. {
  41. int r;
  42. assert(udev_ctx == NULL);
  43. udev_ctx = udev_new();
  44. if (!udev_ctx) {
  45. usbi_err(NULL, "could not create udev context");
  46. goto err;
  47. }
  48. udev_monitor = udev_monitor_new_from_netlink(udev_ctx, "udev");
  49. if (!udev_monitor) {
  50. usbi_err(NULL, "could not initialize udev monitor");
  51. goto err_free_ctx;
  52. }
  53. r = udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "usb", "usb_device");
  54. if (r) {
  55. usbi_err(NULL, "could not initialize udev monitor filter for \"usb\" subsystem");
  56. goto err_free_monitor;
  57. }
  58. if (udev_monitor_enable_receiving(udev_monitor)) {
  59. usbi_err(NULL, "failed to enable the udev monitor");
  60. goto err_free_monitor;
  61. }
  62. udev_monitor_fd = udev_monitor_get_fd(udev_monitor);
  63. #if defined(FD_CLOEXEC)
  64. /* Make sure the udev file descriptor is marked as CLOEXEC */
  65. r = fcntl(udev_monitor_fd, F_GETFD);
  66. if (r == -1) {
  67. usbi_err(NULL, "failed to get udev monitor fd flags, errno=%d", errno);
  68. goto err_free_monitor;
  69. }
  70. if (!(r & FD_CLOEXEC)) {
  71. if (fcntl(udev_monitor_fd, F_SETFD, r | FD_CLOEXEC) == -1) {
  72. usbi_err(NULL, "failed to set udev monitor fd flags, errno=%d", errno);
  73. goto err_free_monitor;
  74. }
  75. }
  76. #endif
  77. /* Some older versions of udev are not non-blocking by default,
  78. * so make sure this is set */
  79. r = fcntl(udev_monitor_fd, F_GETFL);
  80. if (r == -1) {
  81. usbi_err(NULL, "failed to get udev monitor fd status flags, errno=%d", errno);
  82. goto err_free_monitor;
  83. }
  84. if (!(r & O_NONBLOCK)) {
  85. if (fcntl(udev_monitor_fd, F_SETFL, r | O_NONBLOCK) == -1) {
  86. usbi_err(NULL, "failed to set udev monitor fd status flags, errno=%d", errno);
  87. goto err_free_monitor;
  88. }
  89. }
  90. r = usbi_create_event(&udev_control_event);
  91. if (r) {
  92. usbi_err(NULL, "failed to create udev control event");
  93. goto err_free_monitor;
  94. }
  95. r = pthread_create(&linux_event_thread, NULL, linux_udev_event_thread_main, NULL);
  96. if (r) {
  97. usbi_err(NULL, "failed to create hotplug event thread (%d)", r);
  98. goto err_destroy_event;
  99. }
  100. return LIBUSB_SUCCESS;
  101. err_destroy_event:
  102. usbi_destroy_event(&udev_control_event);
  103. udev_control_event = (usbi_event_t)USBI_INVALID_EVENT;
  104. err_free_monitor:
  105. udev_monitor_unref(udev_monitor);
  106. udev_monitor = NULL;
  107. udev_monitor_fd = -1;
  108. err_free_ctx:
  109. udev_unref(udev_ctx);
  110. err:
  111. udev_ctx = NULL;
  112. return LIBUSB_ERROR_OTHER;
  113. }
  114. int linux_udev_stop_event_monitor(void)
  115. {
  116. int r;
  117. assert(udev_ctx != NULL);
  118. assert(udev_monitor != NULL);
  119. assert(udev_monitor_fd != -1);
  120. /* Signal the control event and wait for the thread to exit */
  121. usbi_signal_event(&udev_control_event);
  122. r = pthread_join(linux_event_thread, NULL);
  123. if (r)
  124. usbi_warn(NULL, "failed to join hotplug event thread (%d)", r);
  125. usbi_destroy_event(&udev_control_event);
  126. udev_control_event = (usbi_event_t)USBI_INVALID_EVENT;
  127. /* Release the udev monitor */
  128. udev_monitor_unref(udev_monitor);
  129. udev_monitor = NULL;
  130. udev_monitor_fd = -1;
  131. /* Clean up the udev context */
  132. udev_unref(udev_ctx);
  133. udev_ctx = NULL;
  134. return LIBUSB_SUCCESS;
  135. }
  136. static void *linux_udev_event_thread_main(void *arg)
  137. {
  138. struct pollfd fds[] = {
  139. { .fd = USBI_EVENT_OS_HANDLE(&udev_control_event),
  140. .events = USBI_EVENT_POLL_EVENTS },
  141. { .fd = udev_monitor_fd,
  142. .events = POLLIN },
  143. };
  144. struct udev_device *udev_dev;
  145. int r;
  146. UNUSED(arg);
  147. #if defined(HAVE_PTHREAD_SETNAME_NP)
  148. r = pthread_setname_np(pthread_self(), "libusb_event");
  149. if (r)
  150. usbi_warn(NULL, "failed to set hotplug event thread name, error=%d", r);
  151. #endif
  152. usbi_dbg(NULL, "udev event thread entering");
  153. while (1) {
  154. r = poll(fds, 2, -1);
  155. if (r == -1) {
  156. /* check for temporary failure */
  157. if (errno == EINTR)
  158. continue;
  159. usbi_err(NULL, "poll() failed, errno=%d", errno);
  160. break;
  161. }
  162. if (fds[0].revents) {
  163. /* activity on control event, exit */
  164. break;
  165. }
  166. if (fds[1].revents) {
  167. usbi_mutex_static_lock(&linux_hotplug_lock);
  168. udev_dev = udev_monitor_receive_device(udev_monitor);
  169. if (udev_dev)
  170. udev_hotplug_event(udev_dev);
  171. usbi_mutex_static_unlock(&linux_hotplug_lock);
  172. }
  173. }
  174. usbi_dbg(NULL, "udev event thread exiting");
  175. return NULL;
  176. }
  177. static int udev_device_info(struct libusb_context *ctx, int detached,
  178. struct udev_device *udev_dev, uint8_t *busnum,
  179. uint8_t *devaddr, const char **sys_name) {
  180. const char *dev_node;
  181. dev_node = udev_device_get_devnode(udev_dev);
  182. if (!dev_node) {
  183. return LIBUSB_ERROR_OTHER;
  184. }
  185. *sys_name = udev_device_get_sysname(udev_dev);
  186. if (!*sys_name) {
  187. return LIBUSB_ERROR_OTHER;
  188. }
  189. return linux_get_device_address(ctx, detached, busnum, devaddr,
  190. dev_node, *sys_name, -1);
  191. }
  192. static void udev_hotplug_event(struct udev_device *udev_dev)
  193. {
  194. const char *udev_action;
  195. const char *sys_name = NULL;
  196. uint8_t busnum = 0, devaddr = 0;
  197. int detached;
  198. int r;
  199. do {
  200. udev_action = udev_device_get_action(udev_dev);
  201. if (!udev_action) {
  202. break;
  203. }
  204. detached = !strncmp(udev_action, "remove", 6);
  205. r = udev_device_info(NULL, detached, udev_dev, &busnum, &devaddr, &sys_name);
  206. if (LIBUSB_SUCCESS != r) {
  207. break;
  208. }
  209. usbi_dbg(NULL, "udev hotplug event. action: %s.", udev_action);
  210. if (strncmp(udev_action, "add", 3) == 0) {
  211. linux_hotplug_enumerate(busnum, devaddr, sys_name);
  212. } else if (detached) {
  213. linux_device_disconnected(busnum, devaddr);
  214. } else if (strncmp(udev_action, "bind", 4) == 0) {
  215. /* silently ignore "known unhandled" action */
  216. } else {
  217. usbi_err(NULL, "ignoring udev action %s", udev_action);
  218. }
  219. } while (0);
  220. udev_device_unref(udev_dev);
  221. }
  222. int linux_udev_scan_devices(struct libusb_context *ctx)
  223. {
  224. struct udev_enumerate *enumerator;
  225. struct udev_list_entry *devices, *entry;
  226. struct udev_device *udev_dev;
  227. const char *sys_name;
  228. int r;
  229. assert(udev_ctx != NULL);
  230. enumerator = udev_enumerate_new(udev_ctx);
  231. if (NULL == enumerator) {
  232. usbi_err(ctx, "error creating udev enumerator");
  233. return LIBUSB_ERROR_OTHER;
  234. }
  235. udev_enumerate_add_match_subsystem(enumerator, "usb");
  236. udev_enumerate_add_match_property(enumerator, "DEVTYPE", "usb_device");
  237. udev_enumerate_scan_devices(enumerator);
  238. devices = udev_enumerate_get_list_entry(enumerator);
  239. entry = NULL;
  240. udev_list_entry_foreach(entry, devices) {
  241. const char *path = udev_list_entry_get_name(entry);
  242. uint8_t busnum = 0, devaddr = 0;
  243. udev_dev = udev_device_new_from_syspath(udev_ctx, path);
  244. r = udev_device_info(ctx, 0, udev_dev, &busnum, &devaddr, &sys_name);
  245. if (r) {
  246. udev_device_unref(udev_dev);
  247. continue;
  248. }
  249. linux_enumerate_device(ctx, busnum, devaddr, sys_name);
  250. udev_device_unref(udev_dev);
  251. }
  252. udev_enumerate_unref(enumerator);
  253. return LIBUSB_SUCCESS;
  254. }
  255. void linux_udev_hotplug_poll(void)
  256. {
  257. struct udev_device *udev_dev;
  258. usbi_mutex_static_lock(&linux_hotplug_lock);
  259. do {
  260. udev_dev = udev_monitor_receive_device(udev_monitor);
  261. if (udev_dev) {
  262. usbi_dbg(NULL, "Handling hotplug event from hotplug_poll");
  263. udev_hotplug_event(udev_dev);
  264. }
  265. } while (udev_dev);
  266. usbi_mutex_static_unlock(&linux_hotplug_lock);
  267. }