-
-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ zb: Add (tokio) support for
unixexec
transport
- Loading branch information
Showing
2 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
use std::ffi::OsString; | ||
use std::fmt::Display; | ||
use std::os::unix::ffi::OsStrExt; | ||
use std::os::unix::process::CommandExt; | ||
use std::path::PathBuf; | ||
use std::process::Stdio; | ||
|
||
use tokio::net::UnixStream; | ||
use tracing::warn; | ||
|
||
use crate::Error; | ||
|
||
use super::encode_percents; | ||
|
||
/// A unixexec domain socket transport in a D-Bus address. | ||
#[derive(Clone, Debug, PartialEq, Eq)] | ||
pub struct UnixExec { | ||
pub(super) path: PathBuf, | ||
pub(super) arg0: Option<OsString>, | ||
pub(super) args: Vec<String>, | ||
} | ||
|
||
impl UnixExec { | ||
/// Create a new unixexec transport with the given path and arguments. | ||
pub fn new(path: PathBuf, arg0: Option<OsString>, args: Vec<String>) -> Self { | ||
Self { path, arg0, args } | ||
} | ||
|
||
pub(super) fn from_options(opts: std::collections::HashMap<&str, &str>) -> crate::Result<Self> { | ||
let Some(path) = opts.get("path") else { | ||
return Err(crate::Error::Address( | ||
"unixexec address is missing `path`".to_owned(), | ||
)); | ||
}; | ||
|
||
let arg0 = opts.get("argv0").map(OsString::from); | ||
|
||
let mut args: Vec<String> = Vec::new(); | ||
let mut arg_index = 1; | ||
while let Some(arg) = opts.get(format!("argv{arg_index}").as_str()) { | ||
args.push(arg.to_string()); | ||
arg_index += 1; | ||
} | ||
|
||
Ok(Self::new(PathBuf::from(path), arg0, args)) | ||
} | ||
|
||
pub(super) async fn connect(self) -> crate::Result<UnixStream> { | ||
let mut child = tokio::process::Command::from(self) | ||
.stdin(Stdio::piped()) | ||
.stdout(Stdio::piped()) | ||
.stderr(Stdio::inherit()) | ||
.spawn()?; | ||
|
||
let stdin = child | ||
.stdin | ||
.take() | ||
.ok_or(Error::Failure("child stdin not found".into()))?; | ||
|
||
let stdout = child | ||
.stdout | ||
.take() | ||
.ok_or(Error::Failure("child stdout not found".into()))?; | ||
|
||
let exec_stdio_stream = tokio::io::join(stdout, stdin); | ||
|
||
let (transport_stream, unix_pipe_stream) = tokio::net::UnixStream::pair()?; | ||
|
||
tokio::task::spawn(async move { | ||
let mut unix_pipe_stream = unix_pipe_stream; | ||
let mut exec_stdio_stream = exec_stdio_stream; | ||
if let Err(e) = | ||
tokio::io::copy_bidirectional(&mut unix_pipe_stream, &mut exec_stdio_stream).await | ||
{ | ||
warn!("Error occurred while copying bidirectional streams: {}", e); | ||
} | ||
}); | ||
|
||
Ok(transport_stream) | ||
} | ||
} | ||
|
||
impl Display for UnixExec { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
f.write_str("unixexec:")?; | ||
encode_percents(f, self.path.as_os_str().as_bytes()) | ||
} | ||
} | ||
|
||
impl From<UnixExec> for std::process::Command { | ||
fn from(unixexec: UnixExec) -> Self { | ||
let mut command = std::process::Command::new(unixexec.path); | ||
command.args(unixexec.args); | ||
|
||
if let Some(arg0) = unixexec.arg0.as_ref() { | ||
command.arg0(arg0); | ||
} | ||
|
||
command | ||
} | ||
} | ||
|
||
impl From<UnixExec> for tokio::process::Command { | ||
fn from(unixexec: UnixExec) -> Self { | ||
std::process::Command::from(unixexec).into() | ||
} | ||
} |