diff --git a/bin/common/mod.rs b/bin/common/mod.rs index d82fa555..34efc67a 100644 --- a/bin/common/mod.rs +++ b/bin/common/mod.rs @@ -7,3 +7,8 @@ pub mod daemonize; pub mod logging; pub mod monitor; pub mod validator; + +pub const EXIT_CODE_SERVER_EXIT_UNEXPECTLY: i32 = 1; +pub const EXIT_CODE_SERVER_ABORTED: i32 = 2; +pub const EXIT_CODE_LOAD_CONFIG_FAILURE: i32 = 6; +pub const EXIT_CODE_LOAD_ACL_FAILURE: i32 = 7; diff --git a/bin/sslocal.rs b/bin/sslocal.rs index 137df3d0..9640c938 100644 --- a/bin/sslocal.rs +++ b/bin/sslocal.rs @@ -4,7 +4,7 @@ //! or you could specify a configuration file. The format of configuration file is defined //! in mod `config`. -use std::time::Duration; +use std::{process, time::Duration}; use clap::{clap_app, Arg}; use futures::future::{self, Either}; @@ -200,7 +200,8 @@ fn main() { Some(cpath) => match Config::load_from_file(cpath, ConfigType::Local) { Ok(cfg) => cfg, Err(err) => { - panic!("loading config \"{}\", {}", cpath, err); + eprintln!("loading config \"{}\", {}", cpath, err); + process::exit(common::EXIT_CODE_LOAD_CONFIG_FAILURE); } }, None => Config::new(ConfigType::Local), @@ -376,7 +377,8 @@ fn main() { let acl = match AccessControl::load_from_file(acl_file) { Ok(acl) => acl, Err(err) => { - panic!("loading ACL \"{}\", {}", acl_file, err); + eprintln!("loading ACL \"{}\", {}", acl_file, err); + process::exit(common::EXIT_CODE_LOAD_ACL_FAILURE); } }; config.acl = Some(acl); @@ -474,9 +476,15 @@ fn main() { match future::select(server, abort_signal).await { // Server future resolved without an error. This should never happen. - Either::Left((Ok(..), ..)) => panic!("server exited unexpectly"), + Either::Left((Ok(..), ..)) => { + eprintln!("server exited unexpectly"); + process::exit(common::EXIT_CODE_SERVER_EXIT_UNEXPECTLY); + } // Server future resolved with error, which are listener errors in most cases - Either::Left((Err(err), ..)) => panic!("aborted with {}", err), + Either::Left((Err(err), ..)) => { + eprintln!("server aborted with {}", err); + process::exit(common::EXIT_CODE_SERVER_ABORTED); + } // The abort signal future resolved. Means we should just exit. Either::Right(_) => (), } diff --git a/bin/ssmanager.rs b/bin/ssmanager.rs index e967b53e..2093b533 100644 --- a/bin/ssmanager.rs +++ b/bin/ssmanager.rs @@ -7,7 +7,7 @@ //! *It should be notice that the extented configuration file is not suitable for the server //! side.* -use std::{net::IpAddr, time::Duration}; +use std::{net::IpAddr, process, time::Duration}; use clap::{clap_app, Arg}; use futures::future::{self, Either}; @@ -140,7 +140,8 @@ fn main() { Some(cpath) => match Config::load_from_file(cpath, ConfigType::Manager) { Ok(cfg) => cfg, Err(err) => { - panic!("loading config \"{}\", {}", cpath, err); + eprintln!("loading config \"{}\", {}", cpath, err); + process::exit(common::EXIT_CODE_LOAD_CONFIG_FAILURE); } }, None => Config::new(ConfigType::Manager), @@ -221,7 +222,8 @@ fn main() { let acl = match AccessControl::load_from_file(acl_file) { Ok(acl) => acl, Err(err) => { - panic!("loading ACL \"{}\", {}", acl_file, err); + eprintln!("loading ACL \"{}\", {}", acl_file, err); + process::exit(common::EXIT_CODE_LOAD_ACL_FAILURE); } }; config.acl = Some(acl); @@ -308,9 +310,15 @@ fn main() { match future::select(server, abort_signal).await { // Server future resolved without an error. This should never happen. - Either::Left((Ok(..), ..)) => panic!("server exited unexpectly"), + Either::Left((Ok(..), ..)) => { + eprintln!("server exited unexpectly"); + process::exit(common::EXIT_CODE_SERVER_EXIT_UNEXPECTLY); + } // Server future resolved with error, which are listener errors in most cases - Either::Left((Err(err), ..)) => panic!("aborted with {}", err), + Either::Left((Err(err), ..)) => { + eprintln!("server aborted with {}", err); + process::exit(common::EXIT_CODE_SERVER_ABORTED); + } // The abort signal future resolved. Means we should just exit. Either::Right(_) => (), } diff --git a/bin/ssserver.rs b/bin/ssserver.rs index a2fc9859..171762d8 100644 --- a/bin/ssserver.rs +++ b/bin/ssserver.rs @@ -7,7 +7,7 @@ //! *It should be notice that the extented configuration file is not suitable for the server //! side.* -use std::{net::IpAddr, time::Duration}; +use std::{net::IpAddr, process, time::Duration}; use clap::{clap_app, Arg}; use futures::future::{self, Either}; @@ -142,7 +142,8 @@ fn main() { Some(cpath) => match Config::load_from_file(cpath, ConfigType::Server) { Ok(cfg) => cfg, Err(err) => { - panic!("loading config \"{}\", {}", cpath, err); + eprintln!("loading config \"{}\", {}", cpath, err); + process::exit(common::EXIT_CODE_LOAD_CONFIG_FAILURE); } }, None => Config::new(ConfigType::Server), @@ -231,7 +232,8 @@ fn main() { let acl = match AccessControl::load_from_file(acl_file) { Ok(acl) => acl, Err(err) => { - panic!("loading ACL \"{}\", {}", acl_file, err); + eprintln!("loading ACL \"{}\", {}", acl_file, err); + process::exit(common::EXIT_CODE_LOAD_ACL_FAILURE); } }; config.acl = Some(acl); @@ -319,9 +321,15 @@ fn main() { match future::select(server, abort_signal).await { // Server future resolved without an error. This should never happen. - Either::Left((Ok(..), ..)) => panic!("server exited unexpectly"), + Either::Left((Ok(..), ..)) => { + eprintln!("server exited unexpectly"); + process::exit(common::EXIT_CODE_SERVER_EXIT_UNEXPECTLY); + } // Server future resolved with error, which are listener errors in most cases - Either::Left((Err(err), ..)) => panic!("aborted with {}", err), + Either::Left((Err(err), ..)) => { + eprintln!("server aborted with {}", err); + process::exit(common::EXIT_CODE_SERVER_ABORTED); + } // The abort signal future resolved. Means we should just exit. Either::Right(_) => (), }