1.\" $OpenBSD: event.3,v 1.47 2014/11/20 00:02:44 dlg 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: November 20 2014 $ 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 event_asr_run , 70.Nm event_asr_abort 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 int 80.Fn "event_loop" "int flags" 81.Ft int 82.Fn "event_loopexit" "struct timeval *tv" 83.Ft int 84.Fn "event_loopbreak" "void" 85.Ft void 86.Fn "event_set" "struct event *ev" "int fd" "short event" "void (*fn)(int, short, void *)" "void *arg" 87.Ft int 88.Fn "event_base_dispatch" "struct event_base *base" 89.Ft int 90.Fn "event_base_loop" "struct event_base *base" "int flags" 91.Ft int 92.Fn "event_base_loopexit" "struct event_base *base" "struct timeval *tv" 93.Ft int 94.Fn "event_base_loopbreak" "struct event_base *base" 95.Ft int 96.Fn "event_base_set" "struct event_base *base" "struct event *" 97.Ft void 98.Fn "event_base_free" "struct event_base *base" 99.Ft int 100.Fn "event_add" "struct event *ev" "struct timeval *tv" 101.Ft int 102.Fn "event_del" "struct event *ev" 103.Ft int 104.Fn "event_once" "int fd" "short event" "void (*fn)(int, short, void *)" "void *arg" "struct timeval *tv" 105.Ft int 106.Fn "event_base_once" "struct event_base *base" "int fd" "short event" "void (*fn)(int, short, void *)" "void *arg" "struct timeval *tv" 107.Ft int 108.Fn "event_pending" "struct event *ev" "short event" "struct timeval *tv" 109.Ft int 110.Fn "event_initialized" "struct event *ev" 111.Ft int 112.Fn "event_priority_init" "int npriorities" 113.Ft int 114.Fn "event_priority_set" "struct event *ev" "int priority" 115.Ft void 116.Fn "evtimer_set" "struct event *ev" "void (*fn)(int, short, void *)" "void *arg" 117.Ft void 118.Fn "evtimer_add" "struct event *ev" "struct timeval *" 119.Ft void 120.Fn "evtimer_del" "struct event *ev" 121.Ft int 122.Fn "evtimer_pending" "struct event *ev" "struct timeval *tv" 123.Ft int 124.Fn "evtimer_initialized" "struct event *ev" 125.Ft void 126.Fn "signal_set" "struct event *ev" "int signal" "void (*fn)(int, short, void *)" "void *arg" 127.Ft void 128.Fn "signal_add" "struct event *ev" "struct timeval *" 129.Ft void 130.Fn "signal_del" "struct event *ev" 131.Ft int 132.Fn "signal_pending" "struct event *ev" "struct timeval *tv" 133.Ft int 134.Fn "signal_initialized" "struct event *ev" 135.Ft "struct bufferevent *" 136.Fn "bufferevent_new" "int fd" "evbuffercb readcb" "evbuffercb writecb" "everrorcb" "void *cbarg" 137.Ft void 138.Fn "bufferevent_free" "struct bufferevent *bufev" 139.Ft int 140.Fn "bufferevent_write" "struct bufferevent *bufev" "const void *data" "size_t size" 141.Ft int 142.Fn "bufferevent_write_buffer" "struct bufferevent *bufev" "struct evbuffer *buf" 143.Ft size_t 144.Fn "bufferevent_read" "struct bufferevent *bufev" "void *data" "size_t size" 145.Ft int 146.Fn "bufferevent_enable" "struct bufferevent *bufev" "short event" 147.Ft int 148.Fn "bufferevent_disable" "struct bufferevent *bufev" "short event" 149.Ft void 150.Fn "bufferevent_settimeout" "struct bufferevent *bufev" "int timeout_read" "int timeout_write" 151.Ft int 152.Fn "bufferevent_base_set" "struct event_base *base" "struct bufferevent *bufev" 153.Ft "struct event_asr *" 154.Fn event_asr_run "struct asr_query *aq" "void (*fn)(struct asr_result *, void *)" "void *" 155.Ft "void" 156.Fn event_asr_abort "struct event_asr *eva" 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 419.Nm Libevent 420has experimental support for thread-safe events. 421When initializing the library via 422.Fn event_init , 423an event base is returned. 424This event base can be used in conjunction with calls to 425.Fn event_base_set , 426.Fn event_base_dispatch , 427.Fn event_base_loop , 428.Fn event_base_loopexit , 429.Fn bufferevent_base_set 430and 431.Fn event_base_free . 432.Fn event_base_set 433should be called after preparing an event with 434.Fn event_set , 435as 436.Fn event_set 437assigns the provided event to the most recently created event base. 438.Fn bufferevent_base_set 439should be called after preparing a bufferevent with 440.Fn bufferevent_new . 441.Fn event_base_free 442should be used to free memory associated with the event base 443when it is no longer needed. 444.Sh BUFFERED EVENTS 445.Nm libevent 446provides an abstraction on top of the regular event callbacks. 447This abstraction is called a 448.Va "buffered event" . 449A buffered event provides input and output buffers that get filled 450and drained automatically. 451The user of a buffered event no longer deals directly with the IO, 452but instead is reading from input and writing to output buffers. 453.Pp 454A new bufferevent is created by 455.Fn bufferevent_new . 456The parameter 457.Fa fd 458specifies the file descriptor from which data is read and written to. 459This file descriptor is not allowed to be a 460.Xr pipe 2 . 461The next three parameters are callbacks. 462The read and write callback have the following form: 463.Ft void 464.Fn "(*cb)" "struct bufferevent *bufev" "void *arg" . 465The error callback has the following form: 466.Ft void 467.Fn "(*cb)" "struct bufferevent *bufev" "short what" "void *arg" . 468The argument is specified by the fourth parameter 469.Fa "cbarg" . 470A 471.Fa bufferevent struct 472pointer is returned on success, NULL on error. 473Both the read and the write callback may be NULL. 474The error callback has to be always provided. 475.Pp 476Once initialized, the bufferevent structure can be used repeatedly with 477.Fn bufferevent_enable 478and 479.Fn bufferevent_disable . 480The flags parameter can be a combination of 481.Va EV_READ 482and 483.Va EV_WRITE . 484When read enabled the bufferevent will try to read from the file 485descriptor and call the read callback. 486The write callback is executed 487whenever the output buffer is drained below the write low watermark, 488which is 489.Va 0 490by default. 491.Pp 492The 493.Fn bufferevent_write 494function can be used to write data to the file descriptor. 495The data is appended to the output buffer and written to the descriptor 496automatically as it becomes available for writing. 497.Fn bufferevent_write 498returns 0 on success or \-1 on failure. 499The 500.Fn bufferevent_read 501function is used to read data from the input buffer, 502returning the amount of data read. 503.Pp 504If multiple bases are in use, 505.Fn bufferevent_base_set 506must be called before 507enabling the bufferevent for the first time. 508.Sh ADDITIONAL NOTES 509It is possible to disable support for 510.Va kqueue , poll 511or 512.Va select 513by setting the environment variable 514.Va EVENT_NOKQUEUE , EVENT_NOPOLL 515or 516.Va EVENT_NOSELECT , 517respectively. 518By setting the environment variable 519.Va EVENT_SHOW_METHOD , 520.Nm libevent 521displays the kernel notification method that it uses. 522.Sh RETURN VALUES 523Upon successful completion 524.Fn event_add 525and 526.Fn event_del 527return 0. 528Otherwise, \-1 is returned and the global variable errno is 529set to indicate the error. 530.Sh SEE ALSO 531.Xr kqueue 2 , 532.Xr poll 2 , 533.Xr select 2 , 534.Xr asr_run 3 , 535.Xr evbuffer_new 3 , 536.Xr timeout 9 537.Sh HISTORY 538The 539.Nm event 540API manpage is based on the 541.Xr timeout 9 542manpage by Artur Grabowski. 543Support for real-time signals was added by Taral. 544.Sh AUTHORS 545The 546.Nm event 547library was written by 548.An Niels Provos . 549