tevent: remove TEVENT_FD_AUTOCLOSE feature
[kamenim/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 /*
22   This is SAMBA's default event loop code
23
24 */
25
26 #include "replace.h"
27 #include "system/filesys.h"
28 #include "system/select.h"
29 #include "tevent.h"
30 #include "tevent_util.h"
31 #include "tevent_internal.h"
32
33 struct select_event_context {
34         /* a pointer back to the generic event_context */
35         struct tevent_context *ev;
36
37         /* list of filedescriptor events */
38         struct tevent_fd *fd_events;
39
40         /* list of timed events */
41         struct tevent_timer *timed_events;
42
43         /* the maximum file descriptor number in fd_events */
44         int maxfd;
45
46         /* information for exiting from the event loop */
47         int exit_code;
48
49         /* this is incremented when the loop over events causes something which
50            could change the events yet to be processed */
51         uint32_t destruction_count;
52 };
53
54 /*
55   create a select_event_context structure.
56 */
57 static int select_event_context_init(struct tevent_context *ev)
58 {
59         struct select_event_context *select_ev;
60
61         select_ev = talloc_zero(ev, struct select_event_context);
62         if (!select_ev) return -1;
63         select_ev->ev = ev;
64
65         ev->additional_data = select_ev;
66         return 0;
67 }
68
69 /*
70   recalculate the maxfd
71 */
72 static void calc_maxfd(struct select_event_context *select_ev)
73 {
74         struct tevent_fd *fde;
75
76         select_ev->maxfd = 0;
77         for (fde = select_ev->fd_events; fde; fde = fde->next) {
78                 if (fde->fd > select_ev->maxfd) {
79                         select_ev->maxfd = fde->fd;
80                 }
81         }
82 }
83
84
85 /* to mark the ev->maxfd invalid
86  * this means we need to recalculate it
87  */
88 #define EVENT_INVALID_MAXFD (-1)
89
90 /*
91   destroy an fd_event
92 */
93 static int select_event_fd_destructor(struct tevent_fd *fde)
94 {
95         struct tevent_context *ev = fde->event_ctx;
96         struct select_event_context *select_ev = talloc_get_type(ev->additional_data,
97                                                            struct select_event_context);
98
99         if (select_ev->maxfd == fde->fd) {
100                 select_ev->maxfd = EVENT_INVALID_MAXFD;
101         }
102
103         DLIST_REMOVE(select_ev->fd_events, fde);
104         select_ev->destruction_count++;
105
106         if (fde->close_fn) {
107                 fde->close_fn(ev, fde, fde->fd, fde->private_data);
108                 fde->fd = -1;
109         }
110
111         return 0;
112 }
113
114 /*
115   add a fd based event
116   return NULL on failure (memory allocation error)
117 */
118 static struct tevent_fd *select_event_add_fd(struct tevent_context *ev, TALLOC_CTX *mem_ctx,
119                                              int fd, uint16_t flags,
120                                              tevent_fd_handler_t handler,
121                                              void *private_data,
122                                              const char *handler_name,
123                                              const char *location)
124 {
125         struct select_event_context *select_ev = talloc_get_type(ev->additional_data,
126                                                            struct select_event_context);
127         struct tevent_fd *fde;
128
129         fde = talloc(mem_ctx?mem_ctx:ev, struct tevent_fd);
130         if (!fde) return NULL;
131
132         fde->event_ctx          = ev;
133         fde->fd                 = fd;
134         fde->flags              = flags;
135         fde->handler            = handler;
136         fde->private_data       = private_data;
137         fde->handler_name       = handler_name;
138         fde->location           = location;
139         fde->additional_flags   = 0;
140         fde->additional_data    = NULL;
141
142         DLIST_ADD(select_ev->fd_events, fde);
143         if (fde->fd > select_ev->maxfd) {
144                 select_ev->maxfd = fde->fd;
145         }
146         talloc_set_destructor(fde, select_event_fd_destructor);
147
148         return fde;
149 }
150
151 /*
152   event loop handling using select()
153 */
154 static int select_event_loop_select(struct select_event_context *select_ev, struct timeval *tvalp)
155 {
156         fd_set r_fds, w_fds;
157         struct tevent_fd *fde;
158         int selrtn;
159         uint32_t destruction_count = ++select_ev->destruction_count;
160
161         /* we maybe need to recalculate the maxfd */
162         if (select_ev->maxfd == EVENT_INVALID_MAXFD) {
163                 calc_maxfd(select_ev);
164         }
165
166         FD_ZERO(&r_fds);
167         FD_ZERO(&w_fds);
168
169         /* setup any fd events */
170         for (fde = select_ev->fd_events; fde; fde = fde->next) {
171                 if (fde->flags & TEVENT_FD_READ) {
172                         FD_SET(fde->fd, &r_fds);
173                 }
174                 if (fde->flags & TEVENT_FD_WRITE) {
175                         FD_SET(fde->fd, &w_fds);
176                 }
177         }
178
179         if (select_ev->ev->num_signal_handlers && 
180             tevent_common_check_signal(select_ev->ev)) {
181                 return 0;
182         }
183
184         selrtn = select(select_ev->maxfd+1, &r_fds, &w_fds, NULL, tvalp);
185
186         if (selrtn == -1 && errno == EINTR && 
187             select_ev->ev->num_signal_handlers) {
188                 tevent_common_check_signal(select_ev->ev);
189                 return 0;
190         }
191
192         if (selrtn == -1 && 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                 select_ev->exit_code = EBADF;
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->fd_events; fde; fde = fde->next) {
215                         uint16_t flags = 0;
216
217                         if (FD_ISSET(fde->fd, &r_fds)) flags |= TEVENT_FD_READ;
218                         if (FD_ISSET(fde->fd, &w_fds)) flags |= TEVENT_FD_WRITE;
219                         if (flags) {
220                                 fde->handler(select_ev->ev, fde, flags, fde->private_data);
221                                 if (destruction_count != select_ev->destruction_count) {
222                                         break;
223                                 }
224                         }
225                 }
226         }
227
228         return 0;
229 }               
230
231 /*
232   do a single event loop using the events defined in ev 
233 */
234 static int select_event_loop_once(struct tevent_context *ev)
235 {
236         struct select_event_context *select_ev = talloc_get_type(ev->additional_data,
237                                                            struct select_event_context);
238         struct timeval tval;
239
240         tval = tevent_common_loop_timer_delay(ev);
241         if (ev_timeval_is_zero(&tval)) {
242                 return 0;
243         }
244
245         return select_event_loop_select(select_ev, &tval);
246 }
247
248 /*
249   return on failure or (with 0) if all fd events are removed
250 */
251 static int select_event_loop_wait(struct tevent_context *ev)
252 {
253         struct select_event_context *select_ev = talloc_get_type(ev->additional_data,
254                                                            struct select_event_context);
255         select_ev->exit_code = 0;
256
257         while (select_ev->fd_events && select_ev->exit_code == 0) {
258                 if (select_event_loop_once(ev) != 0) {
259                         break;
260                 }
261         }
262
263         return select_ev->exit_code;
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,
273         .add_signal     = tevent_common_add_signal,
274         .loop_once      = select_event_loop_once,
275         .loop_wait      = select_event_loop_wait,
276 };
277
278 bool tevent_select_init(void)
279 {
280         return tevent_register_backend("select", &select_event_ops);
281 }