Fix PAL library command arguments not follow convention

This commit is contained in:
Hui, Chunyang 2020-07-24 12:03:56 +00:00 committed by Tate, Hongliang Tian
parent 153a6fcd61
commit 7ac917aa1a
5 changed files with 22 additions and 18 deletions

@ -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

@ -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::<Vec<_>>())
{
@ -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();

@ -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!(

@ -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;

@ -2,6 +2,8 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <libgen.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <occlum_pal_api.h>
@ -13,10 +15,17 @@ int main(int argc, char *argv[]) {
fprintf(stderr, "Usage: occlum-run <executable> [<args>]\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,