quota: add supprt for gfs2
[samba.git] / source3 / lib / events.c
1 /*
2    Unix SMB/CIFS implementation.
3    Timed event library.
4    Copyright (C) Andrew Tridgell 1992-1998
5    Copyright (C) Volker Lendecke 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 "includes.h"
22 #include <tevent_internal.h>
23
24 void event_fd_set_writeable(struct tevent_fd *fde)
25 {
26         TEVENT_FD_WRITEABLE(fde);
27 }
28
29 void event_fd_set_not_writeable(struct tevent_fd *fde)
30 {
31         TEVENT_FD_NOT_WRITEABLE(fde);
32 }
33
34 void event_fd_set_readable(struct tevent_fd *fde)
35 {
36         TEVENT_FD_READABLE(fde);
37 }
38
39 void event_fd_set_not_readable(struct tevent_fd *fde)
40 {
41         TEVENT_FD_NOT_READABLE(fde);
42 }
43
44 /*
45  * Return if there's something in the queue
46  */
47
48 bool event_add_to_select_args(struct tevent_context *ev,
49                               const struct timeval *now,
50                               fd_set *read_fds, fd_set *write_fds,
51                               struct timeval *timeout, int *maxfd)
52 {
53         struct tevent_fd *fde;
54         struct timeval diff;
55         bool ret = false;
56
57         for (fde = ev->fd_events; fde; fde = fde->next) {
58                 if (fde->fd < 0 || fde->fd >= FD_SETSIZE) {
59                         /* We ignore here, as it shouldn't be
60                            possible to add an invalid fde->fd
61                            but we don't want FD_SET to see an
62                            invalid fd. */
63                         continue;
64                 }
65
66                 if (fde->flags & EVENT_FD_READ) {
67                         FD_SET(fde->fd, read_fds);
68                         ret = true;
69                 }
70                 if (fde->flags & EVENT_FD_WRITE) {
71                         FD_SET(fde->fd, write_fds);
72                         ret = true;
73                 }
74
75                 if ((fde->flags & (EVENT_FD_READ|EVENT_FD_WRITE))
76                     && (fde->fd > *maxfd)) {
77                         *maxfd = fde->fd;
78                 }
79         }
80
81         if (ev->immediate_events != NULL) {
82                 *timeout = timeval_zero();
83                 return true;
84         }
85
86         if (ev->timer_events == NULL) {
87                 return ret;
88         }
89
90         diff = timeval_until(now, &ev->timer_events->next_event);
91         *timeout = timeval_min(timeout, &diff);
92
93         return true;
94 }
95
96 bool run_events(struct tevent_context *ev,
97                 int selrtn, fd_set *read_fds, fd_set *write_fds)
98 {
99         struct tevent_fd *fde;
100         struct timeval now;
101
102         if (ev->signal_events &&
103             tevent_common_check_signal(ev)) {
104                 return true;
105         }
106
107         if (ev->immediate_events &&
108             tevent_common_loop_immediate(ev)) {
109                 return true;
110         }
111
112         GetTimeOfDay(&now);
113
114         if ((ev->timer_events != NULL)
115             && (timeval_compare(&now, &ev->timer_events->next_event) >= 0)) {
116                 /* this older events system did not auto-free timed
117                    events on running them, and had a race condition
118                    where the event could be called twice if the
119                    talloc_free of the te happened after the callback
120                    made a call which invoked the event loop. To avoid
121                    this while still allowing old code which frees the
122                    te, we need to create a temporary context which
123                    will be used to ensure the te is freed. We also
124                    remove the te from the timed event list before we
125                    call the handler, to ensure we can't loop */
126
127                 struct tevent_timer *te = ev->timer_events;
128                 TALLOC_CTX *tmp_ctx = talloc_new(ev);
129
130                 DEBUG(10, ("Running timed event \"%s\" %p\n",
131                            ev->timer_events->handler_name, ev->timer_events));
132
133                 DLIST_REMOVE(ev->timer_events, te);
134                 talloc_steal(tmp_ctx, te);
135
136                 te->handler(ev, te, now, te->private_data);
137
138                 talloc_free(tmp_ctx);
139                 return true;
140         }
141
142         if (selrtn <= 0) {
143                 /*
144                  * No fd ready
145                  */
146                 return false;
147         }
148
149         for (fde = ev->fd_events; fde; fde = fde->next) {
150                 uint16 flags = 0;
151
152                 if (FD_ISSET(fde->fd, read_fds)) flags |= EVENT_FD_READ;
153                 if (FD_ISSET(fde->fd, write_fds)) flags |= EVENT_FD_WRITE;
154
155                 if (flags & fde->flags) {
156                         DLIST_DEMOTE(ev->fd_events, fde, struct tevent_fd *);
157                         fde->handler(ev, fde, flags, fde->private_data);
158                         return true;
159                 }
160         }
161
162         return false;
163 }
164
165
166 struct timeval *get_timed_events_timeout(struct tevent_context *ev,
167                                          struct timeval *to_ret)
168 {
169         struct timeval now;
170
171         if ((ev->timer_events == NULL) && (ev->immediate_events == NULL)) {
172                 return NULL;
173         }
174         if (ev->immediate_events != NULL) {
175                 *to_ret = timeval_zero();
176                 return to_ret;
177         }
178
179         now = timeval_current();
180         *to_ret = timeval_until(&now, &ev->timer_events->next_event);
181
182         DEBUG(10, ("timed_events_timeout: %d/%d\n", (int)to_ret->tv_sec,
183                 (int)to_ret->tv_usec));
184
185         return to_ret;
186 }
187
188 static int s3_event_loop_once(struct tevent_context *ev, const char *location)
189 {
190         struct timeval now, to;
191         fd_set r_fds, w_fds;
192         int maxfd = 0;
193         int ret;
194
195         FD_ZERO(&r_fds);
196         FD_ZERO(&w_fds);
197
198         to.tv_sec = 9999;       /* Max timeout */
199         to.tv_usec = 0;
200
201         if (run_events(ev, 0, NULL, NULL)) {
202                 return 0;
203         }
204
205         GetTimeOfDay(&now);
206
207         if (!event_add_to_select_args(ev, &now, &r_fds, &w_fds, &to, &maxfd)) {
208                 return -1;
209         }
210
211         ret = sys_select(maxfd+1, &r_fds, &w_fds, NULL, &to);
212
213         if (ret == -1 && errno != EINTR) {
214                 tevent_debug(ev, TEVENT_DEBUG_FATAL,
215                              "sys_select() failed: %d:%s\n",
216                              errno, strerror(errno));
217                 return -1;
218         }
219
220         run_events(ev, ret, &r_fds, &w_fds);
221         return 0;
222 }
223
224 void event_context_reinit(struct tevent_context *ev)
225 {
226         tevent_common_context_destructor(ev);
227         return;
228 }
229
230 static int s3_event_context_init(struct tevent_context *ev)
231 {
232         return 0;
233 }
234
235 void dump_event_list(struct tevent_context *ev)
236 {
237         struct tevent_timer *te;
238         struct tevent_fd *fe;
239         struct timeval evt, now;
240
241         if (!ev) {
242                 return;
243         }
244
245         now = timeval_current();
246
247         DEBUG(10,("dump_event_list:\n"));
248
249         for (te = ev->timer_events; te; te = te->next) {
250
251                 evt = timeval_until(&now, &te->next_event);
252
253                 DEBUGADD(10,("Timed Event \"%s\" %p handled in %d seconds (at %s)\n",
254                            te->handler_name,
255                            te,
256                            (int)evt.tv_sec,
257                            http_timestring(talloc_tos(), te->next_event.tv_sec)));
258         }
259
260         for (fe = ev->fd_events; fe; fe = fe->next) {
261
262                 DEBUGADD(10,("FD Event %d %p, flags: 0x%04x\n",
263                            fe->fd,
264                            fe,
265                            fe->flags));
266         }
267 }
268
269 static const struct tevent_ops s3_event_ops = {
270         .context_init           = s3_event_context_init,
271         .add_fd                 = tevent_common_add_fd,
272         .set_fd_close_fn        = tevent_common_fd_set_close_fn,
273         .get_fd_flags           = tevent_common_fd_get_flags,
274         .set_fd_flags           = tevent_common_fd_set_flags,
275         .add_timer              = tevent_common_add_timer,
276         .schedule_immediate     = tevent_common_schedule_immediate,
277         .add_signal             = tevent_common_add_signal,
278         .loop_once              = s3_event_loop_once,
279         .loop_wait              = tevent_common_loop_wait,
280 };
281
282 static bool s3_tevent_init(void)
283 {
284         static bool initialized;
285         if (initialized) {
286                 return true;
287         }
288         initialized = tevent_register_backend("s3", &s3_event_ops);
289         tevent_set_default_backend("s3");
290         return initialized;
291 }
292
293 /*
294   this is used to catch debug messages from events
295 */
296 static void s3_event_debug(void *context, enum tevent_debug_level level,
297                            const char *fmt, va_list ap)  PRINTF_ATTRIBUTE(3,0);
298
299 static void s3_event_debug(void *context, enum tevent_debug_level level,
300                            const char *fmt, va_list ap)
301 {
302         int samba_level = -1;
303         char *s = NULL;
304         switch (level) {
305         case TEVENT_DEBUG_FATAL:
306                 samba_level = 0;
307                 break;
308         case TEVENT_DEBUG_ERROR:
309                 samba_level = 1;
310                 break;
311         case TEVENT_DEBUG_WARNING:
312                 samba_level = 2;
313                 break;
314         case TEVENT_DEBUG_TRACE:
315                 samba_level = 11;
316                 break;
317
318         };
319         if (vasprintf(&s, fmt, ap) == -1) {
320                 return;
321         }
322         DEBUG(samba_level, ("s3_event: %s", s));
323         free(s);
324 }
325
326 struct tevent_context *s3_tevent_context_init(TALLOC_CTX *mem_ctx)
327 {
328         struct tevent_context *ev;
329
330         s3_tevent_init();
331
332         ev = tevent_context_init_byname(mem_ctx, "s3");
333         if (ev) {
334                 tevent_set_debug(ev, s3_event_debug, NULL);
335         }
336
337         return ev;
338 }
339