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 4, 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 contain the most recently attached process ID, 618with 619.Dv NOTE_JAIL_ATTACH_MULTI 620set in 621.Va fflags . 622.It Dv NOTE_JAIL_REMOVE 623The jail has been removed. 624.It Dv NOTE_JAIL_CHILD 625A child of the watched jail has been created. 626.It Dv NOTE_TRACK 627Follow child jails created under this jail. 628Register a new kevent to monitor the child jail using the same 629.Va fflags 630as the original event. 631The child jail will signal an event with 632.Dv NOTE_CHILD 633set in 634.Va fflags 635and the parent JID in 636.Va data . 637.Pp 638If registering a new kevent fails 639.Pq usually due to resource limitations , 640it will signal an event with 641.Dv NOTE_TRACKERR 642set in 643.Va fflags , 644and the child jail will not signal a 645.Dv NOTE_CHILD 646event. 647.El 648.Pp 649On return, 650.Va fflags 651contains the events which triggered the filter. 652.It Dv EVFILT_TIMER 653Establishes an arbitrary timer identified by 654.Va ident . 655When adding a timer, 656.Va data 657specifies the moment to fire the timer (for 658.Dv NOTE_ABSTIME ) 659or the timeout period. 660The timer will be periodic unless 661.Dv EV_ONESHOT 662or 663.Dv NOTE_ABSTIME 664is specified. 665On return, 666.Va data 667contains the number of times the timeout has expired since the last call to 668.Fn kevent . 669For non-monotonic timers, this filter automatically sets the 670.Dv EV_CLEAR 671flag internally. 672.Pp 673The filter accepts the following flags in the 674.Va fflags 675argument: 676.Bl -tag -width "Dv NOTE_MSECONDS" 677.It Dv NOTE_SECONDS 678.Va data 679is in seconds. 680.It Dv NOTE_MSECONDS 681.Va data 682is in milliseconds. 683.It Dv NOTE_USECONDS 684.Va data 685is in microseconds. 686.It Dv NOTE_NSECONDS 687.Va data 688is in nanoseconds. 689.It Dv NOTE_ABSTIME 690The specified expiration time is absolute. 691.El 692.Pp 693If 694.Va fflags 695is not set, the default is milliseconds. 696On return, 697.Va fflags 698contains the events which triggered the filter. 699.Pp 700Periodic timers with a specified timeout of 0 will be silently adjusted to 701timeout after 1 of the time units specified by the requested precision in 702.Va fflags . 703If an absolute time is specified that has already passed, then it is treated as 704if the current time were specified and the event will fire as soon as possible. 705.Pp 706If an existing timer is re-added, the existing timer will be 707effectively canceled (throwing away any undelivered record of previous 708timer expiration) and re-started using the new parameters contained in 709.Va data 710and 711.Va fflags . 712.Pp 713There is a system wide limit on the number of timers 714which is controlled by the 715.Va kern.kq_calloutmax 716sysctl. 717.It Dv EVFILT_USER 718Establishes a user event identified by 719.Va ident 720which is not associated with any kernel mechanism but is triggered by 721user level code. 722The lower 24 bits of the 723.Va fflags 724may be used for user defined flags and manipulated using the following: 725.Bl -tag -width "Dv NOTE_FFLAGSMASK" 726.It Dv NOTE_FFNOP 727Ignore the input 728.Va fflags . 729.It Dv NOTE_FFAND 730Bitwise AND 731.Va fflags . 732.It Dv NOTE_FFOR 733Bitwise OR 734.Va fflags . 735.It Dv NOTE_FFCOPY 736Copy 737.Va fflags . 738.It Dv NOTE_FFCTRLMASK 739Control mask for 740.Va fflags . 741.It Dv NOTE_FFLAGSMASK 742User defined flag mask for 743.Va fflags . 744.El 745.Pp 746A user event is triggered for output with the following: 747.Bl -tag -width "Dv NOTE_FFLAGSMASK" 748.It Dv NOTE_TRIGGER 749Cause the event to be triggered. 750.El 751.Pp 752On return, 753.Va fflags 754contains the users defined flags in the lower 24 bits. 755.El 756.Sh CANCELLATION BEHAVIOUR 757If 758.Fa nevents 759is non-zero, i.e., the function is potentially blocking, the call 760is a cancellation point. 761Otherwise, i.e., if 762.Fa nevents 763is zero, the call is not cancellable. 764Cancellation can only occur before any changes are made to the kqueue, 765or when the call was blocked and no changes to the queue were requested. 766.Sh RETURN VALUES 767The 768.Fn kqueue 769system call 770creates a new kernel event queue and returns a file descriptor. 771If there was an error creating the kernel event queue, a value of -1 is 772returned and errno set. 773.Pp 774The 775.Fn kevent 776system call 777returns the number of events placed in the 778.Fa eventlist , 779up to the value given by 780.Fa nevents . 781If an error occurs while processing an element of the 782.Fa changelist 783and there is enough room in the 784.Fa eventlist , 785then the event will be placed in the 786.Fa eventlist 787with 788.Dv EV_ERROR 789set in 790.Va flags 791and the system error in 792.Va data . 793Otherwise, 794.Dv -1 795will be returned, and 796.Dv errno 797will be set to indicate the error condition. 798If the time limit expires, then 799.Fn kevent 800returns 0. 801.Sh EXAMPLES 802.Bd -literal -compact 803#include <sys/event.h> 804#include <err.h> 805#include <fcntl.h> 806#include <stdio.h> 807#include <stdlib.h> 808#include <string.h> 809 810int 811main(int argc, char **argv) 812{ 813 struct kevent event; /* Event we want to monitor */ 814 struct kevent tevent; /* Event triggered */ 815 int kq, fd, ret; 816 817 if (argc != 2) 818 err(EXIT_FAILURE, "Usage: %s path\en", argv[0]); 819 fd = open(argv[1], O_RDONLY); 820 if (fd == -1) 821 err(EXIT_FAILURE, "Failed to open '%s'", argv[1]); 822 823 /* Create kqueue. */ 824 kq = kqueue(); 825 if (kq == -1) 826 err(EXIT_FAILURE, "kqueue() failed"); 827 828 /* Initialize kevent structure. */ 829 EV_SET(&event, fd, EVFILT_VNODE, EV_ADD | EV_CLEAR, NOTE_WRITE, 830 0, NULL); 831 /* Attach event to the kqueue. */ 832 ret = kevent(kq, &event, 1, NULL, 0, NULL); 833 if (ret == -1) 834 err(EXIT_FAILURE, "kevent register"); 835 836 for (;;) { 837 /* Sleep until something happens. */ 838 ret = kevent(kq, NULL, 0, &tevent, 1, NULL); 839 if (ret == -1) { 840 err(EXIT_FAILURE, "kevent wait"); 841 } else if (ret > 0) { 842 if (tevent.flags & EV_ERROR) 843 errx(EXIT_FAILURE, "Event error: %s", strerror(event.data)); 844 else 845 printf("Something was written in '%s'\en", argv[1]); 846 } 847 } 848 849 /* kqueues are destroyed upon close() */ 850 (void)close(kq); 851 (void)close(fd); 852} 853.Ed 854.Sh ERRORS 855The 856.Fn kqueue 857system call fails if: 858.Bl -tag -width Er 859.It Bq Er ENOMEM 860The kernel failed to allocate enough memory for the kernel queue. 861.It Bq Er ENOMEM 862The 863.Dv RLIMIT_KQUEUES 864rlimit 865(see 866.Xr getrlimit 2 ) 867for the current user would be exceeded. 868.It Bq Er EMFILE 869The per-process descriptor table is full. 870.It Bq Er ENFILE 871The system file table is full. 872.El 873.Pp 874The 875.Fn kevent 876system call fails if: 877.Bl -tag -width Er 878.It Bq Er EACCES 879The process does not have permission to register a filter. 880.It Bq Er EFAULT 881There was an error reading or writing the 882.Va kevent 883structure. 884.It Bq Er EBADF 885The specified descriptor is invalid. 886.It Bq Er EINTR 887A signal was delivered before the timeout expired and before any 888events were placed on the kqueue for return. 889.It Bq Er EINTR 890A cancellation request was delivered to the thread, but not yet handled. 891.It Bq Er EINVAL 892The specified time limit or filter is invalid. 893.It Bq Er EINVAL 894The specified length of the event or change lists is negative. 895.It Bq Er ENOENT 896The event could not be found to be modified or deleted. 897.It Bq Er ENOMEM 898No memory was available to register the event 899or, in the special case of a timer, the maximum number of 900timers has been exceeded. 901This maximum is configurable via the 902.Va kern.kq_calloutmax 903sysctl. 904.It Bq Er ESRCH 905The specified process to attach to does not exist. 906.El 907.Pp 908When 909.Fn kevent 910call fails with 911.Er EINTR 912error, all changes in the 913.Fa changelist 914have been applied. 915.Sh SEE ALSO 916.Xr aio_error 2 , 917.Xr aio_read 2 , 918.Xr aio_return 2 , 919.Xr poll 2 , 920.Xr read 2 , 921.Xr select 2 , 922.Xr sigaction 2 , 923.Xr write 2 , 924.Xr pthread_setcancelstate 3 , 925.Xr signal 3 926.Rs 927.%A Jonathan Lemon 928.%T "Kqueue: A Generic and Scalable Event Notification Facility" 929.%I USENIX Association 930.%B Proceedings of the FREENIX Track: 2001 USENIX Annual Technical Conference 931.%D June 25-30, 2001 932.\".http://www.usenix.org/event/usenix01/freenix01/full_papers/lemon/lemon.pdf 933.Re 934.Sh HISTORY 935The 936.Fn kqueue 937and 938.Fn kevent 939system calls first appeared in 940.Fx 4.1 . 941The 942.Fn kqueuex 943system call 944and 945.Fn kevent1 946function first appeared in 947.Fx 14.0 . 948.Sh AUTHORS 949The 950.Fn kqueue 951system and this manual page were written by 952.An Jonathan Lemon Aq Mt jlemon@FreeBSD.org . 953.Sh BUGS 954.Pp 955In versions older than 956.Fx 12.0 , 957.In sys/event.h 958failed to parse without including 959.In sys/types.h 960manually. 961