linux_netlink.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. * Copyright (c) 2016 Chris Dickens <christopher.a.dickens@gmail.com>
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #include "libusbi.h"
  24. #include "linux_usbfs.h"
  25. #include <errno.h>
  26. #include <fcntl.h>
  27. #include <poll.h>
  28. #include <pthread.h>
  29. #include <string.h>
  30. #include <unistd.h>
  31. #ifdef HAVE_ASM_TYPES_H
  32. #include <asm/types.h>
  33. #endif
  34. #include <sys/socket.h>
  35. #include <linux/netlink.h>
  36. #define NL_GROUP_KERNEL 1
  37. #ifndef SOCK_CLOEXEC
  38. #define SOCK_CLOEXEC 0
  39. #endif
  40. #ifndef SOCK_NONBLOCK
  41. #define SOCK_NONBLOCK 0
  42. #endif
  43. static int linux_netlink_socket = -1;
  44. static usbi_event_t netlink_control_event = USBI_INVALID_EVENT;
  45. static pthread_t libusb_linux_event_thread;
  46. static void *linux_netlink_event_thread_main(void *arg);
  47. static int set_fd_cloexec_nb(int fd, int socktype)
  48. {
  49. int flags;
  50. #if defined(FD_CLOEXEC)
  51. /* Make sure the netlink socket file descriptor is marked as CLOEXEC */
  52. if (!(socktype & SOCK_CLOEXEC)) {
  53. flags = fcntl(fd, F_GETFD);
  54. if (flags == -1) {
  55. usbi_err(NULL, "failed to get netlink fd flags, errno=%d", errno);
  56. return -1;
  57. }
  58. if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1) {
  59. usbi_err(NULL, "failed to set netlink fd flags, errno=%d", errno);
  60. return -1;
  61. }
  62. }
  63. #endif
  64. /* Make sure the netlink socket is non-blocking */
  65. if (!(socktype & SOCK_NONBLOCK)) {
  66. flags = fcntl(fd, F_GETFL);
  67. if (flags == -1) {
  68. usbi_err(NULL, "failed to get netlink fd status flags, errno=%d", errno);
  69. return -1;
  70. }
  71. if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) {
  72. usbi_err(NULL, "failed to set netlink fd status flags, errno=%d", errno);
  73. return -1;
  74. }
  75. }
  76. return 0;
  77. }
  78. int linux_netlink_start_event_monitor(void)
  79. {
  80. struct sockaddr_nl sa_nl = { .nl_family = AF_NETLINK, .nl_groups = NL_GROUP_KERNEL };
  81. int socktype = SOCK_RAW | SOCK_NONBLOCK | SOCK_CLOEXEC;
  82. int opt = 1;
  83. int ret;
  84. linux_netlink_socket = socket(PF_NETLINK, socktype, NETLINK_KOBJECT_UEVENT);
  85. if (linux_netlink_socket == -1 && errno == EINVAL) {
  86. usbi_dbg(NULL, "failed to create netlink socket of type %d, attempting SOCK_RAW", socktype);
  87. socktype = SOCK_RAW;
  88. linux_netlink_socket = socket(PF_NETLINK, socktype, NETLINK_KOBJECT_UEVENT);
  89. }
  90. if (linux_netlink_socket == -1) {
  91. usbi_err(NULL, "failed to create netlink socket, errno=%d", errno);
  92. goto err;
  93. }
  94. ret = set_fd_cloexec_nb(linux_netlink_socket, socktype);
  95. if (ret == -1)
  96. goto err_close_socket;
  97. ret = bind(linux_netlink_socket, (struct sockaddr *)&sa_nl, sizeof(sa_nl));
  98. if (ret == -1) {
  99. usbi_err(NULL, "failed to bind netlink socket, errno=%d", errno);
  100. goto err_close_socket;
  101. }
  102. ret = setsockopt(linux_netlink_socket, SOL_SOCKET, SO_PASSCRED, &opt, sizeof(opt));
  103. if (ret == -1) {
  104. usbi_err(NULL, "failed to set netlink socket SO_PASSCRED option, errno=%d", errno);
  105. goto err_close_socket;
  106. }
  107. ret = usbi_create_event(&netlink_control_event);
  108. if (ret) {
  109. usbi_err(NULL, "failed to create netlink control event");
  110. goto err_close_socket;
  111. }
  112. ret = pthread_create(&libusb_linux_event_thread, NULL, linux_netlink_event_thread_main, NULL);
  113. if (ret != 0) {
  114. usbi_err(NULL, "failed to create netlink event thread (%d)", ret);
  115. goto err_destroy_event;
  116. }
  117. return LIBUSB_SUCCESS;
  118. err_destroy_event:
  119. usbi_destroy_event(&netlink_control_event);
  120. netlink_control_event = (usbi_event_t)USBI_INVALID_EVENT;
  121. err_close_socket:
  122. close(linux_netlink_socket);
  123. linux_netlink_socket = -1;
  124. err:
  125. return LIBUSB_ERROR_OTHER;
  126. }
  127. int linux_netlink_stop_event_monitor(void)
  128. {
  129. int ret;
  130. assert(linux_netlink_socket != -1);
  131. /* Signal the control event and wait for the thread to exit */
  132. usbi_signal_event(&netlink_control_event);
  133. ret = pthread_join(libusb_linux_event_thread, NULL);
  134. if (ret)
  135. usbi_warn(NULL, "failed to join netlink event thread (%d)", ret);
  136. usbi_destroy_event(&netlink_control_event);
  137. netlink_control_event = (usbi_event_t)USBI_INVALID_EVENT;
  138. close(linux_netlink_socket);
  139. linux_netlink_socket = -1;
  140. return LIBUSB_SUCCESS;
  141. }
  142. static const char *netlink_message_parse(const char *buffer, size_t len, const char *key)
  143. {
  144. const char *end = buffer + len;
  145. size_t keylen = strlen(key);
  146. while (buffer < end && *buffer) {
  147. if (strncmp(buffer, key, keylen) == 0 && buffer[keylen] == '=')
  148. return buffer + keylen + 1;
  149. buffer += strlen(buffer) + 1;
  150. }
  151. return NULL;
  152. }
  153. /* parse parts of netlink message common to both libudev and the kernel */
  154. static int linux_netlink_parse(const char *buffer, size_t len, int *detached,
  155. const char **sys_name, uint8_t *busnum, uint8_t *devaddr)
  156. {
  157. const char *tmp, *slash;
  158. errno = 0;
  159. *sys_name = NULL;
  160. *detached = 0;
  161. *busnum = 0;
  162. *devaddr = 0;
  163. tmp = netlink_message_parse(buffer, len, "ACTION");
  164. if (!tmp) {
  165. return -1;
  166. } else if (strcmp(tmp, "remove") == 0) {
  167. *detached = 1;
  168. } else if (strcmp(tmp, "add") != 0) {
  169. usbi_dbg(NULL, "unknown device action %s", tmp);
  170. return -1;
  171. }
  172. /* check that this is a usb message */
  173. tmp = netlink_message_parse(buffer, len, "SUBSYSTEM");
  174. if (!tmp || strcmp(tmp, "usb") != 0) {
  175. /* not usb. ignore */
  176. return -1;
  177. }
  178. /* check that this is an actual usb device */
  179. tmp = netlink_message_parse(buffer, len, "DEVTYPE");
  180. if (!tmp || strcmp(tmp, "usb_device") != 0) {
  181. /* not usb. ignore */
  182. return -1;
  183. }
  184. tmp = netlink_message_parse(buffer, len, "BUSNUM");
  185. if (tmp) {
  186. *busnum = (uint8_t)(strtoul(tmp, NULL, 10) & 0xff);
  187. if (errno) {
  188. errno = 0;
  189. return -1;
  190. }
  191. tmp = netlink_message_parse(buffer, len, "DEVNUM");
  192. if (NULL == tmp)
  193. return -1;
  194. *devaddr = (uint8_t)(strtoul(tmp, NULL, 10) & 0xff);
  195. if (errno) {
  196. errno = 0;
  197. return -1;
  198. }
  199. } else {
  200. /* no bus number. try "DEVICE" */
  201. tmp = netlink_message_parse(buffer, len, "DEVICE");
  202. if (!tmp) {
  203. /* not usb. ignore */
  204. return -1;
  205. }
  206. /* Parse a device path such as /dev/bus/usb/003/004 */
  207. slash = strrchr(tmp, '/');
  208. if (!slash)
  209. return -1;
  210. *busnum = (uint8_t)(strtoul(slash - 3, NULL, 10) & 0xff);
  211. if (errno) {
  212. errno = 0;
  213. return -1;
  214. }
  215. *devaddr = (uint8_t)(strtoul(slash + 1, NULL, 10) & 0xff);
  216. if (errno) {
  217. errno = 0;
  218. return -1;
  219. }
  220. return 0;
  221. }
  222. tmp = netlink_message_parse(buffer, len, "DEVPATH");
  223. if (!tmp)
  224. return -1;
  225. slash = strrchr(tmp, '/');
  226. if (slash)
  227. *sys_name = slash + 1;
  228. /* found a usb device */
  229. return 0;
  230. }
  231. static int linux_netlink_read_message(void)
  232. {
  233. char cred_buffer[CMSG_SPACE(sizeof(struct ucred))];
  234. char msg_buffer[2048];
  235. const char *sys_name = NULL;
  236. uint8_t busnum, devaddr;
  237. int detached, r;
  238. ssize_t len;
  239. struct cmsghdr *cmsg;
  240. struct ucred *cred;
  241. struct sockaddr_nl sa_nl;
  242. struct iovec iov = { .iov_base = msg_buffer, .iov_len = sizeof(msg_buffer) };
  243. struct msghdr msg = {
  244. .msg_iov = &iov, .msg_iovlen = 1,
  245. .msg_control = cred_buffer, .msg_controllen = sizeof(cred_buffer),
  246. .msg_name = &sa_nl, .msg_namelen = sizeof(sa_nl)
  247. };
  248. /* read netlink message */
  249. len = recvmsg(linux_netlink_socket, &msg, 0);
  250. if (len == -1) {
  251. if (errno != EAGAIN && errno != EINTR)
  252. usbi_err(NULL, "error receiving message from netlink, errno=%d", errno);
  253. return -1;
  254. }
  255. if (len < 32 || (msg.msg_flags & MSG_TRUNC)) {
  256. usbi_err(NULL, "invalid netlink message length");
  257. return -1;
  258. }
  259. if (sa_nl.nl_groups != NL_GROUP_KERNEL || sa_nl.nl_pid != 0) {
  260. usbi_dbg(NULL, "ignoring netlink message from unknown group/PID (%u/%u)",
  261. (unsigned int)sa_nl.nl_groups, (unsigned int)sa_nl.nl_pid);
  262. return -1;
  263. }
  264. cmsg = CMSG_FIRSTHDR(&msg);
  265. if (!cmsg || cmsg->cmsg_type != SCM_CREDENTIALS) {
  266. usbi_dbg(NULL, "ignoring netlink message with no sender credentials");
  267. return -1;
  268. }
  269. cred = (struct ucred *)CMSG_DATA(cmsg);
  270. if (cred->uid != 0) {
  271. usbi_dbg(NULL, "ignoring netlink message with non-zero sender UID %u", (unsigned int)cred->uid);
  272. return -1;
  273. }
  274. r = linux_netlink_parse(msg_buffer, (size_t)len, &detached, &sys_name, &busnum, &devaddr);
  275. if (r)
  276. return r;
  277. usbi_dbg(NULL, "netlink hotplug found device busnum: %hhu, devaddr: %hhu, sys_name: %s, removed: %s",
  278. busnum, devaddr, sys_name, detached ? "yes" : "no");
  279. /* signal device is available (or not) to all contexts */
  280. if (detached)
  281. linux_device_disconnected(busnum, devaddr);
  282. else
  283. linux_hotplug_enumerate(busnum, devaddr, sys_name);
  284. return 0;
  285. }
  286. static void *linux_netlink_event_thread_main(void *arg)
  287. {
  288. struct pollfd fds[] = {
  289. { .fd = USBI_EVENT_OS_HANDLE(&netlink_control_event),
  290. .events = USBI_EVENT_POLL_EVENTS },
  291. { .fd = linux_netlink_socket,
  292. .events = POLLIN },
  293. };
  294. int r;
  295. UNUSED(arg);
  296. #if defined(HAVE_PTHREAD_SETNAME_NP)
  297. r = pthread_setname_np(pthread_self(), "libusb_event");
  298. if (r)
  299. usbi_warn(NULL, "failed to set hotplug event thread name, error=%d", r);
  300. #endif
  301. usbi_dbg(NULL, "netlink event thread entering");
  302. while (1) {
  303. r = poll(fds, 2, -1);
  304. if (r == -1) {
  305. /* check for temporary failure */
  306. if (errno == EINTR)
  307. continue;
  308. usbi_err(NULL, "poll() failed, errno=%d", errno);
  309. break;
  310. }
  311. if (fds[0].revents) {
  312. /* activity on control event, exit */
  313. break;
  314. }
  315. if (fds[1].revents) {
  316. usbi_mutex_static_lock(&linux_hotplug_lock);
  317. linux_netlink_read_message();
  318. usbi_mutex_static_unlock(&linux_hotplug_lock);
  319. }
  320. }
  321. usbi_dbg(NULL, "netlink event thread exiting");
  322. return NULL;
  323. }
  324. void linux_netlink_hotplug_poll(void)
  325. {
  326. int r;
  327. usbi_mutex_static_lock(&linux_hotplug_lock);
  328. do {
  329. r = linux_netlink_read_message();
  330. } while (r == 0);
  331. usbi_mutex_static_unlock(&linux_hotplug_lock);
  332. }