]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/sys/kqueue.2
This commit was generated by cvs2svn to compensate for changes in r159063,
[FreeBSD/FreeBSD.git] / lib / libc / sys / 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$
26 .\"
27 .Dd June 4, 2005
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 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.
54 .Pp
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.
60 .Pp
61 The filter is also run when the user attempts to retrieve the kevent
62 from the kqueue.
63 If the filter indicates that the condition that triggered
64 the event no longer holds, the kevent is removed from the kqueue and
65 is not returned.
66 .Pp
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.
70 Calling
71 .Fn close
72 on a file descriptor will remove any kevents that reference the descriptor.
73 .Pp
74 The
75 .Fn kqueue
76 system call
77 creates a new kernel event queue and returns a descriptor.
78 The queue is not inherited by a child created with
79 .Xr fork 2 .
80 However, if
81 .Xr rfork 2
82 is called without the
83 .Dv RFFDG
84 flag, then the descriptor table is shared,
85 which will allow sharing of the kqueue between two processes.
86 .Pp
87 The
88 .Fn kevent
89 system call
90 is used to register events with the queue, and return any pending
91 events to the user.
92 The
93 .Fa changelist
94 argument
95 is a pointer to an array of
96 .Va kevent
97 structures, as defined in
98 .In sys/event.h .
99 All changes contained in the
100 .Fa changelist
101 are applied before any pending events are read from the queue.
102 The
103 .Fa nchanges
104 argument
105 gives the size of
106 .Fa changelist .
107 The
108 .Fa eventlist
109 argument
110 is a pointer to an array of kevent structures.
111 The
112 .Fa nevents
113 argument
114 determines the size of
115 .Fa eventlist .
116 When
117 .Fa nevents
118 is zero,
119 .Fn kevent
120 will return immediately even if there is a
121 .Fa timeout
122 specified unlike
123 .Xr select 2 .
124 If
125 .Fa timeout
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.
128 If
129 .Fa timeout
130 is a NULL pointer,
131 .Fn kevent
132 waits indefinitely.
133 To effect a poll, the
134 .Fa timeout
135 argument should be non-NULL, pointing to a zero-valued
136 .Va timespec
137 structure.
138 The same array may be used for the
139 .Fa changelist
140 and
141 .Fa eventlist .
142 .Pp
143 The
144 .Fn EV_SET
145 macro is provided for ease of initializing a
146 kevent structure.
147 .Pp
148 The
149 .Va kevent
150 structure is defined as:
151 .Bd -literal
152 struct kevent {
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 */
159 };
160 .Ed
161 .Pp
162 The fields of
163 .Fa struct kevent
164 are:
165 .Bl -tag -width XXXfilter
166 .It ident
167 Value used to identify this event.
168 The exact interpretation is determined by the attached filter,
169 but often is a file descriptor.
170 .It filter
171 Identifies the kernel filter used to process this event.
172 The pre-defined
173 system filters are described below.
174 .It flags
175 Actions to perform on the event.
176 .It fflags
177 Filter-specific flags.
178 .It data
179 Filter-specific data value.
180 .It udata
181 Opaque user-defined value passed through the kernel unchanged.
182 .El
183 .Pp
184 The
185 .Va flags
186 field can contain the following values:
187 .Bl -tag -width XXXEV_ONESHOT
188 .It EV_ADD
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.
195 .It EV_ENABLE
196 Permit
197 .Fn kevent
198 to return the event if it is triggered.
199 .It EV_DISABLE
200 Disable the event so
201 .Fn kevent
202 will not return it.
203 The filter itself is not disabled.
204 .It EV_DELETE
205 Removes the event from the kqueue.
206 Events which are attached to
207 file descriptors are automatically deleted on the last close of
208 the descriptor.
209 .It EV_ONESHOT
210 Causes the event to return only the first occurrence of the filter
211 being triggered.
212 After the user retrieves the event from the kqueue,
213 it is deleted.
214 .It EV_CLEAR
215 After the event is retrieved by the user, its state is reset.
216 This is useful for filters which report state transitions
217 instead of the current state.
218 Note that some filters may automatically
219 set this flag internally.
220 .It EV_EOF
221 Filters may set this flag to indicate filter-specific EOF condition.
222 .It EV_ERROR
223 See
224 .Sx RETURN VALUES
225 below.
226 .El
227 .Pp
228 The predefined system filters are listed below.
229 Arguments may be passed to and from the filter via the
230 .Va fflags
231 and
232 .Va data
233 fields in the kevent structure.
234 .Bl -tag -width EVFILT_SIGNAL
235 .It EVFILT_READ
236 Takes a descriptor as the identifier, and returns whenever
237 there is data available to read.
238 The behavior of the filter is slightly different depending
239 on the descriptor type.
240 .Pp
241 .Bl -tag -width 2n
242 .It Sockets
243 Sockets which have previously been passed to
244 .Fn listen
245 return when there is an incoming connection pending.
246 .Va data
247 contains the size of the listen backlog.
248 .Pp
249 Other socket descriptors return when there is data to be read,
250 subject to the
251 .Dv SO_RCVLOWAT
252 value of the socket buffer.
253 This may be overridden with a per-filter low water mark at the
254 time the filter is added by setting the
255 NOTE_LOWAT
256 flag in
257 .Va fflags ,
258 and specifying the new low water mark in
259 .Va data .
260 On return,
261 .Va data
262 contains the number of bytes of protocol data available to read.
263 .Pp
264 If the read direction of the socket has shutdown, then the filter
265 also sets EV_EOF in
266 .Va flags ,
267 and returns the socket error (if any) in
268 .Va fflags .
269 It is possible for EOF to be returned (indicating the connection is gone)
270 while there is still data pending in the socket buffer.
271 .It Vnodes
272 Returns when the file pointer is not at the end of file.
273 .Va data
274 contains the offset from current position to end of file,
275 and may be negative.
276 .It "Fifos, Pipes"
277 Returns when the there is data to read;
278 .Va data
279 contains the number of bytes available.
280 .Pp
281 When the last writer disconnects, the filter will set EV_EOF in
282 .Va flags .
283 This may be cleared by passing in EV_CLEAR, at which point the
284 filter will resume waiting for data to become available before
285 returning.
286 .It "BPF devices"
287 Returns when the BPF buffer is full, the BPF timeout has expired, or
288 when the BPF has
289 .Dq immediate mode
290 enabled and there is any data to read;
291 .Va data
292 contains the number of bytes available.
293 .El
294 .It EVFILT_WRITE
295 Takes a descriptor as the identifier, and returns whenever
296 it is possible to write to the descriptor.
297 For sockets, pipes
298 and fifos,
299 .Va data
300 will contain the amount of space remaining in the write buffer.
301 The filter will set EV_EOF when the reader disconnects, and for
302 the fifo case, this may be cleared by use of EV_CLEAR.
303 Note that this filter is not supported for vnodes or BPF devices.
304 .Pp
305 For sockets, the low water mark and socket error handling is
306 identical to the EVFILT_READ case.
307 .It EVFILT_AIO
308 The sigevent portion of the AIO request is filled in, with
309 .Va sigev_notify_kqueue
310 containing the descriptor of the kqueue that the event should
311 be attached to,
312 .Va sigev_value
313 containing the udata value, and
314 .Va sigev_notify
315 set to SIGEV_KEVENT.
316 When the
317 .Fn aio_*
318 system call is made, the event will be registered
319 with the specified kqueue, and the
320 .Va ident
321 argument set to the
322 .Fa struct aiocb
323 returned by the
324 .Fn aio_*
325 system call.
326 The filter returns under the same conditions as aio_error.
327 .It EVFILT_VNODE
328 Takes a file descriptor as the identifier and the events to watch for in
329 .Va fflags ,
330 and returns when one or more of the requested events occurs on the descriptor.
331 The events to monitor are:
332 .Bl -tag -width XXNOTE_RENAME
333 .It NOTE_DELETE
334 The
335 .Fn unlink
336 system call
337 was called on the file referenced by the descriptor.
338 .It NOTE_WRITE
339 A write occurred on the file referenced by the descriptor.
340 .It NOTE_EXTEND
341 The file referenced by the descriptor was extended.
342 .It NOTE_ATTRIB
343 The file referenced by the descriptor had its attributes changed.
344 .It NOTE_LINK
345 The link count on the file changed.
346 .It NOTE_RENAME
347 The file referenced by the descriptor was renamed.
348 .It NOTE_REVOKE
349 Access to the file was revoked via
350 .Xr revoke 2
351 or the underlying file system was unmounted.
352 .El
353 .Pp
354 On return,
355 .Va fflags
356 contains the events which triggered the filter.
357 .It EVFILT_PROC
358 Takes the process ID to monitor as the identifier and the events to watch for
359 in
360 .Va fflags ,
361 and returns when the process performs one or more of the requested events.
362 If a process can normally see another process, it can attach an event to it.
363 The events to monitor are:
364 .Bl -tag -width XXNOTE_TRACKERR
365 .It NOTE_EXIT
366 The process has exited.
367 .It NOTE_FORK
368 The process has called
369 .Fn fork .
370 .It NOTE_EXEC
371 The process has executed a new process via
372 .Xr execve 2
373 or similar call.
374 .It NOTE_TRACK
375 Follow a process across
376 .Fn fork
377 calls.
378 The parent process will return with NOTE_TRACK set in the
379 .Va fflags
380 field, while the child process will return with NOTE_CHILD set in
381 .Va fflags
382 and the parent PID in
383 .Va data .
384 .It NOTE_TRACKERR
385 This flag is returned if the system was unable to attach an event to
386 the child process, usually due to resource limitations.
387 .El
388 .Pp
389 On return,
390 .Va fflags
391 contains the events which triggered the filter.
392 .It EVFILT_SIGNAL
393 Takes the signal number to monitor as the identifier and returns
394 when the given signal is delivered to the process.
395 This coexists with the
396 .Fn signal
397 and
398 .Fn sigaction
399 facilities, and has a lower precedence.
400 The filter will record
401 all attempts to deliver a signal to a process, even if the signal has
402 been marked as SIG_IGN.
403 Event notification happens after normal
404 signal delivery processing.
405 .Va data
406 returns the number of times the signal has occurred since the last call to
407 .Fn kevent .
408 This filter automatically sets the EV_CLEAR flag internally.
409 .It EVFILT_TIMER
410 Establishes an arbitrary timer identified by
411 .Va ident .
412 When adding a timer,
413 .Va data
414 specifies the timeout period in milliseconds.
415 The timer will be periodic unless EV_ONESHOT is specified.
416 On return,
417 .Va data
418 contains the number of times the timeout has expired since the last call to
419 .Fn kevent .
420 This filter automatically sets the EV_CLEAR flag internally.
421 .It Dv EVFILT_NETDEV
422 Takes a descriptor to a network interface as the identifier, and the events to watch for in
423 .Va fflags .
424 It returns, when one or more of the requested events occur on the descriptor.
425 The events to monitor are:
426 .Bl -tag -width XXNOTE_LINKDOWN
427 .It Dv NOTE_LINKUP
428 The link is up.
429 .It Dv NOTE_LINKDOWN
430 The link is down.
431 .It Dv NOTE_LINKINV
432 The link state is invalid.
433 .El
434 .Pp
435 On return,
436 .Va fflags
437 contains the events which triggered the filter.
438 .El
439 .Sh RETURN VALUES
440 The
441 .Fn kqueue
442 system call
443 creates a new kernel event queue and returns a file descriptor.
444 If there was an error creating the kernel event queue, a value of -1 is
445 returned and errno set.
446 .Pp
447 The
448 .Fn kevent
449 system call
450 returns the number of events placed in the
451 .Fa eventlist ,
452 up to the value given by
453 .Fa nevents .
454 If an error occurs while processing an element of the
455 .Fa changelist
456 and there is enough room in the
457 .Fa eventlist ,
458 then the event will be placed in the
459 .Fa eventlist
460 with
461 .Dv EV_ERROR
462 set in
463 .Va flags
464 and the system error in
465 .Va data .
466 Otherwise,
467 .Dv -1
468 will be returned, and
469 .Dv errno
470 will be set to indicate the error condition.
471 If the time limit expires, then
472 .Fn kevent
473 returns 0.
474 .Sh ERRORS
475 The
476 .Fn kqueue
477 system call fails if:
478 .Bl -tag -width Er
479 .It Bq Er ENOMEM
480 The kernel failed to allocate enough memory for the kernel queue.
481 .It Bq Er EMFILE
482 The per-process descriptor table is full.
483 .It Bq Er ENFILE
484 The system file table is full.
485 .El
486 .Pp
487 The
488 .Fn kevent
489 system call fails if:
490 .Bl -tag -width Er
491 .It Bq Er EACCES
492 The process does not have permission to register a filter.
493 .It Bq Er EFAULT
494 There was an error reading or writing the
495 .Va kevent
496 structure.
497 .It Bq Er EBADF
498 The specified descriptor is invalid.
499 .It Bq Er EINTR
500 A signal was delivered before the timeout expired and before any
501 events were placed on the kqueue for return.
502 .It Bq Er EINVAL
503 The specified time limit or filter is invalid.
504 .It Bq Er ENOENT
505 The event could not be found to be modified or deleted.
506 .It Bq Er ENOMEM
507 No memory was available to register the event.
508 .It Bq Er ESRCH
509 The specified process to attach to does not exist.
510 .El
511 .Sh SEE ALSO
512 .Xr aio_error 2 ,
513 .Xr aio_read 2 ,
514 .Xr aio_return 2 ,
515 .Xr poll 2 ,
516 .Xr read 2 ,
517 .Xr select 2 ,
518 .Xr sigaction 2 ,
519 .Xr write 2 ,
520 .Xr signal 3
521 .Sh HISTORY
522 The
523 .Fn kqueue
524 and
525 .Fn kevent
526 system calls first appeared in
527 .Fx 4.1 .
528 .Sh AUTHORS
529 The
530 .Fn kqueue
531 system and this manual page were written by
532 .An Jonathan Lemon Aq jlemon@FreeBSD.org .
533 .Sh BUGS
534 It is currently not possible to watch a
535 .Xr vnode 9
536 that resides on anything but
537 a UFS file system.
538 .Pp
539 The
540 .Dv EVFILT_NETDEV
541 filter is currently only implemented for devices that use the
542 .Xr miibus 4
543 driver for LINKUP and LINKDOWN operations.
544 Therefore, it will not work with many non-ethernet devices.
545 .Pp
546 The
547 .Fa timeout
548 value is limited to 24 hours; longer timeouts will be silently
549 reinterpreted as 24 hours.