This is an automated email from the ASF dual-hosted git repository.
jerpelea pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git
The following commit(s) were added to refs/heads/master by this push:
new bbc5b51ac85 libs: Open the files of the library without a descriptor
of the caller.
bbc5b51ac85 is described below
commit bbc5b51ac85fa23e301064cbbfe09367cefb3f00
Author: Marco Casaroli <[email protected]>
AuthorDate: Fri Nov 20 12:33:03 2026 +0100
libs: Open the files of the library without a descriptor of the caller.
The _NX_OPEN, _NX_CLOSE and _NX_STAT macros of nuttx/fs/fs.h use
nx_open() in the kernel copy of the protected and kernel builds, and
open() in the flat build. Both put the descriptor into the file table
of the calling task. Thus getrandom(), the time zone load of
localtime(), the gcov dump to a directory and the ELF loader failed with
EMFILE when that table was full, and the ELF loader kept its descriptor
in that table for the whole load, where every thread of the task can
close or replace it. The XIP code of the ELF loader called ioctl(),
which sets errno, in the kernel too. gcov also returned errno after a
failed open in the kernel, where nx_open() does not set it, and did not
close the file after a failed write.
Now the _NX_FILE macros replace them in these users, and _NX_STAT
goes. The flat build and the kernel
copy use file_open() and the other file_*() interfaces on a struct file
of the caller of the macro: no descriptor of the calling task and no
errno. The user copy of the protected and kernel builds uses open()
and the other user interfaces on a descriptor, as before. A failure
gives a negated errno value through _NX_FILE_ERRVAL() in both.
getrandom() sets errno itself. The ELF loader holds the file of an
XIP pin with file_dup2() of its own file.
The gcov dump to one file and the coverage dump write through a raw
stream, which takes a descriptor: they keep _NX_OPEN and _NX_CLOSE as
before. _NX_READ, _NX_WRITE and _NX_SEEK do not change: stdio and the
raw streams use them on a descriptor that their caller gives.
Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
---
include/nuttx/fs/fs.h | 32 ++++++++++++++++++++++++++++++--
include/nuttx/lib/elf.h | 7 ++++---
libs/libbuiltin/libgcc/gcov.c | 25 +++++++++++++++----------
libs/libc/elf/elf_init.c | 20 +++++++++-----------
libs/libc/elf/elf_insert.c | 1 -
libs/libc/elf/elf_load.c | 25 ++++++++++---------------
libs/libc/elf/elf_read.c | 16 ++++++++--------
libs/libc/elf/elf_uninit.c | 6 +-----
libs/libc/misc/lib_getrandom.c | 17 +++++++++--------
libs/libc/time/lib_localtime.c | 9 ++++-----
10 files changed, 90 insertions(+), 68 deletions(-)
diff --git a/include/nuttx/fs/fs.h b/include/nuttx/fs/fs.h
index a03ebf27ee5..9437619d637 100644
--- a/include/nuttx/fs/fs.h
+++ b/include/nuttx/fs/fs.h
@@ -86,7 +86,6 @@
# define _NX_READ(f,b,s) nx_read(f,b,s)
# define _NX_WRITE(f,b,s) nx_write(f,b,s)
# define _NX_SEEK(f,o,w) nx_seek(f,o,w)
-# define _NX_STAT(f,s) nx_fstat(f,s)
# define _NX_GETERRNO(r) (-(r))
# define _NX_SETERRNO(r) set_errno(-(r))
# define _NX_GETERRVAL(r) (r)
@@ -96,12 +95,41 @@
# define _NX_READ(f,b,s) read(f,b,s)
# define _NX_WRITE(f,b,s) write(f,b,s)
# define _NX_SEEK(f,o,w) lseek(f,o,w)
-# define _NX_STAT(f,s) fstat(f,s)
# define _NX_GETERRNO(r) errno
# define _NX_SETERRNO(r) ((void)(r))
# define _NX_GETERRVAL(r) (-errno)
#endif
+/* The library code uses these interfaces for a file that it opens itself.
+ * The flat build and the kernel use a struct file: it takes no descriptor
+ * of the calling task and does not set its errno. The user space of the
+ * protected and kernel builds uses a descriptor. A result < 0 is an error,
+ * and _NX_FILE_ERRVAL() gives its negated errno value. _NX_FILE_CLOSE() of
+ * a file that did not open does nothing.
+ */
+
+#if defined(CONFIG_BUILD_FLAT) || defined(__KERNEL__)
+# define _NX_FILE struct file
+# define _NX_FILE_OPEN(f,p,o,m) file_open(f,p,o,m)
+# define _NX_FILE_CLOSE(f) file_close(f)
+# define _NX_FILE_READ(f,b,s) file_read(f,b,s)
+# define _NX_FILE_WRITE(f,b,s) file_write(f,b,s)
+# define _NX_FILE_SEEK(f,o,w) file_seek(f,o,w)
+# define _NX_FILE_STAT(f,s) file_fstat(f,s)
+# define _NX_FILE_IOCTL(f,r,a) file_ioctl(f,r,a)
+# define _NX_FILE_ERRVAL(r) (r)
+#else
+# define _NX_FILE int
+# define _NX_FILE_OPEN(f,p,o,m) (*(f) = open(p,o,m))
+# define _NX_FILE_CLOSE(f) (*(f) < 0 ? 0 : close(*(f)))
+# define _NX_FILE_READ(f,b,s) read(*(f),b,s)
+# define _NX_FILE_WRITE(f,b,s) write(*(f),b,s)
+# define _NX_FILE_SEEK(f,o,w) lseek(*(f),o,w)
+# define _NX_FILE_STAT(f,s) fstat(*(f),s)
+# define _NX_FILE_IOCTL(f,r,a) ioctl(*(f),r,a)
+# define _NX_FILE_ERRVAL(r) (-errno)
+#endif
+
/* Inode i_flags values:
*
* Bit 0-3: Inode type (Bit 3 indicates internal OS types)
diff --git a/include/nuttx/lib/elf.h b/include/nuttx/lib/elf.h
index df2dc21df83..628483dcc4d 100644
--- a/include/nuttx/lib/elf.h
+++ b/include/nuttx/lib/elf.h
@@ -34,6 +34,7 @@
#include <elf.h>
#include <nuttx/addrenv.h>
+#include <nuttx/fs/fs.h>
#include <nuttx/list.h>
/****************************************************************************
@@ -50,8 +51,8 @@
* blocks in place. The loader holds the pin through a file reference,
* because the unload runs on another task.
*
- * That reference is taken with file_get() and file_dup2(), which are kernel
- * side, so this is for the copy of libc that runs there.
+ * That reference is taken with file_dup2(), which is kernel side, so this
+ * is for the copy of libc that runs there.
*/
#if defined(CONFIG_FS_PIN) && \
@@ -273,7 +274,7 @@ struct mod_loadinfo_s
uint16_t strtabidx; /* String table section index */
uint16_t dsymtabidx; /* Dynamic symbol table section index */
uint16_t buflen; /* size of iobuffer[] */
- int filfd; /* Descriptor for the file being loaded */
+ _NX_FILE file; /* The file being loaded */
int nexports; /* ET_DYN - Number of symbols exported */
uintptr_t xipbase; /* if elf is position independent, and use
* romfs/tmps, we can try get xipbase,
diff --git a/libs/libbuiltin/libgcc/gcov.c b/libs/libbuiltin/libgcc/gcov.c
index 29b8d28b5c0..9e379f092cf 100644
--- a/libs/libbuiltin/libgcc/gcov.c
+++ b/libs/libbuiltin/libgcc/gcov.c
@@ -294,23 +294,28 @@ static int gcov_reboot_notify(FAR struct notifier_block
*nb,
static int gcov_standard_dump(FAR struct dump_args *args,
FAR char *path, FAR uint8_t *data, size_t size)
{
- int written;
- int fd;
+ ssize_t written;
+ _NX_FILE file;
+ int ret;
- fd = _NX_OPEN(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
- if (fd < 0)
+ ret = _NX_FILE_OPEN(&file, path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
+ if (ret < 0)
{
- return -errno;
+ return _NX_FILE_ERRVAL(ret);
}
- written = _NX_WRITE(fd, data, size);
- if (written != size)
+ written = _NX_FILE_WRITE(&file, data, size);
+ if (written < 0)
{
- return -errno;
+ ret = _NX_FILE_ERRVAL(written);
+ }
+ else
+ {
+ ret = (size_t)written == size ? OK : -EIO;
}
- _NX_CLOSE(fd);
- return OK;
+ _NX_FILE_CLOSE(&file);
+ return ret;
}
static int gcov_onefile_dump(FAR struct dump_args *args,
diff --git a/libs/libc/elf/elf_init.c b/libs/libc/elf/elf_init.c
index cbef6e1ba5a..02340096e85 100644
--- a/libs/libc/elf/elf_init.c
+++ b/libs/libc/elf/elf_init.c
@@ -62,13 +62,12 @@ static inline int libelf_fileinfo(FAR struct mod_loadinfo_s
*loadinfo)
/* Get the file stats */
- ret = _NX_STAT(loadinfo->filfd, &buf);
+ ret = _NX_FILE_STAT(&loadinfo->file, &buf);
if (ret < 0)
{
- int errval = _NX_GETERRNO(ret);
-
- berr("ERROR: Failed to stat file: %d\n", errval);
- return -errval;
+ ret = _NX_FILE_ERRVAL(ret);
+ berr("ERROR: Failed to stat file: %d\n", ret);
+ return ret;
}
/* Return some stats info of the file in the loadinfo structure */
@@ -110,13 +109,12 @@ int libelf_initialize(FAR const char *filename,
/* Open the binary file for reading (only) */
- loadinfo->filfd = _NX_OPEN(filename, O_RDONLY);
- if (loadinfo->filfd < 0)
+ ret = _NX_FILE_OPEN(&loadinfo->file, filename, O_RDONLY, 0);
+ if (ret < 0)
{
- int errval = _NX_GETERRNO(loadinfo->filfd);
-
- berr("ERROR: Failed to open ELF binary %s: %d\n", filename, errval);
- return -errval;
+ ret = _NX_FILE_ERRVAL(ret);
+ berr("ERROR: Failed to open ELF binary %s: %d\n", filename, ret);
+ return ret;
}
/* Get some stats info of the file. */
diff --git a/libs/libc/elf/elf_insert.c b/libs/libc/elf/elf_insert.c
index f6a54f9b4b4..f506bcc90d5 100644
--- a/libs/libc/elf/elf_insert.c
+++ b/libs/libc/elf/elf_insert.c
@@ -59,7 +59,6 @@ void libelf_dumploadinfo(FAR struct mod_loadinfo_s *loadinfo)
binfo(" textalign: %zu\n", loadinfo->textalign);
binfo(" dataalign: %zu\n", loadinfo->dataalign);
binfo(" filelen: %ld\n", (long)loadinfo->filelen);
- binfo(" filfd: %d\n", loadinfo->filfd);
binfo(" symtabidx: %d\n", loadinfo->symtabidx);
binfo(" strtabidx: %d\n", loadinfo->strtabidx);
diff --git a/libs/libc/elf/elf_load.c b/libs/libc/elf/elf_load.c
index 78577f8ef04..809c63676a8 100644
--- a/libs/libc/elf/elf_load.c
+++ b/libs/libc/elf/elf_load.c
@@ -812,11 +812,10 @@ skipload:
#ifdef HAVE_LIBC_ELF_PIN
static int libelf_pinhold(FAR struct mod_loadinfo_s *loadinfo)
{
- FAR struct file *filep;
int ret;
- /* The descriptor belongs to the task that called the loader, and the
- * unload runs on another task. Hold the file instead.
+ /* The loader closes its file when the load ends, and the unload runs
+ * later. Hold the pin through a file of its own.
*/
loadinfo->pinfile = lib_zalloc(sizeof(struct file));
@@ -825,13 +824,7 @@ static int libelf_pinhold(FAR struct mod_loadinfo_s
*loadinfo)
return -ENOMEM;
}
- ret = file_get(loadinfo->filfd, &filep);
- if (ret >= 0)
- {
- ret = file_dup2(filep, loadinfo->pinfile);
- file_put(filep);
- }
-
+ ret = file_dup2(&loadinfo->file, loadinfo->pinfile);
if (ret < 0)
{
lib_free(loadinfo->pinfile);
@@ -848,14 +841,15 @@ static int libelf_xipacquire(FAR struct mod_loadinfo_s
*loadinfo)
uintptr_t base = 0;
#ifdef HAVE_LIBC_ELF_PIN
- if (ioctl(loadinfo->filfd, XIPFSIOC_PIN, (unsigned long)&base) >= 0)
+ if (_NX_FILE_IOCTL(&loadinfo->file, XIPFSIOC_PIN,
+ (unsigned long)&base) >= 0)
{
int ret = libelf_pinhold(loadinfo);
if (ret < 0)
{
berr("ERROR: Failed to hold the pinned file: %d\n", ret);
- ioctl(loadinfo->filfd, XIPFSIOC_UNPIN, 0);
+ _NX_FILE_IOCTL(&loadinfo->file, XIPFSIOC_UNPIN, 0);
return ret;
}
@@ -865,7 +859,8 @@ static int libelf_xipacquire(FAR struct mod_loadinfo_s
*loadinfo)
}
#endif
- if (ioctl(loadinfo->filfd, FIOC_XIPBASE, (unsigned long)&base) >= 0)
+ if (_NX_FILE_IOCTL(&loadinfo->file, FIOC_XIPBASE,
+ (unsigned long)&base) >= 0)
{
loadinfo->xipbase = base;
binfo("can use xipbase %" PRIxPTR "\n", loadinfo->xipbase);
@@ -921,7 +916,7 @@ int libelf_load(FAR struct mod_loadinfo_s *loadinfo)
int i;
binfo("loadinfo: %p\n", loadinfo);
- DEBUGASSERT(loadinfo && loadinfo->filfd >= 0);
+ DEBUGASSERT(loadinfo != NULL);
/* Load section and program headers into memory */
@@ -1188,7 +1183,7 @@ int libelf_load_with_addrenv(FAR struct mod_loadinfo_s
*loadinfo)
int ret;
binfo("loadinfo: %p\n", loadinfo);
- DEBUGASSERT(loadinfo && loadinfo->filfd >= 0);
+ DEBUGASSERT(loadinfo != NULL);
/* Load section and program headers into memory */
diff --git a/libs/libc/elf/elf_read.c b/libs/libc/elf/elf_read.c
index 1ca6ec3289f..a2f1df87c1c 100644
--- a/libs/libc/elf/elf_read.c
+++ b/libs/libc/elf/elf_read.c
@@ -88,32 +88,32 @@ int libelf_read(FAR struct mod_loadinfo_s *loadinfo, FAR
uint8_t *buffer,
/* Seek to the read position */
- rpos = _NX_SEEK(loadinfo->filfd, offset, SEEK_SET);
+ rpos = _NX_FILE_SEEK(&loadinfo->file, offset, SEEK_SET);
if (rpos != offset)
{
- errval = _NX_GETERRNO(rpos);
+ errval = rpos < 0 ? _NX_FILE_ERRVAL(rpos) : -EIO;
berr("ERROR: Failed to seek to position %" PRIdOFF ": %d\n",
offset, errval);
- return -errval;
+ return errval;
}
while (readsize > 0)
{
/* Read the file data at offset into the user buffer */
- nbytes = _NX_READ(loadinfo->filfd,
- buffer + nsize - readsize, readsize);
+ nbytes = _NX_FILE_READ(&loadinfo->file,
+ buffer + nsize - readsize, readsize);
if (nbytes < 0)
{
- errval = _NX_GETERRNO(nbytes);
+ errval = _NX_FILE_ERRVAL(nbytes);
/* EINTR just means that we received a signal */
- if (errval != EINTR)
+ if (errval != -EINTR)
{
berr("ERROR: Read from offset %" PRIdOFF " failed: %d\n",
(off_t)(offset + nsize - readsize), errval);
- return -errval;
+ return errval;
}
}
else if (nbytes == 0)
diff --git a/libs/libc/elf/elf_uninit.c b/libs/libc/elf/elf_uninit.c
index 79fb98982ff..96a069c7981 100644
--- a/libs/libc/elf/elf_uninit.c
+++ b/libs/libc/elf/elf_uninit.c
@@ -60,11 +60,7 @@ int libelf_uninitialize(FAR struct mod_loadinfo_s *loadinfo)
/* Close the ELF file */
- if (loadinfo->filfd >= 0)
- {
- _NX_CLOSE(loadinfo->filfd);
- loadinfo->filfd = -1;
- }
+ _NX_FILE_CLOSE(&loadinfo->file);
return OK;
}
diff --git a/libs/libc/misc/lib_getrandom.c b/libs/libc/misc/lib_getrandom.c
index eb799603fe1..a13d4d1589a 100644
--- a/libs/libc/misc/lib_getrandom.c
+++ b/libs/libc/misc/lib_getrandom.c
@@ -25,6 +25,7 @@
****************************************************************************/
#include <sys/random.h>
+#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
@@ -64,7 +65,7 @@ ssize_t getrandom(FAR void *bytes, size_t nbytes, unsigned
int flags)
{
int oflags = O_RDONLY;
FAR const char *dev;
- int fd;
+ _NX_FILE file;
ssize_t ret;
if ((flags & GRND_NONBLOCK) != 0)
@@ -81,23 +82,23 @@ ssize_t getrandom(FAR void *bytes, size_t nbytes, unsigned
int flags)
dev = "/dev/urandom";
}
- fd = _NX_OPEN(dev, oflags);
- if (fd < 0)
+ ret = _NX_FILE_OPEN(&file, dev, oflags, 0);
+ if (ret < 0)
{
- _NX_SETERRNO(fd);
- return fd;
+ set_errno(-_NX_FILE_ERRVAL(ret));
+ return ERROR;
}
- ret = _NX_READ(fd, bytes, nbytes);
+ ret = _NX_FILE_READ(&file, bytes, nbytes);
if (ret < 0)
{
/* An error occurred on the read. */
- _NX_SETERRNO(ret);
+ set_errno(-_NX_FILE_ERRVAL(ret));
ret = ERROR;
}
- _NX_CLOSE(fd);
+ _NX_FILE_CLOSE(&file);
return ret;
}
diff --git a/libs/libc/time/lib_localtime.c b/libs/libc/time/lib_localtime.c
index 06d597b6876..e7807d25c3d 100644
--- a/libs/libc/time/lib_localtime.c
+++ b/libs/libc/time/lib_localtime.c
@@ -604,7 +604,7 @@ static int tzload(FAR const char *name,
FAR struct state_s *sp, int doextend)
{
int i;
- int fid;
+ _NX_FILE file;
int stored;
ssize_t nread;
@@ -711,14 +711,13 @@ static int tzload(FAR const char *name,
goto oops;
}
- fid = _NX_OPEN(name, O_RDONLY | O_CLOEXEC);
- if (fid < 0)
+ if (_NX_FILE_OPEN(&file, name, O_RDONLY | O_CLOEXEC, 0) < 0)
{
goto oops;
}
- nread = _NX_READ(fid, up->buf, sizeof(up->buf));
- if (_NX_CLOSE(fid) < 0 || nread < tzheadsize)
+ nread = _NX_FILE_READ(&file, up->buf, sizeof(up->buf));
+ if (_NX_FILE_CLOSE(&file) < 0 || nread < tzheadsize)
{
goto oops;
}