mirror of
https://github.com/apache/nuttx.git
synced 2026-06-02 09:38:37 +08:00
libc/shmfs: Ensure uniqueness of memfd.
Use mktemp to create unique path for memfd, so other thread can't find file by path. If don't do this, error will ocurr in this case: thread 1: thread2: open() -- refs = 1 open() -- refs = 2 unlink() unlink() thread1 and thread2 will map one buffer by using file path but not fd or address of buffer. Signed-off-by: wangzhi16 <wangzhi16@xiaomi.com>
This commit is contained in:
@@ -43,7 +43,7 @@
|
|||||||
# define LIBC_MEM_FD_VFS_PATH CONFIG_LIBC_MEM_FD_VFS_PATH
|
# define LIBC_MEM_FD_VFS_PATH CONFIG_LIBC_MEM_FD_VFS_PATH
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define LIBC_MEM_FD_VFS_PATH_FMT LIBC_MEM_FD_VFS_PATH "/%s"
|
#define LIBC_MEM_FD_VFS_PATH_FMT LIBC_MEM_FD_VFS_PATH "/%sXXXXXX"
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Public Functions
|
* Public Functions
|
||||||
@@ -65,22 +65,30 @@ int memfd_create(FAR const char *name, unsigned int flags)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
retry:
|
||||||
snprintf(path, PATH_MAX, LIBC_MEM_FD_VFS_PATH_FMT, name);
|
snprintf(path, PATH_MAX, LIBC_MEM_FD_VFS_PATH_FMT, name);
|
||||||
|
mktemp(path);
|
||||||
|
|
||||||
# ifdef CONFIG_LIBC_MEMFD_SHMFS
|
# ifdef CONFIG_LIBC_MEMFD_SHMFS
|
||||||
ret = shm_open(path, O_RDWR | flags, 0660);
|
ret = shm_open(path, O_RDWR | O_EXCL | flags, 0660);
|
||||||
if (ret >= 0)
|
if (ret >= 0)
|
||||||
{
|
{
|
||||||
shm_unlink(path);
|
shm_unlink(path);
|
||||||
}
|
}
|
||||||
# else
|
# else
|
||||||
mkdir(LIBC_MEM_FD_VFS_PATH, 0666);
|
mkdir(LIBC_MEM_FD_VFS_PATH, 0666);
|
||||||
ret = open(path, O_RDWR | flags, 0660);
|
ret = open(path, O_RDWR | O_EXCL | flags, 0660);
|
||||||
if (ret >= 0)
|
if (ret >= 0)
|
||||||
{
|
{
|
||||||
unlink(path);
|
unlink(path);
|
||||||
}
|
}
|
||||||
# endif
|
# endif
|
||||||
|
|
||||||
|
if (ret < 0 && get_errno() == EEXIST)
|
||||||
|
{
|
||||||
|
goto retry;
|
||||||
|
}
|
||||||
|
|
||||||
lib_put_pathbuffer(path);
|
lib_put_pathbuffer(path);
|
||||||
return ret;
|
return ret;
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user