From aa0b3fcdf91fc82f8ca98f79a7cb3754f09c8b75 Mon Sep 17 00:00:00 2001 From: Xiang Xiao Date: Thu, 5 Oct 2023 19:57:30 +0800 Subject: [PATCH] lib/memfd: shm_unlink or unlink anonymous file https://man7.org/linux/man-pages/man2/memfd_create.2.html: The name supplied in name is used as a filename and will be displayed as the target of the corresponding symbolic link in the directory /proc/self/fd/. The displayed name is always prefixed with memfd: and serves only for debugging purposes. Names do not affect the behavior of the file descriptor, and as such multiple files can have the same name without any side effects. Signed-off-by: Xiang Xiao --- libs/libc/misc/lib_memfd.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/libs/libc/misc/lib_memfd.c b/libs/libc/misc/lib_memfd.c index 98db24fa43..98046b753a 100644 --- a/libs/libc/misc/lib_memfd.c +++ b/libs/libc/misc/lib_memfd.c @@ -52,13 +52,24 @@ int memfd_create(FAR const char *name, unsigned int flags) return -1; #else char path[PATH_MAX]; + int ret; snprintf(path, sizeof(path), LIBC_MEM_FD_VFS_PATH_FMT, name); # ifdef CONFIG_LIBC_MEMFD_SHMFS - return shm_open(path, O_RDWR | flags, 0660); + ret = shm_open(path, O_RDWR | flags, 0660); + if (ret >= 0) + { + shm_unlink(path); + } # else mkdir(LIBC_MEM_FD_VFS_PATH, 0666); - return open(path, O_RDWR | flags, 0660); + ret = open(path, O_RDWR | flags, 0660); + if (ret >= 0) + { + unlink(path); + } # endif + + return ret; #endif }