1 .\" Copyright (c) 2000 Jonathan Lemon
2 .\" All rights reserved.
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
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.
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
25 .\" $FreeBSD: src/lib/libc/sys/kqueue.2,v 1.1.2.16 2002/07/02 21:05:08 mp Exp $
26 .\" $DragonFly: src/lib/libc/sys/kqueue.2,v 1.3 2006/05/26 19:39:37 swildner Exp $
34 .Nd kernel event notification mechanism
44 .Fn kevent "int kq" "const struct kevent *changelist" "int nchanges" "struct kevent *eventlist" "int nevents" "const struct timespec *timeout"
45 .Fn EV_SET "&kev" ident filter flags fflags data udata
48 provides a generic method of notifying the user when an event
49 happens or a condition holds, based on the results of small
50 pieces of kernel code termed filters.
51 A kevent is identified by the (ident, filter) pair; there may only
52 be one unique kevent per kqueue.
54 The filter is executed upon the initial registration of a kevent
55 in order to detect whether a preexisting condition is present, and is also
56 executed whenever an event is passed to the filter for evaluation.
57 If the filter determines that the condition should be reported,
58 then the kevent is placed on the kqueue for the user to retrieve.
60 The filter is also run when the user attempts to retrieve the kevent
62 If the filter indicates that the condition that triggered
63 the event no longer holds, the kevent is removed from the kqueue and
66 Multiple events which trigger the filter do not result in multiple
67 kevents being placed on the kqueue; instead, the filter will aggregate
68 the events into a single struct kevent.
71 on a file descriptor will remove any kevents that reference the descriptor.
74 creates a new kernel event queue and returns a descriptor.
75 The queue is not inherited by a child created with
81 flag, then the descriptor table is shared,
82 which will allow sharing of the kqueue between two processes.
85 is used to register events with the queue, and return any pending
88 is a pointer to an array of
90 structures, as defined in
92 All changes contained in the
94 are applied before any pending events are read from the queue.
99 is a pointer to an array of kevent structures.
101 determines the size of
105 is a non-NULL pointer, it specifies a maximum interval to wait
106 for an event, which will be interpreted as a struct timespec. If
110 waits indefinitely. To effect a poll, the
112 argument should be non-NULL, pointing to a zero-valued
114 structure. The same array may be used for the
120 is a macro which is provided for ease of initializing a
125 structure is defined as:
128 uintptr_t ident; /* identifier for this event */
129 short filter; /* filter for event */
130 u_short flags; /* action flags for kqueue */
131 u_int fflags; /* filter flag value */
132 intptr_t data; /* filter data value */
133 void *udata; /* opaque user data identifier */
140 .Bl -tag -width XXXfilter
142 Value used to identify this event.
143 The exact interpretation is determined by the attached filter,
144 but often is a file descriptor.
146 Identifies the kernel filter used to process this event. The pre-defined
147 system filters are described below.
149 Actions to perform on the event.
151 Filter-specific flags.
153 Filter-specific data value.
155 Opaque user-defined value passed through the kernel unchanged.
160 field can contain the following values:
161 .Bl -tag -width XXXEV_ONESHOT
163 Adds the event to the kqueue. Re-adding an existing event
164 will modify the parameters of the original event, and not result
165 in a duplicate entry. Adding an event automatically enables it,
166 unless overridden by the EV_DISABLE flag.
170 to return the event if it is triggered.
174 will not return it. The filter itself is not disabled.
176 Removes the event from the kqueue. Events which are attached to
177 file descriptors are automatically deleted on the last close of
180 Causes the event to return only the first occurrence of the filter
181 being triggered. After the user retrieves the event from the kqueue,
184 After the event is retrieved by the user, its state is reset.
185 This is useful for filters which report state transitions
186 instead of the current state. Note that some filters may automatically
187 set this flag internally.
189 Filters may set this flag to indicate filter-specific EOF condition.
196 The predefined system filters are listed below.
197 Arguments may be passed to and from the filter via the
201 fields in the kevent structure.
202 .Bl -tag -width EVFILT_SIGNAL
204 Takes a descriptor as the identifier, and returns whenever
205 there is data available to read.
206 The behavior of the filter is slightly different depending
207 on the descriptor type.
211 Sockets which have previously been passed to
213 return when there is an incoming connection pending.
215 contains the size of the listen backlog.
217 Other socket descriptors return when there is data to be read,
220 value of the socket buffer.
221 This may be overridden with a per-filter low water mark at the
222 time the filter is added by setting the
226 and specifying the new low water mark in
230 contains the number of bytes in the socket buffer.
232 If the read direction of the socket has shutdown, then the filter
235 and returns the socket error (if any) in
237 It is possible for EOF to be returned (indicating the connection is gone)
238 while there is still data pending in the socket buffer.
240 Returns when the file pointer is not at the end of file.
242 contains the offset from current position to end of file,
245 Returns when the there is data to read;
247 contains the number of bytes available.
249 When the last writer disconnects, the filter will set EV_EOF in
251 This may be cleared by passing in EV_CLEAR, at which point the
252 filter will resume waiting for data to become available before
256 Takes a descriptor as the identifier, and returns whenever
257 it is possible to write to the descriptor. For sockets, pipes
260 will contain the amount of space remaining in the write buffer.
261 The filter will set EV_EOF when the reader disconnects, and for
262 the fifo case, this may be cleared by use of EV_CLEAR.
263 Note that this filter is not supported for vnodes.
265 For sockets, the low water mark and socket error handling is
266 identical to the EVFILT_READ case.
268 The sigevent portion of the AIO request is filled in, with
269 .Va sigev_notify_kqueue
270 containing the descriptor of the kqueue that the event should
273 containing the udata value, and
276 When the aio_* function is called, the event will be registered
277 with the specified kqueue, and the
281 returned by the aio_* function.
282 The filter returns under the same conditions as aio_error.
284 Alternatively, a kevent structure may be initialized, with
286 containing the descriptor of the kqueue, and the
287 address of the kevent structure placed in the
289 field of the AIO request. However, this approach will not work on
290 architectures with 64-bit pointers, and should be considered depreciated.
292 Takes a file descriptor as the identifier and the events to watch for in
294 and returns when one or more of the requested events occurs on the descriptor.
295 The events to monitor are:
296 .Bl -tag -width XXNOTE_RENAME
299 was called on the file referenced by the descriptor.
301 A write occurred on the file referenced by the descriptor.
303 The file referenced by the descriptor was extended.
305 The file referenced by the descriptor had its attributes changed.
307 The link count on the file changed.
309 The file referenced by the descriptor was renamed.
311 Access to the file was revoked via
313 or the underlying fileystem was unmounted.
318 contains the events which triggered the filter.
320 Takes the process ID to monitor as the identifier and the events to watch for
323 and returns when the process performs one or more of the requested events.
324 If a process can normally see another process, it can attach an event to it.
325 The events to monitor are:
326 .Bl -tag -width XXNOTE_TRACKERR
328 The process has exited.
330 The process has called
333 The process has executed a new process via
337 Follow a process across
339 calls. The parent process will return with NOTE_TRACK set in the
341 field, while the child process will return with NOTE_CHILD set in
343 and the parent PID in
346 This flag is returned if the system was unable to attach an event to
347 the child process, usually due to resource limitations.
352 contains the events which triggered the filter.
354 Takes the signal number to monitor as the identifier and returns
355 when the given signal is delivered to the process.
356 This coexists with the
360 facilities, and has a lower precedence. The filter will record
361 all attempts to deliver a signal to a process, even if the signal has
362 been marked as SIG_IGN. Event notification happens after normal
363 signal delivery processing.
365 returns the number of times the signal has occurred since the last call to
367 This filter automatically sets the EV_CLEAR flag internally.
369 Establishes an arbitrary timer identified by
373 specifies the timeout period in milliseconds.
374 The timer will be periodic unless EV_ONESHOT is specified.
377 contains the number of times the timeout has expired since the last call to
379 This filter automatically sets the EV_CLEAR flag internally.
383 creates a new kernel event queue and returns a file descriptor.
384 If there was an error creating the kernel event queue, a value of -1 is
385 returned and errno set.
388 returns the number of events placed in the
390 up to the value given by
392 If an error occurs while processing an element of the
394 and there is enough room in the
396 then the event will be placed in the
402 and the system error in
406 will be returned, and
408 will be set to indicate the error condition.
409 If the time limit expires, then
418 The kernel failed to allocate enough memory for the kernel queue.
420 The per-process descriptor table is full.
422 The system file table is full.
430 The process does not have permission to register a filter.
432 There was an error reading or writing the
436 The specified descriptor is invalid.
438 A signal was delivered before the timeout expired and before any
439 events were placed on the kqueue for return.
441 The specified time limit or filter is invalid.
443 The event could not be found to be modified or deleted.
445 No memory was available to register the event.
447 The specified process to attach to does not exist.
464 functions first appeared in
469 system and this manual page were written by
470 .An Jonathan Lemon Aq jlemon@FreeBSD.org .
472 It is currently not possible to watch a
474 that resides on anything but