Skip to content

Commit

Permalink
Abort plugin if specific signal received
Browse files Browse the repository at this point in the history
  • Loading branch information
zonyitoo committed Jul 29, 2017
1 parent be8b25d commit b6c5b04
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 1 deletion.
26 changes: 26 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "shadowsocks-rust"
version = "1.6.0"
version = "1.6.1"
authors = ["Y. T. CHUNG <[email protected]>"]
description = "shadowsocks is a fast tunnel proxy that helps you bypass firewalls."
repository = "https://github.com/zonyitoo/shadowsocks-rust"
Expand Down Expand Up @@ -59,3 +59,6 @@ url = "1.5"
[dependencies.sodiumoxide]
version = "0.0.15"
optional = true

[target.'cfg(unix)'.dependencies]
tokio-signal = "0.1"
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ extern crate lazy_static;
extern crate scoped_tls;

extern crate subprocess;
#[cfg(unix)]
extern crate tokio_signal;

/// ShadowSocks version
pub const VERSION: &'static str = env!("CARGO_PKG_VERSION");
Expand All @@ -114,3 +116,4 @@ pub mod config;
pub mod relay;
pub mod crypto;
pub mod plugin;
mod monitor;
40 changes: 40 additions & 0 deletions src/monitor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use std::io;

use libc;
use tokio_core::reactor::Handle;

use futures::{Future, Stream};

#[cfg(unix)]
pub fn monitor_signal(handle: &Handle) {
use tokio_signal::unix::Signal;

let fut = Signal::new(libc::SIGCHLD, handle)
.and_then(|signal| {
signal.for_each(|_| -> Result<(), io::Error> {
panic!("Plugin exited unexpectly (SIGCHLD)");
})
})
.map_err(|err| {
error!("Failed to monitor SIGCHLD, err: {:?}", err);
});

handle.spawn(fut);

let fut = Signal::new(libc::SIGTERM, handle)
.and_then(|sigterm| {
sigterm.for_each(|_| -> Result<(), io::Error> {
panic!("Received SIGTERM, aborting process");
})
})
.map_err(|err| {
error!("Failed to monitor SIGTERM, SIGINT, err: {:?}", err);
});

handle.spawn(fut);
}

#[cfg(not(unix))]
pub fn monitor_signal(handle: &Handle) {
// FIXME: Do nothing ...
}
1 change: 1 addition & 0 deletions src/plugin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ impl Plugin {

impl Drop for Plugin {
fn drop(&mut self) {
debug!("Killing Plugin {:?}", self.process);
let _ = self.process.terminate();
}
}
Expand Down
1 change: 1 addition & 0 deletions src/relay/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub fn run(mut config: Config) -> io::Result<()> {

// Hold it here, kill all plugins when Core is finished
let _plugins = launch_plugin(&mut config, PluginMode::Client)?;
::monitor::monitor_signal(&handle);

let context = Context::new(handle, config);
Context::set(&context, move || if enable_udp {
Expand Down
1 change: 1 addition & 0 deletions src/relay/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub fn run(mut config: Config) -> io::Result<()> {

// Hold it here, kill all plugins when Core is finished
let _plugins = launch_plugin(&mut config, PluginMode::Server)?;
::monitor::monitor_signal(&handle);

let context = Context::new(handle, config);
Context::set(&context, move || if enable_udp {
Expand Down

0 comments on commit b6c5b04

Please sign in to comment.