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 June 8, 2016 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_ERROR 210See 211.Sx RETURN VALUES 212below. 213.El 214.Pp 215The predefined system filters are listed below. 216Arguments may be passed to and from the filter via the 217.Fa fflags 218and 219.Fa data 220fields in the kevent structure. 221.Bl -tag -width ".Dv EVFILT_SIGNAL" 222.It Dv EVFILT_READ 223Takes a descriptor as the identifier, and returns whenever 224there is data available to read. 225The behavior of the filter is slightly different depending 226on the descriptor type. 227.Bl -tag -width 2n 228.It Sockets 229Sockets which have previously been passed to 230.Fn listen 231return when there is an incoming connection pending. 232.Fa data 233contains the size of the listen backlog. 234.Pp 235Other socket descriptors return when there is data to be read, 236subject to the 237.Dv SO_RCVLOWAT 238value of the socket buffer. 239This may be overridden with a per-filter low water mark at the 240time the filter is added by setting the 241.Dv NOTE_LOWAT 242flag in 243.Fa fflags , 244and specifying the new low water mark in 245.Fa data . 246On return, 247.Fa data 248contains the number of bytes in the socket buffer. 249.Pp 250If the read direction of the socket has shutdown, then the filter also sets 251.Dv EV_EOF 252in 253.Fa flags , 254and returns the socket error (if any) in 255.Fa fflags . 256It is possible for EOF to be returned (indicating the connection is gone) 257while there is still data pending in the socket buffer. 258.It Vnodes 259Returns when the file pointer is not at the end of file. 260.Fa data 261contains the offset from current position to end of file, 262and may be negative. 263.It "Fifos, Pipes" 264Returns when the there is data to read; 265.Fa data 266contains the number of bytes available. 267.Pp 268When the last writer disconnects, the filter will set 269.Dv EV_EOF 270in 271.Fa flags . 272This may be cleared by passing in 273.Dv EV_CLEAR , 274at which point the filter will resume waiting for data to become 275available before returning. 276.El 277.It Dv EVFILT_WRITE 278Takes a descriptor as the identifier, and returns whenever 279it is possible to write to the descriptor. 280For sockets, pipes and fifos, 281.Fa data 282will contain the amount of space remaining in the write buffer. 283The filter will set 284.Dv EV_EOF 285when the reader disconnects, and for the fifo case, this may be cleared 286by use of 287.Dv EV_CLEAR . 288Note that this filter is not supported for vnodes. 289.Pp 290For sockets, the low water mark and socket error handling is 291identical to the 292.Dv EVFILT_READ 293case. 294.It Dv EVFILT_EXCEPT 295Takes a descriptor as the identifier, and returns whenever one of the 296specified exceptional conditions has occurred on the descriptor. 297Conditions are specified in 298.Fa fflags . 299Currently, a filter can monitor the reception of out-of-band data with 300.Dv NOTE_OOB . 301.It Dv EVFILT_AIO 302The sigevent portion of the AIO request is filled in, with 303.Fa sigev_notify_kqueue 304containing the descriptor of the kqueue that the event should 305be attached to, 306.Fa sigev_value 307containing the udata value, and 308.Fa sigev_notify 309set to 310.Dv SIGEV_KEVENT . 311When the aio_* function is called, the event will be registered 312with the specified kqueue, and the 313.Fa ident 314argument set to the 315.Fa struct aiocb 316returned by the aio_* function. 317The filter returns under the same conditions as 318.Fn aio_error . 319.Pp 320Alternatively, a kevent structure may be initialized, with 321.Fa ident 322containing the descriptor of the kqueue, and the 323address of the kevent structure placed in the 324.Fa aio_lio_opcode 325field of the AIO request. 326However, this approach will not work on architectures with 64-bit 327pointers, and should be considered deprecated. 328.It Dv EVFILT_VNODE 329Takes a file descriptor as the identifier and the events to watch for in 330.Fa fflags , 331and returns when one or more of the requested events occurs on the descriptor. 332The events to monitor are: 333.Bl -tag -width ".Dv NOTE_RENAME" 334.It Dv NOTE_DELETE 335.Fn unlink 336was called on the file referenced by the descriptor. 337.It Dv NOTE_WRITE 338A write occurred on the file referenced by the descriptor. 339.It Dv NOTE_EXTEND 340The file referenced by the descriptor was extended. 341.It Dv NOTE_ATTRIB 342The file referenced by the descriptor had its attributes changed. 343.It Dv NOTE_LINK 344The link count on the file changed. 345.It Dv NOTE_RENAME 346The file referenced by the descriptor was renamed. 347.It Dv NOTE_REVOKE 348Access to the file was revoked via 349.Xr revoke 2 350or the underlying fileystem was unmounted. 351.El 352.Pp 353On return, 354.Fa fflags 355contains the events which triggered the filter. 356.It Dv EVFILT_PROC 357Takes the process ID to monitor as the identifier and the events to watch for 358in 359.Fa fflags , 360and returns when the process performs one or more of the requested events. 361If a process can normally see another process, it can attach an event to it. 362The events to monitor are: 363.Bl -tag -width ".Dv NOTE_TRACKERR" 364.It Dv NOTE_EXIT 365The process has exited. 366.It Dv NOTE_FORK 367The process has called 368.Fn fork . 369.It Dv NOTE_EXEC 370The process has executed a new process via 371.Xr execve 2 372or similar call. 373.It Dv NOTE_TRACK 374Follow a process across 375.Fn fork 376calls. 377The parent process will return with 378.Dv NOTE_TRACK 379set in the 380.Fa fflags 381field, while the child process will return with 382.Dv NOTE_CHILD 383set in 384.Fa fflags 385and the parent PID in 386.Fa data . 387.It Dv NOTE_TRACKERR 388This flag is returned if the system was unable to attach an event to 389the child process, usually due to resource limitations. 390.El 391.Pp 392On return, 393.Fa fflags 394contains the events which triggered the filter. 395.It Dv EVFILT_SIGNAL 396Takes the signal number to monitor as the identifier and returns 397when the given signal is delivered to the process. 398This coexists with the 399.Fn signal 400and 401.Fn sigaction 402facilities, and has a lower precedence. 403The filter will record all attempts to deliver a signal to a process, 404even if the signal has been marked as 405.Dv SIG_IGN , 406or has been masked by 407.Fn sigpromask . 408Event notification happens after normal signal delivery processing. 409.Fa data 410returns the number of times the signal has occurred since the last call to 411.Fn kevent . 412This filter automatically sets the 413.Dv EV_CLEAR 414flag internally. 415.It Dv EVFILT_TIMER 416Establishes an arbitrary timer identified by 417.Fa ident . 418When adding a timer, 419.Fa data 420specifies the timeout period in milliseconds. 421The timer will be periodic unless 422.Dv EV_ONESHOT 423is specified. 424On return, 425.Fa data 426contains the number of times the timeout has expired since the last call to 427.Fn kevent . 428This filter automatically sets the 429.Dv EV_CLEAR 430flag internally. 431.It Dv EVFILT_FS 432Establishes a file system monitor identified by 433.Fa ident . 434Currently it only monitors file system mount and unmount actions. 435.El 436.Sh RETURN VALUES 437.Fn kqueue 438creates a new kernel event queue and returns a file descriptor. 439If there was an error creating the kernel event queue, a value of -1 is 440returned and 441.Va errno 442set. 443.Pp 444.Fn kevent 445returns the number of events placed in the 446.Fa eventlist , 447up to the value given by 448.Fa nevents . 449If an error occurs while processing an element of the 450.Fa changelist 451and there is enough room in the 452.Fa eventlist , 453then the event will be placed in the 454.Fa eventlist 455with 456.Dv EV_ERROR 457set in 458.Fa flags 459and the system error in 460.Fa data . 461Otherwise, 462.Dv -1 463will be returned, and 464.Va errno 465will be set to indicate the error condition. 466If the time limit expires, then 467.Fn kevent 468returns 0. 469.Sh ERRORS 470The 471.Fn kqueue 472function fails if: 473.Bl -tag -width Er 474.It Bq Er ENOMEM 475The kernel failed to allocate enough memory for the kernel queue. 476.It Bq Er EMFILE 477The per-process descriptor table is full. 478.It Bq Er ENFILE 479The system file table is full. 480.El 481.Pp 482The 483.Fn kevent 484function fails if: 485.Bl -tag -width Er 486.It Bq Er EACCES 487The process does not have permission to register a filter. 488.It Bq Er EFAULT 489There was an error reading or writing the 490.Vt kevent 491structure. 492.It Bq Er EBADF 493The specified descriptor is invalid. 494.It Bq Er EINTR 495A signal was delivered before the timeout expired and before any 496events were placed on the kqueue for return. 497.It Bq Er EINVAL 498The specified time limit or filter is invalid. 499.It Bq Er ENOENT 500The event could not be found to be modified or deleted. 501.It Bq Er ENOMEM 502No memory was available to register the event. 503.It Bq Er ESRCH 504The specified process to attach to does not exist. 505.El 506.Sh SEE ALSO 507.Xr poll 2 , 508.Xr read 2 , 509.Xr select 2 , 510.Xr sigaction 2 , 511.Xr sigprocmask 2 , 512.Xr write 2 , 513.Xr aio_error 3 , 514.Xr aio_read 3 , 515.Xr aio_return 3 , 516.Xr signal 3 517.Sh HISTORY 518The 519.Fn kqueue 520and 521.Fn kevent 522functions first appeared in 523.Fx 4.1 . 524.Sh AUTHORS 525The 526.Fn kqueue 527system and this manual page were written by 528.An Jonathan Lemon Aq Mt jlemon@FreeBSD.org . 529.Sh BUGS 530Currently it is only possible to watch a 531.Xr vnode 9 532on 533.Xr HAMMER 5 , 534.Xr tmpfs 5 535and 536.Xr UFS 5 537file systems. 538