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 $
33 .Nd kernel event notification mechanism
43 .Fn kevent "int kq" "const struct kevent *changelist" "int nchanges" "struct kevent *eventlist" "int nevents" "const struct timespec *timeout"
44 .Fn EV_SET "&kev" ident filter flags fflags data udata
47 provides a generic method of notifying the user when an event
48 happens or a condition holds, based on the results of small
49 pieces of kernel code termed filters.
50 A kevent is identified by the (ident, filter) pair; there may only
51 be one unique kevent per kqueue.
53 The filter is executed upon the initial registration of a kevent
54 in order to detect whether a preexisting condition is present, and is also
55 executed whenever an event is passed to the filter for evaluation.
56 If the filter determines that the condition should be reported,
57 then the kevent is placed on the kqueue for the user to retrieve.
59 The filter is also run when the user attempts to retrieve the kevent
61 If the filter indicates that the condition that triggered
62 the event no longer holds, the kevent is removed from the kqueue and
65 Multiple events which trigger the filter do not result in multiple
66 kevents being placed on the kqueue; instead, the filter will aggregate
67 the events into a single struct kevent.
70 on a file descriptor will remove any kevents that reference the descriptor.
73 creates a new kernel event queue and returns a descriptor.
74 The queue is not inherited by a child created with
80 flag, then the descriptor table is shared,
81 which will allow sharing of the kqueue between two processes.
84 is used to register events with the queue, and return any pending
87 is a pointer to an array of
89 structures, as defined in
91 All changes contained in the
93 are applied before any pending events are read from the queue.
98 is a pointer to an array of kevent structures.
100 determines the size of
104 is a non-NULL pointer, it specifies a maximum interval to wait
105 for an event, which will be interpreted as a struct timespec.
111 To effect a poll, the
113 argument should be non-NULL, pointing to a zero-valued
116 The same array may be used for the
122 is a macro which is provided for ease of initializing a
127 structure is defined as:
130 uintptr_t ident; /* identifier for this event */
131 short filter; /* filter for event */
132 u_short flags; /* action flags for kqueue */
133 u_int fflags; /* filter flag value */
134 intptr_t data; /* filter data value */
135 void *udata; /* opaque user data identifier */
142 .Bl -tag -width XXXfilter
144 Value used to identify this event.
145 The exact interpretation is determined by the attached filter,
146 but often is a file descriptor.
148 Identifies the kernel filter used to process this event.
149 The pre-defined system filters are described below.
151 Actions to perform on the event.
153 Filter-specific flags.
155 Filter-specific data value.
157 Opaque user-defined value passed through the kernel unchanged.
162 field can contain the following values:
163 .Bl -tag -width ".Dv EV_DISPATCH"
165 Adds the event to the kqueue.
166 Re-adding an existing event will modify the parameters of the original
167 event, and not result in a duplicate entry.
168 Adding an event automatically enables it, unless overridden by the
174 to return the event if it is triggered.
179 The filter itself is not disabled.
181 Disable the event source immediately after delivery of an event.
186 Removes the event from the kqueue.
187 Events which are attached to file descriptors are automatically
188 deleted on the last close of the descriptor.
190 This flag is useful for making bulk changes to a kqueue without draining
192 When passed as input, it forces
194 to always be returned.
195 When a filter is successfully added the
199 Causes the event to return only the first occurrence of the filter
201 After the user retrieves the event from the kqueue, it is deleted.
203 After the event is retrieved by the user, its state is reset.
204 This is useful for filters which report state transitions
205 instead of the current state.
206 Note that some filters may automatically set this flag internally.
208 Filters may set this flag to indicate filter-specific EOF condition.
210 Filters may set this flag in addition to
212 to indicate that there is no more data pending in the buffer.
219 The predefined system filters are listed below.
220 Arguments may be passed to and from the filter via the
224 fields in the kevent structure.
225 .Bl -tag -width ".Dv EVFILT_SIGNAL"
227 Takes a descriptor as the identifier, and returns whenever
228 there is data available to read.
229 The behavior of the filter is slightly different depending
230 on the descriptor type.
233 Sockets which have previously been passed to
235 return when there is an incoming connection pending.
237 contains the size of the listen backlog.
239 Other socket descriptors return when there is data to be read,
242 value of the socket buffer.
243 This may be overridden with a per-filter low water mark at the
244 time the filter is added by setting the
248 and specifying the new low water mark in
252 contains the number of bytes in the socket buffer.
254 If the read direction of the socket has shutdown, then the filter also sets
258 and returns the socket error (if any) in
260 It is possible for EOF to be returned (indicating the connection is gone)
261 while there is still data pending in the socket buffer.
263 Returns when the file pointer is not at the end of file.
265 contains the offset from current position to end of file,
268 Returns when the there is data to read;
270 contains the number of bytes available.
272 When the last writer disconnects, the filter will set
276 This will be cleared by the filter when a new writer connects,
277 at which point the filter will resume waiting for data to become
278 available before returning.
281 Takes a descriptor as the identifier, and returns whenever
282 it is possible to write to the descriptor.
283 For sockets, pipes and fifos,
285 will contain the amount of space remaining in the write buffer.
288 when the reader disconnects, and for the fifo case, this will be cleared
289 when a new reader connects.
290 Note that this filter is not supported for vnodes.
292 For sockets, the low water mark and socket error handling is
297 Takes a descriptor as the identifier, and returns whenever one of the
298 specified exceptional conditions has occurred on the descriptor.
299 Conditions are specified in
301 Currently, a filter can monitor the reception of out-of-band data with
304 The sigevent portion of the AIO request is filled in, with
305 .Fa sigev_notify_kqueue
306 containing the descriptor of the kqueue that the event should
309 containing the udata value, and
313 When the aio_* function is called, the event will be registered
314 with the specified kqueue, and the
318 returned by the aio_* function.
319 The filter returns under the same conditions as
322 Alternatively, a kevent structure may be initialized, with
324 containing the descriptor of the kqueue, and the
325 address of the kevent structure placed in the
327 field of the AIO request.
328 However, this approach will not work on architectures with 64-bit
329 pointers, and should be considered deprecated.
331 Takes a file descriptor as the identifier and the events to watch for in
333 and returns when one or more of the requested events occurs on the descriptor.
334 The events to monitor are:
335 .Bl -tag -width ".Dv NOTE_RENAME"
338 was called on the file referenced by the descriptor.
340 A write occurred on the file referenced by the descriptor.
342 The file referenced by the descriptor was extended.
344 The file referenced by the descriptor had its attributes changed.
346 The link count on the file changed.
348 The file referenced by the descriptor was renamed.
350 Access to the file was revoked via
352 or the underlying fileystem was unmounted.
357 contains the events which triggered the filter.
359 Takes the process ID to monitor as the identifier and the events to watch for
362 and returns when the process performs one or more of the requested events.
363 If a process can normally see another process, it can attach an event to it.
364 The events to monitor are:
365 .Bl -tag -width ".Dv NOTE_TRACKERR"
367 The process has exited.
369 The process has called
372 The process has executed a new process via
376 Follow a process across
379 The parent process will return with
383 field, while the child process will return with
387 and the parent PID in
390 This flag is returned if the system was unable to attach an event to
391 the child process, usually due to resource limitations.
396 contains the events which triggered the filter.
398 Takes the signal number to monitor as the identifier and returns
399 when the given signal is delivered to the process.
400 This coexists with the
404 facilities, and has a lower precedence.
405 The filter will record all attempts to deliver a signal to a process,
406 even if the signal has been marked as
408 or has been masked by
410 Event notification happens after normal signal delivery processing.
412 returns the number of times the signal has occurred since the last call to
414 This filter automatically sets the
418 Establishes an arbitrary timer identified by
422 specifies the timeout period in milliseconds.
423 The timer will be periodic unless
428 contains the number of times the timeout has expired since the last call to
430 This filter automatically sets the
434 Establishes a file system monitor identified by
436 Currently it only monitors file system mount and unmount actions.
440 creates a new kernel event queue and returns a file descriptor.
441 If there was an error creating the kernel event queue, a value of -1 is
447 returns the number of events placed in the
449 up to the value given by
451 If an error occurs while processing an element of the
453 and there is enough room in the
455 then the event will be placed in the
461 and the system error in
465 will be returned, and
467 will be set to indicate the error condition.
468 If the time limit expires, then
477 The kernel failed to allocate enough memory for the kernel queue.
479 The per-process descriptor table is full.
481 The system file table is full.
489 The process does not have permission to register a filter.
491 There was an error reading or writing the
495 The specified descriptor is invalid.
497 A signal was delivered before the timeout expired and before any
498 events were placed on the kqueue for return.
500 The specified time limit or filter is invalid.
502 The event could not be found to be modified or deleted.
504 No memory was available to register the event.
506 The specified process to attach to does not exist.
524 functions first appeared in
529 system and this manual page were written by
530 .An Jonathan Lemon Aq Mt jlemon@FreeBSD.org .
532 Currently it is only possible to watch a