From 43f9abf84f7c2b19b74fd0a37b2b5855f1c31136 Mon Sep 17 00:00:00 2001 From: Xiang Xiao Date: Mon, 12 Jun 2023 00:03:55 +0800 Subject: [PATCH] libc: Prefer to implement memfd on top of shm since shm can work in protected and kernel mode too Signed-off-by: Xiang Xiao --- libs/libc/misc/Kconfig | 18 +++++++++++++++++- libs/libc/misc/lib_memfd.c | 25 +++++++++++++++++-------- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/libs/libc/misc/Kconfig b/libs/libc/misc/Kconfig index ba8d50a0ee..f94e97f776 100644 --- a/libs/libc/misc/Kconfig +++ b/libs/libc/misc/Kconfig @@ -81,9 +81,25 @@ config LIBC_FTOK_VFS_PATH ---help--- The relative path to where ftok will exist in the root namespace. +choice + prompt "Select memfd implementation" + +config LIBC_MEMFD_SHMFS + bool "memfd base on shmfs" + depends on FS_SHMFS + +config LIBC_MEMFD_TMPFS + bool "memfd base on tmpfs" + depends on FS_TMPFS + +config LIBC_MEMFD_ERROR + bool "memfd return error" + +endchoice + config LIBC_MEM_FD_VFS_PATH string "Relative path to memfd storage" default "memfd" - depends on FS_TMPFS + depends on !LIBC_MEMFD_ERROR ---help--- The relative path to where memfd will exist in the tmpfs namespace. diff --git a/libs/libc/misc/lib_memfd.c b/libs/libc/misc/lib_memfd.c index d3cc8c6b8f..828874d7fc 100644 --- a/libs/libc/misc/lib_memfd.c +++ b/libs/libc/misc/lib_memfd.c @@ -32,8 +32,13 @@ * Pre-processor Definitions ****************************************************************************/ -#define LIBC_MEM_FD_VFS_PATH \ - CONFIG_LIBC_TMPDIR "/" CONFIG_LIBC_MEM_FD_VFS_PATH "/%s" +#ifdef CONFIG_LIBC_MEMFD_TMPFS +# define LIBC_MEM_FD_VFS_PATH CONFIG_LIBC_TMPDIR "/" CONFIG_LIBC_MEM_FD_VFS_PATH +#else +# define LIBC_MEM_FD_VFS_PATH CONFIG_LIBC_MEM_FD_VFS_PATH +#endif + +#define LIBC_MEM_FD_VFS_PATH_FMT LIBC_MEM_FD_VFS_PATH "/%s" /**************************************************************************** * Public Functions @@ -41,13 +46,17 @@ int memfd_create(FAR const char *name, unsigned int flags) { -#ifdef CONFIG_FS_TMPFS - char path[PATH_MAX]; - - snprintf(path, sizeof(path), LIBC_MEM_FD_VFS_PATH, name); - return open(path, O_RDWR | flags); -#else +#ifdef CONFIG_LIBC_MEMFD_ERROR set_errno(ENOSYS); return -1; +#else + char path[PATH_MAX]; + + snprintf(path, sizeof(path), LIBC_MEM_FD_VFS_PATH_FMT, name); +# ifdef CONFIG_LIBC_MEMFD_SHMFS + return shm_open(path, O_RDWR | flags, 0660); +# else + return open(path, O_RDWR | flags, 0660); +# endif #endif }