s4-smbtorture: fix the build of the ndr spoolss tester.
[jra/samba/.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
6    
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.
11    
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.
16    
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/>.
19 */
20
21 #include "replace.h"
22 #include "system/filesys.h"
23 #include "system/select.h"
24 #include "tevent.h"
25 #include "tevent_util.h"
26 #include "tevent_internal.h"
27
28 struct select_event_context {
29         /* a pointer back to the generic event_context */
30         struct tevent_context *ev;
31
32         /* the maximum file descriptor number in fd_events */
33         int maxfd;
34
35         /* information for exiting from the event loop */
36         int exit_code;
37
38         /* this is incremented when the loop over events causes something which
39            could change the events yet to be processed */
40         uint32_t destruction_count;
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         select_ev = talloc_zero(ev, struct select_event_context);
51         if (!select_ev) return -1;
52         select_ev->ev = ev;
53
54         ev->additional_data = select_ev;
55         return 0;
56 }
57
58 /*
59   recalculate the maxfd
60 */
61 static void calc_maxfd(struct select_event_context *select_ev)
62 {
63         struct tevent_fd *fde;
64
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;
69                 }
70         }
71 }
72
73
74 /* to mark the ev->maxfd invalid
75  * this means we need to recalculate it
76  */
77 #define EVENT_INVALID_MAXFD (-1)
78
79 /*
80   destroy an fd_event
81 */
82 static int select_event_fd_destructor(struct tevent_fd *fde)
83 {
84         struct tevent_context *ev = fde->event_ctx;
85         struct select_event_context *select_ev = NULL;
86
87         if (ev) {
88                 select_ev = talloc_get_type(ev->additional_data,
89                                             struct select_event_context);
90
91                 if (select_ev->maxfd == fde->fd) {
92                         select_ev->maxfd = EVENT_INVALID_MAXFD;
93                 }
94
95                 select_ev->destruction_count++;
96         }
97
98         return tevent_common_fd_destructor(fde);
99 }
100
101 /*
102   add a fd based event
103   return NULL on failure (memory allocation error)
104 */
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,
108                                              void *private_data,
109                                              const char *handler_name,
110                                              const char *location)
111 {
112         struct select_event_context *select_ev = talloc_get_type(ev->additional_data,
113                                                            struct select_event_context);
114         struct tevent_fd *fde;
115
116         fde = tevent_common_add_fd(ev, mem_ctx, fd, flags,
117                                    handler, private_data,
118                                    handler_name, location);
119         if (!fde) return NULL;
120
121         if (fde->fd > select_ev->maxfd) {
122                 select_ev->maxfd = fde->fd;
123         }
124         talloc_set_destructor(fde, select_event_fd_destructor);
125
126         return fde;
127 }
128
129 /*
130   event loop handling using select()
131 */
132 static int select_event_loop_select(struct select_event_context *select_ev, struct timeval *tvalp)
133 {
134         fd_set r_fds, w_fds;
135         struct tevent_fd *fde;
136         int selrtn;
137         uint32_t destruction_count = ++select_ev->destruction_count;
138
139         /* we maybe need to recalculate the maxfd */
140         if (select_ev->maxfd == EVENT_INVALID_MAXFD) {
141                 calc_maxfd(select_ev);
142         }
143
144         FD_ZERO(&r_fds);
145         FD_ZERO(&w_fds);
146
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);
151                 }
152                 if (fde->flags & TEVENT_FD_WRITE) {
153                         FD_SET(fde->fd, &w_fds);
154                 }
155         }
156
157         if (select_ev->ev->signal_events &&
158             tevent_common_check_signal(select_ev->ev)) {
159                 return 0;
160         }
161
162         selrtn = select(select_ev->maxfd+1, &r_fds, &w_fds, NULL, tvalp);
163
164         if (selrtn == -1 && errno == EINTR && 
165             select_ev->ev->signal_events) {
166                 tevent_common_check_signal(select_ev->ev);
167                 return 0;
168         }
169
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
175                    fatal error. */
176                 tevent_debug(select_ev->ev, TEVENT_DEBUG_FATAL,
177                              "ERROR: EBADF on select_event_loop_once\n");
178                 select_ev->exit_code = EBADF;
179                 return -1;
180         }
181
182         if (selrtn == 0 && tvalp) {
183                 /* we don't care about a possible delay here */
184                 tevent_common_loop_timer_delay(select_ev->ev);
185                 return 0;
186         }
187
188         if (selrtn > 0) {
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) {
193                         uint16_t flags = 0;
194
195                         if (FD_ISSET(fde->fd, &r_fds)) flags |= TEVENT_FD_READ;
196                         if (FD_ISSET(fde->fd, &w_fds)) flags |= TEVENT_FD_WRITE;
197                         if (flags) {
198                                 fde->handler(select_ev->ev, fde, flags, fde->private_data);
199                                 if (destruction_count != select_ev->destruction_count) {
200                                         break;
201                                 }
202                         }
203                 }
204         }
205
206         return 0;
207 }               
208
209 /*
210   do a single event loop using the events defined in ev 
211 */
212 static int select_event_loop_once(struct tevent_context *ev)
213 {
214         struct select_event_context *select_ev = talloc_get_type(ev->additional_data,
215                                                            struct select_event_context);
216         struct timeval tval;
217
218         tval = tevent_common_loop_timer_delay(ev);
219         if (ev_timeval_is_zero(&tval)) {
220                 return 0;
221         }
222
223         return select_event_loop_select(select_ev, &tval);
224 }
225
226 /*
227   return on failure or (with 0) if all fd events are removed
228 */
229 static int select_event_loop_wait(struct tevent_context *ev)
230 {
231         struct select_event_context *select_ev = talloc_get_type(ev->additional_data,
232                                                            struct select_event_context);
233         select_ev->exit_code = 0;
234
235         while (ev->fd_events && select_ev->exit_code == 0) {
236                 if (select_event_loop_once(ev) != 0) {
237                         break;
238                 }
239         }
240
241         return select_ev->exit_code;
242 }
243
244 static const struct tevent_ops select_event_ops = {
245         .context_init   = select_event_context_init,
246         .add_fd         = select_event_add_fd,
247         .set_fd_close_fn= tevent_common_fd_set_close_fn,
248         .get_fd_flags   = tevent_common_fd_get_flags,
249         .set_fd_flags   = tevent_common_fd_set_flags,
250         .add_timer      = tevent_common_add_timer,
251         .add_signal     = tevent_common_add_signal,
252         .loop_once      = select_event_loop_once,
253         .loop_wait      = select_event_loop_wait,
254 };
255
256 bool tevent_select_init(void)
257 {
258         return tevent_register_backend("select", &select_event_ops);
259 }