]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/sys/kqueue.2
libc: simplify MDASM/NOASM checks
[FreeBSD/FreeBSD.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 .Dd March 26, 2023
26 .Dt KQUEUE 2
27 .Os
28 .Sh NAME
29 .Nm kqueue ,
30 .Nm kevent
31 .Nd kernel event notification mechanism
32 .Sh LIBRARY
33 .Lb libc
34 .Sh SYNOPSIS
35 .In sys/event.h
36 .Ft int
37 .Fn kqueue "void"
38 .Ft int
39 .Fn kqueuex "u_int flags"
40 .Ft int
41 .Fo kevent
42 .Fa "int kq"
43 .Fa "const struct kevent *changelist"
44 .Fa "int nchanges"
45 .Fa "struct kevent *eventlist"
46 .Fa "int nevents"
47 .Fa "const struct timespec *timeout"
48 .Fc
49 .Fn EV_SET "kev" ident filter flags fflags data udata
50 .Sh DESCRIPTION
51 The
52 .Fn kqueue
53 system call
54 provides a generic method of notifying the user when an event
55 happens or a condition holds, based on the results of small
56 pieces of kernel code termed filters.
57 A kevent is identified by the (ident, filter) pair; there may only
58 be one unique kevent per kqueue.
59 .Pp
60 The filter is executed upon the initial registration of a kevent
61 in order to detect whether a preexisting condition is present, and is also
62 executed whenever an event is passed to the filter for evaluation.
63 If the filter determines that the condition should be reported,
64 then the kevent is placed on the kqueue for the user to retrieve.
65 .Pp
66 The filter is also run when the user attempts to retrieve the kevent
67 from the kqueue.
68 If the filter indicates that the condition that triggered
69 the event no longer holds, the kevent is removed from the kqueue and
70 is not returned.
71 .Pp
72 Multiple events which trigger the filter do not result in multiple
73 kevents being placed on the kqueue; instead, the filter will aggregate
74 the events into a single struct kevent.
75 Calling
76 .Fn close
77 on a file descriptor will remove any kevents that reference the descriptor.
78 .Pp
79 The
80 .Fn kqueue
81 system call
82 creates a new kernel event queue and returns a descriptor.
83 The queue is not inherited by a child created with
84 .Xr fork 2 .
85 However, if
86 .Xr rfork 2
87 is called without the
88 .Dv RFFDG
89 flag, then the descriptor table is shared,
90 which will allow sharing of the kqueue between two processes.
91 .Pp
92 The
93 .Fn kqueuex
94 system call also creates a new kernel event queue, and additionally takes
95 the
96 .Fa flags
97 argument, which is a bitwise-inclusive OR of the following flags:
98 .Bl -tag -width "KQUEUE_CLOEXEC"
99 .It Fa KQUEUE_CLOEXEC
100 The returned file descriptor is automatically closed on
101 .Xr execve 2
102 .El
103 The
104 .Ql fd = kqueue()
105 call is equivalent to
106 .Ql fd = kqueuex(0) .
107 .Pp
108 For compatibility with
109 .Nx ,
110 the
111 .Fn kqueue1
112 function is provided, which accepts the
113 .Dv O_CLOEXEC
114 flag with the expected semantic.
115 .Pp
116 The
117 .Fn kevent
118 system call
119 is used to register events with the queue, and return any pending
120 events to the user.
121 The
122 .Fa changelist
123 argument
124 is a pointer to an array of
125 .Va kevent
126 structures, as defined in
127 .In sys/event.h .
128 All changes contained in the
129 .Fa changelist
130 are applied before any pending events are read from the queue.
131 The
132 .Fa nchanges
133 argument
134 gives the size of
135 .Fa changelist .
136 The
137 .Fa eventlist
138 argument
139 is a pointer to an array of kevent structures.
140 The
141 .Fa nevents
142 argument
143 determines the size of
144 .Fa eventlist .
145 When
146 .Fa nevents
147 is zero,
148 .Fn kevent
149 will return immediately even if there is a
150 .Fa timeout
151 specified unlike
152 .Xr select 2 .
153 If
154 .Fa timeout
155 is a non-NULL pointer, it specifies a maximum interval to wait
156 for an event, which will be interpreted as a struct timespec.
157 If
158 .Fa timeout
159 is a NULL pointer,
160 .Fn kevent
161 waits indefinitely.
162 To effect a poll, the
163 .Fa timeout
164 argument should be non-NULL, pointing to a zero-valued
165 .Va timespec
166 structure.
167 The same array may be used for the
168 .Fa changelist
169 and
170 .Fa eventlist .
171 .Pp
172 The
173 .Fn EV_SET
174 macro is provided for ease of initializing a
175 kevent structure.
176 .Pp
177 The
178 .Va kevent
179 structure is defined as:
180 .Bd -literal
181 struct kevent {
182         uintptr_t  ident;       /* identifier for this event */
183         short     filter;       /* filter for event */
184         u_short   flags;        /* action flags for kqueue */
185         u_int     fflags;       /* filter flag value */
186         int64_t   data;         /* filter data value */
187         void      *udata;       /* opaque user data identifier */
188         uint64_t  ext[4];       /* extensions */
189 };
190 .Ed
191 .Pp
192 The fields of
193 .Fa struct kevent
194 are:
195 .Bl -tag -width "Fa filter"
196 .It Fa ident
197 Value used to identify this event.
198 The exact interpretation is determined by the attached filter,
199 but often is a file descriptor.
200 .It Fa filter
201 Identifies the kernel filter used to process this event.
202 The pre-defined
203 system filters are described below.
204 .It Fa flags
205 Actions to perform on the event.
206 .It Fa fflags
207 Filter-specific flags.
208 .It Fa data
209 Filter-specific data value.
210 .It Fa udata
211 Opaque user-defined value passed through the kernel unchanged.
212 .It Fa ext
213 Extended data passed to and from kernel.
214 The
215 .Fa ext[0]
216 and
217 .Fa ext[1]
218 members use is defined by the filter.
219 If the filter does not use them, the members are copied unchanged.
220 The
221 .Fa ext[2]
222 and
223 .Fa ext[3]
224 members are always passed through the kernel as-is,
225 making additional context available to application.
226 .El
227 .Pp
228 The
229 .Va flags
230 field can contain the following values:
231 .Bl -tag -width EV_DISPATCH
232 .It Dv EV_ADD
233 Adds the event to the kqueue.
234 Re-adding an existing event
235 will modify the parameters of the original event, and not result
236 in a duplicate entry.
237 Adding an event automatically enables it,
238 unless overridden by the EV_DISABLE flag.
239 .It Dv EV_ENABLE
240 Permit
241 .Fn kevent
242 to return the event if it is triggered.
243 .It Dv EV_DISABLE
244 Disable the event so
245 .Fn kevent
246 will not return it.
247 The filter itself is not disabled.
248 .It Dv EV_DISPATCH
249 Disable the event source immediately after delivery of an event.
250 See
251 .Dv EV_DISABLE
252 above.
253 .It Dv EV_DELETE
254 Removes the event from the kqueue.
255 Events which are attached to
256 file descriptors are automatically deleted on the last close of
257 the descriptor.
258 .It Dv EV_RECEIPT
259 This flag is useful for making bulk changes to a kqueue without draining
260 any pending events.
261 When passed as input, it forces
262 .Dv EV_ERROR
263 to always be returned.
264 When a filter is successfully added the
265 .Va data
266 field will be zero.
267 Note that if this flag is encountered and there is no remaining space in
268 .Fa eventlist
269 to hold the
270 .Dv EV_ERROR
271 event, then subsequent changes will not get processed.
272 .It Dv EV_ONESHOT
273 Causes the event to return only the first occurrence of the filter
274 being triggered.
275 After the user retrieves the event from the kqueue,
276 it is deleted.
277 .It Dv EV_CLEAR
278 After the event is retrieved by the user, its state is reset.
279 This is useful for filters which report state transitions
280 instead of the current state.
281 Note that some filters may automatically
282 set this flag internally.
283 .It Dv EV_EOF
284 Filters may set this flag to indicate filter-specific EOF condition.
285 .It Dv EV_ERROR
286 See
287 .Sx RETURN VALUES
288 below.
289 .It Dv EV_KEEPUDATA
290 Causes
291 .Fn kevent
292 to leave unchanged any
293 .Fa udata
294 associated with an existing event.
295 This allows other aspects of the event to be modified without requiring the
296 caller to know the
297 .Fa udata
298 value presently associated.
299 This is especially useful with
300 .Dv NOTE_TRIGGER
301 or flags like
302 .Dv EV_ENABLE .
303 This flag may not be used with
304 .Dv EV_ADD .
305 .El
306 .Pp
307 The predefined system filters are listed below.
308 Arguments may be passed to and from the filter via the
309 .Va fflags
310 and
311 .Va data
312 fields in the kevent structure.
313 .Bl -tag -width "Dv EVFILT_PROCDESC"
314 .It Dv EVFILT_READ
315 Takes a descriptor as the identifier, and returns whenever
316 there is data available to read.
317 The behavior of the filter is slightly different depending
318 on the descriptor type.
319 .Bl -tag -width 2n
320 .It Sockets
321 Sockets which have previously been passed to
322 .Xr listen 2
323 return when there is an incoming connection pending.
324 .Va data
325 contains the size of the listen backlog.
326 .Pp
327 Other socket descriptors return when there is data to be read,
328 subject to the
329 .Dv SO_RCVLOWAT
330 value of the socket buffer.
331 This may be overridden with a per-filter low water mark at the
332 time the filter is added by setting the
333 .Dv NOTE_LOWAT
334 flag in
335 .Va fflags ,
336 and specifying the new low water mark in
337 .Va data .
338 On return,
339 .Va data
340 contains the number of bytes of protocol data available to read.
341 .Pp
342 If the read direction of the socket has shutdown, then the filter
343 also sets
344 .Dv EV_EOF
345 in
346 .Va flags ,
347 and returns the socket error (if any) in
348 .Va fflags .
349 It is possible for EOF to be returned (indicating the connection is gone)
350 while there is still data pending in the socket buffer.
351 .It Vnodes
352 Returns when the file pointer is not at the end of file.
353 .Va data
354 contains the offset from current position to end of file,
355 and may be negative.
356 .Pp
357 This behavior is different from
358 .Xr poll 2 ,
359 where read events are triggered for regular files unconditionally.
360 This event can be triggered unconditionally by setting the
361 .Dv NOTE_FILE_POLL
362 flag in
363 .Va fflags .
364 .It "Fifos, Pipes"
365 Returns when the there is data to read;
366 .Va data
367 contains the number of bytes available.
368 .Pp
369 When the last writer disconnects, the filter will set
370 .Dv EV_EOF
371 in
372 .Va flags .
373 This will be cleared by the filter when a new writer connects,
374 at which point the
375 filter will resume waiting for data to become available before
376 returning.
377 .It "BPF devices"
378 Returns when the BPF buffer is full, the BPF timeout has expired, or
379 when the BPF has
380 .Dq immediate mode
381 enabled and there is any data to read;
382 .Va data
383 contains the number of bytes available.
384 .It Eventfds
385 Returns when the counter is greater than 0;
386 .Va data
387 contains the counter value, which must be cast to
388 .Vt uint64_t .
389 .It Kqueues
390 Returns when pending events are present on the queue;
391 .Va data
392 contains the number of events available.
393 .El
394 .It Dv EVFILT_WRITE
395 Takes a descriptor as the identifier, and returns whenever
396 it is possible to write to the descriptor.
397 For sockets, pipes
398 and fifos,
399 .Va data
400 will contain the amount of space remaining in the write buffer.
401 The filter will set
402 .Dv EV_EOF
403 when the reader disconnects, and for the fifo case, this will be cleared
404 when a new reader connects.
405 Note that this filter is not supported for vnodes.
406 .Pp
407 For sockets, the low water mark and socket error handling is
408 identical to the
409 .Dv EVFILT_READ
410 case.
411 .Pp
412 For eventfds,
413 .Va data
414 will contain the maximum value that can be added to the counter
415 without blocking.
416 .Pp
417 For BPF devices, when the descriptor is attached to an interface the filter
418 always indicates that it is possible to write and
419 .Va data
420 will contain the MTU size of the underlying interface.
421 .It Dv EVFILT_EMPTY
422 Takes a descriptor as the identifier, and returns whenever
423 there is no remaining data in the write buffer.
424 .It Dv EVFILT_AIO
425 Events for this filter are not registered with
426 .Fn kevent
427 directly but are registered via the
428 .Va aio_sigevent
429 member of an asynchronous I/O request when it is scheduled via an
430 asynchronous I/O system call such as
431 .Fn aio_read .
432 The filter returns under the same conditions as
433 .Fn aio_error .
434 For more details on this filter see
435 .Xr sigevent 3 and
436 .Xr aio 4 .
437 .It Dv EVFILT_VNODE
438 Takes a file descriptor as the identifier and the events to watch for in
439 .Va fflags ,
440 and returns when one or more of the requested events occurs on the descriptor.
441 The events to monitor are:
442 .Bl -tag -width "Dv NOTE_CLOSE_WRITE"
443 .It Dv NOTE_ATTRIB
444 The file referenced by the descriptor had its attributes changed.
445 .It Dv NOTE_CLOSE
446 A file descriptor referencing the monitored file, was closed.
447 The closed file descriptor did not have write access.
448 .It Dv NOTE_CLOSE_WRITE
449 A file descriptor referencing the monitored file, was closed.
450 The closed file descriptor had write access.
451 .Pp
452 This note, as well as
453 .Dv NOTE_CLOSE ,
454 are not activated when files are closed forcibly by
455 .Xr unmount 2 or
456 .Xr revoke 2 .
457 Instead,
458 .Dv NOTE_REVOKE
459 is sent for such events.
460 .It Dv NOTE_DELETE
461 The
462 .Fn unlink
463 system call was called on the file referenced by the descriptor.
464 .It Dv NOTE_EXTEND
465 For regular file, the file referenced by the descriptor was extended.
466 .Pp
467 For directory, reports that a directory entry was added or removed,
468 as the result of rename operation.
469 The
470 .Dv NOTE_EXTEND
471 event is not reported when a name is changed inside the directory.
472 .It Dv NOTE_LINK
473 The link count on the file changed.
474 In particular, the
475 .Dv NOTE_LINK
476 event is reported if a subdirectory was created or deleted inside
477 the directory referenced by the descriptor.
478 .It Dv NOTE_OPEN
479 The file referenced by the descriptor was opened.
480 .It Dv NOTE_READ
481 A read occurred on the file referenced by the descriptor.
482 .It Dv NOTE_RENAME
483 The file referenced by the descriptor was renamed.
484 .It Dv NOTE_REVOKE
485 Access to the file was revoked via
486 .Xr revoke 2
487 or the underlying file system was unmounted.
488 .It Dv NOTE_WRITE
489 A write occurred on the file referenced by the descriptor.
490 .El
491 .Pp
492 On return,
493 .Va fflags
494 contains the events which triggered the filter.
495 .It Dv EVFILT_PROC
496 Takes the process ID to monitor as the identifier and the events to watch for
497 in
498 .Va fflags ,
499 and returns when the process performs one or more of the requested events.
500 If a process can normally see another process, it can attach an event to it.
501 The events to monitor are:
502 .Bl -tag -width "Dv NOTE_TRACKERR"
503 .It Dv NOTE_EXIT
504 The process has exited.
505 The exit status will be stored in
506 .Va data
507 in the same format as the status returned by
508 .Xr wait 2 .
509 .It Dv NOTE_FORK
510 The process has called
511 .Fn fork .
512 .It Dv NOTE_EXEC
513 The process has executed a new process via
514 .Xr execve 2
515 or a similar call.
516 .It Dv NOTE_TRACK
517 Follow a process across
518 .Fn fork
519 calls.
520 The parent process registers a new kevent to monitor the child process
521 using the same
522 .Va fflags
523 as the original event.
524 The child process will signal an event with
525 .Dv NOTE_CHILD
526 set in
527 .Va fflags
528 and the parent PID in
529 .Va data .
530 .Pp
531 If the parent process fails to register a new kevent
532 .Pq usually due to resource limitations ,
533 it will signal an event with
534 .Dv NOTE_TRACKERR
535 set in
536 .Va fflags ,
537 and the child process will not signal a
538 .Dv NOTE_CHILD
539 event.
540 .El
541 .Pp
542 On return,
543 .Va fflags
544 contains the events which triggered the filter.
545 .It Dv EVFILT_PROCDESC
546 Takes the process descriptor created by
547 .Xr pdfork 2
548 to monitor as the identifier and the events to watch for in
549 .Va fflags ,
550 and returns when the associated process performs one or more of the
551 requested events.
552 The events to monitor are:
553 .Bl -tag -width "Dv NOTE_EXIT"
554 .It Dv NOTE_EXIT
555 The process has exited.
556 The exit status will be stored in
557 .Va data .
558 .El
559 .Pp
560 On return,
561 .Va fflags
562 contains the events which triggered the filter.
563 .It Dv EVFILT_SIGNAL
564 Takes the signal number to monitor as the identifier and returns
565 when the given signal is delivered to the process.
566 This coexists with the
567 .Fn signal
568 and
569 .Fn sigaction
570 facilities, and has a lower precedence.
571 The filter will record
572 all attempts to deliver a signal to a process, even if the signal has
573 been marked as
574 .Dv SIG_IGN ,
575 except for the
576 .Dv SIGCHLD
577 signal, which, if ignored, will not be recorded by the filter.
578 Event notification happens after normal
579 signal delivery processing.
580 .Va data
581 returns the number of times the signal has occurred since the last call to
582 .Fn kevent .
583 This filter automatically sets the
584 .Dv EV_CLEAR
585 flag internally.
586 .It Dv EVFILT_TIMER
587 Establishes an arbitrary timer identified by
588 .Va ident .
589 When adding a timer,
590 .Va data
591 specifies the moment to fire the timer (for
592 .Dv NOTE_ABSTIME )
593 or the timeout period.
594 The timer will be periodic unless
595 .Dv EV_ONESHOT
596 or
597 .Dv NOTE_ABSTIME
598 is specified.
599 On return,
600 .Va data
601 contains the number of times the timeout has expired since the last call to
602 .Fn kevent .
603 For non-monotonic timers, this filter automatically sets the
604 .Dv EV_CLEAR
605 flag internally.
606 .Pp
607 The filter accepts the following flags in the
608 .Va fflags
609 argument:
610 .Bl -tag -width "Dv NOTE_MSECONDS"
611 .It Dv NOTE_SECONDS
612 .Va data
613 is in seconds.
614 .It Dv NOTE_MSECONDS
615 .Va data
616 is in milliseconds.
617 .It Dv NOTE_USECONDS
618 .Va data
619 is in microseconds.
620 .It Dv NOTE_NSECONDS
621 .Va data
622 is in nanoseconds.
623 .It Dv NOTE_ABSTIME
624 The specified expiration time is absolute.
625 .El
626 .Pp
627 If
628 .Va fflags
629 is not set, the default is milliseconds.
630 On return,
631 .Va fflags
632 contains the events which triggered the filter.
633 .Pp
634 Periodic timers with a specified timeout of 0 will be silently adjusted to
635 timeout after 1 of the time units specified by the requested precision in
636 .Va fflags .
637 If an absolute time is specified that has already passed, then it is treated as
638 if the current time were specified and the event will fire as soon as possible.
639 .Pp
640 If an existing timer is re-added, the existing timer will be
641 effectively canceled (throwing away any undelivered record of previous
642 timer expiration) and re-started using the new parameters contained in
643 .Va data
644 and
645 .Va fflags .
646 .Pp
647 There is a system wide limit on the number of timers
648 which is controlled by the
649 .Va kern.kq_calloutmax
650 sysctl.
651 .It Dv EVFILT_USER
652 Establishes a user event identified by
653 .Va ident
654 which is not associated with any kernel mechanism but is triggered by
655 user level code.
656 The lower 24 bits of the
657 .Va fflags
658 may be used for user defined flags and manipulated using the following:
659 .Bl -tag -width "Dv NOTE_FFLAGSMASK"
660 .It Dv NOTE_FFNOP
661 Ignore the input
662 .Va fflags .
663 .It Dv NOTE_FFAND
664 Bitwise AND
665 .Va fflags .
666 .It Dv NOTE_FFOR
667 Bitwise OR
668 .Va fflags .
669 .It Dv NOTE_FFCOPY
670 Copy
671 .Va fflags .
672 .It Dv NOTE_FFCTRLMASK
673 Control mask for
674 .Va fflags .
675 .It Dv NOTE_FFLAGSMASK
676 User defined flag mask for
677 .Va fflags .
678 .El
679 .Pp
680 A user event is triggered for output with the following:
681 .Bl -tag -width "Dv NOTE_FFLAGSMASK"
682 .It Dv NOTE_TRIGGER
683 Cause the event to be triggered.
684 .El
685 .Pp
686 On return,
687 .Va fflags
688 contains the users defined flags in the lower 24 bits.
689 .El
690 .Sh CANCELLATION BEHAVIOUR
691 If
692 .Fa nevents
693 is non-zero, i.e., the function is potentially blocking, the call
694 is a cancellation point.
695 Otherwise, i.e., if
696 .Fa nevents
697 is zero, the call is not cancellable.
698 Cancellation can only occur before any changes are made to the kqueue,
699 or when the call was blocked and no changes to the queue were requested.
700 .Sh RETURN VALUES
701 The
702 .Fn kqueue
703 system call
704 creates a new kernel event queue and returns a file descriptor.
705 If there was an error creating the kernel event queue, a value of -1 is
706 returned and errno set.
707 .Pp
708 The
709 .Fn kevent
710 system call
711 returns the number of events placed in the
712 .Fa eventlist ,
713 up to the value given by
714 .Fa nevents .
715 If an error occurs while processing an element of the
716 .Fa changelist
717 and there is enough room in the
718 .Fa eventlist ,
719 then the event will be placed in the
720 .Fa eventlist
721 with
722 .Dv EV_ERROR
723 set in
724 .Va flags
725 and the system error in
726 .Va data .
727 Otherwise,
728 .Dv -1
729 will be returned, and
730 .Dv errno
731 will be set to indicate the error condition.
732 If the time limit expires, then
733 .Fn kevent
734 returns 0.
735 .Sh EXAMPLES
736 .Bd -literal -compact
737 #include <sys/event.h>
738 #include <err.h>
739 #include <fcntl.h>
740 #include <stdio.h>
741 #include <stdlib.h>
742 #include <string.h>
743
744 int
745 main(int argc, char **argv)
746 {
747     struct kevent event;    /* Event we want to monitor */
748     struct kevent tevent;   /* Event triggered */
749     int kq, fd, ret;
750
751     if (argc != 2)
752         err(EXIT_FAILURE, "Usage: %s path\en", argv[0]);
753     fd = open(argv[1], O_RDONLY);
754     if (fd == -1)
755         err(EXIT_FAILURE, "Failed to open '%s'", argv[1]);
756
757     /* Create kqueue. */
758     kq = kqueue();
759     if (kq == -1)
760         err(EXIT_FAILURE, "kqueue() failed");
761
762     /* Initialize kevent structure. */
763     EV_SET(&event, fd, EVFILT_VNODE, EV_ADD | EV_CLEAR, NOTE_WRITE,
764         0, NULL);
765     /* Attach event to the kqueue. */
766     ret = kevent(kq, &event, 1, NULL, 0, NULL);
767     if (ret == -1)
768         err(EXIT_FAILURE, "kevent register");
769
770     for (;;) {
771         /* Sleep until something happens. */
772         ret = kevent(kq, NULL, 0, &tevent, 1, NULL);
773         if (ret == -1) {
774             err(EXIT_FAILURE, "kevent wait");
775         } else if (ret > 0) {
776             if (tevent.flags & EV_ERROR)
777                 errx(EXIT_FAILURE, "Event error: %s", strerror(event.data));
778             else
779                 printf("Something was written in '%s'\en", argv[1]);
780         }
781     }
782
783     /* kqueues are destroyed upon close() */
784     (void)close(kq);
785     (void)close(fd);
786 }
787 .Ed
788 .Sh ERRORS
789 The
790 .Fn kqueue
791 system call fails if:
792 .Bl -tag -width Er
793 .It Bq Er ENOMEM
794 The kernel failed to allocate enough memory for the kernel queue.
795 .It Bq Er ENOMEM
796 The
797 .Dv RLIMIT_KQUEUES
798 rlimit
799 (see
800 .Xr getrlimit 2 )
801 for the current user would be exceeded.
802 .It Bq Er EMFILE
803 The per-process descriptor table is full.
804 .It Bq Er ENFILE
805 The system file table is full.
806 .El
807 .Pp
808 The
809 .Fn kevent
810 system call fails if:
811 .Bl -tag -width Er
812 .It Bq Er EACCES
813 The process does not have permission to register a filter.
814 .It Bq Er EFAULT
815 There was an error reading or writing the
816 .Va kevent
817 structure.
818 .It Bq Er EBADF
819 The specified descriptor is invalid.
820 .It Bq Er EINTR
821 A signal was delivered before the timeout expired and before any
822 events were placed on the kqueue for return.
823 .It Bq Er EINTR
824 A cancellation request was delivered to the thread, but not yet handled.
825 .It Bq Er EINVAL
826 The specified time limit or filter is invalid.
827 .It Bq Er EINVAL
828 The specified length of the event or change lists is negative.
829 .It Bq Er ENOENT
830 The event could not be found to be modified or deleted.
831 .It Bq Er ENOMEM
832 No memory was available to register the event
833 or, in the special case of a timer, the maximum number of
834 timers has been exceeded.
835 This maximum is configurable via the
836 .Va kern.kq_calloutmax
837 sysctl.
838 .It Bq Er ESRCH
839 The specified process to attach to does not exist.
840 .El
841 .Pp
842 When
843 .Fn kevent
844 call fails with
845 .Er EINTR
846 error, all changes in the
847 .Fa changelist
848 have been applied.
849 .Sh SEE ALSO
850 .Xr aio_error 2 ,
851 .Xr aio_read 2 ,
852 .Xr aio_return 2 ,
853 .Xr poll 2 ,
854 .Xr read 2 ,
855 .Xr select 2 ,
856 .Xr sigaction 2 ,
857 .Xr write 2 ,
858 .Xr pthread_setcancelstate 3 ,
859 .Xr signal 3
860 .Rs
861 .%A Jonathan Lemon
862 .%T "Kqueue: A Generic and Scalable Event Notification Facility"
863 .%I USENIX Association
864 .%B Proceedings of the FREENIX Track: 2001 USENIX Annual Technical Conference
865 .%D June 25-30, 2001
866 .\".http://www.usenix.org/event/usenix01/freenix01/full_papers/lemon/lemon.pdf
867 .Re
868 .Sh HISTORY
869 The
870 .Fn kqueue
871 and
872 .Fn kevent
873 system calls first appeared in
874 .Fx 4.1 .
875 .Sh AUTHORS
876 The
877 .Fn kqueue
878 system and this manual page were written by
879 .An Jonathan Lemon Aq Mt jlemon@FreeBSD.org .
880 .Sh BUGS
881 .Pp
882 In versions older than
883 .Fx 12.0 ,
884 .In sys/event.h
885 failed to parse without including
886 .In sys/types.h
887 manually.