User-space callback for file created, modified, or deleted events

Suppose I have a program monitoring a pre-defined set of files for their creation, modification, or deletion. If one of those three events occurs on one of the files, I want to call a piece of code in response. Presently the code sits in a loop with a sleep at the bottom and just checks all the files using stat, etc. Is there a way to do this with callbacks so I don't have to waste CPU cycles looping? I expect the events to be very sparse in time so a callback is preferred. I see kevent gives a mechanism for monitoring modifications on an fd, but I'm not sure how it could be used to monitor for files not yet created. Should I just monitor the directory entry where I expect the file to be created? Any advice welcome.

BTW this is FreeBSD 7 (if it matters).
 
Kqueue API does support directory monitoring since they can be opened and have regular file descriptors. However it has a drawback, it does not notify what file exactly was deleted/created/modified so you need to implement a comparison mechanics from previous event to know what file changed.
 
Back
Top