Skip to content

Commit

Permalink
enable O_CLOEXEC explicit
Browse files Browse the repository at this point in the history
Signed-off-by: wanggang26 <[email protected]>
  • Loading branch information
wanggang26 committed Sep 22, 2023
1 parent bb2a6ef commit 11d14ef
Show file tree
Hide file tree
Showing 25 changed files with 264 additions and 35 deletions.
8 changes: 5 additions & 3 deletions arch/arm/src/cxd56xx/cxd56_gnss.c
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,7 @@ static int cxd56_gnss_save_backup_data(struct file *filep,
}

n = file_open(&file, CONFIG_CXD56_GNSS_BACKUP_FILENAME,
O_WRONLY | O_CREAT | O_TRUNC);
O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC);
if (n < 0)
{
kmm_free(buf);
Expand Down Expand Up @@ -2333,7 +2333,8 @@ static void cxd56_gnss_read_backup_file(int *retval)
goto err;
}

ret = file_open(&file, CONFIG_CXD56_GNSS_BACKUP_FILENAME, O_RDONLY);
ret = file_open(&file, CONFIG_CXD56_GNSS_BACKUP_FILENAME,
O_RDONLY | O_CLOEXEC);
if (ret < 0)
{
kmm_free(buf);
Expand Down Expand Up @@ -2489,7 +2490,8 @@ static void cxd56_gnss_default_sighandler(uint32_t data, void *userdata)
file_close(&priv->cepfp);
}

file_open(&priv->cepfp, CONFIG_CXD56_GNSS_CEP_FILENAME, O_RDONLY);
file_open(&priv->cepfp, CONFIG_CXD56_GNSS_CEP_FILENAME,
O_RDONLY | O_CLOEXEC);
return;

case CXD56_GNSS_NOTIFY_TYPE_REQCEPCLOSE:
Expand Down
2 changes: 1 addition & 1 deletion arch/arm/src/lc823450/lc823450_ipl2.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ static int install_recovery(const char *srcpath)
return -1;
}

ret = file_open(&rfile, srcpath, O_RDONLY, 0444);
ret = file_open(&rfile, srcpath, O_RDONLY | O_CLOEXEC, 0444);

if (file_read(&rfile, &upg_image, sizeof(upg_image)) != sizeof(upg_image))
{
Expand Down
2 changes: 1 addition & 1 deletion arch/arm/src/rtl8720c/ameba_hci.c
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ static int hci_open(struct file *filep)
hci_dev_t *dev = inode->i_private;
int ret;
ret = file_open(&dev->filep,
CONFIG_AMEBA_HCI_DEV_NAME, O_RDWR);
CONFIG_AMEBA_HCI_DEV_NAME, O_RDWR | O_CLOEXEC);
if (ret < 0)
{
return ret;
Expand Down
2 changes: 1 addition & 1 deletion binfmt/libelf/libelf_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ int elf_init(FAR const char *filename, FAR struct elf_loadinfo_s *loadinfo)

/* Open the binary file for reading (only) */

ret = file_open(&loadinfo->file, filename, O_RDONLY);
ret = file_open(&loadinfo->file, filename, O_RDONLY | O_CLOEXEC);
if (ret < 0)
{
berr("Failed to open ELF binary %s: %d\n", filename, ret);
Expand Down
2 changes: 1 addition & 1 deletion binfmt/libnxflat/libnxflat_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ int nxflat_init(const char *filename, struct nxflat_loadinfo_s *loadinfo)

/* Open the binary file */

ret = file_open(&loadinfo->file, filename, O_RDONLY);
ret = file_open(&loadinfo->file, filename, O_RDONLY | O_CLOEXEC);
if (ret < 0)
{
berr("ERROR: Failed to open NXFLAT binary %s: %d\n", filename, ret);
Expand Down
2 changes: 1 addition & 1 deletion drivers/bch/bchdev_unregister.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ int bchdev_unregister(FAR const char *chardev)

/* Open the character driver associated with chardev */

ret = file_open(&filestruct, chardev, O_RDONLY);
ret = file_open(&filestruct, chardev, O_RDONLY | O_CLOEXEC);
if (ret < 0)
{
_err("ERROR: Failed to open %s: %d\n", chardev, ret);
Expand Down
4 changes: 2 additions & 2 deletions drivers/loop/losetup.c
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ int losetup(FAR const char *devname, FAR const char *filename,
ret = -ENOSYS;
if (!readonly)
{
ret = file_open(&dev->devfile, filename, O_RDWR);
ret = file_open(&dev->devfile, filename, O_RDWR | O_CLOEXEC);
}

if (ret >= 0)
Expand All @@ -384,7 +384,7 @@ int losetup(FAR const char *devname, FAR const char *filename,
{
/* If that fails, then try to open the device read-only */

ret = file_open(&dev->devfile, filename, O_RDONLY);
ret = file_open(&dev->devfile, filename, O_RDONLY | O_CLOEXEC);
if (ret < 0)
{
ferr("ERROR: Failed to open %s: %d\n", filename, ret);
Expand Down
2 changes: 1 addition & 1 deletion drivers/mtd/filemtd.c
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ FAR struct mtd_dev_s *filemtd_initialize(FAR const char *path, size_t offset,

/* Set the file open mode. */

mode = O_RDOK | O_WROK;
mode = O_RDOK | O_WROK | O_CLOEXEC;

/* Try to open the file. NOTE that block devices will use a character
* driver proxy.
Expand Down
3 changes: 2 additions & 1 deletion drivers/mtd/mtd_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include <stdbool.h>
#include <string.h>
#include <poll.h>
#include <fcntl.h>
#include <assert.h>
#include <errno.h>
#include <debug.h>
Expand Down Expand Up @@ -1801,7 +1802,7 @@ int mtdconfig_unregister(void)
FAR struct inode *inode;
FAR struct mtdconfig_struct_s *dev;

ret = file_open(&file, "/dev/config", 0);
ret = file_open(&file, "/dev/config", O_CLOEXEC);
if (ret < 0)
{
ferr("ERROR: open /dev/config failed: %d\n", ret);
Expand Down
2 changes: 1 addition & 1 deletion drivers/mtd/mtd_config_fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -2081,7 +2081,7 @@ int mtdconfig_unregister_by_path(FAR const char *path)
FAR struct inode *inode;
FAR struct nvs_fs *fs;

ret = file_open(&file, path, 0);
ret = file_open(&file, path, O_CLOEXEC);
if (ret < 0)
{
ferr("ERROR: open file %s err: %d\n", path, ret);
Expand Down
2 changes: 1 addition & 1 deletion drivers/net/slip.c
Original file line number Diff line number Diff line change
Expand Up @@ -998,7 +998,7 @@ int slip_initialize(int intf, FAR const char *devname)
self->dev.d_txavail = slip_txavail; /* New TX data callback */
self->dev.d_private = self; /* Used to recover SLIP I/F instance */

ret = file_open(&self->tty, devname, O_RDWR | O_NONBLOCK);
ret = file_open(&self->tty, devname, O_RDWR | O_NONBLOCK | O_CLOEXEC);

if (ret < 0)
{
Expand Down
2 changes: 1 addition & 1 deletion drivers/rptun/rptun.c
Original file line number Diff line number Diff line change
Expand Up @@ -841,7 +841,7 @@ static int rptun_store_open(FAR void *store_,
int len = 0x100;
int ret;

ret = file_open(&store->file, path, O_RDONLY);
ret = file_open(&store->file, path, O_RDONLY | O_CLOEXEC);
if (ret < 0)
{
return ret;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/****************************************************************************
* drivers/sensors/fakesensor.c
* drivers/sensors/fakesensor_urob.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
Expand Down Expand Up @@ -317,7 +317,8 @@ static int fakesensor_thread(int argc, char** argv)

/* Open csv file and init file handle */

ret = file_open(&sensor->data, sensor->file_path, O_RDONLY);
ret = file_open(&sensor->data, sensor->file_path,
O_RDONLY | O_CLOEXEC);
if (ret < 0)
{
snerr("Failed to open file:%s, err:%d", sensor->file_path, ret);
Expand Down
223 changes: 223 additions & 0 deletions drivers/sensors/goldfish_gps_uorb.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
/****************************************************************************
* drivers/sensors/goldfish_gps_uorb.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 <nuttx/config.h>

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <debug.h>
#include <nuttx/kmalloc.h>
#include <nuttx/kthread.h>
#include <nuttx/nuttx.h>
#include <nuttx/semaphore.h>
#include <nuttx/sensors/goldfish_gps.h>
#include <nuttx/sensors/gps.h>

/****************************************************************************
* Private Types
****************************************************************************/

struct goldfish_gps_s
{
struct gps_lowerhalf_s gps;
struct file pipe;
volatile bool running;
};

/****************************************************************************
* Private Function Prototypes
****************************************************************************/

static int goldfish_gps_activate(FAR struct gps_lowerhalf_s *lower,
FAR struct file *filep, bool enabled);
static int goldfish_gps_thread(int argc, FAR char** argv);

/****************************************************************************
* Private Data
****************************************************************************/

static struct gps_ops_s g_goldfish_gps_ops =
{
.activate = goldfish_gps_activate,
};

/****************************************************************************
* Private Functions
****************************************************************************/

static inline int goldfish_gps_write_pipe(FAR struct file *pipe,
FAR const void *buffer,
size_t size)
{
FAR const char *p = (FAR const char *)buffer;

while (size > 0)
{
int n = file_write(pipe, p, size);
if (n < 0)
{
return n;
}

p += n;
size -= n;
}

return 0;
}

static inline int goldfish_gps_open_pipe(FAR struct file *filep,
FAR const char *ns,
FAR const char *pipe_name,
int flags)
{
char buf[256];
int buf_len;
int ret;

ret = file_open(filep, "/dev/goldfish_pipe", flags);
if (ret < 0)
{
snerr("Could not open '%s': %s",
"/dev/goldfish_pipe", strerror(-ret));
return ret;
}

if (ns)
{
buf_len = snprintf(buf, sizeof(buf), "pipe:%s:%s", ns, pipe_name);
}
else
{
buf_len = snprintf(buf, sizeof(buf), "pipe:%s", pipe_name);
}

ret = goldfish_gps_write_pipe(filep, buf, buf_len + 1);
if (ret < 0)
{
snerr("Could not connect to the '%s' service: %s",
buf, strerror(-ret));
file_close(filep);
return ret;
}

return OK;
}

static int goldfish_gps_activate(FAR struct gps_lowerhalf_s *gps,
FAR struct file *filep,
bool enabled)
{
FAR struct goldfish_gps_s *priv =
container_of(gps, struct goldfish_gps_s, gps);
priv->running = enabled;

return OK;
}

static int goldfish_gps_thread(int argc, FAR char** argv)
{
FAR struct goldfish_gps_s *priv = (FAR struct goldfish_gps_s *)
((uintptr_t)strtoul(argv[1], NULL, 0));
ssize_t len;
char buf[256];

while (true)
{
len = file_read(&priv->pipe, buf, sizeof(buf));
if (priv->running && len > 0)
{
priv->gps.push_data(priv->gps.priv, buf, len, true);
}
}

return OK;
}

/****************************************************************************
* Public Functions
****************************************************************************/

/****************************************************************************
* Name: goldfish_gps_init
*
* Description:
* Goldfish GPS driver entrypoint.
*
* Input Parameters:
* devno - The user specifies which device of this type, from 0.
* batch_number- The maximum number of batch.
*
* Returned Value:
* Zero (OK) or positive on success; a negated errno value on failure.
*
****************************************************************************/

int goldfish_gps_init(int devno, uint32_t batch_number)
{
FAR struct goldfish_gps_s *gps;
FAR char *argv[2];
char arg1[32];
int ret;

/* Alloc memory for sensor */

gps = kmm_zalloc(sizeof(struct goldfish_gps_s));
if (!gps)
{
return -ENOMEM;
}

ret = goldfish_gps_open_pipe(&gps->pipe, "qemud", "gps",
O_RDWR | O_CLOEXEC);
if (ret < 0)
{
kmm_free(gps);
return ret;
}

/* Create thread for sensor */

snprintf(arg1, 32, "%p", gps);
argv[0] = arg1;
argv[1] = NULL;
ret = kthread_create("goldfish_gps_thread", SCHED_PRIORITY_DEFAULT,
CONFIG_DEFAULT_TASK_STACKSIZE,
goldfish_gps_thread, argv);
if (ret < 0)
{
file_close(&gps->pipe);
kmm_free(gps);
return ret;
}

/* Register sensor */

gps->gps.ops = &g_goldfish_gps_ops;

return gps_register(&gps->gps, devno, batch_number);
}
Loading

0 comments on commit 11d14ef

Please sign in to comment.