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