Merge branch 'tdr' of /home/jelmer/samba4
[sfrench/samba-autobuild/.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         /* this is incremented when the loop over events causes something which
43            could change the events yet to be processed */
44         uint32_t destruction_count;
45 };
46
47 /*
48   create a select_event_context structure.
49 */
50 static int select_event_context_init(struct tevent_context *ev)
51 {
52         struct select_event_context *select_ev;
53
54         select_ev = talloc_zero(ev, struct select_event_context);
55         if (!select_ev) return -1;
56         select_ev->ev = ev;
57
58         ev->additional_data = select_ev;
59         return 0;
60 }
61
62 /*
63   recalculate the maxfd
64 */
65 static void calc_maxfd(struct select_event_context *select_ev)
66 {
67         struct tevent_fd *fde;
68
69         select_ev->maxfd = 0;
70         for (fde = select_ev->ev->fd_events; fde; fde = fde->next) {
71                 if (fde->fd > select_ev->maxfd) {
72                         select_ev->maxfd = fde->fd;
73                 }
74         }
75 }
76
77
78 /* to mark the ev->maxfd invalid
79  * this means we need to recalculate it
80  */
81 #define EVENT_INVALID_MAXFD (-1)
82
83 /*
84   destroy an fd_event
85 */
86 static int select_event_fd_destructor(struct tevent_fd *fde)
87 {
88         struct tevent_context *ev = fde->event_ctx;
89         struct select_event_context *select_ev = NULL;
90
91         if (ev) {
92                 select_ev = talloc_get_type(ev->additional_data,
93                                             struct select_event_context);
94
95                 if (select_ev->maxfd == fde->fd) {
96                         select_ev->maxfd = EVENT_INVALID_MAXFD;
97                 }
98
99                 select_ev->destruction_count++;
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         fde = tevent_common_add_fd(ev, mem_ctx, fd, flags,
121                                    handler, private_data,
122                                    handler_name, location);
123         if (!fde) return NULL;
124
125         if (fde->fd > select_ev->maxfd) {
126                 select_ev->maxfd = fde->fd;
127         }
128         talloc_set_destructor(fde, select_event_fd_destructor);
129
130         return fde;
131 }
132
133 /*
134   event loop handling using select()
135 */
136 static int select_event_loop_select(struct select_event_context *select_ev, struct timeval *tvalp)
137 {
138         fd_set r_fds, w_fds;
139         struct tevent_fd *fde;
140         int selrtn;
141         uint32_t destruction_count = ++select_ev->destruction_count;
142
143         /* we maybe need to recalculate the maxfd */
144         if (select_ev->maxfd == EVENT_INVALID_MAXFD) {
145                 calc_maxfd(select_ev);
146         }
147
148         FD_ZERO(&r_fds);
149         FD_ZERO(&w_fds);
150
151         /* setup any fd events */
152         for (fde = select_ev->ev->fd_events; fde; fde = fde->next) {
153                 if (fde->flags & TEVENT_FD_READ) {
154                         FD_SET(fde->fd, &r_fds);
155                 }
156                 if (fde->flags & TEVENT_FD_WRITE) {
157                         FD_SET(fde->fd, &w_fds);
158                 }
159         }
160
161         if (select_ev->ev->signal_events &&
162             tevent_common_check_signal(select_ev->ev)) {
163                 return 0;
164         }
165
166         selrtn = select(select_ev->maxfd+1, &r_fds, &w_fds, NULL, tvalp);
167
168         if (selrtn == -1 && errno == EINTR && 
169             select_ev->ev->signal_events) {
170                 tevent_common_check_signal(select_ev->ev);
171                 return 0;
172         }
173
174         if (selrtn == -1 && errno == EBADF) {
175                 /* the socket is dead! this should never
176                    happen as the socket should have first been
177                    made readable and that should have removed
178                    the event, so this must be a bug. This is a
179                    fatal error. */
180                 tevent_debug(select_ev->ev, TEVENT_DEBUG_FATAL,
181                              "ERROR: EBADF on select_event_loop_once\n");
182                 select_ev->exit_code = EBADF;
183                 return -1;
184         }
185
186         if (selrtn == 0 && tvalp) {
187                 /* we don't care about a possible delay here */
188                 tevent_common_loop_timer_delay(select_ev->ev);
189                 return 0;
190         }
191
192         if (selrtn > 0) {
193                 /* at least one file descriptor is ready - check
194                    which ones and call the handler, being careful to allow
195                    the handler to remove itself when called */
196                 for (fde = select_ev->ev->fd_events; fde; fde = fde->next) {
197                         uint16_t flags = 0;
198
199                         if (FD_ISSET(fde->fd, &r_fds)) flags |= TEVENT_FD_READ;
200                         if (FD_ISSET(fde->fd, &w_fds)) flags |= TEVENT_FD_WRITE;
201                         if (flags) {
202                                 fde->handler(select_ev->ev, fde, flags, fde->private_data);
203                                 if (destruction_count != select_ev->destruction_count) {
204                                         break;
205                                 }
206                         }
207                 }
208         }
209
210         return 0;
211 }               
212
213 /*
214   do a single event loop using the events defined in ev 
215 */
216 static int select_event_loop_once(struct tevent_context *ev, const char *location)
217 {
218         struct select_event_context *select_ev = talloc_get_type(ev->additional_data,
219                                                            struct select_event_context);
220         struct timeval tval;
221
222         tval = tevent_common_loop_timer_delay(ev);
223         if (tevent_timeval_is_zero(&tval)) {
224                 return 0;
225         }
226
227         return select_event_loop_select(select_ev, &tval);
228 }
229
230 /*
231   return on failure or (with 0) if all fd events are removed
232 */
233 static int select_event_loop_wait(struct tevent_context *ev, const char *location)
234 {
235         struct select_event_context *select_ev = talloc_get_type(ev->additional_data,
236                                                            struct select_event_context);
237         select_ev->exit_code = 0;
238
239         while (ev->fd_events && select_ev->exit_code == 0) {
240                 if (select_event_loop_once(ev, location) != 0) {
241                         break;
242                 }
243         }
244
245         return select_ev->exit_code;
246 }
247
248 static const struct tevent_ops select_event_ops = {
249         .context_init   = select_event_context_init,
250         .add_fd         = select_event_add_fd,
251         .set_fd_close_fn= tevent_common_fd_set_close_fn,
252         .get_fd_flags   = tevent_common_fd_get_flags,
253         .set_fd_flags   = tevent_common_fd_set_flags,
254         .add_timer      = tevent_common_add_timer,
255         .add_signal     = tevent_common_add_signal,
256         .loop_once      = select_event_loop_once,
257         .loop_wait      = select_event_loop_wait,
258 };
259
260 bool tevent_select_init(void)
261 {
262         return tevent_register_backend("select", &select_event_ops);
263 }