xref: /netbsd-src/lib/libc/sys/kqueue.2 (revision afab4e300d3a9fb07dd8c80daf53d0feb3345706)
1.\"	$NetBSD: kqueue.2,v 1.58 2022/02/13 16:51:56 pgoyette Exp $
2.\"
3.\" Copyright (c) 2000 Jonathan Lemon
4.\" All rights reserved.
5.\"
6.\" Copyright (c) 2001, 2002, 2003 The NetBSD Foundation, Inc.
7.\" All rights reserved.
8.\"
9.\" Portions of this documentation is derived from text contributed by
10.\" Luke Mewburn.
11.\"
12.\" Redistribution and use in source and binary forms, with or without
13.\" modification, are permitted provided that the following conditions
14.\" are met:
15.\" 1. Redistributions of source code must retain the above copyright
16.\"    notice, this list of conditions and the following disclaimer.
17.\" 2. Redistributions in binary form must reproduce the above copyright
18.\"    notice, this list of conditions and the following disclaimer in the
19.\"    documentation and/or other materials provided with the distribution.
20.\"
21.\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND
22.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31.\" SUCH DAMAGE.
32.\"
33.\" $FreeBSD: src/lib/libc/sys/kqueue.2,v 1.22 2001/06/27 19:55:57 dd Exp $
34.\"
35.Dd February 13, 2022
36.Dt KQUEUE 2
37.Os
38.Sh NAME
39.Nm kqueue ,
40.Nm kqueue1 ,
41.Nm kevent ,
42.Nm EV_SET
43.Nd kernel event notification mechanism
44.Sh LIBRARY
45.Lb libc
46.Sh SYNOPSIS
47.In sys/event.h
48.In sys/time.h
49.Ft int
50.Fn kqueue "void"
51.Ft int
52.Fn kqueue1 "int flags"
53.Ft int
54.Fn kevent "int kq" "const struct kevent *changelist" "size_t nchanges" "struct kevent *eventlist" "size_t nevents" "const struct timespec *timeout"
55.Fn EV_SET "&kev" ident filter flags fflags data udata
56.Sh DESCRIPTION
57The
58.Fn kqueue
59system call
60provides a generic method of notifying the user when an event
61happens or a condition holds, based on the results of small
62pieces of kernel code termed filters.
63A kevent is identified by the (ident, filter) pair; there may only
64be one unique kevent per kqueue.
65.Pp
66The filter is executed upon the initial registration of a kevent
67in order to detect whether a preexisting condition is present, and is also
68executed whenever an event is passed to the filter for evaluation.
69If the filter determines that the condition should be reported,
70then the kevent is placed on the kqueue for the user to retrieve.
71.Pp
72The filter is also run when the user attempts to retrieve the kevent
73from the kqueue.
74If the filter indicates that the condition that triggered
75the event no longer holds, the kevent is removed from the kqueue and
76is not returned.
77.Pp
78Multiple events which trigger the filter do not result in multiple
79kevents being placed on the kqueue; instead, the filter will aggregate
80the events into a single struct kevent.
81Calling
82.Xr close 2
83on a file descriptor will remove any kevents that reference the descriptor.
84.Pp
85The
86.Fn kqueue
87system call
88creates a new kernel event queue and returns a descriptor.
89.Pp
90The
91.Fn kqueue1
92system call also allows to set the following
93.Fa flags
94on the returned file descriptor:
95.Bl -column O_NONBLOCK -offset indent
96.It Dv O_CLOEXEC
97Set the close on exec property.
98.It Dv O_NONBLOCK
99Set non-blocking I/O.
100.It Dv O_NOSIGPIPE
101Return
102.Er EPIPE
103instead of raising
104.Dv SIGPIPE .
105.El
106.Pp
107The queue is not inherited by a child created with
108.Xr fork 2 .
109.\" However, if
110.\" .Xr rfork 2
111.\" is called without the
112.\" .Dv RFFDG
113.\" flag, then the descriptor table is shared,
114.\" which will allow sharing of the kqueue between two processes.
115.Pp
116The
117.Fn kevent
118system call
119is used to register events with the queue, and return any pending
120events to the user.
121The
122.Fa changelist
123argument
124is a pointer to an array of
125.Va kevent
126structures, as defined in
127.In sys/event.h .
128All changes contained in the
129.Fa changelist
130are applied before any pending events are read from the queue.
131The
132.Fa nchanges
133argument
134gives the size of
135.Fa changelist .
136The
137.Fa eventlist
138argument
139is a pointer to an array of kevent structures.
140The
141.Fa nevents
142argument
143determines the size of
144.Fa eventlist .
145When
146.Fa nevents
147is zero,
148.Fn kevent
149will return immediately even if there is a
150.Fa timeout
151specified unlike
152.Xr select 2 .
153If
154.Fa timeout
155is a
156.No non- Ns Dv NULL
157pointer, it specifies a maximum interval to wait
158for an event, which will be interpreted as a
159.Li struct timespec .
160If
161.Fa timeout
162is a
163.Dv NULL
164pointer,
165.Fn kevent
166waits indefinitely.
167To effect a poll, the
168.Fa timeout
169argument should be
170.No non- Ns Dv NULL ,
171pointing to a zero-valued
172.Xr timespec 3
173structure.
174The same array may be used for the
175.Fa changelist
176and
177.Fa eventlist .
178.Pp
179The
180.Fn EV_SET
181macro is provided for ease of initializing a kevent structure.
182This macro does not evaluate its parameters multiple times.
183.Pp
184The
185.Va kevent
186structure is defined as:
187.Bd -literal
188struct kevent {
189	uintptr_t ident;	/* identifier for this event */
190	uint32_t  filter;	/* filter for event */
191	uint32_t  flags;	/* action flags for kqueue */
192	uint32_t  fflags;	/* filter flag value */
193	int64_t   data;		/* filter data value */
194	void     *udata;	/* opaque user data identifier */
195};
196.Ed
197.Pp
198The fields of
199.Fa struct kevent
200are:
201.Bl -tag -width "Fa filter" -offset indent
202.It ident
203Value used to identify this event.
204The exact interpretation is determined by the attached filter,
205but often is a file descriptor.
206.It Fa filter
207Identifies the kernel filter used to process this event.
208There are pre-defined system filters (which are described below), and
209other filters may be added by kernel subsystems as necessary.
210.It Fa flags
211Actions to perform on the event.
212.It Fa fflags
213Filter-specific flags.
214.It Fa data
215Filter-specific data value.
216.It Fa udata
217Opaque user-defined value passed through the kernel unchanged.
218.El
219.Pp
220The
221.Va flags
222field can contain the following values:
223.Bl -tag -width XXXEV_ONESHOT -offset indent
224.It Dv EV_ADD
225Adds the event to the kqueue.
226Re-adding an existing event will modify the parameters of the original
227event, and not result in a duplicate entry.
228Adding an event automatically enables it,
229unless overridden by the EV_DISABLE flag.
230.It Dv EV_ENABLE
231Permit
232.Fn kevent
233to return the event if it is triggered.
234.It Dv EV_DISABLE
235Disable the event so
236.Fn kevent
237will not return it.
238The filter itself is not disabled.
239.It Dv EV_DISPATCH
240Disable the event source immediately after delivery of an event.
241See
242.Dv EV_DISABLE
243above.
244.It Dv EV_DELETE
245Removes the event from the kqueue.
246Events which are attached to file descriptors are automatically deleted
247on the last close of the descriptor.
248.It Dv EV_RECEIPT
249This flag is useful for making bulk changes to a kqueue without draining
250any pending events.
251When passed as input, it forces
252.Dv EV_ERROR
253to always be returned.
254When a filter is successfully added the
255.Va data
256field will be zero.
257Note that if this flag is encountered and there is no remaining space in
258.Fa eventlist
259to hold the
260.Dv EV_ERROR
261event, then subsequent changes will not get processed.
262.It Dv EV_ONESHOT
263Causes the event to return only the first occurrence of the filter
264being triggered.
265After the user retrieves the event from the kqueue, it is deleted.
266.It Dv EV_CLEAR
267After the event is retrieved by the user, its state is reset.
268This is useful for filters which report state transitions
269instead of the current state.
270Note that some filters may automatically set this flag internally.
271.It Dv EV_EOF
272Filters may set this flag to indicate filter-specific EOF condition.
273.It Dv EV_ERROR
274See
275.Sx RETURN VALUES
276below.
277.El
278.Ss Filters
279Filters are identified by a number.
280There are two types of filters; pre-defined filters which
281are described below, and third-party filters that may be added with
282.Xr kfilter_register 9
283by kernel sub-systems, third-party device drivers, or loadable
284kernel modules.
285.Pp
286As a third-party filter is referenced by a well-known name instead
287of a statically assigned number, two
288.Xr ioctl 2 Ns s
289are supported on the file descriptor returned by
290.Fn kqueue
291to map a filter name to a filter number, and vice-versa (passing
292arguments in a structure described below):
293.Bl -tag -width KFILTER_BYFILTER -offset indent
294.It Dv KFILTER_BYFILTER
295Map
296.Va filter
297to
298.Va name ,
299which is of size
300.Va len .
301.It Dv KFILTER_BYNAME
302Map
303.Va name
304to
305.Va filter .
306.Va len
307is ignored.
308.El
309.Pp
310The following structure is used to pass arguments in and out of the
311.Xr ioctl 2 :
312.Bd -literal -offset indent
313struct kfilter_mapping {
314	char	 *name;		/* name to lookup or return */
315	size_t	 len;		/* length of name */
316	uint32_t filter;	/* filter to lookup or return */
317};
318.Ed
319.Pp
320The predefined system filters are listed below.
321Arguments may be passed to and from the filter via the
322.Va fflags
323and
324.Va data
325fields in the kevent structure.
326.Pp
327The predefined system filters are:
328.Bl -tag -width EVFILT_SIGNAL
329.It Dv EVFILT_READ
330Takes a descriptor as the identifier, and returns whenever
331there is data available to read.
332The behavior of the filter is slightly different depending
333on the descriptor type.
334.Bl -tag -width 2n
335.It Sockets
336Sockets which have previously been passed to
337.Xr listen 2
338return when there is an incoming connection pending.
339.Va data
340contains the size of the listen backlog (i.e., the number of
341connections ready to be accepted with
342.Xr accept 2 . )
343.Pp
344Other socket descriptors return when there is data to be read,
345subject to the
346.Dv SO_RCVLOWAT
347value of the socket buffer.
348This may be overridden with a per-filter low water mark at the
349time the filter is added by setting the
350NOTE_LOWAT
351flag in
352.Va fflags ,
353and specifying the new low water mark in
354.Va data .
355On return,
356.Va data
357contains the number of bytes in the socket buffer.
358.Pp
359If the read direction of the socket has shutdown, then the filter
360also sets EV_EOF in
361.Va flags ,
362and returns the socket error (if any) in
363.Va fflags .
364It is possible for EOF to be returned (indicating the connection is gone)
365while there is still data pending in the socket buffer.
366.It Vnodes
367Returns when the file pointer is not at the end of file.
368.Va data
369contains the offset from current position to end of file,
370and may be negative.
371.\" .Pp
372.\" This behavior is different from
373.\" .Xr poll 2 ,
374.\" where read events are triggered for regular files unconditionally.
375.\" This event can be triggered unconditionally by setting the
376.\" .Dv NOTE_FILE_POLL
377.\" flag in
378.\" .Va fflags .
379.It "Fifos, Pipes"
380Returns when there is data to read;
381.Va data
382contains the number of bytes available.
383.Pp
384When the last writer disconnects, the filter will set EV_EOF in
385.Va flags .
386This may be cleared by passing in EV_CLEAR, at which point the
387filter will resume waiting for data to become available before
388returning.
389.It "BPF devices"
390Returns when the BPF buffer is full, the BPF timeout has expired, or
391when the BPF has
392.Dq immediate mode
393enabled and there is any data to read;
394.Va data
395contains the number of bytes available.
396.El
397.It Dv EVFILT_WRITE
398Takes a descriptor as the identifier, and returns whenever
399it is possible to write to the descriptor.
400For sockets, pipes, fifos, and ttys,
401.Va data
402will contain the amount of space remaining in the write buffer.
403The filter will set EV_EOF when the reader disconnects, and for
404the fifo case, this may be cleared by use of EV_CLEAR.
405Note that this filter is not supported for vnodes.
406.Pp
407For sockets, the low water mark and socket error handling is
408identical to the EVFILT_READ case.
409.It Dv EVFILT_EMPTY
410Takes a descriptor as the identifier, and returns whenever
411there is no remaining data in the write buffer.
412This is currently implemented only for sockets.
413It's primary purpose is to provide visibility to an application that all
414previously written data has been acknowledged by the receiver at the TCP
415layer.
416.It Dv EVFILT_AIO
417This is not implemented in
418.Nx .
419.ig
420The sigevent portion of the AIO request is filled in, with
421.Va sigev_notify_kqueue
422containing the descriptor of the kqueue that the event should
423be attached to,
424.Va sigev_value
425containing the udata value, and
426.Va sigev_notify
427set to SIGEV_EVENT.
428When the aio_* function is called, the event will be registered
429with the specified kqueue, and the
430.Va ident
431argument set to the
432.Fa struct aiocb
433returned by the aio_* function.
434The filter returns under the same conditions as aio_error.
435.Pp
436Alternatively, a kevent structure may be initialized, with
437.Va ident
438containing the descriptor of the kqueue, and the
439address of the kevent structure placed in the
440.Va aio_lio_opcode
441field of the AIO request.
442However, this approach will not work on
443architectures with 64-bit pointers, and should be considered deprecated.
444..
445.It Dv EVFILT_VNODE
446Takes a file descriptor as the identifier and the events to watch for in
447.Va fflags ,
448and returns when one or more of the requested events occurs on the descriptor.
449The events to monitor are:
450.Bl -tag -width NOTE_CLOSE_WRITE
451.It Dv NOTE_ATTRIB
452The file referenced by the descriptor had its attributes changed.
453.It Dv NOTE_CLOSE
454A file descriptor without write access referencing the file was closed.
455.It Dv NOTE_CLOSE_WRITE
456A file descriptor with write access referencing the file was closed.
457.It Dv NOTE_DELETE
458.Xr unlink 2
459was called on the file referenced by the descriptor.
460.It Dv NOTE_EXTEND
461The file referenced by the descriptor was extended.
462.It Dv NOTE_LINK
463The link count on the file changed.
464.It Dv NOTE_OPEN
465The file referenced by the descriptor was opened.
466.It Dv NOTE_READ
467A read occurred on the file referenced by the descriptor.
468.It Dv NOTE_RENAME
469The file referenced by the descriptor was renamed.
470.It Dv NOTE_REVOKE
471Access to the file was revoked via
472.Xr revoke 2
473or the underlying file system was unmounted.
474.It Dv NOTE_WRITE
475A write occurred on the file referenced by the descriptor.
476.El
477.Pp
478On return,
479.Va fflags
480contains the events which triggered the filter.
481.It Dv EVFILT_PROC
482Takes the process ID to monitor as the identifier and the events to watch for
483in
484.Va fflags ,
485and returns when the process performs one or more of the requested events.
486If a process can normally see another process, it can attach an event to it.
487The events to monitor are:
488.Bl -tag -width XXNOTE_TRACKERR
489.It Dv NOTE_EXIT
490The process has exited.
491The exit code of the process is stored in
492.Va data .
493.It Dv NOTE_FORK
494The process has called
495.Xr fork 2 .
496.It Dv NOTE_EXEC
497The process has executed a new process via
498.Xr execve 2
499or similar call.
500.It Dv NOTE_TRACK
501Follow a process across
502.Xr fork 2
503calls.
504The parent process will return with NOTE_TRACK set in the
505.Va fflags
506field, while the child process will return with NOTE_CHILD set in
507.Va fflags
508and the parent PID in
509.Va data .
510.It Dv NOTE_TRACKERR
511This flag is returned if the system was unable to attach an event to
512the child process, usually due to resource limitations.
513.El
514.Pp
515On return,
516.Va fflags
517contains the events which triggered the filter.
518.It Dv EVFILT_SIGNAL
519Takes the signal number to monitor as the identifier and returns
520when the given signal is delivered to the current process.
521This coexists with the
522.Xr signal 3
523and
524.Xr sigaction 2
525facilities, and has a lower precedence.
526The filter will record
527all attempts to deliver a signal to a process, even if the signal has
528been marked as SIG_IGN.
529Event notification happens after normal signal delivery processing.
530.Va data
531returns the number of times the signal has occurred since the last call to
532.Fn kevent .
533This filter automatically sets the EV_CLEAR flag internally.
534.It Dv EVFILT_TIMER
535Establishes an arbitrary timer identified by
536.Va ident .
537When adding a timer,
538.Va data
539specifies the timeout period in units described below, or, if
540.Dv NOTE_ABSTIME
541is set in
542.Va fflags ,
543specifies the absolute time at which the timer should fire.
544The timer will repeat unless
545.Dv EV_ONESHOT
546is set in
547.Va flags
548or
549.Dv NOTE_ABSTIME
550is set in
551.Va fflags .
552On return,
553.Va data
554contains the number of times the timeout has expired since the last call to
555.Fn kevent .
556This filter automatically sets
557.Dv EV_CLEAR
558in
559.va flags
560for periodic timers.
561Timers created with
562.Dv NOTE_ABSTIME
563remain activated on the kqueue once the absolute time has passed unless
564.Dv EV_CLEAR
565or
566.Dv EV_ONESHOT
567are also specified.
568.Dv CLOCK_REALTIME
569is the reference clock for timers created with
570.Dv NOTE_ABSTIME.
571.Pp
572The filter accepts the following flags in the
573.Va fflags
574argument:
575.Bl -tag -width XXNOTE_TRACKERR
576.It Dv NOTE_SECONDS
577The timer value in
578.Va data
579is expressed in seconds.
580.It Dv NOTE_MSECONDS
581The timer value in
582.Va data
583is expressed in milliseconds.
584.It Dv NOTE_USECONDS
585The timer value in
586.Va data
587is expressed in microseconds.
588.It Dv NOTE_NSECONDS
589The timer value in
590.Va data
591is expressed in nanoseconds.
592.It Dv NOTE_ABSTIME
593The timer value is an absolute time; see discussion above.
594.El
595.Pp
596Note that
597.Dv NOTE_SECONDS ,
598.Dv NOTE_MSECONDS ,
599.Dv NOTE_USECONDS ,
600and
601.Dv NOTE_NSECONDS
602are mutually exclusive; behavior is undefined if more than one are specified.
603If a timer value unit is not specified, the default is
604.Dv NOTE_MSECONDS .
605.It Dv EVFILT_FS
606Establishes a file system monitor.
607Currently it only monitors file system mount and unmount actions.
608.It Dv EVFILT_USER
609Establishes a user event identified by
610.Va ident
611which is not associated with any kernel mechanism but is triggered by
612user level code.
613The lower 24 bits of the
614.Va fflags
615may be used for user defined flags and manipulated using the following:
616.Bl -tag -width "Dv NOTE_FFLAGSMASK"
617.It Dv NOTE_FFNOP
618Ignore the input
619.Va fflags .
620.It Dv NOTE_FFAND
621Bitwise AND
622.Va fflags .
623.It Dv NOTE_FFOR
624Bitwise OR
625.Va fflags .
626.It Dv NOTE_FFCOPY
627Copy
628.Va fflags .
629.It Dv NOTE_FFCTRLMASK
630Control mask for
631.Va fflags .
632.It Dv NOTE_FFLAGSMASK
633User defined flag mask for
634.Va fflags .
635.El
636.Pp
637A user event is triggered for output with the following:
638.Bl -tag -width "Dv NOTE_FFLAGSMASK"
639.It Dv NOTE_TRIGGER
640Cause the event to be triggered.
641.El
642.Pp
643On return,
644.Va fflags
645contains the users defined flags in the lower 24 bits.
646.El
647.Sh CANCELLATION BEHAVIOUR
648If
649.Fa nevents
650is non-zero, i.e., the function is potentially blocking, the call
651is a cancellation point.
652Otherwise, i.e., if
653.Fa nevents
654is zero, the call is not cancellable.
655Cancellation can only occur before any changes are made to the kqueue,
656or when the call was blocked and no changes to the queue were requested.
657.Sh RETURN VALUES
658The
659.Fn kqueue
660system call
661creates a new kernel event queue and returns a file descriptor.
662If there was an error creating the kernel event queue, a value of \-1 is
663returned and
664.Dv errno
665is set.
666.Pp
667The
668.Fn kevent
669system call
670returns the number of events placed in the
671.Fa eventlist ,
672up to the value given by
673.Fa nevents .
674If an error occurs while processing an element of the
675.Fa changelist
676and there is enough room in the
677.Fa eventlist ,
678then the event will be placed in the
679.Fa eventlist
680with
681.Dv EV_ERROR
682set in
683.Va flags
684and the system error in
685.Va data .
686Otherwise,
687.Dv \-1
688will be returned, and
689.Dv errno
690will be set to indicate the error condition.
691If the time limit expires, then
692.Fn kevent
693returns 0.
694.Sh EXAMPLES
695The following example program monitors a file (provided to it as the first
696argument) and prints information about some common events it receives
697notifications for:
698.Bd -literal -offset indent
699#include <sys/types.h>
700#include <sys/event.h>
701#include <sys/time.h>
702#include <stdio.h>
703#include <unistd.h>
704#include <stdlib.h>
705#include <fcntl.h>
706#include <err.h>
707
708int
709main(int argc, char *argv[])
710{
711        int fd, kq, nev;
712        struct kevent ev;
713        static const struct timespec tout = { 1, 0 };
714
715        if ((fd = open(argv[1], O_RDONLY)) == -1)
716                err(1, "Cannot open `%s'", argv[1]);
717
718        if ((kq = kqueue()) == -1)
719                err(1, "Cannot create kqueue");
720
721        EV_SET(&ev, fd, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR,
722            NOTE_DELETE|NOTE_WRITE|NOTE_EXTEND|NOTE_ATTRIB|NOTE_LINK|
723            NOTE_RENAME|NOTE_REVOKE, 0, 0);
724        if (kevent(kq, &ev, 1, NULL, 0, &tout) == -1)
725                err(1, "kevent");
726        for (;;) {
727                nev = kevent(kq, NULL, 0, &ev, 1, &tout);
728                if (nev == -1)
729                        err(1, "kevent");
730                if (nev == 0)
731                        continue;
732                if (ev.fflags & NOTE_DELETE) {
733                        printf("deleted ");
734                        ev.fflags &= ~NOTE_DELETE;
735                }
736                if (ev.fflags & NOTE_WRITE) {
737                        printf("written ");
738                        ev.fflags &= ~NOTE_WRITE;
739                }
740                if (ev.fflags & NOTE_EXTEND) {
741                        printf("extended ");
742                        ev.fflags &= ~NOTE_EXTEND;
743                }
744                if (ev.fflags & NOTE_ATTRIB) {
745                        printf("chmod/chown/utimes ");
746                        ev.fflags &= ~NOTE_ATTRIB;
747                }
748                if (ev.fflags & NOTE_LINK) {
749                        printf("hardlinked ");
750                        ev.fflags &= ~NOTE_LINK;
751                }
752                if (ev.fflags & NOTE_RENAME) {
753                        printf("renamed ");
754                        ev.fflags &= ~NOTE_RENAME;
755                }
756                if (ev.fflags & NOTE_REVOKE) {
757                        printf("revoked ");
758                        ev.fflags &= ~NOTE_REVOKE;
759                }
760                printf("\\n");
761                if (ev.fflags)
762                        warnx("unknown event 0x%x\\n", ev.fflags);
763        }
764}
765.Ed
766.Sh ERRORS
767The
768.Fn kqueue
769function fails if:
770.Bl -tag -width Er
771.It Bq Er EMFILE
772The per-process descriptor table is full.
773.It Bq Er ENFILE
774The system file table is full.
775.It Bq Er ENOMEM
776The kernel failed to allocate enough memory for the kernel queue.
777.El
778.Pp
779The
780.Fn kevent
781function fails if:
782.Bl -tag -width Er
783.It Bq Er EACCES
784The process does not have permission to register a filter.
785.It Bq Er EBADF
786The specified descriptor is invalid.
787.It Bq Er EFAULT
788There was an error reading or writing the
789.Va kevent
790structure.
791.It Bq Er EINTR
792A signal was delivered before the timeout expired and before any
793events were placed on the kqueue for return.
794All changes contained in the
795.Fa changelist
796are applied before returning this error.
797.It Bq Er EINVAL
798The specified time limit or filter is invalid.
799.It Bq Er ENOENT
800The event could not be found to be modified or deleted.
801.It Bq Er ENOMEM
802No memory was available to register the event.
803.It Bq Er EOPNOTSUPP
804This type of file descriptor is not supported for
805.Fn kevent
806operations.
807.It Bq Er ESRCH
808The specified process to attach to does not exist.
809.El
810.Sh SEE ALSO
811.\" .Xr aio_error 2 ,
812.\" .Xr aio_read 2 ,
813.\" .Xr aio_return 2 ,
814.Xr fork 2 ,
815.Xr ioctl 2 ,
816.Xr listen 2 ,
817.Xr poll 2 ,
818.Xr read 2 ,
819.Xr select 2 ,
820.Xr sigaction 2 ,
821.Xr unlink 2 ,
822.Xr write 2 ,
823.Xr signal 3 ,
824.Xr timespec 3 ,
825.Xr kfilter_register 9 ,
826.Xr knote 9
827.Rs
828.%A Jonathan Lemon
829.%T "Kqueue: A Generic and Scalable Event Notification Facility"
830.%I USENIX Association
831.%B Proceedings of the FREENIX Track: 2001 USENIX Annual Technical Conference
832.%D June 25-30, 2001
833.%U http://www.usenix.org/event/usenix01/freenix01/full_papers/lemon/lemon.pdf
834.Re
835.Sh HISTORY
836The
837.Fn kqueue
838and
839.Fn kevent
840functions first appeared in
841.Fx 4.1 ,
842and then in
843.Nx 2.0 .
844The
845.Fn kqueue1
846function first appeared in
847.Nx 6.0 .
848.Pp
849The
850.Fn EV_SET
851macro was protected from evaluating multiple times the first argument in
852.Nx 8.0 .
853.Pp
854The
855.Va udata
856type was changed from intptr_t to void * in
857.Nx 10.0 .
858.Pp
859Support for
860.Dv NOTE_SECONDS ,
861.Dv NOTE_MSECONDS ,
862.Dv NOTE_USECONDS ,
863.Dv NOTE_NSECONDS ,
864and
865.Dv NOTE_ABSTIME
866filter flags for
867.Dv EVFILT_TIMER
868was added in
869.Nx 10.0 .
870.Pp
871Support for
872.Dv NOTE_OPEN ,
873.Dv NOTE_CLOSE ,
874.Dv NOTE_CLOSE_WRITE ,
875and
876NOTE_READ
877filter flags for
878.Dv EVFILT_VNODE
879was added in
880.Nx 10.0 .
881.Pp
882Support for
883.Dv EVFILT_EMPTY
884was added in
885.Nx 10.0 .
886