A clone of btpd with my configuration changes.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

579 lignes
16 KiB

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