config: allow attaching file descriptors to commands
This commit is contained in:
parent
a1ba476e68
commit
2037a37c1e
8 changed files with 96 additions and 29 deletions
|
|
@ -10,6 +10,6 @@ bincode = "1.3.3"
|
|||
serde = { version = "1.0.196", features = ["derive"] }
|
||||
log = "0.4.14"
|
||||
futures-util = { version = "0.3.30", features = ["io"] }
|
||||
uapi = "0.2.10"
|
||||
uapi = "0.2.12"
|
||||
thiserror = "1.0.57"
|
||||
backtrace = "0.3.69"
|
||||
|
|
|
|||
|
|
@ -28,6 +28,7 @@ use {
|
|||
future::Future,
|
||||
mem,
|
||||
ops::Deref,
|
||||
os::fd::IntoRawFd,
|
||||
panic::{catch_unwind, AssertUnwindSafe},
|
||||
pin::Pin,
|
||||
ptr,
|
||||
|
|
@ -264,11 +265,26 @@ impl Client {
|
|||
.iter()
|
||||
.map(|(a, b)| (a.to_string(), b.to_string()))
|
||||
.collect();
|
||||
self.send(&ClientMessage::Run {
|
||||
prog: &command.prog,
|
||||
args: command.args.clone(),
|
||||
env,
|
||||
});
|
||||
let fds: Vec<_> = command
|
||||
.fds
|
||||
.borrow_mut()
|
||||
.drain()
|
||||
.map(|(a, b)| (a, b.into_raw_fd()))
|
||||
.collect();
|
||||
if fds.is_empty() {
|
||||
self.send(&ClientMessage::Run {
|
||||
prog: &command.prog,
|
||||
args: command.args.clone(),
|
||||
env,
|
||||
});
|
||||
} else {
|
||||
self.send(&ClientMessage::Run2 {
|
||||
prog: &command.prog,
|
||||
args: command.args.clone(),
|
||||
env,
|
||||
fds,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
pub fn grab(&self, kb: InputDevice, grab: bool) {
|
||||
|
|
|
|||
|
|
@ -375,6 +375,12 @@ pub enum ClientMessage<'a> {
|
|||
pollable: PollableId,
|
||||
writable: bool,
|
||||
},
|
||||
Run2 {
|
||||
prog: &'a str,
|
||||
args: Vec<String>,
|
||||
env: Vec<(String, String)>,
|
||||
fds: Vec<(i32, i32)>,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
//! Tools for spawning programs.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::{cell::RefCell, collections::HashMap, os::fd::OwnedFd};
|
||||
|
||||
/// Sets an environment variable.
|
||||
///
|
||||
|
|
@ -14,6 +14,7 @@ pub struct Command {
|
|||
pub(crate) prog: String,
|
||||
pub(crate) args: Vec<String>,
|
||||
pub(crate) env: HashMap<String, String>,
|
||||
pub(crate) fds: RefCell<HashMap<i32, OwnedFd>>,
|
||||
}
|
||||
|
||||
impl Command {
|
||||
|
|
@ -28,6 +29,7 @@ impl Command {
|
|||
prog: prog.to_string(),
|
||||
args: vec![],
|
||||
env: Default::default(),
|
||||
fds: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -43,7 +45,39 @@ impl Command {
|
|||
self
|
||||
}
|
||||
|
||||
/// Sets a file descriptor of the process.
|
||||
///
|
||||
/// By default, the process starts with exactly stdin, stdout, and stderr open and all
|
||||
/// pointing to `/dev/null`.
|
||||
pub fn fd<F: Into<OwnedFd>>(&mut self, idx: i32, fd: F) -> &mut Self {
|
||||
self.fds.borrow_mut().insert(idx, fd.into());
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the stdin of the process.
|
||||
///
|
||||
/// This is equivalent to `fd(0, fd)`.
|
||||
pub fn stdin<F: Into<OwnedFd>>(&mut self, fd: F) -> &mut Self {
|
||||
self.fd(0, fd)
|
||||
}
|
||||
|
||||
/// Sets the stdout of the process.
|
||||
///
|
||||
/// This is equivalent to `fd(1, fd)`.
|
||||
pub fn stdout<F: Into<OwnedFd>>(&mut self, fd: F) -> &mut Self {
|
||||
self.fd(1, fd)
|
||||
}
|
||||
|
||||
/// Sets the stderr of the process.
|
||||
///
|
||||
/// This is equivalent to `fd(2, fd)`.
|
||||
pub fn stderr<F: Into<OwnedFd>>(&mut self, fd: F) -> &mut Self {
|
||||
self.fd(2, fd)
|
||||
}
|
||||
|
||||
/// Executes the command.
|
||||
///
|
||||
/// This consumes all attached file descriptors.
|
||||
pub fn spawn(&self) {
|
||||
get!().spawn(self);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue