xref: /freebsd-14.2/lib/libc/sys/kqueue.2 (revision 4906cdc8)
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 July 18, 2014
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 "Fa filter"
166.It Fa ident
167Value used to identify this event.
168The exact interpretation is determined by the attached filter,
169but often is a file descriptor.
170.It Fa filter
171Identifies the kernel filter used to process this event.
172The pre-defined
173system filters are described below.
174.It Fa flags
175Actions to perform on the event.
176.It Fa fflags
177Filter-specific flags.
178.It Fa data
179Filter-specific data value.
180.It Fa 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 EV_DISPATCH
188.It Dv 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 Dv EV_ENABLE
196Permit
197.Fn kevent
198to return the event if it is triggered.
199.It Dv EV_DISABLE
200Disable the event so
201.Fn kevent
202will not return it.
203The filter itself is not disabled.
204.It Dv EV_DISPATCH
205Disable the event source immediately after delivery of an event.
206See
207.Dv EV_DISABLE
208above.
209.It Dv EV_DELETE
210Removes the event from the kqueue.
211Events which are attached to
212file descriptors are automatically deleted on the last close of
213the descriptor.
214.It Dv EV_RECEIPT
215This flag is useful for making bulk changes to a kqueue without draining
216any pending events.
217When passed as input, it forces
218.Dv EV_ERROR
219to always be returned.
220When a filter is successfully added the
221.Va data
222field will be zero.
223.It Dv EV_ONESHOT
224Causes the event to return only the first occurrence of the filter
225being triggered.
226After the user retrieves the event from the kqueue,
227it is deleted.
228.It Dv EV_CLEAR
229After the event is retrieved by the user, its state is reset.
230This is useful for filters which report state transitions
231instead of the current state.
232Note that some filters may automatically
233set this flag internally.
234.It Dv EV_EOF
235Filters may set this flag to indicate filter-specific EOF condition.
236.It Dv EV_ERROR
237See
238.Sx RETURN VALUES
239below.
240.El
241.Pp
242The predefined system filters are listed below.
243Arguments may be passed to and from the filter via the
244.Va fflags
245and
246.Va data
247fields in the kevent structure.
248.Bl -tag -width "Dv EVFILT_PROCDESC"
249.It Dv EVFILT_READ
250Takes a descriptor as the identifier, and returns whenever
251there is data available to read.
252The behavior of the filter is slightly different depending
253on the descriptor type.
254.Bl -tag -width 2n
255.It Sockets
256Sockets which have previously been passed to
257.Fn listen
258return when there is an incoming connection pending.
259.Va data
260contains the size of the listen backlog.
261.Pp
262Other socket descriptors return when there is data to be read,
263subject to the
264.Dv SO_RCVLOWAT
265value of the socket buffer.
266This may be overridden with a per-filter low water mark at the
267time the filter is added by setting the
268.Dv NOTE_LOWAT
269flag in
270.Va fflags ,
271and specifying the new low water mark in
272.Va data .
273On return,
274.Va data
275contains the number of bytes of protocol data available to read.
276.Pp
277If the read direction of the socket has shutdown, then the filter
278also sets
279.Dv EV_EOF
280in
281.Va flags ,
282and returns the socket error (if any) in
283.Va fflags .
284It is possible for EOF to be returned (indicating the connection is gone)
285while there is still data pending in the socket buffer.
286.It Vnodes
287Returns when the file pointer is not at the end of file.
288.Va data
289contains the offset from current position to end of file,
290and may be negative.
291.It "Fifos, Pipes"
292Returns when the there is data to read;
293.Va data
294contains the number of bytes available.
295.Pp
296When the last writer disconnects, the filter will set
297.Dv EV_EOF
298in
299.Va flags .
300This may be cleared by passing in
301.Dv EV_CLEAR ,
302at which point the
303filter will resume waiting for data to become available before
304returning.
305.It "BPF devices"
306Returns when the BPF buffer is full, the BPF timeout has expired, or
307when the BPF has
308.Dq immediate mode
309enabled and there is any data to read;
310.Va data
311contains the number of bytes available.
312.El
313.It Dv EVFILT_WRITE
314Takes a descriptor as the identifier, and returns whenever
315it is possible to write to the descriptor.
316For sockets, pipes
317and fifos,
318.Va data
319will contain the amount of space remaining in the write buffer.
320The filter will set EV_EOF when the reader disconnects, and for
321the fifo case, this may be cleared by use of
322.Dv EV_CLEAR .
323Note that this filter is not supported for vnodes or BPF devices.
324.Pp
325For sockets, the low water mark and socket error handling is
326identical to the
327.Dv EVFILT_READ
328case.
329.It Dv EVFILT_AIO
330The sigevent portion of the AIO request is filled in, with
331.Va sigev_notify_kqueue
332containing the descriptor of the kqueue that the event should
333be attached to,
334.Va sigev_notify_kevent_flags
335containing the kevent flags which should be
336.Dv EV_ONESHOT ,
337.Dv EV_CLEAR
338or
339.Dv EV_DISPATCH ,
340.Va sigev_value
341containing the udata value, and
342.Va sigev_notify
343set to
344.Dv SIGEV_KEVENT .
345When the
346.Fn aio_*
347system call is made, the event will be registered
348with the specified kqueue, and the
349.Va ident
350argument set to the
351.Fa struct aiocb
352returned by the
353.Fn aio_*
354system call.
355The filter returns under the same conditions as
356.Fn aio_error .
357.It Dv EVFILT_VNODE
358Takes a file descriptor as the identifier and the events to watch for in
359.Va fflags ,
360and returns when one or more of the requested events occurs on the descriptor.
361The events to monitor are:
362.Bl -tag -width "Dv NOTE_RENAME"
363.It Dv NOTE_DELETE
364The
365.Fn unlink
366system call
367was called on the file referenced by the descriptor.
368.It Dv NOTE_WRITE
369A write occurred on the file referenced by the descriptor.
370.It Dv NOTE_EXTEND
371The file referenced by the descriptor was extended.
372.It Dv NOTE_ATTRIB
373The file referenced by the descriptor had its attributes changed.
374.It Dv NOTE_LINK
375The link count on the file changed.
376.It Dv NOTE_RENAME
377The file referenced by the descriptor was renamed.
378.It Dv NOTE_REVOKE
379Access to the file was revoked via
380.Xr revoke 2
381or the underlying file system was unmounted.
382.El
383.Pp
384On return,
385.Va fflags
386contains the events which triggered the filter.
387.It Dv EVFILT_PROC
388Takes the process ID to monitor as the identifier and the events to watch for
389in
390.Va fflags ,
391and returns when the process performs one or more of the requested events.
392If a process can normally see another process, it can attach an event to it.
393The events to monitor are:
394.Bl -tag -width "Dv NOTE_TRACKERR"
395.It Dv NOTE_EXIT
396The process has exited.
397The exit status will be stored in
398.Va data .
399.It Dv NOTE_FORK
400The process has called
401.Fn fork .
402.It Dv NOTE_EXEC
403The process has executed a new process via
404.Xr execve 2
405or a similar call.
406.It Dv NOTE_TRACK
407Follow a process across
408.Fn fork
409calls.
410The parent process registers a new kevent to monitor the child process
411using the same
412.Va fflags
413as the original event.
414The child process will signal an event with
415.Dv NOTE_CHILD
416set in
417.Va fflags
418and the parent PID in
419.Va data .
420.Pp
421If the parent process fails to register a new kevent
422.Pq usually due to resource limitations ,
423it will signal an event with
424.Dv NOTE_TRACKERR
425set in
426.Va fflags ,
427and the child process will not signal a
428.Dv NOTE_CHILD
429event.
430.El
431.Pp
432On return,
433.Va fflags
434contains the events which triggered the filter.
435.It Dv EVFILT_PROCDESC
436Takes the process descriptor created by
437.Xr pdfork 2
438to monitor as the identifier and the events to watch for in
439.Va fflags ,
440and returns when the associated process performs one or more of the
441requested events.
442The events to monitor are:
443.Bl -tag -width "Dv NOTE_EXIT"
444.It Dv NOTE_EXIT
445The process has exited.
446The exit status will be stored in
447.Va data .
448.El
449.Pp
450On return,
451.Va fflags
452contains the events which triggered the filter.
453.It Dv EVFILT_SIGNAL
454Takes the signal number to monitor as the identifier and returns
455when the given signal is delivered to the process.
456This coexists with the
457.Fn signal
458and
459.Fn sigaction
460facilities, and has a lower precedence.
461The filter will record
462all attempts to deliver a signal to a process, even if the signal has
463been marked as
464.Dv SIG_IGN ,
465except for the
466.Dv SIGCHLD
467signal, which, if ignored, won't be recorded by the filter.
468Event notification happens after normal
469signal delivery processing.
470.Va data
471returns the number of times the signal has occurred since the last call to
472.Fn kevent .
473This filter automatically sets the
474.Dv EV_CLEAR
475flag internally.
476.It Dv EVFILT_TIMER
477Establishes an arbitrary timer identified by
478.Va ident .
479When adding a timer,
480.Va data
481specifies the timeout period.
482The timer will be periodic unless
483.Dv EV_ONESHOT
484is specified.
485On return,
486.Va data
487contains the number of times the timeout has expired since the last call to
488.Fn kevent .
489This filter automatically sets the EV_CLEAR flag internally.
490There is a system wide limit on the number of timers
491which is controlled by the
492.Va kern.kq_calloutmax
493sysctl.
494.Bl -tag -width "Dv NOTE_USECONDS"
495.It Dv NOTE_SECONDS
496.Va data
497is in seconds.
498.It Dv NOTE_MSECONDS
499.Va data
500is in milliseconds.
501.It Dv NOTE_USECONDS
502.Va data
503is in microseconds.
504.It Dv NOTE_NSECONDS
505.Va data
506is in nanoseconds.
507.El
508.Pp
509If
510.Va fflags
511is not set, the default is milliseconds. On return,
512.Va fflags
513contains the events which triggered the filter.
514.It Dv EVFILT_USER
515Establishes a user event identified by
516.Va ident
517which is not associated with any kernel mechanism but is triggered by
518user level code.
519The lower 24 bits of the
520.Va fflags
521may be used for user defined flags and manipulated using the following:
522.Bl -tag -width "Dv NOTE_FFLAGSMASK"
523.It Dv NOTE_FFNOP
524Ignore the input
525.Va fflags .
526.It Dv NOTE_FFAND
527Bitwise AND
528.Va fflags .
529.It Dv NOTE_FFOR
530Bitwise OR
531.Va fflags .
532.It Dv NOTE_FFCOPY
533Copy
534.Va fflags .
535.It Dv NOTE_FFCTRLMASK
536Control mask for
537.Va fflags .
538.It Dv NOTE_FFLAGSMASK
539User defined flag mask for
540.Va fflags .
541.El
542.Pp
543A user event is triggered for output with the following:
544.Bl -tag -width "Dv NOTE_FFLAGSMASK"
545.It Dv NOTE_TRIGGER
546Cause the event to be triggered.
547.El
548.Pp
549On return,
550.Va fflags
551contains the users defined flags in the lower 24 bits.
552.El
553.Sh RETURN VALUES
554The
555.Fn kqueue
556system call
557creates a new kernel event queue and returns a file descriptor.
558If there was an error creating the kernel event queue, a value of -1 is
559returned and errno set.
560.Pp
561The
562.Fn kevent
563system call
564returns the number of events placed in the
565.Fa eventlist ,
566up to the value given by
567.Fa nevents .
568If an error occurs while processing an element of the
569.Fa changelist
570and there is enough room in the
571.Fa eventlist ,
572then the event will be placed in the
573.Fa eventlist
574with
575.Dv EV_ERROR
576set in
577.Va flags
578and the system error in
579.Va data .
580Otherwise,
581.Dv -1
582will be returned, and
583.Dv errno
584will be set to indicate the error condition.
585If the time limit expires, then
586.Fn kevent
587returns 0.
588.Sh ERRORS
589The
590.Fn kqueue
591system call fails if:
592.Bl -tag -width Er
593.It Bq Er ENOMEM
594The kernel failed to allocate enough memory for the kernel queue.
595.It Bq Er ENOMEM
596The
597.Dv RLIMIT_KQUEUES
598rlimit
599(see
600.Xr getrlimit 2 )
601for the current user would be exceeded.
602.It Bq Er EMFILE
603The per-process descriptor table is full.
604.It Bq Er ENFILE
605The system file table is full.
606.El
607.Pp
608The
609.Fn kevent
610system call fails if:
611.Bl -tag -width Er
612.It Bq Er EACCES
613The process does not have permission to register a filter.
614.It Bq Er EFAULT
615There was an error reading or writing the
616.Va kevent
617structure.
618.It Bq Er EBADF
619The specified descriptor is invalid.
620.It Bq Er EINTR
621A signal was delivered before the timeout expired and before any
622events were placed on the kqueue for return.
623.It Bq Er EINVAL
624The specified time limit or filter is invalid.
625.It Bq Er ENOENT
626The event could not be found to be modified or deleted.
627.It Bq Er ENOMEM
628No memory was available to register the event
629or, in the special case of a timer, the maximum number of
630timers has been exceeded.
631This maximum is configurable via the
632.Va kern.kq_calloutmax
633sysctl.
634.It Bq Er ESRCH
635The specified process to attach to does not exist.
636.El
637.Sh SEE ALSO
638.Xr aio_error 2 ,
639.Xr aio_read 2 ,
640.Xr aio_return 2 ,
641.Xr poll 2 ,
642.Xr read 2 ,
643.Xr select 2 ,
644.Xr sigaction 2 ,
645.Xr write 2 ,
646.Xr signal 3
647.Sh HISTORY
648The
649.Fn kqueue
650and
651.Fn kevent
652system calls first appeared in
653.Fx 4.1 .
654.Sh AUTHORS
655The
656.Fn kqueue
657system and this manual page were written by
658.An Jonathan Lemon Aq Mt [email protected] .
659.Sh BUGS
660The
661.Fa timeout
662value is limited to 24 hours; longer timeouts will be silently
663reinterpreted as 24 hours.
664