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