Tizen 2.1 base
[platform/upstream/gcd.git] / kqueue-1.0.4 / kqueue.2
1 .\" $FreeBSD: Revision: 197243$
2 .\" Copyright (c) 2010 Mark Heily
3 .\" Copyright (c) 2000 Jonathan Lemon
4 .\" All rights reserved.
5 .\"
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
8 .\" are met:
9 .\" 1. Redistributions of source code must retain the above copyright
10 .\"    notice, this list of conditions and the following disclaimer.
11 .\" 2. Redistributions in binary form must reproduce the above copyright
12 .\"    notice, this list of conditions and the following disclaimer in the
13 .\"    documentation and/or other materials provided with the distribution.
14 .\"
15 .\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND
16 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 .\" SUCH DAMAGE.
26 .\"
27 .\" $FreeBSD$
28 .\"
29 .Dd September 17, 2010
30 .Dt KQUEUE 2
31 .Os
32 .Sh NAME
33 .Nm kqueue ,
34 .Nm kevent
35 .Nd kernel event notification mechanism
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_DISPATCH
205 Disable the event source immediately after delivery of an event.
206 See 
207 .Dv EV_DISABLE
208 above.
209 .It EV_DELETE
210 Removes the event from the kqueue.
211 Events which are attached to
212 file descriptors are automatically deleted on the last close of
213 the descriptor.
214 .It EV_RECEIPT
215 This flag is useful for making bulk changes to a kqueue without draining
216 any pending events.
217 When passed as input, it forces
218 .Dv EV_ERROR
219 to always be returned.
220 When a filter is successfully added the 
221 .Va data
222 field will be zero.
223 .It EV_ONESHOT
224 Causes the event to return only the first occurrence of the filter
225 being triggered.
226 After the user retrieves the event from the kqueue,
227 it is deleted.
228 .It EV_CLEAR
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.
234 .It EV_EOF
235 Filters may set this flag to indicate filter-specific EOF condition.
236 .It EV_ERROR
237 See
238 .Sx RETURN VALUES
239 below.
240 .El
241 .Pp
242 The predefined system filters are listed below.
243 Arguments may be passed to and from the filter via the
244 .Va fflags
245 and
246 .Va data
247 fields in the kevent structure.
248 .Bl -tag -width EVFILT_SIGNAL
249 .It EVFILT_READ
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.
254 .Pp
255 .Bl -tag -width 2n
256 .It Sockets
257 Sockets which have previously been passed to
258 .Fn listen
259 return when there is an incoming connection pending.
260 .Va data
261 contains the size of the listen backlog.
262 .Pp
263 Other socket descriptors return when there is data to be read,
264 subject to the
265 .Dv SO_RCVLOWAT
266 value of the socket buffer.
267 This may be overridden with a per-filter low water mark at the
268 time the filter is added by setting the
269 NOTE_LOWAT
270 flag in
271 .Va fflags ,
272 and specifying the new low water mark in
273 .Va data .
274 On return,
275 .Va data
276 contains the number of bytes of protocol data available to read.
277 .Pp
278 If the read direction of the socket has shutdown, then the filter
279 also sets EV_EOF in
280 .Va flags ,
281 and returns the socket error (if any) in
282 .Va fflags .
283 It is possible for EOF to be returned (indicating the connection is gone)
284 while there is still data pending in the socket buffer.
285 .It Vnodes
286 Returns when the file pointer is not at the end of file.
287 .Va data
288 contains the offset from current position to end of file,
289 and may be negative.
290 .It "Fifos, Pipes"
291 Returns when the there is data to read;
292 .Va data
293 contains the number of bytes available.
294 .Pp
295 When the last writer disconnects, the filter will set EV_EOF in
296 .Va flags .
297 This may be cleared by passing in EV_CLEAR, at which point the
298 filter will resume waiting for data to become available before
299 returning.
300 .It "BPF devices"
301 Returns when the BPF buffer is full, the BPF timeout has expired, or
302 when the BPF has
303 .Dq immediate mode
304 enabled and there is any data to read;
305 .Va data
306 contains the number of bytes available.
307 .El
308 .It EVFILT_WRITE
309 Takes a descriptor as the identifier, and returns whenever
310 it is possible to write to the descriptor.
311 For sockets, pipes
312 and fifos,
313 .Va data
314 will contain the amount of space remaining in the write buffer.
315 The filter will set EV_EOF when the reader disconnects, and for
316 the fifo case, this may be cleared by use of EV_CLEAR.
317 Note that this filter is not supported for vnodes or BPF devices.
318 .Pp
319 For sockets, the low water mark and socket error handling is
320 identical to the EVFILT_READ case.
321 .It EVFILT_VNODE
322 Takes a file descriptor as the identifier and the events to watch for in
323 .Va fflags ,
324 and returns when one or more of the requested events occurs on the descriptor.
325 The events to monitor are:
326 .Bl -tag -width XXNOTE_RENAME
327 .It NOTE_DELETE
328 The
329 .Fn unlink
330 system call
331 was called on the file referenced by the descriptor.
332 .It NOTE_WRITE
333 A write occurred on the file referenced by the descriptor.
334 .It NOTE_EXTEND
335 The file referenced by the descriptor was extended.
336 .It NOTE_ATTRIB
337 The file referenced by the descriptor had its attributes changed.
338 .It NOTE_LINK
339 The link count on the file changed.
340 .It NOTE_RENAME
341 The file referenced by the descriptor was renamed.
342 .El
343 .Pp
344 On return,
345 .Va fflags
346 contains the events which triggered the filter.
347 .It EVFILT_SIGNAL
348 Takes the signal number to monitor as the identifier and returns
349 when the given signal is delivered to the process.
350 This overrides the
351 .Fn signal
352 and
353 .Fn sigaction
354 facilities, and has a higher precedence.
355 The filter will record
356 all attempts to deliver a signal to a process, even if the signal has
357 been marked as SIG_IGN.
358 .Va data
359 returns the number of times the signal has occurred since the last call to
360 .Fn kevent .
361 This filter automatically sets the EV_CLEAR flag internally.
362 .It EVFILT_TIMER
363 Establishes an arbitrary timer identified by
364 .Va ident .
365 When adding a timer,
366 .Va data
367 specifies the timeout period in milliseconds.
368 The timer will be periodic unless EV_ONESHOT is specified.
369 On return,
370 .Va data
371 contains the number of times the timeout has expired since the last call to
372 .Fn kevent .
373 This filter automatically sets the EV_CLEAR flag internally.
374 There is a system wide limit on the number of timers
375 which is controlled by the
376 .Va kern.kq_calloutmax
377 sysctl.
378 .It Dv EVFILT_USER
379 Establishes a user event identified by
380 .Va ident
381 which is not assosicated with any kernel mechanism but is triggered by
382 user level code.
383 The lower 24 bits of the 
384 .Va fflags
385 may be used for user defined flags and manipulated using the following:
386 .Bl -tag -width XXNOTE_FFLAGSMASK 
387 .It Dv NOTE_FFNOP
388 Ignore the input
389 .Va fflags .
390 .It Dv NOTE_FFAND
391 Bitwise AND
392 .Va fflags .
393 .It Dv NOTE_FFOR
394 Bitwise OR
395 .Va fflags .
396 .It Dv NOTE_COPY
397 Copy
398 .Va fflags .
399 .It Dv NOTE_FFCTRLMASK
400 Control mask for
401 .Va fflags .
402 .It Dv NOTE_FFLAGSMASK
403 User defined flag mask for
404 .Va fflags .
405 .El
406 .Pp
407 A user event is triggered for output with the following:
408 .Bl -tag -width XXNOTE_FFLAGSMASK
409 .It Dv NOTE_TRIGGER
410 Cause the event to be triggered.
411 .El
412 .Pp
413 On return,
414 .Va fflags
415 contains the users defined flags in the lower 24 bits.
416 .El
417 .Sh RETURN VALUES
418 The
419 .Fn kqueue
420 system call
421 creates a new kernel event queue and returns a file descriptor.
422 If there was an error creating the kernel event queue, a value of -1 is
423 returned and errno set.
424 .Pp
425 The
426 .Fn kevent
427 system call
428 returns the number of events placed in the
429 .Fa eventlist ,
430 up to the value given by
431 .Fa nevents .
432 If an error occurs while processing an element of the
433 .Fa changelist
434 and there is enough room in the
435 .Fa eventlist ,
436 then the event will be placed in the
437 .Fa eventlist
438 with
439 .Dv EV_ERROR
440 set in
441 .Va flags
442 and the system error in
443 .Va data .
444 Otherwise,
445 .Dv -1
446 will be returned, and
447 .Dv errno
448 will be set to indicate the error condition.
449 If the time limit expires, then
450 .Fn kevent
451 returns 0.
452 .Sh ERRORS
453 The
454 .Fn kqueue
455 system call fails if:
456 .Bl -tag -width Er
457 .It Bq Er ENOMEM
458 The kernel failed to allocate enough memory for the kernel queue.
459 .It Bq Er EMFILE
460 The per-process descriptor table is full.
461 .It Bq Er ENFILE
462 The system file table is full.
463 .El
464 .Pp
465 The
466 .Fn kevent
467 system call fails if:
468 .Bl -tag -width Er
469 .It Bq Er EACCES
470 The process does not have permission to register a filter.
471 .It Bq Er EFAULT
472 There was an error reading or writing the
473 .Va kevent
474 structure.
475 .It Bq Er EBADF
476 The specified descriptor is invalid.
477 .It Bq Er EINTR
478 A signal was delivered before the timeout expired and before any
479 events were placed on the kqueue for return.
480 .It Bq Er EINVAL
481 The specified time limit or filter is invalid.
482 .It Bq Er ENOENT
483 The event could not be found to be modified or deleted.
484 .It Bq Er ENOMEM
485 No memory was available to register the event
486 or, in the special case of a timer, the maximum number of
487 timers has been exceeded.
488 This maximum is configurable via the
489 .Va kern.kq_calloutmax
490 sysctl.
491 .It Bq Er ESRCH
492 The specified process to attach to does not exist.
493 .El
494 .Sh SEE ALSO
495 .Xr aio_error 2 ,
496 .Xr aio_read 2 ,
497 .Xr aio_return 2 ,
498 .Xr poll 2 ,
499 .Xr read 2 ,
500 .Xr select 2 ,
501 .Xr sigaction 2 ,
502 .Xr write 2 ,
503 .Xr signal 3
504 .Sh HISTORY
505 The
506 .Fn kqueue
507 and
508 .Fn kevent
509 system calls first appeared in
510 .Fx 4.1 .
511 .Sh AUTHORS
512 The
513 .Fn kqueue
514 system and this manual page were written by
515 .An Jonathan Lemon Aq jlemon@FreeBSD.org .