coropoll.c 11 KB

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