tevent: add tevent_fd_set_close_fn()
[ira/wip.git] / lib / tevent / tevent_internal.h
1 /* 
2    Unix SMB/CIFS implementation.
3
4    generalised event loop handling
5
6    Internal structs
7
8    Copyright (C) Stefan Metzmacher 2005
9    
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 struct tevent_ops {
25         /* conntext init */
26         int (*context_init)(struct tevent_context *ev);
27
28         /* fd_event functions */
29         struct tevent_fd *(*add_fd)(struct tevent_context *ev,
30                                     TALLOC_CTX *mem_ctx,
31                                     int fd, uint16_t flags,
32                                     tevent_fd_handler_t handler,
33                                     void *private_data,
34                                     const char *handler_name,
35                                     const char *location);
36         void (*set_fd_close_fn)(struct tevent_fd *fde,
37                                 tevent_fd_close_fn_t close_fn);
38         uint16_t (*get_fd_flags)(struct tevent_fd *fde);
39         void (*set_fd_flags)(struct tevent_fd *fde, uint16_t flags);
40
41         /* timed_event functions */
42         struct tevent_timer *(*add_timer)(struct tevent_context *ev,
43                                           TALLOC_CTX *mem_ctx,
44                                           struct timeval next_event,
45                                           tevent_timer_handler_t handler,
46                                           void *private_data,
47                                           const char *handler_name,
48                                           const char *location);
49         /* disk aio event functions */
50         struct tevent_aio *(*add_aio)(struct tevent_context *ev,
51                                       TALLOC_CTX *mem_ctx,
52                                       struct iocb *iocb,
53                                       tevent_aio_handler_t handler,
54                                       void *private_data,
55                                       const char *handler_name,
56                                       const char *location);
57         /* signal functions */
58         struct tevent_signal *(*add_signal)(struct tevent_context *ev,
59                                             TALLOC_CTX *mem_ctx,
60                                             int signum, int sa_flags,
61                                             tevent_signal_handler_t handler,
62                                             void *private_data,
63                                             const char *handler_name,
64                                             const char *location);
65
66         /* loop functions */
67         int (*loop_once)(struct tevent_context *ev);
68         int (*loop_wait)(struct tevent_context *ev);
69 };
70
71 struct tevent_fd {
72         struct tevent_fd *prev, *next;
73         struct tevent_context *event_ctx;
74         int fd;
75         uint16_t flags; /* see TEVENT_FD_* flags */
76         tevent_fd_handler_t handler;
77         tevent_fd_close_fn_t close_fn;
78         /* this is private for the specific handler */
79         void *private_data;
80         /* this is for debugging only! */
81         const char *handler_name;
82         const char *location;
83         /* this is private for the events_ops implementation */
84         uint16_t additional_flags;
85         void *additional_data;
86 };
87
88 struct tevent_timer {
89         struct tevent_timer *prev, *next;
90         struct tevent_context *event_ctx;
91         struct timeval next_event;
92         tevent_timer_handler_t handler;
93         /* this is private for the specific handler */
94         void *private_data;
95         /* this is for debugging only! */
96         const char *handler_name;
97         const char *location;
98         /* this is private for the events_ops implementation */
99         void *additional_data;
100 };
101
102 struct tevent_signal {
103         struct tevent_signal *prev, *next;
104         struct tevent_context *event_ctx;
105         int signum;
106         int sa_flags;
107         tevent_signal_handler_t handler;
108         /* this is private for the specific handler */
109         void *private_data;
110         /* this is for debugging only! */
111         const char *handler_name;
112         const char *location;
113         /* this is private for the events_ops implementation */
114         void *additional_data;
115 };
116
117 struct tevent_debug_ops {
118         void (*debug)(void *context, enum tevent_debug_level level,
119                       const char *fmt, va_list ap) PRINTF_ATTRIBUTE(3,0);
120         void *context;
121 };
122
123 void tevent_debug(struct tevent_context *ev, enum tevent_debug_level level,
124                   const char *fmt, ...) PRINTF_ATTRIBUTE(3,4);
125
126 /* aio event is private to the aio backend */
127 struct tevent_aio;
128
129 struct tevent_context {
130         /* the specific events implementation */
131         const struct tevent_ops *ops;
132
133         /* list of timed events - used by common code */
134         struct tevent_timer *timer_events;
135
136         /* this is private for the events_ops implementation */
137         void *additional_data;
138
139         /* number of signal event handlers */
140         int num_signal_handlers;
141
142         /* pipe hack used with signal handlers */
143         struct tevent_fd *pipe_fde;
144
145         /* debugging operations */
146         struct tevent_debug_ops debug_ops;
147 };
148
149
150 bool tevent_register_backend(const char *name, const struct tevent_ops *ops);
151
152 void tevent_common_fd_set_close_fn(struct tevent_fd *fde,
153                                    tevent_fd_close_fn_t close_fn);
154 uint16_t tevent_common_fd_get_flags(struct tevent_fd *fde);
155 void tevent_common_fd_set_flags(struct tevent_fd *fde, uint16_t flags);
156
157 bool ev_timeval_is_zero(const struct timeval *tv);
158 struct tevent_timer *tevent_common_add_timer(struct tevent_context *ev,
159                                              TALLOC_CTX *mem_ctx,
160                                              struct timeval next_event,
161                                              tevent_timer_handler_t handler,
162                                              void *private_data,
163                                              const char *handler_name,
164                                              const char *location);
165 struct timeval tevent_common_loop_timer_delay(struct tevent_context *);
166
167 struct tevent_signal *tevent_common_add_signal(struct tevent_context *ev,
168                                                TALLOC_CTX *mem_ctx,
169                                                int signum,
170                                                int sa_flags,
171                                                tevent_signal_handler_t handler,
172                                                void *private_data,
173                                                const char *handler_name,
174                                                const char *location);
175 int tevent_common_check_signal(struct tevent_context *ev);
176
177 bool tevent_standard_init(void);
178 bool tevent_select_init(void);
179 #ifdef HAVE_EPOLL
180 bool tevent_epoll_init(void);
181 #endif
182 #ifdef HAVE_LINUX_AIO
183 bool tevent_aio_init(void);
184 #endif