coropoll.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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 <corosync/hdb.h>
  43. #include <corosync/totem/coropoll.h>
  44. #include <corosync/list.h>
  45. #include "tlist.h"
  46. typedef int (*dispatch_fn_t) (hdb_handle_t hdb_handle, int fd, int revents, void *data);
  47. struct poll_entry {
  48. struct pollfd ufd;
  49. dispatch_fn_t dispatch_fn;
  50. void *data;
  51. };
  52. struct poll_instance {
  53. struct poll_entry *poll_entries;
  54. struct pollfd *ufds;
  55. int poll_entry_count;
  56. struct timerlist timerlist;
  57. int stop_requested;
  58. };
  59. DECLARE_HDB_DATABASE (poll_instance_database,NULL);
  60. hdb_handle_t poll_create (void)
  61. {
  62. hdb_handle_t handle;
  63. struct poll_instance *poll_instance;
  64. unsigned int res;
  65. res = hdb_handle_create (&poll_instance_database,
  66. sizeof (struct poll_instance), &handle);
  67. if (res != 0) {
  68. goto error_exit;
  69. }
  70. res = hdb_handle_get (&poll_instance_database, handle,
  71. (void *)&poll_instance);
  72. if (res != 0) {
  73. goto error_destroy;
  74. }
  75. poll_instance->poll_entries = 0;
  76. poll_instance->ufds = 0;
  77. poll_instance->poll_entry_count = 0;
  78. poll_instance->stop_requested = 0;
  79. timerlist_init (&poll_instance->timerlist);
  80. return (handle);
  81. error_destroy:
  82. hdb_handle_destroy (&poll_instance_database, handle);
  83. error_exit:
  84. return (-1);
  85. }
  86. int poll_destroy (hdb_handle_t handle)
  87. {
  88. struct poll_instance *poll_instance;
  89. int res = 0;
  90. res = hdb_handle_get (&poll_instance_database, handle,
  91. (void *)&poll_instance);
  92. if (res != 0) {
  93. res = -ENOENT;
  94. goto error_exit;
  95. }
  96. free (poll_instance->poll_entries);
  97. free (poll_instance->ufds);
  98. hdb_handle_destroy (&poll_instance_database, handle);
  99. hdb_handle_put (&poll_instance_database, handle);
  100. error_exit:
  101. return (res);
  102. }
  103. int poll_dispatch_add (
  104. hdb_handle_t handle,
  105. int fd,
  106. int events,
  107. void *data,
  108. int (*dispatch_fn) (
  109. hdb_handle_t hdb_handle_t,
  110. int fd,
  111. int revents,
  112. void *data))
  113. {
  114. struct poll_instance *poll_instance;
  115. struct poll_entry *poll_entries;
  116. struct pollfd *ufds;
  117. int found = 0;
  118. int install_pos;
  119. int res = 0;
  120. res = hdb_handle_get (&poll_instance_database, handle,
  121. (void *)&poll_instance);
  122. if (res != 0) {
  123. res = -ENOENT;
  124. goto error_exit;
  125. }
  126. for (found = 0, install_pos = 0; install_pos < poll_instance->poll_entry_count; install_pos++) {
  127. if (poll_instance->poll_entries[install_pos].ufd.fd == -1) {
  128. found = 1;
  129. break;
  130. }
  131. }
  132. if (found == 0) {
  133. /*
  134. * Grow pollfd list
  135. */
  136. poll_entries = (struct poll_entry *)realloc (poll_instance->poll_entries,
  137. (poll_instance->poll_entry_count + 1) *
  138. sizeof (struct poll_entry));
  139. if (poll_entries == NULL) {
  140. res = -ENOMEM;
  141. goto error_put;
  142. }
  143. poll_instance->poll_entries = poll_entries;
  144. ufds = (struct pollfd *)realloc (poll_instance->ufds,
  145. (poll_instance->poll_entry_count + 1) *
  146. sizeof (struct pollfd));
  147. if (ufds == NULL) {
  148. res = -ENOMEM;
  149. goto error_put;
  150. }
  151. poll_instance->ufds = ufds;
  152. poll_instance->poll_entry_count += 1;
  153. install_pos = poll_instance->poll_entry_count - 1;
  154. }
  155. /*
  156. * Install new dispatch handler
  157. */
  158. poll_instance->poll_entries[install_pos].ufd.fd = fd;
  159. poll_instance->poll_entries[install_pos].ufd.events = events;
  160. poll_instance->poll_entries[install_pos].ufd.revents = 0;
  161. poll_instance->poll_entries[install_pos].dispatch_fn = dispatch_fn;
  162. poll_instance->poll_entries[install_pos].data = data;
  163. error_put:
  164. hdb_handle_put (&poll_instance_database, handle);
  165. error_exit:
  166. return (res);
  167. }
  168. int poll_dispatch_modify (
  169. hdb_handle_t handle,
  170. int fd,
  171. int events,
  172. int (*dispatch_fn) (
  173. hdb_handle_t hdb_handle_t,
  174. int fd,
  175. int revents,
  176. void *data))
  177. {
  178. struct poll_instance *poll_instance;
  179. int i;
  180. int res = 0;
  181. res = hdb_handle_get (&poll_instance_database, handle,
  182. (void *)&poll_instance);
  183. if (res != 0) {
  184. res = -ENOENT;
  185. goto error_exit;
  186. }
  187. /*
  188. * Find file descriptor to modify events and dispatch function
  189. */
  190. for (i = 0; i < poll_instance->poll_entry_count; i++) {
  191. if (poll_instance->poll_entries[i].ufd.fd == fd) {
  192. poll_instance->poll_entries[i].ufd.events = events;
  193. poll_instance->poll_entries[i].dispatch_fn = dispatch_fn;
  194. goto error_put;
  195. }
  196. }
  197. res = -EBADF;
  198. error_put:
  199. hdb_handle_put (&poll_instance_database, handle);
  200. error_exit:
  201. return (res);
  202. }
  203. int poll_dispatch_delete (
  204. hdb_handle_t handle,
  205. int fd)
  206. {
  207. struct poll_instance *poll_instance;
  208. int i;
  209. int res = 0;
  210. res = hdb_handle_get (&poll_instance_database, handle,
  211. (void *)&poll_instance);
  212. if (res != 0) {
  213. res = -ENOENT;
  214. goto error_exit;
  215. }
  216. /*
  217. * Find dispatch fd to delete
  218. */
  219. res = -EBADF;
  220. for (i = 0; i < poll_instance->poll_entry_count; i++) {
  221. if (poll_instance->poll_entries[i].ufd.fd == fd) {
  222. poll_instance->ufds[i].fd = -1;
  223. poll_instance->poll_entries[i].ufd.fd = -1;
  224. poll_instance->poll_entries[i].ufd.revents = 0;
  225. break;
  226. }
  227. }
  228. hdb_handle_put (&poll_instance_database, handle);
  229. error_exit:
  230. return (res);
  231. }
  232. int poll_timer_add (
  233. hdb_handle_t handle,
  234. int msec_duration, void *data,
  235. void (*timer_fn) (void *data),
  236. poll_timer_handle *timer_handle_out)
  237. {
  238. struct poll_instance *poll_instance;
  239. int res = 0;
  240. if (timer_handle_out == NULL) {
  241. res = -ENOENT;
  242. goto error_exit;
  243. }
  244. res = hdb_handle_get (&poll_instance_database, handle,
  245. (void *)&poll_instance);
  246. if (res != 0) {
  247. res = -ENOENT;
  248. goto error_exit;
  249. }
  250. timerlist_add_duration (&poll_instance->timerlist,
  251. timer_fn, data, ((unsigned long long)msec_duration) * 1000000ULL, timer_handle_out);
  252. hdb_handle_put (&poll_instance_database, handle);
  253. error_exit:
  254. return (res);
  255. }
  256. int poll_timer_delete (
  257. hdb_handle_t handle,
  258. poll_timer_handle th)
  259. {
  260. struct poll_instance *poll_instance;
  261. int res = 0;
  262. if (th == 0) {
  263. return (0);
  264. }
  265. res = hdb_handle_get (&poll_instance_database, handle,
  266. (void *)&poll_instance);
  267. if (res != 0) {
  268. res = -ENOENT;
  269. goto error_exit;
  270. }
  271. timerlist_del (&poll_instance->timerlist, (void *)th);
  272. hdb_handle_put (&poll_instance_database, handle);
  273. error_exit:
  274. return (res);
  275. }
  276. int poll_stop (
  277. hdb_handle_t handle)
  278. {
  279. struct poll_instance *poll_instance;
  280. unsigned int res;
  281. res = hdb_handle_get (&poll_instance_database, handle,
  282. (void *)&poll_instance);
  283. if (res != 0) {
  284. res = -ENOENT;
  285. goto error_exit;
  286. }
  287. poll_instance->stop_requested = 1;
  288. hdb_handle_put (&poll_instance_database, handle);
  289. error_exit:
  290. return (res);
  291. }
  292. int poll_run (
  293. hdb_handle_t handle)
  294. {
  295. struct poll_instance *poll_instance;
  296. int i;
  297. unsigned long long expire_timeout_msec = -1;
  298. int res;
  299. int poll_entry_count;
  300. res = hdb_handle_get (&poll_instance_database, handle,
  301. (void *)&poll_instance);
  302. if (res != 0) {
  303. goto error_exit;
  304. }
  305. for (;;) {
  306. for (i = 0; i < poll_instance->poll_entry_count; i++) {
  307. memcpy (&poll_instance->ufds[i],
  308. &poll_instance->poll_entries[i].ufd,
  309. sizeof (struct pollfd));
  310. }
  311. expire_timeout_msec = timerlist_msec_duration_to_expire (&poll_instance->timerlist);
  312. if (expire_timeout_msec != -1 && expire_timeout_msec > 0xFFFFFFFF) {
  313. expire_timeout_msec = 0xFFFFFFFE;
  314. }
  315. retry_poll:
  316. res = poll (poll_instance->ufds,
  317. poll_instance->poll_entry_count, expire_timeout_msec);
  318. if (poll_instance->stop_requested) {
  319. return (0);
  320. }
  321. if (errno == EINTR && res == -1) {
  322. goto retry_poll;
  323. } else
  324. if (res == -1) {
  325. goto error_exit;
  326. }
  327. poll_entry_count = poll_instance->poll_entry_count;
  328. for (i = 0; i < poll_entry_count; i++) {
  329. if (poll_instance->ufds[i].fd != -1 &&
  330. poll_instance->ufds[i].revents) {
  331. res = poll_instance->poll_entries[i].dispatch_fn (handle,
  332. poll_instance->ufds[i].fd,
  333. poll_instance->ufds[i].revents,
  334. poll_instance->poll_entries[i].data);
  335. /*
  336. * Remove dispatch functions that return -1
  337. */
  338. if (res == -1) {
  339. poll_instance->poll_entries[i].ufd.fd = -1; /* empty entry */
  340. }
  341. }
  342. }
  343. timerlist_expire (&poll_instance->timerlist);
  344. } /* for (;;) */
  345. hdb_handle_put (&poll_instance_database, handle);
  346. error_exit:
  347. return (-1);
  348. }
  349. #ifdef COMPILE_OUT
  350. void poll_print_state (
  351. hdb_handle_t handle,
  352. int fd)
  353. {
  354. struct poll_instance *poll_instance;
  355. int i;
  356. int res = 0;
  357. res = hdb_handle_get (&poll_instance_database, handle,
  358. (void *)&poll_instance);
  359. if (res != 0) {
  360. res = -ENOENT;
  361. exit (1);
  362. }
  363. for (i = 0; i < poll_instance->poll_entry_count; i++) {
  364. if (poll_instance->poll_entries[i].ufd.fd == fd) {
  365. printf ("fd %d\n", poll_instance->poll_entries[i].ufd.fd);
  366. printf ("events %d\n", poll_instance->poll_entries[i].ufd.events);
  367. printf ("dispatch_fn %p\n", poll_instance->poll_entries[i].dispatch_fn);
  368. }
  369. }
  370. }
  371. #endif