xref: /freebsd-14.2/lib/libc/sys/kqueue.2 (revision fa9896e0)
1ad35a4c9SJonathan Lemon.\" Copyright (c) 2000 Jonathan Lemon
2ad35a4c9SJonathan Lemon.\" All rights reserved.
3ad35a4c9SJonathan Lemon.\"
4ad35a4c9SJonathan Lemon.\" Redistribution and use in source and binary forms, with or without
5ad35a4c9SJonathan Lemon.\" modification, are permitted provided that the following conditions
6ad35a4c9SJonathan Lemon.\" are met:
7ad35a4c9SJonathan Lemon.\" 1. Redistributions of source code must retain the above copyright
8ad35a4c9SJonathan Lemon.\"    notice, this list of conditions and the following disclaimer.
9ad35a4c9SJonathan Lemon.\" 2. Redistributions in binary form must reproduce the above copyright
10ad35a4c9SJonathan Lemon.\"    notice, this list of conditions and the following disclaimer in the
11ad35a4c9SJonathan Lemon.\"    documentation and/or other materials provided with the distribution.
12ad35a4c9SJonathan Lemon.\"
13ad35a4c9SJonathan Lemon.\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND
14ad35a4c9SJonathan Lemon.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15ad35a4c9SJonathan Lemon.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16ad35a4c9SJonathan Lemon.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17ad35a4c9SJonathan Lemon.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18ad35a4c9SJonathan Lemon.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19ad35a4c9SJonathan Lemon.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20ad35a4c9SJonathan Lemon.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21ad35a4c9SJonathan Lemon.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22ad35a4c9SJonathan Lemon.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23ad35a4c9SJonathan Lemon.\" SUCH DAMAGE.
24ad35a4c9SJonathan Lemon.\"
25f2ec444bSKonstantin Belousov.Dd March 26, 2023
26ad35a4c9SJonathan Lemon.Dt KQUEUE 2
27ad35a4c9SJonathan Lemon.Os
28ad35a4c9SJonathan Lemon.Sh NAME
294451bb5dSJonathan Lemon.Nm kqueue ,
304451bb5dSJonathan Lemon.Nm kevent
31ad35a4c9SJonathan Lemon.Nd kernel event notification mechanism
324451bb5dSJonathan Lemon.Sh LIBRARY
334451bb5dSJonathan Lemon.Lb libc
34ad35a4c9SJonathan Lemon.Sh SYNOPSIS
3532eef9aeSRuslan Ermilov.In sys/event.h
36ad35a4c9SJonathan Lemon.Ft int
37ad35a4c9SJonathan Lemon.Fn kqueue "void"
38ad35a4c9SJonathan Lemon.Ft int
39dac31024SKonstantin Belousov.Fn kqueuex "u_int flags"
40f2ec444bSKonstantin Belousov.Ft int
410ef405eeSKonstantin Belousov.Fo kevent
420ef405eeSKonstantin Belousov.Fa "int kq"
430ef405eeSKonstantin Belousov.Fa "const struct kevent *changelist"
440ef405eeSKonstantin Belousov.Fa "int nchanges"
450ef405eeSKonstantin Belousov.Fa "struct kevent *eventlist"
460ef405eeSKonstantin Belousov.Fa "int nevents"
470ef405eeSKonstantin Belousov.Fa "const struct timespec *timeout"
480ef405eeSKonstantin Belousov.Fc
49b072e86dSKonstantin Belousov.Fn EV_SET "kev" ident filter flags fflags data udata
50ad35a4c9SJonathan Lemon.Sh DESCRIPTION
512faeeff4SRuslan ErmilovThe
52ad35a4c9SJonathan Lemon.Fn kqueue
532faeeff4SRuslan Ermilovsystem call
54ad35a4c9SJonathan Lemonprovides a generic method of notifying the user when an event
55ad35a4c9SJonathan Lemonhappens or a condition holds, based on the results of small
56ad35a4c9SJonathan Lemonpieces of kernel code termed filters.
573c63f48cSSheldon HearnA kevent is identified by the (ident, filter) pair; there may only
58ad35a4c9SJonathan Lemonbe one unique kevent per kqueue.
594451bb5dSJonathan Lemon.Pp
60ad35a4c9SJonathan LemonThe filter is executed upon the initial registration of a kevent
613c63f48cSSheldon Hearnin order to detect whether a preexisting condition is present, and is also
62ad35a4c9SJonathan Lemonexecuted whenever an event is passed to the filter for evaluation.
63ad35a4c9SJonathan LemonIf the filter determines that the condition should be reported,
64ad35a4c9SJonathan Lemonthen the kevent is placed on the kqueue for the user to retrieve.
654451bb5dSJonathan Lemon.Pp
66ad35a4c9SJonathan LemonThe filter is also run when the user attempts to retrieve the kevent
673c63f48cSSheldon Hearnfrom the kqueue.
683c63f48cSSheldon HearnIf the filter indicates that the condition that triggered
69ad35a4c9SJonathan Lemonthe event no longer holds, the kevent is removed from the kqueue and
70ad35a4c9SJonathan Lemonis not returned.
714451bb5dSJonathan Lemon.Pp
72ad35a4c9SJonathan LemonMultiple events which trigger the filter do not result in multiple
73a269a14fSKyle Evanskevents being placed on the kqueue; instead, the filter will aggregate
74a269a14fSKyle Evansthe events into a single struct kevent.
75ad35a4c9SJonathan LemonCalling
76ad35a4c9SJonathan Lemon.Fn close
77ad35a4c9SJonathan Lemonon a file descriptor will remove any kevents that reference the descriptor.
78ad35a4c9SJonathan Lemon.Pp
792faeeff4SRuslan ErmilovThe
80ad35a4c9SJonathan Lemon.Fn kqueue
812faeeff4SRuslan Ermilovsystem call
82ad35a4c9SJonathan Lemoncreates a new kernel event queue and returns a descriptor.
83af7b56f4SJohn-Mark GurneyThe queue is not inherited by a child created with
84af7b56f4SJohn-Mark Gurney.Xr fork 2 .
853c63f48cSSheldon HearnHowever, if
863c63f48cSSheldon Hearn.Xr rfork 2
87ee85cf8bSJonathan Lemonis called without the
883c63f48cSSheldon Hearn.Dv RFFDG
89ee85cf8bSJonathan Lemonflag, then the descriptor table is shared,
90af7b56f4SJohn-Mark Gurneywhich will allow sharing of the kqueue between two processes.
91ad35a4c9SJonathan Lemon.Pp
922faeeff4SRuslan ErmilovThe
93dac31024SKonstantin Belousov.Fn kqueuex
94f2ec444bSKonstantin Belousovsystem call also creates a new kernel event queue, and additionally takes
95f2ec444bSKonstantin Belousovthe
96f2ec444bSKonstantin Belousov.Fa flags
97f2ec444bSKonstantin Belousovargument, which is a bitwise-inclusive OR of the following flags:
98f2ec444bSKonstantin Belousov.Bl -tag -width "KQUEUE_CLOEXEC"
99f2ec444bSKonstantin Belousov.It Fa KQUEUE_CLOEXEC
100f2ec444bSKonstantin BelousovThe returned file descriptor is automatically closed on
101f2ec444bSKonstantin Belousov.Xr execve 2
102f2ec444bSKonstantin Belousov.El
103f2ec444bSKonstantin BelousovThe
104f2ec444bSKonstantin Belousov.Ql fd = kqueue()
105f2ec444bSKonstantin Belousovcall is equivalent to
106dac31024SKonstantin Belousov.Ql fd = kqueuex(0) .
107f2ec444bSKonstantin Belousov.Pp
108*54579376SKonstantin BelousovFor compatibility with
109*54579376SKonstantin Belousov.Nx ,
110*54579376SKonstantin Belousovthe
111*54579376SKonstantin Belousov.Fn kqueue1
112*54579376SKonstantin Belousovfunction is provided, which accepts the
113*54579376SKonstantin Belousov.Dv O_CLOEXEC
114*54579376SKonstantin Belousovflag with the expected semantic.
115*54579376SKonstantin Belousov.Pp
116f2ec444bSKonstantin BelousovThe
117ad35a4c9SJonathan Lemon.Fn kevent
1182faeeff4SRuslan Ermilovsystem call
119ad35a4c9SJonathan Lemonis used to register events with the queue, and return any pending
120ad35a4c9SJonathan Lemonevents to the user.
1212efeeba5SRuslan ErmilovThe
122ad35a4c9SJonathan Lemon.Fa changelist
1232efeeba5SRuslan Ermilovargument
124a8e65b91SJonathan Lemonis a pointer to an array of
125ad35a4c9SJonathan Lemon.Va kevent
126ad35a4c9SJonathan Lemonstructures, as defined in
127fe08efe6SRuslan Ermilov.In sys/event.h .
128ad35a4c9SJonathan LemonAll changes contained in the
129ad35a4c9SJonathan Lemon.Fa changelist
130ad35a4c9SJonathan Lemonare applied before any pending events are read from the queue.
1312efeeba5SRuslan ErmilovThe
132ad35a4c9SJonathan Lemon.Fa nchanges
1332efeeba5SRuslan Ermilovargument
134ad35a4c9SJonathan Lemongives the size of
135ad35a4c9SJonathan Lemon.Fa changelist .
1362efeeba5SRuslan ErmilovThe
137ad35a4c9SJonathan Lemon.Fa eventlist
1382efeeba5SRuslan Ermilovargument
139ad35a4c9SJonathan Lemonis a pointer to an array of kevent structures.
1402efeeba5SRuslan ErmilovThe
141ad35a4c9SJonathan Lemon.Fa nevents
1422efeeba5SRuslan Ermilovargument
143ad35a4c9SJonathan Lemondetermines the size of
144ad35a4c9SJonathan Lemon.Fa eventlist .
145634862b3SJohn-Mark GurneyWhen
146634862b3SJohn-Mark Gurney.Fa nevents
147634862b3SJohn-Mark Gurneyis zero,
148634862b3SJohn-Mark Gurney.Fn kevent
149634862b3SJohn-Mark Gurneywill return immediately even if there is a
150634862b3SJohn-Mark Gurney.Fa timeout
151634862b3SJohn-Mark Gurneyspecified unlike
152634862b3SJohn-Mark Gurney.Xr select 2 .
153ad35a4c9SJonathan LemonIf
154ad35a4c9SJonathan Lemon.Fa timeout
155ad35a4c9SJonathan Lemonis a non-NULL pointer, it specifies a maximum interval to wait
1561a0a9345SRuslan Ermilovfor an event, which will be interpreted as a struct timespec.
1571a0a9345SRuslan ErmilovIf
158ad35a4c9SJonathan Lemon.Fa timeout
159ad35a4c9SJonathan Lemonis a NULL pointer,
160ad35a4c9SJonathan Lemon.Fn kevent
1611a0a9345SRuslan Ermilovwaits indefinitely.
1621a0a9345SRuslan ErmilovTo effect a poll, the
163ad35a4c9SJonathan Lemon.Fa timeout
164ad35a4c9SJonathan Lemonargument should be non-NULL, pointing to a zero-valued
165ad35a4c9SJonathan Lemon.Va timespec
1661a0a9345SRuslan Ermilovstructure.
1671a0a9345SRuslan ErmilovThe same array may be used for the
168ee85cf8bSJonathan Lemon.Fa changelist
169ee85cf8bSJonathan Lemonand
170ee85cf8bSJonathan Lemon.Fa eventlist .
171ad35a4c9SJonathan Lemon.Pp
1722faeeff4SRuslan ErmilovThe
1735d1c9c56SJonathan Lemon.Fn EV_SET
1742faeeff4SRuslan Ermilovmacro is provided for ease of initializing a
1755d1c9c56SJonathan Lemonkevent structure.
1765d1c9c56SJonathan Lemon.Pp
177ad35a4c9SJonathan LemonThe
178ad35a4c9SJonathan Lemon.Va kevent
179ad35a4c9SJonathan Lemonstructure is defined as:
180ad35a4c9SJonathan Lemon.Bd -literal
181ad35a4c9SJonathan Lemonstruct kevent {
182ad35a4c9SJonathan Lemon	uintptr_t  ident;	/* identifier for this event */
183ad35a4c9SJonathan Lemon	short	  filter;	/* filter for event */
184ad35a4c9SJonathan Lemon	u_short	  flags;	/* action flags for kqueue */
185ad35a4c9SJonathan Lemon	u_int	  fflags;	/* filter flag value */
1862b34e843SKonstantin Belousov	int64_t   data;		/* filter data value */
187ad35a4c9SJonathan Lemon	void	  *udata;	/* opaque user data identifier */
18895c05062SDavid Bright	uint64_t  ext[4];	/* extensions */
189ad35a4c9SJonathan Lemon};
190ad35a4c9SJonathan Lemon.Ed
191ad35a4c9SJonathan Lemon.Pp
192ad35a4c9SJonathan LemonThe fields of
193ad35a4c9SJonathan Lemon.Fa struct kevent
194ad35a4c9SJonathan Lemonare:
19519eaed53SKonstantin Belousov.Bl -tag -width "Fa filter"
19619eaed53SKonstantin Belousov.It Fa ident
197ad35a4c9SJonathan LemonValue used to identify this event.
198ad35a4c9SJonathan LemonThe exact interpretation is determined by the attached filter,
199ad35a4c9SJonathan Lemonbut often is a file descriptor.
20019eaed53SKonstantin Belousov.It Fa filter
2011a0a9345SRuslan ErmilovIdentifies the kernel filter used to process this event.
2021a0a9345SRuslan ErmilovThe pre-defined
203ad35a4c9SJonathan Lemonsystem filters are described below.
20419eaed53SKonstantin Belousov.It Fa flags
205ad35a4c9SJonathan LemonActions to perform on the event.
20619eaed53SKonstantin Belousov.It Fa fflags
207ad35a4c9SJonathan LemonFilter-specific flags.
20819eaed53SKonstantin Belousov.It Fa data
209ad35a4c9SJonathan LemonFilter-specific data value.
21019eaed53SKonstantin Belousov.It Fa udata
211ad35a4c9SJonathan LemonOpaque user-defined value passed through the kernel unchanged.
2122b34e843SKonstantin Belousov.It Fa ext
2132b34e843SKonstantin BelousovExtended data passed to and from kernel.
2142b34e843SKonstantin BelousovThe
2152b34e843SKonstantin Belousov.Fa ext[0]
2162b34e843SKonstantin Belousovand
2172b34e843SKonstantin Belousov.Fa ext[1]
2182b34e843SKonstantin Belousovmembers use is defined by the filter.
2192b34e843SKonstantin BelousovIf the filter does not use them, the members are copied unchanged.
2202b34e843SKonstantin BelousovThe
2212b34e843SKonstantin Belousov.Fa ext[2]
2222b34e843SKonstantin Belousovand
2232b34e843SKonstantin Belousov.Fa ext[3]
224837fe325SEitan Adlermembers are always passed through the kernel as-is,
2252b34e843SKonstantin Belousovmaking additional context available to application.
226ad35a4c9SJonathan Lemon.El
227ad35a4c9SJonathan Lemon.Pp
228ad35a4c9SJonathan LemonThe
229ad35a4c9SJonathan Lemon.Va flags
230ad35a4c9SJonathan Lemonfield can contain the following values:
23119eaed53SKonstantin Belousov.Bl -tag -width EV_DISPATCH
23219eaed53SKonstantin Belousov.It Dv EV_ADD
2331a0a9345SRuslan ErmilovAdds the event to the kqueue.
2341a0a9345SRuslan ErmilovRe-adding an existing event
235ad35a4c9SJonathan Lemonwill modify the parameters of the original event, and not result
2361a0a9345SRuslan Ermilovin a duplicate entry.
2371a0a9345SRuslan ErmilovAdding an event automatically enables it,
238ad35a4c9SJonathan Lemonunless overridden by the EV_DISABLE flag.
23919eaed53SKonstantin Belousov.It Dv EV_ENABLE
240ad35a4c9SJonathan LemonPermit
241ad35a4c9SJonathan Lemon.Fn kevent
242ad35a4c9SJonathan Lemonto return the event if it is triggered.
24319eaed53SKonstantin Belousov.It Dv EV_DISABLE
244ad35a4c9SJonathan LemonDisable the event so
245ad35a4c9SJonathan Lemon.Fn kevent
2461a0a9345SRuslan Ermilovwill not return it.
2471a0a9345SRuslan ErmilovThe filter itself is not disabled.
24819eaed53SKonstantin Belousov.It Dv EV_DISPATCH
2491a921c41SStacey SonDisable the event source immediately after delivery of an event.
2501a921c41SStacey SonSee
2511a921c41SStacey Son.Dv EV_DISABLE
2521a921c41SStacey Sonabove.
25319eaed53SKonstantin Belousov.It Dv EV_DELETE
2541a0a9345SRuslan ErmilovRemoves the event from the kqueue.
2551a0a9345SRuslan ErmilovEvents which are attached to
256ad35a4c9SJonathan Lemonfile descriptors are automatically deleted on the last close of
257ad35a4c9SJonathan Lemonthe descriptor.
25819eaed53SKonstantin Belousov.It Dv EV_RECEIPT
259fdc1a113SStacey SonThis flag is useful for making bulk changes to a kqueue without draining
260fdc1a113SStacey Sonany pending events.
261fdc1a113SStacey SonWhen passed as input, it forces
262fdc1a113SStacey Son.Dv EV_ERROR
263fdc1a113SStacey Sonto always be returned.
264fdc1a113SStacey SonWhen a filter is successfully added the
265fdc1a113SStacey Son.Va data
266fdc1a113SStacey Sonfield will be zero.
26700b0f94cSKyle EvansNote that if this flag is encountered and there is no remaining space in
26800b0f94cSKyle Evans.Fa eventlist
26900b0f94cSKyle Evansto hold the
27000b0f94cSKyle Evans.Dv EV_ERROR
27100b0f94cSKyle Evansevent, then subsequent changes will not get processed.
27219eaed53SKonstantin Belousov.It Dv EV_ONESHOT
273ad35a4c9SJonathan LemonCauses the event to return only the first occurrence of the filter
2741a0a9345SRuslan Ermilovbeing triggered.
2751a0a9345SRuslan ErmilovAfter the user retrieves the event from the kqueue,
276ad35a4c9SJonathan Lemonit is deleted.
27719eaed53SKonstantin Belousov.It Dv EV_CLEAR
278ad35a4c9SJonathan LemonAfter the event is retrieved by the user, its state is reset.
279ad35a4c9SJonathan LemonThis is useful for filters which report state transitions
2801a0a9345SRuslan Ermilovinstead of the current state.
2811a0a9345SRuslan ErmilovNote that some filters may automatically
282ad35a4c9SJonathan Lemonset this flag internally.
28319eaed53SKonstantin Belousov.It Dv EV_EOF
284ad35a4c9SJonathan LemonFilters may set this flag to indicate filter-specific EOF condition.
28519eaed53SKonstantin Belousov.It Dv EV_ERROR
286ad35a4c9SJonathan LemonSee
287ad35a4c9SJonathan Lemon.Sx RETURN VALUES
288ad35a4c9SJonathan Lemonbelow.
2890321a799SNathaniel Wesley Filardo.It Dv EV_KEEPUDATA
2900321a799SNathaniel Wesley FilardoCauses
2910321a799SNathaniel Wesley Filardo.Fn kevent
2920321a799SNathaniel Wesley Filardoto leave unchanged any
2930321a799SNathaniel Wesley Filardo.Fa udata
2940f43c5b5SKyle Evansassociated with an existing event.
2950f43c5b5SKyle EvansThis allows other aspects of the event to be modified without requiring the
2960f43c5b5SKyle Evanscaller to know the
2970321a799SNathaniel Wesley Filardo.Fa udata
2980321a799SNathaniel Wesley Filardovalue presently associated.
2990321a799SNathaniel Wesley FilardoThis is especially useful with
3000321a799SNathaniel Wesley Filardo.Dv NOTE_TRIGGER
3010321a799SNathaniel Wesley Filardoor flags like
3020321a799SNathaniel Wesley Filardo.Dv EV_ENABLE .
3030321a799SNathaniel Wesley FilardoThis flag may not be used with
3040321a799SNathaniel Wesley Filardo.Dv EV_ADD .
305ad35a4c9SJonathan Lemon.El
306ad35a4c9SJonathan Lemon.Pp
307ad35a4c9SJonathan LemonThe predefined system filters are listed below.
308ad35a4c9SJonathan LemonArguments may be passed to and from the filter via the
309ad35a4c9SJonathan Lemon.Va fflags
310ad35a4c9SJonathan Lemonand
311ad35a4c9SJonathan Lemon.Va data
312ad35a4c9SJonathan Lemonfields in the kevent structure.
31319eaed53SKonstantin Belousov.Bl -tag -width "Dv EVFILT_PROCDESC"
31419eaed53SKonstantin Belousov.It Dv EVFILT_READ
315ad35a4c9SJonathan LemonTakes a descriptor as the identifier, and returns whenever
316ad35a4c9SJonathan Lemonthere is data available to read.
317ad35a4c9SJonathan LemonThe behavior of the filter is slightly different depending
318ad35a4c9SJonathan Lemonon the descriptor type.
31942635956SRuslan Ermilov.Bl -tag -width 2n
320ad35a4c9SJonathan Lemon.It Sockets
321ad35a4c9SJonathan LemonSockets which have previously been passed to
3226405997fSMark Johnston.Xr listen 2
323ad35a4c9SJonathan Lemonreturn when there is an incoming connection pending.
324ad35a4c9SJonathan Lemon.Va data
325ad35a4c9SJonathan Lemoncontains the size of the listen backlog.
326ad35a4c9SJonathan Lemon.Pp
327ad35a4c9SJonathan LemonOther socket descriptors return when there is data to be read,
3285d1c9c56SJonathan Lemonsubject to the
3295d1c9c56SJonathan Lemon.Dv SO_RCVLOWAT
3305d1c9c56SJonathan Lemonvalue of the socket buffer.
3315d1c9c56SJonathan LemonThis may be overridden with a per-filter low water mark at the
3325d1c9c56SJonathan Lemontime the filter is added by setting the
33319eaed53SKonstantin Belousov.Dv NOTE_LOWAT
3345d1c9c56SJonathan Lemonflag in
3355d1c9c56SJonathan Lemon.Va fflags ,
3365d1c9c56SJonathan Lemonand specifying the new low water mark in
3375d1c9c56SJonathan Lemon.Va data .
3385d1c9c56SJonathan LemonOn return,
339ad35a4c9SJonathan Lemon.Va data
340e0f640e8SKelly Yanceycontains the number of bytes of protocol data available to read.
341ad35a4c9SJonathan Lemon.Pp
342ad35a4c9SJonathan LemonIf the read direction of the socket has shutdown, then the filter
34319eaed53SKonstantin Belousovalso sets
34419eaed53SKonstantin Belousov.Dv EV_EOF
34519eaed53SKonstantin Belousovin
3465d1c9c56SJonathan Lemon.Va flags ,
3475d1c9c56SJonathan Lemonand returns the socket error (if any) in
3485d1c9c56SJonathan Lemon.Va fflags .
349ad35a4c9SJonathan LemonIt is possible for EOF to be returned (indicating the connection is gone)
350ad35a4c9SJonathan Lemonwhile there is still data pending in the socket buffer.
351ad35a4c9SJonathan Lemon.It Vnodes
352ad35a4c9SJonathan LemonReturns when the file pointer is not at the end of file.
353ad35a4c9SJonathan Lemon.Va data
354ad35a4c9SJonathan Lemoncontains the offset from current position to end of file,
355ad35a4c9SJonathan Lemonand may be negative.
3562433a4ebSEd Schouten.Pp
3572433a4ebSEd SchoutenThis behavior is different from
3582433a4ebSEd Schouten.Xr poll 2 ,
3592433a4ebSEd Schoutenwhere read events are triggered for regular files unconditionally.
3602433a4ebSEd SchoutenThis event can be triggered unconditionally by setting the
3612433a4ebSEd Schouten.Dv NOTE_FILE_POLL
3622433a4ebSEd Schoutenflag in
3632433a4ebSEd Schouten.Va fflags .
36442635956SRuslan Ermilov.It "Fifos, Pipes"
365ad35a4c9SJonathan LemonReturns when the there is data to read;
366ad35a4c9SJonathan Lemon.Va data
367ad35a4c9SJonathan Lemoncontains the number of bytes available.
368ad35a4c9SJonathan Lemon.Pp
36919eaed53SKonstantin BelousovWhen the last writer disconnects, the filter will set
37019eaed53SKonstantin Belousov.Dv EV_EOF
37119eaed53SKonstantin Belousovin
372ad35a4c9SJonathan Lemon.Va flags .
373569eb766SMark JohnstonThis will be cleared by the filter when a new writer connects,
37419eaed53SKonstantin Belousovat which point the
375ad35a4c9SJonathan Lemonfilter will resume waiting for data to become available before
376ad35a4c9SJonathan Lemonreturning.
37795aab9ccSJohn-Mark Gurney.It "BPF devices"
37895aab9ccSJohn-Mark GurneyReturns when the BPF buffer is full, the BPF timeout has expired, or
37995aab9ccSJohn-Mark Gurneywhen the BPF has
38095aab9ccSJohn-Mark Gurney.Dq immediate mode
38195aab9ccSJohn-Mark Gurneyenabled and there is any data to read;
38295aab9ccSJohn-Mark Gurney.Va data
38395aab9ccSJohn-Mark Gurneycontains the number of bytes available.
3846d075fd9SKonstantin Belousov.It Eventfds
3856d075fd9SKonstantin BelousovReturns when the counter is greater than 0;
3866d075fd9SKonstantin Belousov.Va data
3876d075fd9SKonstantin Belousovcontains the counter value, which must be cast to
3886d075fd9SKonstantin Belousov.Vt uint64_t .
389f756c911SMark Johnston.It Kqueues
390f756c911SMark JohnstonReturns when pending events are present on the queue;
391f756c911SMark Johnston.Va data
392f756c911SMark Johnstoncontains the number of events available.
393ad35a4c9SJonathan Lemon.El
39419eaed53SKonstantin Belousov.It Dv EVFILT_WRITE
395ad35a4c9SJonathan LemonTakes a descriptor as the identifier, and returns whenever
3961a0a9345SRuslan Ermilovit is possible to write to the descriptor.
3971a0a9345SRuslan ErmilovFor sockets, pipes
398ad35a4c9SJonathan Lemonand fifos,
399ad35a4c9SJonathan Lemon.Va data
400ad35a4c9SJonathan Lemonwill contain the amount of space remaining in the write buffer.
401569eb766SMark JohnstonThe filter will set
402569eb766SMark Johnston.Dv EV_EOF
403569eb766SMark Johnstonwhen the reader disconnects, and for the fifo case, this will be cleared
404569eb766SMark Johnstonwhen a new reader connects.
405ded77e02SHartmut BrandtNote that this filter is not supported for vnodes.
4065d1c9c56SJonathan Lemon.Pp
4075d1c9c56SJonathan LemonFor sockets, the low water mark and socket error handling is
40819eaed53SKonstantin Belousovidentical to the
40919eaed53SKonstantin Belousov.Dv EVFILT_READ
41019eaed53SKonstantin Belousovcase.
4116d075fd9SKonstantin Belousov.Pp
4126d075fd9SKonstantin BelousovFor eventfds,
4136d075fd9SKonstantin Belousov.Va data
4146d075fd9SKonstantin Belousovwill contain the maximum value that can be added to the counter
4156d075fd9SKonstantin Belousovwithout blocking.
416ded77e02SHartmut Brandt.Pp
417426682b0SMark JohnstonFor BPF devices, when the descriptor is attached to an interface the filter
418426682b0SMark Johnstonalways indicates that it is possible to write and
419ded77e02SHartmut Brandt.Va data
420ded77e02SHartmut Brandtwill contain the MTU size of the underlying interface.
421b11ef6e9SSergey Kandaurov.It Dv EVFILT_EMPTY
422b11ef6e9SSergey KandaurovTakes a descriptor as the identifier, and returns whenever
423b11ef6e9SSergey Kandaurovthere is no remaining data in the write buffer.
42419eaed53SKonstantin Belousov.It Dv EVFILT_AIO
42509986d3bSAlan SomersEvents for this filter are not registered with
42609986d3bSAlan Somers.Fn kevent
42709986d3bSAlan Somersdirectly but are registered via the
42809986d3bSAlan Somers.Va aio_sigevent
42995c05062SDavid Brightmember of an asynchronous I/O request when it is scheduled via an
43095c05062SDavid Brightasynchronous I/O system call such as
43109986d3bSAlan Somers.Fn aio_read .
43219eaed53SKonstantin BelousovThe filter returns under the same conditions as
43319eaed53SKonstantin Belousov.Fn aio_error .
43409986d3bSAlan SomersFor more details on this filter see
43509986d3bSAlan Somers.Xr sigevent 3 and
43609986d3bSAlan Somers.Xr aio 4 .
43719eaed53SKonstantin Belousov.It Dv EVFILT_VNODE
438ad35a4c9SJonathan LemonTakes a file descriptor as the identifier and the events to watch for in
439ad35a4c9SJonathan Lemon.Va fflags ,
440ad35a4c9SJonathan Lemonand returns when one or more of the requested events occurs on the descriptor.
441ad35a4c9SJonathan LemonThe events to monitor are:
442c89e1b87SKonstantin Belousov.Bl -tag -width "Dv NOTE_CLOSE_WRITE"
443c89e1b87SKonstantin Belousov.It Dv NOTE_ATTRIB
444c89e1b87SKonstantin BelousovThe file referenced by the descriptor had its attributes changed.
445c89e1b87SKonstantin Belousov.It Dv NOTE_CLOSE
446c89e1b87SKonstantin BelousovA file descriptor referencing the monitored file, was closed.
4476e745eedSKonstantin BelousovThe closed file descriptor did not have write access.
448c89e1b87SKonstantin Belousov.It Dv NOTE_CLOSE_WRITE
449c89e1b87SKonstantin BelousovA file descriptor referencing the monitored file, was closed.
4503617efe5SBryan DreweryThe closed file descriptor had write access.
451c89e1b87SKonstantin Belousov.Pp
452c89e1b87SKonstantin BelousovThis note, as well as
453c89e1b87SKonstantin Belousov.Dv NOTE_CLOSE ,
454c89e1b87SKonstantin Belousovare not activated when files are closed forcibly by
455c89e1b87SKonstantin Belousov.Xr unmount 2 or
456c89e1b87SKonstantin Belousov.Xr revoke 2 .
457c89e1b87SKonstantin BelousovInstead,
458c89e1b87SKonstantin Belousov.Dv NOTE_REVOKE
459c89e1b87SKonstantin Belousovis sent for such events.
46019eaed53SKonstantin Belousov.It Dv NOTE_DELETE
4612faeeff4SRuslan ErmilovThe
462ad35a4c9SJonathan Lemon.Fn unlink
463c89e1b87SKonstantin Belousovsystem call was called on the file referenced by the descriptor.
46419eaed53SKonstantin Belousov.It Dv NOTE_EXTEND
465f7b71c8aSKonstantin BelousovFor regular file, the file referenced by the descriptor was extended.
466f7b71c8aSKonstantin Belousov.Pp
467f7b71c8aSKonstantin BelousovFor directory, reports that a directory entry was added or removed,
468f7b71c8aSKonstantin Belousovas the result of rename operation.
469f7b71c8aSKonstantin BelousovThe
470f7b71c8aSKonstantin Belousov.Dv NOTE_EXTEND
471f7b71c8aSKonstantin Belousovevent is not reported when a name is changed inside the directory.
47219eaed53SKonstantin Belousov.It Dv NOTE_LINK
473ad35a4c9SJonathan LemonThe link count on the file changed.
474ba55e112SKonstantin BelousovIn particular, the
475ba55e112SKonstantin Belousov.Dv NOTE_LINK
476ba55e112SKonstantin Belousovevent is reported if a subdirectory was created or deleted inside
477ba55e112SKonstantin Belousovthe directory referenced by the descriptor.
478c89e1b87SKonstantin Belousov.It Dv NOTE_OPEN
479c89e1b87SKonstantin BelousovThe file referenced by the descriptor was opened.
480c89e1b87SKonstantin Belousov.It Dv NOTE_READ
481c89e1b87SKonstantin BelousovA read occurred on the file referenced by the descriptor.
48219eaed53SKonstantin Belousov.It Dv NOTE_RENAME
483ad35a4c9SJonathan LemonThe file referenced by the descriptor was renamed.
48419eaed53SKonstantin Belousov.It Dv NOTE_REVOKE
4855d1c9c56SJonathan LemonAccess to the file was revoked via
4865d1c9c56SJonathan Lemon.Xr revoke 2
4873c6dd63bSMike Pritchardor the underlying file system was unmounted.
488c89e1b87SKonstantin Belousov.It Dv NOTE_WRITE
489c89e1b87SKonstantin BelousovA write occurred on the file referenced by the descriptor.
490ad35a4c9SJonathan Lemon.El
491ad35a4c9SJonathan Lemon.Pp
492ad35a4c9SJonathan LemonOn return,
493ad35a4c9SJonathan Lemon.Va fflags
494ad35a4c9SJonathan Lemoncontains the events which triggered the filter.
49519eaed53SKonstantin Belousov.It Dv EVFILT_PROC
496ad35a4c9SJonathan LemonTakes the process ID to monitor as the identifier and the events to watch for
497ad35a4c9SJonathan Lemonin
498ad35a4c9SJonathan Lemon.Va fflags ,
499ad35a4c9SJonathan Lemonand returns when the process performs one or more of the requested events.
500ad35a4c9SJonathan LemonIf a process can normally see another process, it can attach an event to it.
501ad35a4c9SJonathan LemonThe events to monitor are:
50219eaed53SKonstantin Belousov.Bl -tag -width "Dv NOTE_TRACKERR"
50319eaed53SKonstantin Belousov.It Dv NOTE_EXIT
504ad35a4c9SJonathan LemonThe process has exited.
5058cc7b0f9SJohn BaldwinThe exit status will be stored in
5067406ec4eSKonstantin Belousov.Va data
5077406ec4eSKonstantin Belousovin the same format as the status returned by
5087406ec4eSKonstantin Belousov.Xr wait 2 .
50919eaed53SKonstantin Belousov.It Dv NOTE_FORK
510ad35a4c9SJonathan LemonThe process has called
511ad35a4c9SJonathan Lemon.Fn fork .
51219eaed53SKonstantin Belousov.It Dv NOTE_EXEC
513ad35a4c9SJonathan LemonThe process has executed a new process via
514ad35a4c9SJonathan Lemon.Xr execve 2
515513bfc4fSJohn Baldwinor a similar call.
51619eaed53SKonstantin Belousov.It Dv NOTE_TRACK
517ad35a4c9SJonathan LemonFollow a process across
518ad35a4c9SJonathan Lemon.Fn fork
5191a0a9345SRuslan Ermilovcalls.
520513bfc4fSJohn BaldwinThe parent process registers a new kevent to monitor the child process
521513bfc4fSJohn Baldwinusing the same
522ad35a4c9SJonathan Lemon.Va fflags
523513bfc4fSJohn Baldwinas the original event.
52419eaed53SKonstantin BelousovThe child process will signal an event with
52519eaed53SKonstantin Belousov.Dv NOTE_CHILD
52619eaed53SKonstantin Belousovset in
527ad35a4c9SJonathan Lemon.Va fflags
528ad35a4c9SJonathan Lemonand the parent PID in
529ad35a4c9SJonathan Lemon.Va data .
530513bfc4fSJohn Baldwin.Pp
531513bfc4fSJohn BaldwinIf the parent process fails to register a new kevent
532513bfc4fSJohn Baldwin.Pq usually due to resource limitations ,
53319eaed53SKonstantin Belousovit will signal an event with
53419eaed53SKonstantin Belousov.Dv NOTE_TRACKERR
53519eaed53SKonstantin Belousovset in
536513bfc4fSJohn Baldwin.Va fflags ,
53719eaed53SKonstantin Belousovand the child process will not signal a
53819eaed53SKonstantin Belousov.Dv NOTE_CHILD
53919eaed53SKonstantin Belousovevent.
540ad35a4c9SJonathan Lemon.El
541ad35a4c9SJonathan Lemon.Pp
542ad35a4c9SJonathan LemonOn return,
543ad35a4c9SJonathan Lemon.Va fflags
544ad35a4c9SJonathan Lemoncontains the events which triggered the filter.
54519eaed53SKonstantin Belousov.It Dv EVFILT_PROCDESC
54638219d6aSEd SchoutenTakes the process descriptor created by
54738219d6aSEd Schouten.Xr pdfork 2
54838219d6aSEd Schoutento monitor as the identifier and the events to watch for in
54938219d6aSEd Schouten.Va fflags ,
55038219d6aSEd Schoutenand returns when the associated process performs one or more of the
55138219d6aSEd Schoutenrequested events.
55238219d6aSEd SchoutenThe events to monitor are:
55319eaed53SKonstantin Belousov.Bl -tag -width "Dv NOTE_EXIT"
55419eaed53SKonstantin Belousov.It Dv NOTE_EXIT
55538219d6aSEd SchoutenThe process has exited.
55638219d6aSEd SchoutenThe exit status will be stored in
55738219d6aSEd Schouten.Va data .
55838219d6aSEd Schouten.El
55938219d6aSEd Schouten.Pp
56038219d6aSEd SchoutenOn return,
56138219d6aSEd Schouten.Va fflags
56238219d6aSEd Schoutencontains the events which triggered the filter.
56319eaed53SKonstantin Belousov.It Dv EVFILT_SIGNAL
564ad35a4c9SJonathan LemonTakes the signal number to monitor as the identifier and returns
565ad35a4c9SJonathan Lemonwhen the given signal is delivered to the process.
566ad35a4c9SJonathan LemonThis coexists with the
567ad35a4c9SJonathan Lemon.Fn signal
568ad35a4c9SJonathan Lemonand
569ad35a4c9SJonathan Lemon.Fn sigaction
5701a0a9345SRuslan Ermilovfacilities, and has a lower precedence.
5711a0a9345SRuslan ErmilovThe filter will record
572ad35a4c9SJonathan Lemonall attempts to deliver a signal to a process, even if the signal has
57319eaed53SKonstantin Belousovbeen marked as
57419eaed53SKonstantin Belousov.Dv SIG_IGN ,
57519eaed53SKonstantin Belousovexcept for the
576ffce2a5bSPawel Jakub Dawidek.Dv SIGCHLD
57795c05062SDavid Brightsignal, which, if ignored, will not be recorded by the filter.
5781a0a9345SRuslan ErmilovEvent notification happens after normal
579ad35a4c9SJonathan Lemonsignal delivery processing.
580ad35a4c9SJonathan Lemon.Va data
581ad35a4c9SJonathan Lemonreturns the number of times the signal has occurred since the last call to
582add06812SJonathan Lemon.Fn kevent .
58319eaed53SKonstantin BelousovThis filter automatically sets the
58419eaed53SKonstantin Belousov.Dv EV_CLEAR
58519eaed53SKonstantin Belousovflag internally.
58619eaed53SKonstantin Belousov.It Dv EVFILT_TIMER
587e725a129SJonathan LemonEstablishes an arbitrary timer identified by
588e725a129SJonathan Lemon.Va ident .
589e725a129SJonathan LemonWhen adding a timer,
590e725a129SJonathan Lemon.Va data
5912b34e843SKonstantin Belousovspecifies the moment to fire the timer (for
5922b34e843SKonstantin Belousov.Dv NOTE_ABSTIME )
5932b34e843SKonstantin Belousovor the timeout period.
59419eaed53SKonstantin BelousovThe timer will be periodic unless
59519eaed53SKonstantin Belousov.Dv EV_ONESHOT
5962b34e843SKonstantin Belousovor
5972b34e843SKonstantin Belousov.Dv NOTE_ABSTIME
59819eaed53SKonstantin Belousovis specified.
599e725a129SJonathan LemonOn return,
600e725a129SJonathan Lemon.Va data
601fea88e76SGuido van Rooijcontains the number of times the timeout has expired since the last call to
602e725a129SJonathan Lemon.Fn kevent .
6032b34e843SKonstantin BelousovFor non-monotonic timers, this filter automatically sets the
6042b34e843SKonstantin Belousov.Dv EV_CLEAR
6052b34e843SKonstantin Belousovflag internally.
6062b34e843SKonstantin Belousov.Pp
6072b34e843SKonstantin BelousovThe filter accepts the following flags in the
6082b34e843SKonstantin Belousov.Va fflags
6092b34e843SKonstantin Belousovargument:
6102b34e843SKonstantin Belousov.Bl -tag -width "Dv NOTE_MSECONDS"
61142e62ecaSBaptiste Daroussin.It Dv NOTE_SECONDS
61242e62ecaSBaptiste Daroussin.Va data
61342e62ecaSBaptiste Daroussinis in seconds.
61442e62ecaSBaptiste Daroussin.It Dv NOTE_MSECONDS
61542e62ecaSBaptiste Daroussin.Va data
61642e62ecaSBaptiste Daroussinis in milliseconds.
61742e62ecaSBaptiste Daroussin.It Dv NOTE_USECONDS
61842e62ecaSBaptiste Daroussin.Va data
61942e62ecaSBaptiste Daroussinis in microseconds.
62042e62ecaSBaptiste Daroussin.It Dv NOTE_NSECONDS
62142e62ecaSBaptiste Daroussin.Va data
62242e62ecaSBaptiste Daroussinis in nanoseconds.
6232b34e843SKonstantin Belousov.It Dv NOTE_ABSTIME
6242b34e843SKonstantin BelousovThe specified expiration time is absolute.
62542e62ecaSBaptiste Daroussin.El
626bda16723SChristian Brueffer.Pp
62742e62ecaSBaptiste DaroussinIf
62842e62ecaSBaptiste Daroussin.Va fflags
62917c847c1SKonstantin Belousovis not set, the default is milliseconds.
63017c847c1SKonstantin BelousovOn return,
631bda16723SChristian Brueffer.Va fflags
632bda16723SChristian Brueffercontains the events which triggered the filter.
633d60fa657SKonstantin Belousov.Pp
6344b5554ceSKyle EvansPeriodic timers with a specified timeout of 0 will be silently adjusted to
6354b5554ceSKyle Evanstimeout after 1 of the time units specified by the requested precision in
6364b5554ceSKyle Evans.Va fflags .
6374b5554ceSKyle EvansIf an absolute time is specified that has already passed, then it is treated as
6384b5554ceSKyle Evansif the current time were specified and the event will fire as soon as possible.
6394b5554ceSKyle Evans.Pp
64095c05062SDavid BrightIf an existing timer is re-added, the existing timer will be
64195c05062SDavid Brighteffectively canceled (throwing away any undelivered record of previous
64295c05062SDavid Brighttimer expiration) and re-started using the new parameters contained in
64395c05062SDavid Bright.Va data
64495c05062SDavid Brightand
64595c05062SDavid Bright.Va fflags .
64695c05062SDavid Bright.Pp
647d60fa657SKonstantin BelousovThere is a system wide limit on the number of timers
648d60fa657SKonstantin Belousovwhich is controlled by the
649d60fa657SKonstantin Belousov.Va kern.kq_calloutmax
650d60fa657SKonstantin Belousovsysctl.
6512c2e4499SStacey Son.It Dv EVFILT_USER
6522c2e4499SStacey SonEstablishes a user event identified by
6532c2e4499SStacey Son.Va ident
654f6ac2391SJoel Dahlwhich is not associated with any kernel mechanism but is triggered by
6552c2e4499SStacey Sonuser level code.
6562c2e4499SStacey SonThe lower 24 bits of the
6572c2e4499SStacey Son.Va fflags
6582c2e4499SStacey Sonmay be used for user defined flags and manipulated using the following:
65919eaed53SKonstantin Belousov.Bl -tag -width "Dv NOTE_FFLAGSMASK"
6602c2e4499SStacey Son.It Dv NOTE_FFNOP
6612c2e4499SStacey SonIgnore the input
6622c2e4499SStacey Son.Va fflags .
6632c2e4499SStacey Son.It Dv NOTE_FFAND
6642c2e4499SStacey SonBitwise AND
6652c2e4499SStacey Son.Va fflags .
6662c2e4499SStacey Son.It Dv NOTE_FFOR
6672c2e4499SStacey SonBitwise OR
6682c2e4499SStacey Son.Va fflags .
66920df026cSRuslan Ermilov.It Dv NOTE_FFCOPY
6702c2e4499SStacey SonCopy
6712c2e4499SStacey Son.Va fflags .
6722c2e4499SStacey Son.It Dv NOTE_FFCTRLMASK
6732c2e4499SStacey SonControl mask for
6742c2e4499SStacey Son.Va fflags .
6752c2e4499SStacey Son.It Dv NOTE_FFLAGSMASK
6762c2e4499SStacey SonUser defined flag mask for
6772c2e4499SStacey Son.Va fflags .
6782c2e4499SStacey Son.El
6792c2e4499SStacey Son.Pp
6802c2e4499SStacey SonA user event is triggered for output with the following:
68119eaed53SKonstantin Belousov.Bl -tag -width "Dv NOTE_FFLAGSMASK"
6822c2e4499SStacey Son.It Dv NOTE_TRIGGER
6832c2e4499SStacey SonCause the event to be triggered.
6842c2e4499SStacey Son.El
6852c2e4499SStacey Son.Pp
6862c2e4499SStacey SonOn return,
6872c2e4499SStacey Son.Va fflags
6882c2e4499SStacey Soncontains the users defined flags in the lower 24 bits.
689ad35a4c9SJonathan Lemon.El
690b072e86dSKonstantin Belousov.Sh CANCELLATION BEHAVIOUR
691b072e86dSKonstantin BelousovIf
692b072e86dSKonstantin Belousov.Fa nevents
69395c05062SDavid Brightis non-zero, i.e., the function is potentially blocking, the call
694b072e86dSKonstantin Belousovis a cancellation point.
69595c05062SDavid BrightOtherwise, i.e., if
696b072e86dSKonstantin Belousov.Fa nevents
697b072e86dSKonstantin Belousovis zero, the call is not cancellable.
698b072e86dSKonstantin BelousovCancellation can only occur before any changes are made to the kqueue,
699b072e86dSKonstantin Belousovor when the call was blocked and no changes to the queue were requested.
700ad35a4c9SJonathan Lemon.Sh RETURN VALUES
7012faeeff4SRuslan ErmilovThe
702af7b56f4SJohn-Mark Gurney.Fn kqueue
7032faeeff4SRuslan Ermilovsystem call
704af7b56f4SJohn-Mark Gurneycreates a new kernel event queue and returns a file descriptor.
705af7b56f4SJohn-Mark GurneyIf there was an error creating the kernel event queue, a value of -1 is
706af7b56f4SJohn-Mark Gurneyreturned and errno set.
707af7b56f4SJohn-Mark Gurney.Pp
7082faeeff4SRuslan ErmilovThe
709ad35a4c9SJonathan Lemon.Fn kevent
7102faeeff4SRuslan Ermilovsystem call
711ad35a4c9SJonathan Lemonreturns the number of events placed in the
712bb33e422SChris Costello.Fa eventlist ,
713ad35a4c9SJonathan Lemonup to the value given by
714bb33e422SChris Costello.Fa nevents .
715ad35a4c9SJonathan LemonIf an error occurs while processing an element of the
716bb33e422SChris Costello.Fa changelist
717ad35a4c9SJonathan Lemonand there is enough room in the
718bb33e422SChris Costello.Fa eventlist ,
719ad35a4c9SJonathan Lemonthen the event will be placed in the
720bb33e422SChris Costello.Fa eventlist
721ad35a4c9SJonathan Lemonwith
722ad35a4c9SJonathan Lemon.Dv EV_ERROR
723ad35a4c9SJonathan Lemonset in
724ad35a4c9SJonathan Lemon.Va flags
725ad35a4c9SJonathan Lemonand the system error in
726ad35a4c9SJonathan Lemon.Va data .
727ad35a4c9SJonathan LemonOtherwise,
728ad35a4c9SJonathan Lemon.Dv -1
729ad35a4c9SJonathan Lemonwill be returned, and
730ad35a4c9SJonathan Lemon.Dv errno
731ad35a4c9SJonathan Lemonwill be set to indicate the error condition.
732ad35a4c9SJonathan LemonIf the time limit expires, then
733ad35a4c9SJonathan Lemon.Fn kevent
734ad35a4c9SJonathan Lemonreturns 0.
7358c231786SBenedict Reuschling.Sh EXAMPLES
7368c231786SBenedict Reuschling.Bd -literal -compact
7378c231786SBenedict Reuschling#include <sys/event.h>
7388c231786SBenedict Reuschling#include <err.h>
7398c231786SBenedict Reuschling#include <fcntl.h>
7408c231786SBenedict Reuschling#include <stdio.h>
7418c231786SBenedict Reuschling#include <stdlib.h>
7428c231786SBenedict Reuschling#include <string.h>
7438c231786SBenedict Reuschling
7448c231786SBenedict Reuschlingint
7458c231786SBenedict Reuschlingmain(int argc, char **argv)
7468c231786SBenedict Reuschling{
7478c231786SBenedict Reuschling    struct kevent event;    /* Event we want to monitor */
7488c231786SBenedict Reuschling    struct kevent tevent;   /* Event triggered */
7498c231786SBenedict Reuschling    int kq, fd, ret;
7508c231786SBenedict Reuschling
7518c231786SBenedict Reuschling    if (argc != 2)
7528c231786SBenedict Reuschling	err(EXIT_FAILURE, "Usage: %s path\en", argv[0]);
7538c231786SBenedict Reuschling    fd = open(argv[1], O_RDONLY);
7548c231786SBenedict Reuschling    if (fd == -1)
7558c231786SBenedict Reuschling	err(EXIT_FAILURE, "Failed to open '%s'", argv[1]);
7568c231786SBenedict Reuschling
7578c231786SBenedict Reuschling    /* Create kqueue. */
7588c231786SBenedict Reuschling    kq = kqueue();
7598c231786SBenedict Reuschling    if (kq == -1)
7608c231786SBenedict Reuschling	err(EXIT_FAILURE, "kqueue() failed");
7618c231786SBenedict Reuschling
7628c231786SBenedict Reuschling    /* Initialize kevent structure. */
7638c231786SBenedict Reuschling    EV_SET(&event, fd, EVFILT_VNODE, EV_ADD | EV_CLEAR, NOTE_WRITE,
7648c231786SBenedict Reuschling	0, NULL);
7658c231786SBenedict Reuschling    /* Attach event to the kqueue. */
7668c231786SBenedict Reuschling    ret = kevent(kq, &event, 1, NULL, 0, NULL);
7678c231786SBenedict Reuschling    if (ret == -1)
7688c231786SBenedict Reuschling	err(EXIT_FAILURE, "kevent register");
7698c231786SBenedict Reuschling
7708c231786SBenedict Reuschling    for (;;) {
7718c231786SBenedict Reuschling	/* Sleep until something happens. */
7728c231786SBenedict Reuschling	ret = kevent(kq, NULL, 0, &tevent, 1, NULL);
7738c231786SBenedict Reuschling	if (ret == -1) {
7748c231786SBenedict Reuschling	    err(EXIT_FAILURE, "kevent wait");
7758c231786SBenedict Reuschling	} else if (ret > 0) {
776e07b0c12SFernando Apesteguía	    if (tevent.flags & EV_ERROR)
777e07b0c12SFernando Apesteguía		errx(EXIT_FAILURE, "Event error: %s", strerror(event.data));
778e07b0c12SFernando Apesteguía	    else
7798c231786SBenedict Reuschling		printf("Something was written in '%s'\en", argv[1]);
7808c231786SBenedict Reuschling	}
7818c231786SBenedict Reuschling    }
78220c9c3beSEd Maste
78320c9c3beSEd Maste    /* kqueues are destroyed upon close() */
78420c9c3beSEd Maste    (void)close(kq);
78520c9c3beSEd Maste    (void)close(fd);
7868c231786SBenedict Reuschling}
7878c231786SBenedict Reuschling.Ed
788ad35a4c9SJonathan Lemon.Sh ERRORS
789ad35a4c9SJonathan LemonThe
790af7b56f4SJohn-Mark Gurney.Fn kqueue
7912faeeff4SRuslan Ermilovsystem call fails if:
792af7b56f4SJohn-Mark Gurney.Bl -tag -width Er
793af7b56f4SJohn-Mark Gurney.It Bq Er ENOMEM
794af7b56f4SJohn-Mark GurneyThe kernel failed to allocate enough memory for the kernel queue.
7951947c8a6SJilles Tjoelker.It Bq Er ENOMEM
7961947c8a6SJilles TjoelkerThe
7971947c8a6SJilles Tjoelker.Dv RLIMIT_KQUEUES
7981947c8a6SJilles Tjoelkerrlimit
7991947c8a6SJilles Tjoelker(see
8001947c8a6SJilles Tjoelker.Xr getrlimit 2 )
8011947c8a6SJilles Tjoelkerfor the current user would be exceeded.
802af7b56f4SJohn-Mark Gurney.It Bq Er EMFILE
803af7b56f4SJohn-Mark GurneyThe per-process descriptor table is full.
804af7b56f4SJohn-Mark Gurney.It Bq Er ENFILE
805af7b56f4SJohn-Mark GurneyThe system file table is full.
806af7b56f4SJohn-Mark Gurney.El
807af7b56f4SJohn-Mark Gurney.Pp
808af7b56f4SJohn-Mark GurneyThe
809ad35a4c9SJonathan Lemon.Fn kevent
8102faeeff4SRuslan Ermilovsystem call fails if:
811ad35a4c9SJonathan Lemon.Bl -tag -width Er
812c23155a4SRuslan Ermilov.It Bq Er EACCES
813ad35a4c9SJonathan LemonThe process does not have permission to register a filter.
814ad35a4c9SJonathan Lemon.It Bq Er EFAULT
815ad35a4c9SJonathan LemonThere was an error reading or writing the
816ad35a4c9SJonathan Lemon.Va kevent
817ad35a4c9SJonathan Lemonstructure.
818ad35a4c9SJonathan Lemon.It Bq Er EBADF
819ad35a4c9SJonathan LemonThe specified descriptor is invalid.
820ad35a4c9SJonathan Lemon.It Bq Er EINTR
821ad35a4c9SJonathan LemonA signal was delivered before the timeout expired and before any
822ad35a4c9SJonathan Lemonevents were placed on the kqueue for return.
823b072e86dSKonstantin Belousov.It Bq Er EINTR
824b072e86dSKonstantin BelousovA cancellation request was delivered to the thread, but not yet handled.
825ad35a4c9SJonathan Lemon.It Bq Er EINVAL
826ad35a4c9SJonathan LemonThe specified time limit or filter is invalid.
827e00bae5cSMark Johnston.It Bq Er EINVAL
828e00bae5cSMark JohnstonThe specified length of the event or change lists is negative.
829b275c430SJonathan Lemon.It Bq Er ENOENT
830b275c430SJonathan LemonThe event could not be found to be modified or deleted.
831ad35a4c9SJonathan Lemon.It Bq Er ENOMEM
832d2b2cceaSCeri DaviesNo memory was available to register the event
833d2b2cceaSCeri Daviesor, in the special case of a timer, the maximum number of
834d2b2cceaSCeri Daviestimers has been exceeded.
835d2b2cceaSCeri DaviesThis maximum is configurable via the
836d2b2cceaSCeri Davies.Va kern.kq_calloutmax
837d2b2cceaSCeri Daviessysctl.
838ad35a4c9SJonathan Lemon.It Bq Er ESRCH
839ad35a4c9SJonathan LemonThe specified process to attach to does not exist.
840ad35a4c9SJonathan Lemon.El
841b072e86dSKonstantin Belousov.Pp
842b072e86dSKonstantin BelousovWhen
843b072e86dSKonstantin Belousov.Fn kevent
844b072e86dSKonstantin Belousovcall fails with
845b072e86dSKonstantin Belousov.Er EINTR
846b072e86dSKonstantin Belousoverror, all changes in the
847b072e86dSKonstantin Belousov.Fa changelist
848b072e86dSKonstantin Belousovhave been applied.
849ad35a4c9SJonathan Lemon.Sh SEE ALSO
850ad35a4c9SJonathan Lemon.Xr aio_error 2 ,
851ad35a4c9SJonathan Lemon.Xr aio_read 2 ,
852ad35a4c9SJonathan Lemon.Xr aio_return 2 ,
853ad35a4c9SJonathan Lemon.Xr poll 2 ,
854ad35a4c9SJonathan Lemon.Xr read 2 ,
855ad35a4c9SJonathan Lemon.Xr select 2 ,
856ad35a4c9SJonathan Lemon.Xr sigaction 2 ,
8573c63f48cSSheldon Hearn.Xr write 2 ,
858b072e86dSKonstantin Belousov.Xr pthread_setcancelstate 3 ,
8593c63f48cSSheldon Hearn.Xr signal 3
860e0f7c06dSGordon Bergling.Rs
861e0f7c06dSGordon Bergling.%A Jonathan Lemon
862e0f7c06dSGordon Bergling.%T "Kqueue: A Generic and Scalable Event Notification Facility"
863e0f7c06dSGordon Bergling.%I USENIX Association
864e0f7c06dSGordon Bergling.%B Proceedings of the FREENIX Track: 2001 USENIX Annual Technical Conference
865e0f7c06dSGordon Bergling.%D June 25-30, 2001
866e0f7c06dSGordon Bergling.\".http://www.usenix.org/event/usenix01/freenix01/full_papers/lemon/lemon.pdf
867e0f7c06dSGordon Bergling.Re
868ad35a4c9SJonathan Lemon.Sh HISTORY
869ad35a4c9SJonathan LemonThe
870ad35a4c9SJonathan Lemon.Fn kqueue
871ad35a4c9SJonathan Lemonand
872ad35a4c9SJonathan Lemon.Fn kevent
8732faeeff4SRuslan Ermilovsystem calls first appeared in
87455b8fbfbSJohn-Mark Gurney.Fx 4.1 .
8754451bb5dSJonathan Lemon.Sh AUTHORS
876ad35a4c9SJonathan LemonThe
877ad35a4c9SJonathan Lemon.Fn kqueue
878ad35a4c9SJonathan Lemonsystem and this manual page were written by
8798fbf3d50SBaptiste Daroussin.An Jonathan Lemon Aq Mt [email protected] .
880e585cdf0SJohn-Mark Gurney.Sh BUGS
8818eb15797SEd Schouten.Pp
8829e6e05e4SBenjamin KadukIn versions older than
8839e6e05e4SBenjamin Kaduk.Fx 12.0 ,
8848eb15797SEd Schouten.In sys/event.h
8859e6e05e4SBenjamin Kadukfailed to parse without including
8868eb15797SEd Schouten.In sys/types.h
8878eb15797SEd Schoutenmanually.
888