coropoll.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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_t, 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->poll_entries[i].ufd.fd = -1;
  223. poll_instance->poll_entries[i].ufd.revents = 0;
  224. break;
  225. }
  226. }
  227. hdb_handle_put (&poll_instance_database, handle);
  228. error_exit:
  229. return (res);
  230. }
  231. int poll_timer_add (
  232. hdb_handle_t handle,
  233. int msec_duration, void *data,
  234. void (*timer_fn) (void *data),
  235. poll_timer_handle *timer_handle_out)
  236. {
  237. struct poll_instance *poll_instance;
  238. int res = 0;
  239. if (timer_handle_out == NULL) {
  240. res = -ENOENT;
  241. goto error_exit;
  242. }
  243. res = hdb_handle_get (&poll_instance_database, handle,
  244. (void *)&poll_instance);
  245. if (res != 0) {
  246. res = -ENOENT;
  247. goto error_exit;
  248. }
  249. timerlist_add_duration (&poll_instance->timerlist,
  250. timer_fn, data, ((unsigned long long)msec_duration) * 1000000ULL, timer_handle_out);
  251. hdb_handle_put (&poll_instance_database, handle);
  252. error_exit:
  253. return (res);
  254. }
  255. int poll_timer_delete (
  256. hdb_handle_t handle,
  257. poll_timer_handle th)
  258. {
  259. struct poll_instance *poll_instance;
  260. int res = 0;
  261. if (th == 0) {
  262. return (0);
  263. }
  264. res = hdb_handle_get (&poll_instance_database, handle,
  265. (void *)&poll_instance);
  266. if (res != 0) {
  267. res = -ENOENT;
  268. goto error_exit;
  269. }
  270. timerlist_del (&poll_instance->timerlist, (void *)th);
  271. hdb_handle_put (&poll_instance_database, handle);
  272. error_exit:
  273. return (res);
  274. }
  275. int poll_stop (
  276. hdb_handle_t handle)
  277. {
  278. struct poll_instance *poll_instance;
  279. unsigned int res;
  280. res = hdb_handle_get (&poll_instance_database, handle,
  281. (void *)&poll_instance);
  282. if (res != 0) {
  283. res = -ENOENT;
  284. goto error_exit;
  285. }
  286. poll_instance->stop_requested = 1;
  287. hdb_handle_put (&poll_instance_database, handle);
  288. error_exit:
  289. return (res);
  290. }
  291. int poll_run (
  292. hdb_handle_t handle)
  293. {
  294. struct poll_instance *poll_instance;
  295. int i;
  296. unsigned long long expire_timeout_msec = -1;
  297. int res;
  298. int poll_entry_count;
  299. res = hdb_handle_get (&poll_instance_database, handle,
  300. (void *)&poll_instance);
  301. if (res != 0) {
  302. goto error_exit;
  303. }
  304. for (;;) {
  305. for (i = 0; i < poll_instance->poll_entry_count; i++) {
  306. memcpy (&poll_instance->ufds[i],
  307. &poll_instance->poll_entries[i].ufd,
  308. sizeof (struct pollfd));
  309. }
  310. expire_timeout_msec = timerlist_msec_duration_to_expire (&poll_instance->timerlist);
  311. if (expire_timeout_msec != -1 && expire_timeout_msec > 0xFFFFFFFF) {
  312. expire_timeout_msec = 0xFFFFFFFE;
  313. }
  314. retry_poll:
  315. res = poll (poll_instance->ufds,
  316. poll_instance->poll_entry_count, expire_timeout_msec);
  317. if (poll_instance->stop_requested) {
  318. return (0);
  319. }
  320. if (errno == EINTR && res == -1) {
  321. goto retry_poll;
  322. } else
  323. if (res == -1) {
  324. goto error_exit;
  325. }
  326. poll_entry_count = poll_instance->poll_entry_count;
  327. for (i = 0; i < poll_entry_count; i++) {
  328. if (poll_instance->ufds[i].fd != -1 &&
  329. poll_instance->ufds[i].revents) {
  330. res = poll_instance->poll_entries[i].dispatch_fn (handle,
  331. poll_instance->ufds[i].fd,
  332. poll_instance->ufds[i].revents,
  333. poll_instance->poll_entries[i].data);
  334. /*
  335. * Remove dispatch functions that return -1
  336. */
  337. if (res == -1) {
  338. poll_instance->poll_entries[i].ufd.fd = -1; /* empty entry */
  339. }
  340. }
  341. }
  342. timerlist_expire (&poll_instance->timerlist);
  343. } /* for (;;) */
  344. hdb_handle_put (&poll_instance_database, handle);
  345. error_exit:
  346. return (-1);
  347. }
  348. #ifdef COMPILE_OUT
  349. void poll_print_state (
  350. hdb_handle_t handle,
  351. int fd)
  352. {
  353. struct poll_instance *poll_instance;
  354. int i;
  355. int res = 0;
  356. res = hdb_handle_get (&poll_instance_database, handle,
  357. (void *)&poll_instance);
  358. if (res != 0) {
  359. res = -ENOENT;
  360. exit (1);
  361. }
  362. for (i = 0; i < poll_instance->poll_entry_count; i++) {
  363. if (poll_instance->poll_entries[i].ufd.fd == fd) {
  364. printf ("fd %d\n", poll_instance->poll_entries[i].ufd.fd);
  365. printf ("events %d\n", poll_instance->poll_entries[i].ufd.events);
  366. printf ("dispatch_fn %p\n", poll_instance->poll_entries[i].dispatch_fn);
  367. }
  368. }
  369. }
  370. #endif