Skip to content

Commit 3195bc4

Browse files
KaigeFulijinxia
authored andcommitted
tools: acrnlog: Deprecate binary "rm" and "mkdir" with GPLv3+
The tools currently rely on the availability of binary "rm" and "mkdir" 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 8869c86 commit 3195bc4

File tree

1 file changed

+45
-9
lines changed

1 file changed

+45
-9
lines changed

tools/acrnlog/acrnlog.c

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,6 @@ static struct hvlog_file last_log = {
293293
static int new_log_file(struct hvlog_file *log)
294294
{
295295
char file_name[32] = { };
296-
char cmd[64] = { };
297296

298297
if (log->fd >= 0) {
299298
if (!hvlog_log_size)
@@ -304,8 +303,7 @@ static int new_log_file(struct hvlog_file *log)
304303

305304
snprintf(file_name, sizeof(file_name), "%s.%hu", log->path,
306305
log->index + 1);
307-
snprintf(cmd, sizeof(cmd), "rm -f %s", file_name);
308-
system(cmd);
306+
remove(file_name);
309307

310308
log->fd = open(file_name, O_RDWR | O_CREAT | O_TRUNC, 0666);
311309
if (log->fd < 0) {
@@ -315,9 +313,9 @@ static int new_log_file(struct hvlog_file *log)
315313

316314
log->left_space = hvlog_log_size;
317315
log->index++;
318-
snprintf(cmd, sizeof(cmd), "rm -f %s.%hu", log->path,
319-
log->index - hvlog_log_num);
320-
system(cmd);
316+
snprintf(file_name, sizeof(file_name), "%s.%hu", log->path,
317+
log->index - hvlog_log_num);
318+
remove(file_name);
321319

322320
return 0;
323321
}
@@ -367,6 +365,44 @@ static void *cur_read_func(void *arg)
367365
return NULL;
368366
}
369367

368+
/* If dir *path does't exist, create a new one.
369+
* Otherwise, remove all the old acrnlog files in the dir.
370+
*/
371+
static int mk_dir(const char *path)
372+
{
373+
char prefix[32] = "acrnlog_cur."; /* acrnlog file prefix */
374+
char acrnlog_file[64] = { };
375+
struct dirent *pdir;
376+
struct stat st;
377+
int index = 0;
378+
char *find;
379+
DIR *dir;
380+
381+
if (stat(path, &st)) {
382+
if (mkdir(path, 0644))
383+
return -1;
384+
} else {
385+
/* Remove all the old acrnlogs */
386+
dir = opendir(path);
387+
if (!dir) {
388+
printf("Error opening directory %s. Error: %s\n",
389+
path, strerror(errno));
390+
return -1;
391+
}
392+
while (pdir = readdir(dir)) {
393+
find = strstr(pdir->d_name, prefix);
394+
if (!find)
395+
continue;
396+
397+
snprintf(acrnlog_file, sizeof(acrnlog_file), "%s/%s%d",
398+
path, prefix, index++);
399+
remove(acrnlog_file);
400+
}
401+
}
402+
403+
return 0;
404+
}
405+
370406
/* for user optinal args */
371407
static const char optString[] = "s:n:t:h";
372408

@@ -430,10 +466,10 @@ int main(int argc, char *argv[])
430466
if (parse_opt(argc, argv))
431467
return -1;
432468

433-
system("rm -rf /tmp/acrnlog");
434-
ret = system("mkdir -p /tmp/acrnlog");
469+
ret = mk_dir("/tmp/acrnlog");
435470
if (ret) {
436-
printf("can't create /tmp/acrnlog\n");
471+
printf("Cannot create /tmp/acrnlog. Error: %s\n",
472+
strerror(errno));
437473
return ret;
438474
}
439475

0 commit comments

Comments
 (0)