1EVENT(3)                 BSD Library Functions Manual                 EVENT(3)
2

NAME

4     event_init, event_dispatch, event_loop, event_loopexit, event_set,
5     event_base_dispatch, event_base_loop, event_base_loopexit,
6     event_base_set, event_add, event_del, event_once, event_pending,
7     event_initialized, event_priority_init, event_priority_set, evtimer_set,
8     evtimer_add, evtimer_del, evtimer_pending, evtimer_initialized,
9     signal_set, signal_add, signal_del, signal_pending, signal_initialized,
10     bufferevent_new, bufferevent_free, bufferevent_write,
11     bufferevent_write_buffer, bufferevent_read, bufferevent_enable,
12     bufferevent_disable, bufferevent_settimeout, bufferevent_base_set,
13     evbuffer_new, evbuffer_free, evbuffer_add, evbuffer_add_buffer,
14     evbuffer_add_printf, evbuffer_add_vprintf, evbuffer_drain,
15     evbuffer_write, evbuffer_read, evbuffer_find, evbuffer_readline,
16     evhttp_start, evhttp_free — execute a function when a specific event
17     occurs
18

SYNOPSIS

20     #include <sys/time.h>
21     #include <event.h>
22
23     struct event_base *
24     event_init();
25
26     int
27     event_dispatch();
28
29     int
30     event_loop(int flags);
31
32     int
33     event_loopexit(struct timeval *tv);
34
35     void
36     event_set(struct event *ev, int fd, short event,
37         void (*fn)(int, short, void *), void *arg);
38
39     int
40     event_base_dispatch(struct event_base *base);
41
42     int
43     event_base_loop(struct event_base *base, int flags);
44
45     int
46     event_base_loopexit(struct event_base *base, struct timeval *tv);
47
48     int
49     event_base_set(struct event_base *base, struct event *);
50
51     int
52     event_add(struct event *ev, struct timeval *tv);
53
54     int
55     event_del(struct event *ev);
56
57     int
58     event_once(int fd, short event, void (*fn)(int, short, void *),
59         void *arg, struct timeval *tv);
60
61     int
62     event_pending(struct event *ev, short event, struct timeval *tv);
63
64     int
65     event_initialized(struct event *ev);
66
67     int
68     event_priority_init(int npriorities);
69
70     int
71     event_priority_set(struct event *ev, int priority);
72
73     void
74     evtimer_set(struct event *ev, void (*fn)(int, short, void *), void *arg);
75
76     void
77     evtimer_add(struct event *ev, struct timeval *);
78
79     void
80     evtimer_del(struct event *ev);
81
82     int
83     evtimer_pending(struct event *ev, struct timeval *tv);
84
85     int
86     evtimer_initialized(struct event *ev);
87
88     void
89     signal_set(struct event *ev, int signal, void (*fn)(int, short, void *),
90         void *arg);
91
92     void
93     signal_add(struct event *ev, struct timeval *);
94
95     void
96     signal_del(struct event *ev);
97
98     int
99     signal_pending(struct event *ev, struct timeval *tv);
100
101     int
102     signal_initialized(struct event *ev);
103
104     struct bufferevent *
105     bufferevent_new(int fd, evbuffercb readcb, evbuffercb writecb, everrorcb,
106         void *cbarg);
107
108     void
109     bufferevent_free(struct bufferevent *bufev);
110
111     int
112     bufferevent_write(struct bufferevent *bufev, void *data, size_t size);
113
114     int
115     bufferevent_write_buffer(struct bufferevent *bufev,
116         struct evbuffer *buf);
117
118     size_t
119     bufferevent_read(struct bufferevent *bufev, void *data, size_t size);
120
121     int
122     bufferevent_enable(struct bufferevent *bufev, short event);
123
124     int
125     bufferevent_disable(struct bufferevent *bufev, short event);
126
127     void
128     bufferevent_settimeout(struct bufferevent *bufev, int timeout_read,
129         int timeout_write);
130
131     int
132     bufferevent_base_set(struct event_base *base, struct bufferevent *bufev);
133
134     struct evbuffer *
135     evbuffer_new(void);
136
137     void
138     evbuffer_free(struct evbuffer *buf);
139
140     int
141     evbuffer_add(struct evbuffer *buf, u_char *data, size_t size);
142
143     int
144     evbuffer_add_buffer(struct evbuffer *dst, struct evbuffer *src);
145
146     int
147     evbuffer_add_printf(struct evbuffer *buf, char *fmt, ...);
148
149     int
150     evbuffer_add_vprintf(struct evbuffer *buf, const char *fmt, va_list ap);
151
152     void
153     evbuffer_drain(struct evbuffer *buf, size_t size);
154
155     int
156     evbuffer_write(struct evbuffer *buf, int fd);
157
158     int
159     evbuffer_read(struct evbuffer *buf, int fd, int size);
160
161     u_char *
162     evbuffer_find(struct evbuffer *buf, u_char *data, size_t size);
163
164     char *
165     evbuffer_readline(struct evbuffer *buf);
166
167     struct evhttp *
168     evhttp_start(const char *address, u_short port);
169
170     void
171     evhttp_free(struct evhttp* http);
172
173     int (*event_sigcb)(void);
174
175     int event_gotsig;
176

DESCRIPTION

178     The event API provides a mechanism to execute a function when a specific
179     event on a file descriptor occurs or after a given time has passed.
180
181     The event API needs to be initialized with event_init() before it can be
182     used.
183
184     In order to process events, an application needs to call
185     event_dispatch().  This function only returns on error, and should
186     replace the event core of the application program.
187
188     In order to avoid races in signal handlers, the event API provides two
189     variables: event_sigcb and event_gotsig.  A signal handler sets
190     event_gotsig to indicate that a signal has been received.  The applica‐
191     tion sets event_sigcb to a callback function.  After the signal handler
192     sets event_gotsig, event_dispatch will execute the callback function to
193     process received signals.  The callback returns 1 when no events are reg‐
194     istered any more.  It can return -1 to indicate an error to the event
195     library, causing event_dispatch() to terminate with errno set to EINTR.
196
197     The event_loop function provides an interface for single pass execution
198     of pending events.  The flags EVLOOP_ONCE and EVLOOP_NONBLOCK are recog‐
199     nized.  The event_loopexit function allows the loop to be terminated
200     after some amount of time has passed.  The parameter indicates the time
201     after which the loop should terminate.
202
203     It is the responsibility of the caller to provide these functions with
204     pre-allocated event structures.
205
206     The function event_set() prepares the event structure ev to be used in
207     future calls to event_add() and event_del().  The event will be prepared
208     to call the function specified by the fn argument with an int argument
209     indicating the file descriptor, a short argument indicating the type of
210     event, and a void * argument given in the arg argument.  The fd indicates
211     the file descriptor that should be monitored for events.  The events can
212     be either EV_READ, EV_WRITE, or both, indicating that an application can
213     read or write from the file descriptor respectively without blocking.
214
215     The function fn will be called with the file descriptor that triggered
216     the event and the type of event which will be either EV_TIMEOUT,
217     EV_SIGNAL, EV_READ, or EV_WRITE.  The additional flag EV_PERSIST makes an
218     event_add() persistent until event_del() has been called.
219
220     Once initialized, the ev structure can be used repeatedly with
221     event_add() and event_del() and does not need to be reinitialized unless
222     the function called and/or the argument to it are to be changed.  How‐
223     ever, when an ev structure has been added to libevent using event_add()
224     the structure must persist until the event occurs (assuming EV_PERSIST is
225     not set) or is removed using event_del().  You may not reuse the same ev
226     structure for multiple monitored descriptors; each descriptor needs its
227     own ev.
228
229     The function event_add() schedules the execution of the ev event when the
230     event specified in event_set() occurs or in at least the time specified
231     in the tv.  If tv is NULL, no timeout occurs and the function will only
232     be called if a matching event occurs on the file descriptor.  The event
233     in the ev argument must be already initialized by event_set() and may not
234     be used in calls to event_set() until it has timed out or been removed
235     with event_del().  If the event in the ev argument already has a sched‐
236     uled timeout, the old timeout will be replaced by the new one.
237
238     The function event_del() will cancel the event in the argument ev.  If
239     the event has already executed or has never been added the call will have
240     no effect.
241
242     The function event_once() is similar to event_set().  However, it sched‐
243     ules a callback to be called exactly once and does not require the caller
244     to prepare an event structure.  This function supports EV_TIMEOUT,
245     EV_READ, and EV_WRITE.
246
247     The event_pending() function can be used to check if the event specified
248     by event is pending to run.  If EV_TIMEOUT was specified and tv is not
249     NULL, the expiration time of the event will be returned in tv.
250
251     The event_initialized() macro can be used to check if an event has been
252     initialized.
253
254     The functions evtimer_set(), evtimer_add(), evtimer_del(),
255     evtimer_initialized(), and evtimer_pending() are abbreviations for common
256     situations where only a timeout is required.  The file descriptor passed
257     will be -1, and the event type will be EV_TIMEOUT.
258
259     The functions signal_set(), signal_add(), signal_del(),
260     signal_initialized(), and signal_pending() are abbreviations.  The event
261     type will be a persistent EV_SIGNAL.  That means signal_set() adds
262     EV_PERSIST.
263
264     It is possible to disable support for epoll, kqueue, devpoll, poll or
265     select by setting the environment variable EVENT_NOEPOLL, EVENT_NOKQUEUE,
266     EVENT_NODEVPOLL, EVENT_NOPOLL or EVENT_NOSELECT, respectively.  By set‐
267     ting the environment variable EVENT_SHOW_METHOD, libevent displays the
268     kernel notification method that it uses.
269

EVENT PRIORITIES

271     By default libevent schedules all active events with the same priority.
272     However, sometimes it is desirable to process some events with a higher
273     priority than others.  For that reason, libevent supports strict priority
274     queues.  Active events with a lower priority are always processed before
275     events with a higher priority.
276
277     The number of different priorities can be set initially with the
278     event_priority_init() function.  This function should be called before
279     the first call to event_dispatch().  The event_priority_set() function
280     can be used to assign a priority to an event.  By default, libevent
281     assigns the middle priority to all events unless their priority is
282     explicitly set.
283

THREAD SAFE EVENTS

285     Libevent has experimental support for thread-safe events.  When initial‐
286     izing the library via event_init(), an event base is returned.  This
287     event base can be used in conjunction with calls to event_base_set(),
288     event_base_dispatch(), event_base_loop(), event_base_loopexit(), and
289     bufferevent_base_set().  event_base_set() should be called after prepar‐
290     ing an event with event_set(), as event_set() assigns the provided event
291     to the most recently created event base.  bufferevent_base_set() should
292     be called after preparing a bufferevent with bufferevent_new().
293

BUFFERED EVENTS

295     libevent provides an abstraction on top of the regular event callbacks.
296     This abstraction is called a buffered event.  A buffered event provides
297     input and output buffers that get filled and drained automatically.  The
298     user of a buffered event no longer deals directly with the IO, but
299     instead is reading from input and writing to output buffers.
300
301     A new bufferevent is created by bufferevent_new().  The parameter fd
302     specifies the file descriptor from which data is read and written to.
303     This file descriptor is not allowed to be a pipe(2).  The next three
304     parameters are callbacks.  The read and write callback have the following
305     form: void (*cb)(struct bufferevent *bufev, void *arg).  The error call‐
306     back has the following form: void (*cb)(struct bufferevent *bufev, short
307     what, void *arg).  The argument is specified by the fourth parameter
308     cbarg.  A bufferevent struct pointer is returned on success, NULL on
309     error.  Both the read and the write callback may be NULL.  The error
310     callback has to be always provided.
311
312     Once initialized, the bufferevent structure can be used repeatedly with
313     bufferevent_enable() and bufferevent_disable().  The flags parameter can
314     be a combination of EV_READ and EV_WRITE.  When read enabled the buffer‐
315     event will try to read from the file descriptor and call the read call‐
316     back. The write callback is executed whenever the output buffer is
317     drained below the write low watermark, which is 0 by default.
318
319     The bufferevent_write() function can be used to write data to the file
320     descriptor.  The data is appended to the output buffer and written to the
321     descriptor automatically as it becomes available for writing.  The
322     bufferevent_read() function is used to read data from the input buffer.
323     Both functions return the amount of data written or read.
324
325     If multiple bases are in use, bufferevent_base_set() must be called
326     before enabling the bufferevent for the first time.
327

NON-BLOCKING HTTP SUPPORT

329     libevent provides a very thin HTTP layer that can be used both to host an
330     HTTP server and also to make HTTP requests.  An HTTP server can be cre‐
331     ated by calling evhttp_start().  When the HTTP server is no longer used,
332     it can be freed via evhttp_free().
333
334     To be notified of HTTP requests, a user needs to register callbacks with
335     the HTTP server.  This can be done by calling evhttp_set_cb().  The sec‐
336     ond argument is the URI for which a callback is being registered.  The
337     corresponding callback will receive an struct evhttp_request object that
338     contains all information about the request.
339
340     This section does not document all the possible function calls, please
341     check event.h for the public interfaces.
342

RETURN VALUES

344     Upon successful completion event_add() and event_del() return 0.  Other‐
345     wise, -1 is returned and the global variable errno is set to indicate the
346     error.
347

SEE ALSO

349     kqueue(2), poll(2), select(2), timeout(9)
350

HISTORY

352     The event API manpage is based on the timeout(9) manpage by Artur
353     Grabowski.  The port of libevent to Windows is due to Michael A. Davis.
354     Support for real-time signals is due to Taral.
355

AUTHORS

357     The event library was written by Niels Provos.
358

BUGS

360     This documentation is neither complete nor authoritative.  If you are in
361     doubt about the usage of this API then check the source code to find out
362     how it works, write up the missing piece of documentation and send it to
363     me for inclusion in this man page.
364
365BSD                             August 8, 2000                             BSD
Impressum