pr-poll-loop.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /*
  2. * Copyright (c) 2015-2020 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Jan Friesse (jfriesse@redhat.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the Red Hat, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <sys/types.h>
  35. #include <arpa/inet.h>
  36. #include <sys/queue.h>
  37. #include <assert.h>
  38. #include <inttypes.h>
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #include "pr-poll-array.h"
  42. #include "pr-poll-loop.h"
  43. /*
  44. * Needed for creating nspr handle from unix fd
  45. */
  46. #include <private/pprio.h>
  47. /*
  48. * Helper functions declarations
  49. */
  50. static PRInt16 poll_events_to_pr_events(short events);
  51. static short pr_events_to_poll_events(PRInt16 events);
  52. static int pr_poll_loop_add_fd_int(struct pr_poll_loop *poll_loop,
  53. int fd, PRFileDesc *prfd,
  54. short events, pr_poll_loop_fd_set_events_cb_fn fd_set_events_cb,
  55. pr_poll_loop_prfd_set_events_cb_fn prfd_set_events_cb,
  56. pr_poll_loop_fd_read_cb_fn fd_read_cb, pr_poll_loop_prfd_read_cb_fn prfd_read_cb,
  57. pr_poll_loop_fd_write_cb_fn fd_write_cb, pr_poll_loop_prfd_write_cb_fn prfd_write_cb,
  58. pr_poll_loop_fd_err_cb_fn fd_err_cb, pr_poll_loop_prfd_err_cb_fn prfd_err_cb,
  59. void *user_data1, void *user_data2);
  60. static int pr_poll_loop_del_fd_int(struct pr_poll_loop *poll_loop,
  61. int fd, PRFileDesc *prfd);
  62. static struct pr_poll_loop_fd_entry *pr_poll_loop_find_by_fd(
  63. const struct pr_poll_loop *poll_loop, int fd, PRFileDesc *prfd);
  64. static int prepare_poll_array(struct pr_poll_loop *poll_loop);
  65. /*
  66. * Helper functions definitions
  67. */
  68. static PRInt16
  69. poll_events_to_pr_events(short events)
  70. {
  71. PRInt16 res;
  72. res = 0;
  73. if (events & POLLIN) {
  74. res |= PR_POLL_READ;
  75. }
  76. if (events & POLLOUT) {
  77. res |= PR_POLL_WRITE;
  78. }
  79. return (res);
  80. }
  81. static short
  82. pr_events_to_poll_events(PRInt16 events)
  83. {
  84. short res;
  85. res = 0;
  86. if (events & PR_POLL_READ) {
  87. res |= POLLIN;
  88. }
  89. if (events & PR_POLL_WRITE) {
  90. res |= POLLOUT;
  91. }
  92. if (events & PR_POLL_ERR) {
  93. res |= POLLERR;
  94. }
  95. if (events & PR_POLL_NVAL) {
  96. res |= POLLNVAL;
  97. }
  98. if (events & PR_POLL_HUP) {
  99. res |= POLLHUP;
  100. }
  101. if (events & PR_POLL_EXCEPT) {
  102. res |= POLLERR;
  103. }
  104. return (res);
  105. }
  106. static int
  107. pr_poll_loop_add_fd_int(struct pr_poll_loop *poll_loop, int fd, PRFileDesc *prfd,
  108. short events, pr_poll_loop_fd_set_events_cb_fn fd_set_events_cb,
  109. pr_poll_loop_prfd_set_events_cb_fn prfd_set_events_cb,
  110. pr_poll_loop_fd_read_cb_fn fd_read_cb, pr_poll_loop_prfd_read_cb_fn prfd_read_cb,
  111. pr_poll_loop_fd_write_cb_fn fd_write_cb, pr_poll_loop_prfd_write_cb_fn prfd_write_cb,
  112. pr_poll_loop_fd_err_cb_fn fd_err_cb, pr_poll_loop_prfd_err_cb_fn prfd_err_cb,
  113. void *user_data1, void *user_data2)
  114. {
  115. struct pr_poll_loop_fd_entry *new_entry;
  116. assert((prfd != NULL && fd == -1) || (fd != -1 && prfd == NULL));
  117. if ((events & ~(POLLIN|POLLOUT)) != 0) {
  118. return (-1);
  119. }
  120. if (pr_poll_loop_find_by_fd(poll_loop, fd, prfd) != NULL) {
  121. return (-1);
  122. }
  123. new_entry = malloc(sizeof(*new_entry));
  124. if (new_entry == NULL) {
  125. return (-1);
  126. }
  127. memset(new_entry, 0, sizeof(*new_entry));
  128. new_entry->fd = fd;
  129. if (fd != -1) {
  130. new_entry->prfd = PR_CreateSocketPollFd(fd);
  131. if (new_entry->prfd == NULL) {
  132. free(new_entry);
  133. return (-1);
  134. }
  135. } else {
  136. new_entry->prfd = prfd;
  137. }
  138. new_entry->events = events;
  139. new_entry->fd_set_events_cb = fd_set_events_cb;
  140. new_entry->prfd_set_events_cb = prfd_set_events_cb;
  141. new_entry->fd_read_cb = fd_read_cb;
  142. new_entry->prfd_read_cb = prfd_read_cb;
  143. new_entry->fd_write_cb = fd_write_cb;
  144. new_entry->prfd_write_cb = prfd_write_cb;
  145. new_entry->fd_err_cb = fd_err_cb;
  146. new_entry->prfd_err_cb = prfd_err_cb;
  147. new_entry->user_data1 = user_data1;
  148. new_entry->user_data2 = user_data2;
  149. TAILQ_INSERT_TAIL(&poll_loop->fd_list, new_entry, entries);
  150. return (0);
  151. }
  152. static int
  153. pr_poll_loop_del_fd_int(struct pr_poll_loop *poll_loop, int fd, PRFileDesc *prfd)
  154. {
  155. struct pr_poll_loop_fd_entry *fd_entry;
  156. fd_entry = pr_poll_loop_find_by_fd(poll_loop, fd, prfd);
  157. if (fd_entry == NULL) {
  158. return (-1);
  159. }
  160. TAILQ_REMOVE(&poll_loop->fd_list, fd_entry, entries);
  161. if (fd_entry->fd != -1) {
  162. (void)PR_DestroySocketPollFd(fd_entry->prfd);
  163. }
  164. free(fd_entry);
  165. return (0);
  166. }
  167. static struct pr_poll_loop_fd_entry *
  168. pr_poll_loop_find_by_fd(const struct pr_poll_loop *poll_loop, int fd, PRFileDesc *prfd)
  169. {
  170. struct pr_poll_loop_fd_entry *fd_entry;
  171. assert((prfd != NULL && fd == -1) || (fd != -1 && prfd == NULL));
  172. TAILQ_FOREACH(fd_entry, &poll_loop->fd_list, entries) {
  173. if (fd != -1) {
  174. if (fd_entry->fd == fd) {
  175. return (fd_entry);
  176. }
  177. } else {
  178. if (fd_entry->prfd == prfd) {
  179. return (fd_entry);
  180. }
  181. }
  182. }
  183. return (NULL);
  184. }
  185. static
  186. int prepare_poll_array(struct pr_poll_loop *poll_loop)
  187. {
  188. struct pr_poll_loop_fd_entry *fd_entry;
  189. struct pr_poll_loop_fd_entry *fd_entry_next;
  190. struct pr_poll_loop_fd_entry **user_data;
  191. short events;
  192. int res;
  193. PRPollDesc *poll_desc;
  194. struct pr_poll_array *poll_array;
  195. poll_array = &poll_loop->poll_array;
  196. pr_poll_array_clean(poll_array);
  197. /*
  198. * Fill in poll_array
  199. */
  200. fd_entry = TAILQ_FIRST(&poll_loop->fd_list);
  201. while (fd_entry != NULL) {
  202. fd_entry_next = TAILQ_NEXT(fd_entry, entries);
  203. events = fd_entry->events;
  204. if (fd_entry->fd_set_events_cb != NULL || fd_entry->prfd_set_events_cb != NULL) {
  205. if (fd_entry->fd_set_events_cb != NULL) {
  206. res = fd_entry->fd_set_events_cb(fd_entry->fd, &events,
  207. fd_entry->user_data1, fd_entry->user_data2);
  208. } else {
  209. res = fd_entry->prfd_set_events_cb(fd_entry->prfd, &events,
  210. fd_entry->user_data1, fd_entry->user_data2);
  211. }
  212. } else {
  213. /*
  214. * Add entry
  215. */
  216. res = 0;
  217. }
  218. if ((events & ~(POLLIN|POLLOUT)) != 0) {
  219. return (-2);
  220. }
  221. switch (res) {
  222. case 0:
  223. /*
  224. * Add entry
  225. */
  226. if (pr_poll_array_add(poll_array, &poll_desc, (void **)&user_data) < 0) {
  227. return (-1);
  228. }
  229. poll_desc->fd = fd_entry->prfd;
  230. poll_desc->in_flags = poll_events_to_pr_events(events);
  231. *user_data = fd_entry;
  232. break;
  233. case -1:
  234. /*
  235. * Do not add entry
  236. */
  237. break;
  238. case -2:
  239. /*
  240. * -2 = return immediately
  241. */
  242. return (-1);
  243. break;
  244. default:
  245. return (-2);
  246. break;
  247. }
  248. fd_entry = fd_entry_next;
  249. }
  250. pr_poll_array_gc(poll_array);
  251. return (0);
  252. }
  253. /*
  254. * Exported functions
  255. */
  256. void
  257. pr_poll_loop_init(struct pr_poll_loop *poll_loop)
  258. {
  259. memset(poll_loop, 0, sizeof(*poll_loop));
  260. TAILQ_INIT(&(poll_loop->fd_list));
  261. pr_poll_array_init(&poll_loop->poll_array, sizeof(struct pr_poll_loop_fd_entry *));
  262. timer_list_init(&poll_loop->tlist);
  263. }
  264. int
  265. pr_poll_loop_del_fd(struct pr_poll_loop *poll_loop, int fd)
  266. {
  267. return (pr_poll_loop_del_fd_int(poll_loop, fd, NULL));
  268. }
  269. int
  270. pr_poll_loop_del_prfd(struct pr_poll_loop *poll_loop, PRFileDesc *prfd)
  271. {
  272. return (pr_poll_loop_del_fd_int(poll_loop, -1, prfd));
  273. }
  274. int
  275. pr_poll_loop_destroy(struct pr_poll_loop *poll_loop)
  276. {
  277. struct pr_poll_loop_fd_entry *fd_entry;
  278. struct pr_poll_loop_fd_entry *fd_entry_next;
  279. fd_entry = TAILQ_FIRST(&poll_loop->fd_list);
  280. while (fd_entry != NULL) {
  281. fd_entry_next = TAILQ_NEXT(fd_entry, entries);
  282. if (fd_entry->fd != -1) {
  283. (void)PR_DestroySocketPollFd(fd_entry->prfd);
  284. }
  285. free(fd_entry);
  286. fd_entry = fd_entry_next;
  287. }
  288. TAILQ_INIT(&(poll_loop->fd_list));
  289. pr_poll_array_destroy(&poll_loop->poll_array);
  290. timer_list_free(&poll_loop->tlist);
  291. return (0);
  292. }
  293. int
  294. pr_poll_loop_add_fd(struct pr_poll_loop *poll_loop, int fd,
  295. short events, pr_poll_loop_fd_set_events_cb_fn fd_set_events_cb,
  296. pr_poll_loop_fd_read_cb_fn fd_read_cb,
  297. pr_poll_loop_fd_write_cb_fn fd_write_cb,
  298. pr_poll_loop_fd_err_cb_fn fd_err_cb,
  299. void *user_data1, void *user_data2)
  300. {
  301. return (pr_poll_loop_add_fd_int(poll_loop, fd, NULL, events,
  302. fd_set_events_cb, NULL, fd_read_cb, NULL, fd_write_cb, NULL,
  303. fd_err_cb, NULL,
  304. user_data1, user_data2));
  305. }
  306. int
  307. pr_poll_loop_add_prfd(struct pr_poll_loop *poll_loop, PRFileDesc *prfd,
  308. short events, pr_poll_loop_prfd_set_events_cb_fn prfd_set_events_cb,
  309. pr_poll_loop_prfd_read_cb_fn prfd_read_cb,
  310. pr_poll_loop_prfd_read_cb_fn prfd_write_cb,
  311. pr_poll_loop_prfd_err_cb_fn prfd_err_cb,
  312. void *user_data1, void *user_data2)
  313. {
  314. return (pr_poll_loop_add_fd_int(poll_loop, -1, prfd, events,
  315. NULL, prfd_set_events_cb, NULL, prfd_read_cb, NULL, prfd_write_cb,
  316. NULL, prfd_err_cb,
  317. user_data1, user_data2));
  318. }
  319. int
  320. pr_poll_loop_exec(struct pr_poll_loop *poll_loop)
  321. {
  322. PRInt32 poll_res;
  323. struct pr_poll_loop_fd_entry *fd_entry;
  324. struct pr_poll_loop_fd_entry **user_data;
  325. ssize_t i;
  326. int cb_res;
  327. static PRPollDesc *pfds;
  328. int res;
  329. if ((res = prepare_poll_array(poll_loop)) != 0) {
  330. return (res);
  331. }
  332. pfds = poll_loop->poll_array.array;
  333. if ((poll_res = PR_Poll(pfds, pr_poll_array_size(&poll_loop->poll_array),
  334. timer_list_time_to_expire(&poll_loop->tlist))) > 0) {
  335. for (i = 0; i < pr_poll_array_size(&poll_loop->poll_array); i++) {
  336. user_data = pr_poll_array_get_user_data(&poll_loop->poll_array, i);
  337. fd_entry = *user_data;
  338. if (pfds[i].out_flags & PR_POLL_READ &&
  339. (fd_entry->fd_read_cb != NULL || fd_entry->prfd_read_cb != NULL)) {
  340. if (fd_entry->fd_read_cb) {
  341. cb_res = fd_entry->fd_read_cb(fd_entry->fd,
  342. fd_entry->user_data1, fd_entry->user_data2);
  343. } else {
  344. cb_res = fd_entry->prfd_read_cb(fd_entry->prfd,
  345. fd_entry->user_data1, fd_entry->user_data2);
  346. }
  347. if (cb_res != 0) {
  348. return (-1);
  349. }
  350. }
  351. if (pfds[i].out_flags & PR_POLL_WRITE &&
  352. (fd_entry->fd_write_cb != NULL || fd_entry->prfd_write_cb != NULL)) {
  353. if (fd_entry->fd_write_cb) {
  354. cb_res = fd_entry->fd_write_cb(fd_entry->fd,
  355. fd_entry->user_data1, fd_entry->user_data2);
  356. } else {
  357. cb_res = fd_entry->prfd_write_cb(fd_entry->prfd,
  358. fd_entry->user_data1, fd_entry->user_data2);
  359. }
  360. if (cb_res != 0) {
  361. return (-1);
  362. }
  363. }
  364. if ((pfds[i].out_flags & (PR_POLL_ERR|PR_POLL_NVAL|PR_POLL_HUP|PR_POLL_EXCEPT)) &&
  365. !(pfds[i].out_flags & (PR_POLL_READ|PR_POLL_WRITE)) &&
  366. (fd_entry->fd_err_cb != NULL || fd_entry->prfd_err_cb != NULL)) {
  367. if (fd_entry->fd_err_cb) {
  368. cb_res = fd_entry->fd_err_cb(fd_entry->fd,
  369. pr_events_to_poll_events(pfds[i].out_flags),
  370. fd_entry->user_data1, fd_entry->user_data2);
  371. } else {
  372. cb_res = fd_entry->prfd_err_cb(fd_entry->prfd,
  373. pr_events_to_poll_events(pfds[i].out_flags),
  374. fd_entry->user_data1, fd_entry->user_data2);
  375. }
  376. if (cb_res != 0) {
  377. return (-1);
  378. }
  379. }
  380. }
  381. }
  382. timer_list_expire(&poll_loop->tlist);
  383. return (0);
  384. }
  385. struct timer_list *
  386. pr_poll_loop_get_timer_list(struct pr_poll_loop *poll_loop)
  387. {
  388. return (&poll_loop->tlist);
  389. }