Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
signals: Move signal handling to its own crate
cloudhead committed 2 years ago
commit 28014031826b79e4d99abf6368483b8cb8a8104a
parent 425e7b50928d5a9175f57d7455c9d4ecd7e253ae
9 files changed +118 -60
modified Cargo.lock
@@ -2551,6 +2551,7 @@ dependencies = [
 "radicle-crypto",
 "radicle-fetch",
 "radicle-git-ext",
+
 "radicle-signals",
 "scrypt",
 "serde",
 "serde_json",
@@ -2573,6 +2574,14 @@ dependencies = [
]

[[package]]
+
name = "radicle-signals"
+
version = "0.9.0"
+
dependencies = [
+
 "crossbeam-channel",
+
 "libc",
+
]
+

+
[[package]]
name = "radicle-ssh"
version = "0.9.0"
dependencies = [
modified Cargo.toml
@@ -14,6 +14,7 @@ members = [
  "radicle-remote-helper",
  "radicle-ssh",
  "radicle-tools",
+
  "radicle-signals",
]
default-members = [
  "radicle",
@@ -25,6 +26,7 @@ default-members = [
  "radicle-ssh",
  "radicle-remote-helper",
  "radicle-term",
+
  "radicle-signals",
]
resolver = "2"

modified radicle-node/Cargo.toml
@@ -50,6 +50,10 @@ features = ["logger"]
path = "../radicle-fetch"
version = "0.9.0"

+
[dependencies.radicle-signals]
+
path = "../radicle-signals"
+
version = "0"
+

[dev-dependencies]
radicle = { path = "../radicle", version = "0", features = ["test"] }
radicle-crypto = { path = "../radicle-crypto", version = "0", features = ["test", "cyphernet"] }
modified radicle-node/src/lib.rs
@@ -3,7 +3,6 @@ pub mod control;
pub mod deserializer;
pub mod runtime;
pub mod service;
-
pub mod signals;
#[cfg(any(test, feature = "test"))]
pub mod test;
#[cfg(test)]
modified radicle-node/src/main.rs
@@ -9,8 +9,8 @@ use radicle::prelude::Signer;
use radicle::profile;
use radicle::version::Version;
use radicle_node::crypto::ssh::keystore::{Keystore, MemorySigner};
-
use radicle_node::signals;
use radicle_node::Runtime;
+
use radicle_signals as signals;

pub const VERSION: Version = Version {
    name: env!("CARGO_PKG_NAME"),
modified radicle-node/src/runtime.rs
@@ -10,6 +10,7 @@ use crossbeam_channel as chan;
use cyphernet::Ecdh;
use netservices::resource::NetAccept;
use radicle_fetch::FetchLimit;
+
use radicle_signals::Signal;
use reactor::poller::popol;
use reactor::Reactor;
use thiserror::Error;
@@ -128,7 +129,7 @@ pub struct Runtime {
    pub reactor: Reactor<wire::Control, popol::Poller>,
    pub pool: worker::Pool,
    pub local_addrs: Vec<net::SocketAddr>,
-
    pub signals: chan::Receiver<()>,
+
    pub signals: chan::Receiver<Signal>,
}

impl Runtime {
@@ -139,7 +140,7 @@ impl Runtime {
        home: Home,
        config: service::Config,
        listen: Vec<net::SocketAddr>,
-
        signals: chan::Receiver<()>,
+
        signals: chan::Receiver<Signal>,
        signer: G,
    ) -> Result<Runtime, Error>
    where
@@ -306,7 +307,7 @@ impl Runtime {
            || control::listen(self.control, handle)
        });
        let _signals = thread::spawn(&self.id, "signals", move || {
-
            if let Ok(()) = self.signals.recv() {
+
            if let Ok(Signal::Terminate | Signal::Interrupt) = self.signals.recv() {
                log::info!(target: "node", "Termination signal received; shutting down..");
                self.handle.shutdown().ok();
            }
deleted radicle-node/src/signals.rs
@@ -1,55 +0,0 @@
-
use std::io;
-
use std::sync::Mutex;
-

-
use crossbeam_channel as chan;
-

-
/// Signal notifications are sent via this channel.
-
static NOTIFY: Mutex<Option<chan::Sender<()>>> = Mutex::new(None);
-

-
/// Install global signal handlers for `SIGTERM` and `SIGINT`.
-
pub fn install(notify: chan::Sender<()>) -> io::Result<()> {
-
    if let Ok(mut channel) = NOTIFY.try_lock() {
-
        if channel.is_some() {
-
            return Err(io::Error::new(
-
                io::ErrorKind::AlreadyExists,
-
                "signal handler is already installed",
-
            ));
-
        }
-
        *channel = Some(notify);
-

-
        unsafe { _install() }?;
-
    } else {
-
        return Err(io::Error::new(
-
            io::ErrorKind::WouldBlock,
-
            "unable to install signal handler",
-
        ));
-
    }
-
    Ok(())
-
}
-

-
/// Install global signal handlers for `SIGTERM` and `SIGINT`.
-
///
-
/// # Safety
-
///
-
/// Calls `libc` functions safely.
-
unsafe fn _install() -> io::Result<()> {
-
    if libc::signal(libc::SIGTERM, handler as libc::sighandler_t) == libc::SIG_ERR {
-
        return Err(io::Error::last_os_error());
-
    }
-
    if libc::signal(libc::SIGINT, handler as libc::sighandler_t) == libc::SIG_ERR {
-
        return Err(io::Error::last_os_error());
-
    }
-
    Ok(())
-
}
-

-
/// Called by `libc` when a signal is received.
-
extern "C" fn handler(sig: libc::c_int, _info: *mut libc::siginfo_t, _data: *mut libc::c_void) {
-
    if sig != libc::SIGTERM && sig != libc::SIGINT {
-
        return;
-
    }
-
    if let Ok(guard) = NOTIFY.try_lock() {
-
        if let Some(c) = &*guard {
-
            c.try_send(()).ok();
-
        }
-
    }
-
}
added radicle-signals/Cargo.toml
@@ -0,0 +1,10 @@
+
[package]
+
name = "radicle-signals"
+
homepage = "https://radicle.xyz"
+
repository = "https://app.radicle.xyz/seeds/seed.radicle.xyz/rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5"
+
edition = "2021"
+
version = "0.9.0"
+

+
[dependencies]
+
crossbeam-channel = { version = "0.5.6" }
+
libc = { version = "0.2" }
added radicle-signals/src/lib.rs
@@ -0,0 +1,88 @@
+
use std::io;
+
use std::sync::Mutex;
+

+
use crossbeam_channel as chan;
+

+
/// Operating system signal.
+
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
+
pub enum Signal {
+
    /// `SIGINT`.
+
    Interrupt,
+
    /// `SIGTERM`.
+
    Terminate,
+
    /// `SIGHUP`.
+
    Hangup,
+
    /// `SIGWINCH`.
+
    WindowChanged,
+
}
+

+
impl TryFrom<i32> for Signal {
+
    type Error = i32;
+

+
    fn try_from(value: i32) -> Result<Self, Self::Error> {
+
        match value {
+
            libc::SIGTERM => Ok(Self::Terminate),
+
            libc::SIGINT => Ok(Self::Interrupt),
+
            libc::SIGWINCH => Ok(Self::WindowChanged),
+
            libc::SIGHUP => Ok(Self::Hangup),
+
            _ => Err(value),
+
        }
+
    }
+
}
+

+
/// Signal notifications are sent via this channel.
+
static NOTIFY: Mutex<Option<chan::Sender<Signal>>> = Mutex::new(None);
+

+
/// Install global signal handlers.
+
pub fn install(notify: chan::Sender<Signal>) -> io::Result<()> {
+
    if let Ok(mut channel) = NOTIFY.try_lock() {
+
        if channel.is_some() {
+
            return Err(io::Error::new(
+
                io::ErrorKind::AlreadyExists,
+
                "signal handler is already installed",
+
            ));
+
        }
+
        *channel = Some(notify);
+

+
        unsafe { _install() }?;
+
    } else {
+
        return Err(io::Error::new(
+
            io::ErrorKind::WouldBlock,
+
            "unable to install signal handler",
+
        ));
+
    }
+
    Ok(())
+
}
+

+
/// Install global signal handlers.
+
///
+
/// # Safety
+
///
+
/// Calls `libc` functions safely.
+
unsafe fn _install() -> io::Result<()> {
+
    if libc::signal(libc::SIGTERM, handler as libc::sighandler_t) == libc::SIG_ERR {
+
        return Err(io::Error::last_os_error());
+
    }
+
    if libc::signal(libc::SIGINT, handler as libc::sighandler_t) == libc::SIG_ERR {
+
        return Err(io::Error::last_os_error());
+
    }
+
    if libc::signal(libc::SIGHUP, handler as libc::sighandler_t) == libc::SIG_ERR {
+
        return Err(io::Error::last_os_error());
+
    }
+
    if libc::signal(libc::SIGWINCH, handler as libc::sighandler_t) == libc::SIG_ERR {
+
        return Err(io::Error::last_os_error());
+
    }
+
    Ok(())
+
}
+

+
/// Called by `libc` when a signal is received.
+
extern "C" fn handler(sig: libc::c_int, _info: *mut libc::siginfo_t, _data: *mut libc::c_void) {
+
    let Ok(sig) = sig.try_into() else {
+
        return;
+
    };
+
    if let Ok(guard) = NOTIFY.try_lock() {
+
        if let Some(c) = &*guard {
+
            c.try_send(sig).ok();
+
        }
+
    }
+
}