1 .\" $NetBSD: kqueue.2,v 1.25 2009/10/24 17:49:58 christos Exp $
3 .\" Copyright (c) 2000 Jonathan Lemon
4 .\" All rights reserved.
6 .\" Copyright (c) 2001, 2002, 2003 The NetBSD Foundation, Inc.
7 .\" All rights reserved.
9 .\" Portions of this documentation is derived from text contributed by
12 .\" Redistribution and use in source and binary forms, with or without
13 .\" modification, are permitted provided that the following conditions
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.
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
33 .\" $FreeBSD: src/lib/libc/sys/kqueue.2,v 1.22 2001/06/27 19:55:57 dd Exp $
41 .Nd kernel event notification mechanism
50 .Fn kevent "int kq" "const struct kevent *changelist" "size_t nchanges" "struct kevent *eventlist" "size_t nevents" "const struct timespec *timeout"
51 .Fn EV_SET "\*[Am]kev" ident filter flags fflags data udata
54 provides a generic method of notifying the user when an event
55 happens or a condition holds, based on the results of small
56 pieces of kernel code termed filters.
57 A kevent is identified by the (ident, filter) pair; there may only
58 be one unique kevent per kqueue.
60 The filter is executed upon the initial registration of a kevent
61 in order to detect whether a preexisting condition is present, and is also
62 executed whenever an event is passed to the filter for evaluation.
63 If the filter determines that the condition should be reported,
64 then the kevent is placed on the kqueue for the user to retrieve.
66 The filter is also run when the user attempts to retrieve the kevent
68 If the filter indicates that the condition that triggered
69 the event no longer holds, the kevent is removed from the kqueue and
72 Multiple events which trigger the filter do not result in multiple
73 kevents being placed on the kqueue; instead, the filter will aggregate
74 the events into a single struct kevent.
77 on a file descriptor will remove any kevents that reference the descriptor.
80 creates a new kernel event queue and returns a descriptor.
81 The queue is not inherited by a child created with
85 .\" is called without the
87 .\" flag, then the descriptor table is shared,
88 .\" which will allow sharing of the kqueue between two processes.
91 is used to register events with the queue, and return any pending
94 is a pointer to an array of
96 structures, as defined in
98 All changes contained in the
100 are applied before any pending events are read from the queue.
105 is a pointer to an array of kevent structures.
107 determines the size of
113 pointer, it specifies a maximum interval to wait
114 for an event, which will be interpreted as a struct timespec.
122 To effect a poll, the
125 .No non- Ns Dv NULL ,
126 pointing to a zero-valued
129 The same array may be used for the
135 is a macro which is provided for ease of initializing a
140 structure is defined as:
143 uintptr_t ident; /* identifier for this event */
144 uint32_t filter; /* filter for event */
145 uint32_t flags; /* action flags for kqueue */
146 uint32_t fflags; /* filter flag value */
147 int64_t data; /* filter data value */
148 intptr_t udata; /* opaque user data identifier */
155 .Bl -tag -width XXXfilter -offset indent
157 Value used to identify this event.
158 The exact interpretation is determined by the attached filter,
159 but often is a file descriptor.
161 Identifies the kernel filter used to process this event.
162 There are pre-defined system filters (which are described below), and
163 other filters may be added by kernel subsystems as necessary.
165 Actions to perform on the event.
167 Filter-specific flags.
169 Filter-specific data value.
171 Opaque user-defined value passed through the kernel unchanged.
176 field can contain the following values:
177 .Bl -tag -width XXXEV_ONESHOT -offset indent
179 Adds the event to the kqueue.
180 Re-adding an existing event will modify the parameters of the original
181 event, and not result in a duplicate entry.
182 Adding an event automatically enables it,
183 unless overridden by the EV_DISABLE flag.
187 to return the event if it is triggered.
192 The filter itself is not disabled.
194 Removes the event from the kqueue.
195 Events which are attached to file descriptors are automatically deleted
196 on the last close of the descriptor.
198 Causes the event to return only the first occurrence of the filter
200 After the user retrieves the event from the kqueue, it is deleted.
202 After the event is retrieved by the user, its state is reset.
203 This is useful for filters which report state transitions
204 instead of the current state.
205 Note that some filters may automatically set this flag internally.
207 Filters may set this flag to indicate filter-specific EOF condition.
214 Filters are identified by a number.
215 There are two types of filters; pre-defined filters which
216 are described below, and third-party filters that may be added with
217 .Xr kfilter_register 9
218 by kernel sub-systems, third-party device drivers, or loadable
221 As a third-party filter is referenced by a well-known name instead
222 of a statically assigned number, two
224 are supported on the file descriptor returned by
226 to map a filter name to a filter number, and vice-versa (passing
227 arguments in a structure described below):
228 .Bl -tag -width KFILTER_BYFILTER -offset indent
245 The following structure is used to pass arguments in and out of the
247 .Bd -literal -offset indent
248 struct kfilter_mapping {
249 char *name; /* name to lookup or return */
250 size_t len; /* length of name */
251 uint32_t filter; /* filter to lookup or return */
255 Arguments may be passed to and from the filter via the
259 fields in the kevent structure.
261 The predefined system filters are:
262 .Bl -tag -width EVFILT_SIGNAL
264 Takes a descriptor as the identifier, and returns whenever
265 there is data available to read.
266 The behavior of the filter is slightly different depending
267 on the descriptor type.
271 Sockets which have previously been passed to
273 return when there is an incoming connection pending.
275 contains the size of the listen backlog (i.e., the number of
276 connections ready to be accepted with
279 Other socket descriptors return when there is data to be read,
282 value of the socket buffer.
283 This may be overridden with a per-filter low water mark at the
284 time the filter is added by setting the
288 and specifying the new low water mark in
292 contains the number of bytes in the socket buffer.
294 If the read direction of the socket has shutdown, then the filter
297 and returns the socket error (if any) in
299 It is possible for EOF to be returned (indicating the connection is gone)
300 while there is still data pending in the socket buffer.
302 Returns when the file pointer is not at the end of file.
304 contains the offset from current position to end of file,
307 Returns when there is data to read;
309 contains the number of bytes available.
311 When the last writer disconnects, the filter will set EV_EOF in
313 This may be cleared by passing in EV_CLEAR, at which point the
314 filter will resume waiting for data to become available before
318 Takes a descriptor as the identifier, and returns whenever
319 it is possible to write to the descriptor.
320 For sockets, pipes, fifos, and ttys,
322 will contain the amount of space remaining in the write buffer.
323 The filter will set EV_EOF when the reader disconnects, and for
324 the fifo case, this may be cleared by use of EV_CLEAR.
325 Note that this filter is not supported for vnodes.
327 For sockets, the low water mark and socket error handling is
328 identical to the EVFILT_READ case.
330 This is not implemented in
333 The sigevent portion of the AIO request is filled in, with
334 .Va sigev_notify_kqueue
335 containing the descriptor of the kqueue that the event should
338 containing the udata value, and
341 When the aio_* function is called, the event will be registered
342 with the specified kqueue, and the
346 returned by the aio_* function.
347 The filter returns under the same conditions as aio_error.
349 Alternatively, a kevent structure may be initialized, with
351 containing the descriptor of the kqueue, and the
352 address of the kevent structure placed in the
354 field of the AIO request.
355 However, this approach will not work on
356 architectures with 64-bit pointers, and should be considered deprecated.
359 Takes a file descriptor as the identifier and the events to watch for in
361 and returns when one or more of the requested events occurs on the descriptor.
362 The events to monitor are:
363 .Bl -tag -width XXNOTE_RENAME
366 was called on the file referenced by the descriptor.
368 A write occurred on the file referenced by the descriptor.
370 The file referenced by the descriptor was extended.
372 The file referenced by the descriptor had its attributes changed.
374 The link count on the file changed.
376 The file referenced by the descriptor was renamed.
378 Access to the file was revoked via
380 or the underlying fileystem was unmounted.
385 contains the events which triggered the filter.
387 Takes the process ID to monitor as the identifier and the events to watch for
390 and returns when the process performs one or more of the requested events.
391 If a process can normally see another process, it can attach an event to it.
392 The events to monitor are:
393 .Bl -tag -width XXNOTE_TRACKERR
395 The process has exited.
397 The process has called
400 The process has executed a new process via
404 Follow a process across
407 The parent process will return with NOTE_TRACK set in the
409 field, while the child process will return with NOTE_CHILD set in
411 and the parent PID in
414 This flag is returned if the system was unable to attach an event to
415 the child process, usually due to resource limitations.
420 contains the events which triggered the filter.
422 Takes the signal number to monitor as the identifier and returns
423 when the given signal is delivered to the current process.
424 This coexists with the
428 facilities, and has a lower precedence.
429 The filter will record
430 all attempts to deliver a signal to a process, even if the signal has
431 been marked as SIG_IGN.
432 Event notification happens after normal signal delivery processing.
434 returns the number of times the signal has occurred since the last call to
436 This filter automatically sets the EV_CLEAR flag internally.
438 Establishes an arbitrary timer identified by
442 specifies the timeout period in milliseconds.
443 The timer will be periodic unless EV_ONESHOT is specified.
446 contains the number of times the timeout has expired since the last call to
448 This filter automatically sets the EV_CLEAR flag internally.
452 creates a new kernel event queue and returns a file descriptor.
453 If there was an error creating the kernel event queue, a value of \-1 is
454 returned and errno set.
457 returns the number of events placed in the
459 up to the value given by
461 If an error occurs while processing an element of the
463 and there is enough room in the
465 then the event will be placed in the
471 and the system error in
475 will be returned, and
477 will be set to indicate the error condition.
478 If the time limit expires, then
482 The following example program monitors a file (provided to it as the first
483 argument) and prints information about some common events it receives
485 .Bd -literal -offset indent
486 #include \*[Lt]sys/types.h\*[Gt]
487 #include \*[Lt]sys/event.h\*[Gt]
488 #include \*[Lt]sys/time.h\*[Gt]
489 #include \*[Lt]stdio.h\*[Gt]
490 #include \*[Lt]unistd.h\*[Gt]
491 #include \*[Lt]stdlib.h\*[Gt]
492 #include \*[Lt]fcntl.h\*[Gt]
493 #include \*[Lt]err.h\*[Gt]
496 main(int argc, char *argv[])
500 static const struct timespec tout = { 1, 0 };
502 if ((fd = open(argv[1], O_RDONLY)) == -1)
503 err(1, "Cannot open `%s'", argv[1]);
505 if ((kq = kqueue()) == -1)
506 err(1, "Cannot create kqueue");
508 EV_SET(\*[Am]ev, fd, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR,
509 NOTE_DELETE|NOTE_WRITE|NOTE_EXTEND|NOTE_ATTRIB|NOTE_LINK|
510 NOTE_RENAME|NOTE_REVOKE, 0, 0);
511 if (kevent(kq, \*[Am]ch, 1, NULL, 0, \*[Am]tout) == -1)
514 nev = kevent(kq, NULL, 0, \*[Am]ev, 1, \*[Am]tout);
519 if (ev.fflags \*[Am] NOTE_DELETE) {
521 ev.fflags \*[Am]= ~NOTE_DELETE;
523 if (ev.fflags \*[Am] NOTE_WRITE) {
525 ev.fflags \*[Am]= ~NOTE_WRITE;
527 if (ev.fflags \*[Am] NOTE_EXTEND) {
529 ev.fflags \*[Am]= ~NOTE_EXTEND;
531 if (ev.fflags \*[Am] NOTE_ATTRIB) {
532 printf("chmod/chown/utimes ");
533 ev.fflags \*[Am]= ~NOTE_ATTRIB;
535 if (ev.fflags \*[Am] NOTE_LINK) {
536 printf("hardlinked ");
537 ev.fflags \*[Am]= ~NOTE_LINK;
539 if (ev.fflags \*[Am] NOTE_RENAME) {
541 ev.fflags \*[Am]= ~NOTE_RENAME;
543 if (ev.fflags \*[Am] NOTE_REVOKE) {
545 ev.fflags \*[Am]= ~NOTE_REVOKE;
549 warnx("unknown event 0x%x\\n", ev.fflags);
559 The per-process descriptor table is full.
561 The system file table is full.
563 The kernel failed to allocate enough memory for the kernel queue.
571 The process does not have permission to register a filter.
573 The specified descriptor is invalid.
575 There was an error reading or writing the
579 A signal was delivered before the timeout expired and before any
580 events were placed on the kqueue for return.
582 The specified time limit or filter is invalid.
584 The event could not be found to be modified or deleted.
586 No memory was available to register the event.
588 The specified process to attach to does not exist.
591 .\" .Xr aio_error 2 ,
593 .\" .Xr aio_return 2 ,
601 .Xr kfilter_register 9 ,
608 functions first appeared in