From ce174e149629bb8536c777c88a9537a93034d97d Mon Sep 17 00:00:00 2001 From: "zongmin.gu" Date: Wed, 16 Dec 2020 18:24:01 +0800 Subject: [PATCH] Fix the UDS path too long issue --- src/exec/src/bin/occlum_exec_client.rs | 41 +++++++++++++------------- src/exec/src/bin/occlum_exec_server.rs | 31 ++++++++++++------- src/exec/src/lib.rs | 5 ++-- 3 files changed, 42 insertions(+), 35 deletions(-) diff --git a/src/exec/src/bin/occlum_exec_client.rs b/src/exec/src/bin/occlum_exec_client.rs index a30e52f4..9b75baad 100644 --- a/src/exec/src/bin/occlum_exec_client.rs +++ b/src/exec/src/bin/occlum_exec_client.rs @@ -18,9 +18,7 @@ use occlum_exec::occlum_exec::{ KillProcessRequest, StopRequest, }; use occlum_exec::occlum_exec_grpc::OcclumExecClient; -use occlum_exec::{ - DEFAULT_CLIENT_FILE, DEFAULT_SERVER_FILE, DEFAULT_SERVER_TIMER, DEFAULT_SOCK_FILE, -}; +use occlum_exec::{DEFAULT_SERVER_FILE, DEFAULT_SERVER_TIMER, DEFAULT_SOCK_FILE}; use protobuf::RepeatedField; use sendfd::SendWithFd; use signal_hook::iterator::Signals; @@ -142,7 +140,12 @@ fn start_server(client: &OcclumExecClient, server_name: &str) -> Result { if !server_launched { debug!("server is not running, try to launch the server."); - match Command::new(server_name).stdout(Stdio::null()).spawn() { + match Command::new(server_name) + .arg("-d") + .arg(env::current_dir().unwrap()) + .stdout(Stdio::null()) + .spawn() + { Err(_r) => { return Err("Failed to launch server".to_string()); } @@ -233,6 +236,14 @@ fn main() -> Result<(), i32> { let matches = App::new("Occlum") .version("0.1.0") + .arg( + Arg::with_name("instance_dir") + .short("d") + .long("instance_dir") + .takes_value(true) + .default_value("./") + .help("The Occlum instance dir."), + ) .subcommand( App::new("start").about( "Start the Occlum server. If the server already running, immediately return.", @@ -263,32 +274,20 @@ fn main() -> Result<(), i32> { ) .get_matches(); - let args: Vec = env::args().collect(); let env: Vec = env::vars() .into_iter() .map(|(key, val)| format!("{}={}", key, val)) .collect(); - let mut sock_file = String::from(args[0].as_str()); - let sock_file = str::replace( - sock_file.as_mut_str(), - DEFAULT_CLIENT_FILE, - DEFAULT_SOCK_FILE, - ); + // Set the instance_dir as the current dir + let instance_dir = Path::new(matches.value_of("instance_dir").unwrap()); + assert!(env::set_current_dir(&instance_dir).is_ok()); - let client = OcclumExecClient::new_plain_unix(&sock_file, ClientConf::new()) + let client = OcclumExecClient::new_plain_unix(DEFAULT_SOCK_FILE, ClientConf::new()) .expect("failed to create UDS client"); if let Some(ref _matches) = matches.subcommand_matches("start") { - //get the server name with the first args - let mut server_name = String::from(args[0].as_str()); - let server_name = str::replace( - server_name.as_mut_str(), - DEFAULT_CLIENT_FILE, - DEFAULT_SERVER_FILE, - ); - - if let Err(s) = start_server(&client, &server_name) { + if let Err(s) = start_server(&client, DEFAULT_SERVER_FILE) { println!("start_server failed {}", s); return Err(-1); } diff --git a/src/exec/src/bin/occlum_exec_server.rs b/src/exec/src/bin/occlum_exec_server.rs index abe2453d..3edcd70a 100644 --- a/src/exec/src/bin/occlum_exec_server.rs +++ b/src/exec/src/bin/occlum_exec_server.rs @@ -5,16 +5,18 @@ extern crate occlum_exec; extern crate protobuf; #[macro_use] extern crate log; +use clap::{App, Arg}; use futures::executor; use grpc::prelude::*; use grpc::ClientConf; use occlum_exec::occlum_exec::HealthCheckRequest; use occlum_exec::occlum_exec_grpc::{OcclumExecClient, OcclumExecServer}; use occlum_exec::server::OcclumExecImpl; -use occlum_exec::{DEFAULT_SERVER_FILE, DEFAULT_SOCK_FILE}; +use occlum_exec::DEFAULT_SOCK_FILE; use std::env; use std::ffi::{CStr, OsString}; use std::os::unix::ffi::OsStrExt; +use std::path::Path; use std::sync::{Arc, Condvar, Mutex}; //Checks the server status, if the server is running return true, else recover the socket file and return false. @@ -52,17 +54,24 @@ fn check_server_status(sock_file: &str) -> bool { } fn main() { - //get the UDS file name - let args: Vec = env::args().collect(); - let mut sockfile = String::from(args[0].as_str()); - let sockfile = str::replace( - sockfile.as_mut_str(), - DEFAULT_SERVER_FILE, - DEFAULT_SOCK_FILE, - ); + let matches = App::new("Occlum_server") + .version("0.1.0") + .arg( + Arg::with_name("instance_dir") + .short("d") + .long("instance_dir") + .takes_value(true) + .default_value("./") + .help("The Occlum instance dir."), + ) + .get_matches(); + + // Set the instance_dir as the current dir + let instance_dir = Path::new(matches.value_of("instance_dir").unwrap()); + assert!(env::set_current_dir(&instance_dir).is_ok()); //If the server already startted, then return - if check_server_status(sockfile.as_str()) { + if check_server_status(DEFAULT_SOCK_FILE) { println!("server stared"); return; } @@ -74,7 +83,7 @@ fn main() { ); let mut server_builder = grpc::ServerBuilder::new_plain(); server_builder.add_service(service_def); - match server_builder.http.set_unix_addr(sockfile) { + match server_builder.http.set_unix_addr(DEFAULT_SOCK_FILE) { Ok(_) => {} Err(e) => { debug!("{:?}", e); diff --git a/src/exec/src/lib.rs b/src/exec/src/lib.rs index ecc57b3e..b7f93d8a 100644 --- a/src/exec/src/lib.rs +++ b/src/exec/src/lib.rs @@ -13,7 +13,6 @@ pub mod occlum_exec_grpc; pub mod server; -pub const DEFAULT_SERVER_FILE: &'static str = "occlum_exec_server"; -pub const DEFAULT_CLIENT_FILE: &'static str = "occlum_exec_client"; -pub const DEFAULT_SOCK_FILE: &'static str = "occlum_exec.sock"; +pub const DEFAULT_SERVER_FILE: &'static str = "build/bin/occlum_exec_server"; +pub const DEFAULT_SOCK_FILE: &'static str = "run/occlum_exec.sock"; pub const DEFAULT_SERVER_TIMER: u32 = 3;