From 49523779d905e3d1d87a75de1fb18bca9d376094 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Frosteg=C3=A5rd?= Date: Wed, 6 Apr 2022 00:41:15 +0200 Subject: [PATCH] common: add PanicSentinel, improve PrivilegeDropper anyhow context --- aquatic_common/src/lib.rs | 17 +++++++++++++++++ aquatic_common/src/privileges.rs | 2 +- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/aquatic_common/src/lib.rs b/aquatic_common/src/lib.rs index b243cfd..0a66014 100644 --- a/aquatic_common/src/lib.rs +++ b/aquatic_common/src/lib.rs @@ -30,6 +30,23 @@ impl ValidUntil { } } +/// Raises SIGTERM when dropped +/// +/// Pass to threads to have panics in them cause whole program to exit. +#[derive(Clone)] +pub struct PanicSentinel; + +impl Drop for PanicSentinel { + fn drop(&mut self) { + if unsafe { libc::raise(15) } == -1 { + panic!( + "Could not raise SIGTERM: {:#}", + ::std::io::Error::last_os_error() + ) + } + } +} + /// Extract response peers /// /// If there are more peers in map than `max_num_peers_to_take`, do a diff --git a/aquatic_common/src/privileges.rs b/aquatic_common/src/privileges.rs index d252996..eb41282 100644 --- a/aquatic_common/src/privileges.rs +++ b/aquatic_common/src/privileges.rs @@ -55,7 +55,7 @@ impl PrivilegeDropper { .group(self.config.group.clone()) .user(self.config.user.clone()) .apply() - .with_context(|| "drop privileges")?; + .with_context(|| "couldn't drop privileges after socket creation")?; } }