diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2018-01-25 17:54:41 +0100 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2018-01-25 18:10:54 +0100 |
commit | 2cd407355b3813e95f9871e9de778308a4433469 (patch) | |
tree | bb484d50c0e325ccb8cc023f4d968b80eb476b4f /net | |
parent | 10a5646d02d48a14146c960a0223561667364a52 (diff) |
net: Serialize the Context and send it to server.
- Provide external servers with a more useful context. This is not
so critical for the keystore, but could be interesting for servers
providing secret key services.
Diffstat (limited to 'net')
-rw-r--r-- | net/src/ipc.rs | 60 |
1 files changed, 57 insertions, 3 deletions
diff --git a/net/src/ipc.rs b/net/src/ipc.rs index 31cd16d6..56363ddc 100644 --- a/net/src/ipc.rs +++ b/net/src/ipc.rs @@ -39,6 +39,7 @@ extern crate fs2; use self::fs2::FileExt; +use failure; use std::fs; use std::io::{self, Read, Write}; use std::net::{SocketAddr, AddrParseError, TcpStream, TcpListener}; @@ -240,6 +241,10 @@ impl Descriptor { Command::new(&self.executable.clone().into_os_string()) .arg("--home") .arg(self.ctx.home().to_string_lossy().into_owned()) + .arg("--lib") + .arg(self.ctx.home().to_string_lossy().into_owned()) + .arg("--ephemeral") + .arg(format!("{}", self.ctx.ephemeral())) // l will be closed here if the exec fails. .stdin(unsafe { Stdio::from_raw_fd(fd) }) .spawn()?; @@ -273,9 +278,58 @@ impl Server { }) } - /// Turn the current process into an descriptor. External servers - /// must call this early on. Expects 'stdin' to be a listening - /// TCP socket. + /// Creates a Context from `env::args()`. + pub fn context() -> Result<core::Context, failure::Error> { + use std::env::args; + let args: Vec<String> = args().collect(); + + if args.len() != 7 || args[1] != "--home" + || args[3] != "--lib" || args[5] != "--ephemeral" { + return Err(format_err!( + "Usage: {} --home <HOMEDIR> --lib <LIBDIR> \ + --ephemeral true|false", args[0])); + } + + let mut cfg = core::Context::configure("org.sequoia.api.server") + .home(&args[2]).lib(&args[4]); + + if let Ok(ephemeral) = args[6].parse() { + if ephemeral { + cfg.set_ephemeral(); + } + } else { + return Err(format_err!( + "Expected 'true' or 'false' for --ephemeral, got: {}", + args[6])); + } + + cfg.build() + } + + /// Turns this process into a server. + /// + /// External servers must call this early on. Expects 'stdin' to + /// be a listening TCP socket. + /// + /// # Example + /// + /// ```compile_fail + /// // We cannot run this because sequoia-store is not built yet. + /// extern crate sequoia_core; + /// extern crate sequoia_net; + /// extern crate sequoia_store; + /// + /// use sequoia_net::ipc::Server; + /// + /// fn main() { + /// let ctx = Server::context() + /// .expect("Failed to create context"); + /// Server::new(sequoia_store::descriptor(&ctx)) + /// .expect("Failed to create server") + /// .serve() + /// .expect("Failed to start server"); + /// } + /// ``` pub fn serve(&mut self) -> io::Result<()> { self.serve_listener(unsafe { TcpListener::from_raw_fd(0) }) } |