FreeBSD: Add syscall wrapper for kqueuex (FreeBSD 15)
[valgrind.git] / memcheck / tests / freebsd / kqueuex.c
blob0f7cde7b852f7f258df049d42ec98e3ed493de54
1 #include <sys/event.h>
2 #include <err.h>
3 #include <fcntl.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <unistd.h>
9 int
10 main(int argc, char **argv)
12 struct kevent event; /* Event we want to monitor */
13 struct kevent tevent; /* Event triggered */
14 int kq, fd, ret;
16 if (argc != 2)
17 err(EXIT_FAILURE, "Usage: %s path\n", argv[0]);
18 fd = open(argv[1], O_RDONLY);
19 if (fd == -1)
20 err(EXIT_FAILURE, "Failed to open '%s'", argv[1]);
22 /* Create kqueue. */
23 kq = kqueuex(KQUEUE_CLOEXEC);
24 if (kq == -1)
25 err(EXIT_FAILURE, "kqueuex(KQUEUE_CLOEXEC) failed");
27 /* Initialize kevent structure. */
28 EV_SET(&event, fd, EVFILT_VNODE, EV_ADD | EV_CLEAR, NOTE_WRITE,
29 0, NULL);
30 /* Attach event to the kqueue. */
31 ret = kevent(kq, &event, 1, NULL, 0, NULL);
32 if (ret == -1)
33 err(EXIT_FAILURE, "kevent register");
35 for (;;) {
36 /* Sleep until something happens. */
37 ret = kevent(kq, NULL, 0, &tevent, 1, NULL);
38 if (ret == -1) {
39 err(EXIT_FAILURE, "kevent wait");
40 } else if (ret > 0) {
41 if (tevent.flags & EV_ERROR)
42 errx(EXIT_FAILURE, "Event error: %s", strerror(event.data));
43 else {
44 printf("Something was written in '%s'\n", argv[1]);
45 exit(0);
50 /* kqueues are destroyed upon close() */
51 (void)close(kq);
52 (void)close(fd);