pr-poll-loop.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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. if (events == 0) {
  222. /*
  223. * Empty events -> do not add entry
  224. */
  225. res = -1;
  226. }
  227. switch (res) {
  228. case 0:
  229. /*
  230. * Add entry
  231. */
  232. if (pr_poll_array_add(poll_array, &poll_desc, (void **)&user_data) < 0) {
  233. return (-1);
  234. }
  235. poll_desc->fd = fd_entry->prfd;
  236. poll_desc->in_flags = poll_events_to_pr_events(events);
  237. *user_data = fd_entry;
  238. break;
  239. case -1:
  240. /*
  241. * Do not add entry
  242. */
  243. break;
  244. case -2:
  245. /*
  246. * -2 = return immediately
  247. */
  248. return (-1);
  249. break;
  250. default:
  251. return (-2);
  252. break;
  253. }
  254. fd_entry = fd_entry_next;
  255. }
  256. pr_poll_array_gc(poll_array);
  257. return (0);
  258. }
  259. /*
  260. * Exported functions
  261. */
  262. void
  263. pr_poll_loop_init(struct pr_poll_loop *poll_loop)
  264. {
  265. memset(poll_loop, 0, sizeof(*poll_loop));
  266. TAILQ_INIT(&(poll_loop->fd_list));
  267. pr_poll_array_init(&poll_loop->poll_array, sizeof(struct pr_poll_loop_fd_entry *));
  268. timer_list_init(&poll_loop->tlist);
  269. }
  270. int
  271. pr_poll_loop_del_fd(struct pr_poll_loop *poll_loop, int fd)
  272. {
  273. return (pr_poll_loop_del_fd_int(poll_loop, fd, NULL));
  274. }
  275. int
  276. pr_poll_loop_del_prfd(struct pr_poll_loop *poll_loop, PRFileDesc *prfd)
  277. {
  278. return (pr_poll_loop_del_fd_int(poll_loop, -1, prfd));
  279. }
  280. int
  281. pr_poll_loop_destroy(struct pr_poll_loop *poll_loop)
  282. {
  283. struct pr_poll_loop_fd_entry *fd_entry;
  284. struct pr_poll_loop_fd_entry *fd_entry_next;
  285. fd_entry = TAILQ_FIRST(&poll_loop->fd_list);
  286. while (fd_entry != NULL) {
  287. fd_entry_next = TAILQ_NEXT(fd_entry, entries);
  288. if (fd_entry->fd != -1) {
  289. (void)PR_DestroySocketPollFd(fd_entry->prfd);
  290. }
  291. free(fd_entry);
  292. fd_entry = fd_entry_next;
  293. }
  294. TAILQ_INIT(&(poll_loop->fd_list));
  295. pr_poll_array_destroy(&poll_loop->poll_array);
  296. timer_list_free(&poll_loop->tlist);
  297. return (0);
  298. }
  299. int
  300. pr_poll_loop_add_fd(struct pr_poll_loop *poll_loop, int fd,
  301. short events, pr_poll_loop_fd_set_events_cb_fn fd_set_events_cb,
  302. pr_poll_loop_fd_read_cb_fn fd_read_cb,
  303. pr_poll_loop_fd_write_cb_fn fd_write_cb,
  304. pr_poll_loop_fd_err_cb_fn fd_err_cb,
  305. void *user_data1, void *user_data2)
  306. {
  307. return (pr_poll_loop_add_fd_int(poll_loop, fd, NULL, events,
  308. fd_set_events_cb, NULL, fd_read_cb, NULL, fd_write_cb, NULL,
  309. fd_err_cb, NULL,
  310. user_data1, user_data2));
  311. }
  312. int
  313. pr_poll_loop_add_prfd(struct pr_poll_loop *poll_loop, PRFileDesc *prfd,
  314. short events, pr_poll_loop_prfd_set_events_cb_fn prfd_set_events_cb,
  315. pr_poll_loop_prfd_read_cb_fn prfd_read_cb,
  316. pr_poll_loop_prfd_read_cb_fn prfd_write_cb,
  317. pr_poll_loop_prfd_err_cb_fn prfd_err_cb,
  318. void *user_data1, void *user_data2)
  319. {
  320. return (pr_poll_loop_add_fd_int(poll_loop, -1, prfd, events,
  321. NULL, prfd_set_events_cb, NULL, prfd_read_cb, NULL, prfd_write_cb,
  322. NULL, prfd_err_cb,
  323. user_data1, user_data2));
  324. }
  325. int
  326. pr_poll_loop_exec(struct pr_poll_loop *poll_loop)
  327. {
  328. PRInt32 poll_res;
  329. struct pr_poll_loop_fd_entry *fd_entry;
  330. struct pr_poll_loop_fd_entry **user_data;
  331. ssize_t i;
  332. int cb_res;
  333. static PRPollDesc *pfds;
  334. int res;
  335. if ((res = prepare_poll_array(poll_loop)) != 0) {
  336. return (res);
  337. }
  338. pfds = poll_loop->poll_array.array;
  339. if ((poll_res = PR_Poll(pfds, pr_poll_array_size(&poll_loop->poll_array),
  340. timer_list_time_to_expire(&poll_loop->tlist))) > 0) {
  341. for (i = 0; i < pr_poll_array_size(&poll_loop->poll_array); i++) {
  342. user_data = pr_poll_array_get_user_data(&poll_loop->poll_array, i);
  343. fd_entry = *user_data;
  344. if (pfds[i].out_flags & PR_POLL_READ &&
  345. (fd_entry->fd_read_cb != NULL || fd_entry->prfd_read_cb != NULL)) {
  346. if (fd_entry->fd_read_cb) {
  347. cb_res = fd_entry->fd_read_cb(fd_entry->fd,
  348. fd_entry->user_data1, fd_entry->user_data2);
  349. } else {
  350. cb_res = fd_entry->prfd_read_cb(fd_entry->prfd,
  351. fd_entry->user_data1, fd_entry->user_data2);
  352. }
  353. if (cb_res != 0) {
  354. return (-1);
  355. }
  356. }
  357. if (pfds[i].out_flags & PR_POLL_WRITE &&
  358. (fd_entry->fd_write_cb != NULL || fd_entry->prfd_write_cb != NULL)) {
  359. if (fd_entry->fd_write_cb) {
  360. cb_res = fd_entry->fd_write_cb(fd_entry->fd,
  361. fd_entry->user_data1, fd_entry->user_data2);
  362. } else {
  363. cb_res = fd_entry->prfd_write_cb(fd_entry->prfd,
  364. fd_entry->user_data1, fd_entry->user_data2);
  365. }
  366. if (cb_res != 0) {
  367. return (-1);
  368. }
  369. }
  370. if ((pfds[i].out_flags & (PR_POLL_ERR|PR_POLL_NVAL|PR_POLL_HUP|PR_POLL_EXCEPT)) &&
  371. !(pfds[i].out_flags & (PR_POLL_READ|PR_POLL_WRITE)) &&
  372. (fd_entry->fd_err_cb != NULL || fd_entry->prfd_err_cb != NULL)) {
  373. if (fd_entry->fd_err_cb) {
  374. cb_res = fd_entry->fd_err_cb(fd_entry->fd,
  375. pr_events_to_poll_events(pfds[i].out_flags),
  376. fd_entry->user_data1, fd_entry->user_data2);
  377. } else {
  378. cb_res = fd_entry->prfd_err_cb(fd_entry->prfd,
  379. pr_events_to_poll_events(pfds[i].out_flags),
  380. fd_entry->user_data1, fd_entry->user_data2);
  381. }
  382. if (cb_res != 0) {
  383. return (-1);
  384. }
  385. }
  386. }
  387. }
  388. if (poll_res == -1) {
  389. return (-3);
  390. }
  391. timer_list_expire(&poll_loop->tlist);
  392. return (0);
  393. }
  394. struct timer_list *
  395. pr_poll_loop_get_timer_list(struct pr_poll_loop *poll_loop)
  396. {
  397. return (&poll_loop->tlist);
  398. }