notify-rs/notify

No events emitted when watching /proc/self/mounts

Closed this issue · 2 comments

Hi,
I've created a small repo with a binary to monitor the mounts on a system: https://github.com/oren0e/watchmon
The idea is to receive a Write event whenever /proc/self/mounts changes (specifically I look for a specified-by-user term in that file, but that's irrelevant here).
Steps I do for the sake of testing this:

  1. Create a dummy directory in my home folder (I'm root) - mkdir blah
  2. mount /dev/sda5 /root/blah
  3. If I do cat /proc/self/mounts I can see that the new mount point was added.
  4. Run something similar to the simple example you have in the docs - so that whenever there is an event it is printed.
    The code, more or less that does this:
pub fn monitor_file_for_term(term: &str, filepath: &str, cmd: &str) -> AnyhowResult<()> {
    let (tx, rx) = channel();

    let mut watcher = watcher(tx, Duration::from_secs(10)).expect("Failed to create watcher");
    watcher
        .watch(&filepath, RecursiveMode::NonRecursive)
        .unwrap_or_else(|_| panic!("Could not watch path {}", &filepath));

    loop {
        match rx.recv() {
            Ok(event) => {
                println!("Event: {:?}", event);
            }
            Err(e) => println!("watch error: {:?}", e),
        }
    }
}

Disregard the unused variables, it's because I've removed commented-out code.
5. In a second terminal (I ssh again to a remote host I'm working on): umount /root/blah
And then I don't see any events whatsoever being printed - how is that possible? /proc/self/mounts has changed, I can verify that by cat /proc/self/mounts, what's the issue here?

System details:

NAME="Ubuntu"
VERSION="20.04.4 LTS (Focal Fossa)"

Rust version:

rustc 1.62.0 (a8314ef7d 2022-06-27)

You can't watch proc filesystems by this. This special filesystem for linux internals won't emit events like that.

See #391

Use the pollwatcher backend with the correct config in notify for that.

Thank you, that solved the issue for me!