|
| 1 | +use std::{io::ErrorKind, os::fd::RawFd}; |
| 2 | + |
| 3 | +use libc::recv; |
| 4 | +use tokio::io::{unix::AsyncFd, Interest}; |
| 5 | + |
| 6 | +use crate::{cerr, control_message::zeroed_sockaddr_storage}; |
| 7 | + |
| 8 | +pub struct ChangeDetector { |
| 9 | + fd: AsyncFd<RawFd>, |
| 10 | +} |
| 11 | + |
| 12 | +impl ChangeDetector { |
| 13 | + const SOCKET_PATH: &'static [u8] = b"/var/run/devd.seqpacket.pipe"; |
| 14 | + pub fn new() -> std::io::Result<Self> { |
| 15 | + const _: () = assert!( |
| 16 | + std::mem::size_of::<libc::sockaddr_storage>() |
| 17 | + >= std::mem::size_of::<libc::sockaddr_un>() |
| 18 | + ); |
| 19 | + const _: () = assert!( |
| 20 | + std::mem::align_of::<libc::sockaddr_storage>() |
| 21 | + >= std::mem::align_of::<libc::sockaddr_un>() |
| 22 | + ); |
| 23 | + |
| 24 | + let mut address_buf = zeroed_sockaddr_storage(); |
| 25 | + // Safety: the above assertions guarantee that alignment and size are correct. |
| 26 | + // the resulting reference won't outlast the function, and result lives the entire |
| 27 | + // duration of the function |
| 28 | + let address = unsafe { |
| 29 | + &mut *(&mut address_buf as *mut libc::sockaddr_storage as *mut libc::sockaddr_un) |
| 30 | + }; |
| 31 | + |
| 32 | + address.sun_family = libc::AF_UNIX as _; |
| 33 | + for i in 0..Self::SOCKET_PATH.len() { |
| 34 | + address.sun_path[i] = Self::SOCKET_PATH[i] as _; |
| 35 | + } |
| 36 | + |
| 37 | + // Safety: calling socket is safe |
| 38 | + let fd = cerr(unsafe { libc::socket(libc::AF_UNIX, libc::SOCK_SEQPACKET, 0) })?; |
| 39 | + // Safety: address is valid for the duration of the call |
| 40 | + cerr(unsafe { |
| 41 | + libc::bind( |
| 42 | + fd, |
| 43 | + address as *mut _ as *mut _, |
| 44 | + std::mem::size_of_val(address) as _, |
| 45 | + ) |
| 46 | + })?; |
| 47 | + |
| 48 | + let nonblocking = 1 as libc::c_int; |
| 49 | + // Safety: nonblocking lives for the duration of the call, and is 4 bytes long as expected for FIONBIO |
| 50 | + cerr(unsafe { libc::ioctl(fd, libc::FIONBIO, &nonblocking) })?; |
| 51 | + |
| 52 | + Ok(ChangeDetector { |
| 53 | + fd: AsyncFd::new(fd)?, |
| 54 | + }) |
| 55 | + } |
| 56 | + |
| 57 | + fn empty(fd: i32) { |
| 58 | + loop { |
| 59 | + // Safety: buf is valid for the duration of the call, and it's length is passed as the len argument |
| 60 | + let mut buf = [0u8; 16]; |
| 61 | + match cerr(unsafe { |
| 62 | + recv( |
| 63 | + fd, |
| 64 | + &mut buf as *mut _ as *mut _, |
| 65 | + std::mem::size_of_val(&buf) as _, |
| 66 | + 0, |
| 67 | + ) as _ |
| 68 | + }) { |
| 69 | + Ok(_) => continue, |
| 70 | + Err(e) if e.kind() == ErrorKind::WouldBlock => break, |
| 71 | + Err(e) => { |
| 72 | + tracing::error!("Could not receive on change socket: {}", e); |
| 73 | + break; |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + pub async fn wait_for_change(&mut self) { |
| 80 | + if let Err(e) = self |
| 81 | + .fd |
| 82 | + .async_io(Interest::READABLE, |fd| { |
| 83 | + // Safety: buf is valid for the duration of the call, and it's length is passed as the len argument |
| 84 | + let mut buf = [0u8; 16]; |
| 85 | + cerr(unsafe { |
| 86 | + recv( |
| 87 | + *fd, |
| 88 | + &mut buf as *mut _ as *mut _, |
| 89 | + std::mem::size_of_val(&buf) as _, |
| 90 | + 0, |
| 91 | + ) as _ |
| 92 | + })?; |
| 93 | + Self::empty(*fd); |
| 94 | + Ok(()) |
| 95 | + }) |
| 96 | + .await |
| 97 | + { |
| 98 | + tracing::error!("Could not receive on change socket: {}", e); |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments