s3:lib/events: don't loop over fd events is select gave -1
[Samba.git] / source3 / lib / events.c
blob2ac4cc00ed48118fe6abac2b5b781d1a250f39cb
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 fd_set *read_fds, fd_set *write_fds,
31 struct timeval *timeout, int *maxfd)
33 struct timeval now;
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 now = timeval_current();
64 diff = timeval_until(&now, &ev->timer_events->next_event);
65 *timeout = timeval_min(timeout, &diff);
67 return true;
70 bool run_events(struct tevent_context *ev,
71 int selrtn, fd_set *read_fds, fd_set *write_fds)
73 struct tevent_fd *fde;
74 struct timeval now;
76 if (ev->signal_events &&
77 tevent_common_check_signal(ev)) {
78 return true;
81 if (ev->immediate_events &&
82 tevent_common_loop_immediate(ev)) {
83 return true;
86 GetTimeOfDay(&now);
88 if ((ev->timer_events != NULL)
89 && (timeval_compare(&now, &ev->timer_events->next_event) >= 0)) {
90 /* this older events system did not auto-free timed
91 events on running them, and had a race condition
92 where the event could be called twice if the
93 talloc_free of the te happened after the callback
94 made a call which invoked the event loop. To avoid
95 this while still allowing old code which frees the
96 te, we need to create a temporary context which
97 will be used to ensure the te is freed. We also
98 remove the te from the timed event list before we
99 call the handler, to ensure we can't loop */
101 struct tevent_timer *te = ev->timer_events;
102 TALLOC_CTX *tmp_ctx = talloc_new(ev);
104 DEBUG(10, ("Running timed event \"%s\" %p\n",
105 ev->timer_events->handler_name, ev->timer_events));
107 DLIST_REMOVE(ev->timer_events, te);
108 talloc_steal(tmp_ctx, te);
110 te->handler(ev, te, now, te->private_data);
112 talloc_free(tmp_ctx);
113 return true;
116 if (selrtn <= 0) {
118 * No fd ready
120 return false;
123 for (fde = ev->fd_events; fde; fde = fde->next) {
124 uint16 flags = 0;
126 if (FD_ISSET(fde->fd, read_fds)) flags |= EVENT_FD_READ;
127 if (FD_ISSET(fde->fd, write_fds)) flags |= EVENT_FD_WRITE;
129 if (flags & fde->flags) {
130 fde->handler(ev, fde, flags, fde->private_data);
131 return true;
135 return false;
139 struct timeval *get_timed_events_timeout(struct tevent_context *ev,
140 struct timeval *to_ret)
142 struct timeval now;
144 if ((ev->timer_events == NULL) && (ev->immediate_events == NULL)) {
145 return NULL;
147 if (ev->immediate_events != NULL) {
148 *to_ret = timeval_zero();
149 return to_ret;
152 now = timeval_current();
153 *to_ret = timeval_until(&now, &ev->timer_events->next_event);
155 DEBUG(10, ("timed_events_timeout: %d/%d\n", (int)to_ret->tv_sec,
156 (int)to_ret->tv_usec));
158 return to_ret;
161 static int s3_event_loop_once(struct tevent_context *ev, const char *location)
163 struct timeval to;
164 fd_set r_fds, w_fds;
165 int maxfd = 0;
166 int ret;
168 FD_ZERO(&r_fds);
169 FD_ZERO(&w_fds);
171 to.tv_sec = 9999; /* Max timeout */
172 to.tv_usec = 0;
174 if (run_events(ev, 0, NULL, NULL)) {
175 return 0;
178 if (!event_add_to_select_args(ev, &r_fds, &w_fds, &to, &maxfd)) {
179 return -1;
182 ret = sys_select(maxfd+1, &r_fds, &w_fds, NULL, &to);
184 if (ret == -1 && errno != EINTR) {
185 tevent_debug(ev, TEVENT_DEBUG_FATAL,
186 "sys_select() failed: %d:%s\n",
187 errno, strerror(errno));
188 return -1;
191 run_events(ev, ret, &r_fds, &w_fds);
192 return 0;
195 static int s3_event_context_init(struct tevent_context *ev)
197 return 0;
200 void dump_event_list(struct tevent_context *ev)
202 struct tevent_timer *te;
203 struct tevent_fd *fe;
204 struct timeval evt, now;
206 if (!ev) {
207 return;
210 now = timeval_current();
212 DEBUG(10,("dump_event_list:\n"));
214 for (te = ev->timer_events; te; te = te->next) {
216 evt = timeval_until(&now, &te->next_event);
218 DEBUGADD(10,("Timed Event \"%s\" %p handled in %d seconds (at %s)\n",
219 te->handler_name,
221 (int)evt.tv_sec,
222 http_timestring(talloc_tos(), te->next_event.tv_sec)));
225 for (fe = ev->fd_events; fe; fe = fe->next) {
227 DEBUGADD(10,("FD Event %d %p, flags: 0x%04x\n",
228 fe->fd,
230 fe->flags));
234 static const struct tevent_ops s3_event_ops = {
235 .context_init = s3_event_context_init,
236 .add_fd = tevent_common_add_fd,
237 .set_fd_close_fn = tevent_common_fd_set_close_fn,
238 .get_fd_flags = tevent_common_fd_get_flags,
239 .set_fd_flags = tevent_common_fd_set_flags,
240 .add_timer = tevent_common_add_timer,
241 .schedule_immediate = tevent_common_schedule_immediate,
242 .add_signal = tevent_common_add_signal,
243 .loop_once = s3_event_loop_once,
244 .loop_wait = tevent_common_loop_wait,
247 static bool s3_tevent_init(void)
249 static bool initialized;
250 if (initialized) {
251 return true;
253 initialized = tevent_register_backend("s3", &s3_event_ops);
254 tevent_set_default_backend("s3");
255 return initialized;
259 this is used to catch debug messages from events
261 static void s3_event_debug(void *context, enum tevent_debug_level level,
262 const char *fmt, va_list ap) PRINTF_ATTRIBUTE(3,0);
264 static void s3_event_debug(void *context, enum tevent_debug_level level,
265 const char *fmt, va_list ap)
267 int samba_level = -1;
268 char *s = NULL;
269 switch (level) {
270 case TEVENT_DEBUG_FATAL:
271 samba_level = 0;
272 break;
273 case TEVENT_DEBUG_ERROR:
274 samba_level = 1;
275 break;
276 case TEVENT_DEBUG_WARNING:
277 samba_level = 2;
278 break;
279 case TEVENT_DEBUG_TRACE:
280 samba_level = 11;
281 break;
284 if (vasprintf(&s, fmt, ap) == -1) {
285 return;
287 DEBUG(samba_level, ("s3_event: %s", s));
288 free(s);
291 struct tevent_context *s3_tevent_context_init(TALLOC_CTX *mem_ctx)
293 struct tevent_context *ev;
295 s3_tevent_init();
297 ev = tevent_context_init_byname(mem_ctx, "s3");
298 if (ev) {
299 tevent_set_debug(ev, s3_event_debug, NULL);
302 return ev;