-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathkqueue.c
63 lines (57 loc) · 1.81 KB
/
kqueue.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <sys/event.h>
#include <sys/time.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
char TEST_FILE[] = "./test.txt";
/**
* kqueue is a BSD mechanism for kernel event notifications allowing for
* asynchronous I/O in the kernel.
*/
int main(void) {
// the kqueue file descriptor returned from creating a kernel queue.
int kq;
// file descriptor for the file we are monitoring
int fd;
// new events that are returned from kevent
int nev;
// the new events that we want to the kernel to notifiy us about.
struct kevent change;
// the events that kevent is reporting. This gets filled by the kernel
struct kevent event;
// ask the kernel to create a new queue. Will return file descriptor representing
// the queue.
kq = kqueue();
if (kq == -1) {
perror("kqueue");
}
printf("Test file: %s\n", TEST_FILE);
fd = open(TEST_FILE, O_RDONLY);
if (fd == -1) {
perror("open");
}
// specify the events that we are interested in
EV_SET(&change, fd, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR, NOTE_WRITE | NOTE_DELETE, 0, (void *) TEST_FILE);
for (;;) {
nev = kevent(kq, &change, 1, &event, 1, NULL); //NULL is the timeout struct.
if (nev == -1) {
perror("kevent");
} else if (nev > 0) {
if (event.fflags & NOTE_DELETE) {
printf("File deleted. Exiting\n");
break;
}
if (event.fflags & NOTE_WRITE) {
char * data = (char *) event.udata;
printf("File[%s] modified\n", data);
}
if (event.fflags & NOTE_ATTRIB) {
printf("File attributes modified\n");
}
}
}
close(kq);
close(fd);
return EXIT_SUCCESS;
}