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.Dd September 12, 2025 26.Dt KQUEUE 2 27.Os 28.Sh NAME 29.Nm kqueue , 30.Nm kevent 31.Nd kernel event notification mechanism 32.Sh LIBRARY 33.Lb libc 34.Sh SYNOPSIS 35.In sys/event.h 36.Ft int 37.Fn kqueue "void" 38.Ft int 39.Fn kqueuex "u_int flags" 40.Ft int 41.Fn kqueue1 "int flags" 42.Ft int 43.Fo kevent 44.Fa "int kq" 45.Fa "const struct kevent *changelist" 46.Fa "int nchanges" 47.Fa "struct kevent *eventlist" 48.Fa "int nevents" 49.Fa "const struct timespec *timeout" 50.Fc 51.Fn EV_SET "kev" ident filter flags fflags data udata 52.Sh DESCRIPTION 53The 54.Fn kqueue 55system call 56provides a generic method of notifying the user when an event 57happens or a condition holds, based on the results of small 58pieces of kernel code termed filters. 59A kevent is identified by the (ident, filter) pair; there may only 60be one unique kevent per kqueue. 61.Pp 62The filter is executed upon the initial registration of a kevent 63in order to detect whether a preexisting condition is present, and is also 64executed whenever an event is passed to the filter for evaluation. 65If the filter determines that the condition should be reported, 66then the kevent is placed on the kqueue for the user to retrieve. 67.Pp 68The filter is also run when the user attempts to retrieve the kevent 69from the kqueue. 70If the filter indicates that the condition that triggered 71the event no longer holds, the kevent is removed from the kqueue and 72is not returned. 73.Pp 74Multiple events which trigger the filter do not result in multiple 75kevents being placed on the kqueue; instead, the filter will aggregate 76the events into a single struct kevent. 77Calling 78.Fn close 79on a file descriptor will remove any kevents that reference the descriptor. 80.Pp 81The 82.Fn kqueue 83system call 84creates a new kernel event queue and returns a descriptor. 85The queue is not inherited by a child created with 86.Xr fork 2 . 87However, if 88.Xr rfork 2 89is called without the 90.Dv RFFDG 91flag, then the descriptor table is shared, 92which will allow sharing of the kqueue between two processes. 93.Pp 94The 95.Fn kqueuex 96system call also creates a new kernel event queue, and additionally takes 97a 98.Fa flags 99argument, which is a bitwise-inclusive OR of the following flags: 100.Bl -tag -width "KQUEUE_CLOEXEC" 101.It Dv KQUEUE_CLOEXEC 102The returned file descriptor is automatically closed on 103.Xr execve 2 104.El 105.Pp 106The 107.Fn kqueue 108system call is equivalent to calling 109.Fn kqueuex 110with 111.Fa flags 112set to 0. 113.Pp 114The 115.Fn kqueue1 116function exists for compatibility with 117.Nx . 118The 119.Fa flags 120argument accepts zero or more of the following values: 121.Bl -tag -width O_CLOEXEC 122.It Dv O_CLOEXEC 123The returned file descriptor is automatically closed on 124.Xr execve 2 125.El 126.Pp 127The 128.Fn kevent 129system call 130is used to register events with the queue, and return any pending 131events to the user. 132The 133.Fa changelist 134argument 135is a pointer to an array of 136.Va kevent 137structures, as defined in 138.In sys/event.h . 139All changes contained in the 140.Fa changelist 141are applied before any pending events are read from the queue. 142The 143.Fa nchanges 144argument 145gives the size of 146.Fa changelist . 147The 148.Fa eventlist 149argument 150is a pointer to an array of kevent structures. 151The 152.Fa nevents 153argument 154determines the size of 155.Fa eventlist . 156When 157.Fa nevents 158is zero, 159.Fn kevent 160will return immediately even if there is a 161.Fa timeout 162specified unlike 163.Xr select 2 . 164If 165.Fa timeout 166is a non-NULL pointer, it specifies a maximum interval to wait 167for an event, which will be interpreted as a struct timespec. 168If 169.Fa timeout 170is a NULL pointer, 171.Fn kevent 172waits indefinitely. 173To effect a poll, the 174.Fa timeout 175argument should be non-NULL, pointing to a zero-valued 176.Va timespec 177structure. 178The same array may be used for the 179.Fa changelist 180and 181.Fa eventlist . 182.Pp 183The 184.Fn EV_SET 185macro is provided for ease of initializing a 186kevent structure. 187.Pp 188The 189.Va kevent 190structure is defined as: 191.Bd -literal 192struct kevent { 193 uintptr_t ident; /* identifier for this event */ 194 short filter; /* filter for event */ 195 u_short flags; /* action flags for kqueue */ 196 u_int fflags; /* filter flag value */ 197 int64_t data; /* filter data value */ 198 void *udata; /* opaque user data identifier */ 199 uint64_t ext[4]; /* extensions */ 200}; 201.Ed 202.Pp 203The fields of 204.Fa struct kevent 205are: 206.Bl -tag -width "Fa filter" 207.It Fa ident 208Value used to identify this event. 209The exact interpretation is determined by the attached filter, 210but often is a file descriptor. 211.It Fa filter 212Identifies the kernel filter used to process this event. 213The pre-defined 214system filters are described below. 215.It Fa flags 216Actions to perform on the event. 217.It Fa fflags 218Filter-specific flags. 219.It Fa data 220Filter-specific data value. 221.It Fa udata 222Opaque user-defined value passed through the kernel unchanged. 223.It Fa ext 224Extended data passed to and from kernel. 225The meaning of the 226.Fa ext[0] 227and 228.Fa ext[1] 229members is defined by the filter. 230If a filter does not use them, 231these members are passed through the kernel unchanged. 232The 233.Fa ext[2] 234and 235.Fa ext[3] 236members are always passed through the kernel unchanged, 237providing additional user-defined values. 238.El 239.Pp 240The 241.Va flags 242field can contain the following values: 243.Bl -tag -width EV_KEEPUDATA 244.It Dv EV_ADD 245Adds the event to the kqueue. 246Re-adding an existing event 247will modify the parameters of the original event, and not result 248in a duplicate entry. 249Adding an event automatically enables it, 250unless overridden by the EV_DISABLE flag. 251.It Dv EV_ENABLE 252Permit 253.Fn kevent 254to return the event if it is triggered. 255.It Dv EV_DISABLE 256Disable the event so 257.Fn kevent 258will not return it. 259The filter itself is not disabled. 260.It Dv EV_DISPATCH 261Disable the event source immediately after delivery of an event. 262See 263.Dv EV_DISABLE 264above. 265.It Dv EV_DELETE 266Removes the event from the kqueue. 267Events which are attached to 268file descriptors are automatically deleted on the last close of 269the descriptor. 270.It Dv EV_RECEIPT 271This flag is useful for making bulk changes to a kqueue without draining 272any pending events. 273When passed as input, it forces 274.Dv EV_ERROR 275to always be returned. 276When a filter is successfully added the 277.Va data 278field will be zero. 279Note that if this flag is encountered and there is no remaining space in 280.Fa eventlist 281to hold the 282.Dv EV_ERROR 283event, then subsequent changes will not get processed. 284.It Dv EV_ONESHOT 285Return only the first occurrence of the filter 286being triggered. 287After the user retrieves the event from the kqueue, 288it is deleted. 289.It Dv EV_CLEAR 290Reset the state of the event after it is retrieved by the user. 291This is useful for filters which report state transitions 292instead of the current state. 293Note that some filters may automatically 294set this flag internally. 295.It Dv EV_EOF 296Filters may set this flag to indicate filter-specific EOF condition. 297.It Dv EV_ERROR 298See 299.Sx RETURN VALUES 300below. 301.It Dv EV_KEEPUDATA 302Preserve the 303.Fa udata 304associated with an existing event. 305This allows other aspects of the event to be modified without requiring the 306caller to know the 307.Fa udata 308value previously registered with the event. 309This is especially useful with 310.Dv NOTE_TRIGGER 311or 312.Dv EV_ENABLE . 313This flag may not be used with 314.Dv EV_ADD . 315.El 316.Pp 317The predefined system filters are listed below. 318Arguments may be passed to and from the filter via the 319.Va fflags 320and 321.Va data 322fields in the kevent structure. 323.Bl -tag -width "Dv EVFILT_PROCDESC" 324.It Dv EVFILT_READ 325Takes a descriptor as the identifier, and returns whenever 326there is data available to read. 327The behavior of the filter is slightly different depending 328on the descriptor type. 329.Bl -tag -width 2n 330.It Sockets 331Sockets which have previously been passed to 332.Xr listen 2 333return when there is an incoming connection pending. 334.Va data 335contains the size of the listen backlog. 336.Pp 337Other socket descriptors return when there is data to be read, 338subject to the 339.Dv SO_RCVLOWAT 340value of the socket buffer. 341This may be overridden with a per-filter low water mark at the 342time the filter is added by setting the 343.Dv NOTE_LOWAT 344flag in 345.Va fflags , 346and specifying the new low water mark in 347.Va data . 348On return, 349.Va data 350contains the number of bytes of protocol data available to read. 351.Pp 352If the read direction of the socket has shutdown, then the filter 353also sets 354.Dv EV_EOF 355in 356.Va flags , 357and returns the socket error (if any) in 358.Va fflags . 359It is possible for EOF to be returned (indicating the connection is gone) 360while there is still data pending in the socket buffer. 361.It Vnodes 362Returns when the file pointer is not at the end of file. 363.Va data 364contains the offset from current position to end of file, 365and may be negative. 366.Pp 367This behavior is different from 368.Xr poll 2 , 369where read events are triggered for regular files unconditionally. 370This event can be triggered unconditionally by setting the 371.Dv NOTE_FILE_POLL 372flag in 373.Va fflags . 374.It "Fifos, Pipes" 375Returns when the there is data to read; 376.Va data 377contains the number of bytes available. 378.Pp 379When the last writer disconnects, the filter will set 380.Dv EV_EOF 381in 382.Va flags . 383This will be cleared by the filter when a new writer connects, 384at which point the 385filter will resume waiting for data to become available before 386returning. 387.It "BPF devices" 388Returns when the BPF buffer is full, the BPF timeout has expired, or 389when the BPF has 390.Dq immediate mode 391enabled and there is any data to read; 392.Va data 393contains the number of bytes available. 394.It Eventfds 395Returns when the counter is greater than 0; 396.Va data 397contains the counter value, which must be cast to 398.Vt uint64_t . 399.It Kqueues 400Returns when pending events are present on the queue; 401.Va data 402contains the number of events available. 403.El 404.It Dv EVFILT_WRITE 405Takes a descriptor as the identifier, and returns whenever 406it is possible to write to the descriptor. 407For sockets, pipes 408and fifos, 409.Va data 410will contain the amount of space remaining in the write buffer. 411The filter will set 412.Dv EV_EOF 413when the reader disconnects, and for the fifo case, this will be cleared 414when a new reader connects. 415Note that this filter is not supported for vnodes. 416.Pp 417For sockets, the low water mark and socket error handling is 418identical to the 419.Dv EVFILT_READ 420case. 421.Pp 422For eventfds, 423.Va data 424will contain the maximum value that can be added to the counter 425without blocking. 426.Pp 427For BPF devices, when the descriptor is attached to an interface the filter 428always indicates that it is possible to write and 429.Va data 430will contain the MTU size of the underlying interface. 431.It Dv EVFILT_EMPTY 432Takes a descriptor as the identifier, and returns whenever 433there is no remaining data in the write buffer. 434.It Dv EVFILT_AIO 435Events for this filter are not registered with 436.Fn kevent 437directly but are registered via the 438.Va aio_sigevent 439member of an asynchronous I/O request when it is scheduled via an 440asynchronous I/O system call such as 441.Fn aio_read . 442The filter returns under the same conditions as 443.Fn aio_error . 444For more details on this filter see 445.Xr sigevent 3 and 446.Xr aio 4 . 447.It Dv EVFILT_VNODE 448Takes a file descriptor as the identifier and the events to watch for in 449.Va fflags , 450and returns when one or more of the requested events occurs on the descriptor. 451The events to monitor are: 452.Bl -tag -width "Dv NOTE_CLOSE_WRITE" 453.It Dv NOTE_ATTRIB 454The file referenced by the descriptor had its attributes changed. 455.It Dv NOTE_CLOSE 456A file descriptor referencing the monitored file, was closed. 457The closed file descriptor did not have write access. 458.It Dv NOTE_CLOSE_WRITE 459A file descriptor referencing the monitored file, was closed. 460The closed file descriptor had write access. 461.Pp 462This note, as well as 463.Dv NOTE_CLOSE , 464are not activated when files are closed forcibly by 465.Xr unmount 2 or 466.Xr revoke 2 . 467Instead, 468.Dv NOTE_REVOKE 469is sent for such events. 470.It Dv NOTE_DELETE 471The 472.Fn unlink 473system call was called on the file referenced by the descriptor. 474.It Dv NOTE_EXTEND 475For regular file, the file referenced by the descriptor was extended. 476.Pp 477For directory, reports that a directory entry was added or removed, 478as the result of rename operation. 479The 480.Dv NOTE_EXTEND 481event is not reported when a name is changed inside the directory. 482.It Dv NOTE_LINK 483The link count on the file changed. 484In particular, the 485.Dv NOTE_LINK 486event is reported if a subdirectory was created or deleted inside 487the directory referenced by the descriptor. 488.It Dv NOTE_OPEN 489The file referenced by the descriptor was opened. 490.It Dv NOTE_READ 491A read occurred on the file referenced by the descriptor. 492.It Dv NOTE_RENAME 493The file referenced by the descriptor was renamed. 494.It Dv NOTE_REVOKE 495Access to the file was revoked via 496.Xr revoke 2 497or the underlying file system was unmounted. 498.It Dv NOTE_WRITE 499A write occurred on the file referenced by the descriptor. 500.El 501.Pp 502On return, 503.Va fflags 504contains the events which triggered the filter. 505.It Dv EVFILT_PROC 506Takes the process ID to monitor as the identifier and the events to watch for 507in 508.Va fflags , 509and returns when the process performs one or more of the requested events. 510If a process can normally see another process, it can attach an event to it. 511The events to monitor are: 512.Bl -tag -width "Dv NOTE_TRACKERR" 513.It Dv NOTE_EXIT 514The process has exited. 515The exit status will be stored in 516.Va data 517in the same format as the status returned by 518.Xr wait 2 . 519.It Dv NOTE_FORK 520The process has called 521.Fn fork . 522.It Dv NOTE_EXEC 523The process has executed a new process via 524.Xr execve 2 525or a similar call. 526.It Dv NOTE_TRACK 527Follow a process across 528.Fn fork 529calls. 530The parent process registers a new kevent to monitor the child process 531using the same 532.Va fflags 533as the original event. 534The child process will signal an event with 535.Dv NOTE_CHILD 536set in 537.Va fflags 538and the parent PID in 539.Va data . 540.Pp 541If the parent process fails to register a new kevent 542.Pq usually due to resource limitations , 543it will signal an event with 544.Dv NOTE_TRACKERR 545set in 546.Va fflags , 547and the child process will not signal a 548.Dv NOTE_CHILD 549event. 550.El 551.Pp 552On return, 553.Va fflags 554contains the events which triggered the filter. 555.It Dv EVFILT_PROCDESC 556Takes the process descriptor created by 557.Xr pdfork 2 558to monitor as the identifier and the events to watch for in 559.Va fflags , 560and returns when the associated process performs one or more of the 561requested events. 562The events to monitor are: 563.Bl -tag -width "Dv NOTE_EXIT" 564.It Dv NOTE_EXIT 565The process has exited. 566The exit status will be stored in 567.Va data . 568.El 569.Pp 570On return, 571.Va fflags 572contains the events which triggered the filter. 573.It Dv EVFILT_SIGNAL 574Takes the signal number to monitor as the identifier and returns 575when the given signal is delivered to the process. 576This coexists with the 577.Fn signal 578and 579.Fn sigaction 580facilities, and has a lower precedence. 581The filter will record 582all attempts to deliver a signal to a process, even if the signal has 583been marked as 584.Dv SIG_IGN , 585except for the 586.Dv SIGCHLD 587signal, which, if ignored, will not be recorded by the filter. 588Event notification happens after normal 589signal delivery processing. 590.Va data 591returns the number of times the signal has occurred since the last call to 592.Fn kevent . 593This filter automatically sets the 594.Dv EV_CLEAR 595flag internally. 596.It Dv EVFILT_JAIL 597Takes the jail ID to monitor as the identifier and the events to watch for 598in 599.Va fflags , 600and returns when the jail performs one or more of the requested events. 601If a process can normally see a jail, it can attach an event to it. 602An identifier of zero will watch the process's own jail. 603The events to monitor are: 604.Bl -tag -width "Dv NOTE_JAIL_ATTACH" 605.It Dv NOTE_JAIL_SET 606The jail has been changed via 607.Xr jail_set 2 . 608.It Dv NOTE_JAIL_ATTACH 609A process has attached to the jail via 610.Xr jail_attach 2 611or a similar call. 612The process ID will be stored in 613.Va data . 614If more than one process has attached since the last call to 615.Fn kevent , 616.Va data 617will be zero. 618.It Dv NOTE_JAIL_REMOVE 619The jail has been removed. 620.It Dv NOTE_JAIL_CHILD 621A child of the watched jail has been created. 622Its jail ID will be stored in 623.Va data . 624If more than one jail has been created since the last call to 625.Fn kevent , 626.Va data 627will be zero. 628.El 629.Pp 630On return, 631.Va fflags 632contains the events which triggered the filter. 633It will also contain 634.Dv NOTE_JAIL_MULTI 635if more than one 636.Dv NOTE_JAIL_ATTACH 637or 638.Dv NOTE_JAIL_CHILD 639event has been received since the last call to 640.Fn kevent . 641.It Dv EVFILT_JAILDESC 642Takes a jail descriptor returned by 643.Xr jail_set 2 644or 645.Xr jail_get 2 646as the identifier and the events to watch for in 647.Va fflags , 648and returns when the jail performs one or more of the requested events. 649The events to monitor and the resulting 650.Va fflags 651are the same as those listed in 652.Dv EVFILT_JAIL , 653above. 654.It Dv EVFILT_TIMER 655Establishes an arbitrary timer identified by 656.Va ident . 657When adding a timer, 658.Va data 659specifies the moment to fire the timer (for 660.Dv NOTE_ABSTIME ) 661or the timeout period. 662The timer will be periodic unless 663.Dv EV_ONESHOT 664or 665.Dv NOTE_ABSTIME 666is specified. 667On return, 668.Va data 669contains the number of times the timeout has expired since the last call to 670.Fn kevent . 671For non-monotonic timers, this filter automatically sets the 672.Dv EV_CLEAR 673flag internally. 674.Pp 675The filter accepts the following flags in the 676.Va fflags 677argument: 678.Bl -tag -width "Dv NOTE_MSECONDS" 679.It Dv NOTE_SECONDS 680.Va data 681is in seconds. 682.It Dv NOTE_MSECONDS 683.Va data 684is in milliseconds. 685.It Dv NOTE_USECONDS 686.Va data 687is in microseconds. 688.It Dv NOTE_NSECONDS 689.Va data 690is in nanoseconds. 691.It Dv NOTE_ABSTIME 692The specified expiration time is absolute. 693.El 694.Pp 695If 696.Va fflags 697is not set, the default is milliseconds. 698On return, 699.Va fflags 700contains the events which triggered the filter. 701.Pp 702Periodic timers with a specified timeout of 0 will be silently adjusted to 703timeout after 1 of the time units specified by the requested precision in 704.Va fflags . 705If an absolute time is specified that has already passed, then it is treated as 706if the current time were specified and the event will fire as soon as possible. 707.Pp 708If an existing timer is re-added, the existing timer will be 709effectively canceled (throwing away any undelivered record of previous 710timer expiration) and re-started using the new parameters contained in 711.Va data 712and 713.Va fflags . 714.Pp 715There is a system wide limit on the number of timers 716which is controlled by the 717.Va kern.kq_calloutmax 718sysctl. 719.It Dv EVFILT_USER 720Establishes a user event identified by 721.Va ident 722which is not associated with any kernel mechanism but is triggered by 723user level code. 724The lower 24 bits of the 725.Va fflags 726may be used for user defined flags and manipulated using the following: 727.Bl -tag -width "Dv NOTE_FFLAGSMASK" 728.It Dv NOTE_FFNOP 729Ignore the input 730.Va fflags . 731.It Dv NOTE_FFAND 732Bitwise AND 733.Va fflags . 734.It Dv NOTE_FFOR 735Bitwise OR 736.Va fflags . 737.It Dv NOTE_FFCOPY 738Copy 739.Va fflags . 740.It Dv NOTE_FFCTRLMASK 741Control mask for 742.Va fflags . 743.It Dv NOTE_FFLAGSMASK 744User defined flag mask for 745.Va fflags . 746.El 747.Pp 748A user event is triggered for output with the following: 749.Bl -tag -width "Dv NOTE_FFLAGSMASK" 750.It Dv NOTE_TRIGGER 751Cause the event to be triggered. 752.El 753.Pp 754On return, 755.Va fflags 756contains the users defined flags in the lower 24 bits. 757.El 758.Sh CANCELLATION BEHAVIOUR 759If 760.Fa nevents 761is non-zero, i.e., the function is potentially blocking, the call 762is a cancellation point. 763Otherwise, i.e., if 764.Fa nevents 765is zero, the call is not cancellable. 766Cancellation can only occur before any changes are made to the kqueue, 767or when the call was blocked and no changes to the queue were requested. 768.Sh RETURN VALUES 769The 770.Fn kqueue 771system call 772creates a new kernel event queue and returns a file descriptor. 773If there was an error creating the kernel event queue, a value of -1 is 774returned and errno set. 775.Pp 776The 777.Fn kevent 778system call 779returns the number of events placed in the 780.Fa eventlist , 781up to the value given by 782.Fa nevents . 783If an error occurs while processing an element of the 784.Fa changelist 785and there is enough room in the 786.Fa eventlist , 787then the event will be placed in the 788.Fa eventlist 789with 790.Dv EV_ERROR 791set in 792.Va flags 793and the system error in 794.Va data . 795Otherwise, 796.Dv -1 797will be returned, and 798.Dv errno 799will be set to indicate the error condition. 800If the time limit expires, then 801.Fn kevent 802returns 0. 803.Sh EXAMPLES 804.Bd -literal -compact 805#include <sys/event.h> 806#include <err.h> 807#include <fcntl.h> 808#include <stdio.h> 809#include <stdlib.h> 810#include <string.h> 811 812int 813main(int argc, char **argv) 814{ 815 struct kevent event; /* Event we want to monitor */ 816 struct kevent tevent; /* Event triggered */ 817 int kq, fd, ret; 818 819 if (argc != 2) 820 err(EXIT_FAILURE, "Usage: %s path\en", argv[0]); 821 fd = open(argv[1], O_RDONLY); 822 if (fd == -1) 823 err(EXIT_FAILURE, "Failed to open '%s'", argv[1]); 824 825 /* Create kqueue. */ 826 kq = kqueue(); 827 if (kq == -1) 828 err(EXIT_FAILURE, "kqueue() failed"); 829 830 /* Initialize kevent structure. */ 831 EV_SET(&event, fd, EVFILT_VNODE, EV_ADD | EV_CLEAR, NOTE_WRITE, 832 0, NULL); 833 /* Attach event to the kqueue. */ 834 ret = kevent(kq, &event, 1, NULL, 0, NULL); 835 if (ret == -1) 836 err(EXIT_FAILURE, "kevent register"); 837 838 for (;;) { 839 /* Sleep until something happens. */ 840 ret = kevent(kq, NULL, 0, &tevent, 1, NULL); 841 if (ret == -1) { 842 err(EXIT_FAILURE, "kevent wait"); 843 } else if (ret > 0) { 844 if (tevent.flags & EV_ERROR) 845 errx(EXIT_FAILURE, "Event error: %s", strerror(event.data)); 846 else 847 printf("Something was written in '%s'\en", argv[1]); 848 } 849 } 850 851 /* kqueues are destroyed upon close() */ 852 (void)close(kq); 853 (void)close(fd); 854} 855.Ed 856.Sh ERRORS 857The 858.Fn kqueue 859system call fails if: 860.Bl -tag -width Er 861.It Bq Er ENOMEM 862The kernel failed to allocate enough memory for the kernel queue. 863.It Bq Er ENOMEM 864The 865.Dv RLIMIT_KQUEUES 866rlimit 867(see 868.Xr getrlimit 2 ) 869for the current user would be exceeded. 870.It Bq Er EMFILE 871The per-process descriptor table is full. 872.It Bq Er ENFILE 873The system file table is full. 874.El 875.Pp 876The 877.Fn kevent 878system call fails if: 879.Bl -tag -width Er 880.It Bq Er EACCES 881The process does not have permission to register a filter. 882.It Bq Er EFAULT 883There was an error reading or writing the 884.Va kevent 885structure. 886.It Bq Er EBADF 887The specified descriptor is invalid. 888.It Bq Er EINTR 889A signal was delivered before the timeout expired and before any 890events were placed on the kqueue for return. 891.It Bq Er EINTR 892A cancellation request was delivered to the thread, but not yet handled. 893.It Bq Er EINVAL 894The specified time limit or filter is invalid. 895.It Bq Er EINVAL 896The specified length of the event or change lists is negative. 897.It Bq Er ENOENT 898The event could not be found to be modified or deleted. 899.It Bq Er ENOMEM 900No memory was available to register the event 901or, in the special case of a timer, the maximum number of 902timers has been exceeded. 903This maximum is configurable via the 904.Va kern.kq_calloutmax 905sysctl. 906.It Bq Er ESRCH 907The specified process to attach to does not exist. 908.El 909.Pp 910When 911.Fn kevent 912call fails with 913.Er EINTR 914error, all changes in the 915.Fa changelist 916have been applied. 917.Sh SEE ALSO 918.Xr aio_error 2 , 919.Xr aio_read 2 , 920.Xr aio_return 2 , 921.Xr poll 2 , 922.Xr read 2 , 923.Xr select 2 , 924.Xr sigaction 2 , 925.Xr write 2 , 926.Xr pthread_setcancelstate 3 , 927.Xr signal 3 928.Rs 929.%A Jonathan Lemon 930.%T "Kqueue: A Generic and Scalable Event Notification Facility" 931.%I USENIX Association 932.%B Proceedings of the FREENIX Track: 2001 USENIX Annual Technical Conference 933.%D June 25-30, 2001 934.\".http://www.usenix.org/event/usenix01/freenix01/full_papers/lemon/lemon.pdf 935.Re 936.Sh HISTORY 937The 938.Fn kqueue 939and 940.Fn kevent 941system calls first appeared in 942.Fx 4.1 . 943The 944.Fn kqueuex 945system call 946and 947.Fn kevent1 948function first appeared in 949.Fx 14.0 . 950.Sh AUTHORS 951The 952.Fn kqueue 953system and this manual page were written by 954.An Jonathan Lemon Aq Mt jlemon@FreeBSD.org . 955.Sh BUGS 956.Pp 957In versions older than 958.Fx 12.0 , 959.In sys/event.h 960failed to parse without including 961.In sys/types.h 962manually. 963