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
|
/*
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"
#include <tevent_internal.h>
void event_fd_set_writeable(struct tevent_fd *fde)
{
TEVENT_FD_WRITEABLE(fde);
}
void event_fd_set_not_writeable(struct tevent_fd *fde)
{
TEVENT_FD_NOT_WRITEABLE(fde);
}
void event_fd_set_readable(struct tevent_fd *fde)
{
TEVENT_FD_READABLE(fde);
}
void event_fd_set_not_readable(struct tevent_fd *fde)
{
TEVENT_FD_NOT_READABLE(fde);
}
/*
* Return if there's something in the queue
*/
bool event_add_to_select_args(struct tevent_context *ev,
const struct timeval *now,
fd_set *read_fds, fd_set *write_fds,
struct timeval *timeout, int *maxfd)
{
struct tevent_fd *fde;
struct timeval diff;
bool ret = false;
for (fde = ev->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 (ev->timer_events == NULL) {
return ret;
}
diff = timeval_until(now, &ev->timer_events->next_event);
*timeout = timeval_min(timeout, &diff);
return true;
}
bool run_events(struct tevent_context *ev,
int selrtn, fd_set *read_fds, fd_set *write_fds)
{
struct tevent_fd *fde;
struct timeval now;
if (ev->signal_events &&
tevent_common_check_signal(ev)) {
return true;
}
GetTimeOfDay(&now);
if ((ev->timer_events != NULL)
&& (timeval_compare(&now, &ev->timer_events->next_event) >= 0)) {
DEBUG(10, ("Running timed event \"%s\" %p\n",
ev->timer_events->handler_name, ev->timer_events));
ev->timer_events->handler(ev, ev->timer_events, now,
ev->timer_events->private_data);
return true;
}
if (selrtn == 0) {
/*
* No fd ready
*/
return false;
}
for (fde = ev->fd_events; fde; fde = fde->next) {
uint16 flags = 0;
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(ev, fde, flags, fde->private_data);
return true;
}
}
return false;
}
struct timeval *get_timed_events_timeout(struct tevent_context *ev,
struct timeval *to_ret)
{
struct timeval now;
if (ev->timer_events == NULL) {
return NULL;
}
now = timeval_current();
*to_ret = timeval_until(&now, &ev->timer_events->next_event);
DEBUG(10, ("timed_events_timeout: %d/%d\n", (int)to_ret->tv_sec,
(int)to_ret->tv_usec));
return to_ret;
}
static int s3_event_loop_once(struct tevent_context *ev, const char *location)
{
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;
if (run_events(ev, 0, NULL, NULL)) {
return 0;
}
GetTimeOfDay(&now);
if (!event_add_to_select_args(ev, &now, &r_fds, &w_fds, &to, &maxfd)) {
return -1;
}
ret = sys_select(maxfd+1, &r_fds, &w_fds, NULL, &to);
if (ret == -1 && errno != EINTR) {
tevent_debug(ev, TEVENT_DEBUG_FATAL,
"sys_select() failed: %d:%s\n",
errno, strerror(errno));
return -1;
}
run_events(ev, ret, &r_fds, &w_fds);
return 0;
}
static int s3_event_loop_wait(struct tevent_context *ev, const char *location)
{
int ret = 0;
while (ret == 0) {
ret = s3_event_loop_once(ev, location);
}
return ret;
}
void event_context_reinit(struct tevent_context *ev)
{
tevent_common_context_destructor(ev);
return;
}
static int s3_event_context_init(struct tevent_context *ev)
{
return 0;
}
void dump_event_list(struct tevent_context *ev)
{
struct tevent_timer *te;
struct tevent_fd *fe;
struct timeval evt, now;
if (!ev) {
return;
}
now = timeval_current();
DEBUG(10,("dump_event_list:\n"));
for (te = ev->timer_events; te; te = te->next) {
evt = timeval_until(&now, &te->next_event);
DEBUGADD(10,("Timed Event \"%s\" %p handled in %d seconds (at %s)\n",
te->handler_name,
te,
(int)evt.tv_sec,
http_timestring(talloc_tos(), te->next_event.tv_sec)));
}
for (fe = ev->fd_events; fe; fe = fe->next) {
DEBUGADD(10,("FD Event %d %p, flags: 0x%04x\n",
fe->fd,
fe,
fe->flags));
}
}
static const struct tevent_ops s3_event_ops = {
.context_init = s3_event_context_init,
.add_fd = tevent_common_add_fd,
.set_fd_close_fn= tevent_common_fd_set_close_fn,
.get_fd_flags = tevent_common_fd_get_flags,
.set_fd_flags = tevent_common_fd_set_flags,
.add_timer = tevent_common_add_timer,
.add_signal = tevent_common_add_signal,
.loop_once = s3_event_loop_once,
.loop_wait = s3_event_loop_wait,
};
static bool s3_tevent_init(void)
{
static bool initialized;
if (initialized) {
return true;
}
initialized = tevent_register_backend("s3", &s3_event_ops);
tevent_set_default_backend("s3");
return initialized;
}
/*
this is used to catch debug messages from events
*/
static void s3_event_debug(void *context, enum tevent_debug_level level,
const char *fmt, va_list ap) PRINTF_ATTRIBUTE(3,0);
static void s3_event_debug(void *context, enum tevent_debug_level level,
const char *fmt, va_list ap)
{
int samba_level = -1;
char *s = NULL;
switch (level) {
case TEVENT_DEBUG_FATAL:
samba_level = 0;
break;
case TEVENT_DEBUG_ERROR:
samba_level = 1;
break;
case TEVENT_DEBUG_WARNING:
samba_level = 2;
break;
case TEVENT_DEBUG_TRACE:
samba_level = 10;
break;
};
if (vasprintf(&s, fmt, ap) == -1) {
return;
}
DEBUG(samba_level, ("s3_event: %s", s));
free(s);
}
struct tevent_context *s3_tevent_context_init(TALLOC_CTX *mem_ctx)
{
struct tevent_context *ev;
s3_tevent_init();
ev = tevent_context_init_byname(mem_ctx, "s3");
if (ev) {
tevent_set_debug(ev, s3_event_debug, NULL);
}
return ev;
}
|