Skip to content

Commit

Permalink
Conditional compilation on methods
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelinfosec committed May 15, 2024
1 parent 57dfc39 commit 129fd46
Showing 1 changed file with 16 additions and 19 deletions.
35 changes: 16 additions & 19 deletions crates/tripwire/src/tripwire.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use futures::{stream::Stream};
use futures::stream::Stream;
use futures_util::stream::{select, Select};
use std::{
future::Future,
Expand All @@ -9,11 +9,9 @@ use std::{
use tokio::signal::unix::{signal, SignalKind};
#[cfg(windows)]
use tokio::signal::windows::ctrl_c;
use tokio::sync::{mpsc, watch};
#[cfg(windows)]
use tokio_stream::wrappers::CtrlCStream;
use tokio::{
sync::{mpsc, watch},
};
use tokio_stream::wrappers::{ReceiverStream, WatchStream};
use tracing::{debug, warn};

Expand Down Expand Up @@ -57,21 +55,20 @@ impl Tripwire {
}

/// Listen for SIGTERM and SIGINT
pub fn new_signals() -> (Self, TripwireWorker<Select<impl Stream, impl Stream>>) {
#[cfg(unix)]
{
// For non-Windows platforms, create signal streams for SIGTERM and SIGINT
let sigterms = SignalStream::new(signal(SignalKind::terminate()).unwrap());
let sigints = SignalStream::new(signal(SignalKind::interrupt()).unwrap());
Self::new(select(sigterms, sigints))
}
#[cfg(windows)]
{
// For Windows platforms, create two Ctrl-C signal streams to meet the requirement of `select`
let ctrl_c1 = CtrlCStream::new(ctrl_c().unwrap());
let ctrl_c2 = CtrlCStream::new(ctrl_c().unwrap());
Self::new(select(ctrl_c1, ctrl_c2))
}
#[cfg(unix)]
pub fn new_signals() -> (Self, TripwireWorker<Select<SignalStream, SignalStream>>) {
// For non-Windows platforms, create signal streams for SIGTERM and SIGINT
let sigterms = SignalStream::new(signal(SignalKind::terminate()).unwrap());
let sigints = SignalStream::new(signal(SignalKind::interrupt()).unwrap());
Self::new(select(sigterms, sigints))
}

#[cfg(windows)]
pub fn new_signals() -> (Self, TripwireWorker<Select<CtrlCStream, CtrlCStream>>) {
// For Windows platforms, create two Ctrl-C signal streams to meet the requirement of `select`
let ctrl_c1 = CtrlCStream::new(ctrl_c().unwrap());
let ctrl_c2 = CtrlCStream::new(ctrl_c().unwrap());
Self::new(select(ctrl_c1, ctrl_c2))
}

/// Returns an Arc of the current [TripwireState]
Expand Down

0 comments on commit 129fd46

Please sign in to comment.