|
Lines 577-582
Link Here
|
| 577 |
If the time limit expires, then |
577 |
If the time limit expires, then |
| 578 |
.Fn kevent |
578 |
.Fn kevent |
| 579 |
returns 0. |
579 |
returns 0. |
|
|
580 |
.Sh EXAMPLES |
| 581 |
.Bd -literal -compact |
| 582 |
#include <sys/types.h> |
| 583 |
#include <sys/event.h> |
| 584 |
#include <sys/time.h> |
| 585 |
#include <err.h> |
| 586 |
#include <fcntl.h> |
| 587 |
#include <stdio.h> |
| 588 |
#include <stdlib.h> |
| 589 |
#include <string.h> |
| 590 |
#include <unistd.h> |
| 591 |
|
| 592 |
int |
| 593 |
main(int argc, char **argv) |
| 594 |
{ |
| 595 |
|
| 596 |
struct kevent event; /* Event we want to monitor */ |
| 597 |
struct kevent tevent; /* Event triggered */ |
| 598 |
int kq, fd, ret; |
| 599 |
|
| 600 |
if (argc != 2) { |
| 601 |
err(EXIT_FAILURE, "Usage: %s path\en", argv[0]); |
| 602 |
} |
| 603 |
|
| 604 |
if ((fd = open(argv[1], O_RDONLY)) == -1) { |
| 605 |
err(EXIT_FAILURE, "Failed to open '%s'", argv[1]); |
| 606 |
} |
| 607 |
|
| 608 |
/* Create kqueue */ |
| 609 |
if ((kq = kqueue()) == -1) { |
| 610 |
err(EXIT_FAILURE, "kqueue() failed"); |
| 611 |
} |
| 612 |
|
| 613 |
/* Initialize kevent structure */ |
| 614 |
EV_SET(&event, fd, EVFILT_VNODE , EV_ADD | EV_ONESHOT, NOTE_WRITE, 0, NULL); |
| 615 |
|
| 616 |
for (;;) { |
| 617 |
/* Attach event to the kqueue and block until something happens */ |
| 618 |
ret = kevent(kq, &event, 1, &tevent, 1, NULL); |
| 619 |
if (ret == -1) { |
| 620 |
perror("kevent"); |
| 621 |
exit(EXIT_FAILURE); |
| 622 |
} else if (ret > 0) { |
| 623 |
if (event.flags & EV_ERROR) { |
| 624 |
fprintf(stderr, "Event error: %s\en", strerror(event.data)); |
| 625 |
} else { |
| 626 |
printf("Something was written in '%s'\en", argv[1]); |
| 627 |
} |
| 628 |
} |
| 629 |
} |
| 630 |
} |
| 631 |
.Ed |
| 580 |
.Sh ERRORS |
632 |
.Sh ERRORS |
| 581 |
The |
633 |
The |
| 582 |
.Fn kqueue |
634 |
.Fn kqueue |