2019-04-08 15:59:39 +00:00
|
|
|
use failure::*;
|
|
|
|
|
|
|
|
use futures::*;
|
|
|
|
|
|
|
|
use tokio::net::unix::UnixListener;
|
|
|
|
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use serde_json::Value;
|
|
|
|
use std::sync::Arc;
|
2019-04-11 08:51:59 +00:00
|
|
|
use std::os::unix::io::AsRawFd;
|
|
|
|
use nix::sys::socket;
|
2019-04-08 15:59:39 +00:00
|
|
|
|
|
|
|
/// Listens on a Unix Socket to handle simple command asynchronously
|
2019-08-23 13:00:18 +00:00
|
|
|
pub fn create_control_socket<P, F>(path: P, f: F) -> Result<impl Future<Output = ()>, Error>
|
|
|
|
where
|
|
|
|
P: Into<PathBuf>,
|
|
|
|
F: Fn(Value) -> Result<Value, Error> + Send + Sync + 'static,
|
2019-04-08 15:59:39 +00:00
|
|
|
{
|
|
|
|
let path: PathBuf = path.into();
|
2019-04-09 09:47:23 +00:00
|
|
|
|
2019-04-08 15:59:39 +00:00
|
|
|
let socket = UnixListener::bind(&path)?;
|
|
|
|
|
|
|
|
let f = Arc::new(f);
|
2019-04-09 09:47:23 +00:00
|
|
|
let path2 = Arc::new(path);
|
|
|
|
let path3 = path2.clone();
|
2019-04-08 15:59:39 +00:00
|
|
|
|
|
|
|
let control_future = socket.incoming()
|
2019-04-11 08:51:59 +00:00
|
|
|
.map_err(Error::from)
|
|
|
|
.and_then(|conn| {
|
2019-08-23 13:00:18 +00:00
|
|
|
use futures::future::{err, ok};
|
|
|
|
|
2019-04-11 08:51:59 +00:00
|
|
|
// check permissions (same gid, or root user)
|
|
|
|
let opt = socket::sockopt::PeerCredentials {};
|
|
|
|
match socket::getsockopt(conn.as_raw_fd(), opt) {
|
|
|
|
Ok(cred) => {
|
|
|
|
let mygid = unsafe { libc::getgid() };
|
|
|
|
if !(cred.uid() == 0 || cred.gid() == mygid) {
|
2019-08-23 13:00:18 +00:00
|
|
|
return err(format_err!("no permissions for {:?}", cred));
|
2019-04-11 08:51:59 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-23 13:00:18 +00:00
|
|
|
Err(e) => {
|
|
|
|
return err(format_err!(
|
|
|
|
"no permissions - unable to read peer credential - {}",
|
|
|
|
e,
|
|
|
|
));
|
|
|
|
}
|
2019-04-11 08:51:59 +00:00
|
|
|
}
|
2019-08-23 13:00:18 +00:00
|
|
|
ok(conn)
|
2019-04-11 08:51:59 +00:00
|
|
|
})
|
2019-04-08 15:59:39 +00:00
|
|
|
.map_err(move |err| { eprintln!("failed to accept on control socket {:?}: {}", path2, err); })
|
2019-08-23 13:00:18 +00:00
|
|
|
.try_for_each(move |conn| {
|
|
|
|
let f = Arc::clone(&f);
|
2019-04-08 15:59:39 +00:00
|
|
|
|
|
|
|
let (rx, mut tx) = conn.split();
|
|
|
|
let path = path3.clone();
|
|
|
|
|
2019-08-23 13:00:18 +00:00
|
|
|
let abort_future = super::last_worker_future().map(|_| ());
|
|
|
|
|
|
|
|
use tokio::io::{AsyncBufReadExt, AsyncWriteExt};
|
|
|
|
tokio::spawn(futures::future::select(
|
|
|
|
async move {
|
|
|
|
let mut rx = tokio::io::BufReader::new(rx);
|
|
|
|
let mut line = String::new();
|
|
|
|
loop {
|
|
|
|
line.clear();
|
|
|
|
match rx.read_line({ line.clear(); &mut line }).await {
|
|
|
|
Ok(0) => break,
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(err) => {
|
|
|
|
eprintln!("control socket {:?} read error: {}", path, err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2019-04-08 15:59:39 +00:00
|
|
|
|
2019-08-23 13:00:18 +00:00
|
|
|
let response = match line.parse::<Value>() {
|
|
|
|
Ok(param) => match f(param) {
|
|
|
|
Ok(res) => format!("OK: {}\n", res),
|
|
|
|
Err(err) => format!("ERROR: {}\n", err),
|
|
|
|
}
|
2019-04-10 09:05:00 +00:00
|
|
|
Err(err) => format!("ERROR: {}\n", err),
|
|
|
|
};
|
2019-08-23 13:00:18 +00:00
|
|
|
|
|
|
|
if let Err(err) = tx.write_all(response.as_bytes()).await {
|
|
|
|
eprintln!("control socket {:?} write response error: {}", path, err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}.boxed(),
|
|
|
|
abort_future,
|
|
|
|
).map(|_| ()));
|
|
|
|
futures::future::ok(())
|
2019-04-08 15:59:39 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
let abort_future = super::last_worker_future().map_err(|_| {});
|
2019-08-23 13:00:18 +00:00
|
|
|
let task = futures::future::select(
|
|
|
|
control_future,
|
|
|
|
abort_future,
|
|
|
|
).map(|_| ());
|
2019-04-08 15:59:39 +00:00
|
|
|
|
|
|
|
Ok(task)
|
|
|
|
}
|
2019-04-10 10:42:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
pub fn send_command<P>(
|
|
|
|
path: P,
|
|
|
|
params: Value
|
2019-08-23 13:00:18 +00:00
|
|
|
) -> impl Future<Output = Result<Value, Error>>
|
2019-04-10 10:42:24 +00:00
|
|
|
where P: Into<PathBuf>,
|
|
|
|
|
|
|
|
{
|
|
|
|
let path: PathBuf = path.into();
|
|
|
|
|
|
|
|
tokio::net::UnixStream::connect(path)
|
|
|
|
.map_err(move |err| format_err!("control socket connect failed - {}", err))
|
|
|
|
.and_then(move |conn| {
|
|
|
|
|
2019-08-23 13:00:18 +00:00
|
|
|
let (rx, mut tx) = conn.split();
|
2019-04-10 10:42:24 +00:00
|
|
|
|
|
|
|
let mut command_string = params.to_string();
|
|
|
|
command_string.push('\n');
|
|
|
|
|
2019-08-23 13:00:18 +00:00
|
|
|
async move {
|
|
|
|
use tokio::io::{AsyncBufReadExt, AsyncWriteExt};
|
|
|
|
|
|
|
|
tx.write_all(command_string.as_bytes()).await?;
|
|
|
|
tx.shutdown().await?;
|
|
|
|
let mut rx = tokio::io::BufReader::new(rx);
|
|
|
|
let mut data = String::new();
|
|
|
|
if rx.read_line(&mut data).await? == 0 {
|
|
|
|
bail!("no response");
|
|
|
|
}
|
|
|
|
if data.starts_with("OK: ") {
|
|
|
|
match data[4..].parse::<Value>() {
|
|
|
|
Ok(v) => Ok(v),
|
|
|
|
Err(err) => bail!("unable to parse json response - {}", err),
|
|
|
|
}
|
|
|
|
} else if data.starts_with("ERROR: ") {
|
|
|
|
bail!("{}", &data[7..]);
|
|
|
|
} else {
|
|
|
|
bail!("unable to parse response: {}", data);
|
|
|
|
}
|
|
|
|
}
|
2019-04-10 10:42:24 +00:00
|
|
|
})
|
|
|
|
}
|