1*0a6a1f1dSLionel Sambuc.\" $NetBSD: kqueue.2,v 1.34 2015/03/02 19:24:19 christos Exp $ 22fe8fb19SBen Gras.\" 32fe8fb19SBen Gras.\" Copyright (c) 2000 Jonathan Lemon 42fe8fb19SBen Gras.\" All rights reserved. 52fe8fb19SBen Gras.\" 62fe8fb19SBen Gras.\" Copyright (c) 2001, 2002, 2003 The NetBSD Foundation, Inc. 72fe8fb19SBen Gras.\" All rights reserved. 82fe8fb19SBen Gras.\" 92fe8fb19SBen Gras.\" Portions of this documentation is derived from text contributed by 102fe8fb19SBen Gras.\" Luke Mewburn. 112fe8fb19SBen Gras.\" 122fe8fb19SBen Gras.\" Redistribution and use in source and binary forms, with or without 132fe8fb19SBen Gras.\" modification, are permitted provided that the following conditions 142fe8fb19SBen Gras.\" are met: 152fe8fb19SBen Gras.\" 1. Redistributions of source code must retain the above copyright 162fe8fb19SBen Gras.\" notice, this list of conditions and the following disclaimer. 172fe8fb19SBen Gras.\" 2. Redistributions in binary form must reproduce the above copyright 182fe8fb19SBen Gras.\" notice, this list of conditions and the following disclaimer in the 192fe8fb19SBen Gras.\" documentation and/or other materials provided with the distribution. 202fe8fb19SBen Gras.\" 212fe8fb19SBen Gras.\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND 222fe8fb19SBen Gras.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 232fe8fb19SBen Gras.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 242fe8fb19SBen Gras.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 252fe8fb19SBen Gras.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 262fe8fb19SBen Gras.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 272fe8fb19SBen Gras.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 282fe8fb19SBen Gras.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 292fe8fb19SBen Gras.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 302fe8fb19SBen Gras.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 312fe8fb19SBen Gras.\" SUCH DAMAGE. 322fe8fb19SBen Gras.\" 332fe8fb19SBen Gras.\" $FreeBSD: src/lib/libc/sys/kqueue.2,v 1.22 2001/06/27 19:55:57 dd Exp $ 342fe8fb19SBen Gras.\" 35*0a6a1f1dSLionel Sambuc.Dd March 2, 2015 362fe8fb19SBen Gras.Dt KQUEUE 2 372fe8fb19SBen Gras.Os 382fe8fb19SBen Gras.Sh NAME 392fe8fb19SBen Gras.Nm kqueue , 40f14fb602SLionel Sambuc.Nm kqueue1 , 412fe8fb19SBen Gras.Nm kevent 422fe8fb19SBen Gras.Nd kernel event notification mechanism 432fe8fb19SBen Gras.Sh LIBRARY 442fe8fb19SBen Gras.Lb libc 452fe8fb19SBen Gras.Sh SYNOPSIS 462fe8fb19SBen Gras.In sys/event.h 472fe8fb19SBen Gras.In sys/time.h 482fe8fb19SBen Gras.Ft int 492fe8fb19SBen Gras.Fn kqueue "void" 502fe8fb19SBen Gras.Ft int 51f14fb602SLionel Sambuc.Fn kqueue1 "int flags" 52f14fb602SLionel Sambuc.Ft int 532fe8fb19SBen Gras.Fn kevent "int kq" "const struct kevent *changelist" "size_t nchanges" "struct kevent *eventlist" "size_t nevents" "const struct timespec *timeout" 542fe8fb19SBen Gras.Fn EV_SET "\*[Am]kev" ident filter flags fflags data udata 552fe8fb19SBen Gras.Sh DESCRIPTION 562fe8fb19SBen Gras.Fn kqueue 572fe8fb19SBen Grasprovides a generic method of notifying the user when an event 582fe8fb19SBen Grashappens or a condition holds, based on the results of small 592fe8fb19SBen Graspieces of kernel code termed filters. 602fe8fb19SBen GrasA kevent is identified by the (ident, filter) pair; there may only 612fe8fb19SBen Grasbe one unique kevent per kqueue. 622fe8fb19SBen Gras.Pp 632fe8fb19SBen GrasThe filter is executed upon the initial registration of a kevent 642fe8fb19SBen Grasin order to detect whether a preexisting condition is present, and is also 652fe8fb19SBen Grasexecuted whenever an event is passed to the filter for evaluation. 662fe8fb19SBen GrasIf the filter determines that the condition should be reported, 672fe8fb19SBen Grasthen the kevent is placed on the kqueue for the user to retrieve. 682fe8fb19SBen Gras.Pp 692fe8fb19SBen GrasThe filter is also run when the user attempts to retrieve the kevent 702fe8fb19SBen Grasfrom the kqueue. 712fe8fb19SBen GrasIf the filter indicates that the condition that triggered 722fe8fb19SBen Grasthe event no longer holds, the kevent is removed from the kqueue and 732fe8fb19SBen Grasis not returned. 742fe8fb19SBen Gras.Pp 752fe8fb19SBen GrasMultiple events which trigger the filter do not result in multiple 762fe8fb19SBen Graskevents being placed on the kqueue; instead, the filter will aggregate 772fe8fb19SBen Grasthe events into a single struct kevent. 782fe8fb19SBen GrasCalling 792fe8fb19SBen Gras.Fn close 802fe8fb19SBen Grason a file descriptor will remove any kevents that reference the descriptor. 812fe8fb19SBen Gras.Pp 822fe8fb19SBen Gras.Fn kqueue 832fe8fb19SBen Grascreates a new kernel event queue and returns a descriptor. 84f14fb602SLionel Sambuc.Pp 85f14fb602SLionel SambucThe 86f14fb602SLionel Sambuc.Fn kqueue1 87f14fb602SLionel Sambucalso allows to set the following 88f14fb602SLionel Sambuc.Fa flags 89f14fb602SLionel Sambucon the returned file descriptor: 90f14fb602SLionel Sambuc.Bl -column O_NONBLOCK -offset indent 91f14fb602SLionel Sambuc.It Dv O_CLOEXEC 92f14fb602SLionel SambucSet the close on exec property. 93f14fb602SLionel Sambuc.It Dv O_NONBLOCK 94f14fb602SLionel SambucSets non-blocking I/O. 95f14fb602SLionel Sambuc.It Dv O_NOSIGPIPE 96f14fb602SLionel SambucReturn 97f14fb602SLionel Sambuc.Er EPIPE 98f14fb602SLionel Sambucinstead of raising 99f14fb602SLionel Sambuc.Dv SIGPIPE . 100f14fb602SLionel Sambuc.El 1012fe8fb19SBen GrasThe queue is not inherited by a child created with 1022fe8fb19SBen Gras.Xr fork 2 . 1032fe8fb19SBen Gras.\" However, if 1042fe8fb19SBen Gras.\" .Xr rfork 2 1052fe8fb19SBen Gras.\" is called without the 1062fe8fb19SBen Gras.\" .Dv RFFDG 1072fe8fb19SBen Gras.\" flag, then the descriptor table is shared, 1082fe8fb19SBen Gras.\" which will allow sharing of the kqueue between two processes. 1092fe8fb19SBen Gras.Pp 1102fe8fb19SBen Gras.Fn kevent 1112fe8fb19SBen Grasis used to register events with the queue, and return any pending 1122fe8fb19SBen Grasevents to the user. 1132fe8fb19SBen Gras.Fa changelist 1142fe8fb19SBen Grasis a pointer to an array of 1152fe8fb19SBen Gras.Va kevent 1162fe8fb19SBen Grasstructures, as defined in 1172fe8fb19SBen Gras.In sys/event.h . 1182fe8fb19SBen GrasAll changes contained in the 1192fe8fb19SBen Gras.Fa changelist 1202fe8fb19SBen Grasare applied before any pending events are read from the queue. 1212fe8fb19SBen Gras.Fa nchanges 1222fe8fb19SBen Grasgives the size of 1232fe8fb19SBen Gras.Fa changelist . 1242fe8fb19SBen Gras.Fa eventlist 1252fe8fb19SBen Grasis a pointer to an array of kevent structures. 1262fe8fb19SBen Gras.Fa nevents 1272fe8fb19SBen Grasdetermines the size of 1282fe8fb19SBen Gras.Fa eventlist . 1292fe8fb19SBen GrasIf 1302fe8fb19SBen Gras.Fa timeout 1312fe8fb19SBen Grasis a 1322fe8fb19SBen Gras.No non- Ns Dv NULL 1332fe8fb19SBen Graspointer, it specifies a maximum interval to wait 1342fe8fb19SBen Grasfor an event, which will be interpreted as a struct timespec. 1352fe8fb19SBen GrasIf 1362fe8fb19SBen Gras.Fa timeout 1372fe8fb19SBen Grasis a 1382fe8fb19SBen Gras.Dv NULL 1392fe8fb19SBen Graspointer, 1402fe8fb19SBen Gras.Fn kevent 1412fe8fb19SBen Graswaits indefinitely. 1422fe8fb19SBen GrasTo effect a poll, the 1432fe8fb19SBen Gras.Fa timeout 1442fe8fb19SBen Grasargument should be 1452fe8fb19SBen Gras.No non- Ns Dv NULL , 1462fe8fb19SBen Graspointing to a zero-valued 1472fe8fb19SBen Gras.Va timespec 1482fe8fb19SBen Grasstructure. 1492fe8fb19SBen GrasThe same array may be used for the 1502fe8fb19SBen Gras.Fa changelist 1512fe8fb19SBen Grasand 1522fe8fb19SBen Gras.Fa eventlist . 1532fe8fb19SBen Gras.Pp 1542fe8fb19SBen Gras.Fn EV_SET 1552fe8fb19SBen Grasis a macro which is provided for ease of initializing a 1562fe8fb19SBen Graskevent structure. 1572fe8fb19SBen Gras.Pp 1582fe8fb19SBen GrasThe 1592fe8fb19SBen Gras.Va kevent 1602fe8fb19SBen Grasstructure is defined as: 1612fe8fb19SBen Gras.Bd -literal 1622fe8fb19SBen Grasstruct kevent { 1632fe8fb19SBen Gras uintptr_t ident; /* identifier for this event */ 1642fe8fb19SBen Gras uint32_t filter; /* filter for event */ 1652fe8fb19SBen Gras uint32_t flags; /* action flags for kqueue */ 1662fe8fb19SBen Gras uint32_t fflags; /* filter flag value */ 1672fe8fb19SBen Gras int64_t data; /* filter data value */ 1682fe8fb19SBen Gras intptr_t udata; /* opaque user data identifier */ 1692fe8fb19SBen Gras}; 1702fe8fb19SBen Gras.Ed 1712fe8fb19SBen Gras.Pp 1722fe8fb19SBen GrasThe fields of 1732fe8fb19SBen Gras.Fa struct kevent 1742fe8fb19SBen Grasare: 1752fe8fb19SBen Gras.Bl -tag -width XXXfilter -offset indent 1762fe8fb19SBen Gras.It ident 1772fe8fb19SBen GrasValue used to identify this event. 1782fe8fb19SBen GrasThe exact interpretation is determined by the attached filter, 1792fe8fb19SBen Grasbut often is a file descriptor. 1802fe8fb19SBen Gras.It filter 1812fe8fb19SBen GrasIdentifies the kernel filter used to process this event. 1822fe8fb19SBen GrasThere are pre-defined system filters (which are described below), and 1832fe8fb19SBen Grasother filters may be added by kernel subsystems as necessary. 1842fe8fb19SBen Gras.It flags 1852fe8fb19SBen GrasActions to perform on the event. 1862fe8fb19SBen Gras.It fflags 1872fe8fb19SBen GrasFilter-specific flags. 1882fe8fb19SBen Gras.It data 1892fe8fb19SBen GrasFilter-specific data value. 1902fe8fb19SBen Gras.It udata 1912fe8fb19SBen GrasOpaque user-defined value passed through the kernel unchanged. 1922fe8fb19SBen Gras.El 1932fe8fb19SBen Gras.Pp 1942fe8fb19SBen GrasThe 1952fe8fb19SBen Gras.Va flags 1962fe8fb19SBen Grasfield can contain the following values: 1972fe8fb19SBen Gras.Bl -tag -width XXXEV_ONESHOT -offset indent 1982fe8fb19SBen Gras.It EV_ADD 1992fe8fb19SBen GrasAdds the event to the kqueue. 2002fe8fb19SBen GrasRe-adding an existing event will modify the parameters of the original 2012fe8fb19SBen Grasevent, and not result in a duplicate entry. 2022fe8fb19SBen GrasAdding an event automatically enables it, 2032fe8fb19SBen Grasunless overridden by the EV_DISABLE flag. 2042fe8fb19SBen Gras.It EV_ENABLE 2052fe8fb19SBen GrasPermit 2062fe8fb19SBen Gras.Fn kevent 2072fe8fb19SBen Grasto return the event if it is triggered. 2082fe8fb19SBen Gras.It EV_DISABLE 2092fe8fb19SBen GrasDisable the event so 2102fe8fb19SBen Gras.Fn kevent 2112fe8fb19SBen Graswill not return it. 2122fe8fb19SBen GrasThe filter itself is not disabled. 2132fe8fb19SBen Gras.It EV_DELETE 2142fe8fb19SBen GrasRemoves the event from the kqueue. 2152fe8fb19SBen GrasEvents which are attached to file descriptors are automatically deleted 2162fe8fb19SBen Grason the last close of the descriptor. 2172fe8fb19SBen Gras.It EV_ONESHOT 2182fe8fb19SBen GrasCauses the event to return only the first occurrence of the filter 2192fe8fb19SBen Grasbeing triggered. 2202fe8fb19SBen GrasAfter the user retrieves the event from the kqueue, it is deleted. 2212fe8fb19SBen Gras.It EV_CLEAR 2222fe8fb19SBen GrasAfter the event is retrieved by the user, its state is reset. 2232fe8fb19SBen GrasThis is useful for filters which report state transitions 2242fe8fb19SBen Grasinstead of the current state. 2252fe8fb19SBen GrasNote that some filters may automatically set this flag internally. 2262fe8fb19SBen Gras.It EV_EOF 2272fe8fb19SBen GrasFilters may set this flag to indicate filter-specific EOF condition. 2282fe8fb19SBen Gras.It EV_ERROR 2292fe8fb19SBen GrasSee 2302fe8fb19SBen Gras.Sx RETURN VALUES 2312fe8fb19SBen Grasbelow. 2322fe8fb19SBen Gras.El 2332fe8fb19SBen Gras.Ss Filters 2342fe8fb19SBen GrasFilters are identified by a number. 2352fe8fb19SBen GrasThere are two types of filters; pre-defined filters which 2362fe8fb19SBen Grasare described below, and third-party filters that may be added with 2372fe8fb19SBen Gras.Xr kfilter_register 9 2382fe8fb19SBen Grasby kernel sub-systems, third-party device drivers, or loadable 2392fe8fb19SBen Graskernel modules. 2402fe8fb19SBen Gras.Pp 2412fe8fb19SBen GrasAs a third-party filter is referenced by a well-known name instead 2422fe8fb19SBen Grasof a statically assigned number, two 2432fe8fb19SBen Gras.Xr ioctl 2 Ns s 2442fe8fb19SBen Grasare supported on the file descriptor returned by 2452fe8fb19SBen Gras.Fn kqueue 2462fe8fb19SBen Grasto map a filter name to a filter number, and vice-versa (passing 2472fe8fb19SBen Grasarguments in a structure described below): 2482fe8fb19SBen Gras.Bl -tag -width KFILTER_BYFILTER -offset indent 2492fe8fb19SBen Gras.It KFILTER_BYFILTER 2502fe8fb19SBen GrasMap 2512fe8fb19SBen Gras.Va filter 2522fe8fb19SBen Grasto 2532fe8fb19SBen Gras.Va name , 2542fe8fb19SBen Graswhich is of size 2552fe8fb19SBen Gras.Va len . 2562fe8fb19SBen Gras.It KFILTER_BYNAME 2572fe8fb19SBen GrasMap 2582fe8fb19SBen Gras.Va name 2592fe8fb19SBen Grasto 2602fe8fb19SBen Gras.Va filter . 2612fe8fb19SBen Gras.Va len 2622fe8fb19SBen Grasis ignored. 2632fe8fb19SBen Gras.El 2642fe8fb19SBen Gras.Pp 2652fe8fb19SBen GrasThe following structure is used to pass arguments in and out of the 2662fe8fb19SBen Gras.Xr ioctl 2 : 2672fe8fb19SBen Gras.Bd -literal -offset indent 2682fe8fb19SBen Grasstruct kfilter_mapping { 2692fe8fb19SBen Gras char *name; /* name to lookup or return */ 2702fe8fb19SBen Gras size_t len; /* length of name */ 2712fe8fb19SBen Gras uint32_t filter; /* filter to lookup or return */ 2722fe8fb19SBen Gras}; 2732fe8fb19SBen Gras.Ed 2742fe8fb19SBen Gras.Pp 2752fe8fb19SBen GrasArguments may be passed to and from the filter via the 2762fe8fb19SBen Gras.Va fflags 2772fe8fb19SBen Grasand 2782fe8fb19SBen Gras.Va data 2792fe8fb19SBen Grasfields in the kevent structure. 2802fe8fb19SBen Gras.Pp 2812fe8fb19SBen GrasThe predefined system filters are: 2822fe8fb19SBen Gras.Bl -tag -width EVFILT_SIGNAL 2832fe8fb19SBen Gras.It EVFILT_READ 2842fe8fb19SBen GrasTakes a descriptor as the identifier, and returns whenever 2852fe8fb19SBen Grasthere is data available to read. 2862fe8fb19SBen GrasThe behavior of the filter is slightly different depending 2872fe8fb19SBen Grason the descriptor type. 2882fe8fb19SBen Gras.Pp 2892fe8fb19SBen Gras.Bl -tag -width 2n 2902fe8fb19SBen Gras.It Sockets 2912fe8fb19SBen GrasSockets which have previously been passed to 2922fe8fb19SBen Gras.Fn listen 2932fe8fb19SBen Grasreturn when there is an incoming connection pending. 2942fe8fb19SBen Gras.Va data 2952fe8fb19SBen Grascontains the size of the listen backlog (i.e., the number of 2962fe8fb19SBen Grasconnections ready to be accepted with 2972fe8fb19SBen Gras.Xr accept 2 . ) 2982fe8fb19SBen Gras.Pp 2992fe8fb19SBen GrasOther socket descriptors return when there is data to be read, 3002fe8fb19SBen Grassubject to the 3012fe8fb19SBen Gras.Dv SO_RCVLOWAT 3022fe8fb19SBen Grasvalue of the socket buffer. 3032fe8fb19SBen GrasThis may be overridden with a per-filter low water mark at the 3042fe8fb19SBen Grastime the filter is added by setting the 3052fe8fb19SBen GrasNOTE_LOWAT 3062fe8fb19SBen Grasflag in 3072fe8fb19SBen Gras.Va fflags , 3082fe8fb19SBen Grasand specifying the new low water mark in 3092fe8fb19SBen Gras.Va data . 3102fe8fb19SBen GrasOn return, 3112fe8fb19SBen Gras.Va data 3122fe8fb19SBen Grascontains the number of bytes in the socket buffer. 3132fe8fb19SBen Gras.Pp 3142fe8fb19SBen GrasIf the read direction of the socket has shutdown, then the filter 3152fe8fb19SBen Grasalso sets EV_EOF in 3162fe8fb19SBen Gras.Va flags , 3172fe8fb19SBen Grasand returns the socket error (if any) in 3182fe8fb19SBen Gras.Va fflags . 3192fe8fb19SBen GrasIt is possible for EOF to be returned (indicating the connection is gone) 3202fe8fb19SBen Graswhile there is still data pending in the socket buffer. 3212fe8fb19SBen Gras.It Vnodes 3222fe8fb19SBen GrasReturns when the file pointer is not at the end of file. 3232fe8fb19SBen Gras.Va data 3242fe8fb19SBen Grascontains the offset from current position to end of file, 3252fe8fb19SBen Grasand may be negative. 3262fe8fb19SBen Gras.It "Fifos, Pipes" 3272fe8fb19SBen GrasReturns when there is data to read; 3282fe8fb19SBen Gras.Va data 3292fe8fb19SBen Grascontains the number of bytes available. 3302fe8fb19SBen Gras.Pp 3312fe8fb19SBen GrasWhen the last writer disconnects, the filter will set EV_EOF in 3322fe8fb19SBen Gras.Va flags . 3332fe8fb19SBen GrasThis may be cleared by passing in EV_CLEAR, at which point the 3342fe8fb19SBen Grasfilter will resume waiting for data to become available before 3352fe8fb19SBen Grasreturning. 3362fe8fb19SBen Gras.El 3372fe8fb19SBen Gras.It EVFILT_WRITE 3382fe8fb19SBen GrasTakes a descriptor as the identifier, and returns whenever 3392fe8fb19SBen Grasit is possible to write to the descriptor. 3402fe8fb19SBen GrasFor sockets, pipes, fifos, and ttys, 3412fe8fb19SBen Gras.Va data 3422fe8fb19SBen Graswill contain the amount of space remaining in the write buffer. 3432fe8fb19SBen GrasThe filter will set EV_EOF when the reader disconnects, and for 3442fe8fb19SBen Grasthe fifo case, this may be cleared by use of EV_CLEAR. 3452fe8fb19SBen GrasNote that this filter is not supported for vnodes. 3462fe8fb19SBen Gras.Pp 3472fe8fb19SBen GrasFor sockets, the low water mark and socket error handling is 3482fe8fb19SBen Grasidentical to the EVFILT_READ case. 3492fe8fb19SBen Gras.It EVFILT_AIO 3502fe8fb19SBen GrasThis is not implemented in 3512fe8fb19SBen Gras.Nx . 3522fe8fb19SBen Gras.ig 3532fe8fb19SBen GrasThe sigevent portion of the AIO request is filled in, with 3542fe8fb19SBen Gras.Va sigev_notify_kqueue 3552fe8fb19SBen Grascontaining the descriptor of the kqueue that the event should 3562fe8fb19SBen Grasbe attached to, 3572fe8fb19SBen Gras.Va sigev_value 3582fe8fb19SBen Grascontaining the udata value, and 3592fe8fb19SBen Gras.Va sigev_notify 3602fe8fb19SBen Grasset to SIGEV_EVENT. 3612fe8fb19SBen GrasWhen the aio_* function is called, the event will be registered 3622fe8fb19SBen Graswith the specified kqueue, and the 3632fe8fb19SBen Gras.Va ident 3642fe8fb19SBen Grasargument set to the 3652fe8fb19SBen Gras.Fa struct aiocb 3662fe8fb19SBen Grasreturned by the aio_* function. 3672fe8fb19SBen GrasThe filter returns under the same conditions as aio_error. 3682fe8fb19SBen Gras.Pp 3692fe8fb19SBen GrasAlternatively, a kevent structure may be initialized, with 3702fe8fb19SBen Gras.Va ident 3712fe8fb19SBen Grascontaining the descriptor of the kqueue, and the 3722fe8fb19SBen Grasaddress of the kevent structure placed in the 3732fe8fb19SBen Gras.Va aio_lio_opcode 3742fe8fb19SBen Grasfield of the AIO request. 3752fe8fb19SBen GrasHowever, this approach will not work on 3762fe8fb19SBen Grasarchitectures with 64-bit pointers, and should be considered deprecated. 3772fe8fb19SBen Gras.. 3782fe8fb19SBen Gras.It EVFILT_VNODE 3792fe8fb19SBen GrasTakes a file descriptor as the identifier and the events to watch for in 3802fe8fb19SBen Gras.Va fflags , 3812fe8fb19SBen Grasand returns when one or more of the requested events occurs on the descriptor. 3822fe8fb19SBen GrasThe events to monitor are: 3832fe8fb19SBen Gras.Bl -tag -width XXNOTE_RENAME 3842fe8fb19SBen Gras.It NOTE_DELETE 3852fe8fb19SBen Gras.Fn unlink 3862fe8fb19SBen Graswas called on the file referenced by the descriptor. 3872fe8fb19SBen Gras.It NOTE_WRITE 3882fe8fb19SBen GrasA write occurred on the file referenced by the descriptor. 3892fe8fb19SBen Gras.It NOTE_EXTEND 3902fe8fb19SBen GrasThe file referenced by the descriptor was extended. 3912fe8fb19SBen Gras.It NOTE_ATTRIB 3922fe8fb19SBen GrasThe file referenced by the descriptor had its attributes changed. 3932fe8fb19SBen Gras.It NOTE_LINK 3942fe8fb19SBen GrasThe link count on the file changed. 3952fe8fb19SBen Gras.It NOTE_RENAME 3962fe8fb19SBen GrasThe file referenced by the descriptor was renamed. 3972fe8fb19SBen Gras.It NOTE_REVOKE 3982fe8fb19SBen GrasAccess to the file was revoked via 3992fe8fb19SBen Gras.Xr revoke 2 4002fe8fb19SBen Grasor the underlying fileystem was unmounted. 4012fe8fb19SBen Gras.El 4022fe8fb19SBen Gras.Pp 4032fe8fb19SBen GrasOn return, 4042fe8fb19SBen Gras.Va fflags 4052fe8fb19SBen Grascontains the events which triggered the filter. 4062fe8fb19SBen Gras.It EVFILT_PROC 4072fe8fb19SBen GrasTakes the process ID to monitor as the identifier and the events to watch for 4082fe8fb19SBen Grasin 4092fe8fb19SBen Gras.Va fflags , 4102fe8fb19SBen Grasand returns when the process performs one or more of the requested events. 4112fe8fb19SBen GrasIf a process can normally see another process, it can attach an event to it. 4122fe8fb19SBen GrasThe events to monitor are: 4132fe8fb19SBen Gras.Bl -tag -width XXNOTE_TRACKERR 4142fe8fb19SBen Gras.It NOTE_EXIT 4152fe8fb19SBen GrasThe process has exited. 416*0a6a1f1dSLionel SambucThe exit code of the process is stored in 417*0a6a1f1dSLionel Sambuc.Va data . 4182fe8fb19SBen Gras.It NOTE_FORK 4192fe8fb19SBen GrasThe process has called 4202fe8fb19SBen Gras.Fn fork . 4212fe8fb19SBen Gras.It NOTE_EXEC 4222fe8fb19SBen GrasThe process has executed a new process via 4232fe8fb19SBen Gras.Xr execve 2 4242fe8fb19SBen Grasor similar call. 4252fe8fb19SBen Gras.It NOTE_TRACK 4262fe8fb19SBen GrasFollow a process across 4272fe8fb19SBen Gras.Fn fork 4282fe8fb19SBen Grascalls. 4292fe8fb19SBen GrasThe parent process will return with NOTE_TRACK set in the 4302fe8fb19SBen Gras.Va fflags 4312fe8fb19SBen Grasfield, while the child process will return with NOTE_CHILD set in 4322fe8fb19SBen Gras.Va fflags 4332fe8fb19SBen Grasand the parent PID in 4342fe8fb19SBen Gras.Va data . 4352fe8fb19SBen Gras.It NOTE_TRACKERR 4362fe8fb19SBen GrasThis flag is returned if the system was unable to attach an event to 4372fe8fb19SBen Grasthe child process, usually due to resource limitations. 4382fe8fb19SBen Gras.El 4392fe8fb19SBen Gras.Pp 4402fe8fb19SBen GrasOn return, 4412fe8fb19SBen Gras.Va fflags 4422fe8fb19SBen Grascontains the events which triggered the filter. 4432fe8fb19SBen Gras.It EVFILT_SIGNAL 4442fe8fb19SBen GrasTakes the signal number to monitor as the identifier and returns 4452fe8fb19SBen Graswhen the given signal is delivered to the current process. 4462fe8fb19SBen GrasThis coexists with the 4472fe8fb19SBen Gras.Fn signal 4482fe8fb19SBen Grasand 4492fe8fb19SBen Gras.Fn sigaction 4502fe8fb19SBen Grasfacilities, and has a lower precedence. 4512fe8fb19SBen GrasThe filter will record 4522fe8fb19SBen Grasall attempts to deliver a signal to a process, even if the signal has 4532fe8fb19SBen Grasbeen marked as SIG_IGN. 4542fe8fb19SBen GrasEvent notification happens after normal signal delivery processing. 4552fe8fb19SBen Gras.Va data 4562fe8fb19SBen Grasreturns the number of times the signal has occurred since the last call to 4572fe8fb19SBen Gras.Fn kevent . 4582fe8fb19SBen GrasThis filter automatically sets the EV_CLEAR flag internally. 4592fe8fb19SBen Gras.It EVFILT_TIMER 4602fe8fb19SBen GrasEstablishes an arbitrary timer identified by 4612fe8fb19SBen Gras.Va ident . 4622fe8fb19SBen GrasWhen adding a timer, 4632fe8fb19SBen Gras.Va data 4642fe8fb19SBen Grasspecifies the timeout period in milliseconds. 4652fe8fb19SBen GrasThe timer will be periodic unless EV_ONESHOT is specified. 4662fe8fb19SBen GrasOn return, 4672fe8fb19SBen Gras.Va data 4682fe8fb19SBen Grascontains the number of times the timeout has expired since the last call to 4692fe8fb19SBen Gras.Fn kevent . 4702fe8fb19SBen GrasThis filter automatically sets the EV_CLEAR flag internally. 4712fe8fb19SBen Gras.El 4722fe8fb19SBen Gras.Sh RETURN VALUES 4732fe8fb19SBen Gras.Fn kqueue 4742fe8fb19SBen Grascreates a new kernel event queue and returns a file descriptor. 4752fe8fb19SBen GrasIf there was an error creating the kernel event queue, a value of \-1 is 4762fe8fb19SBen Grasreturned and errno set. 4772fe8fb19SBen Gras.Pp 4782fe8fb19SBen Gras.Fn kevent 4792fe8fb19SBen Grasreturns the number of events placed in the 4802fe8fb19SBen Gras.Fa eventlist , 4812fe8fb19SBen Grasup to the value given by 4822fe8fb19SBen Gras.Fa nevents . 4832fe8fb19SBen GrasIf an error occurs while processing an element of the 4842fe8fb19SBen Gras.Fa changelist 4852fe8fb19SBen Grasand there is enough room in the 4862fe8fb19SBen Gras.Fa eventlist , 4872fe8fb19SBen Grasthen the event will be placed in the 4882fe8fb19SBen Gras.Fa eventlist 4892fe8fb19SBen Graswith 4902fe8fb19SBen Gras.Dv EV_ERROR 4912fe8fb19SBen Grasset in 4922fe8fb19SBen Gras.Va flags 4932fe8fb19SBen Grasand the system error in 4942fe8fb19SBen Gras.Va data . 4952fe8fb19SBen GrasOtherwise, 4962fe8fb19SBen Gras.Dv \-1 4972fe8fb19SBen Graswill be returned, and 4982fe8fb19SBen Gras.Dv errno 4992fe8fb19SBen Graswill be set to indicate the error condition. 5002fe8fb19SBen GrasIf the time limit expires, then 5012fe8fb19SBen Gras.Fn kevent 5022fe8fb19SBen Grasreturns 0. 5032fe8fb19SBen Gras.Sh EXAMPLES 5042fe8fb19SBen GrasThe following example program monitors a file (provided to it as the first 5052fe8fb19SBen Grasargument) and prints information about some common events it receives 5062fe8fb19SBen Grasnotifications for: 5072fe8fb19SBen Gras.Bd -literal -offset indent 5082fe8fb19SBen Gras#include \*[Lt]sys/types.h\*[Gt] 5092fe8fb19SBen Gras#include \*[Lt]sys/event.h\*[Gt] 5102fe8fb19SBen Gras#include \*[Lt]sys/time.h\*[Gt] 5112fe8fb19SBen Gras#include \*[Lt]stdio.h\*[Gt] 5122fe8fb19SBen Gras#include \*[Lt]unistd.h\*[Gt] 5132fe8fb19SBen Gras#include \*[Lt]stdlib.h\*[Gt] 5142fe8fb19SBen Gras#include \*[Lt]fcntl.h\*[Gt] 5152fe8fb19SBen Gras#include \*[Lt]err.h\*[Gt] 5162fe8fb19SBen Gras 5172fe8fb19SBen Grasint 5182fe8fb19SBen Grasmain(int argc, char *argv[]) 5192fe8fb19SBen Gras{ 5202fe8fb19SBen Gras int fd, kq, nev; 5212fe8fb19SBen Gras struct kevent ev; 5222fe8fb19SBen Gras static const struct timespec tout = { 1, 0 }; 5232fe8fb19SBen Gras 5242fe8fb19SBen Gras if ((fd = open(argv[1], O_RDONLY)) == -1) 5252fe8fb19SBen Gras err(1, "Cannot open `%s'", argv[1]); 5262fe8fb19SBen Gras 5272fe8fb19SBen Gras if ((kq = kqueue()) == -1) 5282fe8fb19SBen Gras err(1, "Cannot create kqueue"); 5292fe8fb19SBen Gras 5302fe8fb19SBen Gras EV_SET(\*[Am]ev, fd, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR, 5312fe8fb19SBen Gras NOTE_DELETE|NOTE_WRITE|NOTE_EXTEND|NOTE_ATTRIB|NOTE_LINK| 5322fe8fb19SBen Gras NOTE_RENAME|NOTE_REVOKE, 0, 0); 533f14fb602SLionel Sambuc if (kevent(kq, \*[Am]ev, 1, NULL, 0, \*[Am]tout) == -1) 5342fe8fb19SBen Gras err(1, "kevent"); 5352fe8fb19SBen Gras for (;;) { 5362fe8fb19SBen Gras nev = kevent(kq, NULL, 0, \*[Am]ev, 1, \*[Am]tout); 5372fe8fb19SBen Gras if (nev == -1) 5382fe8fb19SBen Gras err(1, "kevent"); 5392fe8fb19SBen Gras if (nev == 0) 5402fe8fb19SBen Gras continue; 5412fe8fb19SBen Gras if (ev.fflags \*[Am] NOTE_DELETE) { 5422fe8fb19SBen Gras printf("deleted "); 5432fe8fb19SBen Gras ev.fflags \*[Am]= ~NOTE_DELETE; 5442fe8fb19SBen Gras } 5452fe8fb19SBen Gras if (ev.fflags \*[Am] NOTE_WRITE) { 5462fe8fb19SBen Gras printf("written "); 5472fe8fb19SBen Gras ev.fflags \*[Am]= ~NOTE_WRITE; 5482fe8fb19SBen Gras } 5492fe8fb19SBen Gras if (ev.fflags \*[Am] NOTE_EXTEND) { 5502fe8fb19SBen Gras printf("extended "); 5512fe8fb19SBen Gras ev.fflags \*[Am]= ~NOTE_EXTEND; 5522fe8fb19SBen Gras } 5532fe8fb19SBen Gras if (ev.fflags \*[Am] NOTE_ATTRIB) { 5542fe8fb19SBen Gras printf("chmod/chown/utimes "); 5552fe8fb19SBen Gras ev.fflags \*[Am]= ~NOTE_ATTRIB; 5562fe8fb19SBen Gras } 5572fe8fb19SBen Gras if (ev.fflags \*[Am] NOTE_LINK) { 5582fe8fb19SBen Gras printf("hardlinked "); 5592fe8fb19SBen Gras ev.fflags \*[Am]= ~NOTE_LINK; 5602fe8fb19SBen Gras } 5612fe8fb19SBen Gras if (ev.fflags \*[Am] NOTE_RENAME) { 5622fe8fb19SBen Gras printf("renamed "); 5632fe8fb19SBen Gras ev.fflags \*[Am]= ~NOTE_RENAME; 5642fe8fb19SBen Gras } 5652fe8fb19SBen Gras if (ev.fflags \*[Am] NOTE_REVOKE) { 5662fe8fb19SBen Gras printf("revoked "); 5672fe8fb19SBen Gras ev.fflags \*[Am]= ~NOTE_REVOKE; 5682fe8fb19SBen Gras } 5692fe8fb19SBen Gras printf("\\n"); 5702fe8fb19SBen Gras if (ev.fflags) 5712fe8fb19SBen Gras warnx("unknown event 0x%x\\n", ev.fflags); 5722fe8fb19SBen Gras } 5732fe8fb19SBen Gras} 5742fe8fb19SBen Gras.Ed 5752fe8fb19SBen Gras.Sh ERRORS 5762fe8fb19SBen GrasThe 5772fe8fb19SBen Gras.Fn kqueue 5782fe8fb19SBen Grasfunction fails if: 5792fe8fb19SBen Gras.Bl -tag -width Er 5802fe8fb19SBen Gras.It Bq Er EMFILE 5812fe8fb19SBen GrasThe per-process descriptor table is full. 5822fe8fb19SBen Gras.It Bq Er ENFILE 5832fe8fb19SBen GrasThe system file table is full. 5842fe8fb19SBen Gras.It Bq Er ENOMEM 5852fe8fb19SBen GrasThe kernel failed to allocate enough memory for the kernel queue. 5862fe8fb19SBen Gras.El 5872fe8fb19SBen Gras.Pp 5882fe8fb19SBen GrasThe 5892fe8fb19SBen Gras.Fn kevent 5902fe8fb19SBen Grasfunction fails if: 5912fe8fb19SBen Gras.Bl -tag -width Er 5922fe8fb19SBen Gras.It Bq Er EACCES 5932fe8fb19SBen GrasThe process does not have permission to register a filter. 5942fe8fb19SBen Gras.It Bq Er EBADF 5952fe8fb19SBen GrasThe specified descriptor is invalid. 5962fe8fb19SBen Gras.It Bq Er EFAULT 5972fe8fb19SBen GrasThere was an error reading or writing the 5982fe8fb19SBen Gras.Va kevent 5992fe8fb19SBen Grasstructure. 6002fe8fb19SBen Gras.It Bq Er EINTR 6012fe8fb19SBen GrasA signal was delivered before the timeout expired and before any 6022fe8fb19SBen Grasevents were placed on the kqueue for return. 6032fe8fb19SBen Gras.It Bq Er EINVAL 6042fe8fb19SBen GrasThe specified time limit or filter is invalid. 6052fe8fb19SBen Gras.It Bq Er ENOENT 6062fe8fb19SBen GrasThe event could not be found to be modified or deleted. 6072fe8fb19SBen Gras.It Bq Er ENOMEM 6082fe8fb19SBen GrasNo memory was available to register the event. 60984d9c625SLionel Sambuc.It Bq Er EOPNOTSUPP 61084d9c625SLionel SambucThis type of file descriptor is not supported for 61184d9c625SLionel Sambuc.Fn kevent 61284d9c625SLionel Sambucoperations. 6132fe8fb19SBen Gras.It Bq Er ESRCH 6142fe8fb19SBen GrasThe specified process to attach to does not exist. 6152fe8fb19SBen Gras.El 6162fe8fb19SBen Gras.Sh SEE ALSO 6172fe8fb19SBen Gras.\" .Xr aio_error 2 , 6182fe8fb19SBen Gras.\" .Xr aio_read 2 , 6192fe8fb19SBen Gras.\" .Xr aio_return 2 , 6202fe8fb19SBen Gras.Xr ioctl 2 , 6212fe8fb19SBen Gras.Xr poll 2 , 6222fe8fb19SBen Gras.Xr read 2 , 6232fe8fb19SBen Gras.Xr select 2 , 6242fe8fb19SBen Gras.Xr sigaction 2 , 6252fe8fb19SBen Gras.Xr write 2 , 6262fe8fb19SBen Gras.Xr signal 3 , 6272fe8fb19SBen Gras.Xr kfilter_register 9 , 6282fe8fb19SBen Gras.Xr knote 9 6292fe8fb19SBen Gras.Rs 6302fe8fb19SBen Gras.%A Jonathan Lemon 6312fe8fb19SBen Gras.%T "Kqueue: A Generic and Scalable Event Notification Facility" 6322fe8fb19SBen Gras.%I USENIX Association 6332fe8fb19SBen Gras.%B Proceedings of the FREENIX Track: 2001 USENIX Annual Technical Conference 6342fe8fb19SBen Gras.%D June 25-30, 2001 6352fe8fb19SBen Gras.%U http://www.usenix.org/event/usenix01/freenix01/full_papers/lemon/lemon.pdf 6362fe8fb19SBen Gras.Re 6372fe8fb19SBen Gras.Sh HISTORY 6382fe8fb19SBen GrasThe 6392fe8fb19SBen Gras.Fn kqueue 6402fe8fb19SBen Grasand 6412fe8fb19SBen Gras.Fn kevent 6422fe8fb19SBen Grasfunctions first appeared in 6432fe8fb19SBen Gras.Fx 4.1 , 6442fe8fb19SBen Grasand then in 6452fe8fb19SBen Gras.Nx 2.0 . 646f14fb602SLionel SambucThe 647f14fb602SLionel Sambuc.Fn kqueue1 648f14fb602SLionel Sambucfunction first appeared in 649f14fb602SLionel Sambuc.Nx 6.0 . 650