From 7ac917aa1aee1100099c54bcee52ea5fe3585feb Mon Sep 17 00:00:00 2001 From: "Hui, Chunyang" Date: Fri, 24 Jul 2020 12:03:56 +0000 Subject: [PATCH] Fix PAL library command arguments not follow convention --- demos/embedded_mode/bench_driver/main.c | 1 + src/exec/src/bin/occlum_exec_client.rs | 9 ++++++--- src/libos/src/entry.rs | 10 ---------- src/pal/include/occlum_pal_api.h | 3 ++- src/run/main.c | 17 +++++++++++++---- 5 files changed, 22 insertions(+), 18 deletions(-) diff --git a/demos/embedded_mode/bench_driver/main.c b/demos/embedded_mode/bench_driver/main.c index c241b528..102b3c3d 100644 --- a/demos/embedded_mode/bench_driver/main.c +++ b/demos/embedded_mode/bench_driver/main.c @@ -58,6 +58,7 @@ int main(int argc, char *argv[]) { snprintf(buf_ptr_str, sizeof buf_ptr_str, "%lu", (unsigned long) shared_buf); snprintf(buf_size_str, sizeof buf_size_str, "%lu", sizeof shared_buf); const char *cmd_args[] = { + cmd_path, buf_ptr_str, // buf_ptr buf_size_str, // buf_size total_bytes_str, // total_bytes diff --git a/src/exec/src/bin/occlum_exec_client.rs b/src/exec/src/bin/occlum_exec_client.rs index f402e832..64bf4a1d 100644 --- a/src/exec/src/bin/occlum_exec_client.rs +++ b/src/exec/src/bin/occlum_exec_client.rs @@ -27,6 +27,7 @@ use signal_hook::iterator::Signals; use signal_hook::{SIGINT, SIGKILL, SIGQUIT, SIGTERM, SIGUSR1}; use std::cmp; use std::env; +use std::path::Path; use std::os::unix::net::UnixListener; use std::process; use std::process::{Command, Stdio}; @@ -297,7 +298,7 @@ fn main() -> Result<(), i32> { stop_server(&client, stop_time); println!("server stopped."); } else if let Some(ref matches) = matches.subcommand_matches("exec") { - let cmd_args: Vec<&str> = match matches + let mut cmd_args: Vec<&str> = match matches .values_of("args") .map(|vals| vals.collect::>()) { @@ -306,7 +307,9 @@ fn main() -> Result<(), i32> { _ => panic!(), }; - let (cmd, args) = cmd_args.split_first().unwrap(); + let cmd = cmd_args[0]; + // Change cmd_args[0] from path name to program name + cmd_args[0] = Path::new(cmd_args[0]).file_name().unwrap().to_str().unwrap(); let env: Vec<&str> = env.iter().map(|string| string.as_str()).collect(); // Create the signal handler @@ -330,7 +333,7 @@ fn main() -> Result<(), i32> { } }); - match exec_command(&client, cmd, args, &env) { + match exec_command(&client, cmd, &cmd_args, &env) { Ok(process_id) => { // the signal thread exit if server finished execution or user kill the client signal_thread.join().unwrap(); diff --git a/src/libos/src/entry.rs b/src/libos/src/entry.rs index 120dd92a..a169824d 100644 --- a/src/libos/src/entry.rs +++ b/src/libos/src/entry.rs @@ -225,18 +225,8 @@ fn parse_arguments( .map_err(|e| errno!(EINVAL, "path contains valid utf-8 data"))?; Path::new(&path_string).to_path_buf() }; - let program_cstring = { - let program_osstr = path_buf - .file_name() - .ok_or_else(|| errno!(EINVAL, "invalid path"))?; - let program_str = program_osstr - .to_str() - .ok_or_else(|| errno!(EINVAL, "invalid path"))?; - CString::new(program_str).map_err(|e| errno!(e))? - }; let mut args = clone_cstrings_safely(argv)?; - args.insert(0, program_cstring); let env_merged = merge_env(env)?; trace!( diff --git a/src/pal/include/occlum_pal_api.h b/src/pal/include/occlum_pal_api.h index 2071eb9c..22d80f8e 100644 --- a/src/pal/include/occlum_pal_api.h +++ b/src/pal/include/occlum_pal_api.h @@ -64,7 +64,8 @@ struct occlum_pal_create_process_args { // Argments array pass to new process. // - // The arguments to the command. The array must be NULL terminated. + // The arguments to the command. By convention, the argv[0] should be the program name. + // And the array must be NULL terminated. // // Mandatory field. Must not be NULL. const char **argv; diff --git a/src/run/main.c b/src/run/main.c index 41cc5502..78d62702 100644 --- a/src/run/main.c +++ b/src/run/main.c @@ -2,6 +2,8 @@ #include #include #include +#include +#include #include #include #include @@ -13,10 +15,17 @@ int main(int argc, char *argv[]) { fprintf(stderr, "Usage: occlum-run []\n"); return EXIT_FAILURE; } - const char *cmd_path = (const char *) argv[1]; - const char **cmd_args = (const char **) &argv[2]; + + char **cmd_args = &argv[1]; + char *cmd_path = strdup(argv[1]); extern const char **environ; + // Change cmd_args[0] from program path to program name in place (e.g., "/bin/abc" to "abc") + char *cmd_path_tmp = strdup(cmd_path); + const char *program_name = (const char *) basename(cmd_path_tmp); + memset(cmd_args[0], 0, strlen(cmd_args[0])); + memcpy(cmd_args[0], program_name, strlen(program_name)); + // Check Occlum PAL version int pal_version = occlum_pal_get_version(); if (pal_version <= 0) { @@ -39,8 +48,8 @@ int main(int argc, char *argv[]) { int exit_status = 0; int libos_tid = 0; struct occlum_pal_create_process_args create_process_args = { - .path = cmd_path, - .argv = cmd_args, + .path = (const char *) cmd_path, + .argv = (const char **) cmd_args, .env = environ, .stdio = (const struct occlum_stdio_fds *) &io_fds, .pid = &libos_tid,