1.\"	$OpenBSD: event.3,v 1.41 2010/04/22 16:35:45 jmc Exp $
2.\"
3.\" Copyright (c) 2000 Artur Grabowski <art@openbsd.org>
4.\" All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\"
10.\" 1. Redistributions of source code must retain the above copyright
11.\"    notice, this list of conditions and the following disclaimer.
12.\" 2. The name of the author may not be used to endorse or promote products
13.\"    derived from this software without specific prior written permission.
14.\"
15.\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
16.\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
17.\" AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
18.\" THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19.\" EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20.\" PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21.\" OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22.\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23.\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24.\" ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25.\"
26.Dd $Mdocdate: October 19 2012 $
27.Dt EVENT 3
28.Os
29.Sh NAME
30.Nm event_init ,
31.Nm event_dispatch ,
32.Nm event_loop ,
33.Nm event_loopexit ,
34.Nm event_loopbreak ,
35.Nm event_set ,
36.Nm event_base_dispatch ,
37.Nm event_base_loop ,
38.Nm event_base_loopexit ,
39.Nm event_base_loopbreak ,
40.Nm event_base_set ,
41.Nm event_base_free ,
42.Nm event_add ,
43.Nm event_del ,
44.Nm event_once ,
45.Nm event_base_once ,
46.Nm event_pending ,
47.Nm event_initialized ,
48.Nm event_priority_init ,
49.Nm event_priority_set ,
50.Nm evtimer_set ,
51.Nm evtimer_add ,
52.Nm evtimer_del ,
53.Nm evtimer_pending ,
54.Nm evtimer_initialized ,
55.Nm signal_set ,
56.Nm signal_add ,
57.Nm signal_del ,
58.Nm signal_pending ,
59.Nm signal_initialized ,
60.Nm bufferevent_new ,
61.Nm bufferevent_free ,
62.Nm bufferevent_write ,
63.Nm bufferevent_write_buffer ,
64.Nm bufferevent_read ,
65.Nm bufferevent_enable ,
66.Nm bufferevent_disable ,
67.Nm bufferevent_settimeout ,
68.Nm bufferevent_base_set ,
69.Nm evbuffer_new ,
70.Nm evbuffer_free ,
71.Nm evbuffer_add ,
72.Nm evbuffer_add_buffer ,
73.Nm evbuffer_add_printf ,
74.Nm evbuffer_add_vprintf ,
75.Nm evbuffer_drain ,
76.Nm evbuffer_write ,
77.Nm evbuffer_read ,
78.Nm evbuffer_find ,
79.Nm evbuffer_readline
80.Nd execute a function when a specific event occurs
81.Sh SYNOPSIS
82.Fd #include <sys/time.h>
83.Fd #include <event.h>
84.Ft "struct event_base *"
85.Fn "event_init" "void"
86.Ft int
87.Fn "event_dispatch" "void"
88.Ft int
89.Fn "event_loop" "int flags"
90.Ft int
91.Fn "event_loopexit" "struct timeval *tv"
92.Ft int
93.Fn "event_loopbreak" "void"
94.Ft void
95.Fn "event_set" "struct event *ev" "int fd" "short event" "void (*fn)(int, short, void *)" "void *arg"
96.Ft int
97.Fn "event_base_dispatch" "struct event_base *base"
98.Ft int
99.Fn "event_base_loop" "struct event_base *base" "int flags"
100.Ft int
101.Fn "event_base_loopexit" "struct event_base *base" "struct timeval *tv"
102.Ft int
103.Fn "event_base_loopbreak" "struct event_base *base"
104.Ft int
105.Fn "event_base_set" "struct event_base *base" "struct event *"
106.Ft void
107.Fn "event_base_free" "struct event_base *base"
108.Ft int
109.Fn "event_add" "struct event *ev" "struct timeval *tv"
110.Ft int
111.Fn "event_del" "struct event *ev"
112.Ft int
113.Fn "event_once" "int fd" "short event" "void (*fn)(int, short, void *)" "void *arg" "struct timeval *tv"
114.Ft int
115.Fn "event_base_once" "struct event_base *base" "int fd" "short event" "void (*fn)(int, short, void *)" "void *arg" "struct timeval *tv"
116.Ft int
117.Fn "event_pending" "struct event *ev" "short event" "struct timeval *tv"
118.Ft int
119.Fn "event_initialized" "struct event *ev"
120.Ft int
121.Fn "event_priority_init" "int npriorities"
122.Ft int
123.Fn "event_priority_set" "struct event *ev" "int priority"
124.Ft void
125.Fn "evtimer_set" "struct event *ev" "void (*fn)(int, short, void *)" "void *arg"
126.Ft void
127.Fn "evtimer_add" "struct event *ev" "struct timeval *"
128.Ft void
129.Fn "evtimer_del" "struct event *ev"
130.Ft int
131.Fn "evtimer_pending" "struct event *ev" "struct timeval *tv"
132.Ft int
133.Fn "evtimer_initialized" "struct event *ev"
134.Ft void
135.Fn "signal_set" "struct event *ev" "int signal" "void (*fn)(int, short, void *)" "void *arg"
136.Ft void
137.Fn "signal_add" "struct event *ev" "struct timeval *"
138.Ft void
139.Fn "signal_del" "struct event *ev"
140.Ft int
141.Fn "signal_pending" "struct event *ev" "struct timeval *tv"
142.Ft int
143.Fn "signal_initialized" "struct event *ev"
144.Ft "struct bufferevent *"
145.Fn "bufferevent_new" "int fd" "evbuffercb readcb" "evbuffercb writecb" "everrorcb" "void *cbarg"
146.Ft void
147.Fn "bufferevent_free" "struct bufferevent *bufev"
148.Ft int
149.Fn "bufferevent_write" "struct bufferevent *bufev" "const void *data" "size_t size"
150.Ft int
151.Fn "bufferevent_write_buffer" "struct bufferevent *bufev" "struct evbuffer *buf"
152.Ft size_t
153.Fn "bufferevent_read" "struct bufferevent *bufev" "void *data" "size_t size"
154.Ft int
155.Fn "bufferevent_enable" "struct bufferevent *bufev" "short event"
156.Ft int
157.Fn "bufferevent_disable" "struct bufferevent *bufev" "short event"
158.Ft void
159.Fn "bufferevent_settimeout" "struct bufferevent *bufev" "int timeout_read" "int timeout_write"
160.Ft int
161.Fn "bufferevent_base_set" "struct event_base *base" "struct bufferevent *bufev"
162.Ft "struct evbuffer *"
163.Fn "evbuffer_new" "void"
164.Ft void
165.Fn "evbuffer_free" "struct evbuffer *buf"
166.Ft int
167.Fn "evbuffer_add" "struct evbuffer *buf" "const void *data" "size_t size"
168.Ft int
169.Fn "evbuffer_add_buffer" "struct evbuffer *dst" "struct evbuffer *src"
170.Ft int
171.Fn "evbuffer_add_printf" "struct evbuffer *buf" "const char *fmt" "..."
172.Ft int
173.Fn "evbuffer_add_vprintf" "struct evbuffer *buf" "const char *fmt" "va_list ap"
174.Ft void
175.Fn "evbuffer_drain" "struct evbuffer *buf" "size_t size"
176.Ft int
177.Fn "evbuffer_write" "struct evbuffer *buf" "int fd"
178.Ft int
179.Fn "evbuffer_read" "struct evbuffer *buf" "int fd" "int size"
180.Ft "u_char *"
181.Fn "evbuffer_find" "struct evbuffer *buf" "const u_char *data" "size_t size"
182.Ft "char *"
183.Fn "evbuffer_readline" "struct evbuffer *buf"
184.Sh DESCRIPTION
185The
186.Nm event
187API provides a mechanism to execute a function when a specific event
188on a file descriptor occurs or after a given time has passed.
189.Pp
190The
191.Nm event
192API needs to be initialized with
193.Fn event_init
194before it can be used.
195.Pp
196In order to process events, an application needs to call
197.Fn event_dispatch .
198This function only returns on error, and should replace the event core
199of the application program.
200.Pp
201The function
202.Fn event_set
203prepares the event structure
204.Fa ev
205to be used in future calls to
206.Fn event_add
207and
208.Fn event_del .
209The event will be prepared to call the function specified by the
210.Fa fn
211argument with an
212.Fa int
213argument indicating the file descriptor, a
214.Fa short
215argument indicating the type of event, and a
216.Fa void *
217argument given in the
218.Fa arg
219argument.
220The
221.Fa fd
222indicates the file descriptor that should be monitored for events.
223The events can be either
224.Va EV_READ ,
225.Va EV_WRITE ,
226or both,
227indicating that an application can read or write from the file descriptor
228respectively without blocking.
229.Pp
230The function
231.Fa fn
232will be called with the file descriptor that triggered the event and
233the type of event which will be either
234.Va EV_TIMEOUT ,
235.Va EV_SIGNAL ,
236.Va EV_READ ,
237or
238.Va EV_WRITE .
239Additionally, an event which has registered interest in more than one of the
240preceding events, via bitwise-OR to
241.Fn event_set ,
242can provide its callback function with a bitwise-OR of more than one triggered
243event.
244The additional flag
245.Va EV_PERSIST
246makes an
247.Fn event_add
248persistent until
249.Fn event_del
250has been called.
251.Pp
252Once initialized, the
253.Fa ev
254structure can be used repeatedly with
255.Fn event_add
256and
257.Fn event_del
258and does not need to be reinitialized unless the function called and/or
259the argument to it are to be changed.
260However, when an
261.Fa ev
262structure has been added to libevent using
263.Fn event_add
264the structure must persist until the event occurs (assuming
265.Fa EV_PERSIST
266is not set) or is removed
267using
268.Fn event_del .
269You may not reuse the same
270.Fa ev
271structure for multiple monitored descriptors; each descriptor
272needs its own
273.Fa ev .
274.Pp
275The function
276.Fn event_add
277schedules the execution of the
278.Fa ev
279event when the event specified in
280.Fn event_set
281occurs or in at least the time specified in the
282.Fa tv .
283If
284.Fa tv
285is
286.Dv NULL ,
287no timeout occurs and the function will only be called
288if a matching event occurs on the file descriptor.
289The event in the
290.Fa ev
291argument must be already initialized by
292.Fn event_set
293and may not be used in calls to
294.Fn event_set
295until it has timed out or been removed with
296.Fn event_del .
297If the event in the
298.Fa ev
299argument already has a scheduled timeout, the old timeout will be
300replaced by the new one.
301.Pp
302The function
303.Fn event_del
304will cancel the event in the argument
305.Fa ev .
306If the event has already executed or has never been added
307the call will have no effect.
308.Pp
309The functions
310.Fn evtimer_set ,
311.Fn evtimer_add ,
312.Fn evtimer_del ,
313.Fn evtimer_initialized ,
314and
315.Fn evtimer_pending
316are abbreviations for common situations where only a timeout is required.
317The file descriptor passed will be \-1, and the event type will be
318.Va EV_TIMEOUT .
319.Pp
320The functions
321.Fn signal_set ,
322.Fn signal_add ,
323.Fn signal_del ,
324.Fn signal_initialized ,
325and
326.Fn signal_pending
327are abbreviations.
328The event type will be a persistent
329.Va EV_SIGNAL .
330That means
331.Fn signal_set
332adds
333.Va EV_PERSIST .
334.Pp
335The function
336.Fn event_once
337is similar to
338.Fn event_set .
339However, it schedules a callback to be called exactly once and does not
340require the caller to prepare an
341.Fa event
342structure.
343This function supports
344.Fa EV_TIMEOUT ,
345.Fa EV_READ ,
346and
347.Fa EV_WRITE .
348.Pp
349The
350.Fn event_pending
351function can be used to check if the event specified by
352.Fa event
353is pending to run.
354If
355.Va EV_TIMEOUT
356was specified and
357.Fa tv
358is not
359.Dv NULL ,
360the expiration time of the event will be returned in
361.Fa tv .
362.Pp
363The
364.Fn event_initialized
365macro can be used to check if an event has been initialized.
366.Pp
367The
368.Nm event_loop
369function provides an interface for single pass execution of pending
370events.
371The flags
372.Va EVLOOP_ONCE
373and
374.Va EVLOOP_NONBLOCK
375are recognized.
376The
377.Nm event_loopexit
378function exits from the event loop.
379The next
380.Fn event_loop
381iteration after the
382given timer expires will complete normally (handling all queued events) then
383exit without blocking for events again.
384Subsequent invocations of
385.Fn event_loop
386will proceed normally.
387The
388.Nm event_loopbreak
389function exits from the event loop immediately.
390.Fn event_loop
391will abort after the next event is completed;
392.Fn event_loopbreak
393is typically invoked from this event's callback.
394This behavior is analogous to the "break;" statement.
395Subsequent invocations of
396.Fn event_loop
397will proceed normally.
398.Pp
399It is the responsibility of the caller to provide these functions with
400pre-allocated event structures.
401.Sh EVENT PRIORITIES
402By default
403.Nm libevent
404schedules all active events with the same priority.
405However, sometimes it is desirable to process some events with a higher
406priority than others.
407For that reason,
408.Nm libevent
409supports strict priority queues.
410Active events with a lower priority are always processed before events
411with a higher priority.
412.Pp
413The number of different priorities can be set initially with the
414.Fn event_priority_init
415function.
416This function should be called before the first call to
417.Fn event_dispatch .
418The
419.Fn event_priority_set
420function can be used to assign a priority to an event.
421By default,
422.Nm libevent
423assigns the middle priority to all events unless their priority
424is explicitly set.
425.Sh THREAD SAFE EVENTS
426.Nm Libevent
427has experimental support for thread-safe events.
428When initializing the library via
429.Fn event_init ,
430an event base is returned.
431This event base can be used in conjunction with calls to
432.Fn event_base_set ,
433.Fn event_base_dispatch ,
434.Fn event_base_loop ,
435.Fn event_base_loopexit ,
436.Fn bufferevent_base_set
437and
438.Fn event_base_free .
439.Fn event_base_set
440should be called after preparing an event with
441.Fn event_set ,
442as
443.Fn event_set
444assigns the provided event to the most recently created event base.
445.Fn bufferevent_base_set
446should be called after preparing a bufferevent with
447.Fn bufferevent_new .
448.Fn event_base_free
449should be used to free memory associated with the event base
450when it is no longer needed.
451.Sh BUFFERED EVENTS
452.Nm libevent
453provides an abstraction on top of the regular event callbacks.
454This abstraction is called a
455.Va "buffered event" .
456A buffered event provides input and output buffers that get filled
457and drained automatically.
458The user of a buffered event no longer deals directly with the IO,
459but instead is reading from input and writing to output buffers.
460.Pp
461A new bufferevent is created by
462.Fn bufferevent_new .
463The parameter
464.Fa fd
465specifies the file descriptor from which data is read and written to.
466This file descriptor is not allowed to be a
467.Xr pipe 2 .
468The next three parameters are callbacks.
469The read and write callback have the following form:
470.Ft void
471.Fn "(*cb)" "struct bufferevent *bufev" "void *arg" .
472The error callback has the following form:
473.Ft void
474.Fn "(*cb)" "struct bufferevent *bufev" "short what" "void *arg" .
475The argument is specified by the fourth parameter
476.Fa "cbarg" .
477A
478.Fa bufferevent struct
479pointer is returned on success, NULL on error.
480Both the read and the write callback may be NULL.
481The error callback has to be always provided.
482.Pp
483Once initialized, the bufferevent structure can be used repeatedly with
484.Fn bufferevent_enable
485and
486.Fn bufferevent_disable .
487The flags parameter can be a combination of
488.Va EV_READ
489and
490.Va EV_WRITE .
491When read enabled the bufferevent will try to read from the file
492descriptor and call the read callback.
493The write callback is executed
494whenever the output buffer is drained below the write low watermark,
495which is
496.Va 0
497by default.
498.Pp
499The
500.Fn bufferevent_write
501function can be used to write data to the file descriptor.
502The data is appended to the output buffer and written to the descriptor
503automatically as it becomes available for writing.
504.Fn bufferevent_write
505returns 0 on success or \-1 on failure.
506The
507.Fn bufferevent_read
508function is used to read data from the input buffer,
509returning the amount of data read.
510.Pp
511If multiple bases are in use,
512.Fn bufferevent_base_set
513must be called before
514enabling the bufferevent for the first time.
515.Sh ADDITIONAL NOTES
516It is possible to disable support for
517.Va kqueue , poll
518or
519.Va select
520by setting the environment variable
521.Va EVENT_NOKQUEUE , EVENT_NOPOLL
522or
523.Va EVENT_NOSELECT ,
524respectively.
525By setting the environment variable
526.Va EVENT_SHOW_METHOD ,
527.Nm libevent
528displays the kernel notification method that it uses.
529.Sh RETURN VALUES
530Upon successful completion
531.Fn event_add
532and
533.Fn event_del
534return 0.
535Otherwise, \-1 is returned and the global variable errno is
536set to indicate the error.
537.Sh SEE ALSO
538.Xr kqueue 2 ,
539.Xr poll 2 ,
540.Xr select 2 ,
541.Xr timeout 9
542.Sh HISTORY
543The
544.Nm event
545API manpage is based on the
546.Xr timeout 9
547manpage by Artur Grabowski.
548Support for real-time signals was added by Taral.
549.Sh AUTHORS
550The
551.Nm event
552library was written by
553.An Niels Provos .
554.Sh BUGS
555This documentation is neither complete nor authoritative.
556If you are in doubt about the usage of this API then
557check the source code to find out how it works, write
558up the missing piece of documentation and send it to
559me for inclusion in this man page.
560