coropoll.c 10 KB

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