From 99c8119309be7c633cc64adac9fed23f9dd838f8 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: yuxuan zhou --- cmds/record.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/cmds/record.c b/cmds/record.c index 54b17c786..b718ccc90 100644 --- a/cmds/record.c +++ b/cmds/record.c @@ -942,16 +942,24 @@ 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]); + /* check the root is existed (due to some embed devices maybe not have it) */ + if (access(uftrace_shmem_root(), F_OK) == 0) { + 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]); + } + + free(shmem_bufs); + } + else { + pr_dbg2("access %s failed, err: %s\n", uftrace_shmem_root(), + strerror(errno)); } - free(shmem_bufs); free(sl); } }