2020-04-17 12:11:25 +00:00
|
|
|
use anyhow::{bail, format_err, Error};
|
2019-04-08 15:59:39 +00:00
|
|
|
|
|
|
|
use futures::*;
|
|
|
|
|
2019-12-12 14:27:07 +00:00
|
|
|
use tokio::net::UnixListener;
|
2019-04-08 15:59:39 +00:00
|
|
|
|
|
|
|
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-12-12 14:27:07 +00:00
|
|
|
pub fn create_control_socket<P, F>(path: P, func: F) -> Result<impl Future<Output = ()>, Error>
|
2019-08-23 13:00:18 +00:00
|
|
|
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-12-12 14:27:07 +00:00
|
|
|
let mut socket = UnixListener::bind(&path)?;
|
2019-04-08 15:59:39 +00:00
|
|
|
|
2019-12-12 14:27:07 +00:00
|
|
|
let func = Arc::new(func);
|
2019-04-08 15:59:39 +00:00
|
|
|
|
2019-12-12 14:27:07 +00:00
|
|
|
let control_future = async move {
|
|
|
|
loop {
|
|
|
|
let (conn, _addr) = socket
|
|
|
|
.accept()
|
|
|
|
.await
|
|
|
|
.map_err(|err| {
|
|
|
|
format_err!("failed to accept on control socket {:?}: {}", path, err)
|
|
|
|
})?;
|
2019-08-23 13:00:18 +00:00
|
|
|
|
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-12-12 14:27:07 +00:00
|
|
|
bail!("no permissions for {:?}", cred);
|
2019-04-11 08:51:59 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-12 14:27:07 +00:00
|
|
|
Err(e) => bail!("no permissions - unable to read peer credential - {}", e),
|
2019-04-11 08:51:59 +00:00
|
|
|
}
|
2019-04-08 15:59:39 +00:00
|
|
|
|
2019-12-12 14:27:07 +00:00
|
|
|
let (rx, mut tx) = tokio::io::split(conn);
|
2019-04-08 15:59:39 +00:00
|
|
|
|
2019-08-23 13:00:18 +00:00
|
|
|
let abort_future = super::last_worker_future().map(|_| ());
|
|
|
|
|
|
|
|
use tokio::io::{AsyncBufReadExt, AsyncWriteExt};
|
2019-12-12 14:27:07 +00:00
|
|
|
let func = Arc::clone(&func);
|
|
|
|
let path = path.clone();
|
2019-08-23 13:00:18 +00:00
|
|
|
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>() {
|
2019-12-12 14:27:07 +00:00
|
|
|
Ok(param) => match func(param) {
|
2019-08-23 13:00:18 +00:00
|
|
|
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(|_| ()));
|
2019-12-12 14:27:07 +00:00
|
|
|
}
|
|
|
|
}.boxed();
|
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,
|
2019-12-12 14:27:07 +00:00
|
|
|
).map(|_: futures::future::Either<(Result<(), Error>, _), _>| ());
|
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))
|
2019-12-12 14:27:07 +00:00
|
|
|
.and_then(move |mut conn| {
|
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};
|
|
|
|
|
2019-12-12 14:27:07 +00:00
|
|
|
conn.write_all(command_string.as_bytes()).await?;
|
|
|
|
AsyncWriteExt::shutdown(&mut conn).await?;
|
|
|
|
let mut rx = tokio::io::BufReader::new(conn);
|
2019-08-23 13:00:18 +00:00
|
|
|
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
|
|
|
})
|
|
|
|
}
|