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

后端 未结 5 1675
栀梦
栀梦 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:48

    On modern Linux systems /etc/mtab often points to /proc/self/mounts:

    $ ls -l /etc/mtab lrwxrwxrwx 1 root root 12 Sep 5 2013 /etc/mtab -> /proc/mounts $ ls -l /proc/mounts lrwxrwxrwx 1 root root 11 Jul 10 14:56 /proc/mounts -> self/mounts

    proc(5) manpage says that you don't really need to use inotify for this file, it is pollable:

    Since kernel version 2.6.15, this file is pollable: after opening the file for reading, a change in this file (i.e., a filesystem mount or unmount) causes select(2) to mark the file descriptor as readable, and poll(2) and epoll_wait(2) mark the file as having an error condition.

    Was wondered why inotify not works on /etc/mtab and found this manpage.

提交回复
热议问题