* Fix a bug in runcmd() - the argv[] list was not NULL terminated.
[dragonfly.git] / contrib / libevent / event.h
blob4c39939cc501951d3fad6c80fb8b71107b1b53b6
1 /*
2 * Copyright (c) 2000-2004 Niels Provos <provos@citi.umich.edu>
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #ifndef _EVENT_H_
28 #define _EVENT_H_
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
34 #include <sys/time.h>
35 #include <stdint.h>
36 #include <stdarg.h>
38 #ifdef WIN32
39 #define WIN32_LEAN_AND_MEAN
40 #include <windows.h>
41 #undef WIN32_LEAN_AND_MEAN
42 typedef unsigned char u_char;
43 typedef unsigned short u_short;
44 #endif
46 #define EVLIST_TIMEOUT 0x01
47 #define EVLIST_INSERTED 0x02
48 #define EVLIST_SIGNAL 0x04
49 #define EVLIST_ACTIVE 0x08
50 #define EVLIST_INTERNAL 0x10
51 #define EVLIST_INIT 0x80
53 /* EVLIST_X_ Private space: 0x1000-0xf000 */
54 #define EVLIST_ALL (0xf000 | 0x9f)
56 #define EV_TIMEOUT 0x01
57 #define EV_READ 0x02
58 #define EV_WRITE 0x04
59 #define EV_SIGNAL 0x08
60 #define EV_PERSIST 0x10 /* Persistant event */
62 /* Fix so that ppl dont have to run with <sys/queue.h> */
63 #ifndef TAILQ_ENTRY
64 #define _EVENT_DEFINED_TQENTRY
65 #define TAILQ_ENTRY(type) \
66 struct { \
67 struct type *tqe_next; /* next element */ \
68 struct type **tqe_prev; /* address of previous next element */ \
70 #endif /* !TAILQ_ENTRY */
71 #ifndef RB_ENTRY
72 #define _EVENT_DEFINED_RBENTRY
73 #define RB_ENTRY(type) \
74 struct { \
75 struct type *rbe_left; /* left element */ \
76 struct type *rbe_right; /* right element */ \
77 struct type *rbe_parent; /* parent element */ \
78 int rbe_color; /* node color */ \
80 #endif /* !RB_ENTRY */
82 struct event_base;
83 struct event {
84 TAILQ_ENTRY (event) ev_next;
85 TAILQ_ENTRY (event) ev_active_next;
86 TAILQ_ENTRY (event) ev_signal_next;
87 RB_ENTRY (event) ev_timeout_node;
89 struct event_base *ev_base;
90 int ev_fd;
91 short ev_events;
92 short ev_ncalls;
93 short *ev_pncalls; /* Allows deletes in callback */
95 struct timeval ev_timeout;
97 int ev_pri; /* smaller numbers are higher priority */
99 void (*ev_callback)(int, short, void *arg);
100 void *ev_arg;
102 int ev_res; /* result passed to event callback */
103 int ev_flags;
106 #define EVENT_SIGNAL(ev) (int)(ev)->ev_fd
107 #define EVENT_FD(ev) (int)(ev)->ev_fd
110 * Key-Value pairs. Can be used for HTTP headers but also for
111 * query argument parsing.
113 struct evkeyval {
114 TAILQ_ENTRY(evkeyval) next;
116 char *key;
117 char *value;
120 #ifdef _EVENT_DEFINED_TQENTRY
121 #undef TAILQ_ENTRY
122 struct event_list;
123 struct evkeyvalq;
124 #undef _EVENT_DEFINED_TQENTRY
125 #else
126 TAILQ_HEAD (event_list, event);
127 TAILQ_HEAD (evkeyvalq, evkeyval);
128 #endif /* _EVENT_DEFINED_TQENTRY */
129 #ifdef _EVENT_DEFINED_RBENTRY
130 #undef RB_ENTRY
131 #undef _EVENT_DEFINED_RBENTRY
132 #endif /* _EVENT_DEFINED_RBENTRY */
134 struct eventop {
135 char *name;
136 void *(*init)(struct event_base *);
137 int (*add)(void *, struct event *);
138 int (*del)(void *, struct event *);
139 int (*recalc)(struct event_base *, void *, int);
140 int (*dispatch)(struct event_base *, void *, struct timeval *);
141 void (*dealloc)(struct event_base *, void *);
144 void *event_init(void);
145 int event_dispatch(void);
146 int event_base_dispatch(struct event_base *);
147 void event_base_free(struct event_base *);
149 #define _EVENT_LOG_DEBUG 0
150 #define _EVENT_LOG_MSG 1
151 #define _EVENT_LOG_WARN 2
152 #define _EVENT_LOG_ERR 3
153 typedef void (*event_log_cb)(int severity, const char *msg);
154 void event_set_log_callback(event_log_cb cb);
156 /* Associate a different event base with an event */
157 int event_base_set(struct event_base *, struct event *);
159 #define EVLOOP_ONCE 0x01
160 #define EVLOOP_NONBLOCK 0x02
161 int event_loop(int);
162 int event_base_loop(struct event_base *, int);
163 int event_loopexit(struct timeval *); /* Causes the loop to exit */
164 int event_base_loopexit(struct event_base *, struct timeval *);
166 #define evtimer_add(ev, tv) event_add(ev, tv)
167 #define evtimer_set(ev, cb, arg) event_set(ev, -1, 0, cb, arg)
168 #define evtimer_del(ev) event_del(ev)
169 #define evtimer_pending(ev, tv) event_pending(ev, EV_TIMEOUT, tv)
170 #define evtimer_initialized(ev) ((ev)->ev_flags & EVLIST_INIT)
172 #define timeout_add(ev, tv) event_add(ev, tv)
173 #define timeout_set(ev, cb, arg) event_set(ev, -1, 0, cb, arg)
174 #define timeout_del(ev) event_del(ev)
175 #define timeout_pending(ev, tv) event_pending(ev, EV_TIMEOUT, tv)
176 #define timeout_initialized(ev) ((ev)->ev_flags & EVLIST_INIT)
178 #define signal_add(ev, tv) event_add(ev, tv)
179 #define signal_set(ev, x, cb, arg) \
180 event_set(ev, x, EV_SIGNAL|EV_PERSIST, cb, arg)
181 #define signal_del(ev) event_del(ev)
182 #define signal_pending(ev, tv) event_pending(ev, EV_SIGNAL, tv)
183 #define signal_initialized(ev) ((ev)->ev_flags & EVLIST_INIT)
185 void event_set(struct event *, int, short, void (*)(int, short, void *), void *);
186 int event_once(int, short, void (*)(int, short, void *), void *, struct timeval *);
187 int event_base_once(struct event_base *, int, short, void (*)(int, short, void *), void *, struct timeval *);
189 int event_add(struct event *, struct timeval *);
190 int event_del(struct event *);
191 void event_active(struct event *, int, short);
193 int event_pending(struct event *, short, struct timeval *);
195 #ifdef WIN32
196 #define event_initialized(ev) ((ev)->ev_flags & EVLIST_INIT && (ev)->ev_fd != (int)INVALID_HANDLE_VALUE)
197 #else
198 #define event_initialized(ev) ((ev)->ev_flags & EVLIST_INIT)
199 #endif
201 /* Some simple debugging functions */
202 const char *event_get_version(void);
203 const char *event_get_method(void);
205 /* These functions deal with event priorities */
207 int event_priority_init(int);
208 int event_base_priority_init(struct event_base *, int);
209 int event_priority_set(struct event *, int);
211 /* These functions deal with buffering input and output */
213 struct evbuffer {
214 u_char *buffer;
215 u_char *orig_buffer;
217 size_t misalign;
218 size_t totallen;
219 size_t off;
221 void (*cb)(struct evbuffer *, size_t, size_t, void *);
222 void *cbarg;
225 /* Just for error reporting - use other constants otherwise */
226 #define EVBUFFER_READ 0x01
227 #define EVBUFFER_WRITE 0x02
228 #define EVBUFFER_EOF 0x10
229 #define EVBUFFER_ERROR 0x20
230 #define EVBUFFER_TIMEOUT 0x40
232 struct bufferevent;
233 typedef void (*evbuffercb)(struct bufferevent *, void *);
234 typedef void (*everrorcb)(struct bufferevent *, short what, void *);
236 struct event_watermark {
237 size_t low;
238 size_t high;
241 struct bufferevent {
242 struct event ev_read;
243 struct event ev_write;
245 struct evbuffer *input;
246 struct evbuffer *output;
248 struct event_watermark wm_read;
249 struct event_watermark wm_write;
251 evbuffercb readcb;
252 evbuffercb writecb;
253 everrorcb errorcb;
254 void *cbarg;
256 int timeout_read; /* in seconds */
257 int timeout_write; /* in seconds */
259 short enabled; /* events that are currently enabled */
262 struct bufferevent *bufferevent_new(int fd,
263 evbuffercb readcb, evbuffercb writecb, everrorcb errorcb, void *cbarg);
264 int bufferevent_base_set(struct event_base *base, struct bufferevent *bufev);
265 int bufferevent_priority_set(struct bufferevent *bufev, int pri);
266 void bufferevent_free(struct bufferevent *bufev);
267 int bufferevent_write(struct bufferevent *bufev, void *data, size_t size);
268 int bufferevent_write_buffer(struct bufferevent *bufev, struct evbuffer *buf);
269 size_t bufferevent_read(struct bufferevent *bufev, void *data, size_t size);
270 int bufferevent_enable(struct bufferevent *bufev, short event);
271 int bufferevent_disable(struct bufferevent *bufev, short event);
272 void bufferevent_settimeout(struct bufferevent *bufev,
273 int timeout_read, int timeout_write);
275 #define EVBUFFER_LENGTH(x) (x)->off
276 #define EVBUFFER_DATA(x) (x)->buffer
277 #define EVBUFFER_INPUT(x) (x)->input
278 #define EVBUFFER_OUTPUT(x) (x)->output
280 struct evbuffer *evbuffer_new(void);
281 void evbuffer_free(struct evbuffer *);
282 int evbuffer_expand(struct evbuffer *, size_t);
283 int evbuffer_add(struct evbuffer *, const void *, size_t);
284 int evbuffer_remove(struct evbuffer *, void *, size_t);
285 char *evbuffer_readline(struct evbuffer *);
286 int evbuffer_add_buffer(struct evbuffer *, struct evbuffer *);
287 int evbuffer_add_printf(struct evbuffer *, const char *fmt, ...);
288 int evbuffer_add_vprintf(struct evbuffer *, const char *fmt, va_list ap);
289 void evbuffer_drain(struct evbuffer *, size_t);
290 int evbuffer_write(struct evbuffer *, int);
291 int evbuffer_read(struct evbuffer *, int, int);
292 u_char *evbuffer_find(struct evbuffer *, const u_char *, size_t);
293 void evbuffer_setcb(struct evbuffer *, void (*)(struct evbuffer *, size_t, size_t, void *), void *);
296 * Marshaling tagged data - We assume that all tags are inserted in their
297 * numeric order - so that unknown tags will always be higher than the
298 * known ones - and we can just ignore the end of an event buffer.
301 void evtag_init(void);
303 void evtag_marshal(struct evbuffer *evbuf, uint8_t tag, const void *data,
304 uint32_t len);
306 void encode_int(struct evbuffer *evbuf, uint32_t number);
308 void evtag_marshal_int(struct evbuffer *evbuf, uint8_t tag, uint32_t integer);
310 void evtag_marshal_string(struct evbuffer *buf, uint8_t tag,
311 const char *string);
313 void evtag_marshal_timeval(struct evbuffer *evbuf, uint8_t tag,
314 struct timeval *tv);
316 void evtag_test(void);
318 int evtag_unmarshal(struct evbuffer *src, uint8_t *ptag, struct evbuffer *dst);
319 int evtag_peek(struct evbuffer *evbuf, uint8_t *ptag);
320 int evtag_peek_length(struct evbuffer *evbuf, uint32_t *plength);
321 int evtag_payload_length(struct evbuffer *evbuf, uint32_t *plength);
322 int evtag_consume(struct evbuffer *evbuf);
324 int evtag_unmarshal_int(struct evbuffer *evbuf, uint8_t need_tag,
325 uint32_t *pinteger);
327 int evtag_unmarshal_fixed(struct evbuffer *src, uint8_t need_tag, void *data,
328 size_t len);
330 int evtag_unmarshal_string(struct evbuffer *evbuf, uint8_t need_tag,
331 char **pstring);
333 int evtag_unmarshal_timeval(struct evbuffer *evbuf, uint8_t need_tag,
334 struct timeval *ptv);
336 #ifdef __cplusplus
338 #endif
340 #endif /* _EVENT_H_ */