2 .\" Copyright (c) 2008 Apple Inc. All rights reserved.
4 .\" @APPLE_LICENSE_HEADER_START@
6 .\" This file contains Original Code and/or Modifications of Original Code
7 .\" as defined in and that are subject to the Apple Public Source License
8 .\" Version 2.0 (the 'License'). You may not use this file except in
9 .\" compliance with the License. Please obtain a copy of the License at
10 .\" http://www.opensource.apple.com/apsl/ and read it before using this
13 .\" The Original Code and all software distributed under the License are
14 .\" distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 .\" EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 .\" INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 .\" FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 .\" Please see the License for the specific language governing rights and
19 .\" limitations under the License.
21 .\" @APPLE_LICENSE_HEADER_END@
24 .\" Copyright (c) 2000 Jonathan Lemon
25 .\" All rights reserved.
27 .\" Redistribution and use in source and binary forms, with or without
28 .\" modification, are permitted provided that the following conditions
30 .\" 1. Redistributions of source code must retain the above copyright
31 .\" notice, this list of conditions and the following disclaimer.
32 .\" 2. Redistributions in binary form must reproduce the above copyright
33 .\" notice, this list of conditions and the following disclaimer in the
34 .\" documentation and/or other materials provided with the distribution.
36 .\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND
37 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
39 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
40 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
41 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
42 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
44 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 .\" $FreeBSD: src/lib/libc/sys/kqueue.2,v 1.32 2002/12/19 09:40:25 ru Exp $
58 .Nd kernel event notification mechanism
68 .Fn kevent "int kq" "const struct kevent *changelist" "int nchanges" "struct kevent *eventlist" "int nevents" "const struct timespec *timeout"
70 .Fn kevent64 "int kq" "const struct kevent64_s *changelist" "int nchanges" "struct kevent64_s *eventlist" "int nevents" "unsigned int flags" "const struct timespec *timeout"
71 .Fn EV_SET "&kev" ident filter flags fflags data udata
72 .Fn EV_SET64 "&kev" ident filter flags fflags data udata "ext[0]" "ext[1]"
77 provides a generic method of notifying the user when a kernel
78 event (kevent) happens or a condition holds, based on the results
79 of small pieces of kernel code termed filters.
80 A kevent is identified by an (ident, filter) pair and specifies
81 the interesting conditions to be notified about for that pair.
82 An (ident, filter) pair can only appear once in a given kqueue.
83 Subsequent attempts to register the same pair for a given kqueue
84 will result in the replacement of the conditions being watched,
87 The filter identified in a kevent is executed upon the initial
88 registration of that event in order to detect whether a preexisting
89 condition is present, and is also executed whenever an event is
90 passed to the filter for evaluation.
91 If the filter determines that the condition should be reported,
92 then the kevent is placed on the kqueue for the user to retrieve.
94 The filter is also run when the user attempts to retrieve the kevent
96 If the filter indicates that the condition that triggered
97 the event no longer holds, the kevent is removed from the kqueue and
100 Multiple events which trigger the filter do not result in multiple
101 kevents being placed on the kqueue; instead, the filter will aggregate
102 the events into a single struct kevent.
105 on a file descriptor will remove any kevents that reference the descriptor.
110 creates a new kernel event queue and returns a descriptor.
111 The queue is not inherited by a child created with
119 are used to register events with the queue, and return any pending
124 is a pointer to an array of
128 structures, as defined in
130 All changes contained in the
132 are applied before any pending events are read from the queue.
141 is a pointer to an array of
149 determines the size of
153 is a non-NULL pointer, it specifies a maximum interval to wait
154 for an event, which will be interpreted as a struct timespec. If
156 is a NULL pointer, both
160 wait indefinitely. To effect a poll, the
162 argument should be non-NULL, pointing to a zero-valued
164 structure. The same array may be used for the
171 macro is provided for ease of initializing a
173 structure. Similarly,
183 structures are defined as:
186 uintptr_t ident; /* identifier for this event */
187 int16_t filter; /* filter for event */
188 uint16_t flags; /* general flags */
189 uint32_t fflags; /* filter-specific flags */
190 intptr_t data; /* filter-specific data */
191 void *udata; /* opaque user data identifier */
196 uint64_t ident; /* identifier for this event */
197 int16_t filter; /* filter for event */
198 uint16_t flags; /* general flags */
199 uint32_t fflags; /* filter-specific flags */
200 int64_t data; /* filter-specific data */
201 uint64_t udata; /* opaque user data identifier */
202 uint64_t ext[2]; /* filter-specific extensions */
211 .Fa struct kevent64_s
213 .Bl -tag -width XXXfilter
215 Value used to identify this event.
216 The exact interpretation is determined by the attached filter,
217 but often is a file descriptor.
219 Identifies the kernel filter used to process this event. The pre-defined
220 system filters are described below.
222 Actions to perform on the event.
224 Filter-specific flags.
226 Filter-specific data value.
228 Opaque user-defined value passed through the kernel unchanged.
232 .Fa struct kevent64_s
234 .Bl -tag -width XXXfilter
236 This field stores extensions for the event's filter. What type of extension depends on
237 what type of filter is being used.
244 field can contain the following values:
245 .Bl -tag -width XXXEV_ONESHOT
247 Adds the event to the kqueue. Re-adding an existing event
248 will modify the parameters of the original event, and not result
249 in a duplicate entry. Adding an event automatically enables it,
250 unless overridden by the EV_DISABLE flag.
256 to return the event if it is triggered.
262 will not return it. The filter itself is not disabled.
264 Removes the event from the kqueue. Events which are attached to
265 file descriptors are automatically deleted on the last close of
268 This flag is useful for making bulk changes to a kqueue without draining any
269 pending events. When passed as input, it forces EV_ERROR to always be returned.
270 When a filter is successfully added, the
274 Causes the event to return only the first occurrence of the filter
275 being triggered. After the user retrieves the event from the kqueue,
278 After the event is retrieved by the user, its state is reset.
279 This is useful for filters which report state transitions
280 instead of the current state. Note that some filters may automatically
281 set this flag internally.
283 Filters may set this flag to indicate filter-specific EOF condition.
292 The predefined system filters are listed below.
293 Arguments may be passed to and from the filter via the
302 .Bl -tag -width EVFILT_MACHPORT
304 Takes a file descriptor as the identifier, and returns whenever
305 there is data available to read.
306 The behavior of the filter is slightly different depending
307 on the descriptor type.
311 Sockets which have previously been passed to
313 return when there is an incoming connection pending.
315 contains the size of the listen backlog.
317 Other socket descriptors return when there is data to be read,
320 value of the socket buffer.
321 This may be overridden with a per-filter low water mark at the
322 time the filter is added by setting the
326 and specifying the new low water mark in
330 contains the number of bytes of protocol data available to read.
332 If the read direction of the socket has shutdown, then the filter
335 and returns the socket error (if any) in
337 It is possible for EOF to be returned (indicating the connection is gone)
338 while there is still data pending in the socket buffer.
340 Returns when the file pointer is not at the end of file.
342 contains the offset from current position to end of file,
345 Returns when the there is data to read;
347 contains the number of bytes available.
349 When the last writer disconnects, the filter will set EV_EOF in
351 This may be cleared by passing in EV_CLEAR, at which point the
352 filter will resume waiting for data to become available before
356 Takes a file descriptor as the identifier, and returns whenever
357 it is possible to write to the descriptor. For sockets, pipes
360 will contain the amount of space remaining in the write buffer.
361 The filter will set EV_EOF when the reader disconnects, and for
362 the fifo case, this may be cleared by use of EV_CLEAR.
363 Note that this filter is not supported for vnodes.
365 For sockets, the low water mark and socket error handling is
366 identical to the EVFILT_READ case.
368 This filter is currently unsupported.
369 .\"The sigevent portion of the AIO request is filled in, with
370 .\".Va sigev_notify_kqueue
371 .\"containing the descriptor of the kqueue that the event should
374 .\"containing the udata value, and
376 .\"set to SIGEV_KEVENT.
379 .\"system call is made, the event will be registered
380 .\"with the specified kqueue, and the
382 .\"argument set to the
387 .\"The filter returns under the same conditions as aio_error.
389 .\"Alternatively, a kevent structure may be initialized, with
391 .\"containing the descriptor of the kqueue, and the
392 .\"address of the kevent structure placed in the
393 .\".Va aio_lio_opcode
394 .\"field of the AIO request. However, this approach will not work on
395 .\"architectures with 64-bit pointers, and should be considered deprecated.
397 Takes a file descriptor as the identifier and the events to watch for in
399 and returns when one or more of the requested events occurs on the descriptor.
400 The events to monitor are:
401 .Bl -tag -width XXNOTE_RENAME
406 was called on the file referenced by the descriptor.
408 A write occurred on the file referenced by the descriptor.
410 The file referenced by the descriptor was extended.
412 The file referenced by the descriptor had its attributes changed.
414 The link count on the file changed.
416 The file referenced by the descriptor was renamed.
418 Access to the file was revoked via
420 or the underlying fileystem was unmounted.
425 contains the events which triggered the filter.
427 Takes the process ID to monitor as the identifier and the events to watch for
430 and returns when the process performs one or more of the requested events.
431 If a process can normally see another process, it can attach an event to it.
432 The events to monitor are:
433 .Bl -tag -width NOTE_SIGNAL
435 The process has exited.
437 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.
439 The process created a child process via
443 The process executed a new process via
447 The process was sent a signal. Status can be checked via
451 The process was reaped by the parent via
458 contains the events which triggered the filter.
460 Takes the signal number to monitor as the identifier and returns
461 when the given signal is generated for the process.
462 This coexists with the
466 facilities, and has a lower precedence. Only signals sent to the process,
467 not to a particular thread, will trigger the filter. The filter will record
468 all attempts to deliver a signal to a process, even if the signal has
469 been marked as SIG_IGN. Event notification happens before normal
470 signal delivery processing.
472 returns the number of times the signal has been generated since the last call to
474 This filter automatically sets the EV_CLEAR flag internally.
476 Takes the name of a mach port, or port set, in
478 and waits until a message is received on the port or port set. When a message
479 is recieved, the size of the message is returned in
483 is set to MACH_RCV_MSG, a pointer to the message is returned in ext[0].
485 Establishes an interval timer with the data
490 specifies the timeout period and
492 can be set to one of the following:
493 .Bl -tag -width NOTE_ABSOLUTE
497 data is in microseconds
499 data is in nanoseconds
501 data is an absolute timeout
504 If fflags is not set, the default is milliseconds. The timer will be periodic unless EV_ONESHOT is specified.
507 contains the number of times the timeout has expired since the last call to
511 This filter automatically sets the EV_CLEAR flag internally.
522 is only used with the EVFILT_MACHPORT filter.
530 can always be used like
532 For the use of ext[0], see the EVFILT_MACHPORT filter above.
537 creates a new kernel event queue and returns a file descriptor.
538 If there was an error creating the kernel event queue, a value of -1 is
539 returned and errno set.
546 return the number of events placed in the
548 up to the value given by
550 If an error occurs while processing an element of the
552 and there is enough room in the
554 then the event will be placed in the
560 and the system error in
564 will be returned, and
566 will be set to indicate the error condition.
567 If the time limit expires, then
575 system call fails if:
578 The kernel failed to allocate enough memory for the kernel queue.
580 The per-process descriptor table is full.
582 The system file table is full.
589 system calls fail if:
592 The process does not have permission to register a filter.
594 There was an error reading or writing the
600 The specified descriptor is invalid.
602 A signal was delivered before the timeout expired and before any
603 events were placed on the kqueue for return.
605 The specified time limit or filter is invalid.
607 The event could not be found to be modified or deleted.
609 No memory was available to register the event.
611 The specified process to attach to does not exist.
627 system calls first appeared in
632 system and this manual page were written by
633 .An Jonathan Lemon Aq jlemon@FreeBSD.org .
635 Not all filesystem types support kqueue-style notifications.
636 And even some that do, like some remote filesystems, may only
637 support a subset of the notification semantics described