1.\" Copyright (c) 2000 Jonathan Lemon 2.\" All rights reserved. 3.\" 4.\" Redistribution and use in source and binary forms, with or without 5.\" modification, are permitted provided that the following conditions 6.\" are met: 7.\" 1. Redistributions of source code must retain the above copyright 8.\" notice, this list of conditions and the following disclaimer. 9.\" 2. Redistributions in binary form must reproduce the above copyright 10.\" notice, this list of conditions and the following disclaimer in the 11.\" documentation and/or other materials provided with the distribution. 12.\" 13.\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND 14.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 17.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23.\" SUCH DAMAGE. 24.\" 25.\" $FreeBSD$ 26.\" 27.Dd April 14, 2000 28.Dt KQUEUE 2 29.Os 30.Sh NAME 31.Nm kqueue , 32.Nm kevent 33.Nd kernel event notification mechanism 34.Sh LIBRARY 35.Lb libc 36.Sh SYNOPSIS 37.Fd #include <sys/event.h> 38.Ft int 39.Fn kqueue "void" 40.Ft int 41.Fn kevent "int kq" "int nchanges" "struct kevent **changelist" \ 42"int nevents" "struct kevent *eventlist" "struct timespec *timeout" 43.Sh DESCRIPTION 44.Fn kqueue 45provides a generic method of notifying the user when an event 46happens or a condition holds, based on the results of small 47pieces of kernel code termed filters. 48An kevent is identified by the (ident, filter) pair; there may only 49be one unique kevent per kqueue. 50.Pp 51The filter is executed upon the initial registration of a kevent 52in order to detect a preexisting condition is present, and is also 53executed whenever an event is passed to the filter for evaluation. 54If the filter determines that the condition should be reported, 55then the kevent is placed on the kqueue for the user to retrieve. 56.Pp 57The filter is also run when the user attempts to retrieve the kevent 58from the kqueue, and if the filter indicates the condition that triggered 59the event no longer holds, the kevent is removed from the kqueue and 60is not returned. 61.Pp 62Multiple events which trigger the filter do not result in multiple 63kevents being placed on the kqueue; instead, the filter will aggregate 64the events into a single struct kevent. 65Calling 66.Fn close 67on a file descriptor will remove any kevents that reference the descriptor. 68.Pp 69.Fn kqueue 70creates a new kernel event queue and returns a descriptor. 71.Pp 72.Fn kevent 73is used to register events with the queue, and return any pending 74events to the user. 75.Fa changelist 76is a pointer to an array of pointers to 77.Va kevent 78structures, as defined in 79.Aq Pa event.h . 80All changes contained in the 81.Fa changelist 82are applied before any pending events are read from the queue. 83.Fa nchanges 84gives the size of 85.Fa changelist . 86.Fa eventlist 87is a pointer to an array of kevent structures. 88.Fa nevents 89determines the size of 90.Fa eventlist . 91If 92.Fa timeout 93is a non-NULL pointer, it specifies a maximum interval to wait 94for an event. If 95.Fa timeout 96is a NULL pointer, 97.Fn kevent 98waits indefinitely. To effect a poll, the 99.Fa timeout 100argument should be non-NULL, pointing to a zero-valued 101.Va timespec 102structure. 103.Pp 104The 105.Va kevent 106structure is defined as: 107.Bd -literal 108struct kevent { 109 uintptr_t ident; /* identifier for this event */ 110 short filter; /* filter for event */ 111 u_short flags; /* action flags for kqueue */ 112 u_int fflags; /* filter flag value */ 113 intptr_t data; /* filter data value */ 114 void *udata; /* opaque user data identifier */ 115}; 116.Ed 117.Pp 118The fields of 119.Fa struct kevent 120are: 121.Bl -tag -width XXXfilter 122.It ident 123Value used to identify this event. 124The exact interpretation is determined by the attached filter, 125but often is a file descriptor. 126.It filter 127Identifies the kernel filter used to process this event. The pre-defined 128system filters are described below. 129.It flags 130Actions to perform on the event. 131.It fflags 132Filter-specific flags. 133.It data 134Filter-specific data value. 135.It udata 136Opaque user-defined value passed through the kernel unchanged. 137.El 138.Pp 139The 140.Va flags 141field can contain the following values: 142.Bl -tag -width XXXEV_ONESHOT 143.It EV_ADD 144Adds the event to the kqueue. Re-adding an existing event 145will modify the parameters of the original event, and not result 146in a duplicate entry. Adding an event automatically enables it, 147unless overridden by the EV_DISABLE flag. 148.It EV_ENABLE 149Permit 150.Fn kevent 151to return the event if it is triggered. 152.It EV_DISABLE 153Disable the event so 154.Fn kevent 155will not return it. The filter itself is not disabled. 156.It EV_DELETE 157Removes the event from the kqueue. Events which are attached to 158file descriptors are automatically deleted on the last close of 159the descriptor. 160.It EV_ONESHOT 161Causes the event to return only the first occurrence of the filter 162being triggered. After the user retrieves the event from the kqueue, 163it is deleted. 164.It EV_CLEAR 165After the event is retrieved by the user, its state is reset. 166This is useful for filters which report state transitions 167instead of the current state. Note that some filters may automatically 168set this flag internally. 169.It EV_EOF 170Filters may set this flag to indicate filter-specific EOF condition. 171.It EV_ERROR 172See 173.Sx RETURN VALUES 174below. 175.El 176.Pp 177The predefined system filters are listed below. 178Arguments may be passed to and from the filter via the 179.Va fflags 180and 181.Va data 182fields in the kevent structure. 183.Bl -tag 184.It EVFILT_READ 185Takes a descriptor as the identifier, and returns whenever 186there is data available to read. 187The behavior of the filter is slightly different depending 188on the descriptor type. 189.Bl -tag 190.It Sockets 191Sockets which have previously been passed to 192.Fn listen 193return when there is an incoming connection pending. 194.Va data 195contains the size of the listen backlog. 196.Pp 197Other socket descriptors return when there is data to be read, 198subject to the SO_RCVLOWAT value of the socket buffer. 199.Va data 200contains the number of bytes in the socket buffer. 201.Pp 202If the read direction of the socket has shutdown, then the filter 203also sets EV_EOF in 204.Va flags . 205It is possible for EOF to be returned (indicating the connection is gone) 206while there is still data pending in the socket buffer. 207.El 208.Bl -tag 209.It Vnodes 210Returns when the file pointer is not at the end of file. 211.Va data 212contains the offset from current position to end of file, 213and may be negative. 214.El 215.Bl -tag 216.It Fifos, Pipes 217Returns when the there is data to read; 218.Va data 219contains the number of bytes available. 220.Pp 221When the last writer disconnects, the filter will set EV_EOF in 222.Va flags . 223This may be cleared by passing in EV_CLEAR, at which point the 224filter will resume waiting for data to become available before 225returning. 226.El 227.El 228.Bl -tag 229.It EVFILT_WRITE 230Takes a descriptor as the identifier, and returns whenever 231it is possible to write to the descriptor. For sockets, pipes 232and fifos, 233.Va data 234will contain the amount of space remaining in the write buffer. 235The filter will set EV_EOF when the reader disconnects, and for 236the fifo case, this may be cleared by use of EV_CLEAR. 237Note that this filter is not supported for vnodes. 238.El 239.Bl -tag 240.It EVFILT_AIO 241A kevent structure is initialized, with 242.Va ident 243containing the descriptor of the kqueue that the event should be 244attached to. The address of the kevent structure is then placed in the 245.Va aio_lio_opcode 246field of the AIO request, and the aio_* function is then called. 247The event will be registered with the specified kqueue, and the 248.Va ident 249argument set to the 250.Fa struct aiocb 251returned by the aio_* function. 252The filter returns under the same conditions as aio_error. 253.Pp 254NOTE: this interface is unstable and subject to change. 255.El 256.Bl -tag 257.It EVFILT_VNODE 258Takes a file descriptor as the identifier and the events to watch for in 259.Va fflags , 260and returns when one or more of the requested events occurs on the descriptor. 261The events to monitor are: 262.Bl -tag -width XXNOTE_RENAME 263.It NOTE_DELETE 264.Fn unlink 265was called on the file referenced by the descriptor. 266.It NOTE_WRITE 267A write occurred on the file referenced by the descriptor. 268.It NOTE_EXTEND 269The file referenced by the descriptor was extended. 270.It NOTE_ATTRIB 271The file referenced by the descriptor had its attributes changed. 272.It NOTE_LINK 273The link count on the file changed. 274.It NOTE_RENAME 275The file referenced by the descriptor was renamed. 276.El 277.Pp 278On return, 279.Va fflags 280contains the events which triggered the filter. 281.El 282.Bl -tag 283.It EVFILT_PROC 284Takes the process ID to monitor as the identifier and the events to watch for 285in 286.Va fflags , 287and returns when the process performs one or more of the requested events. 288If a process can normally see another process, it can attach an event to it. 289The events to monitor are: 290.Bl -tag -width XXNOTE_TRACKERR 291.It NOTE_EXIT 292The process has exited. 293.It NOTE_FORK 294The process has called 295.Fn fork . 296.It NOTE_EXEC 297The process has executed a new process via 298.Xr execve 2 299or similar call. 300.It NOTE_TRACK 301Follow a process across 302.Fn fork 303calls. The parent process will return with NOTE_TRACK set in the 304.Va fflags 305field, while the child process will return with NOTE_CHILD set in 306.Va fflags 307and the parent PID in 308.Va data . 309.It NOTE_TRACKERR 310This flag is returned if the system was unable to attach an event to 311the child process, usually due to resource limitations. 312.El 313.Pp 314On return, 315.Va fflags 316contains the events which triggered the filter. 317.It EVFILT_SIGNAL 318Takes the signal number to monitor as the identifier and returns 319when the given signal is delivered to the process. 320This coexists with the 321.Fn signal 322and 323.Fn sigaction 324facilities, and has a lower precedence. The filter will record 325all attempts to deliver a signal to a process, even if the signal has 326been marked as SIG_IGN. Event notification happens after normal 327signal delivery processing. 328.Va data 329returns the number of times the signal has occurred since the last call to 330.Fn kqueue . 331This filter automatically sets the EV_CLEAR flag internally. 332.El 333.Sh RETURN VALUES 334.Fn kevent 335returns the number of events placed in the 336.Ar eventlist , 337up to the value given by 338.Ar nevents . 339If an error occurs while processing an element of the 340.Ar changelist 341and there is enough room in the 342.Ar eventlist , 343then the event will be placed in the 344.Ar eventlist 345with 346.Dv EV_ERROR 347set in 348.Va flags 349and the system error in 350.Va data . 351Otherwise, 352.Dv -1 353will be returned, and 354.Dv errno 355will be set to indicate the error condition. 356If the time limit expires, then 357.Fn kevent 358returns 0. 359.Sh ERRORS 360The 361.Fn kevent 362function fails if: 363.Bl -tag -width Er 364.It Bq Er EACCESS 365The process does not have permission to register a filter. 366.It Bq Er EFAULT 367There was an error reading or writing the 368.Va kevent 369structure. 370.It Bq Er EBADF 371The specified descriptor is invalid. 372.It Bq Er EINTR 373A signal was delivered before the timeout expired and before any 374events were placed on the kqueue for return. 375.It Bq Er EINVAL 376The specified time limit or filter is invalid. 377.It Bq Er ENOMEM 378No memory was available to register the event. 379.It Bq Er ESRCH 380The specified process to attach to does not exist. 381.El 382.Sh SEE ALSO 383.Xr aio_error 2 , 384.Xr aio_read 2 , 385.Xr aio_return 2 , 386.Xr poll 2 , 387.Xr read 2 , 388.Xr select 2 , 389.Xr signal 3 , 390.Xr sigaction 2 , 391.Xr write 2 392.Sh HISTORY 393The 394.Fn kqueue 395and 396.Fn kevent 397functions first appeared in 398.Fx 5.0 . 399.Sh AUTHORS 400The 401.Fn kqueue 402system and this manual page were written by 403.An Jonathan Lemon Aq [email protected] . 404