2 Unix SMB/CIFS implementation.
3 main select loop and event handling
4 Copyright (C) Andrew Tridgell 2003-2005
5 Copyright (C) Stefan Metzmacher 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/>.
22 This is SAMBA's default event loop code
27 #include "system/filesys.h"
28 #include "system/select.h"
30 #include "tevent_util.h"
31 #include "tevent_internal.h"
33 struct select_event_context {
34 /* a pointer back to the generic event_context */
35 struct tevent_context *ev;
37 /* list of filedescriptor events */
38 struct tevent_fd *fd_events;
40 /* list of timed events */
41 struct tevent_timer *timed_events;
43 /* the maximum file descriptor number in fd_events */
46 /* information for exiting from the event loop */
49 /* this is incremented when the loop over events causes something which
50 could change the events yet to be processed */
51 uint32_t destruction_count;
55 create a select_event_context structure.
57 static int select_event_context_init(struct tevent_context *ev)
59 struct select_event_context *select_ev;
61 select_ev = talloc_zero(ev, struct select_event_context);
62 if (!select_ev) return -1;
65 ev->additional_data = select_ev;
72 static void calc_maxfd(struct select_event_context *select_ev)
74 struct tevent_fd *fde;
77 for (fde = select_ev->fd_events; fde; fde = fde->next) {
78 if (fde->fd > select_ev->maxfd) {
79 select_ev->maxfd = fde->fd;
85 /* to mark the ev->maxfd invalid
86 * this means we need to recalculate it
88 #define EVENT_INVALID_MAXFD (-1)
93 static int select_event_fd_destructor(struct tevent_fd *fde)
95 struct tevent_context *ev = fde->event_ctx;
96 struct select_event_context *select_ev = talloc_get_type(ev->additional_data,
97 struct select_event_context);
99 if (select_ev->maxfd == fde->fd) {
100 select_ev->maxfd = EVENT_INVALID_MAXFD;
103 DLIST_REMOVE(select_ev->fd_events, fde);
104 select_ev->destruction_count++;
107 fde->close_fn(ev, fde, fde->fd, fde->private_data);
109 } else if (fde->flags & TEVENT_FD_AUTOCLOSE) {
119 return NULL on failure (memory allocation error)
121 static struct tevent_fd *select_event_add_fd(struct tevent_context *ev, TALLOC_CTX *mem_ctx,
122 int fd, uint16_t flags,
123 tevent_fd_handler_t handler,
125 const char *handler_name,
126 const char *location)
128 struct select_event_context *select_ev = talloc_get_type(ev->additional_data,
129 struct select_event_context);
130 struct tevent_fd *fde;
132 fde = talloc(mem_ctx?mem_ctx:ev, struct tevent_fd);
133 if (!fde) return NULL;
138 fde->handler = handler;
139 fde->private_data = private_data;
140 fde->handler_name = handler_name;
141 fde->location = location;
142 fde->additional_flags = 0;
143 fde->additional_data = NULL;
145 DLIST_ADD(select_ev->fd_events, fde);
146 if (fde->fd > select_ev->maxfd) {
147 select_ev->maxfd = fde->fd;
149 talloc_set_destructor(fde, select_event_fd_destructor);
155 event loop handling using select()
157 static int select_event_loop_select(struct select_event_context *select_ev, struct timeval *tvalp)
160 struct tevent_fd *fde;
162 uint32_t destruction_count = ++select_ev->destruction_count;
164 /* we maybe need to recalculate the maxfd */
165 if (select_ev->maxfd == EVENT_INVALID_MAXFD) {
166 calc_maxfd(select_ev);
172 /* setup any fd events */
173 for (fde = select_ev->fd_events; fde; fde = fde->next) {
174 if (fde->flags & TEVENT_FD_READ) {
175 FD_SET(fde->fd, &r_fds);
177 if (fde->flags & TEVENT_FD_WRITE) {
178 FD_SET(fde->fd, &w_fds);
182 if (select_ev->ev->num_signal_handlers &&
183 tevent_common_check_signal(select_ev->ev)) {
187 selrtn = select(select_ev->maxfd+1, &r_fds, &w_fds, NULL, tvalp);
189 if (selrtn == -1 && errno == EINTR &&
190 select_ev->ev->num_signal_handlers) {
191 tevent_common_check_signal(select_ev->ev);
195 if (selrtn == -1 && errno == EBADF) {
196 /* the socket is dead! this should never
197 happen as the socket should have first been
198 made readable and that should have removed
199 the event, so this must be a bug. This is a
201 tevent_debug(select_ev->ev, TEVENT_DEBUG_FATAL,
202 "ERROR: EBADF on select_event_loop_once\n");
203 select_ev->exit_code = EBADF;
207 if (selrtn == 0 && tvalp) {
208 /* we don't care about a possible delay here */
209 tevent_common_loop_timer_delay(select_ev->ev);
214 /* at least one file descriptor is ready - check
215 which ones and call the handler, being careful to allow
216 the handler to remove itself when called */
217 for (fde = select_ev->fd_events; fde; fde = fde->next) {
220 if (FD_ISSET(fde->fd, &r_fds)) flags |= TEVENT_FD_READ;
221 if (FD_ISSET(fde->fd, &w_fds)) flags |= TEVENT_FD_WRITE;
223 fde->handler(select_ev->ev, fde, flags, fde->private_data);
224 if (destruction_count != select_ev->destruction_count) {
235 do a single event loop using the events defined in ev
237 static int select_event_loop_once(struct tevent_context *ev)
239 struct select_event_context *select_ev = talloc_get_type(ev->additional_data,
240 struct select_event_context);
243 tval = tevent_common_loop_timer_delay(ev);
244 if (ev_timeval_is_zero(&tval)) {
248 return select_event_loop_select(select_ev, &tval);
252 return on failure or (with 0) if all fd events are removed
254 static int select_event_loop_wait(struct tevent_context *ev)
256 struct select_event_context *select_ev = talloc_get_type(ev->additional_data,
257 struct select_event_context);
258 select_ev->exit_code = 0;
260 while (select_ev->fd_events && select_ev->exit_code == 0) {
261 if (select_event_loop_once(ev) != 0) {
266 return select_ev->exit_code;
269 static const struct tevent_ops select_event_ops = {
270 .context_init = select_event_context_init,
271 .add_fd = select_event_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 .add_signal = tevent_common_add_signal,
277 .loop_once = select_event_loop_once,
278 .loop_wait = select_event_loop_wait,
281 bool tevent_select_init(void)
283 return tevent_register_backend("select", &select_event_ops);