Fix denial of service - memory corruption.
[Samba.git] / source3 / lib / events.c
blob63aba58947406b7b606664575ecf33d95ce67cff
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 fde->handler(ev, fde, flags, fde->private_data);
157 return true;
161 return false;
165 struct timeval *get_timed_events_timeout(struct tevent_context *ev,
166 struct timeval *to_ret)
168 struct timeval now;
170 if ((ev->timer_events == NULL) && (ev->immediate_events == NULL)) {
171 return NULL;
173 if (ev->immediate_events != NULL) {
174 *to_ret = timeval_zero();
175 return to_ret;
178 now = timeval_current();
179 *to_ret = timeval_until(&now, &ev->timer_events->next_event);
181 DEBUG(10, ("timed_events_timeout: %d/%d\n", (int)to_ret->tv_sec,
182 (int)to_ret->tv_usec));
184 return to_ret;
187 static int s3_event_loop_once(struct tevent_context *ev, const char *location)
189 struct timeval now, to;
190 fd_set r_fds, w_fds;
191 int maxfd = 0;
192 int ret;
194 FD_ZERO(&r_fds);
195 FD_ZERO(&w_fds);
197 to.tv_sec = 9999; /* Max timeout */
198 to.tv_usec = 0;
200 if (run_events(ev, 0, NULL, NULL)) {
201 return 0;
204 GetTimeOfDay(&now);
206 if (!event_add_to_select_args(ev, &now, &r_fds, &w_fds, &to, &maxfd)) {
207 return -1;
210 ret = sys_select(maxfd+1, &r_fds, &w_fds, NULL, &to);
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;
219 run_events(ev, ret, &r_fds, &w_fds);
220 return 0;
223 void event_context_reinit(struct tevent_context *ev)
225 tevent_common_context_destructor(ev);
226 return;
229 static int s3_event_context_init(struct tevent_context *ev)
231 return 0;
234 void dump_event_list(struct tevent_context *ev)
236 struct tevent_timer *te;
237 struct tevent_fd *fe;
238 struct timeval evt, now;
240 if (!ev) {
241 return;
244 now = timeval_current();
246 DEBUG(10,("dump_event_list:\n"));
248 for (te = ev->timer_events; te; te = te->next) {
250 evt = timeval_until(&now, &te->next_event);
252 DEBUGADD(10,("Timed Event \"%s\" %p handled in %d seconds (at %s)\n",
253 te->handler_name,
255 (int)evt.tv_sec,
256 http_timestring(talloc_tos(), te->next_event.tv_sec)));
259 for (fe = ev->fd_events; fe; fe = fe->next) {
261 DEBUGADD(10,("FD Event %d %p, flags: 0x%04x\n",
262 fe->fd,
264 fe->flags));
268 static 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,
281 static bool s3_tevent_init(void)
283 static bool initialized;
284 if (initialized) {
285 return true;
287 initialized = tevent_register_backend("s3", &s3_event_ops);
288 tevent_set_default_backend("s3");
289 return initialized;
293 this is used to catch debug messages from events
295 static void s3_event_debug(void *context, enum tevent_debug_level level,
296 const char *fmt, va_list ap) PRINTF_ATTRIBUTE(3,0);
298 static void s3_event_debug(void *context, enum tevent_debug_level level,
299 const char *fmt, va_list ap)
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;
318 if (vasprintf(&s, fmt, ap) == -1) {
319 return;
321 DEBUG(samba_level, ("s3_event: %s", s));
322 free(s);
325 struct tevent_context *s3_tevent_context_init(TALLOC_CTX *mem_ctx)
327 struct tevent_context *ev;
329 s3_tevent_init();
331 ev = tevent_context_init_byname(mem_ctx, "s3");
332 if (ev) {
333 tevent_set_debug(ev, s3_event_debug, NULL);
336 return ev;