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