Merge from vendor branch GCC:
[dragonfly.git] / contrib / libevent / event.3
1 .\"     $OpenBSD: event.3,v 1.4 2002/07/12 18:50:48 provos 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. 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.
17 .\"
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.
28 .\"
29 .Dd August 8, 2000
30 .Dt EVENT 3
31 .Os
32 .Sh NAME
33 .Nm event_init ,
34 .Nm event_dispatch ,
35 .Nm event_loop ,
36 .Nm event_loopexit ,
37 .Nm event_set ,
38 .Nm event_base_dispatch ,
39 .Nm event_base_loop ,
40 .Nm event_base_loopexit ,
41 .Nm event_base_set ,
42 .Nm event_base_free ,
43 .Nm event_add ,
44 .Nm event_del ,
45 .Nm event_once ,
46 .Nm event_base_once ,
47 .Nm event_pending ,
48 .Nm event_initialized ,
49 .Nm event_priority_init ,
50 .Nm event_priority_set ,
51 .Nm evtimer_set ,
52 .Nm evtimer_add ,
53 .Nm evtimer_del ,
54 .Nm evtimer_pending ,
55 .Nm evtimer_initialized ,
56 .Nm signal_set ,
57 .Nm signal_add ,
58 .Nm signal_del ,
59 .Nm signal_pending ,
60 .Nm signal_initialized ,
61 .Nm bufferevent_new ,
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 ,
70 .Nm evbuffer_new ,
71 .Nm evbuffer_free ,
72 .Nm evbuffer_add ,
73 .Nm evbuffer_add_buffer ,
74 .Nm evbuffer_add_printf ,
75 .Nm evbuffer_add_vprintf ,
76 .Nm evbuffer_drain ,
77 .Nm evbuffer_write ,
78 .Nm evbuffer_read ,
79 .Nm evbuffer_find ,
80 .Nm evbuffer_readline ,
81 .Nm evhttp_start ,
82 .Nm evhttp_free
83 .Nd execute a function when a specific event occurs
84 .Sh SYNOPSIS
85 .Fd #include <sys/time.h>
86 .Fd #include <event.h>
87 .Ft "struct event_base *"
88 .Fn "event_init" "void"
89 .Ft int
90 .Fn "event_dispatch" "void"
91 .Ft int
92 .Fn "event_loop" "int flags"
93 .Ft int
94 .Fn "event_loopexit" "struct timeval *tv"
95 .Ft void
96 .Fn "event_set" "struct event *ev" "int fd" "short event" "void (*fn)(int, short, void *)" "void *arg"
97 .Ft int
98 .Fn "event_base_dispatch" "struct event_base *base"
99 .Ft int
100 .Fn "event_base_loop" "struct event_base *base" "int flags"
101 .Ft int
102 .Fn "event_base_loopexit" "struct event_base *base" "struct timeval *tv"
103 .Ft int
104 .Fn "event_base_set" "struct event_base *base" "struct event *"
105 .Ft void
106 .Fn "event_base_free" "struct event_base *base"
107 .Ft int
108 .Fn "event_add" "struct event *ev" "struct timeval *tv"
109 .Ft int
110 .Fn "event_del" "struct event *ev"
111 .Ft int
112 .Fn "event_once" "int fd" "short event" "void (*fn)(int, short, void *)" "void *arg" "struct timeval *tv"
113 .Ft int
114 .Fn "event_base_once" "struct event_base *base" "int fd" "short event" "void (*fn)(int, short, void *)" "void *arg" "struct timeval *tv"
115 .Ft int
116 .Fn "event_pending" "struct event *ev" "short event" "struct timeval *tv"
117 .Ft int
118 .Fn "event_initialized" "struct event *ev"
119 .Ft int
120 .Fn "event_priority_init" "int npriorities"
121 .Ft int
122 .Fn "event_priority_set" "struct event *ev" "int priority"
123 .Ft void
124 .Fn "evtimer_set" "struct event *ev" "void (*fn)(int, short, void *)" "void *arg"
125 .Ft void
126 .Fn "evtimer_add" "struct event *ev" "struct timeval *"
127 .Ft void
128 .Fn "evtimer_del" "struct event *ev"
129 .Ft int
130 .Fn "evtimer_pending" "struct event *ev" "struct timeval *tv"
131 .Ft int
132 .Fn "evtimer_initialized" "struct event *ev"
133 .Ft void
134 .Fn "signal_set" "struct event *ev" "int signal" "void (*fn)(int, short, void *)" "void *arg"
135 .Ft void
136 .Fn "signal_add" "struct event *ev" "struct timeval *"
137 .Ft void
138 .Fn "signal_del" "struct event *ev"
139 .Ft int
140 .Fn "signal_pending" "struct event *ev" "struct timeval *tv"
141 .Ft int
142 .Fn "signal_initialized" "struct event *ev"
143 .Ft "struct bufferevent *"
144 .Fn "bufferevent_new" "int fd" "evbuffercb readcb" "evbuffercb writecb" "everrorcb" "void *cbarg"
145 .Ft void
146 .Fn "bufferevent_free" "struct bufferevent *bufev"
147 .Ft int
148 .Fn "bufferevent_write" "struct bufferevent *bufev" "void *data" "size_t size"
149 .Ft int
150 .Fn "bufferevent_write_buffer" "struct bufferevent *bufev" "struct evbuffer *buf"
151 .Ft size_t
152 .Fn "bufferevent_read" "struct bufferevent *bufev" "void *data" "size_t size"
153 .Ft int
154 .Fn "bufferevent_enable" "struct bufferevent *bufev" "short event"
155 .Ft int
156 .Fn "bufferevent_disable" "struct bufferevent *bufev" "short event"
157 .Ft void
158 .Fn "bufferevent_settimeout" "struct bufferevent *bufev" "int timeout_read" "int timeout_write"
159 .Ft int
160 .Fn "bufferevent_base_set" "struct event_base *base" "struct bufferevent *bufev"
161 .Ft "struct evbuffer *"
162 .Fn "evbuffer_new" "void"
163 .Ft void
164 .Fn "evbuffer_free" "struct evbuffer *buf"
165 .Ft int
166 .Fn "evbuffer_add" "struct evbuffer *buf" "const void *data" "size_t size"
167 .Ft int
168 .Fn "evbuffer_add_buffer" "struct evbuffer *dst" "struct evbuffer *src"
169 .Ft int
170 .Fn "evbuffer_add_printf" "struct evbuffer *buf" "const char *fmt" "..."
171 .Ft int
172 .Fn "evbuffer_add_vprintf" "struct evbuffer *buf" "const char *fmt" "va_list ap"
173 .Ft void
174 .Fn "evbuffer_drain" "struct evbuffer *buf" "size_t size"
175 .Ft int
176 .Fn "evbuffer_write" "struct evbuffer *buf" "int fd"
177 .Ft int
178 .Fn "evbuffer_read" "struct evbuffer *buf" "int fd" "int size"
179 .Ft "u_char *"
180 .Fn "evbuffer_find" "struct evbuffer *buf" "const u_char *data" "size_t size"
181 .Ft "char *"
182 .Fn "evbuffer_readline" "struct evbuffer *buf"
183 .Ft "struct evhttp *"
184 .Fn "evhttp_start" "const char *address" "u_short port"
185 .Ft "void"
186 .Fn "evhttp_free" "struct evhttp* http"
187 .Ft int
188 .Fa (*event_sigcb)(void) ;
189 .Ft volatile sig_atomic_t
190 .Fa event_gotsig ;
191 .Sh DESCRIPTION
192 The
193 .Nm event
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.
196 .Pp
197 The
198 .Nm event
199 API needs to be initialized with
200 .Fn event_init
201 before it can be used.
202 .Pp
203 In order to process events, an application needs to call
204 .Fn event_dispatch .
205 This function only returns on error, and should replace the event core
206 of the application program.
207 .Pp
208 In order to avoid races in signal handlers, the
209 .Nm event
210 API provides two variables:
211 .Va event_sigcb
212 and
213 .Va event_gotsig .
214 A signal handler
215 sets
216 .Va event_gotsig
217 to indicate that a signal has been received.
218 The application sets
219 .Va event_sigcb
220 to a callback function.
221 After the signal handler sets
222 .Va event_gotsig ,
223 .Nm event_dispatch
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
227 .Nm event
228 library, causing
229 .Fn event_dispatch
230 to terminate with
231 .Va errno
232 set to
233 .Er EINTR .
234 .Pp
235 The
236 .Nm event_loop
237 function provides an interface for single pass execution of pending
238 events.
239 The flags
240 .Va EVLOOP_ONCE
241 and
242 .Va EVLOOP_NONBLOCK
243 are recognized.
244 The
245 .Nm event_loopexit
246 function allows the loop to be terminated after some amount of time
247 has passed.
248 The parameter indicates the time after which the loop should terminate.
249 .Pp
250 It is the responsibility of the caller to provide these functions with
251 pre-allocated event structures.
252 .Pp
253 The function
254 .Fn event_set
255 prepares the event structure
256 .Fa ev
257 to be used in future calls to
258 .Fn event_add
259 and
260 .Fn event_del .
261 The event will be prepared to call the function specified by the
262 .Fa fn
263 argument with an
264 .Fa int
265 argument indicating the file descriptor, a
266 .Fa short
267 argument indicating the type of event, and a
268 .Fa void *
269 argument given in the
270 .Fa arg
271 argument.
272 The
273 .Fa fd
274 indicates the file descriptor that should be monitored for events.
275 The events can be either
276 .Va EV_READ ,
277 .Va EV_WRITE ,
278 or both,
279 indicating that an application can read or write from the file descriptor
280 respectively without blocking.
281 .Pp
282 The function
283 .Fa fn
284 will be called with the file descriptor that triggered the event and
285 the type of event which will be either
286 .Va EV_TIMEOUT ,
287 .Va EV_SIGNAL ,
288 .Va EV_READ ,
289 or
290 .Va EV_WRITE .
291 The additional flag
292 .Va EV_PERSIST
293 makes an
294 .Fn event_add
295 persistent until
296 .Fn event_del
297 has been called.
298 .Pp
299 Once initialized, the
300 .Fa ev
301 structure can be used repeatedly with
302 .Fn event_add
303 and
304 .Fn event_del
305 and does not need to be reinitialized unless the function called and/or
306 the argument to it are to be changed.
307 However, when an
308 .Fa ev
309 structure has been added to libevent using
310 .Fn event_add
311 the structure must persist until the event occurs (assuming
312 .Fa EV_PERSIST
313 is not set) or is removed
314 using
315 .Fn event_del .
316 You may not reuse the same
317 .Fa ev
318 structure for multiple monitored descriptors; each descriptor
319 needs its own
320 .Fa ev .
321 .Pp
322 The function
323 .Fn event_add
324 schedules the execution of the
325 .Fa ev
326 event when the event specified in
327 .Fn event_set
328 occurs or in at least the time specified in the
329 .Fa tv .
330 If
331 .Fa tv
332 is
333 .Dv NULL ,
334 no timeout occurs and the function will only be called
335 if a matching event occurs on the file descriptor.
336 The event in the
337 .Fa ev
338 argument must be already initialized by
339 .Fn event_set
340 and may not be used in calls to
341 .Fn event_set
342 until it has timed out or been removed with
343 .Fn event_del .
344 If the event in the
345 .Fa ev
346 argument already has a scheduled timeout, the old timeout will be
347 replaced by the new one.
348 .Pp
349 The function
350 .Fn event_del
351 will cancel the event in the argument
352 .Fa ev .
353 If the event has already executed or has never been added
354 the call will have no effect.
355 .Pp
356 The function
357 .Fn event_once
358 is similar to
359 .Fn event_set .
360 However, it schedules a callback to be called exactly once and does not
361 require the caller to prepare an
362 .Fa event
363 structure.
364 This function supports
365 .Fa EV_TIMEOUT ,
366 .Fa EV_READ ,
367 and
368 .Fa EV_WRITE .
369 .Pp
370 The
371 .Fn event_pending
372 function can be used to check if the event specified by
373 .Fa event
374 is pending to run.
375 If
376 .Va EV_TIMEOUT
377 was specified and
378 .Fa tv
379 is not
380 .Dv NULL ,
381 the expiration time of the event will be returned in
382 .Fa tv .
383 .Pp
384 The
385 .Fn event_initialized
386 macro can be used to check if an event has been initialized.
387 .Pp
388 The functions
389 .Fn evtimer_set ,
390 .Fn evtimer_add ,
391 .Fn evtimer_del ,
392 .Fn evtimer_initialized ,
393 and
394 .Fn evtimer_pending
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
397 .Va EV_TIMEOUT .
398 .Pp
399 The functions
400 .Fn signal_set ,
401 .Fn signal_add ,
402 .Fn signal_del ,
403 .Fn signal_initialized ,
404 and
405 .Fn signal_pending
406 are abbreviations.
407 The event type will be a persistent
408 .Va EV_SIGNAL .
409 That means
410 .Fn signal_set
411 adds
412 .Va EV_PERSIST .
413 .Pp
414 It is possible to disable support for
415 .Va epoll , kqueue , devpoll , poll
416 or
417 .Va select
418 by setting the environment variable
419 .Va EVENT_NOEPOLL , EVENT_NOKQUEUE , EVENT_NODEVPOLL , EVENT_NOPOLL
420 or
421 .Va EVENT_NOSELECT ,
422 respectively.
423 By setting the environment variable
424 .Va EVENT_SHOW_METHOD ,
425 .Nm libevent
426 displays the kernel notification method that it uses.
427 .Sh EVENT PRIORITIES
428 By default
429 .Nm libevent
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.
433 For that reason,
434 .Nm libevent
435 supports strict priority queues.
436 Active events with a lower priority are always processed before events
437 with a higher priority.
438 .Pp
439 The number of different priorities can be set initially with the
440 .Fn event_priority_init
441 function.
442 This function should be called before the first call to
443 .Fn event_dispatch .
444 The
445 .Fn event_priority_set
446 function can be used to assign a priority to an event.
447 By default,
448 .Nm libevent
449 assigns the middle priority to all events unless their priority
450 is explicitly set.
451 .Sh THREAD SAFE EVENTS
452 .Nm Libevent
453 has experimental support for thread-safe events.
454 When initializing the library via
455 .Fn event_init ,
456 an event base is returned.
457 This event base can be used in conjunction with calls to
458 .Fn event_base_set ,
459 .Fn event_base_dispatch ,
460 .Fn event_base_loop ,
461 .Fn event_base_loopexit ,
462 .Fn bufferevent_base_set
463 and
464 .Fn event_base_free .
465 .Fn event_base_set
466 should be called after preparing an event with
467 .Fn event_set ,
468 as
469 .Fn event_set
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 .
474 .Fn event_base_free
475 should be used to free memory associated with the event base
476 when it is no longer needed.
477 .Sh BUFFERED EVENTS
478 .Nm libevent
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.
486 .Pp
487 A new bufferevent is created by
488 .Fn bufferevent_new .
489 The parameter
490 .Fa fd
491 specifies the file descriptor from which data is read and written to.
492 This file descriptor is not allowed to be a
493 .Xr pipe 2 .
494 The next three parameters are callbacks.
495 The read and write callback have the following form:
496 .Ft void
497 .Fn "(*cb)" "struct bufferevent *bufev" "void *arg" .
498 The error callback has the following form:
499 .Ft void
500 .Fn "(*cb)" "struct bufferevent *bufev" "short what" "void *arg" .
501 The argument is specified by the fourth parameter
502 .Fa "cbarg" .
503 A
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.
508 .Pp
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
512 .Va EV_READ
513 and
514 .Va EV_WRITE .
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,
519 which is
520 .Va 0
521 by default.
522 .Pp
523 The
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.
530 The
531 .Fn bufferevent_read
532 function is used to read data from the input buffer,
533 returning the amount of data read.
534 .Pp
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
538 .Nm libevent
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
542 .Fn evhttp_start .
543 When the HTTP server is no longer used, it can be freed via
544 .Fn evhttp_free .
545 .Pp
546 To be notified of HTTP requests, a user needs to register callbacks with the
547 HTTP server.
548 This can be done by calling
549 .Fn evhttp_set_cb .
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.
554 .Pp
555 This section does not document all the possible function calls; please
556 check
557 .Va event.h
558 for the public interfaces.
559 .Sh RETURN VALUES
560 Upon successful completion
561 .Fn event_add
562 and
563 .Fn event_del
564 return 0.
565 Otherwise, \-1 is returned and the global variable errno is
566 set to indicate the error.
567 .Sh SEE ALSO
568 .Xr kqueue 2 ,
569 .Xr poll 2 ,
570 .Xr select 2 ,
571 .Xr evdns 3 ,
572 .Xr timeout 9
573 .Sh HISTORY
574 The
575 .Nm event
576 API manpage is based on the
577 .Xr timeout 9
578 manpage by Artur Grabowski.
579 The port of
580 .Nm libevent
581 to Windows is due to Michael A. Davis.
582 Support for real-time signals is due to Taral.
583 .Sh AUTHORS
584 The
585 .Nm event
586 library was written by Niels Provos.
587 .Sh BUGS
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.