dirmonitor: give kevent a timeout so it doesn't lock forever (#1180)

This commit is contained in:
Jan 2022-11-06 01:00:32 +01:00 committed by GitHub
parent b8a4f729df
commit 56e465c351
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 2 deletions

View File

@ -4,6 +4,7 @@
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
struct dirmonitor_internal {
int fd;
@ -23,7 +24,9 @@ void deinit_dirmonitor(struct dirmonitor_internal* monitor) {
int get_changes_dirmonitor(struct dirmonitor_internal* monitor, char* buffer, int buffer_size) {
int nev = kevent(monitor->fd, NULL, 0, (struct kevent*)buffer, buffer_size / sizeof(kevent), NULL);
struct timespec ts = { 0, 10 * 1000000 }; // 10 ms
int nev = kevent(monitor->fd, NULL, 0, (struct kevent*)buffer, buffer_size / sizeof(kevent), &ts);
if (nev == -1)
return -1;
if (nev <= 0)
@ -42,9 +45,12 @@ int translate_changes_dirmonitor(struct dirmonitor_internal* monitor, char* buff
int add_dirmonitor(struct dirmonitor_internal* monitor, const char* path) {
int fd = open(path, O_RDONLY);
struct kevent change;
// a timeout of zero should make kevent return immediately
struct timespec ts = { 0, 0 }; // 0 s
EV_SET(&change, fd, EVFILT_VNODE, EV_ADD | EV_CLEAR, NOTE_DELETE | NOTE_EXTEND | NOTE_WRITE | NOTE_ATTRIB | NOTE_LINK | NOTE_RENAME, 0, (void*)path);
kevent(monitor->fd, &change, 1, NULL, 0, NULL);
kevent(monitor->fd, &change, 1, NULL, 0, &ts);
return fd;
}