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