diff --git a/src/util/rust/defines.rs b/src/util/rust/defines.rs index b9df0233a7c..0bbc3148029 100644 --- a/src/util/rust/defines.rs +++ b/src/util/rust/defines.rs @@ -88,6 +88,7 @@ pub enum DescriptorType { Memory(u32, u32), // (size, handle_type) WritePipe, Event, + Socket(TubeType), } /// # Safety diff --git a/src/util/rust/meson.build b/src/util/rust/meson.build index 59963c465f0..51e542c1f41 100644 --- a/src/util/rust/meson.build +++ b/src/util/rust/meson.build @@ -1,6 +1,12 @@ # Copyright © 2024 Google # SPDX-License-Identifier: MIT +dep_log = dependency('log', + version: '>= 0.4.22', + fallback: ['log-0.4-rs', 'dep_log'], + required: true, +) + dep_cfg_if = dependency('cfg-if', version: '>= 1.0.0', fallback: ['cfg-if-1-rs', 'dep_cfg_if'], @@ -25,7 +31,8 @@ dep_zerocopy = dependency('zerocopy', required: true, ) -dep_mesa3d_util = [dep_cfg_if, dep_thiserror, dep_remain, dep_zerocopy] +dep_mesa3d_util = [dep_cfg_if, dep_thiserror, dep_remain, dep_zerocopy, + dep_log] supported_systems = ['linux', 'windows', 'darwin', 'macos'] supported_host_machine = host_machine.system() in supported_systems diff --git a/src/util/rust/sys/linux/descriptor.rs b/src/util/rust/sys/linux/descriptor.rs index eeed2e53c1b..b7361b4ef16 100644 --- a/src/util/rust/sys/linux/descriptor.rs +++ b/src/util/rust/sys/linux/descriptor.rs @@ -16,11 +16,14 @@ use std::os::unix::io::RawFd; use rustix::fs::fcntl_get_seals; use rustix::fs::fcntl_getfl; +use rustix::fs::fstat; use rustix::fs::seek; +use rustix::fs::FileType; use rustix::fs::OFlags; use rustix::fs::SealFlags; use rustix::fs::SeekFrom; use rustix::io::Errno; +use rustix::net::sockopt::socket_type; use crate::descriptor::AsRawDescriptor; use crate::descriptor::FromRawDescriptor; @@ -55,6 +58,15 @@ impl OwnedDescriptor { } } + if let Ok(fd_stat) = fstat(&self.owned) { + let fd_type = FileType::from_raw_mode(fd_stat.st_mode); + if fd_type == FileType::Socket { + return Ok(DescriptorType::Socket( + socket_type(&self.owned)?.try_into()?, + )); + } + } + match seek(&self.owned, SeekFrom::End(0)) { Ok(seek_size) => { let size: u32 = seek_size diff --git a/src/util/rust/sys/linux/tube.rs b/src/util/rust/sys/linux/tube.rs index a7e78af7d13..29e549a1f81 100644 --- a/src/util/rust/sys/linux/tube.rs +++ b/src/util/rust/sys/linux/tube.rs @@ -1,6 +1,8 @@ // Copyright 2025 Google // SPDX-License-Identifier: MIT +use std::io::Error; +use std::io::ErrorKind; use std::io::IoSlice; use std::io::IoSliceMut; use std::mem::MaybeUninit; @@ -41,6 +43,21 @@ pub struct Tube { socket: OwnedDescriptor, } +impl TryFrom for TubeType { + type Error = std::io::Error; + + fn try_from(ty: SocketType) -> Result { + match ty { + SocketType::SEQPACKET => Ok(TubeType::Packet), + SocketType::STREAM => Ok(TubeType::Stream), + ty => { + log::warn!("Unsupported socket type {ty:?}"); + Err(Error::from(ErrorKind::Unsupported)) + } + } + } +} + impl Tube { pub fn new + Arg>(path: P, kind: TubeType) -> MesaResult { let socket = match kind {