coropoll.c 10 KB

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