synit/synit-pid1/src/main.rs

75 lines
2.1 KiB
Rust
Raw Normal View History

2021-08-25 15:40:39 +00:00
use clap::Clap;
use std::sync::Arc;
use syndicate::actor::*;
use syndicate::relay;
use syndicate::sturdy;
use syndicate::value::NestedValue;
use syndicate::value::Value;
use tokio::process;
#[derive(Clap, Clone, Debug)]
#[clap(version)]
pub struct CommandLine {
#[clap(long, default_value="/sbin/syndicate-server")]
server_path: String,
}
pub struct Pid1Listener {
config: Arc<CommandLine>,
}
impl Pid1Listener {
fn new(config: Arc<CommandLine>) -> Self {
Self {
config,
}
}
}
impl Entity<AnyValue> for Pid1Listener {
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
syndicate::convenient_logging()?;
match nix::unistd::setsid() {
Ok(_pid) => tracing::info!("setsid(2): new session is {}", _pid),
Err(e) => tracing::info!("setsid(2) failed: {:?}", &e),
}
let config = Arc::new(CommandLine::parse());
let server = process::Command::new(&config.server_path)
.arg("--inferior")
.arg("--port")
.arg("1")
.arg("--socket")
.arg("/run/ds")
.stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::piped())
// .stderr(std::process::Stdio::null())
.spawn()
.expect("Could not spawn main server");
Actor::new().boot(syndicate::name!("server-relay"), |t| {
let listener = t.state.create(Pid1Listener::new(config));
let from_server = server.stdout.expect("Missing dataspace server stdout");
let to_server = server.stdin.expect("Missing dataspace server stdin");
let ds = &relay::TunnelRelay::run(t,
relay::Input::Bytes(Box::pin(from_server)),
relay::Output::Bytes(Box::pin(to_server)),
None,
Some(sturdy::Oid(0.into())))
.expect("Missing reference to dataspace")
.underlying;
t.assert(ds, Value::simple_record1("pid1", AnyValue::domain(Cap::new(&listener))).wrap());
Ok(())
}).await??;
Ok(())
}