--- a/cargo_home/gentoo/vhost-0.10.0/src/vhost_user/backend.rs 2006-07-23 18:21:28.000000000 -0700 +++ b/cargo_home/gentoo/vhost-0.10.0/src/vhost_user/backend.rs 2024-07-07 19:05:49.824602721 -0700 @@ -5,6 +5,8 @@ use std::sync::Arc; +use std::os::fd::{FromRawFd, IntoRawFd, RawFd}; +use std::os::unix::net::UnixStream; use super::connection::{Endpoint, Listener}; use super::message::*; use super::{BackendReqHandler, Result, VhostUserBackendReqHandler}; @@ -26,6 +28,19 @@ }) } + pub fn from_raw_fd(&mut self, fd: RawFd) -> Result>> { + let mut unixstream : UnixStream; + unsafe { + unixstream = UnixStream::from_raw_fd(fd); + } + println!("creating from raw_fd {}", fd); + return Ok(Some(BackendReqHandler::new( + Endpoint::::from_stream(unixstream), + self.backend.take().unwrap(), + ))); + } + + /// Accept an incoming connection from the frontend, returning Some(Backend) on /// success, or None if the socket is nonblocking and no incoming connection /// was detected --- a/cargo_home/gentoo/vhost-user-backend-0.13.1/src/lib.rs 2006-07-23 18:21:28.000000000 -0700 +++ b/cargo_home/gentoo/vhost-user-backend-0.13.1/src/lib.rs 2024-07-07 19:05:15.473777937 -0700 @@ -8,6 +8,7 @@ #[macro_use] extern crate log; +use std::os::fd::RawFd; use std::fmt::{Display, Formatter}; use std::path::Path; use std::sync::{Arc, Mutex}; @@ -123,6 +124,7 @@ &mut self, mut handler: BackendReqHandler>>, ) -> Result<()> { + println!("start_daemon daemon"); let handle = thread::Builder::new() .name(self.name.clone()) .spawn(move || loop { @@ -162,6 +164,22 @@ self.start_daemon(backend_handler) } + pub fn start_from_rawfd(&mut self, listener: Listener, fd: RawFd) -> Result<()> { + println!("staring accept daemon with fd"); + let mut backend_listener = BackendListener::new(listener, self.handler.clone()) + .map_err(Error::CreateBackendListener)?; + let backend_handler = self.from_raw_fd(&mut backend_listener, fd)?; + self.start_daemon(backend_handler) + } + + fn from_raw_fd(&self, backend_listener: &mut BackendListener>>, fd: RawFd) -> Result>>> { + match backend_listener.from_raw_fd(fd) { + Err(e) => return Err(Error::CreateBackendListener(VhostUserError::Disconnected)), + Ok(Some(v)) => return Ok(v), + Ok(None) => Err(Error::CreateBackendListener(VhostUserError::Disconnected)), + } + } + fn accept( &self, backend_listener: &mut BackendListener>>, --- a/src/main.rs 2024-06-20 17:34:36.437397174 -0700 +++ b/virtiofsd-v1.10.1/src/main.rs 2024-07-07 19:04:59.143386023 -0700 @@ -87,6 +87,10 @@ InvalidTag, } +extern "C" { + fn dup(input: i32) -> i32; +} + impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { use self::Error::UnshareCloneFs; @@ -1042,7 +1046,7 @@ // We need to keep _pid_file around because it maintains a lock on the pid file // that prevents another daemon from using the same pid file. let (listener, socket_path, _pid_file) = match opt.fd.as_ref() { - Some(fd) => unsafe { (Listener::from_raw_fd(*fd), None, None) }, + Some(fd) => unsafe { (Listener::from_raw_fd(dup(*fd)), None, None) }, None => { // Set umask to ensure the socket is created with the right permissions let _umask_guard = oslib::ScopedUmask::new(umask); @@ -1173,9 +1177,19 @@ info!("Waiting for vhost-user socket connection..."); - if let Err(e) = daemon.start(listener) { - error!("Failed to start daemon: {:?}", e); - process::exit(1); + match opt.fd.as_ref() { + Some(fd) => { + if let Err(e) = daemon.start_from_rawfd(listener, *fd) { + error!("Failed to start daemon: {:?}", e); + process::exit(1); + } + } + None => { + if let Err(e) = daemon.start(listener) { + error!("Failed to start daemon: {:?}", e); + process::exit(1); + } + } } info!("Client connected, servicing requests");