Fix bug #8341 - libsmbclient segfault when feed the root of a mounted share via an uri
[Samba.git] / lib / tevent / tevent_select.c
blob94faa8659340a7d5ab1bb4d0833520a8b56ebe09
1 /*
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
9 ** under the LGPL
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 "replace.h"
26 #include "system/filesys.h"
27 #include "system/select.h"
28 #include "tevent.h"
29 #include "tevent_util.h"
30 #include "tevent_internal.h"
32 struct select_event_context {
33 /* a pointer back to the generic event_context */
34 struct tevent_context *ev;
36 /* the maximum file descriptor number in fd_events */
37 int maxfd;
39 /* information for exiting from the event loop */
40 int exit_code;
44 create a select_event_context structure.
46 static int select_event_context_init(struct tevent_context *ev)
48 struct select_event_context *select_ev;
50 select_ev = talloc_zero(ev, struct select_event_context);
51 if (!select_ev) return -1;
52 select_ev->ev = ev;
54 ev->additional_data = select_ev;
55 return 0;
59 recalculate the maxfd
61 static void calc_maxfd(struct select_event_context *select_ev)
63 struct tevent_fd *fde;
65 select_ev->maxfd = 0;
66 for (fde = select_ev->ev->fd_events; fde; fde = fde->next) {
67 if (fde->fd > select_ev->maxfd) {
68 select_ev->maxfd = fde->fd;
74 /* to mark the ev->maxfd invalid
75 * this means we need to recalculate it
77 #define EVENT_INVALID_MAXFD (-1)
80 destroy an fd_event
82 static int select_event_fd_destructor(struct tevent_fd *fde)
84 struct tevent_context *ev = fde->event_ctx;
85 struct select_event_context *select_ev = NULL;
87 if (ev) {
88 select_ev = talloc_get_type(ev->additional_data,
89 struct select_event_context);
91 if (select_ev->maxfd == fde->fd) {
92 select_ev->maxfd = EVENT_INVALID_MAXFD;
96 return tevent_common_fd_destructor(fde);
100 add a fd based event
101 return NULL on failure (memory allocation error)
103 static struct tevent_fd *select_event_add_fd(struct tevent_context *ev, TALLOC_CTX *mem_ctx,
104 int fd, uint16_t flags,
105 tevent_fd_handler_t handler,
106 void *private_data,
107 const char *handler_name,
108 const char *location)
110 struct select_event_context *select_ev = talloc_get_type(ev->additional_data,
111 struct select_event_context);
112 struct tevent_fd *fde;
114 if (fd < 0 || fd >= FD_SETSIZE) {
115 errno = EBADF;
116 return NULL;
119 fde = tevent_common_add_fd(ev, mem_ctx, fd, flags,
120 handler, private_data,
121 handler_name, location);
122 if (!fde) return NULL;
124 if ((select_ev->maxfd != EVENT_INVALID_MAXFD)
125 && (fde->fd > select_ev->maxfd)) {
126 select_ev->maxfd = fde->fd;
128 talloc_set_destructor(fde, select_event_fd_destructor);
130 return fde;
134 event loop handling using select()
136 static int select_event_loop_select(struct select_event_context *select_ev, struct timeval *tvalp)
138 fd_set r_fds, w_fds;
139 struct tevent_fd *fde;
140 int selrtn;
142 /* we maybe need to recalculate the maxfd */
143 if (select_ev->maxfd == EVENT_INVALID_MAXFD) {
144 calc_maxfd(select_ev);
147 FD_ZERO(&r_fds);
148 FD_ZERO(&w_fds);
150 /* setup any fd events */
151 for (fde = select_ev->ev->fd_events; fde; fde = fde->next) {
152 if (fde->fd < 0 || fde->fd >= FD_SETSIZE) {
153 errno = EBADF;
154 return -1;
157 if (fde->flags & TEVENT_FD_READ) {
158 FD_SET(fde->fd, &r_fds);
160 if (fde->flags & TEVENT_FD_WRITE) {
161 FD_SET(fde->fd, &w_fds);
165 if (select_ev->ev->signal_events &&
166 tevent_common_check_signal(select_ev->ev)) {
167 return 0;
170 selrtn = select(select_ev->maxfd+1, &r_fds, &w_fds, NULL, tvalp);
172 if (selrtn == -1 && errno == EINTR &&
173 select_ev->ev->signal_events) {
174 tevent_common_check_signal(select_ev->ev);
175 return 0;
178 if (selrtn == -1 && errno == EBADF) {
179 /* the socket is dead! this should never
180 happen as the socket should have first been
181 made readable and that should have removed
182 the event, so this must be a bug. This is a
183 fatal error. */
184 tevent_debug(select_ev->ev, TEVENT_DEBUG_FATAL,
185 "ERROR: EBADF on select_event_loop_once\n");
186 select_ev->exit_code = EBADF;
187 return -1;
190 if (selrtn == 0 && tvalp) {
191 /* we don't care about a possible delay here */
192 tevent_common_loop_timer_delay(select_ev->ev);
193 return 0;
196 if (selrtn > 0) {
197 /* at least one file descriptor is ready - check
198 which ones and call the handler, being careful to allow
199 the handler to remove itself when called */
200 for (fde = select_ev->ev->fd_events; fde; fde = fde->next) {
201 uint16_t flags = 0;
203 if (FD_ISSET(fde->fd, &r_fds)) flags |= TEVENT_FD_READ;
204 if (FD_ISSET(fde->fd, &w_fds)) flags |= TEVENT_FD_WRITE;
205 if (flags) {
206 fde->handler(select_ev->ev, fde, flags, fde->private_data);
207 break;
212 return 0;
216 do a single event loop using the events defined in ev
218 static int select_event_loop_once(struct tevent_context *ev, const char *location)
220 struct select_event_context *select_ev = talloc_get_type(ev->additional_data,
221 struct select_event_context);
222 struct timeval tval;
224 if (ev->signal_events &&
225 tevent_common_check_signal(ev)) {
226 return 0;
229 if (ev->immediate_events &&
230 tevent_common_loop_immediate(ev)) {
231 return 0;
234 tval = tevent_common_loop_timer_delay(ev);
235 if (tevent_timeval_is_zero(&tval)) {
236 return 0;
239 return select_event_loop_select(select_ev, &tval);
242 static const struct tevent_ops select_event_ops = {
243 .context_init = select_event_context_init,
244 .add_fd = select_event_add_fd,
245 .set_fd_close_fn = tevent_common_fd_set_close_fn,
246 .get_fd_flags = tevent_common_fd_get_flags,
247 .set_fd_flags = tevent_common_fd_set_flags,
248 .add_timer = tevent_common_add_timer,
249 .schedule_immediate = tevent_common_schedule_immediate,
250 .add_signal = tevent_common_add_signal,
251 .loop_once = select_event_loop_once,
252 .loop_wait = tevent_common_loop_wait,
255 _PRIVATE_ bool tevent_select_init(void)
257 return tevent_register_backend("select", &select_event_ops);