Expose functions need by backend writers
[ira/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 Internal state of the request
69          *
70          * Callers should only access this via functions and never directly.
71          */
72         struct {
73                 /**
74                  * @brief The talloc type of the data pointer
75                  *
76                  * This is filled by the tevent_req_create() macro.
77                  *
78                  * This for debugging only.
79                  */
80                 const char *private_type;
81
82                 /**
83                  * @brief The location where the request was created
84                  *
85                  * This uses the __location__ macro via the tevent_req_create()
86                  * macro.
87                  *
88                  * This for debugging only.
89                  */
90                 const char *create_location;
91
92                 /**
93                  * @brief The location where the request was finished
94                  *
95                  * This uses the __location__ macro via the tevent_req_done(),
96                  * tevent_req_error() or tevent_req_nomem() macro.
97                  *
98                  * This for debugging only.
99                  */
100                 const char *finish_location;
101
102                 /**
103                  * @brief The external state - will be queried by the caller
104                  *
105                  * While the async request is being processed, state will remain in
106                  * TEVENT_REQ_IN_PROGRESS. A request is finished if
107                  * req->state>=TEVENT_REQ_DONE.
108                  */
109                 enum tevent_req_state state;
110
111                 /**
112                  * @brief status code when finished
113                  *
114                  * This status can be queried in the async completion function. It
115                  * will be set to 0 when everything went fine.
116                  */
117                 uint64_t error;
118
119                 /**
120                  * @brief the immediate event used by tevent_req_post
121                  *
122                  */
123                 struct tevent_immediate *trigger;
124
125                 /**
126                  * @brief the timer event if tevent_req_set_timeout was used
127                  *
128                  */
129                 struct tevent_timer *timer;
130         } internal;
131 };
132
133 struct tevent_fd {
134         struct tevent_fd *prev, *next;
135         struct tevent_context *event_ctx;
136         int fd;
137         uint16_t flags; /* see TEVENT_FD_* flags */
138         tevent_fd_handler_t handler;
139         tevent_fd_close_fn_t close_fn;
140         /* this is private for the specific handler */
141         void *private_data;
142         /* this is for debugging only! */
143         const char *handler_name;
144         const char *location;
145         /* this is private for the events_ops implementation */
146         uint16_t additional_flags;
147         void *additional_data;
148 };
149
150 struct tevent_timer {
151         struct tevent_timer *prev, *next;
152         struct tevent_context *event_ctx;
153         struct timeval next_event;
154         tevent_timer_handler_t handler;
155         /* this is private for the specific handler */
156         void *private_data;
157         /* this is for debugging only! */
158         const char *handler_name;
159         const char *location;
160         /* this is private for the events_ops implementation */
161         void *additional_data;
162 };
163
164 struct tevent_immediate {
165         struct tevent_immediate *prev, *next;
166         struct tevent_context *event_ctx;
167         tevent_immediate_handler_t handler;
168         /* this is private for the specific handler */
169         void *private_data;
170         /* this is for debugging only! */
171         const char *handler_name;
172         const char *create_location;
173         const char *schedule_location;
174         /* this is private for the events_ops implementation */
175         void (*cancel_fn)(struct tevent_immediate *im);
176         void *additional_data;
177 };
178
179 struct tevent_signal {
180         struct tevent_signal *prev, *next;
181         struct tevent_context *event_ctx;
182         int signum;
183         int sa_flags;
184         tevent_signal_handler_t handler;
185         /* this is private for the specific handler */
186         void *private_data;
187         /* this is for debugging only! */
188         const char *handler_name;
189         const char *location;
190         /* this is private for the events_ops implementation */
191         void *additional_data;
192 };
193
194 struct tevent_debug_ops {
195         void (*debug)(void *context, enum tevent_debug_level level,
196                       const char *fmt, va_list ap) PRINTF_ATTRIBUTE(3,0);
197         void *context;
198 };
199
200 void tevent_debug(struct tevent_context *ev, enum tevent_debug_level level,
201                   const char *fmt, ...) PRINTF_ATTRIBUTE(3,4);
202
203 struct tevent_context {
204         /* the specific events implementation */
205         const struct tevent_ops *ops;
206
207         /* list of fd events - used by common code */
208         struct tevent_fd *fd_events;
209
210         /* list of timed events - used by common code */
211         struct tevent_timer *timer_events;
212
213         /* list of immediate events - used by common code */
214         struct tevent_immediate *immediate_events;
215
216         /* list of signal events - used by common code */
217         struct tevent_signal *signal_events;
218
219         /* this is private for the events_ops implementation */
220         void *additional_data;
221
222         /* pipe hack used with signal handlers */
223         struct tevent_fd *pipe_fde;
224
225         /* debugging operations */
226         struct tevent_debug_ops debug_ops;
227
228         /* info about the nesting status */
229         struct {
230                 bool allowed;
231                 uint32_t level;
232                 tevent_nesting_hook hook_fn;
233                 void *hook_private;
234         } nesting;
235 };
236
237
238 int tevent_common_context_destructor(struct tevent_context *ev);
239 int tevent_common_loop_wait(struct tevent_context *ev,
240                             const char *location);
241
242 int tevent_common_fd_destructor(struct tevent_fd *fde);
243 struct tevent_fd *tevent_common_add_fd(struct tevent_context *ev,
244                                        TALLOC_CTX *mem_ctx,
245                                        int fd,
246                                        uint16_t flags,
247                                        tevent_fd_handler_t handler,
248                                        void *private_data,
249                                        const char *handler_name,
250                                        const char *location);
251 void tevent_common_fd_set_close_fn(struct tevent_fd *fde,
252                                    tevent_fd_close_fn_t close_fn);
253 uint16_t tevent_common_fd_get_flags(struct tevent_fd *fde);
254 void tevent_common_fd_set_flags(struct tevent_fd *fde, uint16_t flags);
255
256 struct tevent_timer *tevent_common_add_timer(struct tevent_context *ev,
257                                              TALLOC_CTX *mem_ctx,
258                                              struct timeval next_event,
259                                              tevent_timer_handler_t handler,
260                                              void *private_data,
261                                              const char *handler_name,
262                                              const char *location);
263 struct timeval tevent_common_loop_timer_delay(struct tevent_context *);
264
265 void tevent_common_schedule_immediate(struct tevent_immediate *im,
266                                       struct tevent_context *ev,
267                                       tevent_immediate_handler_t handler,
268                                       void *private_data,
269                                       const char *handler_name,
270                                       const char *location);
271 bool tevent_common_loop_immediate(struct tevent_context *ev);
272
273 struct tevent_signal *tevent_common_add_signal(struct tevent_context *ev,
274                                                TALLOC_CTX *mem_ctx,
275                                                int signum,
276                                                int sa_flags,
277                                                tevent_signal_handler_t handler,
278                                                void *private_data,
279                                                const char *handler_name,
280                                                const char *location);
281 int tevent_common_check_signal(struct tevent_context *ev);
282
283 bool tevent_standard_init(void);
284 bool tevent_select_init(void);
285 #ifdef HAVE_EPOLL
286 bool tevent_epoll_init(void);
287 #endif