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-2009
7 ** NOTE! The following LGPL license applies to the tevent
8 ** library. This does NOT imply that all of Samba is released
11 This library is free software; you can redistribute it and/or
12 modify it under the terms of the GNU Lesser General Public
13 License as published by the Free Software Foundation; either
14 version 3 of the License, or (at your option) any later version.
16 This library is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 Lesser General Public License for more details.
21 You should have received a copy of the GNU Lesser General Public
22 License along with this library; if not, see <http://www.gnu.org/licenses/>.
25 #include <ccan/tevent/tevent.h>
26 #include <ccan/tevent/tevent_internal.h>
27 #include <ccan/tevent/tevent_util.h>
28 #include <sys/select.h>
30 #include <sys/types.h>
34 struct select_event_context {
35 /* a pointer back to the generic event_context */
36 struct tevent_context *ev;
38 /* the maximum file descriptor number in fd_events */
41 /* information for exiting from the event loop */
46 create a select_event_context structure.
48 static int select_event_context_init(struct tevent_context *ev)
50 struct select_event_context *select_ev;
52 select_ev = talloc_zero(ev, struct select_event_context);
53 if (!select_ev) return -1;
56 ev->additional_data = select_ev;
63 static void calc_maxfd(struct select_event_context *select_ev)
65 struct tevent_fd *fde;
68 for (fde = select_ev->ev->fd_events; fde; fde = fde->next) {
69 if (fde->fd > select_ev->maxfd) {
70 select_ev->maxfd = fde->fd;
76 /* to mark the ev->maxfd invalid
77 * this means we need to recalculate it
79 #define EVENT_INVALID_MAXFD (-1)
84 static int select_event_fd_destructor(struct tevent_fd *fde)
86 struct tevent_context *ev = fde->event_ctx;
87 struct select_event_context *select_ev = NULL;
90 select_ev = talloc_get_type(ev->additional_data,
91 struct select_event_context);
93 if (select_ev->maxfd == fde->fd) {
94 select_ev->maxfd = EVENT_INVALID_MAXFD;
98 return tevent_common_fd_destructor(fde);
103 return NULL on failure (memory allocation error)
105 static struct tevent_fd *select_event_add_fd(struct tevent_context *ev, TALLOC_CTX *mem_ctx,
106 int fd, uint16_t flags,
107 tevent_fd_handler_t handler,
109 const char *handler_name,
110 const char *location)
112 struct select_event_context *select_ev = talloc_get_type(ev->additional_data,
113 struct select_event_context);
114 struct tevent_fd *fde;
116 fde = tevent_common_add_fd(ev, mem_ctx, fd, flags,
117 handler, private_data,
118 handler_name, location);
119 if (!fde) return NULL;
121 if ((select_ev->maxfd != EVENT_INVALID_MAXFD)
122 && (fde->fd > select_ev->maxfd)) {
123 select_ev->maxfd = fde->fd;
125 talloc_set_destructor(fde, select_event_fd_destructor);
131 event loop handling using select()
133 static int select_event_loop_select(struct select_event_context *select_ev, struct timeval *tvalp)
136 struct tevent_fd *fde;
139 /* we maybe need to recalculate the maxfd */
140 if (select_ev->maxfd == EVENT_INVALID_MAXFD) {
141 calc_maxfd(select_ev);
147 /* setup any fd events */
148 for (fde = select_ev->ev->fd_events; fde; fde = fde->next) {
149 if (fde->flags & TEVENT_FD_READ) {
150 FD_SET(fde->fd, &r_fds);
152 if (fde->flags & TEVENT_FD_WRITE) {
153 FD_SET(fde->fd, &w_fds);
157 if (select_ev->ev->signal_events &&
158 tevent_common_check_signal(select_ev->ev)) {
162 selrtn = select(select_ev->maxfd+1, &r_fds, &w_fds, NULL, tvalp);
164 if (selrtn == -1 && errno == EINTR &&
165 select_ev->ev->signal_events) {
166 tevent_common_check_signal(select_ev->ev);
170 if (selrtn == -1 && errno == EBADF) {
171 /* the socket is dead! this should never
172 happen as the socket should have first been
173 made readable and that should have removed
174 the event, so this must be a bug. This is a
176 tevent_debug(select_ev->ev, TEVENT_DEBUG_FATAL,
177 "ERROR: EBADF on select_event_loop_once\n");
178 select_ev->exit_code = EBADF;
182 if (selrtn == 0 && tvalp) {
183 /* we don't care about a possible delay here */
184 tevent_common_loop_timer_delay(select_ev->ev);
189 /* at least one file descriptor is ready - check
190 which ones and call the handler, being careful to allow
191 the handler to remove itself when called */
192 for (fde = select_ev->ev->fd_events; fde; fde = fde->next) {
195 if (FD_ISSET(fde->fd, &r_fds)) flags |= TEVENT_FD_READ;
196 if (FD_ISSET(fde->fd, &w_fds)) flags |= TEVENT_FD_WRITE;
198 fde->handler(select_ev->ev, fde, flags, fde->private_data);
208 do a single event loop using the events defined in ev
210 static int select_event_loop_once(struct tevent_context *ev, const char *location)
212 struct select_event_context *select_ev = talloc_get_type(ev->additional_data,
213 struct select_event_context);
216 if (ev->signal_events &&
217 tevent_common_check_signal(ev)) {
221 if (ev->immediate_events &&
222 tevent_common_loop_immediate(ev)) {
226 tval = tevent_common_loop_timer_delay(ev);
227 if (tevent_timeval_is_zero(&tval)) {
231 return select_event_loop_select(select_ev, &tval);
234 static const struct tevent_ops select_event_ops = {
235 .context_init = select_event_context_init,
236 .add_fd = select_event_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 = select_event_loop_once,
244 .loop_wait = tevent_common_loop_wait,
247 _PRIVATE_ bool tevent_select_init(void)
249 return tevent_register_backend("select", &select_event_ops);