From 9e416283554e333c68a8d6a1cc2da8313a6ebe7b Mon Sep 17 00:00:00 2001 From: zyxeeker Date: Sat, 2 Nov 2024 10:53:37 +0800 Subject: [PATCH] record: Fix crash when the root is not existed This patch to fix crash when the root is not existed, due to some embed devices maybe not have it Signed-off-by: zyxeeker --- cmds/record.c | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/cmds/record.c b/cmds/record.c index 54b17c786..0e7e31e54 100644 --- a/cmds/record.c +++ b/cmds/record.c @@ -930,6 +930,13 @@ static int filter_shmem(const struct dirent *de) static void unlink_shmem_list(void) { struct shmem_list *sl, *tmp; + char *shmem_root = uftrace_shmem_root(); + + /* check the root is existed (due to some embed devices maybe not have it) */ + if (access(shmem_root, F_OK) != 0) { + shmem_root = NULL; + pr_warn("access shmem root failed and will ignore it, err: %s\n", strerror(errno)); + } /* unlink shmem list (not used anymore) */ list_for_each_entry_safe(sl, tmp, &shmem_need_unlink, list) { @@ -942,16 +949,17 @@ static void unlink_shmem_list(void) sscanf(sl->id, "/uftrace-%[^-]-%*d-%*d", shmem_session); pr_dbg2("unlink for session: %s\n", shmem_session); - num = scandir(uftrace_shmem_root(), &shmem_bufs, filter_shmem, alphasort); - for (i = 0; i < num; i++) { - sid[0] = '/'; - memcpy(&sid[1], shmem_bufs[i]->d_name, MSG_ID_SIZE); - pr_dbg3("unlink %s\n", sid); - uftrace_shmem_unlink(sid); - free(shmem_bufs[i]); + if (shmem_root) { + num = scandir(shmem_root, &shmem_bufs, filter_shmem, alphasort); + for (i = 0; i < num; i++) { + sid[0] = '/'; + memcpy(&sid[1], shmem_bufs[i]->d_name, MSG_ID_SIZE); + pr_dbg3("unlink %s\n", sid); + uftrace_shmem_unlink(sid); + free(shmem_bufs[i]); + } + free(shmem_bufs); } - - free(shmem_bufs); free(sl); } }