source: trunk/server/source3/lib/events.c@ 593

Last change on this file since 593 was 593, checked in by Herwig Bauernfeind, 14 years ago

Samba 3.5: Update trunk to 3.5.7

File size: 7.8 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3 Timed event library.
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Volker Lendecke 2005
6
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.
11
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.
16
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/>.
19*/
20
21#include "includes.h"
22#include <tevent_internal.h>
23
24void event_fd_set_writeable(struct tevent_fd *fde)
25{
26 TEVENT_FD_WRITEABLE(fde);
27}
28
29void event_fd_set_not_writeable(struct tevent_fd *fde)
30{
31 TEVENT_FD_NOT_WRITEABLE(fde);
32}
33
34void event_fd_set_readable(struct tevent_fd *fde)
35{
36 TEVENT_FD_READABLE(fde);
37}
38
39void event_fd_set_not_readable(struct tevent_fd *fde)
40{
41 TEVENT_FD_NOT_READABLE(fde);
42}
43
44/*
45 * Return if there's something in the queue
46 */
47
48bool event_add_to_select_args(struct tevent_context *ev,
49 const struct timeval *now,
50 fd_set *read_fds, fd_set *write_fds,
51 struct timeval *timeout, int *maxfd)
52{
53 struct tevent_fd *fde;
54 struct timeval diff;
55 bool ret = false;
56
57 for (fde = ev->fd_events; fde; fde = fde->next) {
58 if (fde->fd < 0 || fde->fd >= FD_SETSIZE) {
59 /* We ignore here, as it shouldn't be
60 possible to add an invalid fde->fd
61 but we don't want FD_SET to see an
62 invalid fd. */
63 continue;
64 }
65
66 if (fde->flags & EVENT_FD_READ) {
67 FD_SET(fde->fd, read_fds);
68 ret = true;
69 }
70 if (fde->flags & EVENT_FD_WRITE) {
71 FD_SET(fde->fd, write_fds);
72 ret = true;
73 }
74
75 if ((fde->flags & (EVENT_FD_READ|EVENT_FD_WRITE))
76 && (fde->fd > *maxfd)) {
77 *maxfd = fde->fd;
78 }
79 }
80
81 if (ev->immediate_events != NULL) {
82 *timeout = timeval_zero();
83 return true;
84 }
85
86 if (ev->timer_events == NULL) {
87 return ret;
88 }
89
90 diff = timeval_until(now, &ev->timer_events->next_event);
91 *timeout = timeval_min(timeout, &diff);
92
93 return true;
94}
95
96bool run_events(struct tevent_context *ev,
97 int selrtn, fd_set *read_fds, fd_set *write_fds)
98{
99 struct tevent_fd *fde;
100 struct timeval now;
101
102 if (ev->signal_events &&
103 tevent_common_check_signal(ev)) {
104 return true;
105 }
106
107 if (ev->immediate_events &&
108 tevent_common_loop_immediate(ev)) {
109 return true;
110 }
111
112 GetTimeOfDay(&now);
113
114 if ((ev->timer_events != NULL)
115 && (timeval_compare(&now, &ev->timer_events->next_event) >= 0)) {
116 /* this older events system did not auto-free timed
117 events on running them, and had a race condition
118 where the event could be called twice if the
119 talloc_free of the te happened after the callback
120 made a call which invoked the event loop. To avoid
121 this while still allowing old code which frees the
122 te, we need to create a temporary context which
123 will be used to ensure the te is freed. We also
124 remove the te from the timed event list before we
125 call the handler, to ensure we can't loop */
126
127 struct tevent_timer *te = ev->timer_events;
128 TALLOC_CTX *tmp_ctx = talloc_new(ev);
129
130 DEBUG(10, ("Running timed event \"%s\" %p\n",
131 ev->timer_events->handler_name, ev->timer_events));
132
133 DLIST_REMOVE(ev->timer_events, te);
134 talloc_steal(tmp_ctx, te);
135
136 te->handler(ev, te, now, te->private_data);
137
138 talloc_free(tmp_ctx);
139 return true;
140 }
141
142 if (selrtn == 0) {
143 /*
144 * No fd ready
145 */
146 return false;
147 }
148
149 for (fde = ev->fd_events; fde; fde = fde->next) {
150 uint16 flags = 0;
151
152 if (FD_ISSET(fde->fd, read_fds)) flags |= EVENT_FD_READ;
153 if (FD_ISSET(fde->fd, write_fds)) flags |= EVENT_FD_WRITE;
154
155 if (flags & fde->flags) {
156 fde->handler(ev, fde, flags, fde->private_data);
157 return true;
158 }
159 }
160
161 return false;
162}
163
164
165struct timeval *get_timed_events_timeout(struct tevent_context *ev,
166 struct timeval *to_ret)
167{
168 struct timeval now;
169
170 if ((ev->timer_events == NULL) && (ev->immediate_events == NULL)) {
171 return NULL;
172 }
173 if (ev->immediate_events != NULL) {
174 *to_ret = timeval_zero();
175 return to_ret;
176 }
177
178 now = timeval_current();
179 *to_ret = timeval_until(&now, &ev->timer_events->next_event);
180
181 DEBUG(10, ("timed_events_timeout: %d/%d\n", (int)to_ret->tv_sec,
182 (int)to_ret->tv_usec));
183
184 return to_ret;
185}
186
187static int s3_event_loop_once(struct tevent_context *ev, const char *location)
188{
189 struct timeval now, to;
190 fd_set r_fds, w_fds;
191 int maxfd = 0;
192 int ret;
193
194 FD_ZERO(&r_fds);
195 FD_ZERO(&w_fds);
196
197 to.tv_sec = 9999; /* Max timeout */
198 to.tv_usec = 0;
199
200 if (run_events(ev, 0, NULL, NULL)) {
201 return 0;
202 }
203
204 GetTimeOfDay(&now);
205
206 if (!event_add_to_select_args(ev, &now, &r_fds, &w_fds, &to, &maxfd)) {
207 return -1;
208 }
209
210 ret = sys_select(maxfd+1, &r_fds, &w_fds, NULL, &to);
211
212 if (ret == -1 && errno != EINTR) {
213 tevent_debug(ev, TEVENT_DEBUG_FATAL,
214 "sys_select() failed: %d:%s\n",
215 errno, strerror(errno));
216 return -1;
217 }
218
219 run_events(ev, ret, &r_fds, &w_fds);
220 return 0;
221}
222
223void event_context_reinit(struct tevent_context *ev)
224{
225 tevent_common_context_destructor(ev);
226 return;
227}
228
229static int s3_event_context_init(struct tevent_context *ev)
230{
231 return 0;
232}
233
234void dump_event_list(struct tevent_context *ev)
235{
236 struct tevent_timer *te;
237 struct tevent_fd *fe;
238 struct timeval evt, now;
239
240 if (!ev) {
241 return;
242 }
243
244 now = timeval_current();
245
246 DEBUG(10,("dump_event_list:\n"));
247
248 for (te = ev->timer_events; te; te = te->next) {
249
250 evt = timeval_until(&now, &te->next_event);
251
252 DEBUGADD(10,("Timed Event \"%s\" %p handled in %d seconds (at %s)\n",
253 te->handler_name,
254 te,
255 (int)evt.tv_sec,
256 http_timestring(talloc_tos(), te->next_event.tv_sec)));
257 }
258
259 for (fe = ev->fd_events; fe; fe = fe->next) {
260
261 DEBUGADD(10,("FD Event %d %p, flags: 0x%04x\n",
262 fe->fd,
263 fe,
264 fe->flags));
265 }
266}
267
268static const struct tevent_ops s3_event_ops = {
269 .context_init = s3_event_context_init,
270 .add_fd = tevent_common_add_fd,
271 .set_fd_close_fn = tevent_common_fd_set_close_fn,
272 .get_fd_flags = tevent_common_fd_get_flags,
273 .set_fd_flags = tevent_common_fd_set_flags,
274 .add_timer = tevent_common_add_timer,
275 .schedule_immediate = tevent_common_schedule_immediate,
276 .add_signal = tevent_common_add_signal,
277 .loop_once = s3_event_loop_once,
278 .loop_wait = tevent_common_loop_wait,
279};
280
281static bool s3_tevent_init(void)
282{
283 static bool initialized;
284 if (initialized) {
285 return true;
286 }
287 initialized = tevent_register_backend("s3", &s3_event_ops);
288 tevent_set_default_backend("s3");
289 return initialized;
290}
291
292/*
293 this is used to catch debug messages from events
294*/
295static void s3_event_debug(void *context, enum tevent_debug_level level,
296 const char *fmt, va_list ap) PRINTF_ATTRIBUTE(3,0);
297
298static void s3_event_debug(void *context, enum tevent_debug_level level,
299 const char *fmt, va_list ap)
300{
301 int samba_level = -1;
302 char *s = NULL;
303 switch (level) {
304 case TEVENT_DEBUG_FATAL:
305 samba_level = 0;
306 break;
307 case TEVENT_DEBUG_ERROR:
308 samba_level = 1;
309 break;
310 case TEVENT_DEBUG_WARNING:
311 samba_level = 2;
312 break;
313 case TEVENT_DEBUG_TRACE:
314 samba_level = 11;
315 break;
316
317 };
318 if (vasprintf(&s, fmt, ap) == -1) {
319 return;
320 }
321 DEBUG(samba_level, ("s3_event: %s", s));
322 free(s);
323}
324
325struct tevent_context *s3_tevent_context_init(TALLOC_CTX *mem_ctx)
326{
327 struct tevent_context *ev;
328
329 s3_tevent_init();
330
331 ev = tevent_context_init_byname(mem_ctx, "s3");
332 if (ev) {
333 tevent_set_debug(ev, s3_event_debug, NULL);
334 }
335
336 return ev;
337}
338
Note: See TracBrowser for help on using the repository browser.