s3: Fix bug #9085.
[Samba.git] / source3 / lib / events.c
blob01ea017d676160ca40b28d6f560f83808a3ec728
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"
22 #include <tevent_internal.h>
24 void event_fd_set_writeable(struct tevent_fd *fde)
26 TEVENT_FD_WRITEABLE(fde);
29 void event_fd_set_not_writeable(struct tevent_fd *fde)
31 TEVENT_FD_NOT_WRITEABLE(fde);
34 void event_fd_set_readable(struct tevent_fd *fde)
36 TEVENT_FD_READABLE(fde);
39 void event_fd_set_not_readable(struct tevent_fd *fde)
41 TEVENT_FD_NOT_READABLE(fde);
45 * Return if there's something in the queue
48 bool 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)
53 struct tevent_fd *fde;
54 struct timeval diff;
55 bool ret = false;
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;
66 if (fde->flags & EVENT_FD_READ) {
67 FD_SET(fde->fd, read_fds);
68 ret = true;
70 if (fde->flags & EVENT_FD_WRITE) {
71 FD_SET(fde->fd, write_fds);
72 ret = true;
75 if ((fde->flags & (EVENT_FD_READ|EVENT_FD_WRITE))
76 && (fde->fd > *maxfd)) {
77 *maxfd = fde->fd;
81 if (ev->immediate_events != NULL) {
82 *timeout = timeval_zero();
83 return true;
86 if (ev->timer_events == NULL) {
87 return ret;
90 diff = timeval_until(now, &ev->timer_events->next_event);
91 *timeout = timeval_min(timeout, &diff);
93 return true;
96 bool run_events(struct tevent_context *ev,
97 int selrtn, fd_set *read_fds, fd_set *write_fds)
99 struct tevent_fd *fde;
100 struct timeval now;
102 if (ev->signal_events &&
103 tevent_common_check_signal(ev)) {
104 return true;
107 if (ev->immediate_events &&
108 tevent_common_loop_immediate(ev)) {
109 return true;
112 GetTimeOfDay(&now);
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 */
127 struct tevent_timer *te = ev->timer_events;
128 TALLOC_CTX *tmp_ctx = talloc_new(ev);
130 DEBUG(10, ("Running timed event \"%s\" %p\n",
131 ev->timer_events->handler_name, ev->timer_events));
133 DLIST_REMOVE(ev->timer_events, te);
134 talloc_steal(tmp_ctx, te);
136 te->handler(ev, te, now, te->private_data);
138 talloc_free(tmp_ctx);
139 return true;
142 if (selrtn <= 0) {
144 * No fd ready
146 return false;
149 for (fde = ev->fd_events; fde; fde = fde->next) {
150 uint16 flags = 0;
152 if (FD_ISSET(fde->fd, read_fds)) flags |= EVENT_FD_READ;
153 if (FD_ISSET(fde->fd, write_fds)) flags |= EVENT_FD_WRITE;
155 if (flags & fde->flags) {
156 DLIST_DEMOTE(ev->fd_events, fde, struct tevent_fd *);
157 fde->handler(ev, fde, flags, fde->private_data);
158 return true;
162 return false;
166 struct timeval *get_timed_events_timeout(struct tevent_context *ev,
167 struct timeval *to_ret)
169 struct timeval now;
171 if ((ev->timer_events == NULL) && (ev->immediate_events == NULL)) {
172 return NULL;
174 if (ev->immediate_events != NULL) {
175 *to_ret = timeval_zero();
176 return to_ret;
179 now = timeval_current();
180 *to_ret = timeval_until(&now, &ev->timer_events->next_event);
182 DEBUG(10, ("timed_events_timeout: %d/%d\n", (int)to_ret->tv_sec,
183 (int)to_ret->tv_usec));
185 return to_ret;
188 static int s3_event_loop_once(struct tevent_context *ev, const char *location)
190 struct timeval now, to;
191 fd_set r_fds, w_fds;
192 int maxfd = 0;
193 int ret;
195 FD_ZERO(&r_fds);
196 FD_ZERO(&w_fds);
198 to.tv_sec = 9999; /* Max timeout */
199 to.tv_usec = 0;
201 if (run_events(ev, 0, NULL, NULL)) {
202 return 0;
205 GetTimeOfDay(&now);
207 if (!event_add_to_select_args(ev, &now, &r_fds, &w_fds, &to, &maxfd)) {
208 return -1;
211 ret = sys_select(maxfd+1, &r_fds, &w_fds, NULL, &to);
213 if (ret == -1 && errno != EINTR) {
214 tevent_debug(ev, TEVENT_DEBUG_FATAL,
215 "sys_select() failed: %d:%s\n",
216 errno, strerror(errno));
217 return -1;
220 run_events(ev, ret, &r_fds, &w_fds);
221 return 0;
224 void event_context_reinit(struct tevent_context *ev)
226 tevent_common_context_destructor(ev);
227 return;
230 static int s3_event_context_init(struct tevent_context *ev)
232 return 0;
235 void dump_event_list(struct tevent_context *ev)
237 struct tevent_timer *te;
238 struct tevent_fd *fe;
239 struct timeval evt, now;
241 if (!ev) {
242 return;
245 now = timeval_current();
247 DEBUG(10,("dump_event_list:\n"));
249 for (te = ev->timer_events; te; te = te->next) {
251 evt = timeval_until(&now, &te->next_event);
253 DEBUGADD(10,("Timed Event \"%s\" %p handled in %d seconds (at %s)\n",
254 te->handler_name,
256 (int)evt.tv_sec,
257 http_timestring(talloc_tos(), te->next_event.tv_sec)));
260 for (fe = ev->fd_events; fe; fe = fe->next) {
262 DEBUGADD(10,("FD Event %d %p, flags: 0x%04x\n",
263 fe->fd,
265 fe->flags));
269 static const struct tevent_ops s3_event_ops = {
270 .context_init = s3_event_context_init,
271 .add_fd = tevent_common_add_fd,
272 .set_fd_close_fn = tevent_common_fd_set_close_fn,
273 .get_fd_flags = tevent_common_fd_get_flags,
274 .set_fd_flags = tevent_common_fd_set_flags,
275 .add_timer = tevent_common_add_timer,
276 .schedule_immediate = tevent_common_schedule_immediate,
277 .add_signal = tevent_common_add_signal,
278 .loop_once = s3_event_loop_once,
279 .loop_wait = tevent_common_loop_wait,
282 static bool s3_tevent_init(void)
284 static bool initialized;
285 if (initialized) {
286 return true;
288 initialized = tevent_register_backend("s3", &s3_event_ops);
289 tevent_set_default_backend("s3");
290 return initialized;
294 this is used to catch debug messages from events
296 static void s3_event_debug(void *context, enum tevent_debug_level level,
297 const char *fmt, va_list ap) PRINTF_ATTRIBUTE(3,0);
299 static void s3_event_debug(void *context, enum tevent_debug_level level,
300 const char *fmt, va_list ap)
302 int samba_level = -1;
303 char *s = NULL;
304 switch (level) {
305 case TEVENT_DEBUG_FATAL:
306 samba_level = 0;
307 break;
308 case TEVENT_DEBUG_ERROR:
309 samba_level = 1;
310 break;
311 case TEVENT_DEBUG_WARNING:
312 samba_level = 2;
313 break;
314 case TEVENT_DEBUG_TRACE:
315 samba_level = 11;
316 break;
319 if (vasprintf(&s, fmt, ap) == -1) {
320 return;
322 DEBUG(samba_level, ("s3_event: %s", s));
323 free(s);
326 struct tevent_context *s3_tevent_context_init(TALLOC_CTX *mem_ctx)
328 struct tevent_context *ev;
330 s3_tevent_init();
332 ev = tevent_context_init_byname(mem_ctx, "s3");
333 if (ev) {
334 tevent_set_debug(ev, s3_event_debug, NULL);
337 return ev;