From 8869c862d60d7614f15007824bf1c552d9f4b322 Mon Sep 17 00:00:00 2001 From: Kaige Fu Date: Wed, 4 Jul 2018 10:47:30 +0800 Subject: [PATCH] tools: acrnlog: Deprecate binary "ls" with GPLv3+ The tools currently rely on the availability of binary "ls" to be available in the $PATH on the Service OS. This create 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/acrnlog/acrnlog.c | 54 +++++++++++++---------------------------- 1 file changed, 17 insertions(+), 37 deletions(-) diff --git a/tools/acrnlog/acrnlog.c b/tools/acrnlog/acrnlog.c index c86ed0ff2..125cce8aa 100644 --- a/tools/acrnlog/acrnlog.c +++ b/tools/acrnlog/acrnlog.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include @@ -49,53 +50,32 @@ struct hvlog_dev { struct hvlog_msg latched_msg; /* latch for parsed msg */ }; -static int shell_cmd(const char *cmd, char *outbuf, int len) -{ - FILE *ptr; - char cmd_buf[256]; - int ret; - - if (!outbuf) - return system(cmd); - - memset(cmd_buf, 0, sizeof(cmd_buf)); - memset(outbuf, 0, len); - snprintf(cmd_buf, sizeof(cmd_buf), "%s 2>&1", cmd); - ptr = popen(cmd_buf, "re"); - if (!ptr) - return -1; - - ret = fread(outbuf, 1, len, ptr); - pclose(ptr); - - return ret; -} - /* * get pcpu_num, which is equal to num of acrnlog dev */ static int get_cpu_num(void) { + char prefix[32] = "acrn_hvlog_cur_"; /* acrnlog dev prefix */ + struct dirent *pdir; + int cpu_num = 0; + char *ret; + DIR *dir; - char cmd[128]; - char buf[16]; - int ret; - - snprintf(cmd, sizeof(cmd), "ls /dev/acrn_hvlog_cur_* | wc -l"); - - ret = shell_cmd(cmd, buf, sizeof(buf)); - if (ret <= 0) { - printf("Faile to get cpu number, use default 4\n"); - return PCPU_NUM; + dir = opendir("/dev"); + if (!dir) { + printf("Error opening /dev: %s\n", strerror(errno)); + return -1; } - ret = atoi(buf); - if (ret <= 0) { - printf("Wrong cpu number, use default 4\n"); - return PCPU_NUM; + while (pdir = readdir(dir)) { + ret = strstr(pdir->d_name, prefix); + if (ret) + cpu_num++; } - return ret; + closedir(dir); + + return cpu_num; } /*