xref: /freebsd-14.2/lib/libc/sys/kqueue.2 (revision fdebdf10)
1.\" Copyright (c) 2000 Jonathan Lemon
2.\" All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\"
13.\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND
14.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23.\" SUCH DAMAGE.
24.\"
25.\" $FreeBSD$
26.\"
27.Dd April 14, 2000
28.Dt KQUEUE 2
29.Os
30.Sh NAME
31.Nm kqueue ,
32.Nm kevent
33.Nd kernel event notification mechanism
34.Sh LIBRARY
35.Lb libc
36.Sh SYNOPSIS
37.In sys/types.h
38.In sys/event.h
39.In sys/time.h
40.Ft int
41.Fn kqueue "void"
42.Ft int
43.Fn kevent "int kq" "const struct kevent *changelist" "int nchanges" "struct kevent *eventlist" "int nevents" "const struct timespec *timeout"
44.Fn EV_SET "&kev" ident filter flags fflags data udata
45.Sh DESCRIPTION
46The
47.Fn kqueue
48system call
49provides a generic method of notifying the user when an event
50happens or a condition holds, based on the results of small
51pieces of kernel code termed filters.
52A kevent is identified by the (ident, filter) pair; there may only
53be one unique kevent per kqueue.
54.Pp
55The filter is executed upon the initial registration of a kevent
56in order to detect whether a preexisting condition is present, and is also
57executed whenever an event is passed to the filter for evaluation.
58If the filter determines that the condition should be reported,
59then the kevent is placed on the kqueue for the user to retrieve.
60.Pp
61The filter is also run when the user attempts to retrieve the kevent
62from the kqueue.
63If the filter indicates that the condition that triggered
64the event no longer holds, the kevent is removed from the kqueue and
65is not returned.
66.Pp
67Multiple events which trigger the filter do not result in multiple
68kevents being placed on the kqueue; instead, the filter will aggregate
69the events into a single struct kevent.
70Calling
71.Fn close
72on a file descriptor will remove any kevents that reference the descriptor.
73.Pp
74The
75.Fn kqueue
76system call
77creates a new kernel event queue and returns a descriptor.
78The queue is not inherited by a child created with
79.Xr fork 2 .
80However, if
81.Xr rfork 2
82is called without the
83.Dv RFFDG
84flag, then the descriptor table is shared,
85which will allow sharing of the kqueue between two processes.
86.Pp
87The
88.Fn kevent
89system call
90is used to register events with the queue, and return any pending
91events to the user.
92The
93.Fa changelist
94argument
95is a pointer to an array of
96.Va kevent
97structures, as defined in
98.In sys/event.h .
99All changes contained in the
100.Fa changelist
101are applied before any pending events are read from the queue.
102The
103.Fa nchanges
104argument
105gives the size of
106.Fa changelist .
107The
108.Fa eventlist
109argument
110is a pointer to an array of kevent structures.
111The
112.Fa nevents
113argument
114determines the size of
115.Fa eventlist .
116When
117.Fa nevents
118is zero,
119.Fn kevent
120will return immediately even if there is a
121.Fa timeout
122specified unlike
123.Xr select 2 .
124If
125.Fa timeout
126is a non-NULL pointer, it specifies a maximum interval to wait
127for an event, which will be interpreted as a struct timespec.
128If
129.Fa timeout
130is a NULL pointer,
131.Fn kevent
132waits indefinitely.
133To effect a poll, the
134.Fa timeout
135argument should be non-NULL, pointing to a zero-valued
136.Va timespec
137structure.
138The same array may be used for the
139.Fa changelist
140and
141.Fa eventlist .
142.Pp
143The
144.Fn EV_SET
145macro is provided for ease of initializing a
146kevent structure.
147.Pp
148The
149.Va kevent
150structure is defined as:
151.Bd -literal
152struct kevent {
153	uintptr_t ident;	/* identifier for this event */
154	short	  filter;	/* filter for event */
155	u_short	  flags;	/* action flags for kqueue */
156	u_int	  fflags;	/* filter flag value */
157	intptr_t  data;		/* filter data value */
158	void	  *udata;	/* opaque user data identifier */
159};
160.Ed
161.Pp
162The fields of
163.Fa struct kevent
164are:
165.Bl -tag -width XXXfilter
166.It ident
167Value used to identify this event.
168The exact interpretation is determined by the attached filter,
169but often is a file descriptor.
170.It filter
171Identifies the kernel filter used to process this event.
172The pre-defined
173system filters are described below.
174.It flags
175Actions to perform on the event.
176.It fflags
177Filter-specific flags.
178.It data
179Filter-specific data value.
180.It udata
181Opaque user-defined value passed through the kernel unchanged.
182.El
183.Pp
184The
185.Va flags
186field can contain the following values:
187.Bl -tag -width XXXEV_ONESHOT
188.It EV_ADD
189Adds the event to the kqueue.
190Re-adding an existing event
191will modify the parameters of the original event, and not result
192in a duplicate entry.
193Adding an event automatically enables it,
194unless overridden by the EV_DISABLE flag.
195.It EV_ENABLE
196Permit
197.Fn kevent
198to return the event if it is triggered.
199.It EV_DISABLE
200Disable the event so
201.Fn kevent
202will not return it.
203The filter itself is not disabled.
204.It EV_DELETE
205Removes the event from the kqueue.
206Events which are attached to
207file descriptors are automatically deleted on the last close of
208the descriptor.
209.It EV_ONESHOT
210Causes the event to return only the first occurrence of the filter
211being triggered.
212After the user retrieves the event from the kqueue,
213it is deleted.
214.It EV_CLEAR
215After the event is retrieved by the user, its state is reset.
216This is useful for filters which report state transitions
217instead of the current state.
218Note that some filters may automatically
219set this flag internally.
220.It EV_EOF
221Filters may set this flag to indicate filter-specific EOF condition.
222.It EV_ERROR
223See
224.Sx RETURN VALUES
225below.
226.El
227.Pp
228The predefined system filters are listed below.
229Arguments may be passed to and from the filter via the
230.Va fflags
231and
232.Va data
233fields in the kevent structure.
234.Bl -tag -width EVFILT_SIGNAL
235.It EVFILT_READ
236Takes a descriptor as the identifier, and returns whenever
237there is data available to read.
238The behavior of the filter is slightly different depending
239on the descriptor type.
240.Pp
241.Bl -tag -width 2n
242.It Sockets
243Sockets which have previously been passed to
244.Fn listen
245return when there is an incoming connection pending.
246.Va data
247contains the size of the listen backlog.
248.Pp
249Other socket descriptors return when there is data to be read,
250subject to the
251.Dv SO_RCVLOWAT
252value of the socket buffer.
253This may be overridden with a per-filter low water mark at the
254time the filter is added by setting the
255NOTE_LOWAT
256flag in
257.Va fflags ,
258and specifying the new low water mark in
259.Va data .
260On return,
261.Va data
262contains the number of bytes of protocol data available to read.
263.Pp
264If the read direction of the socket has shutdown, then the filter
265also sets EV_EOF in
266.Va flags ,
267and returns the socket error (if any) in
268.Va fflags .
269It is possible for EOF to be returned (indicating the connection is gone)
270while there is still data pending in the socket buffer.
271.It Vnodes
272Returns when the file pointer is not at the end of file.
273.Va data
274contains the offset from current position to end of file,
275and may be negative.
276.It "Fifos, Pipes"
277Returns when the there is data to read;
278.Va data
279contains the number of bytes available.
280.Pp
281When the last writer disconnects, the filter will set EV_EOF in
282.Va flags .
283This may be cleared by passing in EV_CLEAR, at which point the
284filter will resume waiting for data to become available before
285returning.
286.It "BPF devices"
287Returns when the BPF buffer is full, the BPF timeout has expired, or
288when the BPF has
289.Dq immediate mode
290enabled and there is any data to read;
291.Va data
292contains the number of bytes available.
293.El
294.It EVFILT_WRITE
295Takes a descriptor as the identifier, and returns whenever
296it is possible to write to the descriptor.
297For sockets, pipes
298and fifos,
299.Va data
300will contain the amount of space remaining in the write buffer.
301The filter will set EV_EOF when the reader disconnects, and for
302the fifo case, this may be cleared by use of EV_CLEAR.
303Note that this filter is not supported for vnodes or BPF devices.
304.Pp
305For sockets, the low water mark and socket error handling is
306identical to the EVFILT_READ case.
307.It EVFILT_AIO
308The sigevent portion of the AIO request is filled in, with
309.Va sigev_notify_kqueue
310containing the descriptor of the kqueue that the event should
311be attached to,
312.Va sigev_value
313containing the udata value, and
314.Va sigev_notify
315set to SIGEV_KEVENT.
316When the
317.Fn aio_*
318system call is made, the event will be registered
319with the specified kqueue, and the
320.Va ident
321argument set to the
322.Fa struct aiocb
323returned by the
324.Fn aio_*
325system call.
326The filter returns under the same conditions as aio_error.
327.Pp
328Alternatively, a kevent structure may be initialized, with
329.Va ident
330containing the descriptor of the kqueue, and the
331address of the kevent structure placed in the
332.Va aio_lio_opcode
333field of the AIO request.
334However, this approach will not work on
335architectures with 64-bit pointers, and should be considered deprecated.
336.It EVFILT_VNODE
337Takes a file descriptor as the identifier and the events to watch for in
338.Va fflags ,
339and returns when one or more of the requested events occurs on the descriptor.
340The events to monitor are:
341.Bl -tag -width XXNOTE_RENAME
342.It NOTE_DELETE
343The
344.Fn unlink
345system call
346was called on the file referenced by the descriptor.
347.It NOTE_WRITE
348A write occurred on the file referenced by the descriptor.
349.It NOTE_EXTEND
350The file referenced by the descriptor was extended.
351.It NOTE_ATTRIB
352The file referenced by the descriptor had its attributes changed.
353.It NOTE_LINK
354The link count on the file changed.
355.It NOTE_RENAME
356The file referenced by the descriptor was renamed.
357.It NOTE_REVOKE
358Access to the file was revoked via
359.Xr revoke 2
360or the underlying file system was unmounted.
361.El
362.Pp
363On return,
364.Va fflags
365contains the events which triggered the filter.
366.It EVFILT_PROC
367Takes the process ID to monitor as the identifier and the events to watch for
368in
369.Va fflags ,
370and returns when the process performs one or more of the requested events.
371If a process can normally see another process, it can attach an event to it.
372The events to monitor are:
373.Bl -tag -width XXNOTE_TRACKERR
374.It NOTE_EXIT
375The process has exited.
376.It NOTE_FORK
377The process has called
378.Fn fork .
379.It NOTE_EXEC
380The process has executed a new process via
381.Xr execve 2
382or similar call.
383.It NOTE_TRACK
384Follow a process across
385.Fn fork
386calls.
387The parent process will return with NOTE_TRACK set in the
388.Va fflags
389field, while the child process will return with NOTE_CHILD set in
390.Va fflags
391and the parent PID in
392.Va data .
393.It NOTE_TRACKERR
394This flag is returned if the system was unable to attach an event to
395the child process, usually due to resource limitations.
396.El
397.Pp
398On return,
399.Va fflags
400contains the events which triggered the filter.
401.It EVFILT_SIGNAL
402Takes the signal number to monitor as the identifier and returns
403when the given signal is delivered to the process.
404This coexists with the
405.Fn signal
406and
407.Fn sigaction
408facilities, and has a lower precedence.
409The filter will record
410all attempts to deliver a signal to a process, even if the signal has
411been marked as SIG_IGN.
412Event notification happens after normal
413signal delivery processing.
414.Va data
415returns the number of times the signal has occurred since the last call to
416.Fn kevent .
417This filter automatically sets the EV_CLEAR flag internally.
418.It EVFILT_TIMER
419Establishes an arbitrary timer identified by
420.Va ident .
421When adding a timer,
422.Va data
423specifies the timeout period in milliseconds.
424The timer will be periodic unless EV_ONESHOT is specified.
425On return,
426.Va data
427contains the number of times the timeout has expired since the last call to
428.Fn kevent .
429This filter automatically sets the EV_CLEAR flag internally.
430.It Dv EVFILT_NETDEV
431Takes a descriptor to a network interface as the identifier, and the events to watch for in
432.Va fflags .
433It returns, when one or more of the requested events occur on the descriptor.
434The events to monitor are:
435.Bl -tag -width XXNOTE_LINKDOWN
436.It Dv NOTE_LINKUP
437The link is up.
438.It Dv NOTE_LINKDOWN
439The link is down.
440.It Dv NOTE_LINKINV
441The link state is invalid.
442.El
443.Pp
444On return,
445.Va fflags
446contains the events which triggered the filter.
447.El
448.Sh RETURN VALUES
449The
450.Fn kqueue
451system call
452creates a new kernel event queue and returns a file descriptor.
453If there was an error creating the kernel event queue, a value of -1 is
454returned and errno set.
455.Pp
456The
457.Fn kevent
458system call
459returns the number of events placed in the
460.Fa eventlist ,
461up to the value given by
462.Fa nevents .
463If an error occurs while processing an element of the
464.Fa changelist
465and there is enough room in the
466.Fa eventlist ,
467then the event will be placed in the
468.Fa eventlist
469with
470.Dv EV_ERROR
471set in
472.Va flags
473and the system error in
474.Va data .
475Otherwise,
476.Dv -1
477will be returned, and
478.Dv errno
479will be set to indicate the error condition.
480If the time limit expires, then
481.Fn kevent
482returns 0.
483.Sh ERRORS
484The
485.Fn kqueue
486system call fails if:
487.Bl -tag -width Er
488.It Bq Er ENOMEM
489The kernel failed to allocate enough memory for the kernel queue.
490.It Bq Er EMFILE
491The per-process descriptor table is full.
492.It Bq Er ENFILE
493The system file table is full.
494.El
495.Pp
496The
497.Fn kevent
498system call fails if:
499.Bl -tag -width Er
500.It Bq Er EACCES
501The process does not have permission to register a filter.
502.It Bq Er EFAULT
503There was an error reading or writing the
504.Va kevent
505structure.
506.It Bq Er EBADF
507The specified descriptor is invalid.
508.It Bq Er EINTR
509A signal was delivered before the timeout expired and before any
510events were placed on the kqueue for return.
511.It Bq Er EINVAL
512The specified time limit or filter is invalid.
513.It Bq Er ENOENT
514The event could not be found to be modified or deleted.
515.It Bq Er ENOMEM
516No memory was available to register the event.
517.It Bq Er ESRCH
518The specified process to attach to does not exist.
519.El
520.Sh SEE ALSO
521.Xr aio_error 2 ,
522.Xr aio_read 2 ,
523.Xr aio_return 2 ,
524.Xr poll 2 ,
525.Xr read 2 ,
526.Xr select 2 ,
527.Xr sigaction 2 ,
528.Xr write 2 ,
529.Xr signal 3
530.Sh HISTORY
531The
532.Fn kqueue
533and
534.Fn kevent
535system calls first appeared in
536.Fx 4.1 .
537.Sh AUTHORS
538The
539.Fn kqueue
540system and this manual page were written by
541.An Jonathan Lemon Aq [email protected] .
542.Sh BUGS
543It is currently not possible to watch a
544.Xr vnode 9
545that resides on anything but
546a UFS file system.
547.Pp
548The
549.Dv EVFILT_NETDEV
550filter is currently only implemented for devices that use the
551.Xr miibus 4
552driver for LINKUP and LINKDOWN operations.
553Therefore, it will not work with many non-ethernet devices.
554.Pp
555The
556.Fa timeout
557value is limited to 24 hours; longer timeouts will be silently
558reinterpreted as 24 hours.
559