我正在发现kevent()和kqueue()并试图遵循Apple's tutorial来观看文件。
但是,我仅收到一个事件,仅此而已。 当我尝试通过添加或删除内容在下一个文件之后下一次编辑文件时,没有任何反应...
struct kevent events_to_monitor;
struct kevent event_data;
struct timespec timeout;
unsigned int vnode_events;
int kq;
int event_fd;
event_fd = open(_filename.c_str(), O_EVTONLY);
if (event_fd <=0) {
fprintf(stderr, "The file %s could not be opened for monitoring. Error was %s.\n", _filename.c_str(), strerror(errno));
exit(-1);
}
/* Open a kernel queue. */
if ((kq = kqueue()) < 0) {
fprintf(stderr, "Could not open kernel queue. Error was %s.\n", strerror(errno));
}
timeout.tv_sec = 0; // 0 seconds
timeout.tv_nsec = 500000000; // 500 milliseconds
vnode_events = NOTE_DELETE | NOTE_EXTEND | NOTE_WRITE | NOTE_ATTRIB;
EV_SET( &events_to_monitor, event_fd, EVFILT_VNODE, EV_ADD | EV_CLEAR, vnode_events, 0, NULL);
while (1) {
int event_count = kevent(kq, &events_to_monitor, NUM_EVENT_SLOTS, &event_data, 1, &timeout);
if ((event_count < 0) || (event_data.flags == EV_ERROR)) {
/* An error occurred. */
fprintf(stderr, "An error occurred (event count %d). The error was %s.\n", event_count, strerror(errno));
break;
}
if (event_count) {
printf("Event %" PRIdPTR " occurred. Filter %d, flags %d, filter data %" PRIdPTR ", path %s\n",
event_data.ident,
event_data.filter,
event_data.flags,
event_data.data,
(char *) event_data.udata);
} else {
printf("No event.\n");
}
/* Reset the timeout. In case of a signal interrruption, the
values may change. */
timeout.tv_sec = 0; // 0 seconds
timeout.tv_nsec = 500000000; // 500 milliseconds
}
谢谢您的帮助。