diff --git a/libs/libc/misc/lib_memfd.c b/libs/libc/misc/lib_memfd.c index 98db24fa436..98046b753af 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 }