You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by ac...@apache.org on 2023/10/07 00:02:37 UTC

[nuttx] branch master updated: lib/memfd: shm_unlink or unlink anonymous file

This is an automated email from the ASF dual-hosted git repository.

acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new aa0b3fcdf9 lib/memfd: shm_unlink or unlink anonymous file
aa0b3fcdf9 is described below

commit aa0b3fcdf91fc82f8ca98f79a7cb3754f09c8b75
Author: Xiang Xiao <xi...@xiaomi.com>
AuthorDate: Thu Oct 5 19:57:30 2023 +0800

    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 <xi...@xiaomi.com>
---
 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
 }