change Christinas previous patch to only perform the check/logging
[sahlberg/ctdb.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
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
96         return tevent_common_fd_destructor(fde);
97 }
98
99 /*
100   add a fd based event
101   return NULL on failure (memory allocation error)
102 */
103 static struct tevent_fd *select_event_add_fd(struct tevent_context *ev, TALLOC_CTX *mem_ctx,
104                                              int fd, uint16_t flags,
105                                              tevent_fd_handler_t handler,
106                                              void *private_data,
107                                              const char *handler_name,
108                                              const char *location)
109 {
110         struct select_event_context *select_ev = talloc_get_type(ev->additional_data,
111                                                            struct select_event_context);
112         struct tevent_fd *fde;
113
114         fde = tevent_common_add_fd(ev, mem_ctx, fd, flags,
115                                    handler, private_data,
116                                    handler_name, location);
117         if (!fde) return NULL;
118
119         if ((select_ev->maxfd != EVENT_INVALID_MAXFD)
120             && (fde->fd > select_ev->maxfd)) {
121                 select_ev->maxfd = fde->fd;
122         }
123         talloc_set_destructor(fde, select_event_fd_destructor);
124
125         return fde;
126 }
127
128 extern pid_t ctdbd_pid;
129
130 /*
131   event loop handling using select()
132 */
133 static int select_event_loop_select(struct select_event_context *select_ev, struct timeval *tvalp)
134 {
135         fd_set r_fds, w_fds;
136         struct tevent_fd *fde;
137         int selrtn;
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         if (getpid() == ctdbd_pid) tevent_before_wait(select_ev->ev);
163         selrtn = select(select_ev->maxfd+1, &r_fds, &w_fds, NULL, tvalp);
164         if (getpid() == ctdbd_pid) tevent_after_wait(select_ev->ev);
165
166         if (selrtn == -1 && errno == EINTR &&
167             select_ev->ev->signal_events) {
168                 tevent_common_check_signal(select_ev->ev);
169                 return 0;
170         }
171
172         if (selrtn == -1 && errno == EBADF) {
173                 /* the socket is dead! this should never
174                    happen as the socket should have first been
175                    made readable and that should have removed
176                    the event, so this must be a bug. This is a
177                    fatal error. */
178                 tevent_debug(select_ev->ev, TEVENT_DEBUG_FATAL,
179                              "ERROR: EBADF on select_event_loop_once\n");
180                 select_ev->exit_code = EBADF;
181                 return -1;
182         }
183
184         if (selrtn == 0 && tvalp) {
185                 /* we don't care about a possible delay here */
186                 tevent_common_loop_timer_delay(select_ev->ev);
187                 return 0;
188         }
189
190         if (selrtn > 0) {
191                 /* at least one file descriptor is ready - check
192                    which ones and call the handler, being careful to allow
193                    the handler to remove itself when called */
194                 for (fde = select_ev->ev->fd_events; fde; fde = fde->next) {
195                         uint16_t flags = 0;
196
197                         if (FD_ISSET(fde->fd, &r_fds)) flags |= TEVENT_FD_READ;
198                         if (FD_ISSET(fde->fd, &w_fds)) flags |= TEVENT_FD_WRITE;
199                         if (flags) {
200                                 fde->handler(select_ev->ev, fde, flags, fde->private_data);
201                                 break;
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, const char *location)
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         if (ev->signal_events &&
219             tevent_common_check_signal(ev)) {
220                 return 0;
221         }
222
223         if (ev->immediate_events &&
224             tevent_common_loop_immediate(ev)) {
225                 return 0;
226         }
227
228         tval = tevent_common_loop_timer_delay(ev);
229         if (tevent_timeval_is_zero(&tval)) {
230                 return 0;
231         }
232
233         return select_event_loop_select(select_ev, &tval);
234 }
235
236 static const struct tevent_ops select_event_ops = {
237         .context_init           = select_event_context_init,
238         .add_fd                 = select_event_add_fd,
239         .set_fd_close_fn        = tevent_common_fd_set_close_fn,
240         .get_fd_flags           = tevent_common_fd_get_flags,
241         .set_fd_flags           = tevent_common_fd_set_flags,
242         .add_timer              = tevent_common_add_timer,
243         .schedule_immediate     = tevent_common_schedule_immediate,
244         .add_signal             = tevent_common_add_signal,
245         .loop_once              = select_event_loop_once,
246         .loop_wait              = tevent_common_loop_wait,
247 };
248
249 _PRIVATE_ bool tevent_select_init(void)
250 {
251         return tevent_register_backend("select", &select_event_ops);
252 }