coropoll.c 11 KB

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