diff --git a/proto/interpreter.proto b/proto/interpreter.proto index 46b1871..dd49f7f 100644 --- a/proto/interpreter.proto +++ b/proto/interpreter.proto @@ -20,13 +20,7 @@ message AuthorizeRequest { uint32 pid = 4; } -enum AuthorizationStatus { - AUTHORIZED = 0; - PERMISSION_DENIED = 1; -} - message AuthorizeResponse { - AuthorizationStatus status = 1; string session_id = 2; } @@ -35,11 +29,4 @@ message TerminateRequest { string log_file = 2; } -enum TerminateStatus { - OK = 0; - FAILED = 1; -} - -message TerminateResponse { - TerminateStatus status = 1; -} \ No newline at end of file +message TerminateResponse {} \ No newline at end of file diff --git a/src/daemon/main.rs b/src/daemon/main.rs index 8b69158..25b8db0 100644 --- a/src/daemon/main.rs +++ b/src/daemon/main.rs @@ -1,14 +1,24 @@ #![cfg_attr(not(unix), allow(unused_imports))] +use std::sync::Mutex; mod server; +mod sessions; + +pub(self) static SESSIONS : Mutex> = Mutex::new(Vec::new()); + +pub use sessions::{ + get_sessions_lock, + remove_session_by_id +}; use std::path::Path; #[cfg(unix)] use tokio::net::UnixListener; + #[cfg(unix)] use tokio_stream::wrappers::UnixListenerStream; - use tonic::transport::Server; + use libcommand::interpreter::unix_server::UnixServer; #[cfg(unix)] diff --git a/src/daemon/server.rs b/src/daemon/server.rs index c18c6b7..14e9a97 100644 --- a/src/daemon/server.rs +++ b/src/daemon/server.rs @@ -1,11 +1,11 @@ #![cfg_attr(not(unix), allow(unused_imports))] -use tonic::{Request, Response, Status}; +use tonic::{Code, Request, Response, Status}; use libcommand::interpreter::{ unix_server::Unix, - AuthorizeRequest, AuthorizeResponse, AuthorizationStatus, - TerminateRequest, TerminateResponse, TerminateStatus + AuthorizeRequest, AuthorizeResponse, + TerminateRequest, TerminateResponse }; #[derive(Default)] @@ -15,22 +15,25 @@ pub struct DaemonServer; impl Unix for DaemonServer { async fn authorize( &self, - _request: Request, + request: Request, ) -> Result, Status> { - let reply = AuthorizeResponse { - status: AuthorizationStatus::Authorized.into(), - session_id: uuid::Uuid::new_v4().to_string() - }; - Ok(Response::new(reply)) + let session = libcommand::Session::from(request.get_ref().pid); + let session_id = session.id.clone(); + super::SESSIONS.lock().unwrap().push(session); + + Ok(Response::new(AuthorizeResponse { + session_id + })) } async fn terminate( &self, - _request: Request, + request: Request, ) -> Result, Status> { - let reply = TerminateResponse { - status: TerminateStatus::Ok.into() - }; - Ok(Response::new(reply)) + let mut lock = super::get_sessions_lock() + .map_err(|e| Status::new(Code::Internal, e))?; + super::remove_session_by_id(&mut lock, &request.get_ref().session_id) + .map_err(|e| Status::new(Code::NotFound, e))?; + Ok(Response::new(TerminateResponse {})) } } \ No newline at end of file diff --git a/src/daemon/sessions.rs b/src/daemon/sessions.rs new file mode 100644 index 0000000..725cabd --- /dev/null +++ b/src/daemon/sessions.rs @@ -0,0 +1,15 @@ +use std::sync::MutexGuard; + +pub type LockedSessions<'a> = MutexGuard<'a, Vec>; + +pub fn get_sessions_lock() -> Result, String> { + super::SESSIONS.lock() + .map_err(|_| format!("Failed to get Mutex lock of sessions manager")) +} + +pub fn remove_session_by_id(lock: &mut LockedSessions<'static>, id: &str) -> Result<(), String> { + let position = lock.iter().position(|s| s.id == id) + .ok_or(format!("Session id not found"))?; + lock.remove(position); + Ok(()) +} \ No newline at end of file diff --git a/src/interpreter/main.rs b/src/interpreter/main.rs index 3529a04..799a111 100644 --- a/src/interpreter/main.rs +++ b/src/interpreter/main.rs @@ -2,7 +2,7 @@ pub mod client; -use libcommand::interpreter::{AuthorizationStatus, AuthorizeRequest, AuthorizeResponse}; +use libcommand::interpreter::{AuthorizeRequest, AuthorizeResponse, TerminateRequest}; use tonic::Response; #[cfg(unix)] @@ -25,13 +25,14 @@ async fn main() -> Result<(), Box> { let response : Response = client.authorize(request).await?; - if AuthorizationStatus::from_i32(response.get_ref().status) == Some(AuthorizationStatus::Authorized) { - let mut command : std::process::Command = command_arg.into(); - let mut child = command.spawn().unwrap(); - child.wait().unwrap(); - } else { - eprintln!("Permission denied"); - } + let mut command : std::process::Command = command_arg.into(); + let mut child = command.spawn().unwrap(); + child.wait().unwrap(); + + client.terminate(tonic::Request::new(TerminateRequest { + session_id: response.get_ref().session_id.clone(), + log_file: "".to_string() + })).await?; Ok(()) } diff --git a/src/lib.rs b/src/lib.rs index 3ea43e1..d9eab6f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,8 +2,9 @@ mod command; mod session; pub use command::Command; +pub use session::Session; -pub const SOCK_FILE : &'static str = "/var/run/command_gateway.sock"; +pub const SOCK_FILE : &'static str = "command_gateway.sock"; pub mod interpreter { tonic::include_proto!("interpreter"); diff --git a/src/session.rs b/src/session.rs index 6e84174..e07192d 100644 --- a/src/session.rs +++ b/src/session.rs @@ -1,10 +1,10 @@ pub struct Session { pub id: String, - pub pid: usize, + pub pid: u32, } -impl From for Session { - fn from(pid: usize) -> Self { +impl From for Session { + fn from(pid: u32) -> Self { Self { id: format!("ID{pid}"), pid