s4:rootdse LDB module - remove unused variable
[Samba/gebeck_regimport.git] / source3 / lib / events.c
blob0e127f02dc214c340a5907dea9119edba10395c9
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>
23 #include "../lib/util/select.h"
26 * Return if there's something in the queue
29 bool event_add_to_select_args(struct tevent_context *ev,
30 const struct timeval *now,
31 fd_set *read_fds, fd_set *write_fds,
32 struct timeval *timeout, int *maxfd)
34 struct tevent_fd *fde;
35 struct timeval diff;
36 bool ret = false;
38 for (fde = ev->fd_events; fde; fde = fde->next) {
39 if (fde->flags & EVENT_FD_READ) {
40 FD_SET(fde->fd, read_fds);
41 ret = true;
43 if (fde->flags & EVENT_FD_WRITE) {
44 FD_SET(fde->fd, write_fds);
45 ret = true;
48 if ((fde->flags & (EVENT_FD_READ|EVENT_FD_WRITE))
49 && (fde->fd > *maxfd)) {
50 *maxfd = fde->fd;
54 if (ev->immediate_events != NULL) {
55 *timeout = timeval_zero();
56 return true;
59 if (ev->timer_events == NULL) {
60 return ret;
63 diff = timeval_until(now, &ev->timer_events->next_event);
64 *timeout = timeval_min(timeout, &diff);
66 return true;
69 bool run_events(struct tevent_context *ev,
70 int *selrtn, fd_set *read_fds, fd_set *write_fds)
72 struct tevent_fd *fde;
73 struct timeval now;
75 if (ev->signal_events &&
76 tevent_common_check_signal(ev)) {
77 return true;
80 if (ev->immediate_events &&
81 tevent_common_loop_immediate(ev)) {
82 return true;
85 GetTimeOfDay(&now);
87 if ((ev->timer_events != NULL)
88 && (timeval_compare(&now, &ev->timer_events->next_event) >= 0)) {
89 /* this older events system did not auto-free timed
90 events on running them, and had a race condition
91 where the event could be called twice if the
92 talloc_free of the te happened after the callback
93 made a call which invoked the event loop. To avoid
94 this while still allowing old code which frees the
95 te, we need to create a temporary context which
96 will be used to ensure the te is freed. We also
97 remove the te from the timed event list before we
98 call the handler, to ensure we can't loop */
100 struct tevent_timer *te = ev->timer_events;
101 TALLOC_CTX *tmp_ctx = talloc_new(ev);
103 DEBUG(10, ("Running timed event \"%s\" %p\n",
104 ev->timer_events->handler_name, ev->timer_events));
106 DLIST_REMOVE(ev->timer_events, te);
107 talloc_steal(tmp_ctx, te);
109 te->handler(ev, te, now, te->private_data);
111 talloc_free(tmp_ctx);
112 return true;
115 if (*selrtn <= 0) {
117 * No fd ready
119 return false;
122 for (fde = ev->fd_events; fde; fde = fde->next) {
123 uint16 flags = 0;
125 if (FD_ISSET(fde->fd, read_fds)) {
126 flags |= EVENT_FD_READ;
127 FD_CLR(fde->fd, read_fds);
128 (*selrtn)--;
130 if (FD_ISSET(fde->fd, write_fds)) {
131 flags |= EVENT_FD_WRITE;
132 FD_CLR(fde->fd, write_fds);
133 (*selrtn)--;
136 if (flags & fde->flags) {
137 fde->handler(ev, fde, flags, fde->private_data);
138 return true;
142 return false;
146 struct timeval *get_timed_events_timeout(struct tevent_context *ev,
147 struct timeval *to_ret)
149 struct timeval now;
151 if ((ev->timer_events == NULL) && (ev->immediate_events == NULL)) {
152 return NULL;
154 if (ev->immediate_events != NULL) {
155 *to_ret = timeval_zero();
156 return to_ret;
159 now = timeval_current();
160 *to_ret = timeval_until(&now, &ev->timer_events->next_event);
162 DEBUG(10, ("timed_events_timeout: %d/%d\n", (int)to_ret->tv_sec,
163 (int)to_ret->tv_usec));
165 return to_ret;
168 static int s3_event_loop_once(struct tevent_context *ev, const char *location)
170 struct timeval now, to;
171 fd_set r_fds, w_fds;
172 int maxfd = 0;
173 int ret = 0;
175 FD_ZERO(&r_fds);
176 FD_ZERO(&w_fds);
178 to.tv_sec = 9999; /* Max timeout */
179 to.tv_usec = 0;
181 if (run_events(ev, &ret, NULL, NULL)) {
182 return 0;
185 GetTimeOfDay(&now);
187 if (!event_add_to_select_args(ev, &now, &r_fds, &w_fds, &to, &maxfd)) {
188 return -1;
191 ret = sys_select(maxfd+1, &r_fds, &w_fds, NULL, &to);
193 if (ret == -1 && errno != EINTR) {
194 tevent_debug(ev, TEVENT_DEBUG_FATAL,
195 "sys_select() failed: %d:%s\n",
196 errno, strerror(errno));
197 return -1;
200 run_events(ev, &ret, &r_fds, &w_fds);
201 return 0;
204 static int s3_event_context_init(struct tevent_context *ev)
206 return 0;
209 void dump_event_list(struct tevent_context *ev)
211 struct tevent_timer *te;
212 struct tevent_fd *fe;
213 struct timeval evt, now;
215 if (!ev) {
216 return;
219 now = timeval_current();
221 DEBUG(10,("dump_event_list:\n"));
223 for (te = ev->timer_events; te; te = te->next) {
225 evt = timeval_until(&now, &te->next_event);
227 DEBUGADD(10,("Timed Event \"%s\" %p handled in %d seconds (at %s)\n",
228 te->handler_name,
230 (int)evt.tv_sec,
231 http_timestring(talloc_tos(), te->next_event.tv_sec)));
234 for (fe = ev->fd_events; fe; fe = fe->next) {
236 DEBUGADD(10,("FD Event %d %p, flags: 0x%04x\n",
237 fe->fd,
239 fe->flags));
243 static const struct tevent_ops s3_event_ops = {
244 .context_init = s3_event_context_init,
245 .add_fd = tevent_common_add_fd,
246 .set_fd_close_fn = tevent_common_fd_set_close_fn,
247 .get_fd_flags = tevent_common_fd_get_flags,
248 .set_fd_flags = tevent_common_fd_set_flags,
249 .add_timer = tevent_common_add_timer,
250 .schedule_immediate = tevent_common_schedule_immediate,
251 .add_signal = tevent_common_add_signal,
252 .loop_once = s3_event_loop_once,
253 .loop_wait = tevent_common_loop_wait,
256 static bool s3_tevent_init(void)
258 static bool initialized;
259 if (initialized) {
260 return true;
262 initialized = tevent_register_backend("s3", &s3_event_ops);
263 tevent_set_default_backend("s3");
264 return initialized;
268 this is used to catch debug messages from events
270 static void s3_event_debug(void *context, enum tevent_debug_level level,
271 const char *fmt, va_list ap) PRINTF_ATTRIBUTE(3,0);
273 static void s3_event_debug(void *context, enum tevent_debug_level level,
274 const char *fmt, va_list ap)
276 int samba_level = -1;
277 char *s = NULL;
278 switch (level) {
279 case TEVENT_DEBUG_FATAL:
280 samba_level = 0;
281 break;
282 case TEVENT_DEBUG_ERROR:
283 samba_level = 1;
284 break;
285 case TEVENT_DEBUG_WARNING:
286 samba_level = 2;
287 break;
288 case TEVENT_DEBUG_TRACE:
289 samba_level = 11;
290 break;
293 if (vasprintf(&s, fmt, ap) == -1) {
294 return;
296 DEBUG(samba_level, ("s3_event: %s", s));
297 free(s);
300 struct tevent_context *s3_tevent_context_init(TALLOC_CTX *mem_ctx)
302 struct tevent_context *ev;
304 s3_tevent_init();
306 ev = tevent_context_init_byname(mem_ctx, "s3");
307 if (ev) {
308 tevent_set_debug(ev, s3_event_debug, NULL);
311 return ev;