]> git.saurik.com Git - apple/xnu.git/blame - bsd/man/man2/kqueue.2
xnu-2782.30.5.tar.gz
[apple/xnu.git] / bsd / man / man2 / kqueue.2
CommitLineData
b0d623f7 1.\"
04b8595b 2.\" Copyright (c) 2008-2015 Apple Inc. All rights reserved.
b0d623f7
A
3.\"
4.\" @APPLE_LICENSE_HEADER_START@
5.\"
6.\" This file contains Original Code and/or Modifications of Original Code
7.\" as defined in and that are subject to the Apple Public Source License
8.\" Version 2.0 (the 'License'). You may not use this file except in
9.\" compliance with the License. Please obtain a copy of the License at
10.\" http://www.opensource.apple.com/apsl/ and read it before using this
11.\" file.
12.\"
13.\" The Original Code and all software distributed under the License are
14.\" distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15.\" EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16.\" INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17.\" FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18.\" Please see the License for the specific language governing rights and
19.\" limitations under the License.
20.\"
21.\" @APPLE_LICENSE_HEADER_END@
22.\"
23.\"
55e303ae
A
24.\" Copyright (c) 2000 Jonathan Lemon
25.\" All rights reserved.
26.\"
27.\" Redistribution and use in source and binary forms, with or without
28.\" modification, are permitted provided that the following conditions
29.\" are met:
30.\" 1. Redistributions of source code must retain the above copyright
31.\" notice, this list of conditions and the following disclaimer.
32.\" 2. Redistributions in binary form must reproduce the above copyright
33.\" notice, this list of conditions and the following disclaimer in the
34.\" documentation and/or other materials provided with the distribution.
35.\"
36.\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND
37.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
39.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
40.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
41.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
42.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
44.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46.\" SUCH DAMAGE.
47.\"
48.\" $FreeBSD: src/lib/libc/sys/kqueue.2,v 1.32 2002/12/19 09:40:25 ru Exp $
49.\"
b0d623f7 50.Dd October 21, 2008
55e303ae
A
51.Dt KQUEUE 2
52.Os
53.Sh NAME
54.Nm kqueue ,
b0d623f7
A
55.Nm kevent ,
56and
57.Nm kevent64
55e303ae
A
58.Nd kernel event notification mechanism
59.Sh LIBRARY
60.Lb libc
61.Sh SYNOPSIS
62.In sys/types.h
63.In sys/event.h
64.In sys/time.h
65.Ft int
66.Fn kqueue "void"
67.Ft int
68.Fn kevent "int kq" "const struct kevent *changelist" "int nchanges" "struct kevent *eventlist" "int nevents" "const struct timespec *timeout"
b0d623f7
A
69.Ft int
70.Fn kevent64 "int kq" "const struct kevent64_s *changelist" "int nchanges" "struct kevent64_s *eventlist" "int nevents" "unsigned int flags" "const struct timespec *timeout"
55e303ae 71.Fn EV_SET "&kev" ident filter flags fflags data udata
b0d623f7 72.Fn EV_SET64 "&kev" ident filter flags fflags data udata "ext[0]" "ext[1]"
55e303ae
A
73.Sh DESCRIPTION
74The
75.Fn kqueue
76system call
6d2010ae 77provides a generic method of notifying the user when a kernel
55e303ae
A
78event (kevent) happens or a condition holds, based on the results
79of small pieces of kernel code termed filters.
80A kevent is identified by an (ident, filter) pair and specifies
81the interesting conditions to be notified about for that pair.
b0d623f7 82An (ident, filter) pair can only appear once in a given kqueue.
55e303ae
A
83Subsequent attempts to register the same pair for a given kqueue
84will result in the replacement of the conditions being watched,
85not an addition.
86.Pp
87The filter identified in a kevent is executed upon the initial
88registration of that event in order to detect whether a preexisting
89condition is present, and is also executed whenever an event is
90passed to the filter for evaluation.
91If the filter determines that the condition should be reported,
92then the kevent is placed on the kqueue for the user to retrieve.
93.Pp
94The filter is also run when the user attempts to retrieve the kevent
95from the kqueue.
96If the filter indicates that the condition that triggered
97the event no longer holds, the kevent is removed from the kqueue and
98is not returned.
99.Pp
100Multiple events which trigger the filter do not result in multiple
101kevents being placed on the kqueue; instead, the filter will aggregate
102the events into a single struct kevent.
103Calling
104.Fn close
105on a file descriptor will remove any kevents that reference the descriptor.
106.Pp
107The
108.Fn kqueue
109system call
110creates a new kernel event queue and returns a descriptor.
111The queue is not inherited by a child created with
112.Xr fork 2 .
113.Pp
114The
115.Fn kevent
b0d623f7
A
116and
117.Fn kevent64
118system calls
119are used to register events with the queue, and return any pending
55e303ae
A
120events to the user.
121The
122.Fa changelist
123argument
124is a pointer to an array of
125.Va kevent
b0d623f7
A
126or
127.Va kevent64_s
55e303ae
A
128structures, as defined in
129.Aq Pa sys/event.h .
130All changes contained in the
131.Fa changelist
132are applied before any pending events are read from the queue.
133The
134.Fa nchanges
135argument
136gives the size of
137.Fa changelist .
138The
139.Fa eventlist
140argument
b0d623f7
A
141is a pointer to an array of
142.Va kevent
143or
144.Va kevent64_s
145structures.
55e303ae
A
146The
147.Fa nevents
148argument
149determines the size of
150.Fa eventlist .
151If
152.Fa timeout
153is a non-NULL pointer, it specifies a maximum interval to wait
154for an event, which will be interpreted as a struct timespec. If
155.Fa timeout
b0d623f7 156is a NULL pointer, both
55e303ae 157.Fn kevent
b0d623f7
A
158and
159.Fn kevent64
160wait indefinitely. To effect a poll, the
55e303ae
A
161.Fa timeout
162argument should be non-NULL, pointing to a zero-valued
163.Va timespec
164structure. The same array may be used for the
165.Fa changelist
166and
167.Fa eventlist .
168.Pp
169The
170.Fn EV_SET
171macro is provided for ease of initializing a
b0d623f7
A
172.Va kevent
173structure. Similarly,
174.Fn EV_SET64
175initializes a
176.Va kevent64_s
177structure.
55e303ae
A
178.Pp
179The
180.Va kevent
b0d623f7
A
181and
182.Va kevent64_s
183structures are defined as:
55e303ae
A
184.Bd -literal
185struct kevent {
b0d623f7
A
186 uintptr_t ident; /* identifier for this event */
187 int16_t filter; /* filter for event */
188 uint16_t flags; /* general flags */
189 uint32_t fflags; /* filter-specific flags */
190 intptr_t data; /* filter-specific data */
191 void *udata; /* opaque user data identifier */
192};
193
194
195struct kevent64_s {
196 uint64_t ident; /* identifier for this event */
197 int16_t filter; /* filter for event */
198 uint16_t flags; /* general flags */
199 uint32_t fflags; /* filter-specific flags */
200 int64_t data; /* filter-specific data */
201 uint64_t udata; /* opaque user data identifier */
202 uint64_t ext[2]; /* filter-specific extensions */
55e303ae
A
203};
204.Ed
205.Pp
b0d623f7
A
206----
207.Pp
55e303ae
A
208The fields of
209.Fa struct kevent
b0d623f7
A
210and
211.Fa struct kevent64_s
55e303ae
A
212are:
213.Bl -tag -width XXXfilter
214.It ident
215Value used to identify this event.
216The exact interpretation is determined by the attached filter,
217but often is a file descriptor.
218.It filter
219Identifies the kernel filter used to process this event. The pre-defined
220system filters are described below.
221.It flags
222Actions to perform on the event.
223.It fflags
224Filter-specific flags.
225.It data
226Filter-specific data value.
227.It udata
228Opaque user-defined value passed through the kernel unchanged.
229.El
230.Pp
b0d623f7
A
231In addition,
232.Fa struct kevent64_s
233contains:
234.Bl -tag -width XXXfilter
235.It ext[2]
236This field stores extensions for the event's filter. What type of extension depends on
237what type of filter is being used.
238.El
239.Pp
240----
241.Pp
55e303ae
A
242The
243.Va flags
244field can contain the following values:
245.Bl -tag -width XXXEV_ONESHOT
246.It EV_ADD
247Adds the event to the kqueue. Re-adding an existing event
248will modify the parameters of the original event, and not result
249in a duplicate entry. Adding an event automatically enables it,
250unless overridden by the EV_DISABLE flag.
251.It EV_ENABLE
252Permit
253.Fn kevent
b0d623f7
A
254and
255.Fn kevent64
55e303ae
A
256to return the event if it is triggered.
257.It EV_DISABLE
258Disable the event so
259.Fn kevent
b0d623f7
A
260and
261.Fn kevent64
55e303ae
A
262will not return it. The filter itself is not disabled.
263.It EV_DELETE
264Removes the event from the kqueue. Events which are attached to
265file descriptors are automatically deleted on the last close of
266the descriptor.
2d21ac55
A
267.It EV_RECEIPT
268This flag is useful for making bulk changes to a kqueue without draining any
269pending events. When passed as input, it forces EV_ERROR to always be returned.
6d2010ae 270When a filter is successfully added, the
2d21ac55
A
271.Va data
272field will be zero.
55e303ae
A
273.It EV_ONESHOT
274Causes the event to return only the first occurrence of the filter
275being triggered. After the user retrieves the event from the kqueue,
276it is deleted.
277.It EV_CLEAR
278After the event is retrieved by the user, its state is reset.
279This is useful for filters which report state transitions
280instead of the current state. Note that some filters may automatically
281set this flag internally.
282.It EV_EOF
283Filters may set this flag to indicate filter-specific EOF condition.
04b8595b
A
284.It EV_OOBAND
285Read filter on socket may set this flag to indicate the presence of out of
286band data on the descriptor.
55e303ae
A
287.It EV_ERROR
288See
289.Sx RETURN VALUES
290below.
291.El
292.Pp
b0d623f7
A
293----
294.Pp
55e303ae
A
295The predefined system filters are listed below.
296Arguments may be passed to and from the filter via the
297.Va fflags
298and
299.Va data
b0d623f7
A
300fields in the
301.Va kevent
302or
303.Va kevent64_s
304structure.
305.Bl -tag -width EVFILT_MACHPORT
55e303ae
A
306.It EVFILT_READ
307Takes a file descriptor as the identifier, and returns whenever
308there is data available to read.
309The behavior of the filter is slightly different depending
310on the descriptor type.
311.Pp
312.Bl -tag -width 2n
313.It Sockets
314Sockets which have previously been passed to
315.Fn listen
316return when there is an incoming connection pending.
317.Va data
318contains the size of the listen backlog.
319.Pp
320Other socket descriptors return when there is data to be read,
321subject to the
322.Dv SO_RCVLOWAT
323value of the socket buffer.
324This may be overridden with a per-filter low water mark at the
325time the filter is added by setting the
326NOTE_LOWAT
327flag in
328.Va fflags ,
329and specifying the new low water mark in
330.Va data .
331On return,
332.Va data
333contains the number of bytes of protocol data available to read.
334.Pp
04b8595b
A
335The presence of EV_OOBAND in
336.Va flags ,
337indicates the presence of out of band data on the socket
338.Va data
339equal to the potential number of OOB bytes availble to read.
340.Pp
55e303ae
A
341If the read direction of the socket has shutdown, then the filter
342also sets EV_EOF in
343.Va flags ,
344and returns the socket error (if any) in
345.Va fflags .
346It is possible for EOF to be returned (indicating the connection is gone)
347while there is still data pending in the socket buffer.
348.It Vnodes
349Returns when the file pointer is not at the end of file.
350.Va data
351contains the offset from current position to end of file,
352and may be negative.
353.It "Fifos, Pipes"
354Returns when the there is data to read;
355.Va data
356contains the number of bytes available.
357.Pp
358When the last writer disconnects, the filter will set EV_EOF in
359.Va flags .
360This may be cleared by passing in EV_CLEAR, at which point the
361filter will resume waiting for data to become available before
362returning.
363.El
364.It EVFILT_WRITE
365Takes a file descriptor as the identifier, and returns whenever
366it is possible to write to the descriptor. For sockets, pipes
367and fifos,
368.Va data
369will contain the amount of space remaining in the write buffer.
370The filter will set EV_EOF when the reader disconnects, and for
371the fifo case, this may be cleared by use of EV_CLEAR.
372Note that this filter is not supported for vnodes.
373.Pp
374For sockets, the low water mark and socket error handling is
375identical to the EVFILT_READ case.
376.It EVFILT_AIO
377This filter is currently unsupported.
378.\"The sigevent portion of the AIO request is filled in, with
379.\".Va sigev_notify_kqueue
380.\"containing the descriptor of the kqueue that the event should
381.\"be attached to,
382.\".Va sigev_value
383.\"containing the udata value, and
384.\".Va sigev_notify
385.\"set to SIGEV_KEVENT.
386.\"When the
387.\".Fn aio_*
388.\"system call is made, the event will be registered
389.\"with the specified kqueue, and the
390.\".Va ident
391.\"argument set to the
392.\".Fa struct aiocb
393.\"returned by the
394.\".Fn aio_*
395.\"system call.
396.\"The filter returns under the same conditions as aio_error.
397.\".Pp
398.\"Alternatively, a kevent structure may be initialized, with
399.\".Va ident
400.\"containing the descriptor of the kqueue, and the
401.\"address of the kevent structure placed in the
402.\".Va aio_lio_opcode
403.\"field of the AIO request. However, this approach will not work on
404.\"architectures with 64-bit pointers, and should be considered deprecated.
405.It EVFILT_VNODE
406Takes a file descriptor as the identifier and the events to watch for in
407.Va fflags ,
408and returns when one or more of the requested events occurs on the descriptor.
409The events to monitor are:
410.Bl -tag -width XXNOTE_RENAME
411.It NOTE_DELETE
412The
413.Fn unlink
414system call
415was called on the file referenced by the descriptor.
416.It NOTE_WRITE
417A write occurred on the file referenced by the descriptor.
418.It NOTE_EXTEND
419The file referenced by the descriptor was extended.
420.It NOTE_ATTRIB
421The file referenced by the descriptor had its attributes changed.
422.It NOTE_LINK
423The link count on the file changed.
424.It NOTE_RENAME
425The file referenced by the descriptor was renamed.
426.It NOTE_REVOKE
427Access to the file was revoked via
428.Xr revoke 2
429or the underlying fileystem was unmounted.
430.El
431.Pp
432On return,
433.Va fflags
434contains the events which triggered the filter.
435.It EVFILT_PROC
436Takes the process ID to monitor as the identifier and the events to watch for
437in
438.Va fflags ,
439and returns when the process performs one or more of the requested events.
440If a process can normally see another process, it can attach an event to it.
441The events to monitor are:
b0d623f7 442.Bl -tag -width NOTE_SIGNAL
55e303ae
A
443.It NOTE_EXIT
444The process has exited.
6d2010ae
A
445.It NOTE_EXITSTATUS
446The process has exited and its exit status is in filter specific data. Valid only on child processes and to be used along with NOTE_EXIT.
55e303ae 447.It NOTE_FORK
2d21ac55
A
448The process created a child process via
449.Xr fork 2
450or similar call.
55e303ae 451.It NOTE_EXEC
2d21ac55 452The process executed a new process via
55e303ae
A
453.Xr execve 2
454or similar call.
2d21ac55
A
455.It NOTE_SIGNAL
456The process was sent a signal. Status can be checked via
457.Xr waitpid 2
458or similar call.
459.It NOTE_REAP
460The process was reaped by the parent via
461.Xr wait 2
39236c6e 462or similar call. Deprecated, use NOTE_EXIT.
55e303ae
A
463.El
464.Pp
465On return,
466.Va fflags
467contains the events which triggered the filter.
468.It EVFILT_SIGNAL
469Takes the signal number to monitor as the identifier and returns
b0d623f7 470when the given signal is generated for the process.
55e303ae
A
471This coexists with the
472.Fn signal
473and
474.Fn sigaction
b0d623f7
A
475facilities, and has a lower precedence. Only signals sent to the process,
476not to a particular thread, will trigger the filter. The filter will record
55e303ae 477all attempts to deliver a signal to a process, even if the signal has
b0d623f7 478been marked as SIG_IGN. Event notification happens before normal
55e303ae
A
479signal delivery processing.
480.Va data
b0d623f7 481returns the number of times the signal has been generated since the last call to
55e303ae
A
482.Fn kevent .
483This filter automatically sets the EV_CLEAR flag internally.
b0d623f7
A
484.It EVFILT_MACHPORT
485Takes the name of a mach port, or port set, in
486.Va ident
487and waits until a message is received on the port or port set. When a message
488is recieved, the size of the message is returned in
489.Va data
490and if
491.Va fflags
492is set to MACH_RCV_MSG, a pointer to the message is returned in ext[0].
55e303ae 493.It EVFILT_TIMER
b0d623f7
A
494Establishes an interval timer with the data
495timer identified by
496.Va ident .
497When adding a timer,
498.Va data
499specifies the timeout period and
500.Va fflags
501can be set to one of the following:
502.Bl -tag -width NOTE_ABSOLUTE
503.It NOTE_SECONDS
504data is in seconds
505.It NOTE_USECONDS
506data is in microseconds
507.It NOTE_NSECONDS
508data is in nanoseconds
509.It NOTE_ABSOLUTE
510data is an absolute timeout
39236c6e
A
511.It NOTE_CRITICAL
512system makes a best effort to fire this timer as scheduled.
513.It NOTE_BACKGROUND
514system has extra leeway to coalesce this timer.
515.It NOTE_LEEWAY
516ext[1] holds user-supplied slop in deadline for timer coalescing.
b0d623f7
A
517.El
518.Pp
519If fflags is not set, the default is milliseconds. The timer will be periodic unless EV_ONESHOT is specified.
520On return,
521.Va data
522contains the number of times the timeout has expired since the last call to
523.Fn kevent
524or
525.Fn kevent64 .
526This filter automatically sets the EV_CLEAR flag internally.
b0d623f7
A
527.El
528.Pp
529----
530.Pp
531In the
532.Va ext[2]
533field of the
534.Va kevent64_s
535struture,
536.Va ext[0]
537is only used with the EVFILT_MACHPORT filter.
538With other filters,
539.Va ext[0]
540is passed through
541.Fn kevent64
542much like
543.Va udata .
544.Va ext[1]
545can always be used like
546.Va udata .
547For the use of ext[0], see the EVFILT_MACHPORT filter above.
55e303ae
A
548.Sh RETURN VALUES
549The
550.Fn kqueue
551system call
552creates a new kernel event queue and returns a file descriptor.
553If there was an error creating the kernel event queue, a value of -1 is
554returned and errno set.
555.Pp
556The
557.Fn kevent
b0d623f7
A
558and
559.Fn kevent64
560system calls
561return the number of events placed in the
55e303ae
A
562.Fa eventlist ,
563up to the value given by
564.Fa nevents .
565If an error occurs while processing an element of the
566.Fa changelist
567and there is enough room in the
568.Fa eventlist ,
569then the event will be placed in the
570.Fa eventlist
571with
572.Dv EV_ERROR
573set in
574.Va flags
575and the system error in
576.Va data .
577Otherwise,
578.Dv -1
579will be returned, and
580.Dv errno
581will be set to indicate the error condition.
582If the time limit expires, then
583.Fn kevent
b0d623f7
A
584and
585.Fn kevent64
586return 0.
55e303ae
A
587.Sh ERRORS
588The
589.Fn kqueue
590system call fails if:
591.Bl -tag -width Er
592.It Bq Er ENOMEM
593The kernel failed to allocate enough memory for the kernel queue.
594.It Bq Er EMFILE
595The per-process descriptor table is full.
596.It Bq Er ENFILE
597The system file table is full.
598.El
599.Pp
600The
601.Fn kevent
b0d623f7
A
602and
603.Fn kevent64
604system calls fail if:
55e303ae
A
605.Bl -tag -width Er
606.It Bq Er EACCES
607The process does not have permission to register a filter.
608.It Bq Er EFAULT
609There was an error reading or writing the
610.Va kevent
b0d623f7
A
611or
612.Va kevent64_s
55e303ae
A
613structure.
614.It Bq Er EBADF
615The specified descriptor is invalid.
616.It Bq Er EINTR
617A signal was delivered before the timeout expired and before any
618events were placed on the kqueue for return.
619.It Bq Er EINVAL
620The specified time limit or filter is invalid.
621.It Bq Er ENOENT
622The event could not be found to be modified or deleted.
623.It Bq Er ENOMEM
624No memory was available to register the event.
625.It Bq Er ESRCH
626The specified process to attach to does not exist.
627.El
628.Sh SEE ALSO
629.Xr aio_error 2 ,
630.Xr aio_read 2 ,
631.Xr aio_return 2 ,
632.Xr read 2 ,
633.Xr select 2 ,
634.Xr sigaction 2 ,
635.Xr write 2 ,
636.Xr signal 3
637.Sh HISTORY
638The
639.Fn kqueue
640and
641.Fn kevent
642system calls first appeared in
643.Fx 4.1 .
644.Sh AUTHORS
645The
646.Fn kqueue
647system and this manual page were written by
648.An Jonathan Lemon Aq jlemon@FreeBSD.org .
649.Sh BUGS
650Not all filesystem types support kqueue-style notifications.
651And even some that do, like some remote filesystems, may only
652support a subset of the notification semantics described
653here.