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 July 18, 2014 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 46The 47.Fn kqueue 48system call 49provides a generic method of notifying the user when an event 50happens or a condition holds, based on the results of small 51pieces of kernel code termed filters. 52A kevent is identified by the (ident, filter) pair; there may only 53be one unique kevent per kqueue. 54.Pp 55The filter is executed upon the initial registration of a kevent 56in order to detect whether a preexisting condition is present, and is also 57executed whenever an event is passed to the filter for evaluation. 58If the filter determines that the condition should be reported, 59then the kevent is placed on the kqueue for the user to retrieve. 60.Pp 61The filter is also run when the user attempts to retrieve the kevent 62from the kqueue. 63If the filter indicates that the condition that triggered 64the event no longer holds, the kevent is removed from the kqueue and 65is not returned. 66.Pp 67Multiple events which trigger the filter do not result in multiple 68kevents being placed on the kqueue; instead, the filter will aggregate 69the events into a single struct kevent. 70Calling 71.Fn close 72on a file descriptor will remove any kevents that reference the descriptor. 73.Pp 74The 75.Fn kqueue 76system call 77creates a new kernel event queue and returns a descriptor. 78The queue is not inherited by a child created with 79.Xr fork 2 . 80However, if 81.Xr rfork 2 82is called without the 83.Dv RFFDG 84flag, then the descriptor table is shared, 85which will allow sharing of the kqueue between two processes. 86.Pp 87The 88.Fn kevent 89system call 90is used to register events with the queue, and return any pending 91events to the user. 92The 93.Fa changelist 94argument 95is a pointer to an array of 96.Va kevent 97structures, as defined in 98.In sys/event.h . 99All changes contained in the 100.Fa changelist 101are applied before any pending events are read from the queue. 102The 103.Fa nchanges 104argument 105gives the size of 106.Fa changelist . 107The 108.Fa eventlist 109argument 110is a pointer to an array of kevent structures. 111The 112.Fa nevents 113argument 114determines the size of 115.Fa eventlist . 116When 117.Fa nevents 118is zero, 119.Fn kevent 120will return immediately even if there is a 121.Fa timeout 122specified unlike 123.Xr select 2 . 124If 125.Fa timeout 126is a non-NULL pointer, it specifies a maximum interval to wait 127for an event, which will be interpreted as a struct timespec. 128If 129.Fa timeout 130is a NULL pointer, 131.Fn kevent 132waits indefinitely. 133To effect a poll, the 134.Fa timeout 135argument should be non-NULL, pointing to a zero-valued 136.Va timespec 137structure. 138The same array may be used for the 139.Fa changelist 140and 141.Fa eventlist . 142.Pp 143The 144.Fn EV_SET 145macro is provided for ease of initializing a 146kevent structure. 147.Pp 148The 149.Va kevent 150structure is defined as: 151.Bd -literal 152struct kevent { 153 uintptr_t ident; /* identifier for this event */ 154 short filter; /* filter for event */ 155 u_short flags; /* action flags for kqueue */ 156 u_int fflags; /* filter flag value */ 157 intptr_t data; /* filter data value */ 158 void *udata; /* opaque user data identifier */ 159}; 160.Ed 161.Pp 162The fields of 163.Fa struct kevent 164are: 165.Bl -tag -width XXXfilter 166.It ident 167Value used to identify this event. 168The exact interpretation is determined by the attached filter, 169but often is a file descriptor. 170.It filter 171Identifies the kernel filter used to process this event. 172The pre-defined 173system filters are described below. 174.It flags 175Actions to perform on the event. 176.It fflags 177Filter-specific flags. 178.It data 179Filter-specific data value. 180.It udata 181Opaque user-defined value passed through the kernel unchanged. 182.El 183.Pp 184The 185.Va flags 186field can contain the following values: 187.Bl -tag -width XXXEV_ONESHOT 188.It EV_ADD 189Adds the event to the kqueue. 190Re-adding an existing event 191will modify the parameters of the original event, and not result 192in a duplicate entry. 193Adding an event automatically enables it, 194unless overridden by the EV_DISABLE flag. 195.It EV_ENABLE 196Permit 197.Fn kevent 198to return the event if it is triggered. 199.It EV_DISABLE 200Disable the event so 201.Fn kevent 202will not return it. 203The filter itself is not disabled. 204.It EV_DISPATCH 205Disable the event source immediately after delivery of an event. 206See 207.Dv EV_DISABLE 208above. 209.It EV_DELETE 210Removes the event from the kqueue. 211Events which are attached to 212file descriptors are automatically deleted on the last close of 213the descriptor. 214.It EV_RECEIPT 215This flag is useful for making bulk changes to a kqueue without draining 216any pending events. 217When passed as input, it forces 218.Dv EV_ERROR 219to always be returned. 220When a filter is successfully added the 221.Va data 222field will be zero. 223.It EV_ONESHOT 224Causes the event to return only the first occurrence of the filter 225being triggered. 226After the user retrieves the event from the kqueue, 227it is deleted. 228.It EV_CLEAR 229After the event is retrieved by the user, its state is reset. 230This is useful for filters which report state transitions 231instead of the current state. 232Note that some filters may automatically 233set this flag internally. 234.It EV_EOF 235Filters may set this flag to indicate filter-specific EOF condition. 236.It EV_ERROR 237See 238.Sx RETURN VALUES 239below. 240.El 241.Pp 242The predefined system filters are listed below. 243Arguments may be passed to and from the filter via the 244.Va fflags 245and 246.Va data 247fields in the kevent structure. 248.Bl -tag -width EVFILT_PROCDESC 249.It EVFILT_READ 250Takes a descriptor as the identifier, and returns whenever 251there is data available to read. 252The behavior of the filter is slightly different depending 253on the descriptor type. 254.Bl -tag -width 2n 255.It Sockets 256Sockets which have previously been passed to 257.Fn listen 258return when there is an incoming connection pending. 259.Va data 260contains the size of the listen backlog. 261.Pp 262Other socket descriptors return when there is data to be read, 263subject to the 264.Dv SO_RCVLOWAT 265value of the socket buffer. 266This may be overridden with a per-filter low water mark at the 267time the filter is added by setting the 268NOTE_LOWAT 269flag in 270.Va fflags , 271and specifying the new low water mark in 272.Va data . 273On return, 274.Va data 275contains the number of bytes of protocol data available to read. 276.Pp 277If the read direction of the socket has shutdown, then the filter 278also sets EV_EOF in 279.Va flags , 280and returns the socket error (if any) in 281.Va fflags . 282It is possible for EOF to be returned (indicating the connection is gone) 283while there is still data pending in the socket buffer. 284.It Vnodes 285Returns when the file pointer is not at the end of file. 286.Va data 287contains the offset from current position to end of file, 288and may be negative. 289.It "Fifos, Pipes" 290Returns when the there is data to read; 291.Va data 292contains the number of bytes available. 293.Pp 294When the last writer disconnects, the filter will set EV_EOF in 295.Va flags . 296This may be cleared by passing in EV_CLEAR, at which point the 297filter will resume waiting for data to become available before 298returning. 299.It "BPF devices" 300Returns when the BPF buffer is full, the BPF timeout has expired, or 301when the BPF has 302.Dq immediate mode 303enabled and there is any data to read; 304.Va data 305contains the number of bytes available. 306.El 307.It EVFILT_WRITE 308Takes a descriptor as the identifier, and returns whenever 309it is possible to write to the descriptor. 310For sockets, pipes 311and fifos, 312.Va data 313will contain the amount of space remaining in the write buffer. 314The filter will set EV_EOF when the reader disconnects, and for 315the fifo case, this may be cleared by use of EV_CLEAR. 316Note that this filter is not supported for vnodes or BPF devices. 317.Pp 318For sockets, the low water mark and socket error handling is 319identical to the EVFILT_READ case. 320.It EVFILT_AIO 321The sigevent portion of the AIO request is filled in, with 322.Va sigev_notify_kqueue 323containing the descriptor of the kqueue that the event should 324be attached to, 325.Va sigev_notify_kevent_flags 326containing the kevent flags which should be EV_ONESHOT, EV_CLEAR or 327EV_DISPATCH, 328.Va sigev_value 329containing the udata value, and 330.Va sigev_notify 331set to SIGEV_KEVENT. 332When the 333.Fn aio_* 334system call is made, the event will be registered 335with the specified kqueue, and the 336.Va ident 337argument set to the 338.Fa struct aiocb 339returned by the 340.Fn aio_* 341system call. 342The filter returns under the same conditions as aio_error. 343.It EVFILT_VNODE 344Takes a file descriptor as the identifier and the events to watch for in 345.Va fflags , 346and returns when one or more of the requested events occurs on the descriptor. 347The events to monitor are: 348.Bl -tag -width XXNOTE_RENAME 349.It NOTE_DELETE 350The 351.Fn unlink 352system call 353was called on the file referenced by the descriptor. 354.It NOTE_WRITE 355A write occurred on the file referenced by the descriptor. 356.It NOTE_EXTEND 357The file referenced by the descriptor was extended. 358.It NOTE_ATTRIB 359The file referenced by the descriptor had its attributes changed. 360.It NOTE_LINK 361The link count on the file changed. 362.It NOTE_RENAME 363The file referenced by the descriptor was renamed. 364.It NOTE_REVOKE 365Access to the file was revoked via 366.Xr revoke 2 367or the underlying file system was unmounted. 368.El 369.Pp 370On return, 371.Va fflags 372contains the events which triggered the filter. 373.It EVFILT_PROC 374Takes the process ID to monitor as the identifier and the events to watch for 375in 376.Va fflags , 377and returns when the process performs one or more of the requested events. 378If a process can normally see another process, it can attach an event to it. 379The events to monitor are: 380.Bl -tag -width XXNOTE_TRACKERR 381.It NOTE_EXIT 382The process has exited. 383The exit status will be stored in 384.Va data . 385.It NOTE_FORK 386The process has called 387.Fn fork . 388.It NOTE_EXEC 389The process has executed a new process via 390.Xr execve 2 391or a similar call. 392.It NOTE_TRACK 393Follow a process across 394.Fn fork 395calls. 396The parent process registers a new kevent to monitor the child process 397using the same 398.Va fflags 399as the original event. 400The child process will signal an event with NOTE_CHILD set in 401.Va fflags 402and the parent PID in 403.Va data . 404.Pp 405If the parent process fails to register a new kevent 406.Pq usually due to resource limitations , 407it will signal an event with NOTE_TRACKERR set in 408.Va fflags , 409and the child process will not signal a NOTE_CHILD event. 410.El 411.Pp 412On return, 413.Va fflags 414contains the events which triggered the filter. 415.It EVFILT_PROCDESC 416Takes the process descriptor created by 417.Xr pdfork 2 418to monitor as the identifier and the events to watch for in 419.Va fflags , 420and returns when the associated process performs one or more of the 421requested events. 422The events to monitor are: 423.Bl -tag -width XXNOTE_EXIT 424.It NOTE_EXIT 425The process has exited. 426The exit status will be stored in 427.Va data . 428.El 429.Pp 430On return, 431.Va fflags 432contains the events which triggered the filter. 433.It EVFILT_SIGNAL 434Takes the signal number to monitor as the identifier and returns 435when the given signal is delivered to the process. 436This coexists with the 437.Fn signal 438and 439.Fn sigaction 440facilities, and has a lower precedence. 441The filter will record 442all attempts to deliver a signal to a process, even if the signal has 443been marked as SIG_IGN, except for the 444.Dv SIGCHLD 445signal, which, if ignored, won't be recorded by the filter. 446Event notification happens after normal 447signal delivery processing. 448.Va data 449returns the number of times the signal has occurred since the last call to 450.Fn kevent . 451This filter automatically sets the EV_CLEAR flag internally. 452.It EVFILT_TIMER 453Establishes an arbitrary timer identified by 454.Va ident . 455When adding a timer, 456.Va data 457specifies the timeout period. 458The timer will be periodic unless EV_ONESHOT is specified. 459On return, 460.Va data 461contains the number of times the timeout has expired since the last call to 462.Fn kevent . 463This filter automatically sets the EV_CLEAR flag internally. 464There is a system wide limit on the number of timers 465which is controlled by the 466.Va kern.kq_calloutmax 467sysctl. 468.Bl -tag -width XXNOTE_USECONDS 469.It Dv NOTE_SECONDS 470.Va data 471is in seconds. 472.It Dv NOTE_MSECONDS 473.Va data 474is in milliseconds. 475.It Dv NOTE_USECONDS 476.Va data 477is in microseconds. 478.It Dv NOTE_NSECONDS 479.Va data 480is in nanoseconds. 481.El 482.Pp 483If 484.Va fflags 485is not set, the default is milliseconds. On return, 486.Va fflags 487contains the events which triggered the filter. 488.It Dv EVFILT_USER 489Establishes a user event identified by 490.Va ident 491which is not associated with any kernel mechanism but is triggered by 492user level code. 493The lower 24 bits of the 494.Va fflags 495may be used for user defined flags and manipulated using the following: 496.Bl -tag -width XXNOTE_FFLAGSMASK 497.It Dv NOTE_FFNOP 498Ignore the input 499.Va fflags . 500.It Dv NOTE_FFAND 501Bitwise AND 502.Va fflags . 503.It Dv NOTE_FFOR 504Bitwise OR 505.Va fflags . 506.It Dv NOTE_FFCOPY 507Copy 508.Va fflags . 509.It Dv NOTE_FFCTRLMASK 510Control mask for 511.Va fflags . 512.It Dv NOTE_FFLAGSMASK 513User defined flag mask for 514.Va fflags . 515.El 516.Pp 517A user event is triggered for output with the following: 518.Bl -tag -width XXNOTE_FFLAGSMASK 519.It Dv NOTE_TRIGGER 520Cause the event to be triggered. 521.El 522.Pp 523On return, 524.Va fflags 525contains the users defined flags in the lower 24 bits. 526.El 527.Sh RETURN VALUES 528The 529.Fn kqueue 530system call 531creates a new kernel event queue and returns a file descriptor. 532If there was an error creating the kernel event queue, a value of -1 is 533returned and errno set. 534.Pp 535The 536.Fn kevent 537system call 538returns the number of events placed in the 539.Fa eventlist , 540up to the value given by 541.Fa nevents . 542If an error occurs while processing an element of the 543.Fa changelist 544and there is enough room in the 545.Fa eventlist , 546then the event will be placed in the 547.Fa eventlist 548with 549.Dv EV_ERROR 550set in 551.Va flags 552and the system error in 553.Va data . 554Otherwise, 555.Dv -1 556will be returned, and 557.Dv errno 558will be set to indicate the error condition. 559If the time limit expires, then 560.Fn kevent 561returns 0. 562.Sh ERRORS 563The 564.Fn kqueue 565system call fails if: 566.Bl -tag -width Er 567.It Bq Er ENOMEM 568The kernel failed to allocate enough memory for the kernel queue. 569.It Bq Er ENOMEM 570The 571.Dv RLIMIT_KQUEUES 572rlimit 573(see 574.Xr getrlimit 2 ) 575for the current user would be exceeded. 576.It Bq Er EMFILE 577The per-process descriptor table is full. 578.It Bq Er ENFILE 579The system file table is full. 580.El 581.Pp 582The 583.Fn kevent 584system call fails if: 585.Bl -tag -width Er 586.It Bq Er EACCES 587The process does not have permission to register a filter. 588.It Bq Er EFAULT 589There was an error reading or writing the 590.Va kevent 591structure. 592.It Bq Er EBADF 593The specified descriptor is invalid. 594.It Bq Er EINTR 595A signal was delivered before the timeout expired and before any 596events were placed on the kqueue for return. 597.It Bq Er EINVAL 598The specified time limit or filter is invalid. 599.It Bq Er ENOENT 600The event could not be found to be modified or deleted. 601.It Bq Er ENOMEM 602No memory was available to register the event 603or, in the special case of a timer, the maximum number of 604timers has been exceeded. 605This maximum is configurable via the 606.Va kern.kq_calloutmax 607sysctl. 608.It Bq Er ESRCH 609The specified process to attach to does not exist. 610.El 611.Sh SEE ALSO 612.Xr aio_error 2 , 613.Xr aio_read 2 , 614.Xr aio_return 2 , 615.Xr poll 2 , 616.Xr read 2 , 617.Xr select 2 , 618.Xr sigaction 2 , 619.Xr write 2 , 620.Xr signal 3 621.Sh HISTORY 622The 623.Fn kqueue 624and 625.Fn kevent 626system calls first appeared in 627.Fx 4.1 . 628.Sh AUTHORS 629The 630.Fn kqueue 631system and this manual page were written by 632.An Jonathan Lemon Aq Mt [email protected] . 633.Sh BUGS 634The 635.Fa timeout 636value is limited to 24 hours; longer timeouts will be silently 637reinterpreted as 24 hours. 638