ffb0d18c2e9f21cb737d782eb7b44ba6f8407229
[metze/samba/wip.git] / lib / tevent / tevent_select.c
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
6
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
10
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.
15
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.
20
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/>.
23 */
24
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"
31
32 struct select_event_context {
33         /* a pointer back to the generic event_context */
34         struct tevent_context *ev;
35
36         /* the maximum file descriptor number in fd_events */
37         int maxfd;
38
39         /* information for exiting from the event loop */
40         int exit_code;
41 };
42
43 /*
44   create a select_event_context structure.
45 */
46 static int select_event_context_init(struct tevent_context *ev)
47 {
48         struct select_event_context *select_ev;
49
50         /*
51          * We might be called during tevent_re_initialise()
52          * which means we need to free our old additional_data.
53          */
54         TALLOC_FREE(ev->additional_data);
55
56         select_ev = talloc_zero(ev, struct select_event_context);
57         if (!select_ev) return -1;
58         select_ev->ev = ev;
59
60         ev->additional_data = select_ev;
61         return 0;
62 }
63
64 /*
65   recalculate the maxfd
66 */
67 static void calc_maxfd(struct select_event_context *select_ev)
68 {
69         struct tevent_fd *fde;
70
71         select_ev->maxfd = 0;
72         for (fde = select_ev->ev->fd_events; fde; fde = fde->next) {
73                 if (fde->fd > select_ev->maxfd) {
74                         select_ev->maxfd = fde->fd;
75                 }
76         }
77 }
78
79
80 /* to mark the ev->maxfd invalid
81  * this means we need to recalculate it
82  */
83 #define EVENT_INVALID_MAXFD (-1)
84
85 /*
86   destroy an fd_event
87 */
88 static int select_event_fd_destructor(struct tevent_fd *fde)
89 {
90         struct tevent_context *ev = fde->event_ctx;
91         struct select_event_context *select_ev = NULL;
92
93         if (ev) {
94                 select_ev = talloc_get_type(ev->additional_data,
95                                             struct select_event_context);
96
97                 if (select_ev->maxfd == fde->fd) {
98                         select_ev->maxfd = EVENT_INVALID_MAXFD;
99                 }
100         }
101
102         return tevent_common_fd_destructor(fde);
103 }
104
105 /*
106   add a fd based event
107   return NULL on failure (memory allocation error)
108 */
109 static struct tevent_fd *select_event_add_fd(struct tevent_context *ev, TALLOC_CTX *mem_ctx,
110                                              int fd, uint16_t flags,
111                                              tevent_fd_handler_t handler,
112                                              void *private_data,
113                                              const char *handler_name,
114                                              const char *location)
115 {
116         struct select_event_context *select_ev = talloc_get_type(ev->additional_data,
117                                                            struct select_event_context);
118         struct tevent_fd *fde;
119
120         if (fd < 0 || fd >= FD_SETSIZE) {
121                 errno = EBADF;
122                 return NULL;
123         }
124
125         fde = tevent_common_add_fd(ev, mem_ctx, fd, flags,
126                                    handler, private_data,
127                                    handler_name, location);
128         if (!fde) return NULL;
129
130         if ((select_ev->maxfd != EVENT_INVALID_MAXFD)
131             && (fde->fd > select_ev->maxfd)) {
132                 select_ev->maxfd = fde->fd;
133         }
134         talloc_set_destructor(fde, select_event_fd_destructor);
135
136         return fde;
137 }
138
139 /*
140   event loop handling using select()
141 */
142 static int select_event_loop_select(struct select_event_context *select_ev, struct timeval *tvalp)
143 {
144         fd_set r_fds, w_fds;
145         struct tevent_fd *fde;
146         int selrtn;
147         int select_errno;
148
149         /* we maybe need to recalculate the maxfd */
150         if (select_ev->maxfd == EVENT_INVALID_MAXFD) {
151                 calc_maxfd(select_ev);
152         }
153
154         FD_ZERO(&r_fds);
155         FD_ZERO(&w_fds);
156
157         /* setup any fd events */
158         for (fde = select_ev->ev->fd_events; fde; fde = fde->next) {
159                 if (fde->fd < 0 || fde->fd >= FD_SETSIZE) {
160                         errno = EBADF;
161                         return -1;
162                 }
163
164                 if (fde->flags & TEVENT_FD_READ) {
165                         FD_SET(fde->fd, &r_fds);
166                 }
167                 if (fde->flags & TEVENT_FD_WRITE) {
168                         FD_SET(fde->fd, &w_fds);
169                 }
170         }
171
172         if (select_ev->ev->signal_events &&
173             tevent_common_check_signal(select_ev->ev)) {
174                 return 0;
175         }
176
177         tevent_trace_point_callback(select_ev->ev, TEVENT_TRACE_BEFORE_WAIT);
178         selrtn = select(select_ev->maxfd+1, &r_fds, &w_fds, NULL, tvalp);
179         select_errno = errno;
180         tevent_trace_point_callback(select_ev->ev, TEVENT_TRACE_AFTER_WAIT);
181
182         if (selrtn == -1 && select_errno == EINTR &&
183             select_ev->ev->signal_events) {
184                 tevent_common_check_signal(select_ev->ev);
185                 return 0;
186         }
187
188         if (selrtn == -1 && select_errno == EBADF) {
189                 /* the socket is dead! this should never
190                    happen as the socket should have first been
191                    made readable and that should have removed
192                    the event, so this must be a bug. This is a
193                    fatal error. */
194                 tevent_debug(select_ev->ev, TEVENT_DEBUG_FATAL,
195                              "ERROR: EBADF on select_event_loop_once\n");
196                 select_ev->exit_code = EBADF;
197                 return -1;
198         }
199
200         if (selrtn == 0 && tvalp) {
201                 /* we don't care about a possible delay here */
202                 tevent_common_loop_timer_delay(select_ev->ev);
203                 return 0;
204         }
205
206         if (selrtn > 0) {
207                 /* at least one file descriptor is ready - check
208                    which ones and call the handler, being careful to allow
209                    the handler to remove itself when called */
210                 for (fde = select_ev->ev->fd_events; fde; fde = fde->next) {
211                         uint16_t flags = 0;
212
213                         if (FD_ISSET(fde->fd, &r_fds)) flags |= TEVENT_FD_READ;
214                         if (FD_ISSET(fde->fd, &w_fds)) flags |= TEVENT_FD_WRITE;
215                         if (flags) {
216                                 fde->handler(select_ev->ev, fde, flags, fde->private_data);
217                                 break;
218                         }
219                 }
220         }
221
222         return 0;
223 }
224
225 /*
226   do a single event loop using the events defined in ev 
227 */
228 static int select_event_loop_once(struct tevent_context *ev, const char *location)
229 {
230         struct select_event_context *select_ev = talloc_get_type(ev->additional_data,
231                                                            struct select_event_context);
232         struct timeval tval;
233
234         if (ev->signal_events &&
235             tevent_common_check_signal(ev)) {
236                 return 0;
237         }
238
239         if (ev->immediate_events &&
240             tevent_common_loop_immediate(ev)) {
241                 return 0;
242         }
243
244         tval = tevent_common_loop_timer_delay(ev);
245         if (tevent_timeval_is_zero(&tval)) {
246                 return 0;
247         }
248
249         return select_event_loop_select(select_ev, &tval);
250 }
251
252 static const struct tevent_ops select_event_ops = {
253         .context_init           = select_event_context_init,
254         .add_fd                 = select_event_add_fd,
255         .set_fd_close_fn        = tevent_common_fd_set_close_fn,
256         .get_fd_flags           = tevent_common_fd_get_flags,
257         .set_fd_flags           = tevent_common_fd_set_flags,
258         .add_timer              = tevent_common_add_timer,
259         .schedule_immediate     = tevent_common_schedule_immediate,
260         .add_signal             = tevent_common_add_signal,
261         .loop_once              = select_event_loop_once,
262         .loop_wait              = tevent_common_loop_wait,
263 };
264
265 _PRIVATE_ bool tevent_select_init(void)
266 {
267         return tevent_register_backend("select", &select_event_ops);
268 }