notify
notify copied to clipboard
Can notify watch disk mount event?
I want to be notified when a disk is mounted. Is that possible to be implemented via notify
library?
No, notify only uses the OS specific API to register for folder/file changes. While under linux everything is mounted inside / and thus a disk mount may be visible with a new "folder" event in /foobar (when mounted in that place), this AFAIK won't work under windows.
Hi @d0u9 , I think these 3 points will help you:
- From Linux kernel 2.4.19, mount namespaces were added, so
/proc/mount
is a symlink to/proc/self/mounts
. This means you have different mounts per mount namespace, but lets assume, you want to monitor mounts in the namespace where your program is running so you can monitor/proc/self/mounts
-
/proc
is part of sysfs, hence you cannot use inotify on this file to monitor mount changes - The files
/proc/[pid]/mounts
and/proc/self/mounts
are pollable: after opening this file for reading, a change in this file causes select to mark the file descriptor as having an exceptional condition and poll and epoll_wait mark the file as having a priority eventPOLLPRI
Solution - Use the epoll crate -> https://docs.rs/epoll/4.3.1/epoll/struct.Events.html and wait for EPOLLPRI
after reading once on file /proc/self/mounts
Reference:
- https://man7.org/linux/man-pages/man5/proc.5.html
- https://github.com/nathansizemore/epoll