1 : #ifndef EVENT_H
2 : #define EVENT_H
3 :
4 : #include "vlg.h"
5 :
6 : #include <sys/types.h>
7 : #include <sys/socket.h>
8 : #include <signal.h>
9 :
10 : #include <timer_q.h>
11 :
12 : #ifdef __linux__
13 : # include <sys/prctl.h>
14 : #endif
15 :
16 : #ifdef PR_SET_PDEATHSIG
17 : # define PROC_CNTL_PDEATHSIG(x1) prctl(PR_SET_PDEATHSIG, x1, 0, 0, 0)
18 : #else
19 : # define PROC_CNTL_PDEATHSIG(x1) (-1)
20 : #endif
21 :
22 : #ifndef EVNT_COMPILE_INLINE
23 : #define EVNT_COMPILE_INLINE 1
24 : #endif
25 :
26 : #ifndef EVNT_CONF_NAGLE
27 : #define EVNT_CONF_NAGLE FALSE /* configurable */
28 : #endif
29 :
30 : #define EVNT_IO_OK 0
31 : #define EVNT_IO_READ_EOF 1
32 : #define EVNT_IO_READ_FIN 2
33 : #define EVNT_IO_READ_ERR 3
34 : #define EVNT_IO_SEND_ERR 4
35 :
36 : struct Evnt;
37 :
38 : struct Evnt_cbs
39 : {
40 : struct Evnt *(*cb_func_accept) (struct Evnt *,
41 : int, struct sockaddr *, socklen_t);
42 : int (*cb_func_connect) (struct Evnt *);
43 : int (*cb_func_recv) (struct Evnt *);
44 : int (*cb_func_send) (struct Evnt *);
45 : void (*cb_func_free) (struct Evnt *);
46 : int (*cb_func_shutdown_r)(struct Evnt *);
47 : };
48 :
49 : struct Evnt_limit
50 : {
51 : unsigned long io_r_max; /* read/accept */
52 : unsigned long io_r_cur;
53 : struct timeval io_r_tm;
54 :
55 : unsigned long io_w_max; /* write/sendfile/connect */
56 : unsigned long io_w_cur;
57 : struct timeval io_w_tm;
58 : };
59 :
60 :
61 : struct Evnt
62 : {
63 : struct Evnt *next;
64 : struct Evnt *prev;
65 :
66 : struct Evnt_cbs cbs[1];
67 :
68 : Vstr_ref **lims; /* array of references to struct Evnt_limit */
69 : unsigned int lim_num;
70 :
71 : unsigned int ind; /* socket poll */
72 :
73 : Vstr_ref *sa_ref;
74 : Vstr_ref *acpt_sa_ref;
75 :
76 : Vstr_base *io_r;
77 : Vstr_base *io_w;
78 :
79 : struct Evnt *s_next;
80 : struct Evnt *c_next;
81 :
82 : Timer_q_node *tm_o; /* timeout */
83 : Timer_q_node *tm_l_r; /* limit read/recv */
84 : Timer_q_node *tm_l_w; /* limit write/send */
85 :
86 : struct timeval ctime;
87 : struct timeval mtime;
88 :
89 : unsigned long msecs_tm_mtime;
90 :
91 : uintmax_t prev_bytes_r;
92 : struct
93 : {
94 : unsigned int req_put;
95 : unsigned int req_got;
96 : uintmax_t bytes_r;
97 : uintmax_t bytes_w;
98 : } acct;
99 :
100 : unsigned int flag_q_accept : 1;
101 : unsigned int flag_q_connect : 1;
102 : unsigned int flag_q_recv : 1;
103 : unsigned int flag_q_send_recv : 1;
104 : unsigned int flag_q_none : 1;
105 :
106 : unsigned int flag_q_send_now : 1;
107 : unsigned int flag_q_closed : 1;
108 :
109 : unsigned int flag_q_pkt_move : 1; /* 8 */
110 :
111 : unsigned int flag_io_nagle : 1;
112 : unsigned int flag_io_cork : 1;
113 :
114 : unsigned int flag_io_filter : 1;
115 :
116 : unsigned int flag_fully_acpt : 1;
117 :
118 : unsigned int flag_insta_close : 1;
119 :
120 : unsigned int io_r_shutdown : 1;
121 : unsigned int io_w_shutdown : 1;
122 :
123 : unsigned int io_r_limited : 1; /* 16 */
124 : unsigned int io_w_limited : 1; /* 17 */
125 : };
126 :
127 : #if ! COMPILE_DEBUG
128 : # define EVNT_SA(x) ((struct sockaddr *)(x)->sa_ref->ptr)
129 : # define EVNT_SA_IN4(x) ((struct sockaddr_in *)(x)->sa_ref->ptr)
130 : # define EVNT_SA_IN6(x) ((struct sockaddr_in6 *)(x)->sa_ref->ptr)
131 : # define EVNT_SA_UN(x) ((struct sockaddr_un *)(x)->sa_ref->ptr)
132 : #else
133 : #include <netinet/in.h>
134 : #include <sys/un.h>
135 :
136 : static struct sockaddr *evnt___chk_sa(void *ptr) COMPILE_ATTR_USED();
137 : static struct sockaddr *evnt___chk_sa(void *ptr)
138 5896193 : {
139 5896193 : struct sockaddr *sa = ptr;
140 5896193 : if (sa && /* vlg__fmt__add_vstr_add_sa accepts NULLs */
141 : (sa->sa_family != AF_INET) &&
142 : (sa->sa_family != AF_INET6) &&
143 : (sa->sa_family != AF_LOCAL))
144 0 : abort();
145 5896193 : return (sa);
146 : }
147 : static struct sockaddr_in *evnt___chk_in(void *ptr) COMPILE_ATTR_USED();
148 : static struct sockaddr_in *evnt___chk_in(void *ptr)
149 1676 : {
150 1676 : struct sockaddr_in *sa = ptr;
151 1676 : if (sa->sin_family != AF_INET)
152 0 : abort();
153 1676 : return (sa);
154 : }
155 : static struct sockaddr_in6 *evnt___chk_in6(void *ptr) COMPILE_ATTR_USED();
156 : static struct sockaddr_in6 *evnt___chk_in6(void *ptr)
157 0 : {
158 0 : struct sockaddr_in6 *sa = ptr;
159 0 : if (sa->sin6_family != AF_INET6)
160 0 : abort();
161 0 : return (sa);
162 : }
163 : static struct sockaddr_un *evnt___chk_un(void *ptr) COMPILE_ATTR_USED();
164 : static struct sockaddr_un *evnt___chk_un(void *ptr)
165 116 : {
166 116 : struct sockaddr_un *sa = ptr;
167 116 : if (sa->sun_family != AF_LOCAL)
168 0 : abort();
169 116 : return (sa);
170 : }
171 :
172 : # define EVNT_SA(x) evnt___chk_sa((x)->sa_ref->ptr)
173 : # define EVNT_SA_IN4(x) evnt___chk_in((x)->sa_ref->ptr)
174 : # define EVNT_SA_IN6(x) evnt___chk_in6((x)->sa_ref->ptr)
175 : # define EVNT_SA_UN(x) evnt___chk_un((x)->sa_ref->ptr)
176 : #endif
177 :
178 : /* FIXME: bad namespace */
179 : typedef struct Acpt_data
180 : {
181 : struct Evnt *evnt;
182 : Vstr_ref *sa;
183 : } Acpt_data;
184 : #if ! COMPILE_DEBUG
185 : # define ACPT_SA(x) ((struct sockaddr *)(x)->sa->ptr)
186 : # define ACPT_SA_IN4(x) ((struct sockaddr_in *)(x)->sa->ptr)
187 : # define ACPT_SA_IN6(x) ((struct sockaddr_in6 *)(x)->sa->ptr)
188 : # define ACPT_SA_UN(x) ((struct sockaddr_un *)(x)->sa->ptr)
189 : #else
190 : # define ACPT_SA(x) evnt___chk_sa((x)->sa->ptr)
191 : # define ACPT_SA_IN4(x) evnt___chk_in((x)->sa->ptr)
192 : # define ACPT_SA_IN6(x) evnt___chk_in6((x)->sa->ptr)
193 : # define ACPT_SA_UN(x) evnt___chk_un((x)->sa->ptr)
194 : #endif
195 :
196 : # define EVNT_ACPT_EXISTS(x) (!!(x)->acpt_sa_ref)
197 : # define EVNT_ACPT_DATA(x) ((Acpt_data *)((x)->acpt_sa_ref->ptr))
198 : # define EVNT_ACPT_SA(x) ACPT_SA(EVNT_ACPT_DATA(x))
199 : # define EVNT_ACPT_SA_IN4(x) ACPT_SA_IN4(EVNT_ACPT_DATA(x))
200 : # define EVNT_ACPT_SA_IN6(x) ACPT_SA_IN6(EVNT_ACPT_DATA(x))
201 : # define EVNT_ACPT_SA_UN(x) ACPT_SA_UN(EVNT_ACPT_DATA(x))
202 :
203 : typedef struct Acpt_listener
204 : {
205 : struct Evnt evnt[1];
206 : unsigned int max_connections;
207 : Vstr_ref *def_policy;
208 : Vstr_ref *ref;
209 : } Acpt_listener;
210 :
211 : extern volatile sig_atomic_t evnt_child_exited;
212 :
213 : extern int evnt_opt_nagle;
214 :
215 : extern void evnt_logger(Vlg *);
216 :
217 : extern void evnt_fd__set_nonblock(int, int);
218 :
219 : extern int evnt_fd(struct Evnt *);
220 :
221 : extern void evnt_wait_cntl_add(struct Evnt *, int);
222 : extern void evnt_wait_cntl_del(struct Evnt *, int);
223 :
224 : extern int evnt_cb_func_connect(struct Evnt *);
225 : extern struct Evnt *evnt_cb_func_accept(struct Evnt *,
226 : int, struct sockaddr *, socklen_t);
227 : extern int evnt_cb_func_recv(struct Evnt *);
228 : extern int evnt_cb_func_send(struct Evnt *);
229 : extern void evnt_cb_func_free(struct Evnt *);
230 : extern void evnt_cb_func_F(struct Evnt *);
231 : extern int evnt_cb_func_shutdown_r(struct Evnt *);
232 :
233 : extern int evnt_limit_add(struct Evnt *, Vstr_ref *);
234 : extern int evnt_limit_dup(struct Evnt *, const struct Evnt_limit *);
235 : extern void evnt_limit_chg(struct Evnt *, unsigned int, Vstr_ref *);
236 : extern void evnt_limit_alt(struct Evnt *, unsigned int,
237 : const struct Evnt_limit *);
238 : extern void evnt_limit_free(struct Evnt *);
239 :
240 : extern int evnt_make_con_ipv4(struct Evnt *, const char *, short);
241 : extern int evnt_make_con_local(struct Evnt *, const char *);
242 : extern int evnt_make_bind_ipv4(struct Evnt *, const char *, short,
243 : unsigned int, const char *);
244 : extern int evnt_make_bind_local(struct Evnt *, const char *, unsigned int);
245 : extern int evnt_make_acpt_ref(struct Evnt *, int, Vstr_ref *);
246 : extern int evnt_make_acpt_dup(struct Evnt *, int, struct sockaddr *, socklen_t);
247 : extern int evnt_make_custom(struct Evnt *, int, Vstr_ref *, int);
248 :
249 : extern void evnt_free(struct Evnt *);
250 : extern void evnt_close(struct Evnt *);
251 : extern void evnt_close_all(void);
252 :
253 : extern void evnt_add(struct Evnt **, struct Evnt *);
254 : extern void evnt_del(struct Evnt **, struct Evnt *);
255 : extern void evnt_put_pkt(struct Evnt *);
256 : extern void evnt_got_pkt(struct Evnt *);
257 : extern int evnt_shutdown_r(struct Evnt *, int);
258 : extern int evnt_shutdown_w(struct Evnt *);
259 : extern int evnt_recv(struct Evnt *, unsigned int *);
260 : extern int evnt_send(struct Evnt *);
261 : extern int evnt_sendfile(struct Evnt *, int,
262 : uintmax_t *, uintmax_t *, unsigned int *);
263 : extern int evnt_sc_read_send(struct Evnt *, int, uintmax_t *);
264 : extern int evnt_send_add(struct Evnt *, int, size_t);
265 : extern void evnt_send_del(struct Evnt *);
266 : extern void evnt_scan_fds(unsigned int, size_t);
267 : extern void evnt_scan_send_fds(void);
268 : extern void evnt_scan_q_close(void);
269 : extern void evnt_acpt_close_all(void);
270 :
271 :
272 : extern void evnt_stats_add(struct Evnt *, const struct Evnt *);
273 :
274 : extern unsigned int evnt_num_all(void);
275 : extern int evnt_waiting(void);
276 :
277 : extern void evnt_fd_set_nagle(struct Evnt *, int);
278 : extern void evnt_fd_set_cork(struct Evnt *, int);
279 : extern void evnt_fd_set_defer_accept(struct Evnt *, int);
280 : extern int evnt_fd_set_filter(struct Evnt *, const char *);
281 :
282 : extern void evnt_timeout_init(void);
283 : extern void evnt_timeout_exit(void);
284 :
285 : extern pid_t evnt_make_child(void);
286 : extern int evnt_is_child(void);
287 : extern int evnt_child_block_beg(void);
288 : extern int evnt_child_block_end(void);
289 :
290 : extern int evnt_sc_timeout_via_mtime(struct Evnt *, unsigned long);
291 :
292 : extern void evnt_sc_main_loop(size_t);
293 :
294 : extern time_t evnt_sc_time(void);
295 :
296 : extern void evnt_sc_serv_cb_func_acpt_free(struct Evnt *);
297 : extern struct Evnt *evnt_sc_serv_make_bind_ipv4(const char *, unsigned short,
298 : unsigned int, unsigned int,
299 : unsigned int, const char *,
300 : const char *);
301 :
302 : extern void evnt_vlg_stats_info(struct Evnt *, const char *);
303 :
304 : extern int evnt_poll_init(void);
305 : extern int evnt_poll_direct_enabled(void);
306 : extern int evnt_poll_child_init(void);
307 :
308 : extern unsigned int evnt_poll_add(struct Evnt *, int);
309 : extern void evnt_poll_del(struct Evnt *);
310 : extern int evnt_poll_swap_accept_read(struct Evnt *, int);
311 : extern int evnt_poll(void);
312 :
313 : extern struct Evnt *evnt_find_least_used(void);
314 :
315 : extern struct Evnt *evnt_queue(const char *);
316 :
317 : extern void evnt_out_dbg3(const char *);
318 :
319 : #endif
|