s4-python: Install external included packages only if they're not present on the...
[nivanova/samba-autobuild/.git] / lib / tevent / tevent.h
1 /* 
2    Unix SMB/CIFS implementation.
3
4    generalised event loop handling
5
6    Copyright (C) Andrew Tridgell 2005
7    Copyright (C) Stefan Metzmacher 2005-2009
8    Copyright (C) Volker Lendecke 2008
9
10      ** NOTE! The following LGPL license applies to the tevent
11      ** library. This does NOT imply that all of Samba is released
12      ** under the LGPL
13
14    This library is free software; you can redistribute it and/or
15    modify it under the terms of the GNU Lesser General Public
16    License as published by the Free Software Foundation; either
17    version 3 of the License, or (at your option) any later version.
18
19    This library is distributed in the hope that it will be useful,
20    but WITHOUT ANY WARRANTY; without even the implied warranty of
21    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22    Lesser General Public License for more details.
23
24    You should have received a copy of the GNU Lesser General Public
25    License along with this library; if not, see <http://www.gnu.org/licenses/>.
26 */
27
28 #ifndef __TEVENT_H__
29 #define __TEVENT_H__
30
31 #include <stdint.h>
32 #include <talloc.h>
33 #include <sys/time.h>
34 #include <stdbool.h>
35
36 struct tevent_context;
37 struct tevent_ops;
38 struct tevent_fd;
39 struct tevent_timer;
40 struct tevent_immediate;
41 struct tevent_signal;
42
43 /* event handler types */
44 typedef void (*tevent_fd_handler_t)(struct tevent_context *ev,
45                                     struct tevent_fd *fde,
46                                     uint16_t flags,
47                                     void *private_data);
48 typedef void (*tevent_fd_close_fn_t)(struct tevent_context *ev,
49                                      struct tevent_fd *fde,
50                                      int fd,
51                                      void *private_data);
52 typedef void (*tevent_timer_handler_t)(struct tevent_context *ev,
53                                        struct tevent_timer *te,
54                                        struct timeval current_time,
55                                        void *private_data);
56 typedef void (*tevent_immediate_handler_t)(struct tevent_context *ctx,
57                                            struct tevent_immediate *im,
58                                            void *private_data);
59 typedef void (*tevent_signal_handler_t)(struct tevent_context *ev,
60                                         struct tevent_signal *se,
61                                         int signum,
62                                         int count,
63                                         void *siginfo,
64                                         void *private_data);
65
66 struct tevent_context *tevent_context_init(TALLOC_CTX *mem_ctx);
67 struct tevent_context *tevent_context_init_byname(TALLOC_CTX *mem_ctx, const char *name);
68 const char **tevent_backend_list(TALLOC_CTX *mem_ctx);
69 void tevent_set_default_backend(const char *backend);
70
71 struct tevent_fd *_tevent_add_fd(struct tevent_context *ev,
72                                  TALLOC_CTX *mem_ctx,
73                                  int fd,
74                                  uint16_t flags,
75                                  tevent_fd_handler_t handler,
76                                  void *private_data,
77                                  const char *handler_name,
78                                  const char *location);
79 #define tevent_add_fd(ev, mem_ctx, fd, flags, handler, private_data) \
80         _tevent_add_fd(ev, mem_ctx, fd, flags, handler, private_data, \
81                        #handler, __location__)
82
83 struct tevent_timer *_tevent_add_timer(struct tevent_context *ev,
84                                        TALLOC_CTX *mem_ctx,
85                                        struct timeval next_event,
86                                        tevent_timer_handler_t handler,
87                                        void *private_data,
88                                        const char *handler_name,
89                                        const char *location);
90 #define tevent_add_timer(ev, mem_ctx, next_event, handler, private_data) \
91         _tevent_add_timer(ev, mem_ctx, next_event, handler, private_data, \
92                           #handler, __location__)
93
94 struct tevent_immediate *_tevent_create_immediate(TALLOC_CTX *mem_ctx,
95                                                   const char *location);
96 #define tevent_create_immediate(mem_ctx) \
97         _tevent_create_immediate(mem_ctx, __location__)
98
99 void _tevent_schedule_immediate(struct tevent_immediate *im,
100                                 struct tevent_context *ctx,
101                                 tevent_immediate_handler_t handler,
102                                 void *private_data,
103                                 const char *handler_name,
104                                 const char *location);
105 #define tevent_schedule_immediate(im, ctx, handler, private_data) \
106         _tevent_schedule_immediate(im, ctx, handler, private_data, \
107                                    #handler, __location__);
108
109 struct tevent_signal *_tevent_add_signal(struct tevent_context *ev,
110                                          TALLOC_CTX *mem_ctx,
111                                          int signum,
112                                          int sa_flags,
113                                          tevent_signal_handler_t handler,
114                                          void *private_data,
115                                          const char *handler_name,
116                                          const char *location);
117 #define tevent_add_signal(ev, mem_ctx, signum, sa_flags, handler, private_data) \
118         _tevent_add_signal(ev, mem_ctx, signum, sa_flags, handler, private_data, \
119                            #handler, __location__)
120
121 int _tevent_loop_once(struct tevent_context *ev, const char *location);
122 #define tevent_loop_once(ev) \
123         _tevent_loop_once(ev, __location__) \
124
125 int _tevent_loop_wait(struct tevent_context *ev, const char *location);
126 #define tevent_loop_wait(ev) \
127         _tevent_loop_wait(ev, __location__) \
128
129 void tevent_fd_set_close_fn(struct tevent_fd *fde,
130                             tevent_fd_close_fn_t close_fn);
131 void tevent_fd_set_auto_close(struct tevent_fd *fde);
132 uint16_t tevent_fd_get_flags(struct tevent_fd *fde);
133 void tevent_fd_set_flags(struct tevent_fd *fde, uint16_t flags);
134
135 bool tevent_signal_support(struct tevent_context *ev);
136
137 void tevent_set_abort_fn(void (*abort_fn)(const char *reason));
138
139 /* bits for file descriptor event flags */
140 #define TEVENT_FD_READ 1
141 #define TEVENT_FD_WRITE 2
142
143 #define TEVENT_FD_WRITEABLE(fde) \
144         tevent_fd_set_flags(fde, tevent_fd_get_flags(fde) | TEVENT_FD_WRITE)
145 #define TEVENT_FD_READABLE(fde) \
146         tevent_fd_set_flags(fde, tevent_fd_get_flags(fde) | TEVENT_FD_READ)
147
148 #define TEVENT_FD_NOT_WRITEABLE(fde) \
149         tevent_fd_set_flags(fde, tevent_fd_get_flags(fde) & ~TEVENT_FD_WRITE)
150 #define TEVENT_FD_NOT_READABLE(fde) \
151         tevent_fd_set_flags(fde, tevent_fd_get_flags(fde) & ~TEVENT_FD_READ)
152
153 /* DEBUG */
154 enum tevent_debug_level {
155         TEVENT_DEBUG_FATAL,
156         TEVENT_DEBUG_ERROR,
157         TEVENT_DEBUG_WARNING,
158         TEVENT_DEBUG_TRACE
159 };
160
161 int tevent_set_debug(struct tevent_context *ev,
162                      void (*debug)(void *context,
163                                    enum tevent_debug_level level,
164                                    const char *fmt,
165                                    va_list ap) PRINTF_ATTRIBUTE(3,0),
166                      void *context);
167 int tevent_set_debug_stderr(struct tevent_context *ev);
168
169 /**
170  * An async request moves between the following 4 states:
171  */
172 enum tevent_req_state {
173         /**
174          * we are creating the request
175          */
176         TEVENT_REQ_INIT,
177         /**
178          * we are waiting the request to complete
179          */
180         TEVENT_REQ_IN_PROGRESS,
181         /**
182          * the request is finished
183          */
184         TEVENT_REQ_DONE,
185         /**
186          * A user error has occurred
187          */
188         TEVENT_REQ_USER_ERROR,
189         /**
190          * Request timed out
191          */
192         TEVENT_REQ_TIMED_OUT,
193         /**
194          * No memory in between
195          */
196         TEVENT_REQ_NO_MEMORY,
197         /**
198          * the request is already received by the caller
199          */
200         TEVENT_REQ_RECEIVED
201 };
202
203 /**
204  * @brief An async request
205  *
206  * This represents an async request being processed by callbacks via an event
207  * context. A user can issue for example a write request to a socket, giving
208  * an implementation function the fd, the buffer and the number of bytes to
209  * transfer. The function issuing the request will immediately return without
210  * blocking most likely without having sent anything. The API user then fills
211  * in req->async.fn and req->async.private_data, functions that are called
212  * when the request is finished.
213  *
214  * It is up to the user of the async request to talloc_free it after it has
215  * finished. This can happen while the completion function is called.
216  */
217
218 struct tevent_req;
219
220 typedef void (*tevent_req_fn)(struct tevent_req *);
221
222 void tevent_req_set_callback(struct tevent_req *req, tevent_req_fn fn, void *pvt);
223 void *_tevent_req_callback_data(struct tevent_req *req);
224 void *_tevent_req_data(struct tevent_req *req);
225
226 #define tevent_req_callback_data(_req, _type) \
227         talloc_get_type_abort(_tevent_req_callback_data(_req), _type)
228 #define tevent_req_callback_data_void(_req) \
229         _tevent_req_callback_data(_req)
230 #define tevent_req_data(_req, _type) \
231         talloc_get_type_abort(_tevent_req_data(_req), _type)
232
233 typedef char *(*tevent_req_print_fn)(struct tevent_req *, TALLOC_CTX *);
234
235 void tevent_req_set_print_fn(struct tevent_req *req, tevent_req_print_fn fn);
236
237 char *tevent_req_default_print(struct tevent_req *req, TALLOC_CTX *mem_ctx);
238
239 char *tevent_req_print(TALLOC_CTX *mem_ctx, struct tevent_req *req);
240
241 typedef bool (*tevent_req_cancel_fn)(struct tevent_req *);
242
243 void tevent_req_set_cancel_fn(struct tevent_req *req, tevent_req_cancel_fn fn);
244
245 bool _tevent_req_cancel(struct tevent_req *req, const char *location);
246 #define tevent_req_cancel(req) \
247         _tevent_req_cancel(req, __location__)
248
249 struct tevent_req *_tevent_req_create(TALLOC_CTX *mem_ctx,
250                                       void *pstate,
251                                       size_t state_size,
252                                       const char *type,
253                                       const char *location);
254
255 #define tevent_req_create(_mem_ctx, _pstate, _type) \
256         _tevent_req_create((_mem_ctx), (_pstate), sizeof(_type), \
257                            #_type, __location__)
258
259 bool tevent_req_set_endtime(struct tevent_req *req,
260                             struct tevent_context *ev,
261                             struct timeval endtime);
262
263 void _tevent_req_notify_callback(struct tevent_req *req, const char *location);
264 #define tevent_req_notify_callback(req)         \
265         _tevent_req_notify_callback(req, __location__)
266
267 void _tevent_req_done(struct tevent_req *req,
268                       const char *location);
269 #define tevent_req_done(req) \
270         _tevent_req_done(req, __location__)
271
272 bool _tevent_req_error(struct tevent_req *req,
273                        uint64_t error,
274                        const char *location);
275 #define tevent_req_error(req, error) \
276         _tevent_req_error(req, error, __location__)
277
278 bool _tevent_req_nomem(const void *p,
279                        struct tevent_req *req,
280                        const char *location);
281 #define tevent_req_nomem(p, req) \
282         _tevent_req_nomem(p, req, __location__)
283
284 struct tevent_req *tevent_req_post(struct tevent_req *req,
285                                    struct tevent_context *ev);
286
287 bool tevent_req_is_in_progress(struct tevent_req *req);
288
289 bool tevent_req_poll(struct tevent_req *req,
290                      struct tevent_context *ev);
291
292 bool tevent_req_is_error(struct tevent_req *req,
293                          enum tevent_req_state *state,
294                          uint64_t *error);
295
296 void tevent_req_received(struct tevent_req *req);
297
298 struct tevent_req *tevent_wakeup_send(TALLOC_CTX *mem_ctx,
299                                       struct tevent_context *ev,
300                                       struct timeval wakeup_time);
301 bool tevent_wakeup_recv(struct tevent_req *req);
302
303 int tevent_timeval_compare(const struct timeval *tv1,
304                            const struct timeval *tv2);
305
306 struct timeval tevent_timeval_zero(void);
307
308 struct timeval tevent_timeval_current(void);
309
310 struct timeval tevent_timeval_set(uint32_t secs, uint32_t usecs);
311
312 struct timeval tevent_timeval_until(const struct timeval *tv1,
313                                     const struct timeval *tv2);
314
315 bool tevent_timeval_is_zero(const struct timeval *tv);
316
317 struct timeval tevent_timeval_add(const struct timeval *tv, uint32_t secs,
318                                   uint32_t usecs);
319
320 struct timeval tevent_timeval_current_ofs(uint32_t secs, uint32_t usecs);
321
322 struct tevent_queue;
323
324 struct tevent_queue *_tevent_queue_create(TALLOC_CTX *mem_ctx,
325                                           const char *name,
326                                           const char *location);
327
328 #define tevent_queue_create(_mem_ctx, _name) \
329         _tevent_queue_create((_mem_ctx), (_name), __location__)
330
331 typedef void (*tevent_queue_trigger_fn_t)(struct tevent_req *req,
332                                           void *private_data);
333 bool tevent_queue_add(struct tevent_queue *queue,
334                       struct tevent_context *ev,
335                       struct tevent_req *req,
336                       tevent_queue_trigger_fn_t trigger,
337                       void *private_data);
338 void tevent_queue_start(struct tevent_queue *queue);
339 void tevent_queue_stop(struct tevent_queue *queue);
340
341 size_t tevent_queue_length(struct tevent_queue *queue);
342
343 typedef int (*tevent_nesting_hook)(struct tevent_context *ev,
344                                    void *private_data,
345                                    uint32_t level,
346                                    bool begin,
347                                    void *stack_ptr,
348                                    const char *location);
349 #ifdef TEVENT_DEPRECATED
350 #ifndef _DEPRECATED_
351 #if (__GNUC__ >= 3) && (__GNUC_MINOR__ >= 1 )
352 #define _DEPRECATED_ __attribute__ ((deprecated))
353 #else
354 #define _DEPRECATED_
355 #endif
356 #endif
357 void tevent_loop_allow_nesting(struct tevent_context *ev) _DEPRECATED_;
358 void tevent_loop_set_nesting_hook(struct tevent_context *ev,
359                                   tevent_nesting_hook hook,
360                                   void *private_data) _DEPRECATED_;
361 int _tevent_loop_until(struct tevent_context *ev,
362                        bool (*finished)(void *private_data),
363                        void *private_data,
364                        const char *location) _DEPRECATED_;
365 #define tevent_loop_until(ev, finished, private_data) \
366         _tevent_loop_until(ev, finished, private_data, __location__)
367 #endif
368
369 int tevent_re_initialise(struct tevent_context *ev);
370
371
372 /**
373  * The following structure and registration functions are exclusively
374  * needed for people writing and pluggin a different event engine.
375  * There is nothing useful for normal tevent user in here.
376  */
377
378 struct tevent_ops {
379         /* context init */
380         int (*context_init)(struct tevent_context *ev);
381
382         /* fd_event functions */
383         struct tevent_fd *(*add_fd)(struct tevent_context *ev,
384                                     TALLOC_CTX *mem_ctx,
385                                     int fd, uint16_t flags,
386                                     tevent_fd_handler_t handler,
387                                     void *private_data,
388                                     const char *handler_name,
389                                     const char *location);
390         void (*set_fd_close_fn)(struct tevent_fd *fde,
391                                 tevent_fd_close_fn_t close_fn);
392         uint16_t (*get_fd_flags)(struct tevent_fd *fde);
393         void (*set_fd_flags)(struct tevent_fd *fde, uint16_t flags);
394
395         /* timed_event functions */
396         struct tevent_timer *(*add_timer)(struct tevent_context *ev,
397                                           TALLOC_CTX *mem_ctx,
398                                           struct timeval next_event,
399                                           tevent_timer_handler_t handler,
400                                           void *private_data,
401                                           const char *handler_name,
402                                           const char *location);
403
404         /* immediate event functions */
405         void (*schedule_immediate)(struct tevent_immediate *im,
406                                    struct tevent_context *ev,
407                                    tevent_immediate_handler_t handler,
408                                    void *private_data,
409                                    const char *handler_name,
410                                    const char *location);
411
412         /* signal functions */
413         struct tevent_signal *(*add_signal)(struct tevent_context *ev,
414                                             TALLOC_CTX *mem_ctx,
415                                             int signum, int sa_flags,
416                                             tevent_signal_handler_t handler,
417                                             void *private_data,
418                                             const char *handler_name,
419                                             const char *location);
420
421         /* loop functions */
422         int (*loop_once)(struct tevent_context *ev, const char *location);
423         int (*loop_wait)(struct tevent_context *ev, const char *location);
424 };
425
426 bool tevent_register_backend(const char *name, const struct tevent_ops *ops);
427
428
429 /**
430  * The following definitions are usueful only for compatibility with the
431  * implementation originally developed within the samba4 code and will be
432  * soon removed. Please NEVER use in new code.
433  */
434
435 #ifdef TEVENT_COMPAT_DEFINES
436
437 #define event_context   tevent_context
438 #define event_ops       tevent_ops
439 #define fd_event        tevent_fd
440 #define timed_event     tevent_timer
441 #define signal_event    tevent_signal
442
443 #define event_fd_handler_t      tevent_fd_handler_t
444 #define event_timed_handler_t   tevent_timer_handler_t
445 #define event_signal_handler_t  tevent_signal_handler_t
446
447 #define event_context_init(mem_ctx) \
448         tevent_context_init(mem_ctx)
449
450 #define event_context_init_byname(mem_ctx, name) \
451         tevent_context_init_byname(mem_ctx, name)
452
453 #define event_backend_list(mem_ctx) \
454         tevent_backend_list(mem_ctx)
455
456 #define event_set_default_backend(backend) \
457         tevent_set_default_backend(backend)
458
459 #define event_add_fd(ev, mem_ctx, fd, flags, handler, private_data) \
460         tevent_add_fd(ev, mem_ctx, fd, flags, handler, private_data)
461
462 #define event_add_timed(ev, mem_ctx, next_event, handler, private_data) \
463         tevent_add_timer(ev, mem_ctx, next_event, handler, private_data)
464
465 #define event_add_signal(ev, mem_ctx, signum, sa_flags, handler, private_data) \
466         tevent_add_signal(ev, mem_ctx, signum, sa_flags, handler, private_data)
467
468 #define event_loop_once(ev) \
469         tevent_loop_once(ev)
470
471 #define event_loop_wait(ev) \
472         tevent_loop_wait(ev)
473
474 #define event_get_fd_flags(fde) \
475         tevent_fd_get_flags(fde)
476
477 #define event_set_fd_flags(fde, flags) \
478         tevent_fd_set_flags(fde, flags)
479
480 #define EVENT_FD_READ           TEVENT_FD_READ
481 #define EVENT_FD_WRITE          TEVENT_FD_WRITE
482
483 #define EVENT_FD_WRITEABLE(fde) \
484         TEVENT_FD_WRITEABLE(fde)
485
486 #define EVENT_FD_READABLE(fde) \
487         TEVENT_FD_READABLE(fde)
488
489 #define EVENT_FD_NOT_WRITEABLE(fde) \
490         TEVENT_FD_NOT_WRITEABLE(fde)
491
492 #define EVENT_FD_NOT_READABLE(fde) \
493         TEVENT_FD_NOT_READABLE(fde)
494
495 #define ev_debug_level          tevent_debug_level
496
497 #define EV_DEBUG_FATAL          TEVENT_DEBUG_FATAL
498 #define EV_DEBUG_ERROR          TEVENT_DEBUG_ERROR
499 #define EV_DEBUG_WARNING        TEVENT_DEBUG_WARNING
500 #define EV_DEBUG_TRACE          TEVENT_DEBUG_TRACE
501
502 #define ev_set_debug(ev, debug, context) \
503         tevent_set_debug(ev, debug, context)
504
505 #define ev_set_debug_stderr(_ev) tevent_set_debug_stderr(ev)
506
507 #endif /* TEVENT_COMPAT_DEFINES */
508
509 #endif /* __TEVENT_H__ */