|
12 | 12 | #include <fcntl.h>
|
13 | 13 | #include <unistd.h>
|
14 | 14 | #include <string.h>
|
| 15 | +#include <dirent.h> |
15 | 16 | #include <errno.h>
|
16 | 17 | #include <pthread.h>
|
17 | 18 |
|
@@ -49,53 +50,32 @@ struct hvlog_dev {
|
49 | 50 | struct hvlog_msg latched_msg; /* latch for parsed msg */
|
50 | 51 | };
|
51 | 52 |
|
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 |
| - |
74 | 53 | /*
|
75 | 54 | * get pcpu_num, which is equal to num of acrnlog dev
|
76 | 55 | */
|
77 | 56 | static int get_cpu_num(void)
|
78 | 57 | {
|
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; |
90 | 68 | }
|
91 | 69 |
|
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++; |
96 | 74 | }
|
97 | 75 |
|
98 |
| - return ret; |
| 76 | + closedir(dir); |
| 77 | + |
| 78 | + return cpu_num; |
99 | 79 | }
|
100 | 80 |
|
101 | 81 | /*
|
|
0 commit comments