1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
|
/*
Unix SMB/CIFS implementation.
Timed event library.
Copyright (C) Andrew Tridgell 1992-1998
Copyright (C) Volker Lendecke 2005
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "includes.h"
struct timed_event {
struct timed_event *next, *prev;
struct event_context *event_ctx;
struct timeval when;
const char *event_name;
void (*handler)(struct event_context *event_ctx,
struct timed_event *te,
const struct timeval *now,
void *private_data);
void *private_data;
};
struct fd_event {
struct fd_event *prev, *next;
struct event_context *event_ctx;
int fd;
uint16_t flags; /* see EVENT_FD_* flags */
void (*handler)(struct event_context *event_ctx,
struct fd_event *event,
uint16 flags,
void *private_data);
void *private_data;
};
#define EVENT_FD_WRITEABLE(fde) \
event_set_fd_flags(fde, event_get_fd_flags(fde) | EVENT_FD_WRITE)
#define EVENT_FD_READABLE(fde) \
event_set_fd_flags(fde, event_get_fd_flags(fde) | EVENT_FD_READ)
#define EVENT_FD_NOT_WRITEABLE(fde) \
event_set_fd_flags(fde, event_get_fd_flags(fde) & ~EVENT_FD_WRITE)
#define EVENT_FD_NOT_READABLE(fde) \
event_set_fd_flags(fde, event_get_fd_flags(fde) & ~EVENT_FD_READ)
struct event_context {
struct timed_event *timed_events;
struct fd_event *fd_events;
};
static int timed_event_destructor(struct timed_event *te)
{
DEBUG(10, ("Destroying timed event %lx \"%s\"\n", (unsigned long)te,
te->event_name));
DLIST_REMOVE(te->event_ctx->timed_events, te);
return 0;
}
/****************************************************************************
Add te by time.
****************************************************************************/
static void add_event_by_time(struct timed_event *te)
{
struct event_context *ctx = te->event_ctx;
struct timed_event *last_te, *cur_te;
/* Keep the list ordered by time. We must preserve this. */
last_te = NULL;
for (cur_te = ctx->timed_events; cur_te; cur_te = cur_te->next) {
/* if the new event comes before the current one break */
if (!timeval_is_zero(&cur_te->when) &&
timeval_compare(&te->when, &cur_te->when) < 0) {
break;
}
last_te = cur_te;
}
DLIST_ADD_AFTER(ctx->timed_events, te, last_te);
}
/****************************************************************************
Schedule a function for future calling, cancel with TALLOC_FREE().
It's the responsibility of the handler to call TALLOC_FREE() on the event
handed to it.
****************************************************************************/
struct timed_event *event_add_timed(struct event_context *event_ctx,
TALLOC_CTX *mem_ctx,
struct timeval when,
const char *event_name,
void (*handler)(struct event_context *event_ctx,
struct timed_event *te,
const struct timeval *now,
void *private_data),
void *private_data)
{
struct timed_event *te;
te = TALLOC_P(mem_ctx, struct timed_event);
if (te == NULL) {
DEBUG(0, ("talloc failed\n"));
return NULL;
}
te->event_ctx = event_ctx;
te->when = when;
te->event_name = event_name;
te->handler = handler;
te->private_data = private_data;
add_event_by_time(te);
talloc_set_destructor(te, timed_event_destructor);
DEBUG(10, ("Added timed event \"%s\": %lx\n", event_name,
(unsigned long)te));
return te;
}
static int fd_event_destructor(struct fd_event *fde)
{
struct event_context *event_ctx = fde->event_ctx;
DLIST_REMOVE(event_ctx->fd_events, fde);
return 0;
}
struct fd_event *event_add_fd(struct event_context *event_ctx,
TALLOC_CTX *mem_ctx,
int fd, uint16_t flags,
void (*handler)(struct event_context *event_ctx,
struct fd_event *event,
uint16 flags,
void *private_data),
void *private_data)
{
struct fd_event *fde;
if (!(fde = TALLOC_P(mem_ctx, struct fd_event))) {
return NULL;
}
fde->event_ctx = event_ctx;
fde->fd = fd;
fde->flags = flags;
fde->handler = handler;
fde->private_data = private_data;
DLIST_ADD(event_ctx->fd_events, fde);
talloc_set_destructor(fde, fd_event_destructor);
return fde;
}
void event_fd_set_writeable(struct fd_event *fde)
{
fde->flags |= EVENT_FD_WRITE;
}
void event_fd_set_not_writeable(struct fd_event *fde)
{
fde->flags &= ~EVENT_FD_WRITE;
}
void event_fd_set_readable(struct fd_event *fde)
{
fde->flags |= EVENT_FD_READ;
}
void event_fd_set_not_readable(struct fd_event *fde)
{
fde->flags &= ~EVENT_FD_READ;
}
/*
* Return if there's something in the queue
*/
bool event_add_to_select_args(struct event_context *event_ctx,
const struct timeval *now,
fd_set *read_fds, fd_set *write_fds,
struct timeval *timeout, int *maxfd)
{
struct fd_event *fde;
struct timeval diff;
bool ret = False;
for (fde = event_ctx->fd_events; fde; fde = fde->next) {
if (fde->flags & EVENT_FD_READ) {
FD_SET(fde->fd, read_fds);
ret = True;
}
if (fde->flags & EVENT_FD_WRITE) {
FD_SET(fde->fd, write_fds);
ret = True;
}
if ((fde->flags & (EVENT_FD_READ|EVENT_FD_WRITE))
&& (fde->fd > *maxfd)) {
*maxfd = fde->fd;
}
}
if (event_ctx->timed_events == NULL) {
return ret;
}
diff = timeval_until(now, &event_ctx->timed_events->when);
*timeout = timeval_min(timeout, &diff);
return True;
}
bool events_pending(struct event_context *event_ctx)
{
struct fd_event *fde;
if (event_ctx->timed_events != NULL) {
return True;
}
for (fde = event_ctx->fd_events; fde; fde = fde->next) {
if (fde->flags & (EVENT_FD_READ|EVENT_FD_WRITE)) {
return True;
}
}
return False;
}
bool run_events(struct event_context *event_ctx,
int selrtn, fd_set *read_fds, fd_set *write_fds)
{
bool fired = False;
struct fd_event *fde, *next;
/* Run all events that are pending, not just one (as we
did previously. */
while (event_ctx->timed_events) {
struct timeval now;
GetTimeOfDay(&now);
if (timeval_compare(
&now, &event_ctx->timed_events->when) < 0) {
/* Nothing to do yet */
DEBUG(11, ("run_events: Nothing to do\n"));
break;
}
DEBUG(10, ("Running event \"%s\" %lx\n",
event_ctx->timed_events->event_name,
(unsigned long)event_ctx->timed_events));
event_ctx->timed_events->handler(
event_ctx,
event_ctx->timed_events, &now,
event_ctx->timed_events->private_data);
fired = True;
}
if (fired) {
/*
* We might have changed the socket status during the timed
* events, return to run select again.
*/
return True;
}
if (selrtn == 0) {
/*
* No fd ready
*/
return fired;
}
for (fde = event_ctx->fd_events; fde; fde = next) {
uint16 flags = 0;
next = fde->next;
if (FD_ISSET(fde->fd, read_fds)) flags |= EVENT_FD_READ;
if (FD_ISSET(fde->fd, write_fds)) flags |= EVENT_FD_WRITE;
if (flags & fde->flags) {
fde->handler(event_ctx, fde, flags, fde->private_data);
fired = True;
}
}
return fired;
}
struct timeval *get_timed_events_timeout(struct event_context *event_ctx,
struct timeval *to_ret)
{
struct timeval now;
if (event_ctx->timed_events == NULL) {
return NULL;
}
now = timeval_current();
*to_ret = timeval_until(&now, &event_ctx->timed_events->when);
DEBUG(10, ("timed_events_timeout: %d/%d\n", (int)to_ret->tv_sec,
(int)to_ret->tv_usec));
return to_ret;
}
int event_loop_once(struct event_context *ev)
{
struct timeval now, to;
fd_set r_fds, w_fds;
int maxfd = 0;
int ret;
FD_ZERO(&r_fds);
FD_ZERO(&w_fds);
to.tv_sec = 9999; /* Max timeout */
to.tv_usec = 0;
GetTimeOfDay(&now);
if (!event_add_to_select_args(ev, &now, &r_fds, &w_fds, &to, &maxfd)) {
return -1;
}
if (timeval_is_zero(&to)) {
run_events(ev, 0, NULL, NULL);
return 0;
}
ret = sys_select(maxfd+1, &r_fds, &w_fds, NULL, &to);
if (ret == -1 && errno != EINTR) {
return -1;
}
run_events(ev, ret, &r_fds, &w_fds);
return 0;
}
struct event_context *event_context_init(TALLOC_CTX *mem_ctx)
{
return TALLOC_ZERO_P(mem_ctx, struct event_context);
}
int set_event_dispatch_time(struct event_context *event_ctx,
const char *event_name, struct timeval when)
{
struct timed_event *te;
for (te = event_ctx->timed_events; te; te = te->next) {
if (strcmp(event_name, te->event_name) == 0) {
DLIST_REMOVE(event_ctx->timed_events, te);
te->when = when;
add_event_by_time(te);
return 1;
}
}
return 0;
}
/* Returns 1 if event was found and cancelled, 0 otherwise. */
int cancel_named_event(struct event_context *event_ctx,
const char *event_name)
{
struct timed_event *te;
for (te = event_ctx->timed_events; te; te = te->next) {
if (strcmp(event_name, te->event_name) == 0) {
TALLOC_FREE(te);
return 1;
}
}
return 0;
}
void dump_event_list(struct event_context *event_ctx)
{
struct timed_event *te;
struct fd_event *fe;
struct timeval evt, now;
if (!event_ctx) {
return;
}
now = timeval_current();
DEBUG(10,("dump_event_list:\n"));
for (te = event_ctx->timed_events; te; te = te->next) {
evt = timeval_until(&now, &te->when);
DEBUGADD(10,("Timed Event \"%s\" %lx handled in %d seconds (at %s)\n",
te->event_name,
(unsigned long)te,
(int)evt.tv_sec,
http_timestring(te->when.tv_sec)));
}
for (fe = event_ctx->fd_events; fe; fe = fe->next) {
DEBUGADD(10,("FD Event %d %lx, flags: 0x%04x\n",
fe->fd,
(unsigned long)fe,
fe->flags));
}
}
|