How could I detect when a directory is mounted with inotify?

后端 未结 5 1676
栀梦
栀梦 2021-02-06 01:58

I\'m using Linux Inotify to detect FS events on my program.

How could I be notified when a device is mounted on a monitored directory?

5条回答
  •  南方客
    南方客 (楼主)
    2021-02-06 02:46

    I don't think you can do it with inotify. Here is the method though:

    1. Read uevents from kernel via a Netlink socket and filter out those where "ACTION" is not "mount".
    2. Read and parse "/proc/mounts" when you get an event with a "mount" action.
    3. Find a record for a mount point with device that was just mounted and filter it out if it's not the directory you are watching.

提交回复
热议问题