linux_netlink.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. #include "config.h"
  23. #include <ctype.h>
  24. #include <dirent.h>
  25. #include <errno.h>
  26. #include <fcntl.h>
  27. #include <poll.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <sys/types.h>
  32. #include <sys/socket.h>
  33. #include <arpa/inet.h>
  34. #include "libusb.h"
  35. #include "libusbi.h"
  36. #include "linux_usbfs.h"
  37. #include <linux/netlink.h>
  38. #include <linux/filter.h>
  39. #define KERNEL 1
  40. static int linux_netlink_socket = -1;
  41. static pthread_t libusb_linux_event_thread;
  42. static void *linux_netlink_event_thread_main(void *arg);
  43. struct sockaddr_nl snl = { .nl_family=AF_NETLINK, .nl_groups=KERNEL };
  44. int linux_netlink_start_event_monitor(void)
  45. {
  46. int ret;
  47. snl.nl_groups = KERNEL;
  48. linux_netlink_socket = socket(PF_NETLINK, SOCK_RAW|SOCK_CLOEXEC|SOCK_NONBLOCK, NETLINK_KOBJECT_UEVENT);
  49. if (-1 == linux_netlink_socket) {
  50. return LIBUSB_ERROR_OTHER;
  51. }
  52. ret = bind(linux_netlink_socket, (struct sockaddr *) &snl, sizeof(snl));
  53. if (0 != ret) {
  54. return LIBUSB_ERROR_OTHER;
  55. }
  56. /* TODO -- add authentication */
  57. /* setsockopt(linux_netlink_socket, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)); */
  58. ret = pthread_create(&libusb_linux_event_thread, NULL, linux_netlink_event_thread_main, NULL);
  59. if (0 != ret) {
  60. return LIBUSB_ERROR_OTHER;
  61. }
  62. return LIBUSB_SUCCESS;
  63. }
  64. int linux_netlink_stop_event_monitor(void)
  65. {
  66. int r;
  67. if (-1 == linux_netlink_socket) {
  68. /* already closed. nothing to do */
  69. return LIBUSB_SUCCESS;
  70. }
  71. r = close(linux_netlink_socket);
  72. if (0 > r) {
  73. usbi_err(NULL, "error closing netlink socket. %s", strerror(errno));
  74. return LIBUSB_ERROR_OTHER;
  75. }
  76. pthread_cancel(libusb_linux_event_thread);
  77. linux_netlink_socket = -1;
  78. return LIBUSB_SUCCESS;
  79. }
  80. static const char *netlink_message_parse (const char *buffer, size_t len, const char *key)
  81. {
  82. size_t keylen = strlen(key);
  83. size_t offset;
  84. for (offset = 0 ; offset < len && '\0' != buffer[offset] ; offset += strlen(buffer + offset) + 1) {
  85. if (0 == strncmp(buffer + offset, key, keylen) &&
  86. '=' == buffer[offset + keylen]) {
  87. return buffer + offset + keylen + 1;
  88. }
  89. }
  90. return NULL;
  91. }
  92. /* parse parts of netlink message common to both libudev and the kernel */
  93. static int linux_netlink_parse(char *buffer, size_t len, int *detached, const char **sys_name,
  94. uint8_t *busnum, uint8_t *devaddr) {
  95. const char *tmp;
  96. int i;
  97. errno = 0;
  98. *sys_name = NULL;
  99. *detached = 0;
  100. *busnum = 0;
  101. *devaddr = 0;
  102. tmp = netlink_message_parse((const char *) buffer, len, "ACTION");
  103. if (0 == strcmp(tmp, "remove")) {
  104. *detached = 1;
  105. } else if (0 != strcmp(tmp, "add")) {
  106. usbi_dbg("unknown device action");
  107. return -1;
  108. }
  109. /* check that this is a usb message */
  110. tmp = netlink_message_parse(buffer, len, "SUBSYSTEM");
  111. if (NULL == tmp || 0 != strcmp(tmp, "usb")) {
  112. /* not usb. ignore */
  113. return -1;
  114. }
  115. tmp = netlink_message_parse(buffer, len, "BUSNUM");
  116. if (NULL == tmp) {
  117. /* no bus number (likely a usb interface). ignore*/
  118. return -1;
  119. }
  120. *busnum = (uint8_t)(strtoul(tmp, NULL, 10) & 0xff);
  121. if (errno) {
  122. errno = 0;
  123. return -1;
  124. }
  125. tmp = netlink_message_parse(buffer, len, "DEVNUM");
  126. if (NULL == tmp) {
  127. return -1;
  128. }
  129. *devaddr = (uint8_t)(strtoul(tmp, NULL, 10) & 0xff);
  130. if (errno) {
  131. errno = 0;
  132. return -1;
  133. }
  134. tmp = netlink_message_parse(buffer, len, "DEVPATH");
  135. if (NULL == tmp) {
  136. return -1;
  137. }
  138. for (i = strlen(tmp) - 1 ; i ; --i) {
  139. if ('/' ==tmp[i]) {
  140. *sys_name = tmp + i + 1;
  141. break;
  142. }
  143. }
  144. /* found a usb device */
  145. return 0;
  146. }
  147. static void *linux_netlink_event_thread_main(void *arg)
  148. {
  149. struct pollfd fds = {.fd = linux_netlink_socket,
  150. .events = POLLIN};
  151. unsigned char buffer[1024];
  152. struct iovec iov = {.iov_base = buffer, .iov_len = sizeof(buffer)};
  153. struct msghdr meh = { .msg_iov=&iov, .msg_iovlen=1,
  154. .msg_name=&snl, .msg_namelen=sizeof(snl) };
  155. uint8_t busnum, devaddr;
  156. int detached, r;
  157. size_t len;
  158. /* silence compiler warning */
  159. (void) arg;
  160. while (1 == poll(&fds, 1, -1)) {
  161. const char *sys_name = NULL;
  162. if (POLLIN != fds.revents) {
  163. break;
  164. }
  165. /* read netlink message */
  166. memset(buffer, 0, sizeof(buffer));
  167. len = recvmsg(linux_netlink_socket, &meh, 0);
  168. if (len < 32) {
  169. usbi_dbg("error recieving message from netlink");
  170. continue;
  171. }
  172. /* TODO -- authenticate this message is from the kernel or udevd */
  173. r = linux_netlink_parse(buffer, len, &detached, &sys_name,
  174. &busnum, &devaddr);
  175. if (r)
  176. continue;
  177. usbi_dbg("netlink hotplug found device busnum: %hhu, devaddr: %hhu, sys_name: %s, removed: %s",
  178. busnum, devaddr, sys_name, detached ? "yes" : "no");
  179. /* signal device is available (or not) to all contexts */
  180. if (detached)
  181. linux_hotplug_disconnected(busnum, devaddr, sys_name);
  182. else
  183. linux_hotplug_enumerate(busnum, devaddr, sys_name);
  184. }
  185. return NULL;
  186. }