1 .\" $OpenBSD: event.3,v 1.4 2002/07/12 18:50:48 provos Exp $
3 .\" Copyright (c) 2000 Artur Grabowski <art@openbsd.org>
4 .\" All rights reserved.
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
10 .\" 1. Redistributions of source code must retain the above copyright
11 .\" notice, this list of conditions and the following disclaimer.
12 .\" 2. Redistributions in binary form must reproduce the above copyright
13 .\" notice, this list of conditions and the following disclaimer in the
14 .\" documentation and/or other materials provided with the distribution.
15 .\" 3. The name of the author may not be used to endorse or promote products
16 .\" derived from this software without specific prior written permission.
18 .\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
19 .\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20 .\" AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 .\" THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 .\" EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 .\" PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24 .\" OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 .\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 .\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27 .\" ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 .Nm event_base_dispatch ,
40 .Nm event_base_loopexit ,
48 .Nm event_initialized ,
49 .Nm event_priority_init ,
50 .Nm event_priority_set ,
55 .Nm evtimer_initialized ,
60 .Nm signal_initialized ,
62 .Nm bufferevent_free ,
63 .Nm bufferevent_write ,
64 .Nm bufferevent_write_buffer ,
65 .Nm bufferevent_read ,
66 .Nm bufferevent_enable ,
67 .Nm bufferevent_disable ,
68 .Nm bufferevent_settimeout ,
69 .Nm bufferevent_base_set ,
73 .Nm evbuffer_add_buffer ,
74 .Nm evbuffer_add_printf ,
75 .Nm evbuffer_add_vprintf ,
80 .Nm evbuffer_readline ,
83 .Nd execute a function when a specific event occurs
85 .Fd #include <sys/time.h>
86 .Fd #include <event.h>
87 .Ft "struct event_base *"
88 .Fn "event_init" "void"
90 .Fn "event_dispatch" "void"
92 .Fn "event_loop" "int flags"
94 .Fn "event_loopexit" "struct timeval *tv"
96 .Fn "event_set" "struct event *ev" "int fd" "short event" "void (*fn)(int, short, void *)" "void *arg"
98 .Fn "event_base_dispatch" "struct event_base *base"
100 .Fn "event_base_loop" "struct event_base *base" "int flags"
102 .Fn "event_base_loopexit" "struct event_base *base" "struct timeval *tv"
104 .Fn "event_base_set" "struct event_base *base" "struct event *"
106 .Fn "event_base_free" "struct event_base *base"
108 .Fn "event_add" "struct event *ev" "struct timeval *tv"
110 .Fn "event_del" "struct event *ev"
112 .Fn "event_once" "int fd" "short event" "void (*fn)(int, short, void *)" "void *arg" "struct timeval *tv"
114 .Fn "event_base_once" "struct event_base *base" "int fd" "short event" "void (*fn)(int, short, void *)" "void *arg" "struct timeval *tv"
116 .Fn "event_pending" "struct event *ev" "short event" "struct timeval *tv"
118 .Fn "event_initialized" "struct event *ev"
120 .Fn "event_priority_init" "int npriorities"
122 .Fn "event_priority_set" "struct event *ev" "int priority"
124 .Fn "evtimer_set" "struct event *ev" "void (*fn)(int, short, void *)" "void *arg"
126 .Fn "evtimer_add" "struct event *ev" "struct timeval *"
128 .Fn "evtimer_del" "struct event *ev"
130 .Fn "evtimer_pending" "struct event *ev" "struct timeval *tv"
132 .Fn "evtimer_initialized" "struct event *ev"
134 .Fn "signal_set" "struct event *ev" "int signal" "void (*fn)(int, short, void *)" "void *arg"
136 .Fn "signal_add" "struct event *ev" "struct timeval *"
138 .Fn "signal_del" "struct event *ev"
140 .Fn "signal_pending" "struct event *ev" "struct timeval *tv"
142 .Fn "signal_initialized" "struct event *ev"
143 .Ft "struct bufferevent *"
144 .Fn "bufferevent_new" "int fd" "evbuffercb readcb" "evbuffercb writecb" "everrorcb" "void *cbarg"
146 .Fn "bufferevent_free" "struct bufferevent *bufev"
148 .Fn "bufferevent_write" "struct bufferevent *bufev" "void *data" "size_t size"
150 .Fn "bufferevent_write_buffer" "struct bufferevent *bufev" "struct evbuffer *buf"
152 .Fn "bufferevent_read" "struct bufferevent *bufev" "void *data" "size_t size"
154 .Fn "bufferevent_enable" "struct bufferevent *bufev" "short event"
156 .Fn "bufferevent_disable" "struct bufferevent *bufev" "short event"
158 .Fn "bufferevent_settimeout" "struct bufferevent *bufev" "int timeout_read" "int timeout_write"
160 .Fn "bufferevent_base_set" "struct event_base *base" "struct bufferevent *bufev"
161 .Ft "struct evbuffer *"
162 .Fn "evbuffer_new" "void"
164 .Fn "evbuffer_free" "struct evbuffer *buf"
166 .Fn "evbuffer_add" "struct evbuffer *buf" "const void *data" "size_t size"
168 .Fn "evbuffer_add_buffer" "struct evbuffer *dst" "struct evbuffer *src"
170 .Fn "evbuffer_add_printf" "struct evbuffer *buf" "const char *fmt" "..."
172 .Fn "evbuffer_add_vprintf" "struct evbuffer *buf" "const char *fmt" "va_list ap"
174 .Fn "evbuffer_drain" "struct evbuffer *buf" "size_t size"
176 .Fn "evbuffer_write" "struct evbuffer *buf" "int fd"
178 .Fn "evbuffer_read" "struct evbuffer *buf" "int fd" "int size"
180 .Fn "evbuffer_find" "struct evbuffer *buf" "const u_char *data" "size_t size"
182 .Fn "evbuffer_readline" "struct evbuffer *buf"
183 .Ft "struct evhttp *"
184 .Fn "evhttp_start" "const char *address" "u_short port"
186 .Fn "evhttp_free" "struct evhttp* http"
188 .Fa (*event_sigcb)(void) ;
189 .Ft volatile sig_atomic_t
194 API provides a mechanism to execute a function when a specific event
195 on a file descriptor occurs or after a given time has passed.
199 API needs to be initialized with
201 before it can be used.
203 In order to process events, an application needs to call
205 This function only returns on error, and should replace the event core
206 of the application program.
208 In order to avoid races in signal handlers, the
210 API provides two variables:
217 to indicate that a signal has been received.
220 to a callback function.
221 After the signal handler sets
224 will execute the callback function to process received signals.
225 The callback returns 1 when no events are registered any more.
226 It can return \-1 to indicate an error to the
237 function provides an interface for single pass execution of pending
246 function allows the loop to be terminated after some amount of time
248 The parameter indicates the time after which the loop should terminate.
250 It is the responsibility of the caller to provide these functions with
251 pre-allocated event structures.
255 prepares the event structure
257 to be used in future calls to
261 The event will be prepared to call the function specified by the
265 argument indicating the file descriptor, a
267 argument indicating the type of event, and a
269 argument given in the
274 indicates the file descriptor that should be monitored for events.
275 The events can be either
279 indicating that an application can read or write from the file descriptor
280 respectively without blocking.
284 will be called with the file descriptor that triggered the event and
285 the type of event which will be either
299 Once initialized, the
301 structure can be used repeatedly with
305 and does not need to be reinitialized unless the function called and/or
306 the argument to it are to be changed.
309 structure has been added to libevent using
311 the structure must persist until the event occurs (assuming
313 is not set) or is removed
316 You may not reuse the same
318 structure for multiple monitored descriptors; each descriptor
324 schedules the execution of the
326 event when the event specified in
328 occurs or in at least the time specified in the
334 no timeout occurs and the function will only be called
335 if a matching event occurs on the file descriptor.
338 argument must be already initialized by
340 and may not be used in calls to
342 until it has timed out or been removed with
346 argument already has a scheduled timeout, the old timeout will be
347 replaced by the new one.
351 will cancel the event in the argument
353 If the event has already executed or has never been added
354 the call will have no effect.
360 However, it schedules a callback to be called exactly once and does not
361 require the caller to prepare an
364 This function supports
372 function can be used to check if the event specified by
381 the expiration time of the event will be returned in
385 .Fn event_initialized
386 macro can be used to check if an event has been initialized.
392 .Fn evtimer_initialized ,
395 are abbreviations for common situations where only a timeout is required.
396 The file descriptor passed will be \-1, and the event type will be
403 .Fn signal_initialized ,
407 The event type will be a persistent
414 It is possible to disable support for
415 .Va epoll , kqueue , devpoll , poll
418 by setting the environment variable
419 .Va EVENT_NOEPOLL , EVENT_NOKQUEUE , EVENT_NODEVPOLL , EVENT_NOPOLL
423 By setting the environment variable
424 .Va EVENT_SHOW_METHOD ,
426 displays the kernel notification method that it uses.
430 schedules all active events with the same priority.
431 However, sometimes it is desirable to process some events with a higher
432 priority than others.
435 supports strict priority queues.
436 Active events with a lower priority are always processed before events
437 with a higher priority.
439 The number of different priorities can be set initially with the
440 .Fn event_priority_init
442 This function should be called before the first call to
445 .Fn event_priority_set
446 function can be used to assign a priority to an event.
449 assigns the middle priority to all events unless their priority
451 .Sh THREAD SAFE EVENTS
453 has experimental support for thread-safe events.
454 When initializing the library via
456 an event base is returned.
457 This event base can be used in conjunction with calls to
459 .Fn event_base_dispatch ,
460 .Fn event_base_loop ,
461 .Fn event_base_loopexit ,
462 .Fn bufferevent_base_set
464 .Fn event_base_free .
466 should be called after preparing an event with
470 assigns the provided event to the most recently created event base.
471 .Fn bufferevent_base_set
472 should be called after preparing a bufferevent with
473 .Fn bufferevent_new .
475 should be used to free memory associated with the event base
476 when it is no longer needed.
479 provides an abstraction on top of the regular event callbacks.
480 This abstraction is called a
481 .Va "buffered event" .
482 A buffered event provides input and output buffers that get filled
483 and drained automatically.
484 The user of a buffered event no longer deals directly with the IO,
485 but instead is reading from input and writing to output buffers.
487 A new bufferevent is created by
488 .Fn bufferevent_new .
491 specifies the file descriptor from which data is read and written to.
492 This file descriptor is not allowed to be a
494 The next three parameters are callbacks.
495 The read and write callback have the following form:
497 .Fn "(*cb)" "struct bufferevent *bufev" "void *arg" .
498 The error callback has the following form:
500 .Fn "(*cb)" "struct bufferevent *bufev" "short what" "void *arg" .
501 The argument is specified by the fourth parameter
504 .Fa bufferevent struct
505 pointer is returned on success, NULL on error.
506 Both the read and the write callback may be NULL.
507 The error callback has to be always provided.
509 Once initialized, the bufferevent structure can be used repeatedly with
510 bufferevent_enable() and bufferevent_disable().
511 The flags parameter can be a combination of
515 When read enabled the bufferevent will try to read from the file
516 descriptor and call the read callback.
517 The write callback is executed
518 whenever the output buffer is drained below the write low watermark,
524 .Fn bufferevent_write
525 function can be used to write data to the file descriptor.
526 The data is appended to the output buffer and written to the descriptor
527 automatically as it becomes available for writing.
528 .Fn bufferevent_write
529 returns 0 on success or \-1 on failure.
532 function is used to read data from the input buffer,
533 returning the amount of data read.
535 If multiple bases are in use, bufferevent_base_set() must be called before
536 enabling the bufferevent for the first time.
537 .Sh NON-BLOCKING HTTP SUPPORT
539 provides a very thin HTTP layer that can be used both to host an HTTP
540 server and also to make HTTP requests.
541 An HTTP server can be created by calling
543 When the HTTP server is no longer used, it can be freed via
546 To be notified of HTTP requests, a user needs to register callbacks with the
548 This can be done by calling
550 The second argument is the URI for which a callback is being registered.
551 The corresponding callback will receive an
552 .Va struct evhttp_request
553 object that contains all information about the request.
555 This section does not document all the possible function calls; please
558 for the public interfaces.
560 Upon successful completion
565 Otherwise, \-1 is returned and the global variable errno is
566 set to indicate the error.
576 API manpage is based on the
578 manpage by Artur Grabowski.
581 to Windows is due to Michael A. Davis.
582 Support for real-time signals is due to Taral.
586 library was written by Niels Provos.
588 This documentation is neither complete nor authoritative.
589 If you are in doubt about the usage of this API then
590 check the source code to find out how it works, write
591 up the missing piece of documentation and send it to
592 me for inclusion in this man page.