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



## mbryan (Oct 7, 2013)

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).


----------



## SirDice (Oct 8, 2013)

mbryan said:
			
		

> BTW this is FreeBSD 7 (if it matters).


It does. FreeBSD 7.x is End-of-Life since February 2013.

http://www.freebsd.org/security/unsupported.html


----------



## mbryan (Oct 8, 2013)

SirDice said:
			
		

> It does. FreeBSD 7.x is End-of-Life since February 2013.
> 
> http://www.freebsd.org/security/unsupported.html



In that case, please assume I'm running FreeBSD 10 for the purposes of this question, and I'll look into whether the answers apply to my particular situation as it evolves.


----------



## expl (Oct 9, 2013)

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.


----------

