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

NAME

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

SYNOPSIS

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

DESCRIPTION

196     The event API provides a mechanism to execute a function when a specific
197     event on a file descriptor occurs or after a given time has passed.
198
199     The event API needs to be initialized with event_init() before it can be
200     used.
201
202     In order to process events, an application needs to call
203     event_dispatch().  This function only returns on error, and should
204     replace the event core of the application program.
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.  Additionally, an event which has regis‐
218     tered interest in more than one of the preceeding events, via bitwise-OR
219     to event_set(), can provide its callback function with a bitwise-OR of
220     more than one triggered event.  The additional flag EV_PERSIST makes an
221     event_add() persistent until event_del() has been called.
222
223     Once initialized, the ev structure can be used repeatedly with
224     event_add() and event_del() and does not need to be reinitialized unless
225     the function called and/or the argument to it are to be changed.  How‐
226     ever, when an ev structure has been added to libevent using event_add()
227     the structure must persist until the event occurs (assuming EV_PERSIST is
228     not set) or is removed using event_del().  You may not reuse the same ev
229     structure for multiple monitored descriptors; each descriptor needs its
230     own ev.
231
232     The function event_add() schedules the execution of the ev event when the
233     event specified in event_set() occurs or in at least the time specified
234     in the tv.  If tv is NULL, no timeout occurs and the function will only
235     be called if a matching event occurs on the file descriptor.  The event
236     in the ev argument must be already initialized by event_set() and may not
237     be used in calls to event_set() until it has timed out or been removed
238     with event_del().  If the event in the ev argument already has a sched‐
239     uled timeout, the old timeout will be replaced by the new one.
240
241     The function event_del() will cancel the event in the argument ev.  If
242     the event has already executed or has never been added the call will have
243     no effect.
244
245     The functions evtimer_set(), evtimer_add(), evtimer_del(),
246     evtimer_initialized(), and evtimer_pending() are abbreviations for common
247     situations where only a timeout is required.  The file descriptor passed
248     will be -1, and the event type will be EV_TIMEOUT.
249
250     The functions signal_set(), signal_add(), signal_del(),
251     signal_initialized(), and signal_pending() are abbreviations.  The event
252     type will be a persistent EV_SIGNAL.  That means signal_set() adds
253     EV_PERSIST.
254
255     In order to avoid races in signal handlers, the event API provides two
256     variables: event_sigcb and event_gotsig.  A signal handler sets
257     event_gotsig to indicate that a signal has been received.  The applica‐
258     tion sets event_sigcb to a callback function.  After the signal handler
259     sets event_gotsig, event_dispatch will execute the callback function to
260     process received signals.  The callback returns 1 when no events are reg‐
261     istered any more.  It can return -1 to indicate an error to the event
262     library, causing event_dispatch() to terminate with errno set to EINTR.
263
264     The function event_once() is similar to event_set().  However, it sched‐
265     ules a callback to be called exactly once and does not require the caller
266     to prepare an event structure.  This function supports EV_TIMEOUT,
267     EV_READ, and EV_WRITE.
268
269     The event_pending() function can be used to check if the event specified
270     by event is pending to run.  If EV_TIMEOUT was specified and tv is not
271     NULL, the expiration time of the event will be returned in tv.
272
273     The event_initialized() macro can be used to check if an event has been
274     initialized.
275
276     The event_loop function provides an interface for single pass execution
277     of pending events.  The flags EVLOOP_ONCE and EVLOOP_NONBLOCK are recog‐
278     nized.  The event_loopexit function exits from the event loop. The next
279     event_loop() iteration after the given timer expires will complete nor‐
280     mally (handling all queued events) then exit without blocking for events
281     again. Subsequent invocations of event_loop() will proceed normally.  The
282     event_loopbreak function exits from the event loop immediately.
283     event_loop() will abort after the next event is completed;
284     event_loopbreak() is typically invoked from this event's callback. This
285     behavior is analogous to the "break;" statement. Subsequent invocations
286     of event_loop() will proceed normally.
287
288     It is the responsibility of the caller to provide these functions with
289     pre-allocated event structures.
290

EVENT PRIORITIES

292     By default libevent schedules all active events with the same priority.
293     However, sometimes it is desirable to process some events with a higher
294     priority than others.  For that reason, libevent supports strict priority
295     queues.  Active events with a lower priority are always processed before
296     events with a higher priority.
297
298     The number of different priorities can be set initially with the
299     event_priority_init() function.  This function should be called before
300     the first call to event_dispatch().  The event_priority_set() function
301     can be used to assign a priority to an event.  By default, libevent
302     assigns the middle priority to all events unless their priority is
303     explicitly set.
304

THREAD SAFE EVENTS

306     Libevent has experimental support for thread-safe events.  When initial‐
307     izing the library via event_init(), an event base is returned.  This
308     event base can be used in conjunction with calls to event_base_set(),
309     event_base_dispatch(), event_base_loop(), event_base_loopexit(),
310     bufferevent_base_set() and event_base_free().  event_base_set() should be
311     called after preparing an event with event_set(), as event_set() assigns
312     the provided event to the most recently created event base.
313     bufferevent_base_set() should be called after preparing a bufferevent
314     with bufferevent_new().  event_base_free() should be used to free memory
315     associated with the event base when it is no longer needed.
316

BUFFERED EVENTS

318     libevent provides an abstraction on top of the regular event callbacks.
319     This abstraction is called a buffered event.  A buffered event provides
320     input and output buffers that get filled and drained automatically.  The
321     user of a buffered event no longer deals directly with the IO, but
322     instead is reading from input and writing to output buffers.
323
324     A new bufferevent is created by bufferevent_new().  The parameter fd
325     specifies the file descriptor from which data is read and written to.
326     This file descriptor is not allowed to be a pipe(2).  The next three
327     parameters are callbacks.  The read and write callback have the following
328     form: void (*cb)(struct bufferevent *bufev, void *arg).  The error call‐
329     back has the following form: void (*cb)(struct bufferevent *bufev, short
330     what, void *arg).  The argument is specified by the fourth parameter
331     cbarg.  A bufferevent struct pointer is returned on success, NULL on
332     error.  Both the read and the write callback may be NULL.  The error
333     callback has to be always provided.
334
335     Once initialized, the bufferevent structure can be used repeatedly with
336     bufferevent_enable() and bufferevent_disable().  The flags parameter can
337     be a combination of EV_READ and EV_WRITE.  When read enabled the buffer‐
338     event will try to read from the file descriptor and call the read call‐
339     back.  The write callback is executed whenever the output buffer is
340     drained below the write low watermark, which is 0 by default.
341
342     The bufferevent_write() function can be used to write data to the file
343     descriptor.  The data is appended to the output buffer and written to the
344     descriptor automatically as it becomes available for writing.
345     bufferevent_write() returns 0 on success or -1 on failure.  The
346     bufferevent_read() function is used to read data from the input buffer,
347     returning the amount of data read.
348
349     If multiple bases are in use, bufferevent_base_set() must be called
350     before enabling the bufferevent for the first time.
351

NON-BLOCKING HTTP SUPPORT

353     libevent provides a very thin HTTP layer that can be used both to host an
354     HTTP server and also to make HTTP requests.  An HTTP server can be cre‐
355     ated by calling evhttp_new().  It can be bound to any port and address
356     with the evhttp_bind_socket() function.  When the HTTP server is no
357     longer used, it can be freed via evhttp_free().
358
359     To be notified of HTTP requests, a user needs to register callbacks with
360     the HTTP server.  This can be done by calling evhttp_set_cb().  The sec‐
361     ond argument is the URI for which a callback is being registered.  The
362     corresponding callback will receive an struct evhttp_request object that
363     contains all information about the request.
364
365     This section does not document all the possible function calls; please
366     check event.h for the public interfaces.
367

ADDITIONAL NOTES

369     It is possible to disable support for epoll, kqueue, devpoll, poll or
370     select by setting the environment variable EVENT_NOEPOLL, EVENT_NOKQUEUE,
371     EVENT_NODEVPOLL, EVENT_NOPOLL or EVENT_NOSELECT, respectively.  By set‐
372     ting the environment variable EVENT_SHOW_METHOD, libevent displays the
373     kernel notification method that it uses.
374

RETURN VALUES

376     Upon successful completion event_add() and event_del() return 0.  Other‐
377     wise, -1 is returned and the global variable errno is set to indicate the
378     error.
379

SEE ALSO

381     kqueue(2), poll(2), select(2), evdns(3), timeout(9)
382

HISTORY

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

AUTHORS

389     The event library was written by Niels Provos.
390

BUGS

392     This documentation is neither complete nor authoritative.  If you are in
393     doubt about the usage of this API then check the source code to find out
394     how it works, write up the missing piece of documentation and send it to
395     me for inclusion in this man page.
396
397BSD                             August 8, 2000                             BSD
Impressum