简体   繁体   中英

How to reliably monitor drives mount/umount on linux?

I've found a few similar questions here but none of them works now.

Monitoring (inotify) of /etc/mtab is not working because it's symling to /proc/mounts now. Monitoring via udisks doesn't catch manually mount/umounted drives. Netlink sockets also don't work (no mount/umount actions).

So what's the really working way to monitor mounts and umounts?

PS I don't care about namespaced mounts, just global.

我相信udev应该可用于监控坐骑。

You can monitor /proc/mounts without polling. Put it in the exceptfds list to select, and then seek to the beginning of the file every time. Here's a quick demo in python but it should be easily translatable to C:

f = open("/proc/mounts")
while True:
    r,w,x = select.select([],[],[f])
    f.seek(0)
    print f.read()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM