events_posix.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. * libusb event abstraction on POSIX platforms
  3. *
  4. * Copyright © 2020 Chris Dickens <christopher.a.dickens@gmail.com>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libusbi.h"
  21. #include <errno.h>
  22. #include <fcntl.h>
  23. #ifdef HAVE_EVENTFD
  24. #include <sys/eventfd.h>
  25. #endif
  26. #ifdef HAVE_TIMERFD
  27. #include <sys/timerfd.h>
  28. #endif
  29. #include <unistd.h>
  30. #ifdef HAVE_EVENTFD
  31. #define EVENT_READ_FD(e) ((e)->eventfd)
  32. #define EVENT_WRITE_FD(e) ((e)->eventfd)
  33. #else
  34. #define EVENT_READ_FD(e) ((e)->pipefd[0])
  35. #define EVENT_WRITE_FD(e) ((e)->pipefd[1])
  36. #endif
  37. #ifdef HAVE_NFDS_T
  38. typedef nfds_t usbi_nfds_t;
  39. #else
  40. typedef unsigned int usbi_nfds_t;
  41. #endif
  42. int usbi_create_event(usbi_event_t *event)
  43. {
  44. #ifdef HAVE_EVENTFD
  45. event->eventfd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
  46. if (event->eventfd == -1) {
  47. usbi_err(NULL, "failed to create eventfd, errno=%d", errno);
  48. return LIBUSB_ERROR_OTHER;
  49. }
  50. return 0;
  51. #else
  52. #if defined(HAVE_PIPE2)
  53. int ret = pipe2(event->pipefd, O_CLOEXEC);
  54. #else
  55. int ret = pipe(event->pipefd);
  56. #endif
  57. if (ret != 0) {
  58. usbi_err(NULL, "failed to create pipe, errno=%d", errno);
  59. return LIBUSB_ERROR_OTHER;
  60. }
  61. #if !defined(HAVE_PIPE2) && defined(FD_CLOEXEC)
  62. ret = fcntl(event->pipefd[0], F_GETFD);
  63. if (ret == -1) {
  64. usbi_err(NULL, "failed to get pipe fd flags, errno=%d", errno);
  65. goto err_close_pipe;
  66. }
  67. ret = fcntl(event->pipefd[0], F_SETFD, ret | FD_CLOEXEC);
  68. if (ret == -1) {
  69. usbi_err(NULL, "failed to set pipe fd flags, errno=%d", errno);
  70. goto err_close_pipe;
  71. }
  72. ret = fcntl(event->pipefd[1], F_GETFD);
  73. if (ret == -1) {
  74. usbi_err(NULL, "failed to get pipe fd flags, errno=%d", errno);
  75. goto err_close_pipe;
  76. }
  77. ret = fcntl(event->pipefd[1], F_SETFD, ret | FD_CLOEXEC);
  78. if (ret == -1) {
  79. usbi_err(NULL, "failed to set pipe fd flags, errno=%d", errno);
  80. goto err_close_pipe;
  81. }
  82. #endif
  83. ret = fcntl(event->pipefd[1], F_GETFL);
  84. if (ret == -1) {
  85. usbi_err(NULL, "failed to get pipe fd status flags, errno=%d", errno);
  86. goto err_close_pipe;
  87. }
  88. ret = fcntl(event->pipefd[1], F_SETFL, ret | O_NONBLOCK);
  89. if (ret == -1) {
  90. usbi_err(NULL, "failed to set pipe fd status flags, errno=%d", errno);
  91. goto err_close_pipe;
  92. }
  93. return 0;
  94. err_close_pipe:
  95. close(event->pipefd[1]);
  96. close(event->pipefd[0]);
  97. return LIBUSB_ERROR_OTHER;
  98. #endif
  99. }
  100. void usbi_destroy_event(usbi_event_t *event)
  101. {
  102. #ifdef HAVE_EVENTFD
  103. if (close(event->eventfd) == -1)
  104. usbi_warn(NULL, "failed to close eventfd, errno=%d", errno);
  105. #else
  106. if (close(event->pipefd[1]) == -1)
  107. usbi_warn(NULL, "failed to close pipe write end, errno=%d", errno);
  108. if (close(event->pipefd[0]) == -1)
  109. usbi_warn(NULL, "failed to close pipe read end, errno=%d", errno);
  110. #endif
  111. }
  112. void usbi_signal_event(usbi_event_t *event)
  113. {
  114. uint64_t dummy = 1;
  115. ssize_t r;
  116. r = write(EVENT_WRITE_FD(event), &dummy, sizeof(dummy));
  117. if (r != sizeof(dummy))
  118. usbi_warn(NULL, "event write failed");
  119. }
  120. void usbi_clear_event(usbi_event_t *event)
  121. {
  122. uint64_t dummy;
  123. ssize_t r;
  124. r = read(EVENT_READ_FD(event), &dummy, sizeof(dummy));
  125. if (r != sizeof(dummy))
  126. usbi_warn(NULL, "event read failed");
  127. }
  128. #ifdef HAVE_TIMERFD
  129. int usbi_create_timer(usbi_timer_t *timer)
  130. {
  131. timer->timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC);
  132. if (timer->timerfd == -1) {
  133. usbi_warn(NULL, "failed to create timerfd, errno=%d", errno);
  134. return LIBUSB_ERROR_OTHER;
  135. }
  136. return 0;
  137. }
  138. void usbi_destroy_timer(usbi_timer_t *timer)
  139. {
  140. if (close(timer->timerfd) == -1)
  141. usbi_warn(NULL, "failed to close timerfd, errno=%d", errno);
  142. }
  143. int usbi_arm_timer(usbi_timer_t *timer, const struct timespec *timeout)
  144. {
  145. const struct itimerspec it = { { 0, 0 }, { timeout->tv_sec, timeout->tv_nsec } };
  146. if (timerfd_settime(timer->timerfd, TFD_TIMER_ABSTIME, &it, NULL) == -1) {
  147. usbi_warn(NULL, "failed to arm timerfd, errno=%d", errno);
  148. return LIBUSB_ERROR_OTHER;
  149. }
  150. return 0;
  151. }
  152. int usbi_disarm_timer(usbi_timer_t *timer)
  153. {
  154. const struct itimerspec it = { { 0, 0 }, { 0, 0 } };
  155. if (timerfd_settime(timer->timerfd, 0, &it, NULL) == -1) {
  156. usbi_warn(NULL, "failed to disarm timerfd, errno=%d", errno);
  157. return LIBUSB_ERROR_OTHER;
  158. }
  159. return 0;
  160. }
  161. #endif
  162. int usbi_alloc_event_data(struct libusb_context *ctx)
  163. {
  164. struct usbi_event_source *ievent_source;
  165. struct pollfd *fds;
  166. size_t i = 0;
  167. if (ctx->event_data) {
  168. free(ctx->event_data);
  169. ctx->event_data = NULL;
  170. }
  171. ctx->event_data_cnt = 0;
  172. for_each_event_source(ctx, ievent_source)
  173. ctx->event_data_cnt++;
  174. fds = calloc(ctx->event_data_cnt, sizeof(*fds));
  175. if (!fds)
  176. return LIBUSB_ERROR_NO_MEM;
  177. for_each_event_source(ctx, ievent_source) {
  178. fds[i].fd = ievent_source->data.os_handle;
  179. fds[i].events = ievent_source->data.poll_events;
  180. i++;
  181. }
  182. ctx->event_data = fds;
  183. return 0;
  184. }
  185. int usbi_wait_for_events(struct libusb_context *ctx,
  186. struct usbi_reported_events *reported_events, int timeout_ms)
  187. {
  188. struct pollfd *fds = ctx->event_data;
  189. usbi_nfds_t nfds = (usbi_nfds_t)ctx->event_data_cnt;
  190. int internal_fds, num_ready;
  191. usbi_dbg(ctx, "poll() %u fds with timeout in %dms", (unsigned int)nfds, timeout_ms);
  192. num_ready = poll(fds, nfds, timeout_ms);
  193. usbi_dbg(ctx, "poll() returned %d", num_ready);
  194. if (num_ready == 0) {
  195. if (usbi_using_timer(ctx))
  196. goto done;
  197. return LIBUSB_ERROR_TIMEOUT;
  198. } else if (num_ready == -1) {
  199. if (errno == EINTR)
  200. return LIBUSB_ERROR_INTERRUPTED;
  201. usbi_err(ctx, "poll() failed, errno=%d", errno);
  202. return LIBUSB_ERROR_IO;
  203. }
  204. /* fds[0] is always the internal signalling event */
  205. if (fds[0].revents) {
  206. reported_events->event_triggered = 1;
  207. num_ready--;
  208. } else {
  209. reported_events->event_triggered = 0;
  210. }
  211. #ifdef HAVE_OS_TIMER
  212. /* on timer configurations, fds[1] is the timer */
  213. if (usbi_using_timer(ctx) && fds[1].revents) {
  214. reported_events->timer_triggered = 1;
  215. num_ready--;
  216. } else {
  217. reported_events->timer_triggered = 0;
  218. }
  219. #endif
  220. if (!num_ready)
  221. goto done;
  222. /* the backend will never need to attempt to handle events on the
  223. * library's internal file descriptors, so we determine how many are
  224. * in use internally for this context and skip these when passing any
  225. * remaining pollfds to the backend. */
  226. internal_fds = usbi_using_timer(ctx) ? 2 : 1;
  227. fds += internal_fds;
  228. nfds -= internal_fds;
  229. usbi_mutex_lock(&ctx->event_data_lock);
  230. if (ctx->event_flags & USBI_EVENT_EVENT_SOURCES_MODIFIED) {
  231. struct usbi_event_source *ievent_source;
  232. for_each_removed_event_source(ctx, ievent_source) {
  233. usbi_nfds_t n;
  234. for (n = 0; n < nfds; n++) {
  235. if (ievent_source->data.os_handle != fds[n].fd)
  236. continue;
  237. if (!fds[n].revents)
  238. continue;
  239. /* pollfd was removed between the creation of the fds array and
  240. * here. remove triggered revent as it is no longer relevant. */
  241. usbi_dbg(ctx, "fd %d was removed, ignoring raised events", fds[n].fd);
  242. fds[n].revents = 0;
  243. num_ready--;
  244. break;
  245. }
  246. }
  247. }
  248. usbi_mutex_unlock(&ctx->event_data_lock);
  249. if (num_ready) {
  250. assert(num_ready > 0);
  251. reported_events->event_data = fds;
  252. reported_events->event_data_count = (unsigned int)nfds;
  253. }
  254. done:
  255. reported_events->num_ready = num_ready;
  256. return LIBUSB_SUCCESS;
  257. }