xref: /freebsd-13.1/lib/libc/sys/kqueue.2 (revision 42e62eca)
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.\"
25ad35a4c9SJonathan Lemon.\" $FreeBSD$
26ad35a4c9SJonathan Lemon.\"
27*42e62ecaSBaptiste Daroussin.Dd July 18, 2014
28ad35a4c9SJonathan Lemon.Dt KQUEUE 2
29ad35a4c9SJonathan Lemon.Os
30ad35a4c9SJonathan Lemon.Sh NAME
314451bb5dSJonathan Lemon.Nm kqueue ,
324451bb5dSJonathan Lemon.Nm kevent
33ad35a4c9SJonathan Lemon.Nd kernel event notification mechanism
344451bb5dSJonathan Lemon.Sh LIBRARY
354451bb5dSJonathan Lemon.Lb libc
36ad35a4c9SJonathan Lemon.Sh SYNOPSIS
3732eef9aeSRuslan Ermilov.In sys/types.h
3832eef9aeSRuslan Ermilov.In sys/event.h
3932eef9aeSRuslan Ermilov.In sys/time.h
40ad35a4c9SJonathan Lemon.Ft int
41ad35a4c9SJonathan Lemon.Fn kqueue "void"
42ad35a4c9SJonathan Lemon.Ft int
43a8e65b91SJonathan Lemon.Fn kevent "int kq" "const struct kevent *changelist" "int nchanges" "struct kevent *eventlist" "int nevents" "const struct timespec *timeout"
445d1c9c56SJonathan Lemon.Fn EV_SET "&kev" ident filter flags fflags data udata
45ad35a4c9SJonathan Lemon.Sh DESCRIPTION
462faeeff4SRuslan ErmilovThe
47ad35a4c9SJonathan Lemon.Fn kqueue
482faeeff4SRuslan Ermilovsystem call
49ad35a4c9SJonathan Lemonprovides a generic method of notifying the user when an event
50ad35a4c9SJonathan Lemonhappens or a condition holds, based on the results of small
51ad35a4c9SJonathan Lemonpieces of kernel code termed filters.
523c63f48cSSheldon HearnA kevent is identified by the (ident, filter) pair; there may only
53ad35a4c9SJonathan Lemonbe one unique kevent per kqueue.
544451bb5dSJonathan Lemon.Pp
55ad35a4c9SJonathan LemonThe filter is executed upon the initial registration of a kevent
563c63f48cSSheldon Hearnin order to detect whether a preexisting condition is present, and is also
57ad35a4c9SJonathan Lemonexecuted whenever an event is passed to the filter for evaluation.
58ad35a4c9SJonathan LemonIf the filter determines that the condition should be reported,
59ad35a4c9SJonathan Lemonthen the kevent is placed on the kqueue for the user to retrieve.
604451bb5dSJonathan Lemon.Pp
61ad35a4c9SJonathan LemonThe filter is also run when the user attempts to retrieve the kevent
623c63f48cSSheldon Hearnfrom the kqueue.
633c63f48cSSheldon HearnIf the filter indicates that the condition that triggered
64ad35a4c9SJonathan Lemonthe event no longer holds, the kevent is removed from the kqueue and
65ad35a4c9SJonathan Lemonis not returned.
664451bb5dSJonathan Lemon.Pp
67ad35a4c9SJonathan LemonMultiple events which trigger the filter do not result in multiple
68ad35a4c9SJonathan Lemonkevents being placed on the kqueue; instead, the filter will aggregate
69ad35a4c9SJonathan Lemonthe events into a single struct kevent.
70ad35a4c9SJonathan LemonCalling
71ad35a4c9SJonathan Lemon.Fn close
72ad35a4c9SJonathan Lemonon a file descriptor will remove any kevents that reference the descriptor.
73ad35a4c9SJonathan Lemon.Pp
742faeeff4SRuslan ErmilovThe
75ad35a4c9SJonathan Lemon.Fn kqueue
762faeeff4SRuslan Ermilovsystem call
77ad35a4c9SJonathan Lemoncreates a new kernel event queue and returns a descriptor.
78af7b56f4SJohn-Mark GurneyThe queue is not inherited by a child created with
79af7b56f4SJohn-Mark Gurney.Xr fork 2 .
803c63f48cSSheldon HearnHowever, if
813c63f48cSSheldon Hearn.Xr rfork 2
82ee85cf8bSJonathan Lemonis called without the
833c63f48cSSheldon Hearn.Dv RFFDG
84ee85cf8bSJonathan Lemonflag, then the descriptor table is shared,
85af7b56f4SJohn-Mark Gurneywhich will allow sharing of the kqueue between two processes.
86ad35a4c9SJonathan Lemon.Pp
872faeeff4SRuslan ErmilovThe
88ad35a4c9SJonathan Lemon.Fn kevent
892faeeff4SRuslan Ermilovsystem call
90ad35a4c9SJonathan Lemonis used to register events with the queue, and return any pending
91ad35a4c9SJonathan Lemonevents to the user.
922efeeba5SRuslan ErmilovThe
93ad35a4c9SJonathan Lemon.Fa changelist
942efeeba5SRuslan Ermilovargument
95a8e65b91SJonathan Lemonis a pointer to an array of
96ad35a4c9SJonathan Lemon.Va kevent
97ad35a4c9SJonathan Lemonstructures, as defined in
98fe08efe6SRuslan Ermilov.In sys/event.h .
99ad35a4c9SJonathan LemonAll changes contained in the
100ad35a4c9SJonathan Lemon.Fa changelist
101ad35a4c9SJonathan Lemonare applied before any pending events are read from the queue.
1022efeeba5SRuslan ErmilovThe
103ad35a4c9SJonathan Lemon.Fa nchanges
1042efeeba5SRuslan Ermilovargument
105ad35a4c9SJonathan Lemongives the size of
106ad35a4c9SJonathan Lemon.Fa changelist .
1072efeeba5SRuslan ErmilovThe
108ad35a4c9SJonathan Lemon.Fa eventlist
1092efeeba5SRuslan Ermilovargument
110ad35a4c9SJonathan Lemonis a pointer to an array of kevent structures.
1112efeeba5SRuslan ErmilovThe
112ad35a4c9SJonathan Lemon.Fa nevents
1132efeeba5SRuslan Ermilovargument
114ad35a4c9SJonathan Lemondetermines the size of
115ad35a4c9SJonathan Lemon.Fa eventlist .
116634862b3SJohn-Mark GurneyWhen
117634862b3SJohn-Mark Gurney.Fa nevents
118634862b3SJohn-Mark Gurneyis zero,
119634862b3SJohn-Mark Gurney.Fn kevent
120634862b3SJohn-Mark Gurneywill return immediately even if there is a
121634862b3SJohn-Mark Gurney.Fa timeout
122634862b3SJohn-Mark Gurneyspecified unlike
123634862b3SJohn-Mark Gurney.Xr select 2 .
124ad35a4c9SJonathan LemonIf
125ad35a4c9SJonathan Lemon.Fa timeout
126ad35a4c9SJonathan Lemonis a non-NULL pointer, it specifies a maximum interval to wait
1271a0a9345SRuslan Ermilovfor an event, which will be interpreted as a struct timespec.
1281a0a9345SRuslan ErmilovIf
129ad35a4c9SJonathan Lemon.Fa timeout
130ad35a4c9SJonathan Lemonis a NULL pointer,
131ad35a4c9SJonathan Lemon.Fn kevent
1321a0a9345SRuslan Ermilovwaits indefinitely.
1331a0a9345SRuslan ErmilovTo effect a poll, the
134ad35a4c9SJonathan Lemon.Fa timeout
135ad35a4c9SJonathan Lemonargument should be non-NULL, pointing to a zero-valued
136ad35a4c9SJonathan Lemon.Va timespec
1371a0a9345SRuslan Ermilovstructure.
1381a0a9345SRuslan ErmilovThe same array may be used for the
139ee85cf8bSJonathan Lemon.Fa changelist
140ee85cf8bSJonathan Lemonand
141ee85cf8bSJonathan Lemon.Fa eventlist .
142ad35a4c9SJonathan Lemon.Pp
1432faeeff4SRuslan ErmilovThe
1445d1c9c56SJonathan Lemon.Fn EV_SET
1452faeeff4SRuslan Ermilovmacro is provided for ease of initializing a
1465d1c9c56SJonathan Lemonkevent structure.
1475d1c9c56SJonathan Lemon.Pp
148ad35a4c9SJonathan LemonThe
149ad35a4c9SJonathan Lemon.Va kevent
150ad35a4c9SJonathan Lemonstructure is defined as:
151ad35a4c9SJonathan Lemon.Bd -literal
152ad35a4c9SJonathan Lemonstruct kevent {
153ad35a4c9SJonathan Lemon	uintptr_t ident;	/* identifier for this event */
154ad35a4c9SJonathan Lemon	short	  filter;	/* filter for event */
155ad35a4c9SJonathan Lemon	u_short	  flags;	/* action flags for kqueue */
156ad35a4c9SJonathan Lemon	u_int	  fflags;	/* filter flag value */
157ad35a4c9SJonathan Lemon	intptr_t  data;		/* filter data value */
158ad35a4c9SJonathan Lemon	void	  *udata;	/* opaque user data identifier */
159ad35a4c9SJonathan Lemon};
160ad35a4c9SJonathan Lemon.Ed
161ad35a4c9SJonathan Lemon.Pp
162ad35a4c9SJonathan LemonThe fields of
163ad35a4c9SJonathan Lemon.Fa struct kevent
164ad35a4c9SJonathan Lemonare:
165ad35a4c9SJonathan Lemon.Bl -tag -width XXXfilter
166ad35a4c9SJonathan Lemon.It ident
167ad35a4c9SJonathan LemonValue used to identify this event.
168ad35a4c9SJonathan LemonThe exact interpretation is determined by the attached filter,
169ad35a4c9SJonathan Lemonbut often is a file descriptor.
170ad35a4c9SJonathan Lemon.It filter
1711a0a9345SRuslan ErmilovIdentifies the kernel filter used to process this event.
1721a0a9345SRuslan ErmilovThe pre-defined
173ad35a4c9SJonathan Lemonsystem filters are described below.
174ad35a4c9SJonathan Lemon.It flags
175ad35a4c9SJonathan LemonActions to perform on the event.
176ad35a4c9SJonathan Lemon.It fflags
177ad35a4c9SJonathan LemonFilter-specific flags.
178ad35a4c9SJonathan Lemon.It data
179ad35a4c9SJonathan LemonFilter-specific data value.
180ad35a4c9SJonathan Lemon.It udata
181ad35a4c9SJonathan LemonOpaque user-defined value passed through the kernel unchanged.
182ad35a4c9SJonathan Lemon.El
183ad35a4c9SJonathan Lemon.Pp
184ad35a4c9SJonathan LemonThe
185ad35a4c9SJonathan Lemon.Va flags
186ad35a4c9SJonathan Lemonfield can contain the following values:
187ad35a4c9SJonathan Lemon.Bl -tag -width XXXEV_ONESHOT
188ad35a4c9SJonathan Lemon.It EV_ADD
1891a0a9345SRuslan ErmilovAdds the event to the kqueue.
1901a0a9345SRuslan ErmilovRe-adding an existing event
191ad35a4c9SJonathan Lemonwill modify the parameters of the original event, and not result
1921a0a9345SRuslan Ermilovin a duplicate entry.
1931a0a9345SRuslan ErmilovAdding an event automatically enables it,
194ad35a4c9SJonathan Lemonunless overridden by the EV_DISABLE flag.
195ad35a4c9SJonathan Lemon.It EV_ENABLE
196ad35a4c9SJonathan LemonPermit
197ad35a4c9SJonathan Lemon.Fn kevent
198ad35a4c9SJonathan Lemonto return the event if it is triggered.
199ad35a4c9SJonathan Lemon.It EV_DISABLE
200ad35a4c9SJonathan LemonDisable the event so
201ad35a4c9SJonathan Lemon.Fn kevent
2021a0a9345SRuslan Ermilovwill not return it.
2031a0a9345SRuslan ErmilovThe filter itself is not disabled.
2041a921c41SStacey Son.It EV_DISPATCH
2051a921c41SStacey SonDisable the event source immediately after delivery of an event.
2061a921c41SStacey SonSee
2071a921c41SStacey Son.Dv EV_DISABLE
2081a921c41SStacey Sonabove.
209ad35a4c9SJonathan Lemon.It EV_DELETE
2101a0a9345SRuslan ErmilovRemoves the event from the kqueue.
2111a0a9345SRuslan ErmilovEvents which are attached to
212ad35a4c9SJonathan Lemonfile descriptors are automatically deleted on the last close of
213ad35a4c9SJonathan Lemonthe descriptor.
214fdc1a113SStacey Son.It EV_RECEIPT
215fdc1a113SStacey SonThis flag is useful for making bulk changes to a kqueue without draining
216fdc1a113SStacey Sonany pending events.
217fdc1a113SStacey SonWhen passed as input, it forces
218fdc1a113SStacey Son.Dv EV_ERROR
219fdc1a113SStacey Sonto always be returned.
220fdc1a113SStacey SonWhen a filter is successfully added the
221fdc1a113SStacey Son.Va data
222fdc1a113SStacey Sonfield will be zero.
223ad35a4c9SJonathan Lemon.It EV_ONESHOT
224ad35a4c9SJonathan LemonCauses the event to return only the first occurrence of the filter
2251a0a9345SRuslan Ermilovbeing triggered.
2261a0a9345SRuslan ErmilovAfter the user retrieves the event from the kqueue,
227ad35a4c9SJonathan Lemonit is deleted.
228ad35a4c9SJonathan Lemon.It EV_CLEAR
229ad35a4c9SJonathan LemonAfter the event is retrieved by the user, its state is reset.
230ad35a4c9SJonathan LemonThis is useful for filters which report state transitions
2311a0a9345SRuslan Ermilovinstead of the current state.
2321a0a9345SRuslan ErmilovNote that some filters may automatically
233ad35a4c9SJonathan Lemonset this flag internally.
234ad35a4c9SJonathan Lemon.It EV_EOF
235ad35a4c9SJonathan LemonFilters may set this flag to indicate filter-specific EOF condition.
236ad35a4c9SJonathan Lemon.It EV_ERROR
237ad35a4c9SJonathan LemonSee
238ad35a4c9SJonathan Lemon.Sx RETURN VALUES
239ad35a4c9SJonathan Lemonbelow.
240ad35a4c9SJonathan Lemon.El
241ad35a4c9SJonathan Lemon.Pp
242ad35a4c9SJonathan LemonThe predefined system filters are listed below.
243ad35a4c9SJonathan LemonArguments may be passed to and from the filter via the
244ad35a4c9SJonathan Lemon.Va fflags
245ad35a4c9SJonathan Lemonand
246ad35a4c9SJonathan Lemon.Va data
247ad35a4c9SJonathan Lemonfields in the kevent structure.
248f56cfe8dSEd Schouten.Bl -tag -width EVFILT_PROCDESC
249ad35a4c9SJonathan Lemon.It EVFILT_READ
250ad35a4c9SJonathan LemonTakes a descriptor as the identifier, and returns whenever
251ad35a4c9SJonathan Lemonthere is data available to read.
252ad35a4c9SJonathan LemonThe behavior of the filter is slightly different depending
253ad35a4c9SJonathan Lemonon the descriptor type.
25442635956SRuslan Ermilov.Bl -tag -width 2n
255ad35a4c9SJonathan Lemon.It Sockets
256ad35a4c9SJonathan LemonSockets which have previously been passed to
257ad35a4c9SJonathan Lemon.Fn listen
258ad35a4c9SJonathan Lemonreturn when there is an incoming connection pending.
259ad35a4c9SJonathan Lemon.Va data
260ad35a4c9SJonathan Lemoncontains the size of the listen backlog.
261ad35a4c9SJonathan Lemon.Pp
262ad35a4c9SJonathan LemonOther socket descriptors return when there is data to be read,
2635d1c9c56SJonathan Lemonsubject to the
2645d1c9c56SJonathan Lemon.Dv SO_RCVLOWAT
2655d1c9c56SJonathan Lemonvalue of the socket buffer.
2665d1c9c56SJonathan LemonThis may be overridden with a per-filter low water mark at the
2675d1c9c56SJonathan Lemontime the filter is added by setting the
2685d1c9c56SJonathan LemonNOTE_LOWAT
2695d1c9c56SJonathan Lemonflag in
2705d1c9c56SJonathan Lemon.Va fflags ,
2715d1c9c56SJonathan Lemonand specifying the new low water mark in
2725d1c9c56SJonathan Lemon.Va data .
2735d1c9c56SJonathan LemonOn return,
274ad35a4c9SJonathan Lemon.Va data
275e0f640e8SKelly Yanceycontains the number of bytes of protocol data available to read.
276ad35a4c9SJonathan Lemon.Pp
277ad35a4c9SJonathan LemonIf the read direction of the socket has shutdown, then the filter
278ad35a4c9SJonathan Lemonalso sets EV_EOF in
2795d1c9c56SJonathan Lemon.Va flags ,
2805d1c9c56SJonathan Lemonand returns the socket error (if any) in
2815d1c9c56SJonathan Lemon.Va fflags .
282ad35a4c9SJonathan LemonIt is possible for EOF to be returned (indicating the connection is gone)
283ad35a4c9SJonathan Lemonwhile there is still data pending in the socket buffer.
284ad35a4c9SJonathan Lemon.It Vnodes
285ad35a4c9SJonathan LemonReturns when the file pointer is not at the end of file.
286ad35a4c9SJonathan Lemon.Va data
287ad35a4c9SJonathan Lemoncontains the offset from current position to end of file,
288ad35a4c9SJonathan Lemonand may be negative.
28942635956SRuslan Ermilov.It "Fifos, Pipes"
290ad35a4c9SJonathan LemonReturns when the there is data to read;
291ad35a4c9SJonathan Lemon.Va data
292ad35a4c9SJonathan Lemoncontains the number of bytes available.
293ad35a4c9SJonathan Lemon.Pp
294ad35a4c9SJonathan LemonWhen the last writer disconnects, the filter will set EV_EOF in
295ad35a4c9SJonathan Lemon.Va flags .
296ad35a4c9SJonathan LemonThis may be cleared by passing in EV_CLEAR, at which point the
297ad35a4c9SJonathan Lemonfilter will resume waiting for data to become available before
298ad35a4c9SJonathan Lemonreturning.
29995aab9ccSJohn-Mark Gurney.It "BPF devices"
30095aab9ccSJohn-Mark GurneyReturns when the BPF buffer is full, the BPF timeout has expired, or
30195aab9ccSJohn-Mark Gurneywhen the BPF has
30295aab9ccSJohn-Mark Gurney.Dq immediate mode
30395aab9ccSJohn-Mark Gurneyenabled and there is any data to read;
30495aab9ccSJohn-Mark Gurney.Va data
30595aab9ccSJohn-Mark Gurneycontains the number of bytes available.
306ad35a4c9SJonathan Lemon.El
307ad35a4c9SJonathan Lemon.It EVFILT_WRITE
308ad35a4c9SJonathan LemonTakes a descriptor as the identifier, and returns whenever
3091a0a9345SRuslan Ermilovit is possible to write to the descriptor.
3101a0a9345SRuslan ErmilovFor sockets, pipes
311ad35a4c9SJonathan Lemonand fifos,
312ad35a4c9SJonathan Lemon.Va data
313ad35a4c9SJonathan Lemonwill contain the amount of space remaining in the write buffer.
314ad35a4c9SJonathan LemonThe filter will set EV_EOF when the reader disconnects, and for
315ad35a4c9SJonathan Lemonthe fifo case, this may be cleared by use of EV_CLEAR.
31695aab9ccSJohn-Mark GurneyNote that this filter is not supported for vnodes or BPF devices.
3175d1c9c56SJonathan Lemon.Pp
3185d1c9c56SJonathan LemonFor sockets, the low water mark and socket error handling is
3195d1c9c56SJonathan Lemonidentical to the EVFILT_READ case.
320ad35a4c9SJonathan Lemon.It EVFILT_AIO
3215d1c9c56SJonathan LemonThe sigevent portion of the AIO request is filled in, with
3225d1c9c56SJonathan Lemon.Va sigev_notify_kqueue
3235d1c9c56SJonathan Lemoncontaining the descriptor of the kqueue that the event should
3245d1c9c56SJonathan Lemonbe attached to,
32503a67b59SDavid Xu.Va sigev_notify_kevent_flags
32603a67b59SDavid Xucontaining the kevent flags which should be EV_ONESHOT, EV_CLEAR or
32703a67b59SDavid XuEV_DISPATCH,
3285d1c9c56SJonathan Lemon.Va sigev_value
3295d1c9c56SJonathan Lemoncontaining the udata value, and
3305d1c9c56SJonathan Lemon.Va sigev_notify
3313aaa9695SMark Peekset to SIGEV_KEVENT.
3322faeeff4SRuslan ErmilovWhen the
3332faeeff4SRuslan Ermilov.Fn aio_*
3342faeeff4SRuslan Ermilovsystem call is made, the event will be registered
3355d1c9c56SJonathan Lemonwith the specified kqueue, and the
336ad35a4c9SJonathan Lemon.Va ident
337ad35a4c9SJonathan Lemonargument set to the
338ad35a4c9SJonathan Lemon.Fa struct aiocb
3392faeeff4SRuslan Ermilovreturned by the
3402faeeff4SRuslan Ermilov.Fn aio_*
3412faeeff4SRuslan Ermilovsystem call.
342ad35a4c9SJonathan LemonThe filter returns under the same conditions as aio_error.
343ad35a4c9SJonathan Lemon.It EVFILT_VNODE
344ad35a4c9SJonathan LemonTakes a file descriptor as the identifier and the events to watch for in
345ad35a4c9SJonathan Lemon.Va fflags ,
346ad35a4c9SJonathan Lemonand returns when one or more of the requested events occurs on the descriptor.
347ad35a4c9SJonathan LemonThe events to monitor are:
348ad35a4c9SJonathan Lemon.Bl -tag -width XXNOTE_RENAME
349ad35a4c9SJonathan Lemon.It NOTE_DELETE
3502faeeff4SRuslan ErmilovThe
351ad35a4c9SJonathan Lemon.Fn unlink
3522faeeff4SRuslan Ermilovsystem call
353ad35a4c9SJonathan Lemonwas called on the file referenced by the descriptor.
354ad35a4c9SJonathan Lemon.It NOTE_WRITE
355ad35a4c9SJonathan LemonA write occurred on the file referenced by the descriptor.
356ad35a4c9SJonathan Lemon.It NOTE_EXTEND
357ad35a4c9SJonathan LemonThe file referenced by the descriptor was extended.
358ad35a4c9SJonathan Lemon.It NOTE_ATTRIB
359ad35a4c9SJonathan LemonThe file referenced by the descriptor had its attributes changed.
360ad35a4c9SJonathan Lemon.It NOTE_LINK
361ad35a4c9SJonathan LemonThe link count on the file changed.
362ad35a4c9SJonathan Lemon.It NOTE_RENAME
363ad35a4c9SJonathan LemonThe file referenced by the descriptor was renamed.
3645d1c9c56SJonathan Lemon.It NOTE_REVOKE
3655d1c9c56SJonathan LemonAccess to the file was revoked via
3665d1c9c56SJonathan Lemon.Xr revoke 2
3673c6dd63bSMike Pritchardor the underlying file system was unmounted.
368ad35a4c9SJonathan Lemon.El
369ad35a4c9SJonathan Lemon.Pp
370ad35a4c9SJonathan LemonOn return,
371ad35a4c9SJonathan Lemon.Va fflags
372ad35a4c9SJonathan Lemoncontains the events which triggered the filter.
373ad35a4c9SJonathan Lemon.It EVFILT_PROC
374ad35a4c9SJonathan LemonTakes the process ID to monitor as the identifier and the events to watch for
375ad35a4c9SJonathan Lemonin
376ad35a4c9SJonathan Lemon.Va fflags ,
377ad35a4c9SJonathan Lemonand returns when the process performs one or more of the requested events.
378ad35a4c9SJonathan LemonIf a process can normally see another process, it can attach an event to it.
379ad35a4c9SJonathan LemonThe events to monitor are:
380ad35a4c9SJonathan Lemon.Bl -tag -width XXNOTE_TRACKERR
381ad35a4c9SJonathan Lemon.It NOTE_EXIT
382ad35a4c9SJonathan LemonThe process has exited.
3838cc7b0f9SJohn BaldwinThe exit status will be stored in
3848cc7b0f9SJohn Baldwin.Va data .
385ad35a4c9SJonathan Lemon.It NOTE_FORK
386ad35a4c9SJonathan LemonThe process has called
387ad35a4c9SJonathan Lemon.Fn fork .
388ad35a4c9SJonathan Lemon.It NOTE_EXEC
389ad35a4c9SJonathan LemonThe process has executed a new process via
390ad35a4c9SJonathan Lemon.Xr execve 2
391513bfc4fSJohn Baldwinor a similar call.
392ad35a4c9SJonathan Lemon.It NOTE_TRACK
393ad35a4c9SJonathan LemonFollow a process across
394ad35a4c9SJonathan Lemon.Fn fork
3951a0a9345SRuslan Ermilovcalls.
396513bfc4fSJohn BaldwinThe parent process registers a new kevent to monitor the child process
397513bfc4fSJohn Baldwinusing the same
398ad35a4c9SJonathan Lemon.Va fflags
399513bfc4fSJohn Baldwinas the original event.
400513bfc4fSJohn BaldwinThe child process will signal an event with NOTE_CHILD set in
401ad35a4c9SJonathan Lemon.Va fflags
402ad35a4c9SJonathan Lemonand the parent PID in
403ad35a4c9SJonathan Lemon.Va data .
404513bfc4fSJohn Baldwin.Pp
405513bfc4fSJohn BaldwinIf the parent process fails to register a new kevent
406513bfc4fSJohn Baldwin.Pq usually due to resource limitations ,
407513bfc4fSJohn Baldwinit will signal an event with NOTE_TRACKERR set in
408513bfc4fSJohn Baldwin.Va fflags ,
409513bfc4fSJohn Baldwinand the child process will not signal a NOTE_CHILD event.
410ad35a4c9SJonathan Lemon.El
411ad35a4c9SJonathan Lemon.Pp
412ad35a4c9SJonathan LemonOn return,
413ad35a4c9SJonathan Lemon.Va fflags
414ad35a4c9SJonathan Lemoncontains the events which triggered the filter.
41538219d6aSEd Schouten.It EVFILT_PROCDESC
41638219d6aSEd SchoutenTakes the process descriptor created by
41738219d6aSEd Schouten.Xr pdfork 2
41838219d6aSEd Schoutento monitor as the identifier and the events to watch for in
41938219d6aSEd Schouten.Va fflags ,
42038219d6aSEd Schoutenand returns when the associated process performs one or more of the
42138219d6aSEd Schoutenrequested events.
42238219d6aSEd SchoutenThe events to monitor are:
42338219d6aSEd Schouten.Bl -tag -width XXNOTE_EXIT
42438219d6aSEd Schouten.It NOTE_EXIT
42538219d6aSEd SchoutenThe process has exited.
42638219d6aSEd SchoutenThe exit status will be stored in
42738219d6aSEd Schouten.Va data .
42838219d6aSEd Schouten.El
42938219d6aSEd Schouten.Pp
43038219d6aSEd SchoutenOn return,
43138219d6aSEd Schouten.Va fflags
43238219d6aSEd Schoutencontains the events which triggered the filter.
433ad35a4c9SJonathan Lemon.It EVFILT_SIGNAL
434ad35a4c9SJonathan LemonTakes the signal number to monitor as the identifier and returns
435ad35a4c9SJonathan Lemonwhen the given signal is delivered to the process.
436ad35a4c9SJonathan LemonThis coexists with the
437ad35a4c9SJonathan Lemon.Fn signal
438ad35a4c9SJonathan Lemonand
439ad35a4c9SJonathan Lemon.Fn sigaction
4401a0a9345SRuslan Ermilovfacilities, and has a lower precedence.
4411a0a9345SRuslan ErmilovThe filter will record
442ad35a4c9SJonathan Lemonall attempts to deliver a signal to a process, even if the signal has
443ffce2a5bSPawel Jakub Dawidekbeen marked as SIG_IGN, except for the
444ffce2a5bSPawel Jakub Dawidek.Dv SIGCHLD
445ffce2a5bSPawel Jakub Dawideksignal, which, if ignored, won't be recorded by the filter.
4461a0a9345SRuslan ErmilovEvent notification happens after normal
447ad35a4c9SJonathan Lemonsignal delivery processing.
448ad35a4c9SJonathan Lemon.Va data
449ad35a4c9SJonathan Lemonreturns the number of times the signal has occurred since the last call to
450add06812SJonathan Lemon.Fn kevent .
451ad35a4c9SJonathan LemonThis filter automatically sets the EV_CLEAR flag internally.
452e725a129SJonathan Lemon.It EVFILT_TIMER
453e725a129SJonathan LemonEstablishes an arbitrary timer identified by
454e725a129SJonathan Lemon.Va ident .
455e725a129SJonathan LemonWhen adding a timer,
456e725a129SJonathan Lemon.Va data
457*42e62ecaSBaptiste Daroussinspecifies the timeout period.
458e725a129SJonathan LemonThe timer will be periodic unless EV_ONESHOT is specified.
459e725a129SJonathan LemonOn return,
460e725a129SJonathan Lemon.Va data
461fea88e76SGuido van Rooijcontains the number of times the timeout has expired since the last call to
462e725a129SJonathan Lemon.Fn kevent .
463e725a129SJonathan LemonThis filter automatically sets the EV_CLEAR flag internally.
464d2b2cceaSCeri DaviesThere is a system wide limit on the number of timers
465d2b2cceaSCeri Davieswhich is controlled by the
466d2b2cceaSCeri Davies.Va kern.kq_calloutmax
467d2b2cceaSCeri Daviessysctl.
468*42e62ecaSBaptiste Daroussin.Bl -tag -width XXNOTE_USECONDS
469*42e62ecaSBaptiste Daroussin.It Dv NOTE_SECONDS
470*42e62ecaSBaptiste Daroussin.Va data
471*42e62ecaSBaptiste Daroussinis in seconds.
472*42e62ecaSBaptiste Daroussin.It Dv NOTE_MSECONDS
473*42e62ecaSBaptiste Daroussin.Va data
474*42e62ecaSBaptiste Daroussinis in milliseconds.
475*42e62ecaSBaptiste Daroussin.It Dv NOTE_USECONDS
476*42e62ecaSBaptiste Daroussin.Va data
477*42e62ecaSBaptiste Daroussinis in microseconds.
478*42e62ecaSBaptiste Daroussin.It Dv NOTE_NSECONDS
479*42e62ecaSBaptiste Daroussin.Va data
480*42e62ecaSBaptiste Daroussinis in nanoseconds.
481*42e62ecaSBaptiste Daroussin.It
482*42e62ecaSBaptiste Daroussin.El
483bda16723SChristian Brueffer.Pp
484*42e62ecaSBaptiste DaroussinIf
485*42e62ecaSBaptiste Daroussin.Va fflags
486*42e62ecaSBaptiste Daroussinis not set, the default is milliseconds. On return,
487bda16723SChristian Brueffer.Va fflags
488bda16723SChristian Brueffercontains the events which triggered the filter.
4892c2e4499SStacey Son.It Dv EVFILT_USER
4902c2e4499SStacey SonEstablishes a user event identified by
4912c2e4499SStacey Son.Va ident
492f6ac2391SJoel Dahlwhich is not associated with any kernel mechanism but is triggered by
4932c2e4499SStacey Sonuser level code.
4942c2e4499SStacey SonThe lower 24 bits of the
4952c2e4499SStacey Son.Va fflags
4962c2e4499SStacey Sonmay be used for user defined flags and manipulated using the following:
4972c2e4499SStacey Son.Bl -tag -width XXNOTE_FFLAGSMASK
4982c2e4499SStacey Son.It Dv NOTE_FFNOP
4992c2e4499SStacey SonIgnore the input
5002c2e4499SStacey Son.Va fflags .
5012c2e4499SStacey Son.It Dv NOTE_FFAND
5022c2e4499SStacey SonBitwise AND
5032c2e4499SStacey Son.Va fflags .
5042c2e4499SStacey Son.It Dv NOTE_FFOR
5052c2e4499SStacey SonBitwise OR
5062c2e4499SStacey Son.Va fflags .
50720df026cSRuslan Ermilov.It Dv NOTE_FFCOPY
5082c2e4499SStacey SonCopy
5092c2e4499SStacey Son.Va fflags .
5102c2e4499SStacey Son.It Dv NOTE_FFCTRLMASK
5112c2e4499SStacey SonControl mask for
5122c2e4499SStacey Son.Va fflags .
5132c2e4499SStacey Son.It Dv NOTE_FFLAGSMASK
5142c2e4499SStacey SonUser defined flag mask for
5152c2e4499SStacey Son.Va fflags .
5162c2e4499SStacey Son.El
5172c2e4499SStacey Son.Pp
5182c2e4499SStacey SonA user event is triggered for output with the following:
5192c2e4499SStacey Son.Bl -tag -width XXNOTE_FFLAGSMASK
5202c2e4499SStacey Son.It Dv NOTE_TRIGGER
5212c2e4499SStacey SonCause the event to be triggered.
5222c2e4499SStacey Son.El
5232c2e4499SStacey Son.Pp
5242c2e4499SStacey SonOn return,
5252c2e4499SStacey Son.Va fflags
5262c2e4499SStacey Soncontains the users defined flags in the lower 24 bits.
527ad35a4c9SJonathan Lemon.El
528ad35a4c9SJonathan Lemon.Sh RETURN VALUES
5292faeeff4SRuslan ErmilovThe
530af7b56f4SJohn-Mark Gurney.Fn kqueue
5312faeeff4SRuslan Ermilovsystem call
532af7b56f4SJohn-Mark Gurneycreates a new kernel event queue and returns a file descriptor.
533af7b56f4SJohn-Mark GurneyIf there was an error creating the kernel event queue, a value of -1 is
534af7b56f4SJohn-Mark Gurneyreturned and errno set.
535af7b56f4SJohn-Mark Gurney.Pp
5362faeeff4SRuslan ErmilovThe
537ad35a4c9SJonathan Lemon.Fn kevent
5382faeeff4SRuslan Ermilovsystem call
539ad35a4c9SJonathan Lemonreturns the number of events placed in the
540bb33e422SChris Costello.Fa eventlist ,
541ad35a4c9SJonathan Lemonup to the value given by
542bb33e422SChris Costello.Fa nevents .
543ad35a4c9SJonathan LemonIf an error occurs while processing an element of the
544bb33e422SChris Costello.Fa changelist
545ad35a4c9SJonathan Lemonand there is enough room in the
546bb33e422SChris Costello.Fa eventlist ,
547ad35a4c9SJonathan Lemonthen the event will be placed in the
548bb33e422SChris Costello.Fa eventlist
549ad35a4c9SJonathan Lemonwith
550ad35a4c9SJonathan Lemon.Dv EV_ERROR
551ad35a4c9SJonathan Lemonset in
552ad35a4c9SJonathan Lemon.Va flags
553ad35a4c9SJonathan Lemonand the system error in
554ad35a4c9SJonathan Lemon.Va data .
555ad35a4c9SJonathan LemonOtherwise,
556ad35a4c9SJonathan Lemon.Dv -1
557ad35a4c9SJonathan Lemonwill be returned, and
558ad35a4c9SJonathan Lemon.Dv errno
559ad35a4c9SJonathan Lemonwill be set to indicate the error condition.
560ad35a4c9SJonathan LemonIf the time limit expires, then
561ad35a4c9SJonathan Lemon.Fn kevent
562ad35a4c9SJonathan Lemonreturns 0.
563ad35a4c9SJonathan Lemon.Sh ERRORS
564ad35a4c9SJonathan LemonThe
565af7b56f4SJohn-Mark Gurney.Fn kqueue
5662faeeff4SRuslan Ermilovsystem call fails if:
567af7b56f4SJohn-Mark Gurney.Bl -tag -width Er
568af7b56f4SJohn-Mark Gurney.It Bq Er ENOMEM
569af7b56f4SJohn-Mark GurneyThe kernel failed to allocate enough memory for the kernel queue.
5701947c8a6SJilles Tjoelker.It Bq Er ENOMEM
5711947c8a6SJilles TjoelkerThe
5721947c8a6SJilles Tjoelker.Dv RLIMIT_KQUEUES
5731947c8a6SJilles Tjoelkerrlimit
5741947c8a6SJilles Tjoelker(see
5751947c8a6SJilles Tjoelker.Xr getrlimit 2 )
5761947c8a6SJilles Tjoelkerfor the current user would be exceeded.
577af7b56f4SJohn-Mark Gurney.It Bq Er EMFILE
578af7b56f4SJohn-Mark GurneyThe per-process descriptor table is full.
579af7b56f4SJohn-Mark Gurney.It Bq Er ENFILE
580af7b56f4SJohn-Mark GurneyThe system file table is full.
581af7b56f4SJohn-Mark Gurney.El
582af7b56f4SJohn-Mark Gurney.Pp
583af7b56f4SJohn-Mark GurneyThe
584ad35a4c9SJonathan Lemon.Fn kevent
5852faeeff4SRuslan Ermilovsystem call fails if:
586ad35a4c9SJonathan Lemon.Bl -tag -width Er
587c23155a4SRuslan Ermilov.It Bq Er EACCES
588ad35a4c9SJonathan LemonThe process does not have permission to register a filter.
589ad35a4c9SJonathan Lemon.It Bq Er EFAULT
590ad35a4c9SJonathan LemonThere was an error reading or writing the
591ad35a4c9SJonathan Lemon.Va kevent
592ad35a4c9SJonathan Lemonstructure.
593ad35a4c9SJonathan Lemon.It Bq Er EBADF
594ad35a4c9SJonathan LemonThe specified descriptor is invalid.
595ad35a4c9SJonathan Lemon.It Bq Er EINTR
596ad35a4c9SJonathan LemonA signal was delivered before the timeout expired and before any
597ad35a4c9SJonathan Lemonevents were placed on the kqueue for return.
598ad35a4c9SJonathan Lemon.It Bq Er EINVAL
599ad35a4c9SJonathan LemonThe specified time limit or filter is invalid.
600b275c430SJonathan Lemon.It Bq Er ENOENT
601b275c430SJonathan LemonThe event could not be found to be modified or deleted.
602ad35a4c9SJonathan Lemon.It Bq Er ENOMEM
603d2b2cceaSCeri DaviesNo memory was available to register the event
604d2b2cceaSCeri Daviesor, in the special case of a timer, the maximum number of
605d2b2cceaSCeri Daviestimers has been exceeded.
606d2b2cceaSCeri DaviesThis maximum is configurable via the
607d2b2cceaSCeri Davies.Va kern.kq_calloutmax
608d2b2cceaSCeri Daviessysctl.
609ad35a4c9SJonathan Lemon.It Bq Er ESRCH
610ad35a4c9SJonathan LemonThe specified process to attach to does not exist.
611ad35a4c9SJonathan Lemon.El
612ad35a4c9SJonathan Lemon.Sh SEE ALSO
613ad35a4c9SJonathan Lemon.Xr aio_error 2 ,
614ad35a4c9SJonathan Lemon.Xr aio_read 2 ,
615ad35a4c9SJonathan Lemon.Xr aio_return 2 ,
616ad35a4c9SJonathan Lemon.Xr poll 2 ,
617ad35a4c9SJonathan Lemon.Xr read 2 ,
618ad35a4c9SJonathan Lemon.Xr select 2 ,
619ad35a4c9SJonathan Lemon.Xr sigaction 2 ,
6203c63f48cSSheldon Hearn.Xr write 2 ,
6213c63f48cSSheldon Hearn.Xr signal 3
622ad35a4c9SJonathan Lemon.Sh HISTORY
623ad35a4c9SJonathan LemonThe
624ad35a4c9SJonathan Lemon.Fn kqueue
625ad35a4c9SJonathan Lemonand
626ad35a4c9SJonathan Lemon.Fn kevent
6272faeeff4SRuslan Ermilovsystem calls first appeared in
62855b8fbfbSJohn-Mark Gurney.Fx 4.1 .
6294451bb5dSJonathan Lemon.Sh AUTHORS
630ad35a4c9SJonathan LemonThe
631ad35a4c9SJonathan Lemon.Fn kqueue
632ad35a4c9SJonathan Lemonsystem and this manual page were written by
6338fbf3d50SBaptiste Daroussin.An Jonathan Lemon Aq Mt [email protected] .
634e585cdf0SJohn-Mark Gurney.Sh BUGS
635bda16723SChristian BruefferThe
6363b38d66bSColin Percival.Fa timeout
6373b38d66bSColin Percivalvalue is limited to 24 hours; longer timeouts will be silently
6383b38d66bSColin Percivalreinterpreted as 24 hours.
639