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: src/lib/libc/sys/kqueue.2,v 1.1.2.16 2002/07/02 21:05:08 mp Exp $ 26.\" 27.Dd November 22, 2017 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.In sys/types.h 38.In sys/event.h 39.In sys/time.h 40.Ft int 41.Fn kqueue "void" 42.Ft int 43.Fn kevent "int kq" "const struct kevent *changelist" "int nchanges" "struct kevent *eventlist" "int nevents" "const struct timespec *timeout" 44.Fn EV_SET "&kev" ident filter flags fflags data udata 45.Sh DESCRIPTION 46.Fn kqueue 47provides a generic method of notifying the user when an event 48happens or a condition holds, based on the results of small 49pieces of kernel code termed filters. 50A kevent is identified by the (ident, filter) pair; there may only 51be one unique kevent per kqueue. 52.Pp 53The filter is executed upon the initial registration of a kevent 54in order to detect whether a preexisting condition is present, and is also 55executed whenever an event is passed to the filter for evaluation. 56If the filter determines that the condition should be reported, 57then the kevent is placed on the kqueue for the user to retrieve. 58.Pp 59The filter is also run when the user attempts to retrieve the kevent 60from the kqueue. 61If the filter indicates that the condition that triggered 62the event no longer holds, the kevent is removed from the kqueue and 63is not returned. 64.Pp 65Multiple events which trigger the filter do not result in multiple 66kevents being placed on the kqueue; instead, the filter will aggregate 67the events into a single struct kevent. 68Calling 69.Fn close 70on a file descriptor will remove any kevents that reference the descriptor. 71.Pp 72.Fn kqueue 73creates a new kernel event queue and returns a descriptor. 74The queue is not inherited by a child created with 75.Xr fork 2 . 76However, if 77.Xr rfork 2 78is called without the 79.Dv RFFDG 80flag, then the descriptor table is shared, 81which will allow sharing of the kqueue between two processes. 82.Pp 83.Fn kevent 84is used to register events with the queue, and return any pending 85events to the user. 86.Fa changelist 87is a pointer to an array of 88.Vt kevent 89structures, as defined in 90.In sys/event.h . 91All changes contained in the 92.Fa changelist 93are applied before any pending events are read from the queue. 94.Fa nchanges 95gives the size of 96.Fa changelist . 97.Fa eventlist 98is a pointer to an array of kevent structures. 99.Fa nevents 100determines the size of 101.Fa eventlist . 102If 103.Fa timeout 104is a non-NULL pointer, it specifies a maximum interval to wait 105for an event, which will be interpreted as a struct timespec. 106If 107.Fa timeout 108is a NULL pointer, 109.Fn kevent 110waits indefinitely. 111To effect a poll, the 112.Fa timeout 113argument should be non-NULL, pointing to a zero-valued 114.Vt timespec 115structure. 116The same array may be used for the 117.Fa changelist 118and 119.Fa eventlist . 120.Pp 121.Fn EV_SET 122is a macro which is provided for ease of initializing a 123kevent structure. 124.Pp 125The 126.Vt kevent 127structure is defined as: 128.Bd -literal 129struct kevent { 130 uintptr_t ident; /* identifier for this event */ 131 short filter; /* filter for event */ 132 u_short flags; /* action flags for kqueue */ 133 u_int fflags; /* filter flag value */ 134 intptr_t data; /* filter data value */ 135 void *udata; /* opaque user data identifier */ 136}; 137.Ed 138.Pp 139The fields of 140.Fa struct kevent 141are: 142.Bl -tag -width XXXfilter 143.It Fa ident 144Value used to identify this event. 145The exact interpretation is determined by the attached filter, 146but often is a file descriptor. 147.It Fa filter 148Identifies the kernel filter used to process this event. 149The pre-defined system filters are described below. 150.It Fa flags 151Actions to perform on the event. 152.It Fa fflags 153Filter-specific flags. 154.It Fa data 155Filter-specific data value. 156.It Fa udata 157Opaque user-defined value passed through the kernel unchanged. 158.El 159.Pp 160The 161.Fa flags 162field can contain the following values: 163.Bl -tag -width ".Dv EV_DISPATCH" 164.It Dv EV_ADD 165Adds the event to the kqueue. 166Re-adding an existing event will modify the parameters of the original 167event, and not result in a duplicate entry. 168Adding an event automatically enables it, unless overridden by the 169.Dv EV_DISABLE 170flag. 171.It Dv EV_ENABLE 172Permit 173.Fn kevent 174to return the event if it is triggered. 175.It Dv EV_DISABLE 176Disable the event so 177.Fn kevent 178will not return it. 179The filter itself is not disabled. 180.It Dv EV_DISPATCH 181Disable the event source immediately after delivery of an event. 182See 183.Dv EV_DISABLE 184above. 185.It Dv EV_DELETE 186Removes the event from the kqueue. 187Events which are attached to file descriptors are automatically 188deleted on the last close of the descriptor. 189.It Dv EV_RECEIPT 190This flag is useful for making bulk changes to a kqueue without draining 191any pending events. 192When passed as input, it forces 193.Dv EV_ERROR 194to always be returned. 195When a filter is successfully added the 196.Fa data 197field will be zero. 198.It Dv EV_ONESHOT 199Causes the event to return only the first occurrence of the filter 200being triggered. 201After the user retrieves the event from the kqueue, it is deleted. 202.It Dv EV_CLEAR 203After the event is retrieved by the user, its state is reset. 204This is useful for filters which report state transitions 205instead of the current state. 206Note that some filters may automatically set this flag internally. 207.It Dv EV_EOF 208Filters may set this flag to indicate filter-specific EOF condition. 209.It Dv EV_NODATA 210Filters may set this flag in addition to 211.Dv EV_EOF 212to indicate that there is no more data pending in the buffer. 213.It Dv EV_ERROR 214See 215.Sx RETURN VALUES 216below. 217.El 218.Pp 219The predefined system filters are listed below. 220Arguments may be passed to and from the filter via the 221.Fa fflags 222and 223.Fa data 224fields in the kevent structure. 225.Bl -tag -width ".Dv EVFILT_SIGNAL" 226.It Dv EVFILT_READ 227Takes a descriptor as the identifier, and returns whenever 228there is data available to read. 229The behavior of the filter is slightly different depending 230on the descriptor type. 231.Bl -tag -width 2n 232.It Sockets 233Sockets which have previously been passed to 234.Fn listen 235return when there is an incoming connection pending. 236.Fa data 237contains the size of the listen backlog. 238.Pp 239Other socket descriptors return when there is data to be read, 240subject to the 241.Dv SO_RCVLOWAT 242value of the socket buffer. 243This may be overridden with a per-filter low water mark at the 244time the filter is added by setting the 245.Dv NOTE_LOWAT 246flag in 247.Fa fflags , 248and specifying the new low water mark in 249.Fa data . 250On return, 251.Fa data 252contains the number of bytes in the socket buffer. 253.Pp 254If the read direction of the socket has shutdown, then the filter also sets 255.Dv EV_EOF 256in 257.Fa flags , 258and returns the socket error (if any) in 259.Fa fflags . 260It is possible for EOF to be returned (indicating the connection is gone) 261while there is still data pending in the socket buffer. 262.It Vnodes 263Returns when the file pointer is not at the end of file. 264.Fa data 265contains the offset from current position to end of file, 266and may be negative. 267.It "Fifos, Pipes" 268Returns when the there is data to read; 269.Fa data 270contains the number of bytes available. 271.Pp 272When the last writer disconnects, the filter will set 273.Dv EV_EOF 274in 275.Fa flags . 276This will be cleared by the filter when a new writer connects, 277at which point the filter will resume waiting for data to become 278available before returning. 279.El 280.It Dv EVFILT_WRITE 281Takes a descriptor as the identifier, and returns whenever 282it is possible to write to the descriptor. 283For sockets, pipes and fifos, 284.Fa data 285will contain the amount of space remaining in the write buffer. 286The filter will set 287.Dv EV_EOF 288when the reader disconnects, and for the fifo case, this will be cleared 289when a new reader connects. 290Note that this filter is not supported for vnodes. 291.Pp 292For sockets, the low water mark and socket error handling is 293identical to the 294.Dv EVFILT_READ 295case. 296.It Dv EVFILT_EXCEPT 297Takes a descriptor as the identifier, and returns whenever one of the 298specified exceptional conditions has occurred on the descriptor. 299Conditions are specified in 300.Fa fflags . 301Currently, a filter can monitor the reception of out-of-band data with 302.Dv NOTE_OOB . 303.It Dv EVFILT_AIO 304The sigevent portion of the AIO request is filled in, with 305.Fa sigev_notify_kqueue 306containing the descriptor of the kqueue that the event should 307be attached to, 308.Fa sigev_value 309containing the udata value, and 310.Fa sigev_notify 311set to 312.Dv SIGEV_KEVENT . 313When the aio_* function is called, the event will be registered 314with the specified kqueue, and the 315.Fa ident 316argument set to the 317.Fa struct aiocb 318returned by the aio_* function. 319The filter returns under the same conditions as 320.Fn aio_error . 321.Pp 322Alternatively, a kevent structure may be initialized, with 323.Fa ident 324containing the descriptor of the kqueue, and the 325address of the kevent structure placed in the 326.Fa aio_lio_opcode 327field of the AIO request. 328However, this approach will not work on architectures with 64-bit 329pointers, and should be considered deprecated. 330.It Dv EVFILT_VNODE 331Takes a file descriptor as the identifier and the events to watch for in 332.Fa fflags , 333and returns when one or more of the requested events occurs on the descriptor. 334The events to monitor are: 335.Bl -tag -width ".Dv NOTE_RENAME" 336.It Dv NOTE_DELETE 337.Fn unlink 338was called on the file referenced by the descriptor. 339.It Dv NOTE_WRITE 340A write occurred on the file referenced by the descriptor. 341.It Dv NOTE_EXTEND 342The file referenced by the descriptor was extended. 343.It Dv NOTE_ATTRIB 344The file referenced by the descriptor had its attributes changed. 345.It Dv NOTE_LINK 346The link count on the file changed. 347.It Dv NOTE_RENAME 348The file referenced by the descriptor was renamed. 349.It Dv NOTE_REVOKE 350Access to the file was revoked via 351.Xr revoke 2 352or the underlying fileystem was unmounted. 353.El 354.Pp 355On return, 356.Fa fflags 357contains the events which triggered the filter. 358.It Dv EVFILT_PROC 359Takes the process ID to monitor as the identifier and the events to watch for 360in 361.Fa fflags , 362and returns when the process performs one or more of the requested events. 363If a process can normally see another process, it can attach an event to it. 364The events to monitor are: 365.Bl -tag -width ".Dv NOTE_TRACKERR" 366.It Dv NOTE_EXIT 367The process has exited. 368.It Dv NOTE_FORK 369The process has called 370.Fn fork . 371.It Dv NOTE_EXEC 372The process has executed a new process via 373.Xr execve 2 374or similar call. 375.It Dv NOTE_TRACK 376Follow a process across 377.Fn fork 378calls. 379The parent process will return with 380.Dv NOTE_TRACK 381set in the 382.Fa fflags 383field, while the child process will return with 384.Dv NOTE_CHILD 385set in 386.Fa fflags 387and the parent PID in 388.Fa data . 389.It Dv NOTE_TRACKERR 390This flag is returned if the system was unable to attach an event to 391the child process, usually due to resource limitations. 392.El 393.Pp 394On return, 395.Fa fflags 396contains the events which triggered the filter. 397.It Dv EVFILT_SIGNAL 398Takes the signal number to monitor as the identifier and returns 399when the given signal is delivered to the process. 400This coexists with the 401.Fn signal 402and 403.Fn sigaction 404facilities, and has a lower precedence. 405The filter will record all attempts to deliver a signal to a process, 406even if the signal has been marked as 407.Dv SIG_IGN , 408or has been masked by 409.Fn sigprocmask . 410Event notification happens after normal signal delivery processing. 411.Fa data 412returns the number of times the signal has occurred since the last call to 413.Fn kevent . 414This filter automatically sets the 415.Dv EV_CLEAR 416flag internally. 417.It Dv EVFILT_TIMER 418Establishes an arbitrary timer identified by 419.Fa ident . 420When adding a timer, 421.Fa data 422specifies the timeout period in milliseconds. 423The timer will be periodic unless 424.Dv EV_ONESHOT 425is specified. 426On return, 427.Fa data 428contains the number of times the timeout has expired since the last call to 429.Fn kevent . 430This filter automatically sets the 431.Dv EV_CLEAR 432flag internally. 433.It Dv EVFILT_FS 434Establishes a file system monitor. 435Currently it only monitors file system mount and unmount actions. 436.El 437.Sh RETURN VALUES 438.Fn kqueue 439creates a new kernel event queue and returns a file descriptor. 440If there was an error creating the kernel event queue, a value of -1 is 441returned and 442.Va errno 443set. 444.Pp 445.Fn kevent 446returns the number of events placed in the 447.Fa eventlist , 448up to the value given by 449.Fa nevents . 450If an error occurs while processing an element of the 451.Fa changelist 452and there is enough room in the 453.Fa eventlist , 454then the event will be placed in the 455.Fa eventlist 456with 457.Dv EV_ERROR 458set in 459.Fa flags 460and the system error in 461.Fa data . 462Otherwise, 463.Dv -1 464will be returned, and 465.Va errno 466will be set to indicate the error condition. 467If the time limit expires, then 468.Fn kevent 469returns 0. 470.Sh ERRORS 471The 472.Fn kqueue 473function fails if: 474.Bl -tag -width Er 475.It Bq Er ENOMEM 476The kernel failed to allocate enough memory for the kernel queue. 477.It Bq Er EMFILE 478The per-process descriptor table is full. 479.It Bq Er ENFILE 480The system file table is full. 481.El 482.Pp 483The 484.Fn kevent 485function fails if: 486.Bl -tag -width Er 487.It Bq Er EACCES 488The process does not have permission to register a filter. 489.It Bq Er EFAULT 490There was an error reading or writing the 491.Vt kevent 492structure. 493.It Bq Er EBADF 494The specified descriptor is invalid. 495.It Bq Er EINTR 496A signal was delivered before the timeout expired and before any 497events were placed on the kqueue for return. 498.It Bq Er EINVAL 499The specified time limit or filter is invalid. 500.It Bq Er ENOENT 501The event could not be found to be modified or deleted. 502.It Bq Er ENOMEM 503No memory was available to register the event. 504.It Bq Er ESRCH 505The specified process to attach to does not exist. 506.El 507.Sh SEE ALSO 508.Xr poll 2 , 509.Xr read 2 , 510.Xr select 2 , 511.Xr sigaction 2 , 512.Xr sigprocmask 2 , 513.Xr write 2 , 514.Xr aio_error 3 , 515.Xr aio_read 3 , 516.Xr aio_return 3 , 517.Xr signal 3 518.Sh HISTORY 519The 520.Fn kqueue 521and 522.Fn kevent 523functions first appeared in 524.Fx 4.1 . 525.Sh AUTHORS 526The 527.Fn kqueue 528system and this manual page were written by 529.An Jonathan Lemon Aq Mt jlemon@FreeBSD.org . 530.Sh BUGS 531Currently it is only possible to watch a 532.Xr vnode 9 533on 534.Xr HAMMER 5 , 535.Xr tmpfs 5 536and 537.Xr UFS 5 538file systems. 539