how to monitor operation on mac platform

680 views Asked by At

I am trying to get file open/write/create operation, I have tried fslogger which can only get file creation/delete....and other operations, can not get open/close operation, then I wrote a driver to do it, I can get open/close operation but can not get create operation, what's more, it's too messy! for example, if I open a file and modify it, and then close it, the driver gets a lot of open/write operations..I have no way to tell which one is really caused by user open/close operation.. any hints about this? thanks.

1

There are 1 answers

3
Joe On

Your best bet is going to be the KAuth system. You install your kauth handler (as a kernel extension) and get various callback codes when someone tries to create, open or close a file. This involves getting your callback in the critical path of opening files, so whatever you do has to be quick!

To quote:

KAUTH_SCOPE_FILEOP defines the following actions.

  • KAUTH_FILEOP_OPEN
  • KAUTH_FILEOP_CLOSE
  • KAUTH_FILEOP_CLOSE_MODIFIED
  • KAUTH_FILEOP_RENAME
  • KAUTH_FILEOP_EXCHANGE
  • KAUTH_FILEOP_LINK
  • KAUTH_FILEOP_EXEC

https://developer.apple.com/library/mac/technotes/tn2127/_index.html

If you're writing a kext you then have the question of how to get that info back into userland. FWIW I used Kqueue but you may have success with another method (let me know in the comments if you do!).

More info on Kauth here and KQueue here. It's not brilliantly documented, but there's enough info between those two to work out what you need to do.