linux_netlink.c 8.5 KB

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