]> git.saurik.com Git - apple/xnu.git/blame - bsd/man/man2/kqueue.2
xnu-1228.7.58.tar.gz
[apple/xnu.git] / bsd / man / man2 / kqueue.2
CommitLineData
55e303ae
A
1.\" Copyright (c) 2000 Jonathan Lemon
2.\" All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
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.
12.\"
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
23.\" SUCH DAMAGE.
24.\"
25.\" $FreeBSD: src/lib/libc/sys/kqueue.2,v 1.32 2002/12/19 09:40:25 ru Exp $
26.\"
27.Dd April 14, 2000
28.Dt KQUEUE 2
29.Os
30.Sh NAME
31.Nm kqueue ,
32.Nm kevent
33.Nd kernel event notification mechanism
34.Sh LIBRARY
35.Lb libc
36.Sh SYNOPSIS
37.In sys/types.h
38.In sys/event.h
39.In sys/time.h
40.Ft int
41.Fn kqueue "void"
42.Ft int
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
45.Sh DESCRIPTION
46The
47.Fn kqueue
48system call
49provides a generic method of notifying the user when an kernel
50event (kevent) happens or a condition holds, based on the results
51of small pieces of kernel code termed filters.
52A kevent is identified by an (ident, filter) pair and specifies
53the interesting conditions to be notified about for that pair.
54An (ident, filter) pair can only appear once is a given kqueue.
55Subsequent attempts to register the same pair for a given kqueue
56will result in the replacement of the conditions being watched,
57not an addition.
58.Pp
59The filter identified in a kevent is executed upon the initial
60registration of that event in order to detect whether a preexisting
61condition is present, and is also executed whenever an event is
62passed to the filter for evaluation.
63If the filter determines that the condition should be reported,
64then the kevent is placed on the kqueue for the user to retrieve.
65.Pp
66The filter is also run when the user attempts to retrieve the kevent
67from the kqueue.
68If the filter indicates that the condition that triggered
69the event no longer holds, the kevent is removed from the kqueue and
70is not returned.
71.Pp
72Multiple events which trigger the filter do not result in multiple
73kevents being placed on the kqueue; instead, the filter will aggregate
74the events into a single struct kevent.
75Calling
76.Fn close
77on a file descriptor will remove any kevents that reference the descriptor.
78.Pp
79The
80.Fn kqueue
81system call
82creates a new kernel event queue and returns a descriptor.
83The queue is not inherited by a child created with
84.Xr fork 2 .
85.Pp
86The
87.Fn kevent
88system call
89is used to register events with the queue, and return any pending
90events to the user.
91The
92.Fa changelist
93argument
94is a pointer to an array of
95.Va kevent
96structures, as defined in
97.Aq Pa sys/event.h .
98All changes contained in the
99.Fa changelist
100are applied before any pending events are read from the queue.
101The
102.Fa nchanges
103argument
104gives the size of
105.Fa changelist .
106The
107.Fa eventlist
108argument
109is a pointer to an array of kevent structures.
110The
111.Fa nevents
112argument
113determines the size of
114.Fa eventlist .
115If
116.Fa timeout
117is a non-NULL pointer, it specifies a maximum interval to wait
118for an event, which will be interpreted as a struct timespec. If
119.Fa timeout
120is a NULL pointer,
121.Fn kevent
122waits indefinitely. To effect a poll, the
123.Fa timeout
124argument should be non-NULL, pointing to a zero-valued
125.Va timespec
126structure. The same array may be used for the
127.Fa changelist
128and
129.Fa eventlist .
130.Pp
131The
132.Fn EV_SET
133macro is provided for ease of initializing a
134kevent structure.
135.Pp
136The
137.Va kevent
138structure is defined as:
139.Bd -literal
140struct kevent {
141 uintptr_t ident; /* identifier for this event */
142 short filter; /* filter for event */
143 u_short flags; /* action flags for kqueue */
144 u_int fflags; /* filter flag value */
145 intptr_t data; /* filter data value */
146 void *udata; /* opaque user data identifier */
147};
148.Ed
149.Pp
150The fields of
151.Fa struct kevent
152are:
153.Bl -tag -width XXXfilter
154.It ident
155Value used to identify this event.
156The exact interpretation is determined by the attached filter,
157but often is a file descriptor.
158.It filter
159Identifies the kernel filter used to process this event. The pre-defined
160system filters are described below.
161.It flags
162Actions to perform on the event.
163.It fflags
164Filter-specific flags.
165.It data
166Filter-specific data value.
167.It udata
168Opaque user-defined value passed through the kernel unchanged.
169.El
170.Pp
171The
172.Va flags
173field can contain the following values:
174.Bl -tag -width XXXEV_ONESHOT
175.It EV_ADD
176Adds the event to the kqueue. Re-adding an existing event
177will modify the parameters of the original event, and not result
178in a duplicate entry. Adding an event automatically enables it,
179unless overridden by the EV_DISABLE flag.
180.It EV_ENABLE
181Permit
182.Fn kevent
183to return the event if it is triggered.
184.It EV_DISABLE
185Disable the event so
186.Fn kevent
187will not return it. The filter itself is not disabled.
188.It EV_DELETE
189Removes the event from the kqueue. Events which are attached to
190file descriptors are automatically deleted on the last close of
191the descriptor.
2d21ac55
A
192.It EV_RECEIPT
193This flag is useful for making bulk changes to a kqueue without draining any
194pending events. When passed as input, it forces EV_ERROR to always be returned.
195When a filter is successfully added. The
196.Va data
197field will be zero.
55e303ae
A
198.It EV_ONESHOT
199Causes the event to return only the first occurrence of the filter
200being triggered. After the user retrieves the event from the kqueue,
201it is deleted.
202.It EV_CLEAR
203After the event is retrieved by the user, its state is reset.
204This is useful for filters which report state transitions
205instead of the current state. Note that some filters may automatically
206set this flag internally.
207.It EV_EOF
208Filters may set this flag to indicate filter-specific EOF condition.
209.It EV_ERROR
210See
211.Sx RETURN VALUES
212below.
213.El
214.Pp
215The predefined system filters are listed below.
216Arguments may be passed to and from the filter via the
217.Va fflags
218and
219.Va data
220fields in the kevent structure.
221.Bl -tag -width EVFILT_SIGNAL
222.It EVFILT_READ
223Takes a file descriptor as the identifier, and returns whenever
224there is data available to read.
225The behavior of the filter is slightly different depending
226on the descriptor type.
227.Pp
228.Bl -tag -width 2n
229.It Sockets
230Sockets which have previously been passed to
231.Fn listen
232return when there is an incoming connection pending.
233.Va data
234contains the size of the listen backlog.
235.Pp
236Other socket descriptors return when there is data to be read,
237subject to the
238.Dv SO_RCVLOWAT
239value of the socket buffer.
240This may be overridden with a per-filter low water mark at the
241time the filter is added by setting the
242NOTE_LOWAT
243flag in
244.Va fflags ,
245and specifying the new low water mark in
246.Va data .
247On return,
248.Va data
249contains the number of bytes of protocol data available to read.
250.Pp
251If the read direction of the socket has shutdown, then the filter
252also sets EV_EOF in
253.Va flags ,
254and returns the socket error (if any) in
255.Va fflags .
256It is possible for EOF to be returned (indicating the connection is gone)
257while there is still data pending in the socket buffer.
258.It Vnodes
259Returns when the file pointer is not at the end of file.
260.Va data
261contains the offset from current position to end of file,
262and may be negative.
263.It "Fifos, Pipes"
264Returns when the there is data to read;
265.Va data
266contains the number of bytes available.
267.Pp
268When the last writer disconnects, the filter will set EV_EOF in
269.Va flags .
270This may be cleared by passing in EV_CLEAR, at which point the
271filter will resume waiting for data to become available before
272returning.
273.El
274.It EVFILT_WRITE
275Takes a file descriptor as the identifier, and returns whenever
276it is possible to write to the descriptor. For sockets, pipes
277and fifos,
278.Va data
279will contain the amount of space remaining in the write buffer.
280The filter will set EV_EOF when the reader disconnects, and for
281the fifo case, this may be cleared by use of EV_CLEAR.
282Note that this filter is not supported for vnodes.
283.Pp
284For sockets, the low water mark and socket error handling is
285identical to the EVFILT_READ case.
286.It EVFILT_AIO
287This filter is currently unsupported.
288.\"The sigevent portion of the AIO request is filled in, with
289.\".Va sigev_notify_kqueue
290.\"containing the descriptor of the kqueue that the event should
291.\"be attached to,
292.\".Va sigev_value
293.\"containing the udata value, and
294.\".Va sigev_notify
295.\"set to SIGEV_KEVENT.
296.\"When the
297.\".Fn aio_*
298.\"system call is made, the event will be registered
299.\"with the specified kqueue, and the
300.\".Va ident
301.\"argument set to the
302.\".Fa struct aiocb
303.\"returned by the
304.\".Fn aio_*
305.\"system call.
306.\"The filter returns under the same conditions as aio_error.
307.\".Pp
308.\"Alternatively, a kevent structure may be initialized, with
309.\".Va ident
310.\"containing the descriptor of the kqueue, and the
311.\"address of the kevent structure placed in the
312.\".Va aio_lio_opcode
313.\"field of the AIO request. However, this approach will not work on
314.\"architectures with 64-bit pointers, and should be considered deprecated.
315.It EVFILT_VNODE
316Takes a file descriptor as the identifier and the events to watch for in
317.Va fflags ,
318and returns when one or more of the requested events occurs on the descriptor.
319The events to monitor are:
320.Bl -tag -width XXNOTE_RENAME
321.It NOTE_DELETE
322The
323.Fn unlink
324system call
325was called on the file referenced by the descriptor.
326.It NOTE_WRITE
327A write occurred on the file referenced by the descriptor.
328.It NOTE_EXTEND
329The file referenced by the descriptor was extended.
330.It NOTE_ATTRIB
331The file referenced by the descriptor had its attributes changed.
332.It NOTE_LINK
333The link count on the file changed.
334.It NOTE_RENAME
335The file referenced by the descriptor was renamed.
336.It NOTE_REVOKE
337Access to the file was revoked via
338.Xr revoke 2
339or the underlying fileystem was unmounted.
340.El
341.Pp
342On return,
343.Va fflags
344contains the events which triggered the filter.
345.It EVFILT_PROC
346Takes the process ID to monitor as the identifier and the events to watch for
347in
348.Va fflags ,
349and returns when the process performs one or more of the requested events.
350If a process can normally see another process, it can attach an event to it.
351The events to monitor are:
2d21ac55 352.Bl -tag -width
55e303ae
A
353.It NOTE_EXIT
354The process has exited.
355.It NOTE_FORK
2d21ac55
A
356The process created a child process via
357.Xr fork 2
358or similar call.
55e303ae 359.It NOTE_EXEC
2d21ac55 360The process executed a new process via
55e303ae
A
361.Xr execve 2
362or similar call.
2d21ac55
A
363.It NOTE_SIGNAL
364The process was sent a signal. Status can be checked via
365.Xr waitpid 2
366or similar call.
367.It NOTE_REAP
368The process was reaped by the parent via
369.Xr wait 2
370or similar call.
55e303ae
A
371.El
372.Pp
373On return,
374.Va fflags
375contains the events which triggered the filter.
376.It EVFILT_SIGNAL
377Takes the signal number to monitor as the identifier and returns
378when the given signal is delivered to the process.
379This coexists with the
380.Fn signal
381and
382.Fn sigaction
383facilities, and has a lower precedence. The filter will record
384all attempts to deliver a signal to a process, even if the signal has
385been marked as SIG_IGN. Event notification happens after normal
386signal delivery processing.
387.Va data
388returns the number of times the signal has occurred since the last call to
389.Fn kevent .
390This filter automatically sets the EV_CLEAR flag internally.
391.It EVFILT_TIMER
392This filter is currently unsupported.
393.\"Establishes an arbitrary timer identified by
394.\".Va ident .
395.\"When adding a timer,
396.\".Va data
397.\"specifies the timeout period in milliseconds.
398.\"The timer will be periodic unless EV_ONESHOT is specified.
399.\"On return,
400.\".Va data
401.\"contains the number of times the timeout has expired since the last call to
402.\".Fn kevent .
403.\"This filter automatically sets the EV_CLEAR flag internally.
404.El
405.Sh RETURN VALUES
406The
407.Fn kqueue
408system call
409creates a new kernel event queue and returns a file descriptor.
410If there was an error creating the kernel event queue, a value of -1 is
411returned and errno set.
412.Pp
413The
414.Fn kevent
415system call
416returns the number of events placed in the
417.Fa eventlist ,
418up to the value given by
419.Fa nevents .
420If an error occurs while processing an element of the
421.Fa changelist
422and there is enough room in the
423.Fa eventlist ,
424then the event will be placed in the
425.Fa eventlist
426with
427.Dv EV_ERROR
428set in
429.Va flags
430and the system error in
431.Va data .
432Otherwise,
433.Dv -1
434will be returned, and
435.Dv errno
436will be set to indicate the error condition.
437If the time limit expires, then
438.Fn kevent
439returns 0.
440.Sh ERRORS
441The
442.Fn kqueue
443system call fails if:
444.Bl -tag -width Er
445.It Bq Er ENOMEM
446The kernel failed to allocate enough memory for the kernel queue.
447.It Bq Er EMFILE
448The per-process descriptor table is full.
449.It Bq Er ENFILE
450The system file table is full.
451.El
452.Pp
453The
454.Fn kevent
455system call fails if:
456.Bl -tag -width Er
457.It Bq Er EACCES
458The process does not have permission to register a filter.
459.It Bq Er EFAULT
460There was an error reading or writing the
461.Va kevent
462structure.
463.It Bq Er EBADF
464The specified descriptor is invalid.
465.It Bq Er EINTR
466A signal was delivered before the timeout expired and before any
467events were placed on the kqueue for return.
468.It Bq Er EINVAL
469The specified time limit or filter is invalid.
470.It Bq Er ENOENT
471The event could not be found to be modified or deleted.
472.It Bq Er ENOMEM
473No memory was available to register the event.
474.It Bq Er ESRCH
475The specified process to attach to does not exist.
476.El
477.Sh SEE ALSO
478.Xr aio_error 2 ,
479.Xr aio_read 2 ,
480.Xr aio_return 2 ,
481.Xr read 2 ,
482.Xr select 2 ,
483.Xr sigaction 2 ,
484.Xr write 2 ,
485.Xr signal 3
486.Sh HISTORY
487The
488.Fn kqueue
489and
490.Fn kevent
491system calls first appeared in
492.Fx 4.1 .
493.Sh AUTHORS
494The
495.Fn kqueue
496system and this manual page were written by
497.An Jonathan Lemon Aq jlemon@FreeBSD.org .
498.Sh BUGS
499Not all filesystem types support kqueue-style notifications.
500And even some that do, like some remote filesystems, may only
501support a subset of the notification semantics described
502here.