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