This is an automated email from the ASF dual-hosted git repository.
pkarashchenko pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git
The following commit(s) were added to refs/heads/master by this push:
new fa2e1897ea libc: Implement memfd on top of tmpfs
fa2e1897ea is described below
commit fa2e1897eab8bc6838f0e8bc791a0d7fbaa58972
Author: Xiang Xiao <[email protected]>
AuthorDate: Mon Aug 8 00:17:41 2022 +0800
libc: Implement memfd on top of tmpfs
https://man7.org/linux/man-pages/man2/memfd_create.2.html
Signed-off-by: Xiang Xiao <[email protected]>
---
include/fcntl.h | 10 +++++++
include/sys/memfd.h | 72 ++++++++++++++++++++++++++++++++++++++++++++++
libs/libc/misc/Kconfig | 7 +++++
libs/libc/misc/Make.defs | 2 +-
libs/libc/misc/lib_memfd.c | 51 ++++++++++++++++++++++++++++++++
5 files changed, 141 insertions(+), 1 deletion(-)
diff --git a/include/fcntl.h b/include/fcntl.h
index 25a0cce335..b6381d0cc9 100644
--- a/include/fcntl.h
+++ b/include/fcntl.h
@@ -97,6 +97,8 @@
#define F_SETOWN 13 /* Set pid that will receive SIGIO and SIGURG signals
for fd */
#define F_SETSIG 14 /* Set the signal to be sent */
#define F_GETPATH 15 /* Get the path of the file descriptor(BSD/macOS) */
+#define F_ADD_SEALS 16 /* Add the bit-mask argument arg to the set of seals of
the inode */
+#define F_GET_SEALS 17 /* Get (as the function result) the current set of
seals of the inode */
/* For posix fcntl() and lockf() */
@@ -117,6 +119,14 @@
#define DN_RENAME 4 /* A file was renamed */
#define DN_ATTRIB 5 /* Attributes of a file were changed */
+/* Types of seals */
+
+#define F_SEAL_SEAL 0x0001 /* Prevent further seals from being set */
+#define F_SEAL_SHRINK 0x0002 /* Prevent file from shrinking */
+#define F_SEAL_GROW 0x0004 /* Prevent file from growing */
+#define F_SEAL_WRITE 0x0008 /* Prevent writes */
+#define F_SEAL_FUTURE_WRITE 0x0010 /* Prevent future writes while mapped */
+
/* int creat(const char *path, mode_t mode);
*
* is equivalent to open with O_WRONLY|O_CREAT|O_TRUNC.
diff --git a/include/sys/memfd.h b/include/sys/memfd.h
new file mode 100644
index 0000000000..7bd244bf9e
--- /dev/null
+++ b/include/sys/memfd.h
@@ -0,0 +1,72 @@
+/****************************************************************************
+ * include/sys/memfd.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __INCLUDE_SYS_MEMFD_H
+#define __INCLUDE_SYS_MEMFD_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <fcntl.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define MFD_CLOEXEC O_CLOEXEC
+#define MFD_ALLOW_SEALING (1 << 16)
+#define MFD_HUGETLB (1 << 17)
+
+#define MFD_HUGE_SHIFT 26
+#define MFD_HUGE_MASK 0x3f
+#define MFD_HUGE_64KB (16 << MFD_HUGE_SHIFT)
+#define MFD_HUGE_512KB (19 << MFD_HUGE_SHIFT)
+#define MFD_HUGE_1MB (20 << MFD_HUGE_SHIFT)
+#define MFD_HUGE_2MB (21 << MFD_HUGE_SHIFT)
+#define MFD_HUGE_8MB (23 << MFD_HUGE_SHIFT)
+#define MFD_HUGE_16MB (24 << MFD_HUGE_SHIFT)
+#define MFD_HUGE_32MB (25 << MFD_HUGE_SHIFT)
+#define MFD_HUGE_256MB (28 << MFD_HUGE_SHIFT)
+#define MFD_HUGE_512MB (29 << MFD_HUGE_SHIFT)
+#define MFD_HUGE_1GB (30 << MFD_HUGE_SHIFT)
+#define MFD_HUGE_2GB (31 << MFD_HUGE_SHIFT)
+#define MFD_HUGE_16GB (34 << MFD_HUGE_SHIFT)
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+#ifdef __cplusplus
+#define EXTERN extern "C"
+extern "C"
+{
+#else
+#define EXTERN extern
+#endif
+
+int memfd_create(FAR const char *name, unsigned int flags);
+
+#undef EXTERN
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __INCLUDE_SYS_MEMFD_H */
diff --git a/libs/libc/misc/Kconfig b/libs/libc/misc/Kconfig
index 8bc4e543e2..18cde14c52 100644
--- a/libs/libc/misc/Kconfig
+++ b/libs/libc/misc/Kconfig
@@ -62,6 +62,13 @@ config LIBC_ENVPATH
Use the contents of the common environment variable to locate
executable
or library files. Default: n
+config MEM_FD_VFS_PATH
+ string "Relative path to memfd storage"
+ default "memfd"
+ depends on FS_TMPFS
+ ---help---
+ The relative path to where memfd will exist in the tmpfs
namespace.
+
config LIBC_ERR
bool "Support err() verr() verrx() warn() vwarn() vwarnx()"
default y
diff --git a/libs/libc/misc/Make.defs b/libs/libc/misc/Make.defs
index 3ce49fa0df..d196ddedf9 100644
--- a/libs/libc/misc/Make.defs
+++ b/libs/libc/misc/Make.defs
@@ -22,7 +22,7 @@
CSRCS += lib_mknod.c lib_umask.c lib_utsname.c lib_getrandom.c
CSRCS += lib_xorshift128.c lib_tea_encrypt.c lib_tea_decrypt.c
-CSRCS += lib_cxx_initialize.c lib_impure.c
+CSRCS += lib_cxx_initialize.c lib_impure.c lib_memfd.c
# Support for platforms that do not have long long types
diff --git a/libs/libc/misc/lib_memfd.c b/libs/libc/misc/lib_memfd.c
new file mode 100644
index 0000000000..1f05fe5ea8
--- /dev/null
+++ b/libs/libc/misc/lib_memfd.c
@@ -0,0 +1,51 @@
+/****************************************************************************
+ * libs/libc/misc/lib_memfd.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <sys/memfd.h>
+#include <errno.h>
+#include <stdio.h>
+#include <unistd.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define MEM_FD_VFS_PATH CONFIG_LIBC_TMPDIR "/" CONFIG_MEM_FD_VFS_PATH "/%s"
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+int memfd_create(FAR const char *name, unsigned int flags)
+{
+#ifdef CONFIG_FS_TMPFS
+ char path[PATH_MAX];
+
+ snprintf(path, sizeof(path), MEM_FD_VFS_PATH, name);
+ return open(path, O_RDWR | flags);
+#else
+ set_errno(ENOSYS);
+ return -1;
+#endif
+}