From 636515aac2afe1b35402bd300e722c4d19dc563a Mon Sep 17 00:00:00 2001 From: Kaige Fu Date: Thu, 5 Jul 2018 10:53:02 +0800 Subject: [PATCH] tools: acrntrace: Remove use of binary "mkdir" The tools currently rely on the availability of binary "mkdir" to be available in the $PATH on the Service OS. This creates an obscure dependency. The patch changes the code to use standard C APIs to perform the same functionality which makes the code more self-contained. Signed-off-by: Kaige Fu Reviewed-by: Yan, Like --- tools/acrntrace/acrntrace.c | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/tools/acrntrace/acrntrace.c b/tools/acrntrace/acrntrace.c index 963e6e95b..007103029 100644 --- a/tools/acrntrace/acrntrace.c +++ b/tools/acrntrace/acrntrace.c @@ -10,6 +10,8 @@ #include #include #include +#include +#include #include #include #include @@ -166,11 +168,12 @@ static int get_cpu_num(void) static int create_trace_file_dir(char *dir) { - int status; + int err; char cmd[CMD_MAX_LEN]; char time_str[TIME_STR_LEN]; time_t timep; struct tm *p; + struct stat st; time(&timep); p = localtime(&timep); @@ -183,18 +186,29 @@ static int create_trace_file_dir(char *dir) pr_info("start tracing at %s\n", time_str); + /* Pre-condition: Make sure tmpfs is mounted on /tmp */ + if (stat(TRACE_FILE_ROOT, &st)) { + err = mkdir(TRACE_FILE_ROOT, 0644); + if (err) { + pr_err("Fail to create dir %s, Error: %s\n", + TRACE_FILE_ROOT, strerror(errno)); + return -1; + } + } + snprintf(dir, TRACE_FILE_DIR_LEN, "%s%s", TRACE_FILE_ROOT, time_str); - - memset(cmd, 0, CMD_MAX_LEN); - snprintf(cmd, CMD_MAX_LEN, "%s %s", "mkdir -p ", dir); - - status = system(cmd); - if (-1 == status) - return -1; /* failed to execute sh */ + if (stat(dir, &st)) { + err = mkdir(dir, 0644); + if (err) { + pr_err("Fail to create dir %s, Error: %s\n", + dir, strerror(errno)); + return -1; + } + } pr_dbg("dir %s creted\n", dir); - return WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE; + return WIFEXITED(err) ? WEXITSTATUS(err) : EXIT_FAILURE; } /* function executed in each consumer thread */