From f9df7170ac929ccc915642c2e6415e768973bb54 Mon Sep 17 00:00:00 2001 From: Thomas Karpiniec Date: Sat, 15 Jun 2024 12:30:32 +0100 Subject: [PATCH 1/1] Commit to reporting error if original interface listing fails --- src/lib.rs | 7 +++++++ src/watch_linux.rs | 34 ++++++++++++++++++---------------- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 5c8d73d..cb3a117 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -145,6 +145,13 @@ pub fn list_interfaces() -> Result, Error> { /// /// The callback will fire once immediately with an initial interface list, and a diff as if /// there were originally no interfaces present. +/// +/// This function will return an error if there is a problem configuring the watcher, or if there +/// is an error retrieving the initial interface list. +/// +/// We assume that if listing the interfaces worked the first time, then it will continue to work +/// for as long as the watcher is running. If listing interfaces begins to fail later, those +/// failures will be swallowed and the callback will not be called for that change event. pub fn watch_interfaces( callback: F, ) -> Result { diff --git a/src/watch_linux.rs b/src/watch_linux.rs index e584211..95ef106 100644 --- a/src/watch_linux.rs +++ b/src/watch_linux.rs @@ -51,24 +51,26 @@ fn start_watcher_thread( bind(sockfd.as_raw_fd(), &sa_nl).map_err(|_| Error::Internal)?; // TODO: proper errors let (pipe_rd, pipe_wr) = pipe().map_err(|_| Error::Internal)?; - std::thread::spawn(move || { - let mut prev_list = List::default(); - let mut buf = [0u8; 4096]; - let mut handle_update = move |new_list: List| { - if new_list == prev_list { - return; - } - let update = Update { - interfaces: new_list.0.clone(), - diff: new_list.diff_from(&prev_list), - }; - (callback)(update); - prev_list = new_list; + let mut prev_list = List::default(); + let mut handle_update = move |new_list: List| { + if new_list == prev_list { + return; + } + let update = Update { + interfaces: new_list.0.clone(), + diff: new_list.diff_from(&prev_list), }; + (callback)(update); + prev_list = new_list; + }; - if let Ok(initial) = crate::list::list_interfaces() { - handle_update(initial); - }; + // Now that netlink socket is open, provide an initial update. + // By having this outside the thread we can return an error synchronously if it + // looks like we're going to have trouble listing interfaces. + handle_update(crate::list::list_interfaces()?); + + std::thread::spawn(move || { + let mut buf = [0u8; 4096]; loop { let mut fds = [ -- 2.39.5