From: Thomas Karpiniec Date: Fri, 28 Jun 2024 01:13:47 +0000 (+1000) Subject: Block on dropping handle on Linux X-Git-Tag: v0.1.0~4 X-Git-Url: https://code.octet-stream.net/netwatcher/commitdiff_plain/8f78cf14e9d177b9dfd43b6b35038d391821627e?ds=sidebyside Block on dropping handle on Linux --- diff --git a/src/watch_linux.rs b/src/watch_linux.rs index c38be37..dc99572 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; @@ -10,9 +11,9 @@ use nix::sys::socket::socket; use nix::sys::socket::AddressFamily; use nix::sys::socket::MsgFlags; use nix::sys::socket::NetlinkAddr; -use nix::sys::socket::SockFlag; use nix::sys::socket::SockProtocol; use nix::sys::socket::SockType; +use nix::sys::socket::SOCK_NONBLOCK; use nix::unistd::pipe; use crate::Error; @@ -24,24 +25,37 @@ 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().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(), + SOCK_NONBLOCK, Some(SockProtocol::NetlinkRoute), ) .map_err(|e| Error::CreateSocket(e.to_string()))?; @@ -71,6 +85,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,6 +120,8 @@ fn start_watcher_thread( break; } } + + drop(complete_tx); }); Ok(pipe_wr)