---
name: "kevent"
section: "2"
platform: "macos"
package: "xnu"
version: "12377.121.6"
release: "macOS 26.5"
platforms: ["linux", "freebsd", "macos"]
url: "https://new.carta.tech/kqueue"
---

# kevent(2)

kernel event notification mechanism

Part of [kqueue(2)](/kqueue/2.md).

## NAME

**kqueue**, **kevent**, **kevent64** and **kevent\_qos** – kernel event notification mechanism

## LIBRARY

Standard C Library (libc, -lc)

## SYNOPSIS

**#include \<sys/types.h\>**

**#include \<sys/event.h\>**

**#include \<sys/time.h\>**

*int*\

**kqueue**(*void*);

*int*\

**kevent**(*int kq*, *const struct kevent \*changelist*, *int nchanges*, *struct kevent \*eventlist*, *int nevents*, *const struct timespec \*timeout*);

*int*\

**kevent64**(*int kq*, *const struct kevent64\_s \*changelist*, *int nchanges*, *struct kevent64\_s \*eventlist*, *int nevents*, *unsigned int flags*, *const struct timespec \*timeout*);

*int*\

**kevent\_qos**(*int kq*, *const struct kevent\_qos\_s \*changelist*, *int nchanges*, *struct kevent\_qos\_s \*eventlist*, *int nevents*, *void \*data\_out*, *size\_t \*data\_available*, *unsigned int flags*);

**EV\_SET**(*&kev*, *ident*, *filter*, *flags*, *fflags*, *data*, *udata*);

**EV\_SET64**(*&kev*, *ident*, *filter*, *flags*, *fflags*, *data*, *udata*, *ext\[0\]*, *ext\[1\]*);

**EV\_SET\_QOS**(*&kev*, *ident*, *filter*, *flags*, *qos*, *udata*, *fflags*, *xflags*, *data*, *ext\[0\]*, *ext\[1\]*, *ext\[2\]*, *ext\[3\]*);

## DESCRIPTION

The **kqueue**() system call allocates a kqueue file descriptor.  This file descriptor provides a generic method of notifying the user when a kernel event (kevent) happens or a condition holds, based on the results of small pieces of kernel code termed filters.

A kevent is identified by an (ident, filter, and optional udata value)  tuple.  It specifies the interesting conditions to be notified about for that tuple. An (ident, filter, and optional udata value) tuple can only appear once in a given kqueue. Subsequent attempts to register the same tuple for a given kqueue will result in the replacement of the conditions being watched, not an addition. Whether the udata value is considered as part of the tuple is controlled by the EV\_UDATA\_SPECIFIC flag on the kevent.

The filter identified in a kevent is executed upon the initial registration of that event in order to detect whether a preexisting condition is present, and is also executed whenever an event is passed to the filter for evaluation. If the filter determines that the condition should be reported, then the kevent is placed on the kqueue for the user to retrieve.

The filter is also run when the user attempts to retrieve the kevent from the kqueue. If the filter indicates that the condition that triggered the event no longer holds, the kevent is removed from the kqueue and is not returned.

Multiple events which trigger the filter do not result in multiple kevents being placed on the kqueue; instead, the filter will aggregate the events into a single struct kevent. Calling **close**() on a file descriptor will remove any kevents that reference the descriptor.

The **kqueue**() system call creates a new kernel event queue and returns a descriptor. The queue is not inherited by a child created with [fork(2)](/fork/2.md).

The **kevent,**() **kevent64**() and **kevent\_qos**() system calls are used to register events with the queue, and return any pending events to the user. The *changelist* argument is a pointer to an array of *kevent,* *kevent64\_s* or *kevent\_qos\_s* structures, as defined in ⟨*sys/event.h*⟩. All changes contained in the *changelist* are applied before any pending events are read from the queue. The *nchanges* argument gives the size of *changelist*.

The *eventlist* argument is a pointer to an array of out *kevent,* *kevent64\_s* or *kevent\_qos\_s* structures. The *nevents* argument determines the size of *eventlist*.

The *data\_out* argument provides space for extra out data provided by specific filters. The *data\_available* argument's contents specified the space available in the data pool on input, and contains the amount still remaining on output. If the KEVENT\_FLAG\_STACK\_DATA flag is specified on the system call, the data is allocated from the pool in stack order instead of typical heap order.

If *timeout* is a non-NULL pointer, it specifies a maximum interval to wait for an event, which will be interpreted as a struct timespec.  If *timeout* is a NULL pointer, both **kevent**() and **kevent64**() wait indefinitely.  To effect a poll, the *flags* argument to **kevent64**() or **kevent\_qos**() can include the KEVENT\_FLAG\_IMMEDIATE value to indicate an immediate timeout.  Alternatively, the *timeout* argument should be non-NULL, pointing to a zero-valued *timespec* structure.  The same array may be used for the *changelist* and *eventlist*.

The **EV\_SET**() macro is provided for ease of initializing a *kevent* structure. Similarly,  **EV\_SET64**() initializes a *kevent64\_s* structure and **EV\_SET\_QOS**() initializes a *kevent\_qos\_s* structure.

The *kevent,* *kevent64\_s* and *kevent\_qos\_s* structures are defined as:

```
struct kevent {
	uintptr_t 	ident;		/* identifier for this event */
	int16_t		filter;		/* filter for event */
	uint16_t	flags;		/* general flags */
	uint32_t	fflags;		/* filter-specific flags */
	intptr_t  	data;		/* filter-specific data */
	void		*udata;		/* opaque user data identifier */
};
struct kevent64_s {
        uint64_t        ident;          /* identifier for this event */
	int16_t         filter;         /* filter for event */
	uint16_t        flags;          /* general flags */
	uint32_t        fflags;         /* filter-specific flags */
	int64_t         data;           /* filter-specific data */
	uint64_t        udata;          /* opaque user data identifier */
	uint64_t        ext[2];         /* filter-specific extensions */
};
struct kevent_qos_s {
        uint64_t        ident;          /* identifier for this event */
	int16_t         filter;         /* filter for event */
	uint16_t        flags;          /* general flags */
	uint32_t        qos;            /* quality of service when servicing event */
	uint64_t        udata;          /* opaque user data identifier */
	uint32_t        fflags;         /* filter-specific flags */
	uint32_t        xflags;         /* extra filter-specific flags */
	int64_t         data;           /* filter-specific data */
	uint64_t        ext[4];         /* filter-specific extensions */
};
```

\----

The fields of *struct*, *kevent,* *struct*, *kevent64\_s* and *struct*, *kevent\_qos\_s* are:

- ident

  Value used to identify the source of the event. The exact interpretation is determined by the attached filter, but often is a file descriptor.

- filter

  Identifies the kernel filter used to process this event.  The pre-defined system filters are described below.

- flags

  Actions to perform on the event.

- fflags

  Filter-specific flags.

- data

  Filter-specific data value.

- udata

  Opaque user-defined value passed through the kernel unchanged. It can optionally be part of the uniquing decision of the kevent system

In addition, *struct*, *kevent64\_s* contains:

- ext\[2\]

  This field stores extensions for the event's filter. What type of extension depends on  what type of filter is being used.

In addition, *struct*, *kevent\_qos\_s* contains:

- xflags

  Extra filter-specific flags.

- ext\[4\]

  The QoS variant provides twice as many extension values for filter-specific uses.

\----

The *flags* field can contain the following values:

- EV\_ADD

  Adds the event to the kqueue.  Re-adding an existing event will modify the parameters of the original event, and not result in a duplicate entry.  Adding an event automatically enables it, unless overridden by the EV\_DISABLE flag.

- EV\_ENABLE

  Permit **kevent,**() **kevent64**() and **kevent\_qos**() to return the event if it is triggered.

- EV\_DISABLE

  Disable the event so **kevent,**() **kevent64**() and **kevent\_qos**() will not return it.  The filter itself is not disabled.

- EV\_DELETE

  Removes the event from the kqueue.  Events which are attached to file descriptors are automatically deleted on the last close of the descriptor.

- EV\_RECEIPT

  This flag is useful for making bulk changes to a kqueue without draining any pending events. When passed as input, it forces EV\_ERROR to always be returned. When a filter is successfully added, the *data* field will be zero.

- EV\_ONESHOT

  Causes the event to return only the first occurrence of the filter being triggered.  After the user retrieves the event from the kqueue, it is deleted.

- EV\_CLEAR

  After the event is retrieved by the user, its state is reset. This is useful for filters which report state transitions instead of the current state.  Note that some filters may automatically set this flag internally.

- EV\_EOF

  Filters may set this flag to indicate filter-specific EOF condition.

- EV\_OOBAND

  Read filter on socket may set this flag to indicate the presence of out of band data on the descriptor.

- EV\_ERROR

  See RETURN VALUES below.

\----

The predefined system filters are listed below. Arguments may be passed to and from the filter via the *data,* *fflags* and optionally *xflags* fields in the *kevent,* *kevent64\_s* or *kevent\_qos\_s* structure.

- EVFILT\_READ

  Takes a file descriptor as the identifier, and returns whenever there is data available to read. The behavior of the filter is slightly different depending on the descriptor type.

  - Sockets

    Sockets which have previously been passed to **listen**() return when there is an incoming connection pending. *data* contains the size of the listen backlog.

    Other socket descriptors return when there is data to be read, subject to the SO\_RCVLOWAT value of the socket buffer. This may be overridden with a per-filter low water mark at the time the filter is added by setting the NOTE\_LOWAT flag in *fflags*, and specifying the new low water mark in *data*. The derived per filter low water mark value is, however, bounded by socket receive buffer's high and low water mark values. On return, *data* contains the number of bytes of protocol data available to read.

    The presence of EV\_OOBAND in *flags*, indicates the presence of out of band data on the socket *data* equal to the potential number of OOB bytes availble to read.

    If the read direction of the socket has shutdown, then the filter also sets EV\_EOF in *flags*, and returns the socket error (if any) in *fflags*. It is possible for EOF to be returned (indicating the connection is gone) while there is still data pending in the socket buffer.

  - Vnodes

    Returns when the file pointer is not at the end of file. *data* contains the offset from current position to end of file, and may be negative.

  - Fifos, Pipes

    Returns when there is data to read; *data* contains the number of bytes available.

    When the last writer disconnects, the filter will set EV\_EOF in *flags*. This may be cleared by passing in EV\_CLEAR, at which point the filter will resume waiting for data to become available before returning.

  - Device nodes

    Returns when there is data to read from the device; *data* contains the number of bytes available.  If the device does not support returning number of bytes, it will not allow the filter to be attached.  However, if the NOTE\_LOWAT flag is specified and the *data* field contains 1 on input, those devices will attach - but cannot be relied upon to provide an accurate count of bytes to be read on output.

- EVFILT\_EXCEPT

  Takes a descriptor as the identifier, and returns whenever one of the specified exceptional conditions has occurred on the descriptor. Conditions are specified in *fflags*. Currently, this filter can be used to monitor the arrival of out-of-band data on a socket descriptor using the filter flag NOTE\_OOB.

  If the read direction of the socket has shutdown, then the filter also sets EV\_EOF in *flags*, and returns the socket error (if any) in *fflags*.

- EVFILT\_WRITE

  Takes a file descriptor as the identifier, and returns whenever it is possible to write to the descriptor.  For sockets, pipes and fifos, *data* will contain the amount of space remaining in the write buffer. The filter will set EV\_EOF when the reader disconnects, and for the fifo case, this may be cleared by use of EV\_CLEAR. Note that this filter is not supported for vnodes.

  For sockets, the low water mark and socket error handling is identical to the EVFILT\_READ case.

- EVFILT\_AIO

  Events for this filter are not registered with **kevent64**() directly but are registered via the *aio\_sigevent* member of an asynchronous I/O request when it is scheduled via an asynchronous I/O system call such as **aio\_read**().

- EVFILT\_VNODE

  Takes a file descriptor as the identifier and the events to watch for in *fflags*, and returns when one or more of the requested events occurs on the descriptor. The events to monitor are:

  - NOTE\_DELETE

    The **unlink**() system call was called on the file referenced by the descriptor.

  - NOTE\_WRITE

    A write occurred on the file referenced by the descriptor.

  - NOTE\_EXTEND

    The file referenced by the descriptor was extended.

  - NOTE\_ATTRIB

    The file referenced by the descriptor had its attributes changed.

  - NOTE\_LINK

    The link count on the file changed.

  - NOTE\_RENAME

    The file referenced by the descriptor was renamed.

  - NOTE\_REVOKE

    Access to the file was revoked via [revoke(2)](/revoke/2.md) or the underlying fileystem was unmounted.

  - NOTE\_FUNLOCK

    The file was unlocked by calling [flock(2)](/flock/2.md) or [close(2)](/close/2.md)

  - NOTE\_LEASE\_DOWNGRADE

    A lease break to downgrade the lease to read lease is requested on the file referenced by the descriptor.

  - NOTE\_LEASE\_RELEASE

    A lease break to release the lease is requested on the file or directory referenced by the descriptor.

    On return, *fflags* contains the filter-specific flags which are associated with the triggered events seen by this filter.

- EVFILT\_PROC

  Takes the process ID to monitor as the identifier and the events to watch for in *fflags*, and returns when the process performs one or more of the requested events. If a process can normally see another process, it can attach an event to it. The events to monitor are:

  - NOTE\_EXIT

    The process has exited.

  - NOTE\_EXITSTATUS

    The process has exited and its exit status is in filter specific data. Valid only on child processes and to be used along with NOTE\_EXIT.

  - NOTE\_FORK

    The process created a child process via [fork(2)](/fork/2.md) or similar call.

  - NOTE\_EXEC

    The process executed a new process via [execve(2)](/execve/2.md) or similar call.

  - NOTE\_SIGNAL

    The process was sent a signal. Status can be checked via [waitpid(2)](/waitpid/2.md) or similar call.

  - NOTE\_REAP

    The process was reaped by the parent via [wait(2)](/wait/2.md) or similar call. Deprecated, use NOTE\_EXIT.

    On return, *fflags* contains the events which triggered the filter.

- EVFILT\_SIGNAL

  Takes the signal number to monitor as the identifier and returns when the given signal is generated for the process. This coexists with the **signal**() and **sigaction**() facilities, and has a lower precedence.  Only signals sent to the process,  not to a particular thread, will trigger the filter. The filter will record all attempts to deliver a signal to a process, even if the signal has been marked as SIG\_IGN.  Event notification happens before normal signal delivery processing. *data* returns the number of times the signal has been generated since the last call to **kevent**(). This filter automatically sets the EV\_CLEAR flag internally.

- EVFILT\_MACHPORT

  Takes the name of a mach port, or port set, in  *ident* and waits until a message is enqueued on the port or port set. When a message  is detected, but not directly received by the kevent call, the name of the specific port where the message is enqueued is returned in *data*. If *fflags* contains MACH\_RCV\_MSG, the ext\[0\] and ext\[1\] flags are assumed to contain a pointer to the buffer where the message is to be received and the size of the receive buffer, respectively.  If MACH\_RCV\_MSG is specifed, yet the buffer size in ext\[1\] is zero, The space for the buffer may be carved out of the data\_out area provided to **kevent\_qos**() if there is enough space remaining there.

- EVFILT\_TIMER

  Establishes an interval timer identified by *ident* where *data* specifies the timeout period (in milliseconds).

  *fflags* can include one of the following flags to specify a different unit:

  - NOTE\_SECONDS

    *data* is in seconds

  - NOTE\_USECONDS

    *data* is in microseconds

  - NOTE\_NSECONDS

    *data* is in nanoseconds

  - NOTE\_MACHTIME

    *data* is in Mach absolute time units

    *fflags* can also include NOTE\_ABSOLUTE, which establishes an EV\_ONESHOT timer with an absolute deadline instead of an interval. The absolute deadline is expressed in terms of [gettimeofday(2)](/gettimeofday/2.md). With NOTE\_MACHTIME, the deadline is expressed in terms of **mach\_absolute\_time**().

    The timer can be coalesced with other timers to save power. The following flags can be set in *fflags* to modify this behavior:

    - NOTE\_CRITICAL

      override default power-saving techniques to more strictly respect the leeway value

    - NOTE\_BACKGROUND

      apply more power-saving techniques to coalesce this timer with other timers

    - NOTE\_LEEWAY

      *ext\[1\]* holds user-supplied slop in deadline for timer coalescing.

      The timer will be periodic unless EV\_ONESHOT is specified. On return, *data* contains the number of times the timeout has expired since the last arming or last delivery of the timer event.

      This filter automatically sets the EV\_CLEAR flag.

\----

In the  *ext\[2\]* field of the *kevent64\_s* struture,  *ext\[0\]* is only used with the EVFILT\_MACHPORT filter. With other filters,  *ext\[0\]* is passed through **kevent64**() much like *udata*. *ext\[1\]* can always be used like *udata*. For the use of ext\[0\], see the EVFILT\_MACHPORT filter above.

## RETURN VALUES

The **kqueue**() system call creates a new kernel event queue and returns a file descriptor. If there was an error creating the kernel event queue, a value of -1 is returned and errno set.

The **kevent**(), **kevent64**() and **kevent\_qos**() system calls return the number of events placed in the *eventlist*, up to the value given by *nevents*. If an error occurs while processing an element of the *changelist* and there is enough room in the *eventlist*, then the event will be placed in the *eventlist* with EV\_ERROR set in *flags* and the system error in *data*. Otherwise, -1 will be returned, and errno will be set to indicate the error condition. If the time limit expires, then **kevent**(), **kevent64**() and **kevent\_qos**() return 0.

## ERRORS

The **kqueue**() system call fails if:

- \[ENOMEM\]

  The kernel failed to allocate enough memory for the kernel queue.

- \[EMFILE\]

  The per-process descriptor table is full.

- \[ENFILE\]

  The system file table is full.

The **kevent**() and **kevent64**() system calls fail if:

- \[EACCES\]

  The process does not have permission to register a filter.

- \[EFAULT\]

  There was an error reading or writing the *kevent* or *kevent64\_s* structure.

- \[EBADF\]

  The specified descriptor is invalid.

- \[EINTR\]

  A signal was delivered before the timeout expired and before any events were placed on the kqueue for return.

- \[EINVAL\]

  The specified time limit or filter is invalid.

- \[ENOENT\]

  The event could not be found to be modified or deleted.

- \[ENOMEM\]

  No memory was available to register the event.

- \[ESRCH\]

  The specified process to attach to does not exist.

## SEE ALSO

[aio\_error(2)](/aio_error/2.md), [aio\_read(2)](/aio_read/2.md), [aio\_return(2)](/aio_return/2.md), [read(2)](/read/2.md), [select(2)](/select/2.md), [sigaction(2)](/sigaction/2.md), [write(2)](/write/2.md), [signal(3)](/signal/3.md)

## HISTORY

The **kqueue**() and **kevent**() system calls first appeared in FreeBSD 4.1.

## AUTHORS

The **kqueue**() system and this manual page were written by Jonathan Lemon ⟨jlemon@FreeBSD.org⟩.

## BUGS

Not all filesystem types support kqueue-style notifications. And even some that do, like some remote filesystems, may only support a subset of the notification semantics described here.
