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 <xiaoxiang@xiaomi.com>
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 <xiaoxiang@xiaomi.com>
This commit is contained in:
Xiang Xiao 2022-09-16 14:53:46 +08:00 committed by archer
parent db518bf0df
commit 9afb6540dc
2 changed files with 18 additions and 2 deletions

View File

@ -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"

View File

@ -27,6 +27,8 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <errno.h>
#include <string.h>
/****************************************************************************
* 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 |