1 .\" $Id: eventlib.mdoc,v 1.1.2.1.10.1 2004/03/09 08:33:43 marka Exp $
3 .\" Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
4 .\" Copyright (c) 1995-1999 by Internet Software Consortium
6 .\" Permission to use, copy, modify, and distribute this software for any
7 .\" purpose with or without fee is hereby granted, provided that the above
8 .\" copyright notice and this permission notice appear in all copies.
10 .\" THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
11 .\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 .\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
13 .\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
16 .\" OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
47 .Nm evTouchIdleTimer ,
48 .Nm evClearIdleTimer ,
73 .Nd event handling library
75 .Fd #include <isc/eventlib.h>
77 .Fn \*(lp*evConnFunc\*(rp "evContext ctx" "void *uap" "int fd" \
78 "const void *la" "int lalen" "const void *ra" "int ralen"
80 .Fn \*(lp*evTimerFunc\*(rp "evContext ctx" "void *uap" \
81 "struct timespec due" "struct timespec inter"
83 .Fn \*(lp*evFileFunc\*(rp "evContext ctx" "void *uap" "int fd" "int eventmask"
85 .Fn \*(lp*evStreamFunc\*(rp "evContext ctx" "void *uap" "int fd" "int bytes"
87 .Fn \*(lp*evWaitFunc\*(rp "evContext ctx" "void *uap" "const void *tag"
89 .Fn evCreate "evContext *ctx"
91 .Fn evDestroy "evContext ctx"
93 .Fn evGetNext "evContext ctx" "evEvent *ev" "int options"
95 .Fn evDispatch "evContext ctx" "evEvent ev"
97 .Fn evDrop "evContext ctx" "evEvent ev"
99 .Fn evMainLoop "evContext ctx"
101 .Fn evConsTime "int sec" "int usec"
103 .Fn evTimeSpec "struct timeval tv"
105 .Fn evTimeVal "struct timespec ts"
107 .Fn evAddTime "struct timespec addend1" "struct timespec addend2"
109 .Fn evSubTime "struct timespec minuend" "struct timespec subtrahend"
111 .Fn evCmpTime "struct timespec a" "struct timespec b"
117 .Fn evLastEventTime "evContext opaqueCtx"
119 .Fn evSetTimer "evContext ctx" "evTimerFunc func" "void *uap" \
120 "struct timespec due" "struct timespec inter" "evTimerID *id"
122 .Fn evResetTimer "evContext ctx" "evTimerID id" "evTimerFunc func" \
123 "void *uap" "struct timespec due" "struct timespec inter"
125 .Fn evConfigTimer "evContext ctx" "evTimerID id" "const char *param" \
128 .Fn evClearTimer "evContext ctx" "evTimerID id"
130 .Fn evSetIdleTimer "evContext opaqueCtx" "evTimerFunc func" "void *uap" \
131 "struct timespec max_idle" "evTimerID *opaqueID"
133 .Fn evTouchIdleTimer "evContext opaqueCtx" "evTimerID id"
135 .Fn evResetIdleTimer "evContext opaqueCtx" "evTimerID id" "evTimerFunc func" \
136 "void *uap" "struct timespec max_idle"
138 .Fn evClearIdleTimer "evContext opaqueCtx" "evTimerID id"
140 .Fn evWaitFor "evContext opaqueCtx" "const void *tag" \
141 "evWaitFunc func" "void *uap" "evWaitID *id"
143 .Fn evDo "evContext opaqueCtx" "const void *tag"
145 .Fn evUnwait "evContext opaqueCtx" "evWaitID id"
147 .Fn evDefer "evContext opaqueCtx" "evWaitFunc func" "void *uap"
149 .Fn evSelectFD "evContext ctx" "int fd" "int eventmask" \
150 "evFileFunc func" "void *uap" "evFileID *id"
152 .Fn evDeselectFD "evContext ctx" "evFileID id"
154 .Fn evConsIovec "void *buf" "size_t cnt"
156 .Fn evWrite "evContext ctx" "int fd" "const struct iovec *iov" "int cnt" \
157 "evStreamFunc func" "void *uap" "evStreamID *id"
159 .Fn evRead "evContext ctx" "int fd" "const struct iovec *iov" "int cnt" \
160 "evStreamFunc func" "void *uap" "evStreamID *id"
162 .Fn evCancelRW "evContext ctx" "evStreamID id"
164 .Fn evTimeRW "evContext opaqueCtx" "evStreamID id" "evTimerID timer"
166 .Fn evUntimeRW "evContext opaqueCtx" "evStreamID id"
168 .Fn evListen "evContext ctx" "int fd" "int maxconn" \
169 "evConnFunc func" "void *uap" "evConnID *id"
171 .Fn evConnect "evContext ctx" "int fd" "void *ra" "int ralen" \
172 "evConnFunc func" "void *uap" "evConnID *id"
174 .Fn evCancelConn "evContext ctx" "evConnID id"
176 .Fn evHold "evContext ctx" "evConnID id"
178 .Fn evUnhold "evContext ctx" "evConnID id"
180 .Fn evTryAccept "evContext ctx" "evConnID id" "int *sys_errno"
182 .Fn evSetDebug "evContext ctx" "int level" "FILE *output"
184 .Fn evPrintf "const evContext_p *ctx" "int level" "const char *fmt" "..."
186 .Fn evInitID "*\s-1ID\s+1"
188 .Fn evTestID "\s-1ID\s+1"
190 .Fn evGetOption "evContext *ctx" "const char *option" "int *ret"
192 .Fn evSetOption "evContext *ctx" "const char *option" "int val"
194 This library provides multiple outstanding asynchronous timers and I/O
195 to a cooperating application. The model is similar to that of the X
196 Toolkit, in that events are registered with the library and the application
197 spends most of its time in the
199 function. If an application already has a main loop, it can safely register
200 events with this library as long as it periodically calls the
204 functions. (Note that
206 has both polling and blocking modes.)
210 creates an event context which is needed by all the other functions in this
211 library. All information used internally by this library is bound to this
212 context, rather than to static storage. This makes the library
214 and permits other library functions to use events without
215 disrupting the application's use of events.
219 destroys a context that has been created by
221 All dynamic memory bound to this context will be freed. An implicit
223 will be done on all timers set in this event context. An implicit
225 will be done on all file descriptors selected in this event context.
229 potentially waits for and then retrieves the next asynchronous event,
230 placing it in the object of the
232 pointer argument. The following
238 should not block, but rather return
244 if no events have occurred;
248 to block internally until the next event occurs; and
252 that it should return a special
254 event, which is ignored by
256 but handled correctly by
259 can be necessary to the correct functioning of a caller\-written equivilent to
261 wherein perterbations caused by external system events must be polled for, and
262 the default behaviour of internally ignoring such events is undesirable.
267 are mutually exclusive.
271 dispatches an event retrieved by
273 This usually involves calling the function that was associated with the event
274 when the event was registered with
279 All events retrieved by
281 must be given over to
283 at some point, since there is some dynamic memory associated with each event.
287 deallocates dynamic memory that has been allocated by
291 has the side effect of calling
293 but if you are going to drop the event rather than dispatch it, you will have
301 .Bd -literal -offset indent
302 while ((x = evGetNext(opaqueCtx, &event, EV_WAIT)) == 0)
303 if ((x = evDispatch(opaqueCtx, event)) < 0)
308 In other words, get events and dispatch them until an error occurs. One such
309 error would be that all the events under this context become unregistered; in
310 that event, there will be nothing to wait for and
312 becomes an undefined operation.
317 .Dq Fa struct timespec
318 which allows these structures to be created and then passed as arguments to
319 other functions without the use of temporary variables. (If C had inline
320 constructors, there would be no need for this function.)
326 are utilities which allow the caller to convert a
327 .Dq Fa struct timeval
329 .Dq Fa struct timespec
332 or vice versa (the function of
334 Note that the name of the function indicates the type of the return value.
339 .Dq Fa struct timespec
340 values and returns the result as a
341 .Dq Fa struct timespec .
346 .Dq Fa struct timespec
347 argument from its first
348 .Dq Fa struct timespec
349 argument and returns the result as a
350 .Dq Fa struct timespec .
355 .Dq Fa struct timespec
356 arguments and returns an
358 that is less than zero if the first argument specifies an earlier time than
359 the second, or more than zero if the first argument specifies a later time
360 than the second, or equal to zero if both arguments specify the same time.
365 .Dq Fa struct timespec
366 which either describes the current time
368 .Xr clock_gettime 2 or
369 .Xr gettimeofday 2 ) ,
370 if successful, or has its fields set to zero, if there is an error.
371 (In the latter case, the caller can check
373 since it will be set by
374 .Xr gettimeofday 2 . )
375 The timestamp returned may not be UTC time if
376 the "monotime" option has been enabled with
383 except the result is always on the UTC timescale.
388 .Dq Fa struct timespec
389 which describes the last time that certain events happened to the
390 event context indicated by
392 This value is updated by
396 (upon entry and after
398 returns); it is routinely compared with other times in the internal handling
403 registers a timer event, which will be delivered as a function call to the
404 function specified by the
406 argument. The event will be delivered at absolute time
411 .Dq Fn evConsTime 0 0 ,
412 subsequently at intervals equal to time
414 As a special case, specifying a
417 .Dq Fn evConsTime 0 0
419 .Dq due immediately .
422 argument, if specified as a value other than
424 will be used to store the resulting
425 .Dq timer \s-1ID\s+1 ,
426 useful as an argument to
433 .Dq Fa evConsTime(0,0) )
436 should deallocate any dynamic memory that is uniquely bound to the
438 since no handles to this memory will exist within the event library
439 after a one\-shot timer has been delivered.
443 resets the values of the timer specified by
445 to the given arguments. The arguments are the same as in the description of
451 will unregister the timer event specified by
455 specified in the corresponding
457 call is uniquely bound to any dynamic memory, then that dynamic memory should
458 be freed by the caller before the handle is lost. After a call to
462 will exist within the event library.
466 can be used to manipulate other aspects of a timer.
467 Currently two modes are defined "rate" and "interval" which affect the
468 way recurrent timers are scheduled.
469 The default mode is "interval" where the event gets scheduled
471 after last time it was run.
472 If mode "rate" is selected the event gets scheduled
474 after last time it was scheduled.
475 For both "rate" and "interval" the numerical argument
481 is similar to (and built on)
483 it registers an idle timer event which provides for the function call to
485 to occur. However, for an
487 timer, the call will occur after at least
489 time has passed since the time the idle timer was
491 originally, this is set to the time returned by
493 (described above) for the event context specified by
497 timer, but the time at which the
499 is actually called can be changed by recourse to
501 (described below). The pointer to the underlying
506 .No non- Ns Dv NULL .
510 function updates the idle timer associated with
512 setting its idea of the time it was last accessed to the value returned by
514 (described above) for the event context specified by
516 This means that the idle timer will expire after at least
518 time has passed since this (possibly new) time, providing a caller mechanism
519 for resetting the call to the
521 associated with the idle timer. (See the description of
523 above, for information about
530 function reschedules a timer and resets the callback function and its argument.
531 Note that resetting a timer also ``touches'' it.
535 function unregisters the idle timer associated with
537 See the discussion under
539 above, for information regarding caller handling of the
541 associated with the corresponding
549 on the given event context's wait queue with the associated (possibly
555 .No non- Ns Dv NULL ,
556 then it will contain the
558 associated with the created queue element.
566 functions in the given event context's wait queue with the associated (possibly
569 as runnable. This places these functions in a
571 queue which will be read by
579 in the wait queue of the given event context; if an element with the given
581 is not found, then the
583 queue of that context is searched. If found, the queue element is removed
584 from the appropriate list.
588 causes a function (specified as
592 to be dispatched at some later time. Note that the
602 registers a file I/O event for the file descriptor specified by
611 At least one of these bits must be specified. If the
613 argument is not equal to
615 it will be used to store a unique ``file event \s-1ID\s+1'' for this event,
616 which is useful in subsequent calls to
618 A file descriptor will be made nonblocking using the
622 on its first concurrent registration via
626 remains in effect until cancelled via
631 unregisters the ``file event'' specified by the
633 argument. If the corresponding
635 uniquely points to dynamic memory, that memory should be freed before its
636 handle is lost, since after a call to
638 no handles to this event's
640 will remain within the event library. A file descriptor will be taken out of
641 nonblocking mode (see
645 when its last event registration is removed via
647 if it was in blocking mode before the first registration via
652 is a constructor for a single
654 structure, which is useful for
663 start asynchronous stream I/O operations on file descriptor
665 The data to be written or read is in the scatter/gather descriptor specified by
669 The supplied function
671 will be called with argument
673 when the I/O operation is complete. If
677 it will be filled a with the stream event identifier suitable for use with
682 extinguishes an outstanding
686 call. System I/O calls cannot always be cancelled, but you are guaranteed
693 will not be called after a call to
695 Care should be taken not to deallocate or otherwise reuse the space pointed
696 to by the segment descriptors in
698 unless the underlying file descriptor is closed first.
702 sets the stream associated with the given stream \s-1ID\s+1
704 to have the idle timer associated with the timer \s-1ID\s+1
709 says that the stream associated with the given stream \s-1ID\s+1
711 should ignore its idle timer, if present.
718 can be used to manage asynchronous incoming and outgoing socket connections.
719 Sockets to be used with these functions should first be created with
721 and given a local name with
723 It is extremely unlikely that the same socket will ever be
724 useful for both incoming and outgoing connections. The
734 variable which can then be used in a subsequent call to
739 each incoming connection arriving on
745 as one of its arguments.
747 initiates an outgoing connection on
749 to destination address
753 When the connection is complete,
757 as one of its arguments. The argument
763 if an error occurred that prevented this connection from completing
764 successfully. In this case
766 will have been set and the socket described by
768 will have been closed. The
770 function will prevent delivery of all pending and subsequent
771 events for the outstanding connection. The
773 function will suspend the acceptance of new connections on the listener
776 Connections will be queued by the protocol stack up to the system's limit. The
778 function will reverse the effects of
780 allowing incoming connections to be delivered for listener
784 function will poll the listener specified by
786 accepting a new connection if one is available, and queuing a connection event
787 for later retrieval by
789 If the connection event queued is an accept error(), sys_errno will contain
790 the error code; otherwise it will be zero. All connection events queued by
794 before a new select is done on the listener.
802 file handle for an event context. Greater numeric levels will
803 result in more verbose output being sent to the output FILE during program
808 prints a message with the format
810 and the following arguments (if any), on the output stream associated
811 with the event context pointed to by
813 The message is output if the event context's debug level is greater than
814 or equal to the indicated
819 will initialize an opaque
820 .Dq evConn \s-1ID\s+1 ,
821 .Dq evFile \s-1ID\s+1 ,
822 .Dq evStream \s-1ID\s+1 ,
823 .Dq evTimer \s-1ID\s+1 ,
824 .Dq evWait \s-1ID\s+1 ,
828 which is passed by reference to a state which
831 This is useful to make a handle as "not in use".
835 will examine an opaque \s-1ID\s+1 and return
837 only if it is not in its initialized state.
843 can be used to inspect and modify options.
844 Currently there is only one option, "monotime" and it is global for all
845 instances of eventlib so the ctx argument must be passed as NULL.
847 The default value for the "monotime" option is zero which selects
849 When set to a value of one, eventlib will use the
850 CLOCK_MONOTONIC timescale from
853 The CLOCK_MONOTONIC timescale is never stepped and should
854 run at a rate as close to TAI as possible, so it is unaffected
855 when the system clock is set.
856 If timerevents should run at a predictable rate, set the value
857 to one, of they should run at a predictable time of day, leave
859 If the CLOCK_MONOTONIC timescale is not available on the system,
860 attempts to set/get this option will fail.
862 All the functions whose return type is
864 use the standard convention of returning zero (0) to indicate success, or
874 directory of the current
878 The possible values for
882 functions in this library returns
884 include those of the Standard C Library and also:
885 .Bl -tag -width EWOULDBLOCKAA
887 Some function argument has an unreasonable value.
889 The specified file descriptor has an integer value greater than the default
891 meaning that the application's limit is higher than the library's.
896 .It Bq Er EWOULDBLOCK
897 No events have occurred and the
899 option was specified.
901 The specified signal was unblocked outside the library.
908 .Xr @INDOT@named @SYS_OPS_EXT@ ,
912 This huge man page needs to be broken up into a handful of smaller ones.
916 library was designed by Paul Vixie with excellent advice from his friends
917 and with tips 'o the cap to the X Consortium and the implementors of DEC SRC