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
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
49 provides a generic method of notifying the user when an event
50 happens or a condition holds, based on the results of small
51 pieces of kernel code termed filters.
52 A kevent is identified by the (ident, filter) pair; there may only
53 be one unique kevent per kqueue.
55 The filter is executed upon the initial registration of a kevent
56 in order to detect whether a preexisting condition is present, and is also
57 executed whenever an event is passed to the filter for evaluation.
58 If the filter determines that the condition should be reported,
59 then the kevent is placed on the kqueue for the user to retrieve.
61 The filter is also run when the user attempts to retrieve the kevent
63 If the filter indicates that the condition that triggered
64 the event no longer holds, the kevent is removed from the kqueue and
67 Multiple events which trigger the filter do not result in multiple
68 kevents being placed on the kqueue; instead, the filter will aggregate
69 the events into a single struct kevent.
72 on a file descriptor will remove any kevents that reference the descriptor.
77 creates a new kernel event queue and returns a descriptor.
78 The queue is not inherited by a child created with
84 flag, then the descriptor table is shared,
85 which will allow sharing of the kqueue between two processes.
90 is used to register events with the queue, and return any pending
95 is a pointer to an array of
97 structures, as defined in
99 All changes contained in the
101 are applied before any pending events are read from the queue.
110 is a pointer to an array of kevent structures.
114 determines the size of
120 will return immediately even if there is a
126 is a non-NULL pointer, it specifies a maximum interval to wait
127 for an event, which will be interpreted as a struct timespec.
133 To effect a poll, the
135 argument should be non-NULL, pointing to a zero-valued
138 The same array may be used for the
145 macro is provided for ease of initializing a
150 structure is defined as:
153 uintptr_t ident; /* identifier for this event */
154 short filter; /* filter for event */
155 u_short flags; /* action flags for kqueue */
156 u_int fflags; /* filter flag value */
157 intptr_t data; /* filter data value */
158 void *udata; /* opaque user data identifier */
165 .Bl -tag -width "Fa filter"
167 Value used to identify this event.
168 The exact interpretation is determined by the attached filter,
169 but often is a file descriptor.
171 Identifies the kernel filter used to process this event.
173 system filters are described below.
175 Actions to perform on the event.
177 Filter-specific flags.
179 Filter-specific data value.
181 Opaque user-defined value passed through the kernel unchanged.
186 field can contain the following values:
187 .Bl -tag -width EV_DISPATCH
189 Adds the event to the kqueue.
190 Re-adding an existing event
191 will modify the parameters of the original event, and not result
192 in a duplicate entry.
193 Adding an event automatically enables it,
194 unless overridden by the EV_DISABLE flag.
198 to return the event if it is triggered.
203 The filter itself is not disabled.
205 Disable the event source immediately after delivery of an event.
210 Removes the event from the kqueue.
211 Events which are attached to
212 file descriptors are automatically deleted on the last close of
215 This flag is useful for making bulk changes to a kqueue without draining
217 When passed as input, it forces
219 to always be returned.
220 When a filter is successfully added the
224 Causes the event to return only the first occurrence of the filter
226 After the user retrieves the event from the kqueue,
229 After the event is retrieved by the user, its state is reset.
230 This is useful for filters which report state transitions
231 instead of the current state.
232 Note that some filters may automatically
233 set this flag internally.
235 Filters may set this flag to indicate filter-specific EOF condition.
242 The predefined system filters are listed below.
243 Arguments may be passed to and from the filter via the
247 fields in the kevent structure.
248 .Bl -tag -width "Dv EVFILT_PROCDESC"
250 Takes a descriptor as the identifier, and returns whenever
251 there is data available to read.
252 The behavior of the filter is slightly different depending
253 on the descriptor type.
256 Sockets which have previously been passed to
258 return when there is an incoming connection pending.
260 contains the size of the listen backlog.
262 Other socket descriptors return when there is data to be read,
265 value of the socket buffer.
266 This may be overridden with a per-filter low water mark at the
267 time the filter is added by setting the
271 and specifying the new low water mark in
275 contains the number of bytes of protocol data available to read.
277 If the read direction of the socket has shutdown, then the filter
282 and returns the socket error (if any) in
284 It is possible for EOF to be returned (indicating the connection is gone)
285 while there is still data pending in the socket buffer.
287 Returns when the file pointer is not at the end of file.
289 contains the offset from current position to end of file,
292 This behavior is different from
294 where read events are triggered for regular files unconditionally.
295 This event can be triggered unconditionally by setting the
300 Returns when the there is data to read;
302 contains the number of bytes available.
304 When the last writer disconnects, the filter will set
308 This may be cleared by passing in
311 filter will resume waiting for data to become available before
314 Returns when the BPF buffer is full, the BPF timeout has expired, or
317 enabled and there is any data to read;
319 contains the number of bytes available.
322 Takes a descriptor as the identifier, and returns whenever
323 it is possible to write to the descriptor.
327 will contain the amount of space remaining in the write buffer.
328 The filter will set EV_EOF when the reader disconnects, and for
329 the fifo case, this may be cleared by use of
331 Note that this filter is not supported for vnodes or BPF devices.
333 For sockets, the low water mark and socket error handling is
338 The sigevent portion of the AIO request is filled in, with
339 .Va sigev_notify_kqueue
340 containing the descriptor of the kqueue that the event should
342 .Va sigev_notify_kevent_flags
343 containing the kevent flags which should be
349 containing the udata value, and
355 system call is made, the event will be registered
356 with the specified kqueue, and the
363 The filter returns under the same conditions as
366 Takes a file descriptor as the identifier and the events to watch for in
368 and returns when one or more of the requested events occurs on the descriptor.
369 The events to monitor are:
370 .Bl -tag -width "Dv NOTE_CLOSE_WRITE"
372 The file referenced by the descriptor had its attributes changed.
374 A file descriptor referencing the monitored file, was closed.
375 The closed file descriptor did not have write access.
376 .It Dv NOTE_CLOSE_WRITE
377 A file descriptor referencing the monitored file, was closed.
378 The closed file descriptor has write access.
380 This note, as well as
382 are not activated when files are closed forcibly by
387 is sent for such events.
391 system call was called on the file referenced by the descriptor.
393 For regular file, the file referenced by the descriptor was extended.
395 For directory, reports that a directory entry was added or removed,
396 as the result of rename operation.
399 event is not reported when a name is changed inside the directory.
401 The link count on the file changed.
404 event is reported if a subdirectory was created or deleted inside
405 the directory referenced by the descriptor.
407 The file referenced by the descriptor was opened.
409 A read occurred on the file referenced by the descriptor.
411 The file referenced by the descriptor was renamed.
413 Access to the file was revoked via
415 or the underlying file system was unmounted.
417 A write occurred on the file referenced by the descriptor.
422 contains the events which triggered the filter.
424 Takes the process ID to monitor as the identifier and the events to watch for
427 and returns when the process performs one or more of the requested events.
428 If a process can normally see another process, it can attach an event to it.
429 The events to monitor are:
430 .Bl -tag -width "Dv NOTE_TRACKERR"
432 The process has exited.
433 The exit status will be stored in
436 The process has called
439 The process has executed a new process via
443 Follow a process across
446 The parent process registers a new kevent to monitor the child process
449 as the original event.
450 The child process will signal an event with
454 and the parent PID in
457 If the parent process fails to register a new kevent
458 .Pq usually due to resource limitations ,
459 it will signal an event with
463 and the child process will not signal a
470 contains the events which triggered the filter.
471 .It Dv EVFILT_PROCDESC
472 Takes the process descriptor created by
474 to monitor as the identifier and the events to watch for in
476 and returns when the associated process performs one or more of the
478 The events to monitor are:
479 .Bl -tag -width "Dv NOTE_EXIT"
481 The process has exited.
482 The exit status will be stored in
488 contains the events which triggered the filter.
490 Takes the signal number to monitor as the identifier and returns
491 when the given signal is delivered to the process.
492 This coexists with the
496 facilities, and has a lower precedence.
497 The filter will record
498 all attempts to deliver a signal to a process, even if the signal has
503 signal, which, if ignored, won't be recorded by the filter.
504 Event notification happens after normal
505 signal delivery processing.
507 returns the number of times the signal has occurred since the last call to
509 This filter automatically sets the
513 Establishes an arbitrary timer identified by
517 specifies the timeout period.
518 The timer will be periodic unless
523 contains the number of times the timeout has expired since the last call to
525 This filter automatically sets the EV_CLEAR flag internally.
526 There is a system wide limit on the number of timers
527 which is controlled by the
528 .Va kern.kq_calloutmax
530 .Bl -tag -width "Dv NOTE_USECONDS"
547 is not set, the default is milliseconds. On return,
549 contains the events which triggered the filter.
551 Establishes a user event identified by
553 which is not associated with any kernel mechanism but is triggered by
555 The lower 24 bits of the
557 may be used for user defined flags and manipulated using the following:
558 .Bl -tag -width "Dv NOTE_FFLAGSMASK"
571 .It Dv NOTE_FFCTRLMASK
574 .It Dv NOTE_FFLAGSMASK
575 User defined flag mask for
579 A user event is triggered for output with the following:
580 .Bl -tag -width "Dv NOTE_FFLAGSMASK"
582 Cause the event to be triggered.
587 contains the users defined flags in the lower 24 bits.
589 .Sh CANCELLATION BEHAVIOUR
592 is non-zero, i.e. the function is potentially blocking, the call
593 is a cancellation point.
596 is zero, the call is not cancellable.
597 Cancellation can only occur before any changes are made to the kqueue,
598 or when the call was blocked and no changes to the queue were requested.
603 creates a new kernel event queue and returns a file descriptor.
604 If there was an error creating the kernel event queue, a value of -1 is
605 returned and errno set.
610 returns the number of events placed in the
612 up to the value given by
614 If an error occurs while processing an element of the
616 and there is enough room in the
618 then the event will be placed in the
624 and the system error in
628 will be returned, and
630 will be set to indicate the error condition.
631 If the time limit expires, then
635 .Bd -literal -compact
636 #include <sys/types.h>
637 #include <sys/event.h>
638 #include <sys/time.h>
647 main(int argc, char **argv)
649 struct kevent event; /* Event we want to monitor */
650 struct kevent tevent; /* Event triggered */
654 err(EXIT_FAILURE, "Usage: %s path\en", argv[0]);
655 fd = open(argv[1], O_RDONLY);
657 err(EXIT_FAILURE, "Failed to open '%s'", argv[1]);
662 err(EXIT_FAILURE, "kqueue() failed");
664 /* Initialize kevent structure. */
665 EV_SET(&event, fd, EVFILT_VNODE, EV_ADD | EV_CLEAR, NOTE_WRITE,
667 /* Attach event to the kqueue. */
668 ret = kevent(kq, &event, 1, NULL, 0, NULL);
670 err(EXIT_FAILURE, "kevent register");
671 if (event.flags & EV_ERROR)
672 errx(EXIT_FAILURE, "Event error: %s", strerror(event.data));
675 /* Sleep until something happens. */
676 ret = kevent(kq, NULL, 0, &tevent, 1, NULL);
678 err(EXIT_FAILURE, "kevent wait");
679 } else if (ret > 0) {
680 printf("Something was written in '%s'\en", argv[1]);
688 system call fails if:
691 The kernel failed to allocate enough memory for the kernel queue.
698 for the current user would be exceeded.
700 The per-process descriptor table is full.
702 The system file table is full.
707 system call fails if:
710 The process does not have permission to register a filter.
712 There was an error reading or writing the
716 The specified descriptor is invalid.
718 A signal was delivered before the timeout expired and before any
719 events were placed on the kqueue for return.
721 A cancellation request was delivered to the thread, but not yet handled.
723 The specified time limit or filter is invalid.
725 The event could not be found to be modified or deleted.
727 No memory was available to register the event
728 or, in the special case of a timer, the maximum number of
729 timers has been exceeded.
730 This maximum is configurable via the
731 .Va kern.kq_calloutmax
734 The specified process to attach to does not exist.
741 error, all changes in the
753 .Xr pthread_setcancelstate 3 ,
760 system calls first appeared in
765 system and this manual page were written by
766 .An Jonathan Lemon Aq Mt jlemon@FreeBSD.org .
770 value is limited to 24 hours; longer timeouts will be silently
771 reinterpreted as 24 hours.