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