Skip to content

Commit 71f75eb

Browse files
JiangMaoxacrnsi
authored andcommitted
Tools: acrnlog: fix confusing message "bad file descriptor" when start acrnlog
ACRNLog tries to open acrn_hvlog_last_* without check the presence of the device nodes at startup, which results a confusing message reporting "bad file descriptor" when there is no last device nodes exist. This commit fix the issue by checking the number of last devices before trying to open them. Tracked-On: #2787 Signed-off-by: Jiang Mao <maox.jiang@intel.com> Acked-by: Yan, Like <like.yan@intel.com>
1 parent 9f23422 commit 71f75eb

File tree

1 file changed

+39
-29
lines changed

1 file changed

+39
-29
lines changed

tools/acrnlog/acrnlog.c

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"Try reducing polling interval"
2525

2626
/* Count of /dev/acrn_hvlog_cur_xxx */
27-
static unsigned int dev_cnt;
27+
static int cur_cnt,last_cnt;
2828
static unsigned long interval = DEFAULT_POLL_INTERVAL;
2929

3030
/* this is for log file */
@@ -83,9 +83,8 @@ struct hvlog_dev {
8383

8484
size_t write_log_file(struct hvlog_file * log, const char *buf, size_t len);
8585

86-
static int get_dev_cnt(void)
86+
static int get_dev_cnt(char *prefix)
8787
{
88-
char prefix[32] = "acrn_hvlog_cur_"; /* acrnlog dev prefix */
8988
struct dirent *pdir;
9089
int cnt = 0;
9190
char *ret;
@@ -354,8 +353,8 @@ static void *cur_read_func(void *arg)
354353
char warn_msg[LOG_MSG_SIZE] = {0};
355354

356355
while (1) {
357-
hvlog_dev_read_msg(cur, dev_cnt);
358-
msg = get_min_seq_msg(cur, dev_cnt);
356+
hvlog_dev_read_msg(cur, cur_cnt);
357+
msg = get_min_seq_msg(cur, cur_cnt);
359358
if (!msg) {
360359
usleep(interval);
361360
continue;
@@ -502,27 +501,32 @@ int main(int argc, char *argv[])
502501
return ret;
503502
}
504503

505-
dev_cnt = get_dev_cnt();
506-
if (dev_cnt == 0) {
504+
cur_cnt = get_dev_cnt("acrn_hvlog_cur_");
505+
last_cnt = get_dev_cnt("acrn_hvlog_last_");
506+
507+
if (cur_cnt == 0) {
507508
printf("Failed to find acrn hvlog devices, please check whether module acrn_hvlog is inserted\n");
508509
return -1;
509-
}
510+
} else if (cur_cnt == -1)
511+
return -1;
510512

511-
cur = calloc(dev_cnt, sizeof(struct hvlog_data));
513+
cur = calloc(cur_cnt, sizeof(struct hvlog_data));
512514
if (!cur) {
513515
printf("Failed to allocate buf for cur log buf\n");
514516
return -1;
515517
}
516518

517-
last = calloc(dev_cnt, sizeof(struct hvlog_data));
518-
if (!last) {
519-
printf("Failed to allocate buf for last log buf\n");
520-
free(cur);
521-
return -1;
519+
if (last_cnt) {
520+
last = calloc(cur_cnt, sizeof(struct hvlog_data));
521+
if (!last) {
522+
printf("Failed to allocate buf for last log buf\n");
523+
free(cur);
524+
return -1;
525+
}
522526
}
523527

524528
num_cur = 0;
525-
for (i = 0; i < dev_cnt; i++) {
529+
for (i = 0; i < cur_cnt; i++) {
526530
if (snprintf(name, sizeof(name), "/dev/acrn_hvlog_cur_%d", i) >= sizeof(name)) {
527531
printf("ERROR: cur hvlog path is truncated\n");
528532
return -1;
@@ -536,17 +540,19 @@ int main(int argc, char *argv[])
536540
}
537541

538542
num_last = 0;
539-
for (i = 0; i < dev_cnt; i++) {
540-
if (snprintf(name, sizeof(name), "/dev/acrn_hvlog_last_%d", i) >= sizeof(name)) {
541-
printf("ERROR: last hvlog path is truncated\n");
542-
return -1;
543+
if (last_cnt) {
544+
for (i = 0; i < cur_cnt; i++) {
545+
if (snprintf(name, sizeof(name), "/dev/acrn_hvlog_last_%d", i) >= sizeof(name)) {
546+
printf("ERROR: last hvlog path is truncated\n");
547+
return -1;
548+
}
549+
last[i].dev = hvlog_open_dev(name);
550+
if (!last[i].dev)
551+
perror(name);
552+
else
553+
num_last++;
554+
last[i].msg = NULL;
543555
}
544-
last[i].dev = hvlog_open_dev(name);
545-
if (!last[i].dev)
546-
perror(name);
547-
else
548-
num_last++;
549-
last[i].msg = NULL;
550556
}
551557

552558
printf("open cur:%d last:%d\n", num_cur, num_last);
@@ -562,8 +568,8 @@ int main(int argc, char *argv[])
562568

563569
if (num_last) {
564570
while (1) {
565-
hvlog_dev_read_msg(last, dev_cnt);
566-
msg = get_min_seq_msg(last, dev_cnt);
571+
hvlog_dev_read_msg(last, cur_cnt);
572+
msg = get_min_seq_msg(last, cur_cnt);
567573
if (!msg)
568574
break;
569575
write_log_file(&last_log, msg->raw, msg->len);
@@ -573,13 +579,17 @@ int main(int argc, char *argv[])
573579
if (cur_thread)
574580
pthread_join(cur_thread, NULL);
575581

576-
for (i = 0; i < dev_cnt; i++) {
582+
for (i = 0; i < cur_cnt; i++) {
577583
hvlog_close_dev(cur[i].dev);
584+
}
585+
586+
for (i = 0; i < last_cnt; i++) {
578587
hvlog_close_dev(last[i].dev);
579588
}
580589

581590
free(cur);
582-
free(last);
591+
if (last_cnt)
592+
free(last);
583593

584594
return 0;
585595
}

0 commit comments

Comments
 (0)