pr-poll-loop.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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. if (events & POLLPRI) {
  80. res |= PR_POLL_EXCEPT;
  81. }
  82. return (res);
  83. }
  84. static short
  85. pr_events_to_poll_events(PRInt16 events)
  86. {
  87. short res;
  88. res = 0;
  89. if (events & PR_POLL_READ) {
  90. res |= POLLIN;
  91. }
  92. if (events & PR_POLL_WRITE) {
  93. res |= POLLOUT;
  94. }
  95. if (events & PR_POLL_ERR) {
  96. res |= POLLERR;
  97. }
  98. if (events & PR_POLL_NVAL) {
  99. res |= POLLNVAL;
  100. }
  101. if (events & PR_POLL_HUP) {
  102. res |= POLLHUP;
  103. }
  104. if (events & PR_POLL_EXCEPT) {
  105. res |= POLLPRI;
  106. }
  107. return (res);
  108. }
  109. static int
  110. pr_poll_loop_add_fd_int(struct pr_poll_loop *poll_loop, int fd, PRFileDesc *prfd,
  111. short events, pr_poll_loop_fd_set_events_cb_fn fd_set_events_cb,
  112. pr_poll_loop_prfd_set_events_cb_fn prfd_set_events_cb,
  113. pr_poll_loop_fd_read_cb_fn fd_read_cb, pr_poll_loop_prfd_read_cb_fn prfd_read_cb,
  114. pr_poll_loop_fd_write_cb_fn fd_write_cb, pr_poll_loop_prfd_write_cb_fn prfd_write_cb,
  115. pr_poll_loop_fd_err_cb_fn fd_err_cb, pr_poll_loop_prfd_err_cb_fn prfd_err_cb,
  116. void *user_data1, void *user_data2)
  117. {
  118. struct pr_poll_loop_fd_entry *new_entry;
  119. assert((prfd != NULL && fd == -1) || (fd != -1 && prfd == NULL));
  120. if ((events & ~(POLLIN|POLLOUT|POLLPRI)) != 0) {
  121. return (-1);
  122. }
  123. if (pr_poll_loop_find_by_fd(poll_loop, fd, prfd) != NULL) {
  124. return (-1);
  125. }
  126. new_entry = malloc(sizeof(*new_entry));
  127. if (new_entry == NULL) {
  128. return (-1);
  129. }
  130. memset(new_entry, 0, sizeof(*new_entry));
  131. new_entry->fd = fd;
  132. if (fd != -1) {
  133. new_entry->prfd = PR_CreateSocketPollFd(fd);
  134. if (new_entry->prfd == NULL) {
  135. free(new_entry);
  136. return (-1);
  137. }
  138. } else {
  139. new_entry->prfd = prfd;
  140. }
  141. new_entry->events = events;
  142. new_entry->fd_set_events_cb = fd_set_events_cb;
  143. new_entry->prfd_set_events_cb = prfd_set_events_cb;
  144. new_entry->fd_read_cb = fd_read_cb;
  145. new_entry->prfd_read_cb = prfd_read_cb;
  146. new_entry->fd_write_cb = fd_write_cb;
  147. new_entry->prfd_write_cb = prfd_write_cb;
  148. new_entry->fd_err_cb = fd_err_cb;
  149. new_entry->prfd_err_cb = prfd_err_cb;
  150. new_entry->user_data1 = user_data1;
  151. new_entry->user_data2 = user_data2;
  152. TAILQ_INSERT_TAIL(&poll_loop->fd_list, new_entry, entries);
  153. return (0);
  154. }
  155. static int
  156. pr_poll_loop_del_fd_int(struct pr_poll_loop *poll_loop, int fd, PRFileDesc *prfd)
  157. {
  158. struct pr_poll_loop_fd_entry *fd_entry;
  159. fd_entry = pr_poll_loop_find_by_fd(poll_loop, fd, prfd);
  160. if (fd_entry == NULL) {
  161. return (-1);
  162. }
  163. TAILQ_REMOVE(&poll_loop->fd_list, fd_entry, entries);
  164. if (fd_entry->fd != -1) {
  165. (void)PR_DestroySocketPollFd(fd_entry->prfd);
  166. }
  167. free(fd_entry);
  168. return (0);
  169. }
  170. static struct pr_poll_loop_fd_entry *
  171. pr_poll_loop_find_by_fd(const struct pr_poll_loop *poll_loop, int fd, PRFileDesc *prfd)
  172. {
  173. struct pr_poll_loop_fd_entry *fd_entry;
  174. assert((prfd != NULL && fd == -1) || (fd != -1 && prfd == NULL));
  175. TAILQ_FOREACH(fd_entry, &poll_loop->fd_list, entries) {
  176. if (fd != -1) {
  177. if (fd_entry->fd == fd) {
  178. return (fd_entry);
  179. }
  180. } else {
  181. if (fd_entry->prfd == prfd) {
  182. return (fd_entry);
  183. }
  184. }
  185. }
  186. return (NULL);
  187. }
  188. static
  189. int prepare_poll_array(struct pr_poll_loop *poll_loop)
  190. {
  191. struct pr_poll_loop_fd_entry *fd_entry;
  192. struct pr_poll_loop_fd_entry *fd_entry_next;
  193. struct pr_poll_loop_fd_entry **user_data;
  194. short events;
  195. int res;
  196. PRPollDesc *poll_desc;
  197. struct pr_poll_array *poll_array;
  198. poll_array = &poll_loop->poll_array;
  199. pr_poll_array_clean(poll_array);
  200. /*
  201. * Fill in poll_array
  202. */
  203. fd_entry = TAILQ_FIRST(&poll_loop->fd_list);
  204. while (fd_entry != NULL) {
  205. fd_entry_next = TAILQ_NEXT(fd_entry, entries);
  206. events = fd_entry->events;
  207. if (fd_entry->fd_set_events_cb != NULL || fd_entry->prfd_set_events_cb != NULL) {
  208. if (fd_entry->fd_set_events_cb != NULL) {
  209. res = fd_entry->fd_set_events_cb(fd_entry->fd, &events,
  210. fd_entry->user_data1, fd_entry->user_data2);
  211. } else {
  212. res = fd_entry->prfd_set_events_cb(fd_entry->prfd, &events,
  213. fd_entry->user_data1, fd_entry->user_data2);
  214. }
  215. } else {
  216. /*
  217. * Add entry
  218. */
  219. res = 0;
  220. }
  221. if ((events & ~(POLLIN|POLLOUT|POLLPRI)) != 0) {
  222. return (-2);
  223. }
  224. if (events == 0) {
  225. /*
  226. * Empty events -> do not add entry
  227. */
  228. res = -1;
  229. }
  230. switch (res) {
  231. case 0:
  232. /*
  233. * Add entry
  234. */
  235. if (pr_poll_array_add(poll_array, &poll_desc, (void **)&user_data) < 0) {
  236. return (-1);
  237. }
  238. poll_desc->fd = fd_entry->prfd;
  239. poll_desc->in_flags = poll_events_to_pr_events(events);
  240. *user_data = fd_entry;
  241. break;
  242. case -1:
  243. /*
  244. * Do not add entry
  245. */
  246. break;
  247. case -2:
  248. /*
  249. * -2 = return immediately
  250. */
  251. return (-1);
  252. break;
  253. default:
  254. return (-2);
  255. break;
  256. }
  257. fd_entry = fd_entry_next;
  258. }
  259. pr_poll_array_gc(poll_array);
  260. return (0);
  261. }
  262. /*
  263. * Exported functions
  264. */
  265. void
  266. pr_poll_loop_init(struct pr_poll_loop *poll_loop)
  267. {
  268. memset(poll_loop, 0, sizeof(*poll_loop));
  269. TAILQ_INIT(&(poll_loop->fd_list));
  270. pr_poll_array_init(&poll_loop->poll_array, sizeof(struct pr_poll_loop_fd_entry *));
  271. timer_list_init(&poll_loop->tlist);
  272. }
  273. int
  274. pr_poll_loop_del_fd(struct pr_poll_loop *poll_loop, int fd)
  275. {
  276. return (pr_poll_loop_del_fd_int(poll_loop, fd, NULL));
  277. }
  278. int
  279. pr_poll_loop_del_prfd(struct pr_poll_loop *poll_loop, PRFileDesc *prfd)
  280. {
  281. return (pr_poll_loop_del_fd_int(poll_loop, -1, prfd));
  282. }
  283. int
  284. pr_poll_loop_destroy(struct pr_poll_loop *poll_loop)
  285. {
  286. struct pr_poll_loop_fd_entry *fd_entry;
  287. struct pr_poll_loop_fd_entry *fd_entry_next;
  288. fd_entry = TAILQ_FIRST(&poll_loop->fd_list);
  289. while (fd_entry != NULL) {
  290. fd_entry_next = TAILQ_NEXT(fd_entry, entries);
  291. if (fd_entry->fd != -1) {
  292. (void)PR_DestroySocketPollFd(fd_entry->prfd);
  293. }
  294. free(fd_entry);
  295. fd_entry = fd_entry_next;
  296. }
  297. TAILQ_INIT(&(poll_loop->fd_list));
  298. pr_poll_array_destroy(&poll_loop->poll_array);
  299. timer_list_free(&poll_loop->tlist);
  300. return (0);
  301. }
  302. int
  303. pr_poll_loop_add_fd(struct pr_poll_loop *poll_loop, int fd,
  304. short events, pr_poll_loop_fd_set_events_cb_fn fd_set_events_cb,
  305. pr_poll_loop_fd_read_cb_fn fd_read_cb,
  306. pr_poll_loop_fd_write_cb_fn fd_write_cb,
  307. pr_poll_loop_fd_err_cb_fn fd_err_cb,
  308. void *user_data1, void *user_data2)
  309. {
  310. return (pr_poll_loop_add_fd_int(poll_loop, fd, NULL, events,
  311. fd_set_events_cb, NULL, fd_read_cb, NULL, fd_write_cb, NULL,
  312. fd_err_cb, NULL,
  313. user_data1, user_data2));
  314. }
  315. int
  316. pr_poll_loop_add_prfd(struct pr_poll_loop *poll_loop, PRFileDesc *prfd,
  317. short events, pr_poll_loop_prfd_set_events_cb_fn prfd_set_events_cb,
  318. pr_poll_loop_prfd_read_cb_fn prfd_read_cb,
  319. pr_poll_loop_prfd_read_cb_fn prfd_write_cb,
  320. pr_poll_loop_prfd_err_cb_fn prfd_err_cb,
  321. void *user_data1, void *user_data2)
  322. {
  323. return (pr_poll_loop_add_fd_int(poll_loop, -1, prfd, events,
  324. NULL, prfd_set_events_cb, NULL, prfd_read_cb, NULL, prfd_write_cb,
  325. NULL, prfd_err_cb,
  326. user_data1, user_data2));
  327. }
  328. int
  329. pr_poll_loop_exec(struct pr_poll_loop *poll_loop)
  330. {
  331. PRInt32 poll_res;
  332. struct pr_poll_loop_fd_entry *fd_entry;
  333. struct pr_poll_loop_fd_entry **user_data;
  334. ssize_t i;
  335. int cb_res;
  336. static PRPollDesc *pfds;
  337. int res;
  338. if ((res = prepare_poll_array(poll_loop)) != 0) {
  339. return (res);
  340. }
  341. pfds = poll_loop->poll_array.array;
  342. if ((poll_res = PR_Poll(pfds, pr_poll_array_size(&poll_loop->poll_array),
  343. timer_list_time_to_expire(&poll_loop->tlist))) > 0) {
  344. for (i = 0; i < pr_poll_array_size(&poll_loop->poll_array); i++) {
  345. user_data = pr_poll_array_get_user_data(&poll_loop->poll_array, i);
  346. fd_entry = *user_data;
  347. if (pfds[i].out_flags & PR_POLL_READ &&
  348. (fd_entry->fd_read_cb != NULL || fd_entry->prfd_read_cb != NULL)) {
  349. if (fd_entry->fd_read_cb) {
  350. cb_res = fd_entry->fd_read_cb(fd_entry->fd,
  351. fd_entry->user_data1, fd_entry->user_data2);
  352. } else {
  353. cb_res = fd_entry->prfd_read_cb(fd_entry->prfd, &pfds[i],
  354. fd_entry->user_data1, fd_entry->user_data2);
  355. }
  356. if (cb_res != 0) {
  357. return (-1);
  358. }
  359. }
  360. if (pfds[i].out_flags & PR_POLL_WRITE &&
  361. (fd_entry->fd_write_cb != NULL || fd_entry->prfd_write_cb != NULL)) {
  362. if (fd_entry->fd_write_cb) {
  363. cb_res = fd_entry->fd_write_cb(fd_entry->fd,
  364. fd_entry->user_data1, fd_entry->user_data2);
  365. } else {
  366. cb_res = fd_entry->prfd_write_cb(fd_entry->prfd, &pfds[i],
  367. fd_entry->user_data1, fd_entry->user_data2);
  368. }
  369. if (cb_res != 0) {
  370. return (-1);
  371. }
  372. }
  373. if ((pfds[i].out_flags & (PR_POLL_ERR|PR_POLL_NVAL|PR_POLL_HUP|PR_POLL_EXCEPT)) &&
  374. !(pfds[i].out_flags & (PR_POLL_READ|PR_POLL_WRITE)) &&
  375. (fd_entry->fd_err_cb != NULL || fd_entry->prfd_err_cb != NULL)) {
  376. if (fd_entry->fd_err_cb) {
  377. cb_res = fd_entry->fd_err_cb(fd_entry->fd,
  378. pr_events_to_poll_events(pfds[i].out_flags),
  379. fd_entry->user_data1, fd_entry->user_data2);
  380. } else {
  381. cb_res = fd_entry->prfd_err_cb(fd_entry->prfd,
  382. pr_events_to_poll_events(pfds[i].out_flags),
  383. &pfds[i],
  384. fd_entry->user_data1, fd_entry->user_data2);
  385. }
  386. if (cb_res != 0) {
  387. return (-1);
  388. }
  389. }
  390. }
  391. }
  392. if (poll_res == -1) {
  393. return (-3);
  394. }
  395. timer_list_expire(&poll_loop->tlist);
  396. return (0);
  397. }
  398. struct timer_list *
  399. pr_poll_loop_get_timer_list(struct pr_poll_loop *poll_loop)
  400. {
  401. return (&poll_loop->tlist);
  402. }