From 9afb6540dca26cd4c734cff91e72c59a7721cf2a Mon Sep 17 00:00:00 2001 From: Xiang Xiao Date: Fri, 16 Sep 2022 14:53:46 +0800 Subject: [PATCH] libc/ftok: Map token to the root pseduo file system directory to ensure the generated key is unique from each other since only root pseduo file system really support st_ino field after: commit d35fbf534d51f7ef72382d9666a1f19e07c6f00f Author: Xiang Xiao Date: Fri Sep 16 14:24:55 2022 +0800 fs: Allocate unique serial number for the root pseduo file system node Signed-off-by: Xiang Xiao --- libs/libc/misc/Kconfig | 6 ++++++ libs/libc/misc/lib_ftok.c | 14 ++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/libs/libc/misc/Kconfig b/libs/libc/misc/Kconfig index 4a7635e533..fc7de51b7d 100644 --- a/libs/libc/misc/Kconfig +++ b/libs/libc/misc/Kconfig @@ -62,6 +62,12 @@ config LIBC_ENVPATH Use the contents of the common environment variable to locate executable or library files. Default: n +config LIBC_FTOK_VFS_PATH + string "Relative path to ftok storage" + default "/var/ftok" + ---help--- + The relative path to where ftok will exist in the root namespace. + config LIBC_MEM_FD_VFS_PATH string "Relative path to memfd storage" default "memfd" diff --git a/libs/libc/misc/lib_ftok.c b/libs/libc/misc/lib_ftok.c index 6d38ed62b1..faaddc2c34 100644 --- a/libs/libc/misc/lib_ftok.c +++ b/libs/libc/misc/lib_ftok.c @@ -27,6 +27,8 @@ #include #include #include +#include +#include /**************************************************************************** * Public Functions @@ -56,11 +58,19 @@ key_t ftok(FAR const char *pathname, int proj_id) { + char fullpath[PATH_MAX] = CONFIG_LIBC_FTOK_VFS_PATH "/"; struct stat st; - if (stat(pathname, &st) < 0) + strlcat(fullpath, pathname, sizeof(fullpath)); + if (stat(fullpath, &st) < 0 && get_errno() == ENOENT) { - return (key_t)-1; + /* Directory not exist, let's create one for caller */ + + mkdir(fullpath, S_IRWXU); + if (stat(fullpath, &st) < 0) + { + return (key_t)-1; + } } return ((key_t)proj_id << 24 |