coropoll.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /*
  2. * Copyright (c) 2003-2004 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2009 Red Hat, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Author: Steven Dake (sdake@redhat.com)
  8. *
  9. * This software licensed under BSD license, the text of which follows:
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. * - Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  20. * contributors may be used to endorse or promote products derived from this
  21. * software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  33. * THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. #include <config.h>
  36. #include <errno.h>
  37. #include <pthread.h>
  38. #include <sys/poll.h>
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #include <stdio.h>
  42. #include <unistd.h>
  43. #include <corosync/hdb.h>
  44. #include <corosync/totem/coropoll.h>
  45. #include <corosync/list.h>
  46. #include "tlist.h"
  47. typedef int (*dispatch_fn_t) (hdb_handle_t hdb_handle, int fd, int revents, void *data);
  48. struct poll_entry {
  49. struct pollfd ufd;
  50. dispatch_fn_t dispatch_fn;
  51. void *data;
  52. };
  53. struct poll_instance {
  54. struct poll_entry *poll_entries;
  55. struct pollfd *ufds;
  56. int poll_entry_count;
  57. struct timerlist timerlist;
  58. int stop_requested;
  59. int pipefds[2];
  60. };
  61. DECLARE_HDB_DATABASE (poll_instance_database,NULL);
  62. static int dummy_dispatch_fn (hdb_handle_t handle, int fd, int revents, void *data) {
  63. return (0);
  64. }
  65. hdb_handle_t poll_create (void)
  66. {
  67. hdb_handle_t handle;
  68. struct poll_instance *poll_instance;
  69. unsigned int res;
  70. res = hdb_handle_create (&poll_instance_database,
  71. sizeof (struct poll_instance), &handle);
  72. if (res != 0) {
  73. goto error_exit;
  74. }
  75. res = hdb_handle_get (&poll_instance_database, handle,
  76. (void *)&poll_instance);
  77. if (res != 0) {
  78. goto error_destroy;
  79. }
  80. poll_instance->poll_entries = 0;
  81. poll_instance->ufds = 0;
  82. poll_instance->poll_entry_count = 0;
  83. poll_instance->stop_requested = 0;
  84. timerlist_init (&poll_instance->timerlist);
  85. res = pipe (poll_instance->pipefds);
  86. if (res != 0) {
  87. goto error_destroy;
  88. }
  89. /*
  90. * Allow changes in modify to propogate into new poll instance
  91. */
  92. res = poll_dispatch_add (
  93. handle,
  94. poll_instance->pipefds[0],
  95. POLLIN,
  96. NULL,
  97. dummy_dispatch_fn);
  98. if (res != 0) {
  99. goto error_destroy;
  100. }
  101. return (handle);
  102. error_destroy:
  103. hdb_handle_destroy (&poll_instance_database, handle);
  104. error_exit:
  105. return (-1);
  106. }
  107. int poll_destroy (hdb_handle_t handle)
  108. {
  109. struct poll_instance *poll_instance;
  110. int res = 0;
  111. res = hdb_handle_get (&poll_instance_database, handle,
  112. (void *)&poll_instance);
  113. if (res != 0) {
  114. res = -ENOENT;
  115. goto error_exit;
  116. }
  117. free (poll_instance->poll_entries);
  118. free (poll_instance->ufds);
  119. hdb_handle_destroy (&poll_instance_database, handle);
  120. hdb_handle_put (&poll_instance_database, handle);
  121. error_exit:
  122. return (res);
  123. }
  124. int poll_dispatch_add (
  125. hdb_handle_t handle,
  126. int fd,
  127. int events,
  128. void *data,
  129. int (*dispatch_fn) (
  130. hdb_handle_t hdb_handle_t,
  131. int fd,
  132. int revents,
  133. void *data))
  134. {
  135. struct poll_instance *poll_instance;
  136. struct poll_entry *poll_entries;
  137. struct pollfd *ufds;
  138. int found = 0;
  139. int install_pos;
  140. int res = 0;
  141. res = hdb_handle_get (&poll_instance_database, handle,
  142. (void *)&poll_instance);
  143. if (res != 0) {
  144. res = -ENOENT;
  145. goto error_exit;
  146. }
  147. for (found = 0, install_pos = 0; install_pos < poll_instance->poll_entry_count; install_pos++) {
  148. if (poll_instance->poll_entries[install_pos].ufd.fd == -1) {
  149. found = 1;
  150. break;
  151. }
  152. }
  153. if (found == 0) {
  154. /*
  155. * Grow pollfd list
  156. */
  157. poll_entries = (struct poll_entry *)realloc (poll_instance->poll_entries,
  158. (poll_instance->poll_entry_count + 1) *
  159. sizeof (struct poll_entry));
  160. if (poll_entries == NULL) {
  161. res = -ENOMEM;
  162. goto error_put;
  163. }
  164. poll_instance->poll_entries = poll_entries;
  165. ufds = (struct pollfd *)realloc (poll_instance->ufds,
  166. (poll_instance->poll_entry_count + 1) *
  167. sizeof (struct pollfd));
  168. if (ufds == NULL) {
  169. res = -ENOMEM;
  170. goto error_put;
  171. }
  172. poll_instance->ufds = ufds;
  173. poll_instance->poll_entry_count += 1;
  174. install_pos = poll_instance->poll_entry_count - 1;
  175. }
  176. /*
  177. * Install new dispatch handler
  178. */
  179. poll_instance->poll_entries[install_pos].ufd.fd = fd;
  180. poll_instance->poll_entries[install_pos].ufd.events = events;
  181. poll_instance->poll_entries[install_pos].ufd.revents = 0;
  182. poll_instance->poll_entries[install_pos].dispatch_fn = dispatch_fn;
  183. poll_instance->poll_entries[install_pos].data = data;
  184. error_put:
  185. hdb_handle_put (&poll_instance_database, handle);
  186. error_exit:
  187. return (res);
  188. }
  189. int poll_dispatch_modify (
  190. hdb_handle_t handle,
  191. int fd,
  192. int events,
  193. int (*dispatch_fn) (
  194. hdb_handle_t hdb_handle_t,
  195. int fd,
  196. int revents,
  197. void *data))
  198. {
  199. struct poll_instance *poll_instance;
  200. int i;
  201. int res = 0;
  202. res = hdb_handle_get (&poll_instance_database, handle,
  203. (void *)&poll_instance);
  204. if (res != 0) {
  205. res = -ENOENT;
  206. goto error_exit;
  207. }
  208. /*
  209. * Find file descriptor to modify events and dispatch function
  210. */
  211. for (i = 0; i < poll_instance->poll_entry_count; i++) {
  212. if (poll_instance->poll_entries[i].ufd.fd == fd) {
  213. int change_notify = 0;
  214. if (poll_instance->poll_entries[i].ufd.events != events) {
  215. change_notify = 1;
  216. }
  217. poll_instance->poll_entries[i].ufd.events = events;
  218. poll_instance->poll_entries[i].dispatch_fn = dispatch_fn;
  219. if (change_notify) {
  220. char buf;
  221. write (poll_instance->pipefds[1], &buf, 1);
  222. }
  223. goto error_put;
  224. }
  225. }
  226. res = -EBADF;
  227. error_put:
  228. hdb_handle_put (&poll_instance_database, handle);
  229. error_exit:
  230. return (res);
  231. }
  232. int poll_dispatch_delete (
  233. hdb_handle_t handle,
  234. int fd)
  235. {
  236. struct poll_instance *poll_instance;
  237. int i;
  238. int res = 0;
  239. res = hdb_handle_get (&poll_instance_database, handle,
  240. (void *)&poll_instance);
  241. if (res != 0) {
  242. res = -ENOENT;
  243. goto error_exit;
  244. }
  245. /*
  246. * Find dispatch fd to delete
  247. */
  248. res = -EBADF;
  249. for (i = 0; i < poll_instance->poll_entry_count; i++) {
  250. if (poll_instance->poll_entries[i].ufd.fd == fd) {
  251. poll_instance->ufds[i].fd = -1;
  252. poll_instance->poll_entries[i].ufd.fd = -1;
  253. poll_instance->poll_entries[i].ufd.revents = 0;
  254. res = 0;
  255. break;
  256. }
  257. }
  258. hdb_handle_put (&poll_instance_database, handle);
  259. error_exit:
  260. return (res);
  261. }
  262. int poll_timer_add (
  263. hdb_handle_t handle,
  264. int msec_duration, void *data,
  265. void (*timer_fn) (void *data),
  266. poll_timer_handle *timer_handle_out)
  267. {
  268. struct poll_instance *poll_instance;
  269. int res = 0;
  270. if (timer_handle_out == NULL) {
  271. res = -ENOENT;
  272. goto error_exit;
  273. }
  274. res = hdb_handle_get (&poll_instance_database, handle,
  275. (void *)&poll_instance);
  276. if (res != 0) {
  277. res = -ENOENT;
  278. goto error_exit;
  279. }
  280. timerlist_add_duration (&poll_instance->timerlist,
  281. timer_fn, data, ((unsigned long long)msec_duration) * 1000000ULL, timer_handle_out);
  282. hdb_handle_put (&poll_instance_database, handle);
  283. error_exit:
  284. return (res);
  285. }
  286. int poll_timer_delete (
  287. hdb_handle_t handle,
  288. poll_timer_handle th)
  289. {
  290. struct poll_instance *poll_instance;
  291. int res = 0;
  292. if (th == 0) {
  293. return (0);
  294. }
  295. res = hdb_handle_get (&poll_instance_database, handle,
  296. (void *)&poll_instance);
  297. if (res != 0) {
  298. res = -ENOENT;
  299. goto error_exit;
  300. }
  301. timerlist_del (&poll_instance->timerlist, (void *)th);
  302. hdb_handle_put (&poll_instance_database, handle);
  303. error_exit:
  304. return (res);
  305. }
  306. int poll_stop (
  307. hdb_handle_t handle)
  308. {
  309. struct poll_instance *poll_instance;
  310. unsigned int res;
  311. res = hdb_handle_get (&poll_instance_database, handle,
  312. (void *)&poll_instance);
  313. if (res != 0) {
  314. res = -ENOENT;
  315. goto error_exit;
  316. }
  317. poll_instance->stop_requested = 1;
  318. hdb_handle_put (&poll_instance_database, handle);
  319. error_exit:
  320. return (res);
  321. }
  322. int poll_run (
  323. hdb_handle_t handle)
  324. {
  325. struct poll_instance *poll_instance;
  326. int i;
  327. unsigned long long expire_timeout_msec = -1;
  328. int res;
  329. int poll_entry_count;
  330. res = hdb_handle_get (&poll_instance_database, handle,
  331. (void *)&poll_instance);
  332. if (res != 0) {
  333. goto error_exit;
  334. }
  335. for (;;) {
  336. rebuild_poll:
  337. for (i = 0; i < poll_instance->poll_entry_count; i++) {
  338. memcpy (&poll_instance->ufds[i],
  339. &poll_instance->poll_entries[i].ufd,
  340. sizeof (struct pollfd));
  341. }
  342. expire_timeout_msec = timerlist_msec_duration_to_expire (&poll_instance->timerlist);
  343. if (expire_timeout_msec != -1 && expire_timeout_msec > 0xFFFFFFFF) {
  344. expire_timeout_msec = 0xFFFFFFFE;
  345. }
  346. retry_poll:
  347. res = poll (poll_instance->ufds,
  348. poll_instance->poll_entry_count, expire_timeout_msec);
  349. if (poll_instance->stop_requested) {
  350. return (0);
  351. }
  352. if (errno == EINTR && res == -1) {
  353. goto retry_poll;
  354. } else
  355. if (res == -1) {
  356. goto error_exit;
  357. }
  358. if (poll_instance->ufds[0].revents) {
  359. char buf;
  360. read (poll_instance->ufds[0].fd, &buf, 1);
  361. goto rebuild_poll;
  362. }
  363. poll_entry_count = poll_instance->poll_entry_count;
  364. for (i = 0; i < poll_entry_count; i++) {
  365. if (poll_instance->ufds[i].fd != -1 &&
  366. poll_instance->ufds[i].revents) {
  367. res = poll_instance->poll_entries[i].dispatch_fn (handle,
  368. poll_instance->ufds[i].fd,
  369. poll_instance->ufds[i].revents,
  370. poll_instance->poll_entries[i].data);
  371. /*
  372. * Remove dispatch functions that return -1
  373. */
  374. if (res == -1) {
  375. poll_instance->poll_entries[i].ufd.fd = -1; /* empty entry */
  376. }
  377. }
  378. }
  379. timerlist_expire (&poll_instance->timerlist);
  380. } /* for (;;) */
  381. hdb_handle_put (&poll_instance_database, handle);
  382. error_exit:
  383. return (-1);
  384. }
  385. #ifdef COMPILE_OUT
  386. void poll_print_state (
  387. hdb_handle_t handle,
  388. int fd)
  389. {
  390. struct poll_instance *poll_instance;
  391. int i;
  392. int res = 0;
  393. res = hdb_handle_get (&poll_instance_database, handle,
  394. (void *)&poll_instance);
  395. if (res != 0) {
  396. res = -ENOENT;
  397. exit (1);
  398. }
  399. for (i = 0; i < poll_instance->poll_entry_count; i++) {
  400. if (poll_instance->poll_entries[i].ufd.fd == fd) {
  401. printf ("fd %d\n", poll_instance->poll_entries[i].ufd.fd);
  402. printf ("events %d\n", poll_instance->poll_entries[i].ufd.events);
  403. printf ("dispatch_fn %p\n", poll_instance->poll_entries[i].dispatch_fn);
  404. }
  405. }
  406. }
  407. #endif