nrelease - fix/improve livecd
[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 November 22, 2017
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 .Vt 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 .Vt 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 .Vt 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 Fa 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 Fa filter
148 Identifies the kernel filter used to process this event.
149 The pre-defined system filters are described below.
150 .It Fa flags
151 Actions to perform on the event.
152 .It Fa fflags
153 Filter-specific flags.
154 .It Fa data
155 Filter-specific data value.
156 .It Fa udata
157 Opaque user-defined value passed through the kernel unchanged.
158 .El
159 .Pp
160 The
161 .Fa flags
162 field can contain the following values:
163 .Bl -tag -width ".Dv EV_DISPATCH"
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_DISPATCH
181 Disable the event source immediately after delivery of an event.
182 See
183 .Dv EV_DISABLE
184 above.
185 .It Dv EV_DELETE
186 Removes the event from the kqueue.
187 Events which are attached to file descriptors are automatically
188 deleted on the last close of the descriptor.
189 .It Dv EV_RECEIPT
190 This flag is useful for making bulk changes to a kqueue without draining
191 any pending events.
192 When passed as input, it forces
193 .Dv EV_ERROR
194 to always be returned.
195 When a filter is successfully added the
196 .Fa data
197 field will be zero.
198 .It Dv EV_ONESHOT
199 Causes the event to return only the first occurrence of the filter
200 being triggered.
201 After the user retrieves the event from the kqueue, it is deleted.
202 .It Dv EV_CLEAR
203 After the event is retrieved by the user, its state is reset.
204 This is useful for filters which report state transitions
205 instead of the current state.
206 Note that some filters may automatically set this flag internally.
207 .It Dv EV_EOF
208 Filters may set this flag to indicate filter-specific EOF condition.
209 .It Dv EV_NODATA
210 Filters may set this flag in addition to
211 .Dv EV_EOF
212 to indicate that there is no more data pending in the buffer.
213 .It Dv EV_ERROR
214 See
215 .Sx RETURN VALUES
216 below.
217 .El
218 .Pp
219 The predefined system filters are listed below.
220 Arguments may be passed to and from the filter via the
221 .Fa fflags
222 and
223 .Fa data
224 fields in the kevent structure.
225 .Bl -tag -width ".Dv EVFILT_SIGNAL"
226 .It Dv EVFILT_READ
227 Takes a descriptor as the identifier, and returns whenever
228 there is data available to read.
229 The behavior of the filter is slightly different depending
230 on the descriptor type.
231 .Bl -tag -width 2n
232 .It Sockets
233 Sockets which have previously been passed to
234 .Fn listen
235 return when there is an incoming connection pending.
236 .Fa data
237 contains the size of the listen backlog.
238 .Pp
239 Other socket descriptors return when there is data to be read,
240 subject to the
241 .Dv SO_RCVLOWAT
242 value of the socket buffer.
243 This may be overridden with a per-filter low water mark at the
244 time the filter is added by setting the
245 .Dv NOTE_LOWAT
246 flag in
247 .Fa fflags ,
248 and specifying the new low water mark in
249 .Fa data .
250 On return,
251 .Fa data
252 contains the number of bytes in the socket buffer.
253 .Pp
254 If the read direction of the socket has shutdown, then the filter also sets
255 .Dv EV_EOF
256 in
257 .Fa flags ,
258 and returns the socket error (if any) in
259 .Fa fflags .
260 It is possible for EOF to be returned (indicating the connection is gone)
261 while there is still data pending in the socket buffer.
262 .It Vnodes
263 Returns when the file pointer is not at the end of file.
264 .Fa data
265 contains the offset from current position to end of file,
266 and may be negative.
267 .It "Fifos, Pipes"
268 Returns when the there is data to read;
269 .Fa data
270 contains the number of bytes available.
271 .Pp
272 When the last writer disconnects, the filter will set
273 .Dv EV_EOF
274 in
275 .Fa flags .
276 This will be cleared by the filter when a new writer connects,
277 at which point the filter will resume waiting for data to become
278 available before returning.
279 .El
280 .It Dv EVFILT_WRITE
281 Takes a descriptor as the identifier, and returns whenever
282 it is possible to write to the descriptor.
283 For sockets, pipes and fifos,
284 .Fa data
285 will contain the amount of space remaining in the write buffer.
286 The filter will set
287 .Dv EV_EOF
288 when the reader disconnects, and for the fifo case, this will be cleared
289 when a new reader connects.
290 Note that this filter is not supported for vnodes.
291 .Pp
292 For sockets, the low water mark and socket error handling is
293 identical to the
294 .Dv EVFILT_READ
295 case.
296 .It Dv EVFILT_EXCEPT
297 Takes a descriptor as the identifier, and returns whenever one of the
298 specified exceptional conditions has occurred on the descriptor.
299 Conditions are specified in
300 .Fa fflags .
301 Currently, a filter can monitor the reception of out-of-band data with
302 .Dv NOTE_OOB .
303 .It Dv EVFILT_AIO
304 The sigevent portion of the AIO request is filled in, with
305 .Fa sigev_notify_kqueue
306 containing the descriptor of the kqueue that the event should
307 be attached to,
308 .Fa sigev_value
309 containing the udata value, and
310 .Fa sigev_notify
311 set to
312 .Dv SIGEV_KEVENT .
313 When the aio_* function is called, the event will be registered
314 with the specified kqueue, and the
315 .Fa ident
316 argument set to the
317 .Fa struct aiocb
318 returned by the aio_* function.
319 The filter returns under the same conditions as
320 .Fn aio_error .
321 .Pp
322 Alternatively, a kevent structure may be initialized, with
323 .Fa ident
324 containing the descriptor of the kqueue, and the
325 address of the kevent structure placed in the
326 .Fa aio_lio_opcode
327 field of the AIO request.
328 However, this approach will not work on architectures with 64-bit
329 pointers, and should be considered deprecated.
330 .It Dv EVFILT_VNODE
331 Takes a file descriptor as the identifier and the events to watch for in
332 .Fa fflags ,
333 and returns when one or more of the requested events occurs on the descriptor.
334 The events to monitor are:
335 .Bl -tag -width ".Dv NOTE_RENAME"
336 .It Dv NOTE_DELETE
337 .Fn unlink
338 was called on the file referenced by the descriptor.
339 .It Dv NOTE_WRITE
340 A write occurred on the file referenced by the descriptor.
341 .It Dv NOTE_EXTEND
342 The file referenced by the descriptor was extended.
343 .It Dv NOTE_ATTRIB
344 The file referenced by the descriptor had its attributes changed.
345 .It Dv NOTE_LINK
346 The link count on the file changed.
347 .It Dv NOTE_RENAME
348 The file referenced by the descriptor was renamed.
349 .It Dv NOTE_REVOKE
350 Access to the file was revoked via
351 .Xr revoke 2
352 or the underlying fileystem was unmounted.
353 .El
354 .Pp
355 On return,
356 .Fa fflags
357 contains the events which triggered the filter.
358 .It Dv EVFILT_PROC
359 Takes the process ID to monitor as the identifier and the events to watch for
360 in
361 .Fa fflags ,
362 and returns when the process performs one or more of the requested events.
363 If a process can normally see another process, it can attach an event to it.
364 The events to monitor are:
365 .Bl -tag -width ".Dv NOTE_TRACKERR"
366 .It Dv NOTE_EXIT
367 The process has exited.
368 .It Dv NOTE_FORK
369 The process has called
370 .Fn fork .
371 .It Dv NOTE_EXEC
372 The process has executed a new process via
373 .Xr execve 2
374 or similar call.
375 .It Dv NOTE_TRACK
376 Follow a process across
377 .Fn fork
378 calls.
379 The parent process will return with
380 .Dv NOTE_TRACK
381 set in the
382 .Fa fflags
383 field, while the child process will return with
384 .Dv NOTE_CHILD
385 set in
386 .Fa fflags
387 and the parent PID in
388 .Fa data .
389 .It Dv NOTE_TRACKERR
390 This flag is returned if the system was unable to attach an event to
391 the child process, usually due to resource limitations.
392 .El
393 .Pp
394 On return,
395 .Fa fflags
396 contains the events which triggered the filter.
397 .It Dv EVFILT_SIGNAL
398 Takes the signal number to monitor as the identifier and returns
399 when the given signal is delivered to the process.
400 This coexists with the
401 .Fn signal
402 and
403 .Fn sigaction
404 facilities, and has a lower precedence.
405 The filter will record all attempts to deliver a signal to a process,
406 even if the signal has been marked as
407 .Dv SIG_IGN ,
408 or has been masked by
409 .Fn sigprocmask .
410 Event notification happens after normal signal delivery processing.
411 .Fa data
412 returns the number of times the signal has occurred since the last call to
413 .Fn kevent .
414 This filter automatically sets the
415 .Dv EV_CLEAR
416 flag internally.
417 .It Dv EVFILT_TIMER
418 Establishes an arbitrary timer identified by
419 .Fa ident .
420 When adding a timer,
421 .Fa data
422 specifies the timeout period in milliseconds.
423 The timer will be periodic unless
424 .Dv EV_ONESHOT
425 is specified.
426 On return,
427 .Fa data
428 contains the number of times the timeout has expired since the last call to
429 .Fn kevent .
430 This filter automatically sets the
431 .Dv EV_CLEAR
432 flag internally.
433 .It Dv EVFILT_FS
434 Establishes a file system monitor.
435 Currently it only monitors file system mount and unmount actions.
436 .El
437 .Sh RETURN VALUES
438 .Fn kqueue
439 creates a new kernel event queue and returns a file descriptor.
440 If there was an error creating the kernel event queue, a value of -1 is
441 returned and
442 .Va errno
443 set.
444 .Pp
445 .Fn kevent
446 returns the number of events placed in the
447 .Fa eventlist ,
448 up to the value given by
449 .Fa nevents .
450 If an error occurs while processing an element of the
451 .Fa changelist
452 and there is enough room in the
453 .Fa eventlist ,
454 then the event will be placed in the
455 .Fa eventlist
456 with
457 .Dv EV_ERROR
458 set in
459 .Fa flags
460 and the system error in
461 .Fa data .
462 Otherwise,
463 .Dv -1
464 will be returned, and
465 .Va errno
466 will be set to indicate the error condition.
467 If the time limit expires, then
468 .Fn kevent
469 returns 0.
470 .Sh ERRORS
471 The
472 .Fn kqueue
473 function fails if:
474 .Bl -tag -width Er
475 .It Bq Er ENOMEM
476 The kernel failed to allocate enough memory for the kernel queue.
477 .It Bq Er EMFILE
478 The per-process descriptor table is full.
479 .It Bq Er ENFILE
480 The system file table is full.
481 .El
482 .Pp
483 The
484 .Fn kevent
485 function fails if:
486 .Bl -tag -width Er
487 .It Bq Er EACCES
488 The process does not have permission to register a filter.
489 .It Bq Er EFAULT
490 There was an error reading or writing the
491 .Vt kevent
492 structure.
493 .It Bq Er EBADF
494 The specified descriptor is invalid.
495 .It Bq Er EINTR
496 A signal was delivered before the timeout expired and before any
497 events were placed on the kqueue for return.
498 .It Bq Er EINVAL
499 The specified time limit or filter is invalid.
500 .It Bq Er ENOENT
501 The event could not be found to be modified or deleted.
502 .It Bq Er ENOMEM
503 No memory was available to register the event.
504 .It Bq Er ESRCH
505 The specified process to attach to does not exist.
506 .El
507 .Sh SEE ALSO
508 .Xr poll 2 ,
509 .Xr read 2 ,
510 .Xr select 2 ,
511 .Xr sigaction 2 ,
512 .Xr sigprocmask 2 ,
513 .Xr write 2 ,
514 .Xr aio_error 3 ,
515 .Xr aio_read 3 ,
516 .Xr aio_return 3 ,
517 .Xr signal 3
518 .Sh HISTORY
519 The
520 .Fn kqueue
521 and
522 .Fn kevent
523 functions first appeared in
524 .Fx 4.1 .
525 .Sh AUTHORS
526 The
527 .Fn kqueue
528 system and this manual page were written by
529 .An Jonathan Lemon Aq Mt jlemon@FreeBSD.org .
530 .Sh BUGS
531 Currently it is only possible to watch a
532 .Xr vnode 9
533 on
534 .Xr HAMMER 5 ,
535 .Xr tmpfs 5
536 and
537 .Xr UFS 5
538 file systems.