Squashed commit of the following:

many locations:  Change occurences of open() followed by file_detach() to file_open().  Change most non-controversion calls to open() to nx_open().

    fs/inode/fs_fileopen.c:  Flesh out file_open() with some interim, placeholder logic.

    fs/inode/fs_fileopen.c:  Add a framework for a file_open() implementation (no real logic in place yet).

    fs/vfs/fs_open.c:  Add nx_open() which is the same as open() except that it does not create a cancellation point nor does it modify the errno variable.
This commit is contained in:
Gregory Nutt 2018-09-15 10:49:41 -06:00
parent 17b0c9b6b0
commit a7fd58c4db
34 changed files with 375 additions and 261 deletions

6
TODO
View file

@ -1,4 +1,4 @@
NuttX TODO List (Last updated August 30, 2018)
NuttX TODO List (Last updated September 15, 2018)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This file summarizes known NuttX bugs, limitations, inconsistencies with
@ -268,6 +268,10 @@ o Task/Scheduler (sched/)
scheduler functions used within the OS: sched_getparam(),
sched_setparam(), sched_getscheduler(), sched_setschedule(),
and sched_setaffinity(),
2018-09-15: This change has been completed for the case of
open() used within the OS. There are places under libs/ and
configs/ that have not been converted. I also note cases
where fopen() is called under libs/libc/netdb/.
Status: Open
Priority: Low. Things are working OK the way they are. But the design

View file

@ -48,6 +48,7 @@
#include <debug.h>
#include <errno.h>
#include <nuttx/fs/fs.h>
#include <nuttx/fs/ioctl.h>
#include <nuttx/binfmt/binfmt.h>
#include <nuttx/binfmt/builtin.h>
@ -99,12 +100,11 @@ static int builtin_loadbinary(struct binary_s *binp)
/* Open the binary file for reading (only) */
fd = open(binp->filename, O_RDONLY);
fd = nx_open(binp->filename, O_RDONLY);
if (fd < 0)
{
int errval = get_errno();
berr("ERROR: Failed to open binary %s: %d\n", binp->filename, errval);
return -errval;
berr("ERROR: Failed to open binary %s: %d\n", binp->filename, fd);
return fd;
}
/* If this file is a BINFS file system, then we can recover the name of

View file

@ -48,6 +48,7 @@
#include <debug.h>
#include <errno.h>
#include <nuttx/fs/fs.h>
#include <nuttx/binfmt/elf.h>
#include "libelf.h"
@ -162,12 +163,12 @@ int elf_init(FAR const char *filename, FAR struct elf_loadinfo_s *loadinfo)
/* Open the binary file for reading (only) */
loadinfo->filfd = open(filename, O_RDONLY);
loadinfo->filfd = nx_open(filename, O_RDONLY);
if (loadinfo->filfd < 0)
{
int errval = get_errno();
berr("Failed to open ELF binary %s: %d\n", filename, errval);
return -errval;
ret = loadinfo->filfd;
berr("Failed to open ELF binary %s: %d\n", filename, ret);
return ret;
}
/* Read the ELF ehdr from offset 0 */

View file

@ -1,7 +1,7 @@
/****************************************************************************
* binfmt/libnxflat/libnxflat_init.c
*
* Copyright (C) 2009 Gregory Nutt. All rights reserved.
* Copyright (C) 2009, 2018 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@ -48,6 +48,7 @@
#include <errno.h>
#include <arpa/inet.h>
#include <nuttx/fs/fs.h>
#include <nuttx/binfmt/nxflat.h>
/****************************************************************************
@ -108,12 +109,12 @@ int nxflat_init(const char *filename, struct nxflat_loadinfo_s *loadinfo)
/* Open the binary file */
loadinfo->filfd = open(filename, O_RDONLY);
loadinfo->filfd = nx_open(filename, O_RDONLY);
if (loadinfo->filfd < 0)
{
int errval = get_errno();
berr("Failed to open NXFLAT binary %s: %d\n", filename, errval);
return -errval;
ret = loadinfo->filfd;
berr("Failed to open NXFLAT binary %s: %d\n", filename, ret);
return ret;
}
/* Read the NXFLAT header from offset 0 */

View file

@ -1,7 +1,7 @@
/****************************************************************************
* binfmt/pcode.c
*
* Copyright (C) 2014-2017 Gregory Nutt. All rights reserved.
* Copyright (C) 2014-2018 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@ -49,7 +49,7 @@
#include <nuttx/kmalloc.h>
#include <nuttx/poff.h>
#include <nuttx/fd/fs.h>
#include <nuttx/fs/fs.h>
#include <nuttx/drivers/ramdisk.h>
#include <nuttx/binfmt/binfmt.h>
#include <nuttx/binfmt/pcode.h>
@ -391,12 +391,11 @@ static int pcode_load(struct binary_s *binp)
/* Open the binary file for reading (only) */
fd = open(binp->filename, O_RDONLY);
fd = nx_open(binp->filename, O_RDONLY);
if (fd < 0)
{
int errval = get_errno();
berr("ERROR: Failed to open binary %s: %d\n", binp->filename, errval);
return -errval;
berr("ERROR: Failed to open binary %s: %d\n", binp->filename, fd);
return fd;
}
/* Read the POFF file header */

View file

@ -1,7 +1,8 @@
/****************************************************************************
* configs/ea3131/src/lpc31_fillpage.c
*
* Copyright (C) 2010, 2012-2013, 2017 Gregory Nutt. All rights reserved.
* Copyright (C) 2010, 2012-2013, 20172018 Gregory Nutt. All rights
* reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@ -277,7 +278,7 @@ static inline void lpc31_initsrc(void)
/* Open the selected path for read-only access */
g_pgsrc.fd = open(CONFIG_PAGING_BINPATH, O_RDONLY);
g_pgsrc.fd = nx_open(CONFIG_PAGING_BINPATH, O_RDONLY);
DEBUGASSERT(g_pgsrc.fd >= 0);
/* Then we are initialized */

View file

@ -1,7 +1,7 @@
/****************************************************************************
* configs/ea3152/src/lpc31_fillpage.c
*
* Copyright (C) 2011, 2013, 2017 Gregory Nutt. All rights reserved.
* Copyright (C) 2011, 2013, 2017-2018 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@ -277,7 +277,7 @@ static inline void lpc31_initsrc(void)
/* Open the selected path for read-only access */
g_pgsrc.fd = open(CONFIG_PAGING_BINPATH, O_RDONLY);
g_pgsrc.fd = nx_open(CONFIG_PAGING_BINPATH, O_RDONLY);
DEBUGASSERT(g_pgsrc.fd >= 0);
/* Then we are initialized */

View file

@ -1,7 +1,7 @@
/****************************************************************************
* configs/nucleo-144/src/stm32_bbsram.c
*
* Copyright (C) 2016 Gregory Nutt. All rights reserved.
* Copyright (C) 2016m, 2018 Gregory Nutt. All rights reserved.
* Author: David Sidrane <david_s5@nscdg.com>
*
* Redistribution and use in source and binary forms, with or without
@ -50,8 +50,10 @@
#include <debug.h>
#include <syslog.h>
#include <up_internal.h>
#include <stm32_bbsram.h>
#include <nuttx/fs/fs.h>
#include "up_internal.h"
#include "stm32_bbsram.h"
#include "nucleo-144.h"
@ -290,7 +292,7 @@ static uint8_t g_sdata[STM32F7_BBSRAM_SIZE];
static int hardfault_get_desc(struct bbsramd_s *desc)
{
int ret = -ENOENT;
int fd = open(HARDFAULT_PATH, O_RDONLY);
int fd = nx_open(HARDFAULT_PATH, O_RDONLY);
int rv;
if (fd < 0)

View file

@ -1,7 +1,7 @@
/****************************************************************************
* configs/nucleo-f3x1re/src/stm32_ajoystick.c
*
* Copyright (C) 2014, 2016-2017 Gregory Nutt. All rights reserved.
* Copyright (C) 2014, 2016-2018 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@ -451,30 +451,15 @@ int board_ajoy_initialize(void)
int i;
#ifndef NO_JOYSTICK_ADC
int fd;
iinfo("Initialize ADC driver: /dev/adc0\n");
/* NOTE: The ADC driver was initialized earlier in the bring-up sequence. */
/* Open the ADC driver for reading. */
fd = open("/dev/adc0", O_RDONLY);
if (fd < 0)
{
int errcode = get_errno();
ierr("ERROR: Failed to open /dev/adc0: %d\n", errcode);
return -errcode;
}
/* Detach the file structure from the file descriptor so that it can be
* used on any thread.
*/
ret = file_detach(fd, &g_adcfile);
ret = file_open(&g_adcfile, "/dev/adc0", O_RDONLY);
if (ret < 0)
{
ierr("ERROR: Failed to detach from file descriptor: %d\n", ret);
(void)close(fd);
ierr("ERROR: Failed to open /dev/adc0: %d\n", ret);
return ret;
}
#endif

View file

@ -172,7 +172,7 @@ int stm32l4_adc_measure_voltages(uint32_t *vrefint, uint32_t *vbat, uint32_t *ve
int ret;
int fd;
fd = open("/dev/adc0", O_RDONLY);
fd = nx_open("/dev/adc0", O_RDONLY);
if (fd < 0)
{
aerr("ERROR: Cannot open ADC converter\n");

View file

@ -1,7 +1,7 @@
/****************************************************************************
* configs/nucleo-l476rg/src/stm32_ajoystick.c
*
* Copyright (C) 2014, 2016-2017 Gregory Nutt. All rights reserved.
* Copyright (C) 2014, 2016-2018 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@ -450,30 +450,16 @@ int board_ajoy_initialize(void)
int i;
#ifndef NO_JOYSTICK_ADC
int fd;
iinfo("Initialize ADC driver: /dev/adc0\n");
/* NOTE: The ADC driver was initialized earlier in the bring-up sequence. */
/* Open the ADC driver for reading. */
fd = open("/dev/adc0", O_RDONLY);
if (fd < 0)
{
int errcode = get_errno();
ierr("ERROR: Failed to open /dev/adc0: %d\n", errcode);
return -errcode;
}
/* Detach the file structure from the file descriptor so that it can be
* used on any thread.
*/
ret = file_detach(fd, &g_adcfile);
ret = file_open(&g_adcfile, "/dev/adc0", O_RDONLY);
if (ret < 0)
{
ierr("ERROR: Failed to detach from file descriptor: %d\n", ret);
(void)close(fd);
ierr("ERROR: Failed to open /dev/adc0: %d\n", ret);
return ret;
}
#endif

View file

@ -44,6 +44,7 @@
#include <fcntl.h>
#include <stdio.h>
#include <nuttx/fs/fs.h>
#include <nuttx/drivers/pwm.h>
#include <nuttx/leds/rgbled.h>
#include <arch/board/board.h>
@ -163,7 +164,7 @@ int stm32_rgbled_setup(void)
return ret;
}
fd = open("/dev/rgbled0", O_WRONLY);
fd = nx_open("/dev/rgbled0", O_WRONLY);
if (fd < 0)
{
lederr("ERROR: open failed: %d\n", fd);
@ -172,7 +173,7 @@ int stm32_rgbled_setup(void)
/* Initialize led off */
write(fd, "#000000", 8);
(void)nx_write(fd, "#000000", 8);
close(fd);
/* Now we are initialized */

View file

@ -1,7 +1,7 @@
/************************************************************************************
* configs/photon/src/stm32_wdt.c
*
* Copyright (C) 2017 Gregory Nutt. All rights reserved.
* Copyright (C) 2017-2018 Gregory Nutt. All rights reserved.
* Author: Simon Piriou <spiriou31@gmail.com>
*
* Redistribution and use in source and binary forms, with or without
@ -48,6 +48,7 @@
#include <fcntl.h>
#include <nuttx/signal.h>
#include <nuttx/fs/fs.h>
#include <nuttx/timers/watchdog.h>
#include <arch/board/board.h>
@ -69,12 +70,12 @@ static int wdog_daemon(int argc, char *argv[])
/* Open watchdog device */
fd = open(CONFIG_WATCHDOG_DEVPATH, O_RDONLY);
fd = nx_open(CONFIG_WATCHDOG_DEVPATH, O_RDONLY);
if (fd < 0)
{
wderr("ERROR: open %s failed: %d\n", CONFIG_WATCHDOG_DEVPATH, errno);
return ERROR;
wderr("ERROR: open %s failed: %d\n", CONFIG_WATCHDOG_DEVPATH, fd);
return fd;
}
/* Start watchdog timer */
@ -128,11 +129,11 @@ int photon_watchdog_initialize(void)
/* Open the watchdog device */
fd = open(CONFIG_WATCHDOG_DEVPATH, O_RDONLY);
fd = nx_open(CONFIG_WATCHDOG_DEVPATH, O_RDONLY);
if (fd < 0)
{
wderr("ERROR: open %s failed: %d\n", CONFIG_WATCHDOG_DEVPATH, errno);
return ERROR;
wderr("ERROR: open %s failed: %d\n", CONFIG_WATCHDOG_DEVPATH, fd);
return fd;
}
/* Set the watchdog timeout */

View file

@ -1,7 +1,7 @@
/************************************************************************************
* configs/sam4s-xplained-pro/src/up_wdt.c
*
* Copyright (C) 2014, 2016-2017 Gregory Nutt. All rights reserved.
* Copyright (C) 2014, 2016-2018 Gregory Nutt. All rights reserved.
* Authors: Gregory Nutt <gnutt@nuttx.org>
* Bob Doiron
*
@ -49,6 +49,7 @@
#include <fcntl.h>
#include <nuttx/signal.h>
#include <nuttx/fs/fs.h>
#include <nuttx/timers/watchdog.h>
#include <arch/board/board.h>
@ -98,10 +99,10 @@ static int wdog_daemon(int argc, char *argv[])
/* Open the watchdog device for reading */
wdinfo("Opening.\n");
fd = open(CONFIG_WATCHDOG_DEVPATH, O_RDONLY);
fd = nx_open(CONFIG_WATCHDOG_DEVPATH, O_RDONLY);
if (fd < 0)
{
wderr("ERROR: open %s failed: %d\n", CONFIG_WATCHDOG_DEVPATH, errno);
wderr("ERROR: open %s failed: %d\n", CONFIG_WATCHDOG_DEVPATH, fd);
goto errout;
}
@ -160,11 +161,11 @@ int sam_watchdog_initialize(void)
/* Open the watchdog device */
wdinfo("Opening.\n");
fd = open(CONFIG_WATCHDOG_DEVPATH, O_RDONLY);
fd = nx_open(CONFIG_WATCHDOG_DEVPATH, O_RDONLY);
if (fd < 0)
{
wderr("ERROR: open %s failed: %d\n", CONFIG_WATCHDOG_DEVPATH, errno);
goto errout;
wderr("ERROR: open %s failed: %d\n", CONFIG_WATCHDOG_DEVPATH, fd);
goto fd;
}
/* Set the watchdog timeout */

View file

@ -1,7 +1,7 @@
/****************************************************************************
* configs/sama5d3-xplained/src/sam_ajoystick.c
*
* Copyright (C) 2014, 2016 Gregory Nutt. All rights reserved.
* Copyright (C) 2014, 2016, 2018 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@ -402,29 +402,15 @@ static int ajoy_interrupt(int irq, FAR void *context, FAR void *arg)
int sam_ajoy_initialization(void)
{
int ret;
int fd;
int i;
/* NOTE: The ADC driver was initialized earlier in the bring-up sequence. */
/* Open the ADC driver for reading. */
fd = open("/dev/adc0", O_RDONLY);
if (fd < 0)
{
int errcode = get_errno();
ierr("ERROR: Failed to open /dev/adc0: %d\n", errcode);
return -errcode;
}
/* Detach the file structure from the file descriptor so that it can be
* used on any thread.
*/
ret = file_detach(fd, &g_adcfile);
ret = file_open(&g_adcfile, "/dev/adc0", O_RDONLY);
if (ret < 0)
{
ierr("ERROR: Failed to detach from file descriptor: %d\n", ret);
(void)close(fd);
ierr("ERROR: Failed to open /dev/adc0: %d\n", ret);
return ret;
}

View file

@ -172,7 +172,7 @@ int stm32l4_adc_measure_voltages(uint32_t *vrefint, uint32_t *vbat, uint32_t *ve
int ret;
int fd;
fd = open("/dev/adc0", O_RDONLY);
fd = nx_open("/dev/adc0", O_RDONLY);
if (fd < 0)
{
aerr("ERROR: Cannot open ADC converter\n");

View file

@ -1,7 +1,8 @@
/****************************************************************************
* drivers/bch/bchdev_unregister.c
*
* Copyright (C) 2008-2009, 2012, 2016 Gregory Nutt. All rights reserved.
* Copyright (C) 2008-2009, 2012, 2016, 2018 Gregory Nutt. All rights
* reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@ -64,7 +65,8 @@
*
* Description:
* Unregister character driver access to a block device that was created
* by a previous call to bchdev_register().
/
* by a previous call to bchdev_register().
*
****************************************************************************/
@ -85,11 +87,11 @@ int bchdev_unregister(FAR const char *chardev)
/* Open the character driver associated with chardev */
fd = open(chardev, O_RDONLY);
fd = nx_open(chardev, O_RDONLY);
if (fd < 0)
{
_err("ERROR: Failed to open %s: %d\n", chardev, errno);
return -errno;
_err("ERROR: Failed to open %s: %d\n", chardev, fd);
return fd;
}
/* Get a reference to the internal data structure. On success, we

View file

@ -1,7 +1,7 @@
/****************************************************************************
* drivers/loop/losetup.c
*
* Copyright (C) 2008-2009, 2011, 2014-2015, 2017 Gregory Nutt. All
* Copyright (C) 2008-2009, 2011, 2014-2015, 2017-2018 Gregory Nutt. All
* rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
@ -373,7 +373,6 @@ int losetup(FAR const char *devname, FAR const char *filename,
FAR struct loop_struct_s *dev;
struct stat sb;
int ret;
int fd = -1;
/* Sanity check */
@ -424,12 +423,13 @@ int losetup(FAR const char *devname, FAR const char *filename,
* to open it readonly).
*/
ret = -ENOSYS;
if (!readonly)
{
fd = open(filename, O_RDWR);
ret = file_open(&dev->devfile, filename, O_RDWR);
}
if (fd >= 0)
if (ret >= 0)
{
dev->writeenabled = true; /* Success */
}
@ -438,25 +438,14 @@ int losetup(FAR const char *devname, FAR const char *filename,
{
/* If that fails, then try to open the device read-only */
fd = open(filename, O_RDONLY);
if (fd < 0)
ret = file_open(&dev->devfile, filename, O_RDONLY);
if (ret < 0)
{
ret = -get_errno();
ferr("ERROR: Failed to open %s: %d\n", filename, ret);
goto errout_with_dev;
}
}
/* Detach the file from the file descriptor */
ret = file_detach(fd, &dev->devfile);
if (ret < 0)
{
ferr("ERROR: Failed to open %s: %d\n", filename, ret);
close(fd);
goto errout_with_dev;
}
/* Inode private data will be reference to the loop device structure */
ret = register_blockdriver(devname, &g_bops, 0, dev);

View file

@ -494,7 +494,6 @@ FAR struct mtd_dev_s *blockmtd_initialize(FAR const char *path, size_t offset,
size_t nblocks;
int mode;
int ret;
int fd;
/* Create an instance of the FILE MTD device state structure */
@ -516,21 +515,10 @@ FAR struct mtd_dev_s *blockmtd_initialize(FAR const char *path, size_t offset,
* driver proxy.
*/
fd = open(path, mode);
if (fd <0)
{
ferr("ERROR: Failed to open the FILE MTD file %s\n", path);
kmm_free(priv);
return NULL;
}
/* Detach the file descriptor from the open file */
ret = file_detach(fd, &priv->mtdfile);
ret = file_open(&priv->mtdfile, path, mode);
if (ret < 0)
{
ferr("ERROR: Failed to detail the FILE MTD file %s\n", path);
close(fd);
ferr("ERROR: Failed to open the FILE MTD file %s: %d\n", path, ret);
kmm_free(priv);
return NULL;
}

View file

@ -1,7 +1,7 @@
/****************************************************************************
* drivers/net/slip.c
*
* Copyright (C) 2011-2012, 2015-2017 Gregory Nutt. All rights reserved.
* Copyright (C) 2011-2012, 2015-2018 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Reference: RFC 1055
@ -935,6 +935,7 @@ int slip_initialize(int intf, FAR const char *devname)
FAR struct slip_driver_s *priv;
char buffer[8];
FAR char *argv[2];
int ret;
/* Get the interface structure associated with this interface number. */
@ -955,11 +956,12 @@ int slip_initialize(int intf, FAR const char *devname)
/* Open the device */
priv->fd = open(devname, O_RDWR, 0666);
priv->fd = nx_open(devname, O_RDWR, 0666);
if (priv->fd < 0)
{
nerr("ERROR: Failed to open %s: %d\n", devname, errno);
return -errno;
ret = priv->fd;
nerr("ERROR: Failed to open %s: %d\n", devname, ret);
return ret;
}
/* Initialize the wait semaphore */

View file

@ -1,7 +1,7 @@
/****************************************************************************
* drivers/pipes/pipe.c
*
* Copyright (C) 2008-2009, 2015 Gregory Nutt. All rights reserved.
* Copyright (C) 2008-2009, 2015, 2018 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@ -150,7 +150,7 @@ static int pipe_close(FAR struct file *filep)
/* Perform common close operations */
ret = pipecommon_close(filep);
ret = pipecommon_close(filep);
if (ret == 0 && dev->d_refs == 0)
{
/* Release the pipe when there are no further open references to it. */
@ -254,7 +254,7 @@ int pipe2(int fd[2], size_t bufsize)
/* Get a write file descriptor */
fd[1] = open(devname, O_WRONLY);
fd[1] = nx_open(devname, O_WRONLY);
if (fd[1] < 0)
{
errcode = -fd[1];
@ -263,7 +263,7 @@ int pipe2(int fd[2], size_t bufsize)
/* Get a read file descriptor */
fd[0] = open(devname, O_RDONLY);
fd[0] = nx_open(devname, O_RDONLY);
if (fd[0] < 0)
{
errcode = -fd[0];

View file

@ -1,7 +1,7 @@
/****************************************************************************
* drivers/serial/ptmx.c
*
* Copyright (C) 2016-2017 Gregory Nutt. All rights reserved.
* Copyright (C) 2016-2018 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@ -248,8 +248,8 @@ static int ptmx_open(FAR struct file *filep)
/* Open the master device: /dev/ptyN, where N=minor */
snprintf(devname, 16, "/dev/pty%d", minor);
fd = open(devname, O_RDWR);
DEBUGASSERT(fd >= 0); /* open() should never fail */
fd = nx_open(devname, O_RDWR);
DEBUGASSERT(fd >= 0); /* nx_open() should never fail */
/* No unlink the master. This will remove it from the VFS namespace,
* the driver will still persist because of the open count on the

View file

@ -325,7 +325,6 @@ static int syslog_dev_outputready(void)
int syslog_dev_initialize(FAR const char *devpath, int oflags, int mode)
{
int fd;
int ret;
/* At this point, the only expected states are SYSLOG_UNINITIALIZED or
@ -366,12 +365,9 @@ int syslog_dev_initialize(FAR const char *devpath, int oflags, int mode)
/* Open the device driver. */
fd = open(devpath, oflags, mode);
if (fd < 0)
ret = file_open(&g_syslog_dev.sl_file, devpath, oflags, mode);
if (ret < 0)
{
int errcode = get_errno();
DEBUGASSERT(errcode > 0);
/* We failed to open the file. Perhaps it does exist? Perhaps it
* exists, but is not ready because it depends on insertion of a
* removable device?
@ -383,23 +379,6 @@ int syslog_dev_initialize(FAR const char *devpath, int oflags, int mode)
*/
g_syslog_dev.sl_state = SYSLOG_REOPEN;
return -errcode;
}
/* Detach the file descriptor from the file structure. The file
* descriptor is a task-specific concept. Detaching the file
* descriptor allows us to use the device on all threads in all tasks.
*/
ret = file_detach(fd, &g_syslog_dev.sl_file);
if (ret < 0)
{
/* This should not happen and means that something very bad has
* occurred.
*/
g_syslog_dev.sl_state = SYSLOG_FAILURE;
close(fd);
return ret;
}

View file

@ -1,7 +1,7 @@
/****************************************************************************
* fs/driver/fs_blockproxy.c
*
* Copyright (C) 2015-2017 Gregory Nutt. All rights reserved.
* Copyright (C) 2015-2018 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@ -54,6 +54,7 @@
#include <nuttx/kmalloc.h>
#include <nuttx/drivers/drivers.h>
#include <nuttx/fs/fs.h>
#if !defined(CONFIG_DISABLE_MOUNTPOINT) && \
!defined(CONFIG_DISABLE_PSEUDOFS_OPERATIONS)
@ -202,10 +203,10 @@ int block_proxy(FAR const char *blkdev, int oflags)
/* Open the newly created character driver */
oflags &= ~(O_CREAT | O_EXCL | O_APPEND | O_TRUNC);
fd = open(chardev, oflags);
fd = nx_open(chardev, oflags);
if (fd < 0)
{
ret = -errno;
ret = fd;
ferr("ERROR: Failed to open %s: %d\n", chardev, ret);
goto errout_with_bchdev;
}

View file

@ -40,7 +40,7 @@ ifneq ($(CONFIG_NFILE_DESCRIPTORS),0)
CSRCS += fs_files.c fs_foreachinode.c fs_inode.c fs_inodeaddref.c
CSRCS += fs_inodebasename.c fs_inodefind.c fs_inodefree.c fs_inoderelease.c
CSRCS += fs_inoderemove.c fs_inodereserve.c fs_inodesearch.c
CSRCS += fs_filedetach.c
CSRCS += fs_fileopen.c fs_filedetach.c
# Include inode/utils build support

108
fs/inode/fs_fileopen.c Normal file
View file

@ -0,0 +1,108 @@
/****************************************************************************
* fs/inode/fs_fileopen.c
*
* Copyright (C) 2018 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdarg.h>
#include <nuttx/fs/fs.h>
#include "inode/inode.h"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: file_open
*
* Description:
* file_open() is similar to the standard 'open' interface except that it
* returns an instance of 'struct file' rather than a file descriptor. It
* also is not a cancellation point and does not modify the errno variable.
*
* Input Parameters:
* filep - The caller provided location in which to return the 'struct
* file' instance.
* path - The full path to the file to be open.
* oflags - open flags
* ... - Variable number of arguments, may include 'mode_t mode'
*
* Returned Value:
* Zero (OK) is returned on success. On failure, a negated errno value is
* returned.
*
****************************************************************************/
int file_open(FAR struct file *filep, FAR const char *path, int oflags, ...)
{
va_list ap;
int ret;
int fd;
/* At present, this is just a placeholder. It is just a wrapper around
* nx_open() followed by a called to file_detach(). Ideally, this should
* a native open function that opens the VFS node directly without using
* any file descriptors.
*/
va_start(ap, oflags);
fd = nx_vopen(path, oflags, ap);
va_end(ap);
if (fd < 0)
{
return fd;
}
/* Detach the file structure from the file descriptor so that it can be
* used on any thread.
*/
ret = file_detach(fd, filep);
if (ret < 0)
{
(void)close(fd);
return ret;
}
return OK;
}

View file

@ -46,9 +46,7 @@
#include <sched.h>
#include <errno.h>
#include <assert.h>
#ifdef CONFIG_FILE_MODE
#include <stdarg.h>
#endif
#include <nuttx/cancelpt.h>
#include <nuttx/fs/fs.h>
@ -82,14 +80,22 @@ int inode_checkflags(FAR struct inode *inode, int oflags)
}
/****************************************************************************
* Name: open
* Name: nx_vopen
*
* Description:
* Standard 'open' interface
* nx_vopen() is identical to 'nx_open' except that it accepts a va_list
* as an argument versus taking a variable length list of arguments.
*
* nx_vopen() is an internal NuttX interface and should not be called from
* applications.
*
* Returned Value:
* Zero (OK) is returned on success; a negated errno value is returned on
* any failure.
*
****************************************************************************/
int open(FAR const char *path, int oflags, ...)
int nx_vopen(FAR const char *path, int oflags, va_list ap)
{
struct inode_search_s desc;
FAR struct file *filep;
@ -100,14 +106,9 @@ int open(FAR const char *path, int oflags, ...)
int ret;
int fd;
/* open() is a cancellation point */
(void)enter_cancellation_point();
if (path == NULL)
{
set_errno(EINVAL);
goto errout;
return -EINVAL;
}
#ifdef CONFIG_FILE_MODE
@ -119,7 +120,6 @@ int open(FAR const char *path, int oflags, ...)
if ((oflags & (O_WRONLY | O_CREAT)) != 0)
{
va_list ap;
va_start(ap, oflags);
mode = va_arg(ap, mode_t);
va_end(ap);
@ -138,7 +138,6 @@ int open(FAR const char *path, int oflags, ...)
* symbolic link."
*/
ret = -ret;
goto errout_with_search;
}
@ -169,14 +168,12 @@ int open(FAR const char *path, int oflags, ...)
fd = block_proxy(path, oflags);
if (fd < 0)
{
ret = -fd;
goto errout_with_search;
}
/* Return the file descriptor */
RELEASE_SEARCH(&desc);
leave_cancellation_point();
return fd;
}
else
@ -193,7 +190,7 @@ int open(FAR const char *path, int oflags, ...)
if (!INODE_IS_DRIVER(inode) || !inode->u.i_ops)
#endif
{
ret = ENXIO;
ret = -ENXIO;
goto errout_with_inode;
}
@ -202,7 +199,6 @@ int open(FAR const char *path, int oflags, ...)
ret = inode_checkflags(inode, oflags);
if (ret < 0)
{
ret = -ret;
goto errout_with_inode;
}
@ -211,7 +207,7 @@ int open(FAR const char *path, int oflags, ...)
fd = files_allocate(inode, oflags, 0, 0);
if (fd < 0)
{
ret = EMFILE;
ret = -EMFILE;
goto errout_with_inode;
}
@ -220,7 +216,6 @@ int open(FAR const char *path, int oflags, ...)
ret = fs_getfilep(fd, &filep);
if (ret < 0)
{
ret = -ret;
goto errout_with_inode;
}
@ -246,7 +241,6 @@ int open(FAR const char *path, int oflags, ...)
if (ret < 0)
{
ret = -ret;
goto errout_with_fd;
}
@ -282,7 +276,6 @@ int open(FAR const char *path, int oflags, ...)
#endif
RELEASE_SEARCH(&desc);
leave_cancellation_point();
return fd;
errout_with_fd:
@ -293,9 +286,74 @@ errout_with_inode:
errout_with_search:
RELEASE_SEARCH(&desc);
set_errno(ret);
errout:
leave_cancellation_point();
return ERROR;
return ret;
}
/****************************************************************************
* Name: nx_open
*
* Description:
* nx_open() is similar to the standard 'open' interface except that is is
* not a cancellation point and it does not modify the errno variable.
*
* nx_open() is an internal NuttX interface and should not be called from
* applications.
*
* Returned Value:
* Zero (OK) is returned on success; a negated errno value is returned on
* any failure.
*
****************************************************************************/
int nx_open(FAR const char *path, int oflags, ...)
{
va_list ap;
int fd;
/* Let nx_vopen() do all of the work */
va_start(ap, oflags);
fd = nx_vopen(path, oflags, ap);
va_end(ap);
return fd;
}
/****************************************************************************
* Name: open
*
* Description:
* Standard 'open' interface
*
* Returned Value:
* Zero (OK) is returned on success; -1 (ERROR) is returned on any failure
* the the errno value set appropriately.
*
****************************************************************************/
int open(FAR const char *path, int oflags, ...)
{
va_list ap;
int fd;
/* open() is a cancellation point */
(void)enter_cancellation_point();
/* Let nx_vopen() do most of the work */
va_start(ap, oflags);
fd = nx_vopen(path, oflags, ap);
va_end(ap);
/* Set the errno value if any errors were reported by nx_open() */
if (fd < 0)
{
set_errno(-fd);
fd = ERROR;
}
leave_cancellation_point();
return fd;
}

View file

@ -83,12 +83,22 @@
*/
#if defined(CONFIG_BUILD_FLAT) || defined(__KERNEL__)
# ifdef CONFIG_CPP_HAVE_VARARGS
# define _NX_OPEN(p,f,...) nx_open(f,b,##__VA_ARGS__)
# else
# define _NX_OPEN nx_open
# endif
# define _NX_READ(f,b,s) nx_read(f,b,s)
# define _NX_WRITE(f,b,s) nx_write(f,b,s)
# define _NX_GETERRNO(r) (-(r))
# define _NX_SETERRNO(r) set_errno(-(r))
# define _NX_GETERRVAL(r) (r)
#else
# ifdef CONFIG_CPP_HAVE_VARARGS
# define _NX_OPEN(p,f,...) open(f,b,##__VA_ARGS__)
# else
# define _NX_OPEN open
# endif
# define _NX_READ(f,b,s) read(f,b,s)
# define _NX_WRITE(f,b,s) write(f,b,s)
# define _NX_GETERRNO(r) errno
@ -718,6 +728,29 @@ int fs_dupfd2(int fd1, int fd2);
#endif
#endif
/****************************************************************************
* Name: file_open
*
* Description:
* file_open() is similar to the standard 'open' interface except that it
* returns an instance of 'struct file' rather than a file descriptor. It
* also is not a cancellation point and does not modify the errno variable.
*
* Input Parameters:
* filep - The caller provided location in which to return the 'struct
* file' instance.
* path - The full path to the file to be open.
* oflags - open flags
* ... - Variable number of arguments, may include 'mode_t mode'
*
* Returned Value:
* Zero (OK) is returned on success. On failure, a negated errno value is
* returned.
*
****************************************************************************/
int file_open(FAR struct file *filep, FAR const char *path, int oflags, ...);
/****************************************************************************
* Name: file_detach
*
@ -907,6 +940,28 @@ ssize_t lib_sendfile(int outfd, int infd, off_t *offset, size_t count);
int fs_getfilep(int fd, FAR struct file **filep);
#endif
/****************************************************************************
* Name: nx_open and nx_vopen
*
* Description:
* nx_open() is similar to the standard 'open' interface except that is is
* not a cancellation point and it does not modify the errno variable.
*
* nx_vopen() is identical except that it accepts a va_list as an argument
* versus taking a variable length list of arguments.
*
* nx_open() and nx_vopen are internal NuttX interface and should not be
* called from applications.
*
* Returned Value:
* Zero (OK) is returned on success; a negated errno value is returned on
* any failure.
*
****************************************************************************/
int nx_vopen(FAR const char *path, int oflags, va_list ap);
int nx_open(FAR const char *path, int oflags, ...);
/****************************************************************************
* Name: file_read
*

View file

@ -1,7 +1,7 @@
/****************************************************************************
* libs/libc/modlib/modlib_init.c
*
* Copyright (C) 2015, 2017 Gregory Nutt. All rights reserved.
* Copyright (C) 2015, 2017-2018 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@ -49,6 +49,7 @@
#include <errno.h>
#include <nuttx/module.h>
#include <nuttx/fs/fs.h>
#include <nuttx/lib/modlib.h>
#include "modlib/modlib.h"
@ -160,10 +161,10 @@ int modlib_initialize(FAR const char *filename,
/* Open the binary file for reading (only) */
loadinfo->filfd = open(filename, O_RDONLY);
loadinfo->filfd = _NX_OPEN(filename, O_RDONLY);
if (loadinfo->filfd < 0)
{
int errval = get_errno();
int errval = _NX_GETERRNO(loadinfo->filfd);
berr("ERROR: Failed to open ELF binary %s: %d\n", filename, errval);
return -errval;
}

View file

@ -114,7 +114,7 @@ FAR FILE *fopen(FAR const char *path, FAR const char *mode)
fd = open(path, oflags, 0666);
/* If the open was successful, then fdopen() the fil using the file
/* If the open was successful, then call fdopen() using the file
* descriptor returned by open. If open failed, then just return the
* NULL stream -- open() has already set the errno.
*/
@ -131,6 +131,7 @@ FAR FILE *fopen(FAR const char *path, FAR const char *mode)
(void)close(fd);
}
}
return ret;
}

View file

@ -254,35 +254,22 @@ static int local_rx_open(FAR struct local_conn_s *conn, FAR const char *path,
{
int oflags = nonblock ? O_RDONLY | O_NONBLOCK : O_RDONLY;
int ret;
int fd;
fd = open(path, oflags);
if (fd < 0)
ret = file_open(&conn->lc_infile, path, oflags);
if (ret < 0)
{
int errcode = get_errno();
DEBUGASSERT(errcode > 0);
nerr("ERROR: Failed on open %s for reading: %d\n",
path, errcode);
path, ret);
/* Map the errcode to something consistent with the return
/* Map the error code to something consistent with the return
* error codes from connect():
*
* If errcode is ENOENT, meaning that the FIFO does exist,
* If error is ENOENT, meaning that the FIFO does exist,
* return EFAULT meaning that the socket structure address is
* outside the user's address space.
*/
return errcode == ENOENT ? -EFAULT : -errcode;
}
/* Detach the file descriptor from the open file instance */
ret = file_detach(fd, &conn->lc_infile);
if (ret < 0)
{
close(fd);
return ret;
return ret == -ENOENT ? -EFAULT : ret;
}
return OK;
@ -301,35 +288,22 @@ static int local_tx_open(FAR struct local_conn_s *conn, FAR const char *path,
{
int oflags = nonblock ? O_WRONLY | O_NONBLOCK : O_WRONLY;
int ret;
int fd;
fd = open(path, oflags);
if (fd < 0)
ret = file_open(&conn->lc_outfile, path, oflags);
if (ret < 0)
{
int errcode = get_errno();
DEBUGASSERT(errcode > 0);
nerr("ERROR: Failed on open %s for writing: %d\n",
path, errcode);
path, ret);
/* Map the errcode to something consistent with the return
/* Map the error code to something consistent with the return
* error codes from connect():
*
* If errcode is ENOENT, meaning that the FIFO does exist,
* If error is ENOENT, meaning that the FIFO does exist,
* return EFAULT meaning that the socket structure address is
* outside the user's address space.
*/
return errcode == ENOENT ? -EFAULT : -errcode;
}
/* Detach the file descriptor from the open file instance */
ret = file_detach(fd, &conn->lc_outfile);
if (ret < 0)
{
close(fd);
return ret;
return ret == -ENOENT ? -EFAULT : -errcode;
}
return OK;

View file

@ -110,27 +110,13 @@ int net_openroute_detached(FAR const char *pathname, int oflags,
FAR struct file *filep)
{
int ret;
int fd;
/* Open the file for read/write access. Here we borrow the file descriptor
* of this tread of execution. We won't use it for long.
*/
/* Open the file for read/write access. */
fd = open(pathname, oflags, 0644);
if (fd < 0)
{
int errcode = get_errno();
nerr("ERROR: Failed to open %s: %d\n", pathname, errcode);
return -errcode;
}
/* Now detach the file descriptor */
ret = file_detach(fd, filep);
ret = file_open(filep, pathname, oflags, 0644);
if (ret < 0)
{
nerr("ERROR: file_detach() failed: %d\n", ret);
(void)close(fd);
nerr("ERROR: Failed to open %s: %d\n", pathname, ret);
return ret;
}

View file

@ -1,7 +1,8 @@
/****************************************************************************
* sched/group/group_setupidlefiles.c
*
* Copyright (C) 2007-2010, 2012-2013 Gregory Nutt. All rights reserved.
* Copyright (C) 2007-2010, 2012-2013, 2018 Gregory Nutt. All rights
* reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@ -104,7 +105,7 @@ int group_setupidlefiles(FAR struct task_tcb_s *tcb)
*/
#if CONFIG_NFILE_DESCRIPTORS > 0 && defined(CONFIG_DEV_CONSOLE)
fd = open("/dev/console", O_RDWR);
fd = nx_open("/dev/console", O_RDWR);
if (fd == 0)
{
/* Successfully opened /dev/console as stdin (fd == 0) */
@ -125,7 +126,7 @@ int group_setupidlefiles(FAR struct task_tcb_s *tcb)
}
else
{
serr("ERROR: Failed to open /dev/console: %d\n", errno);
serr("ERROR: Failed to open /dev/console: %d\n", fd);
}
return -ENFILE;

View file

@ -46,6 +46,7 @@
#include <nuttx/signal.h>
#include <nuttx/spawn.h>
#include <nuttx/fs/fs.h>
#include "task/spawn.h"
#include "task/task.h"
@ -120,10 +121,10 @@ static inline int spawn_open(FAR struct spawn_open_file_action_s *action)
sinfo("Open'ing path=%s oflags=%04x mode=%04x\n",
action->path, action->oflags, action->mode);
fd = open(action->path, action->oflags, action->mode);
fd = nx_open(action->path, action->oflags, action->mode);
if (fd < 0)
{
ret = get_errno();
ret = fd;
serr("ERROR: open failed: %d\n", ret);
}