Skip to content

Commit 8869c86

Browse files
KaigeFulijinxia
authored andcommitted
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 <kaige.fu@intel.com> Reviewed-by: Yan, Like <like.yan@intel.com>
1 parent 0ccd74b commit 8869c86

File tree

1 file changed

+18
-38
lines changed

1 file changed

+18
-38
lines changed

tools/acrnlog/acrnlog.c

Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <fcntl.h>
1313
#include <unistd.h>
1414
#include <string.h>
15+
#include <dirent.h>
1516
#include <errno.h>
1617
#include <pthread.h>
1718

@@ -49,53 +50,32 @@ struct hvlog_dev {
4950
struct hvlog_msg latched_msg; /* latch for parsed msg */
5051
};
5152

52-
static int shell_cmd(const char *cmd, char *outbuf, int len)
53-
{
54-
FILE *ptr;
55-
char cmd_buf[256];
56-
int ret;
57-
58-
if (!outbuf)
59-
return system(cmd);
60-
61-
memset(cmd_buf, 0, sizeof(cmd_buf));
62-
memset(outbuf, 0, len);
63-
snprintf(cmd_buf, sizeof(cmd_buf), "%s 2>&1", cmd);
64-
ptr = popen(cmd_buf, "re");
65-
if (!ptr)
66-
return -1;
67-
68-
ret = fread(outbuf, 1, len, ptr);
69-
pclose(ptr);
70-
71-
return ret;
72-
}
73-
7453
/*
7554
* get pcpu_num, which is equal to num of acrnlog dev
7655
*/
7756
static int get_cpu_num(void)
7857
{
79-
80-
char cmd[128];
81-
char buf[16];
82-
int ret;
83-
84-
snprintf(cmd, sizeof(cmd), "ls /dev/acrn_hvlog_cur_* | wc -l");
85-
86-
ret = shell_cmd(cmd, buf, sizeof(buf));
87-
if (ret <= 0) {
88-
printf("Faile to get cpu number, use default 4\n");
89-
return PCPU_NUM;
58+
char prefix[32] = "acrn_hvlog_cur_"; /* acrnlog dev prefix */
59+
struct dirent *pdir;
60+
int cpu_num = 0;
61+
char *ret;
62+
DIR *dir;
63+
64+
dir = opendir("/dev");
65+
if (!dir) {
66+
printf("Error opening /dev: %s\n", strerror(errno));
67+
return -1;
9068
}
9169

92-
ret = atoi(buf);
93-
if (ret <= 0) {
94-
printf("Wrong cpu number, use default 4\n");
95-
return PCPU_NUM;
70+
while (pdir = readdir(dir)) {
71+
ret = strstr(pdir->d_name, prefix);
72+
if (ret)
73+
cpu_num++;
9674
}
9775

98-
return ret;
76+
closedir(dir);
77+
78+
return cpu_num;
9979
}
10080

10181
/*

0 commit comments

Comments
 (0)