X-Git-Url: https://code.octet-stream.net/netwatcher/blobdiff_plain/473c9605820f4531f9d40823338fa4bf8718dd6f..518e4dd38939e602cbfd19379d64b901cbcb96ce:/src/watch_linux.rs?ds=inline diff --git a/src/watch_linux.rs b/src/watch_linux.rs index e8e1ffc..e49d629 100644 --- a/src/watch_linux.rs +++ b/src/watch_linux.rs @@ -1,5 +1,6 @@ use std::os::fd::AsRawFd; use std::os::fd::OwnedFd; +use std::sync::mpsc; use nix::libc::poll; use nix::libc::pollfd; @@ -24,34 +25,46 @@ const RTMGRP_IPV6_IFADDR: u32 = 0x20; const RTMGRP_LINK: u32 = 0x01; pub(crate) struct WatchHandle { - // Dropping will close the fd which will be detected by poll - _pipefd: OwnedFd, + // Close on drop, which will be detected by poll in background thread + pipefd: Option, + + // Detect when thread has completed + complete: Option>, +} + +impl Drop for WatchHandle { + fn drop(&mut self) { + drop(self.pipefd.take()); + let _ = self.complete.take().unwrap().recv(); + } } pub(crate) fn watch_interfaces( callback: F, ) -> Result { - let pipefd = start_watcher_thread(callback)?; - Ok(WatchHandle { _pipefd: pipefd }) + let (pipefd, complete) = start_watcher_thread(callback)?; + Ok(WatchHandle { + pipefd: Some(pipefd), + complete: Some(complete), + }) } fn start_watcher_thread( mut callback: F, -) -> Result { +) -> Result<(OwnedFd, mpsc::Receiver<()>), Error> { let sockfd = socket( AddressFamily::Netlink, SockType::Raw, - SockFlag::empty(), + SockFlag::SOCK_NONBLOCK, Some(SockProtocol::NetlinkRoute), ) - .map_err(|e| Error::CreateSocket(e))?; - // TODO: set nonblocking + .map_err(|e| Error::CreateSocket(e.to_string()))?; let sa_nl = NetlinkAddr::new( 0, (RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV6_IFADDR) as u32, ); - bind(sockfd.as_raw_fd(), &sa_nl).map_err(|e| Error::Bind(e))?; - let (pipe_rd, pipe_wr) = pipe().map_err(|e| Error::CreatePipe(e))?; + bind(sockfd.as_raw_fd(), &sa_nl).map_err(|e| Error::Bind(e.to_string()))?; + let (pipe_rd, pipe_wr) = pipe().map_err(|e| Error::CreatePipe(e.to_string()))?; let mut prev_list = List::default(); let mut handle_update = move |new_list: List| { @@ -71,6 +84,8 @@ fn start_watcher_thread( // looks like we're going to have trouble listing interfaces. handle_update(crate::list::list_interfaces()?); + let (complete_tx, complete_rx) = mpsc::channel(); + std::thread::spawn(move || { let mut buf = [0u8; 4096]; @@ -104,7 +119,9 @@ fn start_watcher_thread( break; } } + + drop(complete_tx); }); - Ok(pipe_wr) + Ok((pipe_wr, complete_rx)) }