From 4b2cdeb9a6742c8110f88ce7cf081178ae0e4dae Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Fri, 1 Feb 2019 09:54:56 +0100 Subject: [PATCH] implement relead_timezone flag --- src/api/router.rs | 13 +++++++++++++ src/api2/node/time.rs | 8 +------- src/server/rest.rs | 15 ++++++++++++++- 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/src/api/router.rs b/src/api/router.rs index 581c5ae0..b159ca4a 100644 --- a/src/api/router.rs +++ b/src/api/router.rs @@ -64,7 +64,12 @@ type ApiHandlerFn = fn(Value, &ApiMethod, &mut dyn RpcEnvironment) -> Result Result; pub struct ApiMethod { + /// The protected flag indicates that the provides function should be forwarded + /// to the deaemon running in priviledged mode. pub protected: bool, + /// This flag indicates that the provided method may change the local timezone, so the server + /// should do a tzset afterwards + pub reload_timezone: bool, pub parameters: ObjectSchema, pub returns: Arc, pub handler: ApiHandlerFn, @@ -78,6 +83,7 @@ impl ApiMethod { handler, returns: Arc::new(Schema::Null), protected: false, + reload_timezone: false, } } @@ -94,6 +100,13 @@ impl ApiMethod { self } + + pub fn reload_timezone(mut self, reload_timezone: bool) -> Self { + + self.reload_timezone = reload_timezone; + + self + } } pub struct ApiAsyncMethod { diff --git a/src/api2/node/time.rs b/src/api2/node/time.rs index 367c893c..b25bfe71 100644 --- a/src/api2/node/time.rs +++ b/src/api2/node/time.rs @@ -32,10 +32,6 @@ fn get_time( })) } -extern "C" { fn tzset(); } - -// Note:: this needs root rights ?? - fn set_timezone( param: Value, _info: &ApiMethod, @@ -57,8 +53,6 @@ fn set_timezone( use std::os::unix::fs::symlink; symlink(path, "/etc/localtime")?; - unsafe { tzset() }; - Ok(Value::Null) } @@ -83,7 +77,7 @@ pub fn router() -> Router { set_timezone, ObjectSchema::new("Set time zone.") .required("timezone", StringSchema::new("Time zone. The file '/usr/share/zoneinfo/zone.tab' contains the list of valid names.")) - ).protected(true) + ).protected(true).reload_timezone(true) ); diff --git a/src/server/rest.rs b/src/server/rest.rs index 49fc0280..a22b23d3 100644 --- a/src/server/rest.rs +++ b/src/server/rest.rs @@ -27,6 +27,8 @@ use hyper::service::{Service, NewService}; use hyper::rt::{Future, Stream}; use hyper::header; +extern "C" { fn tzset(); } + pub struct RestServer { pub api_config: Arc, } @@ -135,6 +137,7 @@ fn get_request_parameters_async( } fn proxy_protected_request( + info: &'static ApiMethod, mut parts: Parts, req_body: Body, ) -> BoxFut @@ -154,6 +157,12 @@ fn proxy_protected_request( .request(request) .map_err(|e| Error::from(e)); + let resp = if info.reload_timezone { + Either::A(resp.then(|resp| {unsafe { tzset() }; resp })) + } else { + Either::B(resp) + }; + return Box::new(resp); } @@ -183,6 +192,10 @@ fn handle_sync_api_request( } }; + if info.reload_timezone { + unsafe { tzset() }; + } + if delay { let delayed_response = tokio::timer::Delay::new(delay_unauth_time) .map_err(|err| http_err!(INTERNAL_SERVER_ERROR, format!("tokio timer delay error: {}", err))) @@ -462,7 +475,7 @@ pub fn handle_request(api: Arc, req: Request) -> BoxFut { MethodDefinition::None => {} MethodDefinition::Simple(api_method) => { if api_method.protected && env_type == RpcEnvironmentType::PUBLIC { - return proxy_protected_request(parts, body); + return proxy_protected_request(api_method, parts, body); } else { return handle_sync_api_request(rpcenv, api_method, formatter, parts, body, uri_param); }