summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJustus Winter <justus@sequoia-pgp.org>2018-01-25 17:54:41 +0100
committerJustus Winter <justus@sequoia-pgp.org>2018-01-25 18:10:54 +0100
commit2cd407355b3813e95f9871e9de778308a4433469 (patch)
treebb484d50c0e325ccb8cc023f4d968b80eb476b4f
parent10a5646d02d48a14146c960a0223561667364a52 (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.
-rw-r--r--net/src/ipc.rs60
-rw-r--r--store/src/server.rs22
2 files changed, 61 insertions, 21 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) })
}
diff --git a/store/src/server.rs b/store/src/server.rs
index 281245d8..912bbd00 100644
--- a/store/src/server.rs
+++ b/store/src/server.rs
@@ -1,28 +1,14 @@
-use std::env::args;
-use std::process::exit;
-
extern crate sequoia_core;
extern crate sequoia_net;
extern crate sequoia_store;
-use sequoia_core::Context;
use sequoia_net::ipc::Server;
fn main() {
- let argv: Vec<String> = args().collect();
- let argc = argv.len();
-
- if argc != 3 || argv[1] != "--home" {
- eprintln!("Usage: {} --home <HOMEDIR>", argv[0]);
- exit(1);
- }
-
- let ctx = Context::configure("org.example.sequoia")
- .home(&argv[2]).build()
- .expect("Failed to create context.");
-
+ let ctx = Server::context()
+ .expect("Failed to create context");
Server::new(sequoia_store::descriptor(&ctx))
- .expect("Failed to create server.")
+ .expect("Failed to create server")
.serve()
- .expect("Failed to start server.");
+ .expect("Failed to start server");
}