2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Volker Lendecke 2005
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 struct timed_event *next, *prev;
26 struct event_context *event_ctx;
28 const char *event_name;
29 void (*handler)(struct event_context *event_ctx,
30 struct timed_event *te,
31 const struct timeval *now,
37 struct fd_event *prev, *next;
38 struct event_context *event_ctx;
40 uint16_t flags; /* see EVENT_FD_* flags */
41 void (*handler)(struct event_context *event_ctx,
42 struct fd_event *event,
48 #define EVENT_FD_WRITEABLE(fde) \
49 event_set_fd_flags(fde, event_get_fd_flags(fde) | EVENT_FD_WRITE)
50 #define EVENT_FD_READABLE(fde) \
51 event_set_fd_flags(fde, event_get_fd_flags(fde) | EVENT_FD_READ)
53 #define EVENT_FD_NOT_WRITEABLE(fde) \
54 event_set_fd_flags(fde, event_get_fd_flags(fde) & ~EVENT_FD_WRITE)
55 #define EVENT_FD_NOT_READABLE(fde) \
56 event_set_fd_flags(fde, event_get_fd_flags(fde) & ~EVENT_FD_READ)
58 struct event_context {
59 struct timed_event *timed_events;
60 struct fd_event *fd_events;
63 static int timed_event_destructor(struct timed_event *te)
65 DEBUG(10, ("Destroying timed event %lx \"%s\"\n", (unsigned long)te,
67 DLIST_REMOVE(te->event_ctx->timed_events, te);
71 /****************************************************************************
73 ****************************************************************************/
75 static void add_event_by_time(struct timed_event *te)
77 struct timed_event *last_te, *cur_te;
79 /* Keep the list ordered by time. We must preserve this. */
81 for (cur_te = timed_events; cur_te; cur_te = cur_te->next) {
82 /* if the new event comes before the current one break */
83 if (!timeval_is_zero(&cur_te->when) &&
84 timeval_compare(&te->when, &cur_te->when) < 0) {
90 DLIST_ADD_AFTER(timed_events, te, last_te);
93 /****************************************************************************
94 Schedule a function for future calling, cancel with TALLOC_FREE().
95 It's the responsibility of the handler to call TALLOC_FREE() on the event
97 ****************************************************************************/
99 struct timed_event *event_add_timed(struct event_context *event_ctx,
102 const char *event_name,
103 void (*handler)(struct event_context *event_ctx,
104 struct timed_event *te,
105 const struct timeval *now,
109 struct timed_event *te;
111 te = TALLOC_P(mem_ctx, struct timed_event);
113 DEBUG(0, ("talloc failed\n"));
117 te->event_ctx = event_ctx;
119 te->event_name = event_name;
120 te->handler = handler;
121 te->private_data = private_data;
123 add_event_by_time(te);
125 talloc_set_destructor(te, timed_event_destructor);
127 DEBUG(10, ("Added timed event \"%s\": %lx\n", event_name,
132 static int fd_event_destructor(struct fd_event *fde)
134 struct event_context *event_ctx = fde->event_ctx;
136 DLIST_REMOVE(event_ctx->fd_events, fde);
140 struct fd_event *event_add_fd(struct event_context *event_ctx,
142 int fd, uint16_t flags,
143 void (*handler)(struct event_context *event_ctx,
144 struct fd_event *event,
149 struct fd_event *fde;
151 if (!(fde = TALLOC_P(mem_ctx, struct fd_event))) {
155 fde->event_ctx = event_ctx;
158 fde->handler = handler;
159 fde->private_data = private_data;
161 DLIST_ADD(event_ctx->fd_events, fde);
163 talloc_set_destructor(fde, fd_event_destructor);
167 void event_fd_set_writeable(struct fd_event *fde)
169 fde->flags |= EVENT_FD_WRITE;
172 void event_fd_set_not_writeable(struct fd_event *fde)
174 fde->flags &= ~EVENT_FD_WRITE;
177 void event_fd_set_readable(struct fd_event *fde)
179 fde->flags |= EVENT_FD_READ;
182 void event_fd_set_not_readable(struct fd_event *fde)
184 fde->flags &= ~EVENT_FD_READ;
187 void event_add_to_select_args(struct event_context *event_ctx,
188 const struct timeval *now,
189 fd_set *read_fds, fd_set *write_fds,
190 struct timeval *timeout, int *maxfd)
192 struct fd_event *fde;
195 for (fde = event_ctx->fd_events; fde; fde = fde->next) {
196 if (fde->flags & EVENT_FD_READ) {
197 FD_SET(fde->fd, read_fds);
199 if (fde->flags & EVENT_FD_WRITE) {
200 FD_SET(fde->fd, write_fds);
203 if ((fde->flags & (EVENT_FD_READ|EVENT_FD_WRITE))
204 && (fde->fd > *maxfd)) {
209 if (event_ctx->timed_events == NULL) {
213 diff = timeval_until(now, &event_ctx->timed_events->when);
214 *timeout = timeval_min(timeout, &diff);
217 BOOL run_events(struct event_context *event_ctx,
218 int selrtn, fd_set *read_fds, fd_set *write_fds)
221 struct fd_event *fde, *next;
223 /* Run all events that are pending, not just one (as we
226 while (event_ctx->timed_events) {
231 &now, &event_ctx->timed_events->when) < 0) {
232 /* Nothing to do yet */
233 DEBUG(11, ("run_events: Nothing to do\n"));
237 DEBUG(10, ("Running event \"%s\" %lx\n",
238 event_ctx->timed_events->event_name,
239 (unsigned long)event_ctx->timed_events));
241 event_ctx->timed_events->handler(
243 event_ctx->timed_events, &now,
244 event_ctx->timed_events->private_data);
251 * We might have changed the socket status during the timed
252 * events, return to run select again.
264 for (fde = event_ctx->fd_events; fde; fde = next) {
268 if (FD_ISSET(fde->fd, read_fds)) flags |= EVENT_FD_READ;
269 if (FD_ISSET(fde->fd, write_fds)) flags |= EVENT_FD_WRITE;
272 fde->handler(event_ctx, fde, flags, fde->private_data);
281 struct timeval *get_timed_events_timeout(struct event_context *event_ctx,
282 struct timeval *to_ret)
286 if (event_ctx->timed_events == NULL) {
290 now = timeval_current();
291 *to_ret = timeval_until(&now, &event_ctx->timed_events->when);
293 DEBUG(10, ("timed_events_timeout: %d/%d\n", (int)to_ret->tv_sec,
294 (int)to_ret->tv_usec));
299 struct event_context *event_context_init(TALLOC_CTX *mem_ctx)
301 return TALLOC_ZERO_P(NULL, struct event_context);
304 int set_event_dispatch_time(struct event_context *event_ctx,
305 const char *event_name, struct timeval when)
307 struct timed_event *te;
309 for (te = event_ctx->timed_events; te; te = te->next) {
310 if (strcmp(event_name, te->event_name) == 0) {
311 DLIST_REMOVE(timed_events, te);
313 add_event_by_time(te);
320 /* Returns 1 if event was found and cancelled, 0 otherwise. */
322 int cancel_named_event(struct event_context *event_ctx,
323 const char *event_name)
325 struct timed_event *te;
327 for (te = event_ctx->timed_events; te; te = te->next) {
328 if (strcmp(event_name, te->event_name) == 0) {