xref: /netbsd-src/lib/libc/sys/kqueue.2 (revision 6805bcc8e7c1caed206e9dd3ad20c481c4ea4b51)
1.\"	$NetBSD: kqueue.2,v 1.60 2023/07/28 23:41:16 wiz 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 June 19, 2023
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	uint64_t  ext[4];	/* extensions */
196};
197.Ed
198.Pp
199The fields of
200.Fa struct kevent
201are:
202.Bl -tag -width "Fa filter" -offset indent
203.It ident
204Value used to identify this event.
205The exact interpretation is determined by the attached filter,
206but often is a file descriptor.
207.It Fa filter
208Identifies the kernel filter used to process this event.
209There are pre-defined system filters (which are described below), and
210other filters may be added by kernel subsystems as necessary.
211.It Fa flags
212Actions to perform on the event.
213.It Fa fflags
214Filter-specific flags.
215.It Fa data
216Filter-specific data value.
217.It Fa udata
218Opaque user-defined value passed through the kernel unchanged.
219.It Fa ext
220Extended data passed to and from kernel.
221The
222.Fa ext[0]
223and
224.Fa ext[1]
225members use is defined by the filter.
226If the filter does not use them, the members are copied unchanged.
227The
228.Fa ext[2]
229and
230.Fa ext[3]
231members are always passed through the kernel as-is,
232making additional context available to application.
233.El
234.Pp
235The
236.Va flags
237field can contain the following values:
238.Bl -tag -width XXXEV_ONESHOT -offset indent
239.It Dv EV_ADD
240Adds the event to the kqueue.
241Re-adding an existing event will modify the parameters of the original
242event, and not result in a duplicate entry.
243Adding an event automatically enables it,
244unless overridden by the EV_DISABLE flag.
245.It Dv EV_ENABLE
246Permit
247.Fn kevent
248to return the event if it is triggered.
249.It Dv EV_DISABLE
250Disable the event so
251.Fn kevent
252will not return it.
253The filter itself is not disabled.
254.It Dv EV_DISPATCH
255Disable the event source immediately after delivery of an event.
256See
257.Dv EV_DISABLE
258above.
259.It Dv EV_DELETE
260Removes the event from the kqueue.
261Events which are attached to file descriptors are automatically deleted
262on the last close of the descriptor.
263.It Dv EV_RECEIPT
264This flag is useful for making bulk changes to a kqueue without draining
265any pending events.
266When passed as input, it forces
267.Dv EV_ERROR
268to always be returned.
269When a filter is successfully added the
270.Va data
271field will be zero.
272Note that if this flag is encountered and there is no remaining space in
273.Fa eventlist
274to hold the
275.Dv EV_ERROR
276event, then subsequent changes will not get processed.
277.It Dv EV_ONESHOT
278Causes the event to return only the first occurrence of the filter
279being triggered.
280After the user retrieves the event from the kqueue, it is deleted.
281.It Dv EV_CLEAR
282After the event is retrieved by the user, its state is reset.
283This is useful for filters which report state transitions
284instead of the current state.
285Note that some filters may automatically set this flag internally.
286.It Dv EV_EOF
287Filters may set this flag to indicate filter-specific EOF condition.
288.It Dv EV_ERROR
289See
290.Sx RETURN VALUES
291below.
292.El
293.Ss Filters
294Filters are identified by a number.
295There are two types of filters; pre-defined filters which
296are described below, and third-party filters that may be added with
297.Xr kfilter_register 9
298by kernel sub-systems, third-party device drivers, or loadable
299kernel modules.
300.Pp
301As a third-party filter is referenced by a well-known name instead
302of a statically assigned number, two
303.Xr ioctl 2 Ns s
304are supported on the file descriptor returned by
305.Fn kqueue
306to map a filter name to a filter number, and vice-versa (passing
307arguments in a structure described below):
308.Bl -tag -width KFILTER_BYFILTER -offset indent
309.It Dv KFILTER_BYFILTER
310Map
311.Va filter
312to
313.Va name ,
314which is of size
315.Va len .
316.It Dv KFILTER_BYNAME
317Map
318.Va name
319to
320.Va filter .
321.Va len
322is ignored.
323.El
324.Pp
325The following structure is used to pass arguments in and out of the
326.Xr ioctl 2 :
327.Bd -literal -offset indent
328struct kfilter_mapping {
329	char	 *name;		/* name to lookup or return */
330	size_t	 len;		/* length of name */
331	uint32_t filter;	/* filter to lookup or return */
332};
333.Ed
334.Pp
335The predefined system filters are listed below.
336Arguments may be passed to and from the filter via the
337.Va fflags
338and
339.Va data
340fields in the kevent structure.
341.Pp
342The predefined system filters are:
343.Bl -tag -width EVFILT_SIGNAL
344.It Dv EVFILT_READ
345Takes a descriptor as the identifier, and returns whenever
346there is data available to read.
347The behavior of the filter is slightly different depending
348on the descriptor type.
349.Bl -tag -width 2n
350.It Sockets
351Sockets which have previously been passed to
352.Xr listen 2
353return when there is an incoming connection pending.
354.Va data
355contains the size of the listen backlog (i.e., the number of
356connections ready to be accepted with
357.Xr accept 2 . )
358.Pp
359Other socket descriptors return when there is data to be read,
360subject to the
361.Dv SO_RCVLOWAT
362value of the socket buffer.
363This may be overridden with a per-filter low water mark at the
364time the filter is added by setting the
365NOTE_LOWAT
366flag in
367.Va fflags ,
368and specifying the new low water mark in
369.Va data .
370On return,
371.Va data
372contains the number of bytes in the socket buffer.
373.Pp
374If the read direction of the socket has shutdown, then the filter
375also sets EV_EOF in
376.Va flags ,
377and returns the socket error (if any) in
378.Va fflags .
379It is possible for EOF to be returned (indicating the connection is gone)
380while there is still data pending in the socket buffer.
381.It Vnodes
382Returns when the file pointer is not at the end of file.
383.Va data
384contains the offset from current position to end of file,
385and may be negative.
386.\" .Pp
387.\" This behavior is different from
388.\" .Xr poll 2 ,
389.\" where read events are triggered for regular files unconditionally.
390.\" This event can be triggered unconditionally by setting the
391.\" .Dv NOTE_FILE_POLL
392.\" flag in
393.\" .Va fflags .
394.It "Fifos, Pipes"
395Returns when there is data to read;
396.Va data
397contains the number of bytes available.
398.Pp
399When the last writer disconnects, the filter will set EV_EOF in
400.Va flags .
401This may be cleared by passing in EV_CLEAR, at which point the
402filter will resume waiting for data to become available before
403returning.
404.It "BPF devices"
405Returns when the BPF buffer is full, the BPF timeout has expired, or
406when the BPF has
407.Dq immediate mode
408enabled and there is any data to read;
409.Va data
410contains the number of bytes available.
411.El
412.It Dv EVFILT_WRITE
413Takes a descriptor as the identifier, and returns whenever
414it is possible to write to the descriptor.
415For sockets, pipes, fifos, and ttys,
416.Va data
417will contain the amount of space remaining in the write buffer.
418The filter will set EV_EOF when the reader disconnects, and for
419the fifo case, this may be cleared by use of EV_CLEAR.
420Note that this filter is not supported for vnodes.
421.Pp
422For sockets, the low water mark and socket error handling is
423identical to the EVFILT_READ case.
424.It Dv EVFILT_EMPTY
425Takes a descriptor as the identifier, and returns whenever
426there is no remaining data in the write buffer.
427This is currently implemented only for sockets.
428It's primary purpose is to provide visibility to an application that all
429previously written data has been acknowledged by the receiver at the TCP
430layer.
431.It Dv EVFILT_AIO
432This is not implemented in
433.Nx .
434.ig
435The sigevent portion of the AIO request is filled in, with
436.Va sigev_notify_kqueue
437containing the descriptor of the kqueue that the event should
438be attached to,
439.Va sigev_value
440containing the udata value, and
441.Va sigev_notify
442set to SIGEV_EVENT.
443When the aio_* function is called, the event will be registered
444with the specified kqueue, and the
445.Va ident
446argument set to the
447.Fa struct aiocb
448returned by the aio_* function.
449The filter returns under the same conditions as aio_error.
450.Pp
451Alternatively, a kevent structure may be initialized, with
452.Va ident
453containing the descriptor of the kqueue, and the
454address of the kevent structure placed in the
455.Va aio_lio_opcode
456field of the AIO request.
457However, this approach will not work on
458architectures with 64-bit pointers, and should be considered deprecated.
459..
460.It Dv EVFILT_VNODE
461Takes a file descriptor as the identifier and the events to watch for in
462.Va fflags ,
463and returns when one or more of the requested events occurs on the descriptor.
464The events to monitor are:
465.Bl -tag -width NOTE_CLOSE_WRITE
466.It Dv NOTE_ATTRIB
467The file referenced by the descriptor had its attributes changed.
468.It Dv NOTE_CLOSE
469A file descriptor without write access referencing the file was closed.
470.It Dv NOTE_CLOSE_WRITE
471A file descriptor with write access referencing the file was closed.
472.It Dv NOTE_DELETE
473.Xr unlink 2
474was called on the file referenced by the descriptor.
475.It Dv NOTE_EXTEND
476The file referenced by the descriptor was extended.
477.It Dv NOTE_LINK
478The link count on the file changed.
479.It Dv NOTE_OPEN
480The file referenced by the descriptor was opened.
481.It Dv NOTE_READ
482A read occurred on the file referenced by the descriptor.
483.It Dv NOTE_RENAME
484The file referenced by the descriptor was renamed.
485.It Dv NOTE_REVOKE
486Access to the file was revoked via
487.Xr revoke 2
488or the underlying file system was unmounted.
489.It Dv NOTE_WRITE
490A write occurred on the file referenced by the descriptor.
491.El
492.Pp
493On return,
494.Va fflags
495contains the events which triggered the filter.
496.It Dv EVFILT_PROC
497Takes the process ID to monitor as the identifier and the events to watch for
498in
499.Va fflags ,
500and returns when the process performs one or more of the requested events.
501If a process can normally see another process, it can attach an event to it.
502The events to monitor are:
503.Bl -tag -width XXNOTE_TRACKERR
504.It Dv NOTE_EXIT
505The process has exited.
506The exit code of the process is stored in
507.Va data .
508.It Dv NOTE_FORK
509The process has called
510.Xr fork 2 .
511.It Dv NOTE_EXEC
512The process has executed a new process via
513.Xr execve 2
514or similar call.
515.It Dv NOTE_TRACK
516Follow a process across
517.Xr fork 2
518calls.
519The parent process will return with NOTE_TRACK set in the
520.Va fflags
521field, while the child process will return with NOTE_CHILD set in
522.Va fflags
523and the parent PID in
524.Va data .
525.It Dv NOTE_TRACKERR
526This flag is returned if the system was unable to attach an event to
527the child process, usually due to resource limitations.
528.El
529.Pp
530On return,
531.Va fflags
532contains the events which triggered the filter.
533.It Dv EVFILT_SIGNAL
534Takes the signal number to monitor as the identifier and returns
535when the given signal is delivered to the current process.
536This coexists with the
537.Xr signal 3
538and
539.Xr sigaction 2
540facilities, and has a lower precedence.
541The filter will record
542all attempts to deliver a signal to a process, even if the signal has
543been marked as SIG_IGN.
544Event notification happens after normal signal delivery processing.
545.Va data
546returns the number of times the signal has occurred since the last call to
547.Fn kevent .
548This filter automatically sets the EV_CLEAR flag internally.
549.It Dv EVFILT_TIMER
550Establishes an arbitrary timer identified by
551.Va ident .
552When adding a timer,
553.Va data
554specifies the timeout period in units described below, or, if
555.Dv NOTE_ABSTIME
556is set in
557.Va fflags ,
558specifies the absolute time at which the timer should fire.
559The timer will repeat unless
560.Dv EV_ONESHOT
561is set in
562.Va flags
563or
564.Dv NOTE_ABSTIME
565is set in
566.Va fflags .
567On return,
568.Va data
569contains the number of times the timeout has expired since the last call to
570.Fn kevent .
571This filter automatically sets
572.Dv EV_CLEAR
573in
574.Va flags
575for periodic timers.
576Timers created with
577.Dv NOTE_ABSTIME
578remain activated on the kqueue once the absolute time has passed unless
579.Dv EV_CLEAR
580or
581.Dv EV_ONESHOT
582are also specified.
583.Dv CLOCK_REALTIME
584is the reference clock for timers created with
585.Dv NOTE_ABSTIME .
586.Pp
587The filter accepts the following flags in the
588.Va fflags
589argument:
590.Bl -tag -width XXNOTE_TRACKERR
591.It Dv NOTE_SECONDS
592The timer value in
593.Va data
594is expressed in seconds.
595.It Dv NOTE_MSECONDS
596The timer value in
597.Va data
598is expressed in milliseconds.
599.It Dv NOTE_USECONDS
600The timer value in
601.Va data
602is expressed in microseconds.
603.It Dv NOTE_NSECONDS
604The timer value in
605.Va data
606is expressed in nanoseconds.
607.It Dv NOTE_ABSTIME
608The timer value is an absolute time; see discussion above.
609.El
610.Pp
611Note that
612.Dv NOTE_SECONDS ,
613.Dv NOTE_MSECONDS ,
614.Dv NOTE_USECONDS ,
615and
616.Dv NOTE_NSECONDS
617are mutually exclusive; behavior is undefined if more than one are specified.
618If a timer value unit is not specified, the default is
619.Dv NOTE_MSECONDS .
620.It Dv EVFILT_FS
621Establishes a file system monitor.
622Currently it only monitors file system mount and unmount actions.
623.It Dv EVFILT_USER
624Establishes a user event identified by
625.Va ident
626which is not associated with any kernel mechanism but is triggered by
627user level code.
628The lower 24 bits of the
629.Va fflags
630may be used for user defined flags and manipulated using the following:
631.Bl -tag -width "Dv NOTE_FFLAGSMASK"
632.It Dv NOTE_FFNOP
633Ignore the input
634.Va fflags .
635.It Dv NOTE_FFAND
636Bitwise AND
637.Va fflags .
638.It Dv NOTE_FFOR
639Bitwise OR
640.Va fflags .
641.It Dv NOTE_FFCOPY
642Copy
643.Va fflags .
644.It Dv NOTE_FFCTRLMASK
645Control mask for
646.Va fflags .
647.It Dv NOTE_FFLAGSMASK
648User defined flag mask for
649.Va fflags .
650.El
651.Pp
652A user event is triggered for output with the following:
653.Bl -tag -width "Dv NOTE_FFLAGSMASK"
654.It Dv NOTE_TRIGGER
655Cause the event to be triggered.
656.El
657.Pp
658On return,
659.Va fflags
660contains the users defined flags in the lower 24 bits.
661.El
662.Sh CANCELLATION BEHAVIOUR
663If
664.Fa nevents
665is non-zero, i.e., the function is potentially blocking, the call
666is a cancellation point.
667Otherwise, i.e., if
668.Fa nevents
669is zero, the call is not cancellable.
670Cancellation can only occur before any changes are made to the kqueue,
671or when the call was blocked and no changes to the queue were requested.
672.Sh RETURN VALUES
673The
674.Fn kqueue
675system call
676creates a new kernel event queue and returns a file descriptor.
677If there was an error creating the kernel event queue, a value of \-1 is
678returned and
679.Dv errno
680is set.
681.Pp
682The
683.Fn kevent
684system call
685returns the number of events placed in the
686.Fa eventlist ,
687up to the value given by
688.Fa nevents .
689If an error occurs while processing an element of the
690.Fa changelist
691and there is enough room in the
692.Fa eventlist ,
693then the event will be placed in the
694.Fa eventlist
695with
696.Dv EV_ERROR
697set in
698.Va flags
699and the system error in
700.Va data .
701Otherwise,
702.Dv \-1
703will be returned, and
704.Dv errno
705will be set to indicate the error condition.
706If the time limit expires, then
707.Fn kevent
708returns 0.
709.Sh EXAMPLES
710The following example program monitors a file (provided to it as the first
711argument) and prints information about some common events it receives
712notifications for:
713.Bd -literal -offset indent
714#include <sys/types.h>
715#include <sys/event.h>
716#include <sys/time.h>
717#include <stdio.h>
718#include <unistd.h>
719#include <stdlib.h>
720#include <fcntl.h>
721#include <err.h>
722
723int
724main(int argc, char *argv[])
725{
726        int fd, kq, nev;
727        struct kevent ev;
728        static const struct timespec tout = { 1, 0 };
729
730        if ((fd = open(argv[1], O_RDONLY)) == -1)
731                err(1, "Cannot open `%s'", argv[1]);
732
733        if ((kq = kqueue()) == -1)
734                err(1, "Cannot create kqueue");
735
736        EV_SET(&ev, fd, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR,
737            NOTE_DELETE|NOTE_WRITE|NOTE_EXTEND|NOTE_ATTRIB|NOTE_LINK|
738            NOTE_RENAME|NOTE_REVOKE, 0, 0);
739        if (kevent(kq, &ev, 1, NULL, 0, &tout) == -1)
740                err(1, "kevent");
741        for (;;) {
742                nev = kevent(kq, NULL, 0, &ev, 1, &tout);
743                if (nev == -1)
744                        err(1, "kevent");
745                if (nev == 0)
746                        continue;
747                if (ev.fflags & NOTE_DELETE) {
748                        printf("deleted ");
749                        ev.fflags &= ~NOTE_DELETE;
750                }
751                if (ev.fflags & NOTE_WRITE) {
752                        printf("written ");
753                        ev.fflags &= ~NOTE_WRITE;
754                }
755                if (ev.fflags & NOTE_EXTEND) {
756                        printf("extended ");
757                        ev.fflags &= ~NOTE_EXTEND;
758                }
759                if (ev.fflags & NOTE_ATTRIB) {
760                        printf("chmod/chown/utimes ");
761                        ev.fflags &= ~NOTE_ATTRIB;
762                }
763                if (ev.fflags & NOTE_LINK) {
764                        printf("hardlinked ");
765                        ev.fflags &= ~NOTE_LINK;
766                }
767                if (ev.fflags & NOTE_RENAME) {
768                        printf("renamed ");
769                        ev.fflags &= ~NOTE_RENAME;
770                }
771                if (ev.fflags & NOTE_REVOKE) {
772                        printf("revoked ");
773                        ev.fflags &= ~NOTE_REVOKE;
774                }
775                printf("\\n");
776                if (ev.fflags)
777                        warnx("unknown event 0x%x\\n", ev.fflags);
778        }
779}
780.Ed
781.Sh ERRORS
782The
783.Fn kqueue
784function fails if:
785.Bl -tag -width Er
786.It Bq Er EMFILE
787The per-process descriptor table is full.
788.It Bq Er ENFILE
789The system file table is full.
790.It Bq Er ENOMEM
791The kernel failed to allocate enough memory for the kernel queue.
792.El
793.Pp
794The
795.Fn kevent
796function fails if:
797.Bl -tag -width Er
798.It Bq Er EACCES
799The process does not have permission to register a filter.
800.It Bq Er EBADF
801The specified descriptor is invalid.
802.It Bq Er EFAULT
803There was an error reading or writing the
804.Va kevent
805structure.
806.It Bq Er EINTR
807A signal was delivered before the timeout expired and before any
808events were placed on the kqueue for return.
809All changes contained in the
810.Fa changelist
811are applied before returning this error.
812.It Bq Er EINVAL
813The specified time limit or filter is invalid.
814.It Bq Er ENOENT
815The event could not be found to be modified or deleted.
816.It Bq Er ENOMEM
817No memory was available to register the event.
818.It Bq Er EOPNOTSUPP
819This type of file descriptor is not supported for
820.Fn kevent
821operations.
822.It Bq Er ESRCH
823The specified process to attach to does not exist.
824.El
825.Sh SEE ALSO
826.\" .Xr aio_error 2 ,
827.\" .Xr aio_read 2 ,
828.\" .Xr aio_return 2 ,
829.Xr fork 2 ,
830.Xr ioctl 2 ,
831.Xr listen 2 ,
832.Xr poll 2 ,
833.Xr read 2 ,
834.Xr select 2 ,
835.Xr sigaction 2 ,
836.Xr unlink 2 ,
837.Xr write 2 ,
838.Xr signal 3 ,
839.Xr timespec 3 ,
840.Xr kfilter_register 9 ,
841.Xr knote 9
842.Rs
843.%A Jonathan Lemon
844.%T "Kqueue: A Generic and Scalable Event Notification Facility"
845.%I USENIX Association
846.%B Proceedings of the FREENIX Track: 2001 USENIX Annual Technical Conference
847.%D June 25-30, 2001
848.%U http://www.usenix.org/event/usenix01/freenix01/full_papers/lemon/lemon.pdf
849.Re
850.Sh HISTORY
851The
852.Fn kqueue
853and
854.Fn kevent
855functions first appeared in
856.Fx 4.1 ,
857and then in
858.Nx 2.0 .
859The
860.Fn kqueue1
861function first appeared in
862.Nx 6.0 .
863.Pp
864The
865.Fn EV_SET
866macro was protected from evaluating multiple times the first argument in
867.Nx 8.0 .
868.Pp
869The
870.Va udata
871type was changed from intptr_t to void * in
872.Nx 10.0 .
873.Pp
874Support for
875.Dv NOTE_SECONDS ,
876.Dv NOTE_MSECONDS ,
877.Dv NOTE_USECONDS ,
878.Dv NOTE_NSECONDS ,
879and
880.Dv NOTE_ABSTIME
881filter flags for
882.Dv EVFILT_TIMER
883was added in
884.Nx 10.0 .
885.Pp
886Support for
887.Dv NOTE_OPEN ,
888.Dv NOTE_CLOSE ,
889.Dv NOTE_CLOSE_WRITE ,
890and
891NOTE_READ
892filter flags for
893.Dv EVFILT_VNODE
894was added in
895.Nx 10.0 .
896.Pp
897Support for
898.Dv EVFILT_EMPTY
899was added in
900.Nx 10.0 .
901