4074c9f0f9be0df9af9999203bb1fc81588f9bf2
[metze/samba/wip.git] / lib / tevent / tevent_internal.h
1 /* 
2    Unix SMB/CIFS implementation.
3
4    generalised event loop handling
5
6    INTERNAL STRUCTS. THERE ARE NO API GUARANTEES.
7    External users should only ever have to include this header when 
8    implementing new tevent backends.
9
10    Copyright (C) Stefan Metzmacher 2005-2009
11
12      ** NOTE! The following LGPL license applies to the tevent
13      ** library. This does NOT imply that all of Samba is released
14      ** under the LGPL
15
16    This library is free software; you can redistribute it and/or
17    modify it under the terms of the GNU Lesser General Public
18    License as published by the Free Software Foundation; either
19    version 3 of the License, or (at your option) any later version.
20
21    This library is distributed in the hope that it will be useful,
22    but WITHOUT ANY WARRANTY; without even the implied warranty of
23    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
24    Lesser General Public License for more details.
25
26    You should have received a copy of the GNU Lesser General Public
27    License along with this library; if not, see <http://www.gnu.org/licenses/>.
28 */
29
30 struct tevent_req {
31         /**
32          * @brief What to do on completion
33          *
34          * This is used for the user of an async request, fn is called when
35          * the request completes, either successfully or with an error.
36          */
37         struct {
38                 /**
39                  * @brief Completion function
40                  * Completion function, to be filled by the API user
41                  */
42                 tevent_req_fn fn;
43                 /**
44                  * @brief Private data for the completion function
45                  */
46                 void *private_data;
47         } async;
48
49         /**
50          * @brief Private state pointer for the actual implementation
51          *
52          * The implementation doing the work for the async request needs to
53          * keep around current data like for example a fd event. The user of
54          * an async request should not touch this.
55          */
56         void *data;
57
58         /**
59          * @brief A function to overwrite the default print function
60          *
61          * The implementation doing the work may want to implement a
62          * custom function to print the text representation of the async
63          * request.
64          */
65         tevent_req_print_fn private_print;
66
67         /**
68          * @brief A function to cancel the request
69          *
70          * The implementation might want to set a function
71          * that is called when the tevent_req_cancel() function
72          * was called.
73          */
74         tevent_req_cancel_fn private_cancel;
75
76         /**
77          * @brief A function to cleanup the request
78          *
79          * The implementation might want to set a function
80          * that is called before the tevent_req_done() and tevent_req_error()
81          * trigger the callers callback function.
82          */
83         struct {
84                 tevent_req_cleanup_fn fn;
85                 enum tevent_req_state state;
86         } private_cleanup;
87
88         /**
89          * @brief Internal state of the request
90          *
91          * Callers should only access this via functions and never directly.
92          */
93         struct {
94                 /**
95                  * @brief The talloc type of the data pointer
96                  *
97                  * This is filled by the tevent_req_create() macro.
98                  *
99                  * This for debugging only.
100                  */
101                 const char *private_type;
102
103                 /**
104                  * @brief The location where the request was created
105                  *
106                  * This uses the __location__ macro via the tevent_req_create()
107                  * macro.
108                  *
109                  * This for debugging only.
110                  */
111                 const char *create_location;
112
113                 /**
114                  * @brief The location where the request was finished
115                  *
116                  * This uses the __location__ macro via the tevent_req_done(),
117                  * tevent_req_error() or tevent_req_nomem() macro.
118                  *
119                  * This for debugging only.
120                  */
121                 const char *finish_location;
122
123                 /**
124                  * @brief The location where the request was canceled
125                  *
126                  * This uses the __location__ macro via the
127                  * tevent_req_cancel() macro.
128                  *
129                  * This for debugging only.
130                  */
131                 const char *cancel_location;
132
133                 /**
134                  * @brief The external state - will be queried by the caller
135                  *
136                  * While the async request is being processed, state will remain in
137                  * TEVENT_REQ_IN_PROGRESS. A request is finished if
138                  * req->state>=TEVENT_REQ_DONE.
139                  */
140                 enum tevent_req_state state;
141
142                 /**
143                  * @brief status code when finished
144                  *
145                  * This status can be queried in the async completion function. It
146                  * will be set to 0 when everything went fine.
147                  */
148                 uint64_t error;
149
150                 /**
151                  * @brief the immediate event used by tevent_req_post
152                  *
153                  */
154                 struct tevent_immediate *trigger;
155
156                 /**
157                  * @brief An event context which will be used to
158                  *        defer the _tevent_req_notify_callback().
159                  */
160                 struct tevent_context *defer_callback_ev;
161
162                 /**
163                  * @brief the timer event if tevent_req_set_endtime was used
164                  *
165                  */
166                 struct tevent_timer *timer;
167         } internal;
168 };
169
170 struct tevent_fd {
171         struct tevent_fd *prev, *next;
172         struct tevent_context *event_ctx;
173         int fd;
174         uint16_t flags; /* see TEVENT_FD_* flags */
175         tevent_fd_handler_t handler;
176         tevent_fd_close_fn_t close_fn;
177         /* this is private for the specific handler */
178         void *private_data;
179         /* this is for debugging only! */
180         const char *handler_name;
181         const char *location;
182         /* this is private for the events_ops implementation */
183         uint64_t additional_flags;
184         void *additional_data;
185 };
186
187 struct tevent_timer {
188         struct tevent_timer *prev, *next;
189         struct tevent_context *event_ctx;
190         struct timeval next_event;
191         tevent_timer_handler_t handler;
192         /* this is private for the specific handler */
193         void *private_data;
194         /* this is for debugging only! */
195         const char *handler_name;
196         const char *location;
197         /* this is private for the events_ops implementation */
198         void *additional_data;
199 };
200
201 struct tevent_immediate {
202         struct tevent_immediate *prev, *next;
203         struct tevent_context *event_ctx;
204         tevent_immediate_handler_t handler;
205         /* this is private for the specific handler */
206         void *private_data;
207         /* this is for debugging only! */
208         const char *handler_name;
209         const char *create_location;
210         const char *schedule_location;
211         /* this is private for the events_ops implementation */
212         void (*cancel_fn)(struct tevent_immediate *im);
213         void *additional_data;
214 };
215
216 struct tevent_signal {
217         struct tevent_signal *prev, *next;
218         struct tevent_context *event_ctx;
219         int signum;
220         int sa_flags;
221         tevent_signal_handler_t handler;
222         /* this is private for the specific handler */
223         void *private_data;
224         /* this is for debugging only! */
225         const char *handler_name;
226         const char *location;
227         /* this is private for the events_ops implementation */
228         void *additional_data;
229 };
230
231 struct tevent_threaded_context {
232         struct tevent_threaded_context *next, *prev;
233         struct tevent_context *event_ctx;
234 };
235
236 struct tevent_debug_ops {
237         void (*debug)(void *context, enum tevent_debug_level level,
238                       const char *fmt, va_list ap) PRINTF_ATTRIBUTE(3,0);
239         void *context;
240 };
241
242 void tevent_debug(struct tevent_context *ev, enum tevent_debug_level level,
243                   const char *fmt, ...) PRINTF_ATTRIBUTE(3,4);
244
245 struct tevent_context {
246         /* the specific events implementation */
247         const struct tevent_ops *ops;
248
249         /*
250          * The following three pointers are queried on every loop_once
251          * in the order in which they appear here. Not measured, but
252          * hopefully putting them at the top together with "ops"
253          * should make tevent a *bit* more cache-friendly than before.
254          */
255
256         /* list of signal events - used by common code */
257         struct tevent_signal *signal_events;
258
259         /* List of threaded job indicators */
260         struct tevent_threaded_context *threaded_contexts;
261
262         /* list of immediate events - used by common code */
263         struct tevent_immediate *immediate_events;
264
265         /* list of fd events - used by common code */
266         struct tevent_fd *fd_events;
267
268         /* list of timed events - used by common code */
269         struct tevent_timer *timer_events;
270
271         /* List of scheduled immediates */
272         pthread_mutex_t scheduled_mutex;
273         struct tevent_immediate *scheduled_immediates;
274
275         /* this is private for the events_ops implementation */
276         void *additional_data;
277
278         /* pipe hack used with signal handlers */
279         struct tevent_fd *wakeup_fde;
280         int wakeup_fd;
281 #ifndef HAVE_EVENT_FD
282         int wakeup_write_fd;
283 #endif
284
285         /* debugging operations */
286         struct tevent_debug_ops debug_ops;
287
288         /* info about the nesting status */
289         struct {
290                 bool allowed;
291                 uint32_t level;
292                 tevent_nesting_hook hook_fn;
293                 void *hook_private;
294         } nesting;
295
296         struct {
297                 tevent_trace_callback_t callback;
298                 void *private_data;
299         } tracing;
300
301         /*
302          * an optimization pointer into timer_events
303          * used by used by common code via
304          * tevent_common_add_timer_v2()
305          */
306         struct tevent_timer *last_zero_timer;
307
308 #ifdef HAVE_PTHREAD
309         struct tevent_context *prev, *next;
310 #endif
311 };
312
313 const struct tevent_ops *tevent_find_ops_byname(const char *name);
314
315 int tevent_common_context_destructor(struct tevent_context *ev);
316 int tevent_common_loop_wait(struct tevent_context *ev,
317                             const char *location);
318
319 int tevent_common_fd_destructor(struct tevent_fd *fde);
320 struct tevent_fd *tevent_common_add_fd(struct tevent_context *ev,
321                                        TALLOC_CTX *mem_ctx,
322                                        int fd,
323                                        uint16_t flags,
324                                        tevent_fd_handler_t handler,
325                                        void *private_data,
326                                        const char *handler_name,
327                                        const char *location);
328 void tevent_common_fd_set_close_fn(struct tevent_fd *fde,
329                                    tevent_fd_close_fn_t close_fn);
330 uint16_t tevent_common_fd_get_flags(struct tevent_fd *fde);
331 void tevent_common_fd_set_flags(struct tevent_fd *fde, uint16_t flags);
332
333 struct tevent_timer *tevent_common_add_timer(struct tevent_context *ev,
334                                              TALLOC_CTX *mem_ctx,
335                                              struct timeval next_event,
336                                              tevent_timer_handler_t handler,
337                                              void *private_data,
338                                              const char *handler_name,
339                                              const char *location);
340 struct tevent_timer *tevent_common_add_timer_v2(struct tevent_context *ev,
341                                                 TALLOC_CTX *mem_ctx,
342                                                 struct timeval next_event,
343                                                 tevent_timer_handler_t handler,
344                                                 void *private_data,
345                                                 const char *handler_name,
346                                                 const char *location);
347 struct timeval tevent_common_loop_timer_delay(struct tevent_context *);
348
349 void tevent_common_schedule_immediate(struct tevent_immediate *im,
350                                       struct tevent_context *ev,
351                                       tevent_immediate_handler_t handler,
352                                       void *private_data,
353                                       const char *handler_name,
354                                       const char *location);
355 bool tevent_common_loop_immediate(struct tevent_context *ev);
356 void tevent_common_threaded_activate_immediate(struct tevent_context *ev);
357
358 bool tevent_common_have_events(struct tevent_context *ev);
359 int tevent_common_wakeup_init(struct tevent_context *ev);
360 int tevent_common_wakeup(struct tevent_context *ev);
361
362 struct tevent_signal *tevent_common_add_signal(struct tevent_context *ev,
363                                                TALLOC_CTX *mem_ctx,
364                                                int signum,
365                                                int sa_flags,
366                                                tevent_signal_handler_t handler,
367                                                void *private_data,
368                                                const char *handler_name,
369                                                const char *location);
370 int tevent_common_check_signal(struct tevent_context *ev);
371 void tevent_cleanup_pending_signal_handlers(struct tevent_signal *se);
372
373 bool tevent_standard_init(void);
374 bool tevent_select_init(void);
375 bool tevent_poll_init(void);
376 void tevent_poll_event_add_fd_internal(struct tevent_context *ev,
377                                        struct tevent_fd *fde);
378 bool tevent_poll_mt_init(void);
379 #ifdef HAVE_EPOLL
380 bool tevent_epoll_init(void);
381 void tevent_epoll_set_panic_fallback(struct tevent_context *ev,
382                         bool (*panic_fallback)(struct tevent_context *ev,
383                                                bool replay));
384 #endif
385 #ifdef HAVE_SOLARIS_PORTS
386 bool tevent_port_init(void);
387 #ifdef HAVE_KQUEUE
388 bool tevent_kqueue_init(void);
389 bool tevent_kqueue_set_panic_fallback(struct tevent_context *ev,
390                         bool (*panic_fallback)(struct tevent_context *ev,
391                                                bool replay));
392 #ifndef HAVE_SYS_EVENT_H
393 struct kevent {
394         uintptr_t ident;             /* identifier for this event */
395         short     filter;            /* filter for event */
396 #define EVFILT_WRITE 1
397 #define EVFILT_READ 2
398         u_short   flags;             /* action flags for kqueue */
399 #define EV_ADD          0x0001
400 #define EV_ENABLE       0x0002
401 #define EV_DISABLE      0x0004
402 #define EV_DELETE       0x0008
403 #define EV_EOF          0x0010
404         u_int     fflags;            /* filter flag value */
405         intptr_t  data;      /* filter data value */
406         void      *udata;            /* opaque user data identifier */
407 };
408
409 #define EV_SET(_kev, _ident, _filter, _flags, _fflags, _data, _udata) do { \
410         struct kevent *__kev = _kev; \
411         __kev->ident = _ident; \
412         __kev->filter = _filter; \
413         __kev->flags = _flags; \
414         __kev->fflags = _fflags; \
415         __kev->data = (intptr_t)_data; \
416         __kev->udata = _udata; \
417 } while(0)
418
419 static inline int kqueue(void)
420 {
421         errno = ENOSYS;
422         return -1;
423 }
424
425 struct timespec;
426 static inline int kevent(int kq, const struct kevent *changelist, int nchanges,
427                          struct kevent *eventlist, int nevents,
428                          const struct timespec *timeout)
429 {
430         errno = ENOSYS;
431         return -1;
432 }
433 #endif /* not HAVE_SYS_EVENT_H */
434 #endif
435
436
437 void tevent_trace_point_callback(struct tevent_context *ev,
438                                  enum tevent_trace_point);