selftest: Use TESTS env var to specify tests to run like Samba4 does.
[Samba.git] / source / lib / events.c
blob9decf213b4a71017fd58744649a8efbd70333b21
1 /*
2 Unix SMB/CIFS implementation.
3 Timed event library.
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 3 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, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
23 struct timed_event {
24 struct timed_event *next, *prev;
25 struct event_context *event_ctx;
26 struct timeval when;
27 const char *event_name;
28 void (*handler)(struct event_context *event_ctx,
29 struct timed_event *te,
30 const struct timeval *now,
31 void *private_data);
32 void *private_data;
35 struct fd_event {
36 struct fd_event *prev, *next;
37 struct event_context *event_ctx;
38 int fd;
39 uint16_t flags; /* see EVENT_FD_* flags */
40 void (*handler)(struct event_context *event_ctx,
41 struct fd_event *event,
42 uint16 flags,
43 void *private_data);
44 void *private_data;
47 #define EVENT_FD_WRITEABLE(fde) \
48 event_set_fd_flags(fde, event_get_fd_flags(fde) | EVENT_FD_WRITE)
49 #define EVENT_FD_READABLE(fde) \
50 event_set_fd_flags(fde, event_get_fd_flags(fde) | EVENT_FD_READ)
52 #define EVENT_FD_NOT_WRITEABLE(fde) \
53 event_set_fd_flags(fde, event_get_fd_flags(fde) & ~EVENT_FD_WRITE)
54 #define EVENT_FD_NOT_READABLE(fde) \
55 event_set_fd_flags(fde, event_get_fd_flags(fde) & ~EVENT_FD_READ)
57 struct event_context {
58 struct timed_event *timed_events;
59 struct fd_event *fd_events;
62 static int timed_event_destructor(struct timed_event *te)
64 DEBUG(10, ("Destroying timed event %lx \"%s\"\n", (unsigned long)te,
65 te->event_name));
66 DLIST_REMOVE(te->event_ctx->timed_events, te);
67 return 0;
70 /****************************************************************************
71 Add te by time.
72 ****************************************************************************/
74 static void add_event_by_time(struct timed_event *te)
76 struct event_context *ctx = te->event_ctx;
77 struct timed_event *last_te, *cur_te;
79 /* Keep the list ordered by time. We must preserve this. */
80 last_te = NULL;
81 for (cur_te = ctx->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) {
85 break;
87 last_te = cur_te;
90 DLIST_ADD_AFTER(ctx->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
96 handed to it.
97 ****************************************************************************/
99 struct timed_event *event_add_timed(struct event_context *event_ctx,
100 TALLOC_CTX *mem_ctx,
101 struct timeval when,
102 const char *event_name,
103 void (*handler)(struct event_context *event_ctx,
104 struct timed_event *te,
105 const struct timeval *now,
106 void *private_data),
107 void *private_data)
109 struct timed_event *te;
111 te = TALLOC_P(mem_ctx, struct timed_event);
112 if (te == NULL) {
113 DEBUG(0, ("talloc failed\n"));
114 return NULL;
117 te->event_ctx = event_ctx;
118 te->when = when;
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,
128 (unsigned long)te));
129 return te;
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);
137 return 0;
140 struct fd_event *event_add_fd(struct event_context *event_ctx,
141 TALLOC_CTX *mem_ctx,
142 int fd, uint16_t flags,
143 void (*handler)(struct event_context *event_ctx,
144 struct fd_event *event,
145 uint16 flags,
146 void *private_data),
147 void *private_data)
149 struct fd_event *fde;
151 if (!(fde = TALLOC_P(mem_ctx, struct fd_event))) {
152 return NULL;
155 fde->event_ctx = event_ctx;
156 fde->fd = fd;
157 fde->flags = flags;
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);
164 return fde;
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;
188 * Return if there's something in the queue
191 bool event_add_to_select_args(struct event_context *event_ctx,
192 const struct timeval *now,
193 fd_set *read_fds, fd_set *write_fds,
194 struct timeval *timeout, int *maxfd)
196 struct fd_event *fde;
197 struct timeval diff;
198 bool ret = False;
200 for (fde = event_ctx->fd_events; fde; fde = fde->next) {
201 if (fde->flags & EVENT_FD_READ) {
202 FD_SET(fde->fd, read_fds);
203 ret = True;
205 if (fde->flags & EVENT_FD_WRITE) {
206 FD_SET(fde->fd, write_fds);
207 ret = True;
210 if ((fde->flags & (EVENT_FD_READ|EVENT_FD_WRITE))
211 && (fde->fd > *maxfd)) {
212 *maxfd = fde->fd;
216 if (event_ctx->timed_events == NULL) {
217 return ret;
220 diff = timeval_until(now, &event_ctx->timed_events->when);
221 *timeout = timeval_min(timeout, &diff);
223 return True;
226 bool events_pending(struct event_context *event_ctx)
228 struct fd_event *fde;
230 if (event_ctx->timed_events != NULL) {
231 return True;
233 for (fde = event_ctx->fd_events; fde; fde = fde->next) {
234 if (fde->flags & (EVENT_FD_READ|EVENT_FD_WRITE)) {
235 return True;
238 return False;
241 bool run_events(struct event_context *event_ctx,
242 int selrtn, fd_set *read_fds, fd_set *write_fds)
244 bool fired = False;
245 struct fd_event *fde, *next;
247 /* Run all events that are pending, not just one (as we
248 did previously. */
250 while (event_ctx->timed_events) {
251 struct timeval now;
252 GetTimeOfDay(&now);
254 if (timeval_compare(
255 &now, &event_ctx->timed_events->when) < 0) {
256 /* Nothing to do yet */
257 DEBUG(11, ("run_events: Nothing to do\n"));
258 break;
261 DEBUG(10, ("Running event \"%s\" %lx\n",
262 event_ctx->timed_events->event_name,
263 (unsigned long)event_ctx->timed_events));
265 event_ctx->timed_events->handler(
266 event_ctx,
267 event_ctx->timed_events, &now,
268 event_ctx->timed_events->private_data);
270 fired = True;
273 if (fired) {
275 * We might have changed the socket status during the timed
276 * events, return to run select again.
278 return True;
281 if (selrtn == 0) {
283 * No fd ready
285 return fired;
288 for (fde = event_ctx->fd_events; fde; fde = next) {
289 uint16 flags = 0;
291 next = fde->next;
292 if (FD_ISSET(fde->fd, read_fds)) flags |= EVENT_FD_READ;
293 if (FD_ISSET(fde->fd, write_fds)) flags |= EVENT_FD_WRITE;
295 if (flags & fde->flags) {
296 fde->handler(event_ctx, fde, flags, fde->private_data);
297 fired = True;
301 return fired;
305 struct timeval *get_timed_events_timeout(struct event_context *event_ctx,
306 struct timeval *to_ret)
308 struct timeval now;
310 if (event_ctx->timed_events == NULL) {
311 return NULL;
314 now = timeval_current();
315 *to_ret = timeval_until(&now, &event_ctx->timed_events->when);
317 DEBUG(10, ("timed_events_timeout: %d/%d\n", (int)to_ret->tv_sec,
318 (int)to_ret->tv_usec));
320 return to_ret;
323 int event_loop_once(struct event_context *ev)
325 struct timeval now, to;
326 fd_set r_fds, w_fds;
327 int maxfd = 0;
328 int ret;
330 FD_ZERO(&r_fds);
331 FD_ZERO(&w_fds);
333 to.tv_sec = 9999; /* Max timeout */
334 to.tv_usec = 0;
336 GetTimeOfDay(&now);
338 if (!event_add_to_select_args(ev, &now, &r_fds, &w_fds, &to, &maxfd)) {
339 return -1;
342 if (timeval_is_zero(&to)) {
343 run_events(ev, 0, NULL, NULL);
344 return 0;
347 ret = sys_select(maxfd, &r_fds, &w_fds, NULL, &to);
349 if (ret == -1 && errno != EINTR) {
350 return -1;
353 run_events(ev, ret, &r_fds, &w_fds);
354 return 0;
357 struct event_context *event_context_init(TALLOC_CTX *mem_ctx)
359 return TALLOC_ZERO_P(NULL, struct event_context);
362 int set_event_dispatch_time(struct event_context *event_ctx,
363 const char *event_name, struct timeval when)
365 struct timed_event *te;
367 for (te = event_ctx->timed_events; te; te = te->next) {
368 if (strcmp(event_name, te->event_name) == 0) {
369 DLIST_REMOVE(event_ctx->timed_events, te);
370 te->when = when;
371 add_event_by_time(te);
372 return 1;
375 return 0;
378 /* Returns 1 if event was found and cancelled, 0 otherwise. */
380 int cancel_named_event(struct event_context *event_ctx,
381 const char *event_name)
383 struct timed_event *te;
385 for (te = event_ctx->timed_events; te; te = te->next) {
386 if (strcmp(event_name, te->event_name) == 0) {
387 TALLOC_FREE(te);
388 return 1;
391 return 0;
394 void dump_event_list(struct event_context *event_ctx)
396 struct timed_event *te;
397 struct fd_event *fe;
398 struct timeval evt, now;
400 if (!event_ctx) {
401 return;
404 now = timeval_current();
406 DEBUG(10,("dump_event_list:\n"));
408 for (te = event_ctx->timed_events; te; te = te->next) {
410 evt = timeval_until(&now, &te->when);
412 DEBUGADD(10,("Timed Event \"%s\" %lx handled in %d seconds (at %s)\n",
413 te->event_name,
414 (unsigned long)te,
415 (int)evt.tv_sec,
416 http_timestring(te->when.tv_sec)));
419 for (fe = event_ctx->fd_events; fe; fe = fe->next) {
421 DEBUGADD(10,("FD Event %d %lx, flags: 0x%04x\n",
422 fe->fd,
423 (unsigned long)fe,
424 fe->flags));