linux_netlink.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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 "libusb.h"
  24. #include "libusbi.h"
  25. #include "linux_usbfs.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/types.h>
  35. #ifdef HAVE_ASM_TYPES_H
  36. #include <asm/types.h>
  37. #endif
  38. #ifdef HAVE_SYS_SOCKET_H
  39. #include <sys/socket.h>
  40. #endif
  41. #include <arpa/inet.h>
  42. #ifdef HAVE_LINUX_NETLINK_H
  43. #include <linux/netlink.h>
  44. #endif
  45. #ifdef HAVE_LINUX_FILTER_H
  46. #include <linux/filter.h>
  47. #endif
  48. #define KERNEL 1
  49. static int linux_netlink_socket = -1;
  50. static int netlink_control_pipe[2] = { -1, -1 };
  51. static pthread_t libusb_linux_event_thread;
  52. static void *linux_netlink_event_thread_main(void *arg);
  53. struct sockaddr_nl snl = { .nl_family=AF_NETLINK, .nl_groups=KERNEL };
  54. static int set_fd_cloexec_nb (int fd)
  55. {
  56. int flags;
  57. #if defined(FD_CLOEXEC)
  58. flags = fcntl (linux_netlink_socket, F_GETFD);
  59. if (0 > flags) {
  60. return -1;
  61. }
  62. if (!(flags & FD_CLOEXEC)) {
  63. fcntl (linux_netlink_socket, F_SETFD, flags | FD_CLOEXEC);
  64. }
  65. #endif
  66. flags = fcntl (linux_netlink_socket, F_GETFL);
  67. if (0 > flags) {
  68. return -1;
  69. }
  70. if (!(flags & O_NONBLOCK)) {
  71. fcntl (linux_netlink_socket, F_SETFL, flags | O_NONBLOCK);
  72. }
  73. return 0;
  74. }
  75. int linux_netlink_start_event_monitor(void)
  76. {
  77. int socktype = SOCK_RAW;
  78. int ret;
  79. snl.nl_groups = KERNEL;
  80. #if defined(SOCK_CLOEXEC)
  81. socktype |= SOCK_CLOEXEC;
  82. #endif
  83. #if defined(SOCK_NONBLOCK)
  84. socktype |= SOCK_NONBLOCK;
  85. #endif
  86. linux_netlink_socket = socket(PF_NETLINK, socktype, NETLINK_KOBJECT_UEVENT);
  87. if (-1 == linux_netlink_socket && EINVAL == errno) {
  88. linux_netlink_socket = socket(PF_NETLINK, SOCK_RAW, NETLINK_KOBJECT_UEVENT);
  89. }
  90. if (-1 == linux_netlink_socket) {
  91. return LIBUSB_ERROR_OTHER;
  92. }
  93. ret = set_fd_cloexec_nb (linux_netlink_socket);
  94. if (0 != ret) {
  95. close (linux_netlink_socket);
  96. linux_netlink_socket = -1;
  97. return LIBUSB_ERROR_OTHER;
  98. }
  99. ret = bind(linux_netlink_socket, (struct sockaddr *) &snl, sizeof(snl));
  100. if (0 != ret) {
  101. close(linux_netlink_socket);
  102. return LIBUSB_ERROR_OTHER;
  103. }
  104. /* TODO -- add authentication */
  105. /* setsockopt(linux_netlink_socket, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)); */
  106. ret = usbi_pipe(netlink_control_pipe);
  107. if (ret) {
  108. usbi_err(NULL, "could not create netlink control pipe");
  109. close(linux_netlink_socket);
  110. return LIBUSB_ERROR_OTHER;
  111. }
  112. ret = pthread_create(&libusb_linux_event_thread, NULL, linux_netlink_event_thread_main, NULL);
  113. if (0 != ret) {
  114. close(netlink_control_pipe[0]);
  115. close(netlink_control_pipe[1]);
  116. close(linux_netlink_socket);
  117. return LIBUSB_ERROR_OTHER;
  118. }
  119. return LIBUSB_SUCCESS;
  120. }
  121. int linux_netlink_stop_event_monitor(void)
  122. {
  123. int r;
  124. char dummy = 1;
  125. if (-1 == linux_netlink_socket) {
  126. /* already closed. nothing to do */
  127. return LIBUSB_SUCCESS;
  128. }
  129. /* Write some dummy data to the control pipe and
  130. * wait for the thread to exit */
  131. r = usbi_write(netlink_control_pipe[1], &dummy, sizeof(dummy));
  132. if (r <= 0) {
  133. usbi_warn(NULL, "netlink control pipe signal failed");
  134. }
  135. pthread_join(libusb_linux_event_thread, NULL);
  136. close(linux_netlink_socket);
  137. linux_netlink_socket = -1;
  138. /* close and reset control pipe */
  139. close(netlink_control_pipe[0]);
  140. close(netlink_control_pipe[1]);
  141. netlink_control_pipe[0] = -1;
  142. netlink_control_pipe[1] = -1;
  143. return LIBUSB_SUCCESS;
  144. }
  145. static const char *netlink_message_parse (const char *buffer, size_t len, const char *key)
  146. {
  147. size_t keylen = strlen(key);
  148. size_t offset;
  149. for (offset = 0 ; offset < len && '\0' != buffer[offset] ; offset += strlen(buffer + offset) + 1) {
  150. if (0 == strncmp(buffer + offset, key, keylen) &&
  151. '=' == buffer[offset + keylen]) {
  152. return buffer + offset + keylen + 1;
  153. }
  154. }
  155. return NULL;
  156. }
  157. /* parse parts of netlink message common to both libudev and the kernel */
  158. static int linux_netlink_parse(char *buffer, size_t len, int *detached, const char **sys_name,
  159. uint8_t *busnum, uint8_t *devaddr) {
  160. const char *tmp;
  161. int i;
  162. errno = 0;
  163. *sys_name = NULL;
  164. *detached = 0;
  165. *busnum = 0;
  166. *devaddr = 0;
  167. tmp = netlink_message_parse((const char *) buffer, len, "ACTION");
  168. if (tmp == NULL)
  169. return -1;
  170. if (0 == strcmp(tmp, "remove")) {
  171. *detached = 1;
  172. } else if (0 != strcmp(tmp, "add")) {
  173. usbi_dbg("unknown device action %s", tmp);
  174. return -1;
  175. }
  176. /* check that this is a usb message */
  177. tmp = netlink_message_parse(buffer, len, "SUBSYSTEM");
  178. if (NULL == tmp || 0 != strcmp(tmp, "usb")) {
  179. /* not usb. ignore */
  180. return -1;
  181. }
  182. tmp = netlink_message_parse(buffer, len, "BUSNUM");
  183. if (NULL == tmp) {
  184. /* no bus number (likely a usb interface). ignore*/
  185. return -1;
  186. }
  187. *busnum = (uint8_t)(strtoul(tmp, NULL, 10) & 0xff);
  188. if (errno) {
  189. errno = 0;
  190. return -1;
  191. }
  192. tmp = netlink_message_parse(buffer, len, "DEVNUM");
  193. if (NULL == tmp) {
  194. return -1;
  195. }
  196. *devaddr = (uint8_t)(strtoul(tmp, NULL, 10) & 0xff);
  197. if (errno) {
  198. errno = 0;
  199. return -1;
  200. }
  201. tmp = netlink_message_parse(buffer, len, "DEVPATH");
  202. if (NULL == tmp) {
  203. return -1;
  204. }
  205. for (i = strlen(tmp) - 1 ; i ; --i) {
  206. if ('/' ==tmp[i]) {
  207. *sys_name = tmp + i + 1;
  208. break;
  209. }
  210. }
  211. /* found a usb device */
  212. return 0;
  213. }
  214. static int linux_netlink_read_message(void)
  215. {
  216. unsigned char buffer[1024];
  217. struct iovec iov = {.iov_base = buffer, .iov_len = sizeof(buffer)};
  218. struct msghdr meh = { .msg_iov=&iov, .msg_iovlen=1,
  219. .msg_name=&snl, .msg_namelen=sizeof(snl) };
  220. const char *sys_name = NULL;
  221. uint8_t busnum, devaddr;
  222. int detached, r;
  223. size_t len;
  224. /* read netlink message */
  225. memset(buffer, 0, sizeof(buffer));
  226. len = recvmsg(linux_netlink_socket, &meh, 0);
  227. if (len < 32) {
  228. if (errno != EAGAIN)
  229. usbi_dbg("error recieving message from netlink");
  230. return -1;
  231. }
  232. /* TODO -- authenticate this message is from the kernel or udevd */
  233. r = linux_netlink_parse(buffer, len, &detached, &sys_name,
  234. &busnum, &devaddr);
  235. if (r)
  236. return r;
  237. usbi_dbg("netlink hotplug found device busnum: %hhu, devaddr: %hhu, sys_name: %s, removed: %s",
  238. busnum, devaddr, sys_name, detached ? "yes" : "no");
  239. /* signal device is available (or not) to all contexts */
  240. if (detached)
  241. linux_device_disconnected(busnum, devaddr, sys_name);
  242. else
  243. linux_hotplug_enumerate(busnum, devaddr, sys_name);
  244. return 0;
  245. }
  246. static void *linux_netlink_event_thread_main(void *arg)
  247. {
  248. char dummy;
  249. int r;
  250. struct pollfd fds[] = {
  251. { .fd = netlink_control_pipe[0],
  252. .events = POLLIN },
  253. { .fd = linux_netlink_socket,
  254. .events = POLLIN },
  255. };
  256. /* silence compiler warning */
  257. (void) arg;
  258. while (poll(fds, 2, -1) >= 0) {
  259. if (fds[0].revents & POLLIN) {
  260. /* activity on control pipe, read the byte and exit */
  261. r = usbi_read(netlink_control_pipe[0], &dummy, sizeof(dummy));
  262. if (r <= 0) {
  263. usbi_warn(NULL, "netlink control pipe read failed");
  264. }
  265. break;
  266. }
  267. if (fds[1].revents & POLLIN) {
  268. usbi_mutex_static_lock(&linux_hotplug_lock);
  269. linux_netlink_read_message();
  270. usbi_mutex_static_unlock(&linux_hotplug_lock);
  271. }
  272. }
  273. return NULL;
  274. }
  275. void linux_netlink_hotplug_poll(void)
  276. {
  277. int r;
  278. usbi_mutex_static_lock(&linux_hotplug_lock);
  279. do {
  280. r = linux_netlink_read_message();
  281. } while (r == 0);
  282. usbi_mutex_static_unlock(&linux_hotplug_lock);
  283. }