pr-poll-loop.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  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(
  61. struct pr_poll_loop *poll_loop, 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 struct pr_poll_loop_pre_poll_cb_entry *pr_poll_loop_find_pre_poll_cb(
  65. const struct pr_poll_loop *poll_loop,
  66. pr_poll_loop_pre_poll_cb_fn pre_poll_cb);
  67. static int prepare_poll_array(struct pr_poll_loop *poll_loop);
  68. /*
  69. * Helper functions definitions
  70. */
  71. static PRInt16
  72. poll_events_to_pr_events(short events)
  73. {
  74. PRInt16 res;
  75. res = 0;
  76. if (events & POLLIN) {
  77. res |= PR_POLL_READ;
  78. }
  79. if (events & POLLOUT) {
  80. res |= PR_POLL_WRITE;
  81. }
  82. if (events & POLLPRI) {
  83. res |= PR_POLL_EXCEPT;
  84. }
  85. return (res);
  86. }
  87. static short
  88. pr_events_to_poll_events(PRInt16 events)
  89. {
  90. short res;
  91. res = 0;
  92. if (events & PR_POLL_READ) {
  93. res |= POLLIN;
  94. }
  95. if (events & PR_POLL_WRITE) {
  96. res |= POLLOUT;
  97. }
  98. if (events & PR_POLL_ERR) {
  99. res |= POLLERR;
  100. }
  101. if (events & PR_POLL_NVAL) {
  102. res |= POLLNVAL;
  103. }
  104. if (events & PR_POLL_HUP) {
  105. res |= POLLHUP;
  106. }
  107. if (events & PR_POLL_EXCEPT) {
  108. res |= POLLPRI;
  109. }
  110. return (res);
  111. }
  112. static int
  113. pr_poll_loop_add_fd_int(struct pr_poll_loop *poll_loop, int fd, PRFileDesc *prfd,
  114. short events, pr_poll_loop_fd_set_events_cb_fn fd_set_events_cb,
  115. pr_poll_loop_prfd_set_events_cb_fn prfd_set_events_cb,
  116. pr_poll_loop_fd_read_cb_fn fd_read_cb, pr_poll_loop_prfd_read_cb_fn prfd_read_cb,
  117. pr_poll_loop_fd_write_cb_fn fd_write_cb, pr_poll_loop_prfd_write_cb_fn prfd_write_cb,
  118. pr_poll_loop_fd_err_cb_fn fd_err_cb, pr_poll_loop_prfd_err_cb_fn prfd_err_cb,
  119. void *user_data1, void *user_data2)
  120. {
  121. struct pr_poll_loop_fd_entry *new_entry;
  122. assert((prfd != NULL && fd == -1) || (fd != -1 && prfd == NULL));
  123. if ((events & ~(POLLIN|POLLOUT|POLLPRI)) != 0) {
  124. return (-1);
  125. }
  126. if (pr_poll_loop_find_by_fd(poll_loop, fd, prfd) != NULL) {
  127. return (-1);
  128. }
  129. new_entry = malloc(sizeof(*new_entry));
  130. if (new_entry == NULL) {
  131. return (-1);
  132. }
  133. memset(new_entry, 0, sizeof(*new_entry));
  134. new_entry->fd = fd;
  135. if (fd != -1) {
  136. new_entry->prfd = PR_CreateSocketPollFd(fd);
  137. if (new_entry->prfd == NULL) {
  138. free(new_entry);
  139. return (-1);
  140. }
  141. } else {
  142. new_entry->prfd = prfd;
  143. }
  144. new_entry->events = events;
  145. new_entry->fd_set_events_cb = fd_set_events_cb;
  146. new_entry->prfd_set_events_cb = prfd_set_events_cb;
  147. new_entry->fd_read_cb = fd_read_cb;
  148. new_entry->prfd_read_cb = prfd_read_cb;
  149. new_entry->fd_write_cb = fd_write_cb;
  150. new_entry->prfd_write_cb = prfd_write_cb;
  151. new_entry->fd_err_cb = fd_err_cb;
  152. new_entry->prfd_err_cb = prfd_err_cb;
  153. new_entry->user_data1 = user_data1;
  154. new_entry->user_data2 = user_data2;
  155. TAILQ_INSERT_TAIL(&poll_loop->fd_list, new_entry, entries);
  156. return (0);
  157. }
  158. static int
  159. pr_poll_loop_del_fd_int(struct pr_poll_loop *poll_loop, int fd, PRFileDesc *prfd)
  160. {
  161. struct pr_poll_loop_fd_entry *fd_entry;
  162. fd_entry = pr_poll_loop_find_by_fd(poll_loop, fd, prfd);
  163. if (fd_entry == NULL) {
  164. return (-1);
  165. }
  166. TAILQ_REMOVE(&poll_loop->fd_list, fd_entry, entries);
  167. if (fd_entry->fd != -1) {
  168. (void)PR_DestroySocketPollFd(fd_entry->prfd);
  169. }
  170. free(fd_entry);
  171. return (0);
  172. }
  173. static struct pr_poll_loop_fd_entry *
  174. pr_poll_loop_find_by_fd(const struct pr_poll_loop *poll_loop, int fd, PRFileDesc *prfd)
  175. {
  176. struct pr_poll_loop_fd_entry *fd_entry;
  177. assert((prfd != NULL && fd == -1) || (fd != -1 && prfd == NULL));
  178. TAILQ_FOREACH(fd_entry, &poll_loop->fd_list, entries) {
  179. if (fd != -1) {
  180. if (fd_entry->fd == fd) {
  181. return (fd_entry);
  182. }
  183. } else {
  184. if (fd_entry->prfd == prfd) {
  185. return (fd_entry);
  186. }
  187. }
  188. }
  189. return (NULL);
  190. }
  191. static struct pr_poll_loop_pre_poll_cb_entry *
  192. pr_poll_loop_find_pre_poll_cb(const struct pr_poll_loop *poll_loop,
  193. pr_poll_loop_pre_poll_cb_fn pre_poll_cb)
  194. {
  195. struct pr_poll_loop_pre_poll_cb_entry *pre_poll_cb_entry;
  196. TAILQ_FOREACH(pre_poll_cb_entry, &poll_loop->pre_poll_cb_list, entries) {
  197. if (pre_poll_cb_entry->pre_poll_cb == pre_poll_cb) {
  198. return (pre_poll_cb_entry);
  199. }
  200. }
  201. return (NULL);
  202. }
  203. static
  204. int prepare_poll_array(struct pr_poll_loop *poll_loop)
  205. {
  206. struct pr_poll_loop_fd_entry *fd_entry;
  207. struct pr_poll_loop_fd_entry *fd_entry_next;
  208. struct pr_poll_loop_pre_poll_cb_entry *pre_poll_cb_entry;
  209. struct pr_poll_loop_pre_poll_cb_entry *pre_poll_cb_entry_next;
  210. struct pr_poll_loop_fd_entry **user_data;
  211. short events;
  212. int res;
  213. PRPollDesc *poll_desc;
  214. struct pr_poll_array *poll_array;
  215. poll_array = &poll_loop->poll_array;
  216. pr_poll_array_clean(poll_array);
  217. /*
  218. * Call pre poll callbacks
  219. */
  220. pre_poll_cb_entry = TAILQ_FIRST(&poll_loop->pre_poll_cb_list);
  221. while (pre_poll_cb_entry != NULL) {
  222. pre_poll_cb_entry_next = TAILQ_NEXT(pre_poll_cb_entry, entries);
  223. if (pre_poll_cb_entry->pre_poll_cb != NULL) {
  224. res = pre_poll_cb_entry->pre_poll_cb(pre_poll_cb_entry->user_data1,
  225. pre_poll_cb_entry->user_data2);
  226. } else {
  227. res = 0;
  228. }
  229. switch (res) {
  230. case 0:
  231. /*
  232. * Continue
  233. */
  234. break;
  235. case -1:
  236. /*
  237. * return immediately
  238. */
  239. return (-1);
  240. break;
  241. default:
  242. return (-2);
  243. break;
  244. }
  245. pre_poll_cb_entry = pre_poll_cb_entry_next;
  246. }
  247. /*
  248. * Fill in poll_array
  249. */
  250. fd_entry = TAILQ_FIRST(&poll_loop->fd_list);
  251. while (fd_entry != NULL) {
  252. fd_entry_next = TAILQ_NEXT(fd_entry, entries);
  253. events = fd_entry->events;
  254. if (fd_entry->fd_set_events_cb != NULL || fd_entry->prfd_set_events_cb != NULL) {
  255. if (fd_entry->fd_set_events_cb != NULL) {
  256. res = fd_entry->fd_set_events_cb(fd_entry->fd, &events,
  257. fd_entry->user_data1, fd_entry->user_data2);
  258. } else {
  259. res = fd_entry->prfd_set_events_cb(fd_entry->prfd, &events,
  260. fd_entry->user_data1, fd_entry->user_data2);
  261. }
  262. } else {
  263. /*
  264. * Add entry
  265. */
  266. res = 0;
  267. }
  268. if ((events & ~(POLLIN|POLLOUT|POLLPRI)) != 0) {
  269. return (-2);
  270. }
  271. if (res == 0 && events == 0) {
  272. /*
  273. * Empty events -> do not add entry
  274. */
  275. res = -1;
  276. }
  277. switch (res) {
  278. case 0:
  279. /*
  280. * Add entry
  281. */
  282. if (pr_poll_array_add(poll_array, &poll_desc, (void **)&user_data) < 0) {
  283. return (-1);
  284. }
  285. poll_desc->fd = fd_entry->prfd;
  286. poll_desc->in_flags = poll_events_to_pr_events(events);
  287. *user_data = fd_entry;
  288. break;
  289. case -1:
  290. /*
  291. * Do not add entry
  292. */
  293. break;
  294. case -2:
  295. /*
  296. * -2 = return immediately
  297. */
  298. return (-1);
  299. break;
  300. default:
  301. return (-2);
  302. break;
  303. }
  304. fd_entry = fd_entry_next;
  305. }
  306. pr_poll_array_gc(poll_array);
  307. return (0);
  308. }
  309. /*
  310. * Exported functions
  311. */
  312. void
  313. pr_poll_loop_init(struct pr_poll_loop *poll_loop)
  314. {
  315. memset(poll_loop, 0, sizeof(*poll_loop));
  316. TAILQ_INIT(&(poll_loop->fd_list));
  317. TAILQ_INIT(&(poll_loop->pre_poll_cb_list));
  318. pr_poll_array_init(&poll_loop->poll_array, sizeof(struct pr_poll_loop_fd_entry *));
  319. timer_list_init(&poll_loop->tlist);
  320. }
  321. int
  322. pr_poll_loop_del_fd(struct pr_poll_loop *poll_loop, int fd)
  323. {
  324. return (pr_poll_loop_del_fd_int(poll_loop, fd, NULL));
  325. }
  326. int
  327. pr_poll_loop_del_pre_poll_cb(struct pr_poll_loop *poll_loop,
  328. pr_poll_loop_pre_poll_cb_fn pre_poll_cb)
  329. {
  330. struct pr_poll_loop_pre_poll_cb_entry *pre_poll_cb_entry;
  331. pre_poll_cb_entry = pr_poll_loop_find_pre_poll_cb(poll_loop, pre_poll_cb);
  332. if (pre_poll_cb_entry == NULL) {
  333. return (-1);
  334. }
  335. TAILQ_REMOVE(&poll_loop->pre_poll_cb_list, pre_poll_cb_entry, entries);
  336. free(pre_poll_cb_entry);
  337. return (0);
  338. }
  339. int
  340. pr_poll_loop_del_prfd(struct pr_poll_loop *poll_loop, PRFileDesc *prfd)
  341. {
  342. return (pr_poll_loop_del_fd_int(poll_loop, -1, prfd));
  343. }
  344. int
  345. pr_poll_loop_destroy(struct pr_poll_loop *poll_loop)
  346. {
  347. struct pr_poll_loop_fd_entry *fd_entry;
  348. struct pr_poll_loop_fd_entry *fd_entry_next;
  349. struct pr_poll_loop_pre_poll_cb_entry *pre_poll_cb_entry;
  350. struct pr_poll_loop_pre_poll_cb_entry *pre_poll_cb_entry_next;
  351. fd_entry = TAILQ_FIRST(&poll_loop->fd_list);
  352. while (fd_entry != NULL) {
  353. fd_entry_next = TAILQ_NEXT(fd_entry, entries);
  354. if (fd_entry->fd != -1) {
  355. (void)PR_DestroySocketPollFd(fd_entry->prfd);
  356. }
  357. free(fd_entry);
  358. fd_entry = fd_entry_next;
  359. }
  360. TAILQ_INIT(&(poll_loop->fd_list));
  361. pre_poll_cb_entry = TAILQ_FIRST(&poll_loop->pre_poll_cb_list);
  362. while (pre_poll_cb_entry != NULL) {
  363. pre_poll_cb_entry_next = TAILQ_NEXT(pre_poll_cb_entry, entries);
  364. free(pre_poll_cb_entry);
  365. pre_poll_cb_entry = pre_poll_cb_entry_next;
  366. }
  367. TAILQ_INIT(&(poll_loop->pre_poll_cb_list));
  368. pr_poll_array_destroy(&poll_loop->poll_array);
  369. timer_list_free(&poll_loop->tlist);
  370. return (0);
  371. }
  372. int
  373. pr_poll_loop_add_fd(struct pr_poll_loop *poll_loop, int fd,
  374. short events, pr_poll_loop_fd_set_events_cb_fn fd_set_events_cb,
  375. pr_poll_loop_fd_read_cb_fn fd_read_cb,
  376. pr_poll_loop_fd_write_cb_fn fd_write_cb,
  377. pr_poll_loop_fd_err_cb_fn fd_err_cb,
  378. void *user_data1, void *user_data2)
  379. {
  380. return (pr_poll_loop_add_fd_int(poll_loop, fd, NULL, events,
  381. fd_set_events_cb, NULL, fd_read_cb, NULL, fd_write_cb, NULL,
  382. fd_err_cb, NULL,
  383. user_data1, user_data2));
  384. }
  385. int
  386. pr_poll_loop_add_pre_poll_cb(struct pr_poll_loop *poll_loop,
  387. pr_poll_loop_pre_poll_cb_fn pre_poll_cb,
  388. void *user_data1, void *user_data2)
  389. {
  390. struct pr_poll_loop_pre_poll_cb_entry *new_entry;
  391. if (pr_poll_loop_find_pre_poll_cb(poll_loop, pre_poll_cb) != NULL) {
  392. return (-1);
  393. }
  394. new_entry = malloc(sizeof(*new_entry));
  395. if (new_entry == NULL) {
  396. return (-1);
  397. }
  398. memset(new_entry, 0, sizeof(*new_entry));
  399. new_entry->pre_poll_cb = pre_poll_cb;
  400. new_entry->user_data1 = user_data1;
  401. new_entry->user_data2 = user_data2;
  402. TAILQ_INSERT_TAIL(&poll_loop->pre_poll_cb_list, new_entry, entries);
  403. return (0);
  404. }
  405. int
  406. pr_poll_loop_add_prfd(struct pr_poll_loop *poll_loop, PRFileDesc *prfd,
  407. short events, pr_poll_loop_prfd_set_events_cb_fn prfd_set_events_cb,
  408. pr_poll_loop_prfd_read_cb_fn prfd_read_cb,
  409. pr_poll_loop_prfd_read_cb_fn prfd_write_cb,
  410. pr_poll_loop_prfd_err_cb_fn prfd_err_cb,
  411. void *user_data1, void *user_data2)
  412. {
  413. return (pr_poll_loop_add_fd_int(poll_loop, -1, prfd, events,
  414. NULL, prfd_set_events_cb, NULL, prfd_read_cb, NULL, prfd_write_cb,
  415. NULL, prfd_err_cb,
  416. user_data1, user_data2));
  417. }
  418. int
  419. pr_poll_loop_exec(struct pr_poll_loop *poll_loop)
  420. {
  421. PRInt32 poll_res;
  422. struct pr_poll_loop_fd_entry *fd_entry;
  423. struct pr_poll_loop_fd_entry **user_data;
  424. ssize_t i;
  425. int cb_res;
  426. static PRPollDesc *pfds;
  427. int res;
  428. if ((res = prepare_poll_array(poll_loop)) != 0) {
  429. return (res);
  430. }
  431. pfds = poll_loop->poll_array.array;
  432. if ((poll_res = PR_Poll(pfds, pr_poll_array_size(&poll_loop->poll_array),
  433. timer_list_time_to_expire(&poll_loop->tlist))) > 0) {
  434. for (i = 0; i < pr_poll_array_size(&poll_loop->poll_array); i++) {
  435. user_data = pr_poll_array_get_user_data(&poll_loop->poll_array, i);
  436. fd_entry = *user_data;
  437. if (pfds[i].out_flags & PR_POLL_READ &&
  438. (fd_entry->fd_read_cb != NULL || fd_entry->prfd_read_cb != NULL)) {
  439. if (fd_entry->fd_read_cb) {
  440. cb_res = fd_entry->fd_read_cb(fd_entry->fd,
  441. fd_entry->user_data1, fd_entry->user_data2);
  442. } else {
  443. cb_res = fd_entry->prfd_read_cb(fd_entry->prfd, &pfds[i],
  444. fd_entry->user_data1, fd_entry->user_data2);
  445. }
  446. if (cb_res != 0) {
  447. return (-1);
  448. }
  449. }
  450. if (pfds[i].out_flags & PR_POLL_WRITE &&
  451. (fd_entry->fd_write_cb != NULL || fd_entry->prfd_write_cb != NULL)) {
  452. if (fd_entry->fd_write_cb) {
  453. cb_res = fd_entry->fd_write_cb(fd_entry->fd,
  454. fd_entry->user_data1, fd_entry->user_data2);
  455. } else {
  456. cb_res = fd_entry->prfd_write_cb(fd_entry->prfd, &pfds[i],
  457. fd_entry->user_data1, fd_entry->user_data2);
  458. }
  459. if (cb_res != 0) {
  460. return (-1);
  461. }
  462. }
  463. if ((pfds[i].out_flags & (PR_POLL_ERR|PR_POLL_NVAL|PR_POLL_HUP|PR_POLL_EXCEPT)) &&
  464. !(pfds[i].out_flags & (PR_POLL_READ|PR_POLL_WRITE)) &&
  465. (fd_entry->fd_err_cb != NULL || fd_entry->prfd_err_cb != NULL)) {
  466. if (fd_entry->fd_err_cb) {
  467. cb_res = fd_entry->fd_err_cb(fd_entry->fd,
  468. pr_events_to_poll_events(pfds[i].out_flags),
  469. fd_entry->user_data1, fd_entry->user_data2);
  470. } else {
  471. cb_res = fd_entry->prfd_err_cb(fd_entry->prfd,
  472. pr_events_to_poll_events(pfds[i].out_flags),
  473. &pfds[i],
  474. fd_entry->user_data1, fd_entry->user_data2);
  475. }
  476. if (cb_res != 0) {
  477. return (-1);
  478. }
  479. }
  480. }
  481. }
  482. if (poll_res == -1) {
  483. return (-3);
  484. }
  485. timer_list_expire(&poll_loop->tlist);
  486. return (0);
  487. }
  488. struct timer_list *
  489. pr_poll_loop_get_timer_list(struct pr_poll_loop *poll_loop)
  490. {
  491. return (&poll_loop->tlist);
  492. }