ctorrent stat collector
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

314 lines
7.9KB

  1. /***************************************************************************
  2. * Copyright (C) 07/2007 by Olaf Rempel *
  3. * razzor@kopf-tisch.de *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; version 2 of the License *
  8. * *
  9. * This program is distributed in the hope that it will be useful, *
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  12. * GNU General Public License for more details. *
  13. * *
  14. * You should have received a copy of the GNU General Public License *
  15. * along with this program; if not, write to the *
  16. * Free Software Foundation, Inc., *
  17. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  18. ***************************************************************************/
  19. #include <stdlib.h>
  20. #include <unistd.h>
  21. #include <string.h>
  22. #include <errno.h>
  23. #include <sys/time.h>
  24. #include <sys/types.h>
  25. #include "list.h"
  26. #include "logging.h"
  27. #include "event.h"
  28. static LIST_HEAD(event_fd_list);
  29. static LIST_HEAD(event_timeout_list);
  30. static int leave_loop;
  31. struct event_fd {
  32. struct list_head list;
  33. unsigned int flags;
  34. int fd;
  35. int (*read_cb)(int fd, void *privdata);
  36. int (*write_cb)(int fd, void *privdata);
  37. void *read_priv;
  38. void *write_priv;
  39. };
  40. struct event_timeout {
  41. struct list_head list;
  42. unsigned int flags;
  43. struct timeval intervall;
  44. struct timeval nextrun;
  45. int (*callback)(void *privdata);
  46. void *privdata;
  47. };
  48. struct event_fd * event_add_fd(
  49. struct event_fd *entry,
  50. int fd,
  51. int type,
  52. int (*callback)(int fd, void *privdata),
  53. void *privdata)
  54. {
  55. /* check valid filediskriptor */
  56. if (fd < 0 || fd > FD_SETSIZE) {
  57. log_print(LOG_ERROR, "event_add_fd(): invalid fd");
  58. return NULL;
  59. }
  60. /* check valid type (read/write) */
  61. if (!(type & FD_TYPES)) {
  62. log_print(LOG_ERROR, "event_add_fd(): invalid type");
  63. return NULL;
  64. }
  65. /* create new entry */
  66. if (entry == NULL) {
  67. entry = malloc(sizeof(struct event_fd));
  68. if (entry == NULL) {
  69. log_print(LOG_ERROR, "event_add_fd(): out of memory");
  70. return NULL;
  71. }
  72. memset(entry, 0, sizeof(struct event_fd));
  73. entry->flags |= EVENT_NEW;
  74. entry->fd = fd;
  75. /* put it on the list */
  76. list_add_tail(&entry->list, &event_fd_list);
  77. }
  78. if (type & FD_READ) {
  79. entry->flags = (callback != NULL) ? (entry->flags | FD_READ | EVENT_NEW) : (entry->flags & ~FD_READ);
  80. entry->read_cb = callback;
  81. entry->read_priv = privdata;
  82. } else if (type & FD_WRITE) {
  83. entry->flags = (callback != NULL) ? (entry->flags | FD_WRITE | EVENT_NEW) : (entry->flags & ~FD_WRITE);
  84. entry->write_cb = callback;
  85. entry->write_priv = privdata;
  86. }
  87. return entry;
  88. }
  89. int event_get_fd(struct event_fd *entry)
  90. {
  91. return (entry != NULL) ? entry->fd: -1;
  92. }
  93. void event_remove_fd(struct event_fd *entry)
  94. {
  95. /* mark the event as deleted -> remove in select() loop */
  96. entry->flags |= EVENT_DELETE;
  97. }
  98. static void add_timeval(struct timeval *ret, struct timeval *a, struct timeval *b)
  99. {
  100. ret->tv_usec = a->tv_usec + b->tv_usec;
  101. ret->tv_sec = a->tv_sec + b->tv_sec;
  102. if (ret->tv_usec >= 1000000) {
  103. ret->tv_usec -= 1000000;
  104. ret->tv_sec++;
  105. }
  106. }
  107. static void sub_timeval(struct timeval *ret, struct timeval *a, struct timeval *b)
  108. {
  109. ret->tv_usec = a->tv_usec - b->tv_usec;
  110. ret->tv_sec = a->tv_sec - b->tv_sec;
  111. if (ret->tv_usec < 0) {
  112. ret->tv_usec += 1000000;
  113. ret->tv_sec--;
  114. }
  115. }
  116. static int cmp_timeval(struct timeval *a, struct timeval *b)
  117. {
  118. if (a->tv_sec > b->tv_sec)
  119. return -1;
  120. if (a->tv_sec < b->tv_sec)
  121. return 1;
  122. if (a->tv_usec > b->tv_usec)
  123. return -1;
  124. if (a->tv_usec < b->tv_usec)
  125. return 1;
  126. return 0;
  127. }
  128. static void schedule_nextrun(struct event_timeout *entry, struct timeval *now)
  129. {
  130. add_timeval(&entry->nextrun, now, &entry->intervall);
  131. struct event_timeout *search;
  132. list_for_each_entry(search, &event_timeout_list, list) {
  133. if (search->nextrun.tv_sec > entry->nextrun.tv_sec) {
  134. list_add_tail(&entry->list, &search->list);
  135. return;
  136. } else if (search->nextrun.tv_sec == entry->nextrun.tv_sec &&
  137. search->nextrun.tv_usec > entry->nextrun.tv_usec) {
  138. list_add_tail(&entry->list, &search->list);
  139. return;
  140. }
  141. }
  142. list_add_tail(&entry->list, &event_timeout_list);
  143. }
  144. struct event_timeout * event_add_timeout(
  145. struct timeval *timeout,
  146. int (*callback)(void *privdata),
  147. void *privdata)
  148. {
  149. struct event_timeout *entry;
  150. entry = malloc(sizeof(struct event_timeout));
  151. if (entry == NULL) {
  152. log_print(LOG_ERROR, "event_add_timeout(): out of memory");
  153. return NULL;
  154. }
  155. entry->flags = 0;
  156. memcpy(&entry->intervall, timeout, sizeof(entry->intervall));
  157. entry->callback = callback;
  158. entry->privdata = privdata;
  159. struct timeval now;
  160. gettimeofday(&now, NULL);
  161. schedule_nextrun(entry, &now);
  162. return entry;
  163. }
  164. void event_remove_timeout(struct event_timeout *entry)
  165. {
  166. /* mark the event as deleted -> remove in select() loop */
  167. entry->flags |= EVENT_DELETE;
  168. }
  169. void event_loop_break(void)
  170. {
  171. leave_loop = 1;
  172. }
  173. int event_loop(void)
  174. {
  175. fd_set *fdsets = malloc(sizeof(fd_set) * 2);
  176. if (fdsets == NULL) {
  177. log_print(LOG_ERROR, "event_loop(): out of memory");
  178. return -1;
  179. }
  180. leave_loop = 0;
  181. while (!leave_loop) {
  182. struct timeval timeout, *timeout_p = NULL;
  183. if (!list_empty(&event_timeout_list)) {
  184. struct timeval now;
  185. gettimeofday(&now, NULL);
  186. struct event_timeout *entry, *tmp;
  187. list_for_each_entry_safe(entry, tmp, &event_timeout_list, list) {
  188. if (entry->flags & EVENT_DELETE) {
  189. list_del(&entry->list);
  190. free(entry);
  191. continue;
  192. }
  193. /* first timeout not elapsed, exit search (since list is sorted) */
  194. if (cmp_timeval(&entry->nextrun, &now) == -1)
  195. break;
  196. /* remove event from list */
  197. list_del(&entry->list);
  198. /* execute callback, when callback returns 0 -> schedule event again */
  199. if (entry->callback(entry->privdata)) {
  200. free(entry);
  201. } else {
  202. schedule_nextrun(entry, &now);
  203. }
  204. }
  205. if (!list_empty(&event_timeout_list)) {
  206. entry = list_entry(event_timeout_list.next, typeof(*entry), list);
  207. /* calc select() timeout */
  208. sub_timeval(&timeout, &entry->nextrun, &now);
  209. timeout_p = &timeout;
  210. }
  211. }
  212. fd_set *readfds = NULL, *writefds = NULL;
  213. struct event_fd *entry, *tmp;
  214. int maxfd = -1;
  215. list_for_each_entry_safe(entry, tmp, &event_fd_list, list) {
  216. entry->flags &= ~EVENT_NEW;
  217. if (entry->flags & EVENT_DELETE) {
  218. list_del(&entry->list);
  219. free(entry);
  220. continue;
  221. }
  222. if (entry->flags & FD_READ) {
  223. if (readfds == NULL) {
  224. readfds = &fdsets[0];
  225. FD_ZERO(readfds);
  226. }
  227. FD_SET(entry->fd, readfds);
  228. }
  229. if (entry->flags & FD_WRITE) {
  230. if (writefds == NULL) {
  231. writefds = &fdsets[1];
  232. FD_ZERO(writefds);
  233. }
  234. FD_SET(entry->fd, writefds);
  235. }
  236. maxfd = (entry->fd > maxfd) ? entry->fd : maxfd;
  237. }
  238. int i = select(maxfd +1, readfds, writefds, NULL, timeout_p);
  239. if (i < 0 && errno == EINTR) {
  240. errno = 0;
  241. continue;
  242. } else if (i < 0) {
  243. log_print(LOG_ERROR, "event_loop(): select():");
  244. continue;
  245. } else if (i == 0) {
  246. continue;
  247. }
  248. list_for_each_entry(entry, &event_fd_list, list) {
  249. if (((entry->flags & (FD_READ | EVENT_NEW)) == FD_READ) && FD_ISSET(entry->fd, readfds))
  250. if (entry->read_cb(entry->fd, entry->read_priv) != 0)
  251. entry->flags |= EVENT_DELETE;
  252. if (((entry->flags & (FD_WRITE | EVENT_NEW)) == FD_WRITE) && FD_ISSET(entry->fd, writefds))
  253. if (entry->write_cb(entry->fd, entry->write_priv) != 0)
  254. entry->flags |= EVENT_DELETE;
  255. }
  256. }
  257. free(fdsets);
  258. return 0;
  259. }