notify icon indicating copy to clipboard operation
notify copied to clipboard

Can notify watch disk mount event?

Open d0u9 opened this issue 3 years ago • 2 comments

I want to be notified when a disk is mounted. Is that possible to be implemented via notify library?

d0u9 avatar Aug 30 '21 04:08 d0u9

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.

0xpr03 avatar Aug 31 '21 09:08 0xpr03

Hi @d0u9 , I think these 3 points will help you:

  1. 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
  2. /proc is part of sysfs, hence you cannot use inotify on this file to monitor mount changes
  3. 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 event POLLPRI

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

crazystylus avatar Dec 05 '21 05:12 crazystylus