I'd like build a user-space tool that monitor any attempt to open non-existent file with specific path+name (it doesn't appear in filesystem).
Looking at the the available option, the one that is closest to my need is using kevent framework.
Here's a sample for for monitor file represented by path
, which requires file descriptor. perhaps there's an option to monitor every access to path
event if open
fails since the file doesn't exist.
fd = open(path, O_EVTONLY);
user_data = path;
/* Set up a list of events to monitor. */
vnode_events = NOTE_DELETE | NOTE_WRITE | NOTE_EXTEND | NOTE_ATTRIB | NOTE_LINK | NOTE_RENAME | NOTE_REVOKE;
EV_SET( &events_to_monitor[0], fd, EVFILT_VNODE, EV_ADD | EV_CLEAR, vnode_events, 0, user_data);
int event_count = kevent(kq, events_to_monitor, NUM_EVENT_SLOTS, event_data, num_files, NULL);