aboutsummaryrefslogtreecommitdiffstats
path: root/src/bin/rbw-agent/main.rs
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2020-04-12 00:08:03 -0400
committerJesse Luehrs <doy@tozt.net>2020-04-12 00:08:03 -0400
commit236f06736e45c2a70f43589c9d447a0a3ef240b5 (patch)
treec390d4cbfec5223ac1aefe3947f8e1bb885757d2 /src/bin/rbw-agent/main.rs
parent91d1d1890bdc3ee75b69e00d5368c5b29a4f461c (diff)
downloadrbw-236f06736e45c2a70f43589c9d447a0a3ef240b5.tar.gz
rbw-236f06736e45c2a70f43589c9d447a0a3ef240b5.zip
improve error handling and reporting
Diffstat (limited to 'src/bin/rbw-agent/main.rs')
-rw-r--r--src/bin/rbw-agent/main.rs47
1 files changed, 40 insertions, 7 deletions
diff --git a/src/bin/rbw-agent/main.rs b/src/bin/rbw-agent/main.rs
index 235c4f2..622778e 100644
--- a/src/bin/rbw-agent/main.rs
+++ b/src/bin/rbw-agent/main.rs
@@ -1,24 +1,57 @@
+use anyhow::Context as _;
+
mod actions;
mod agent;
mod daemon;
mod sock;
-fn main() {
+async fn tokio_main(
+ startup_ack: crate::daemon::StartupAck,
+) -> anyhow::Result<()> {
+ let listener =
+ crate::sock::listen().context("failed to listen on socket")?;
+
+ startup_ack.ack()?;
+
+ let mut agent = crate::agent::Agent::new()?;
+ agent.run(listener).await?;
+
+ Ok(())
+}
+
+fn real_main() -> anyhow::Result<()> {
env_logger::from_env(
env_logger::Env::default().default_filter_or("info"),
)
.init();
- let startup_ack = daemon::daemonize();
+ let startup_ack = daemon::daemonize().context("failed to daemonize")?;
+ let (w, r) = std::sync::mpsc::channel();
// can't use tokio::main because we need to daemonize before starting the
// tokio runloop, or else things break
+ // unwrap is fine here because there's no good reason that this should
+ // ever fail
tokio::runtime::Runtime::new().unwrap().block_on(async {
- let listener = crate::sock::listen();
+ if let Err(e) = tokio_main(startup_ack).await {
+ // this unwrap is fine because it's the only real option here
+ w.send(e).unwrap();
+ }
+ });
+
+ if let Ok(e) = r.recv() {
+ return Err(e);
+ }
- startup_ack.ack();
+ Ok(())
+}
+
+fn main() {
+ let res = real_main();
- let mut agent = crate::agent::Agent::new();
- agent.run(listener.unwrap()).await;
- })
+ if let Err(e) = res {
+ // XXX log file?
+ eprintln!("{:#}", e);
+ std::process::exit(1);
+ }
}