2008-07-26 15:12:11 +02:00
|
|
|
/****************************************************************************
|
2009-10-18 15:52:21 +02:00
|
|
|
* drivers/pipes/pipe_common.c
|
2008-07-26 15:12:11 +02:00
|
|
|
*
|
2020-03-31 21:59:25 +02:00
|
|
|
* 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
|
2008-07-26 15:12:11 +02:00
|
|
|
*
|
2020-03-31 21:59:25 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2008-07-26 15:12:11 +02:00
|
|
|
*
|
2020-03-31 21:59:25 +02:00
|
|
|
* 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.
|
2008-07-26 15:12:11 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2009-09-13 18:15:46 +02:00
|
|
|
|
2009-12-15 15:25:14 +01:00
|
|
|
#include <sys/types.h>
|
2008-07-26 15:12:11 +02:00
|
|
|
#include <sys/stat.h>
|
2015-01-30 18:14:24 +01:00
|
|
|
#include <sys/ioctl.h>
|
2009-12-15 15:25:14 +01:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
2008-07-26 15:12:11 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sched.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
2008-11-17 21:24:28 +01:00
|
|
|
#include <debug.h>
|
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2009-09-14 00:32:01 +02:00
|
|
|
# include <nuttx/arch.h>
|
|
|
|
#endif
|
2016-11-03 18:00:47 +01:00
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
#include <nuttx/semaphore.h>
|
|
|
|
#include <nuttx/fs/fs.h>
|
|
|
|
#include <nuttx/fs/ioctl.h>
|
2008-07-26 15:12:11 +02:00
|
|
|
|
2008-08-10 18:57:15 +02:00
|
|
|
#include "pipe_common.h"
|
2008-07-26 15:12:11 +02:00
|
|
|
|
2016-07-19 21:34:18 +02:00
|
|
|
#ifdef CONFIG_PIPES
|
2008-07-26 15:12:11 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
2009-09-15 22:55:06 +02:00
|
|
|
* Pre-processor Definitions
|
2008-07-26 15:12:11 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2009-09-15 22:55:06 +02:00
|
|
|
/* CONFIG_DEV_PIPEDUMP will dump the contents of each transfer into and out
|
|
|
|
* of the pipe.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEV_PIPEDUMP
|
|
|
|
# define pipe_dumpbuffer(m,a,n) lib_dumpbuffer(m,a,n)
|
|
|
|
#else
|
|
|
|
# define pipe_dumpbuffer(m,a,n)
|
|
|
|
#endif
|
|
|
|
|
2008-07-26 15:12:11 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pipecommon_semtake
|
|
|
|
****************************************************************************/
|
2008-08-10 18:57:15 +02:00
|
|
|
|
2020-03-31 21:59:25 +02:00
|
|
|
static int pipecommon_semtake(FAR sem_t *sem)
|
2008-07-26 15:12:11 +02:00
|
|
|
{
|
2020-03-31 21:59:25 +02:00
|
|
|
return nxsem_wait_uninterruptible(sem);
|
2008-07-26 15:12:11 +02:00
|
|
|
}
|
|
|
|
|
2008-11-17 21:24:28 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: pipecommon_pollnotify
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-05-26 18:01:15 +02:00
|
|
|
static void pipecommon_pollnotify(FAR struct pipe_dev_s *dev,
|
|
|
|
pollevent_t eventset)
|
2008-11-17 21:24:28 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2015-05-12 15:41:12 +02:00
|
|
|
if (eventset & POLLERR)
|
|
|
|
{
|
|
|
|
eventset &= ~(POLLOUT | POLLIN);
|
|
|
|
}
|
|
|
|
|
2008-11-17 21:24:28 +01:00
|
|
|
for (i = 0; i < CONFIG_DEV_PIPE_NPOLLWAITERS; i++)
|
|
|
|
{
|
2016-05-26 18:01:15 +02:00
|
|
|
FAR struct pollfd *fds = dev->d_fds[i];
|
2016-07-26 18:09:35 +02:00
|
|
|
|
2008-11-17 21:24:28 +01:00
|
|
|
if (fds)
|
|
|
|
{
|
2015-05-12 15:41:12 +02:00
|
|
|
fds->revents |= eventset & (fds->events | POLLERR | POLLHUP);
|
|
|
|
|
|
|
|
if ((fds->revents & (POLLOUT | POLLHUP)) == (POLLOUT | POLLHUP))
|
|
|
|
{
|
|
|
|
/* POLLOUT and POLLHUP are mutually exclusive. */
|
|
|
|
|
|
|
|
fds->revents &= ~POLLOUT;
|
|
|
|
}
|
|
|
|
|
2008-11-17 21:24:28 +01:00
|
|
|
if (fds->revents != 0)
|
|
|
|
{
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Report events: %02x\n", fds->revents);
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(fds->sem);
|
2008-11-17 21:24:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-26 15:12:11 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pipecommon_allocdev
|
|
|
|
****************************************************************************/
|
2008-08-10 18:57:15 +02:00
|
|
|
|
2016-07-19 21:34:18 +02:00
|
|
|
FAR struct pipe_dev_s *pipecommon_allocdev(size_t bufsize)
|
2008-07-26 15:12:11 +02:00
|
|
|
{
|
2015-10-04 23:04:00 +02:00
|
|
|
FAR struct pipe_dev_s *dev;
|
2008-07-26 15:12:11 +02:00
|
|
|
|
2016-07-19 21:40:54 +02:00
|
|
|
DEBUGASSERT(bufsize <= CONFIG_DEV_PIPE_MAXSIZE);
|
2016-07-19 21:34:18 +02:00
|
|
|
|
2008-07-26 16:02:46 +02:00
|
|
|
/* Allocate a private structure to manage the pipe */
|
2008-07-26 15:12:11 +02:00
|
|
|
|
2016-05-26 18:01:15 +02:00
|
|
|
dev = (FAR struct pipe_dev_s *)kmm_malloc(sizeof(struct pipe_dev_s));
|
2008-07-26 15:12:11 +02:00
|
|
|
if (dev)
|
|
|
|
{
|
|
|
|
/* Initialize the private structure */
|
|
|
|
|
2008-07-27 16:58:36 +02:00
|
|
|
memset(dev, 0, sizeof(struct pipe_dev_s));
|
2017-10-03 20:51:15 +02:00
|
|
|
nxsem_init(&dev->d_bfsem, 0, 1);
|
|
|
|
nxsem_init(&dev->d_rdsem, 0, 0);
|
|
|
|
nxsem_init(&dev->d_wrsem, 0, 0);
|
2016-07-19 21:34:18 +02:00
|
|
|
|
2019-12-07 06:36:33 +01:00
|
|
|
/* The read/write wait semaphores are used for signaling and, hence,
|
|
|
|
* should not have priority inheritance enabled.
|
|
|
|
*/
|
2016-11-03 18:00:47 +01:00
|
|
|
|
2020-05-17 15:56:21 +02:00
|
|
|
nxsem_set_protocol(&dev->d_rdsem, SEM_PRIO_NONE);
|
|
|
|
nxsem_set_protocol(&dev->d_wrsem, SEM_PRIO_NONE);
|
2016-11-03 18:00:47 +01:00
|
|
|
|
2016-07-19 21:34:18 +02:00
|
|
|
dev->d_bufsize = bufsize;
|
2008-07-26 15:12:11 +02:00
|
|
|
}
|
2012-07-15 16:56:25 +02:00
|
|
|
|
2008-07-26 15:12:11 +02:00
|
|
|
return dev;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pipecommon_freedev
|
|
|
|
****************************************************************************/
|
2008-08-10 18:57:15 +02:00
|
|
|
|
|
|
|
void pipecommon_freedev(FAR struct pipe_dev_s *dev)
|
2008-07-26 15:12:11 +02:00
|
|
|
{
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_destroy(&dev->d_bfsem);
|
|
|
|
nxsem_destroy(&dev->d_rdsem);
|
|
|
|
nxsem_destroy(&dev->d_wrsem);
|
2015-01-31 19:05:01 +01:00
|
|
|
kmm_free(dev);
|
2008-07-26 15:12:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pipecommon_open
|
|
|
|
****************************************************************************/
|
2008-08-10 18:57:15 +02:00
|
|
|
|
2008-07-26 15:12:11 +02:00
|
|
|
int pipecommon_open(FAR struct file *filep)
|
|
|
|
{
|
2016-05-26 18:01:15 +02:00
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct pipe_dev_s *dev = inode->i_private;
|
|
|
|
int sval;
|
|
|
|
int ret;
|
2014-04-13 22:32:20 +02:00
|
|
|
|
2015-10-10 18:41:00 +02:00
|
|
|
DEBUGASSERT(dev != NULL);
|
2012-07-15 16:56:25 +02:00
|
|
|
|
2011-03-02 01:33:42 +01:00
|
|
|
/* Make sure that we have exclusive access to the device structure. The
|
2020-03-31 21:59:25 +02:00
|
|
|
* nxsem_wait() call should fail if we are awakened by a signal or if the
|
|
|
|
* thread was canceled.
|
2011-03-02 01:33:42 +01:00
|
|
|
*/
|
2008-07-26 15:12:11 +02:00
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
ret = nxsem_wait(&dev->d_bfsem);
|
|
|
|
if (ret < 0)
|
2008-07-26 15:12:11 +02:00
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
ferr("ERROR: nxsem_wait failed: %d\n", ret);
|
|
|
|
return ret;
|
2011-03-02 01:33:42 +01:00
|
|
|
}
|
|
|
|
|
2015-01-31 19:05:01 +01:00
|
|
|
/* If this the first reference on the device, then allocate the buffer.
|
|
|
|
* In the case of policy 1, the buffer already be present when the pipe
|
|
|
|
* is first opened.
|
2015-01-30 18:14:24 +01:00
|
|
|
*/
|
2008-07-27 16:58:36 +02:00
|
|
|
|
2019-08-06 15:31:57 +02:00
|
|
|
if (inode->i_crefs == 1 && dev->d_buffer == NULL)
|
2011-03-02 01:33:42 +01:00
|
|
|
{
|
2016-07-19 21:34:18 +02:00
|
|
|
dev->d_buffer = (FAR uint8_t *)kmm_malloc(dev->d_bufsize);
|
2011-03-02 01:33:42 +01:00
|
|
|
if (!dev->d_buffer)
|
2008-07-27 16:58:36 +02:00
|
|
|
{
|
2020-01-02 17:49:34 +01:00
|
|
|
nxsem_post(&dev->d_bfsem);
|
2011-03-02 01:33:42 +01:00
|
|
|
return -ENOMEM;
|
2008-07-27 16:58:36 +02:00
|
|
|
}
|
2011-03-02 01:33:42 +01:00
|
|
|
}
|
2008-07-27 16:58:36 +02:00
|
|
|
|
2020-05-17 16:47:40 +02:00
|
|
|
/* If opened for writing, increment the count of writers on the pipe
|
|
|
|
* instance.
|
|
|
|
*/
|
2008-07-26 15:12:11 +02:00
|
|
|
|
2011-03-02 01:33:42 +01:00
|
|
|
if ((filep->f_oflags & O_WROK) != 0)
|
|
|
|
{
|
|
|
|
dev->d_nwriters++;
|
2008-07-26 16:24:17 +02:00
|
|
|
|
Sources and Docs: Fix typos and nxstyle issues
Documentation/contributing/coding_style.rst:
* Fix repeated words: ("this this").
* Remove trailing spaces.
boards/z80/z80/z80sim/README.txt:
* Fix repeated words: ("this this") and rewrap lines.
graphics/Kconfig,
libs/libc/math/Kconfig:
* Fix repeated words: ("this this").
arch/arm/src/armv7-a/arm_assert.c,
arch/arm/src/armv7-r/arm_assert.c,
arch/arm/src/imxrt/imxrt_enet.c,
arch/arm/src/kinetis/kinetis_enet.c,
arch/arm/src/kinetis/kinetis_flexcan.c,
arch/arm/src/s32k1xx/s32k1xx_enet.c,
arch/arm/src/s32k1xx/s32k1xx_flexcan.c,
arch/arm/src/stm32/stm32_pwm.c,
arch/arm/src/stm32h7/stm32_pwm.c,
arch/arm/src/stm32l4/stm32l4_pwm.c,
arch/renesas/src/rx65n/rx65n_usbdev.c,
binfmt/libnxflat/libnxflat_bind.c,
drivers/pipes/pipe_common.c,
net/igmp/igmp_input.c,
net/tcp/tcp_conn.c,
sched/sched/sched_roundrobin.c:
* Fix typo in comment ("this this").
arch/arm/src/cxd56xx/cxd56_usbdev.c,
arch/arm/src/lc823450/lc823450_usbdev.c:
* Fix typo in comment and rewrap lines.
arch/arm/src/imxrt/imxrt_usbdev.c,
arch/arm/src/stm32/stm32_dac.c,
arch/arm/src/stm32f0l0g0/stm32_pwm.c,
arch/arm/src/stm32f7/stm32_pwm.c,
arch/arm/src/tiva/lm/lm4f_gpio.h,
fs/nxffs/nxffs_write.c,
include/nuttx/analog/pga11x.h,
include/nuttx/usb/usbdev.h,
net/mld/mld_join.c:
* Fix typo in comment ("this this").
* Fix nxstyle issues.
2020-10-02 01:29:35 +02:00
|
|
|
/* If this is the first writer, then the read semaphore indicates the
|
|
|
|
* number of readers waiting for the first writer. Wake them all up.
|
2011-03-02 01:33:42 +01:00
|
|
|
*/
|
2009-09-13 18:15:46 +02:00
|
|
|
|
2011-03-02 01:33:42 +01:00
|
|
|
if (dev->d_nwriters == 1)
|
|
|
|
{
|
2020-05-17 15:56:21 +02:00
|
|
|
while (nxsem_get_value(&dev->d_rdsem, &sval) == 0 && sval < 0)
|
2008-07-26 16:24:17 +02:00
|
|
|
{
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&dev->d_rdsem);
|
2008-07-26 16:24:17 +02:00
|
|
|
}
|
2008-07-26 15:12:11 +02:00
|
|
|
}
|
2011-03-02 01:33:42 +01:00
|
|
|
}
|
|
|
|
|
2020-05-17 16:47:40 +02:00
|
|
|
/* If opened for reading, increment the count of reader on on the pipe
|
|
|
|
* instance.
|
|
|
|
*/
|
2015-05-12 15:41:12 +02:00
|
|
|
|
|
|
|
if ((filep->f_oflags & O_RDOK) != 0)
|
|
|
|
{
|
|
|
|
dev->d_nreaders++;
|
|
|
|
}
|
|
|
|
|
2015-01-30 18:14:24 +01:00
|
|
|
/* If opened for read-only, then wait for either (1) at least one writer
|
|
|
|
* on the pipe (policy == 0), or (2) until there is buffered data to be
|
|
|
|
* read (policy == 1).
|
|
|
|
*/
|
2008-07-26 15:12:11 +02:00
|
|
|
|
2011-03-02 01:33:42 +01:00
|
|
|
sched_lock();
|
2020-01-02 17:49:34 +01:00
|
|
|
nxsem_post(&dev->d_bfsem);
|
2015-01-30 18:14:24 +01:00
|
|
|
|
2021-08-13 14:54:11 +02:00
|
|
|
if (!(filep->f_oflags & O_NONBLOCK) && /* Non-blocking */
|
|
|
|
(filep->f_oflags & O_RDWR) == O_RDONLY && /* Read-only */
|
2015-01-30 18:14:24 +01:00
|
|
|
dev->d_nwriters < 1 && /* No writers on the pipe */
|
|
|
|
dev->d_wrndx == dev->d_rdndx) /* Buffer is empty */
|
2011-03-02 01:33:42 +01:00
|
|
|
{
|
|
|
|
/* NOTE: d_rdsem is normally used when the read logic waits for more
|
|
|
|
* data to be written. But until the first writer has opened the
|
|
|
|
* pipe, the meaning is different: it is used prevent O_RDONLY open
|
|
|
|
* calls from returning until there is at least one writer on the pipe.
|
|
|
|
* This is required both by spec and also because it prevents
|
|
|
|
* subsequent read() calls from returning end-of-file because there is
|
|
|
|
* no writer on the pipe.
|
|
|
|
*/
|
2008-07-26 16:02:46 +02:00
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
ret = nxsem_wait(&dev->d_rdsem);
|
|
|
|
if (ret < 0)
|
2008-07-26 16:24:17 +02:00
|
|
|
{
|
2020-03-31 21:59:25 +02:00
|
|
|
/* The nxsem_wait() call should fail if we are awakened by a
|
|
|
|
* signal or if the task is canceled.
|
2008-07-26 16:24:17 +02:00
|
|
|
*/
|
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
ferr("ERROR: nxsem_wait failed: %d\n", ret);
|
2011-03-02 01:33:42 +01:00
|
|
|
|
|
|
|
/* Immediately close the pipe that we just opened */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
pipecommon_close(filep);
|
2008-07-26 16:24:17 +02:00
|
|
|
}
|
2011-03-02 01:33:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sched_unlock();
|
|
|
|
return ret;
|
2008-07-26 15:12:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pipecommon_close
|
|
|
|
****************************************************************************/
|
2008-08-10 18:57:15 +02:00
|
|
|
|
2008-07-26 15:12:11 +02:00
|
|
|
int pipecommon_close(FAR struct file *filep)
|
|
|
|
{
|
2016-05-26 18:01:15 +02:00
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct pipe_dev_s *dev = inode->i_private;
|
|
|
|
int sval;
|
2020-03-31 21:59:25 +02:00
|
|
|
int ret;
|
2008-07-26 15:12:11 +02:00
|
|
|
|
2019-08-06 15:31:57 +02:00
|
|
|
DEBUGASSERT(dev && filep->f_inode->i_crefs > 0);
|
2008-07-26 15:12:11 +02:00
|
|
|
|
|
|
|
/* Make sure that we have exclusive access to the device structure.
|
|
|
|
* NOTE: close() is supposed to return EINTR if interrupted, however
|
|
|
|
* I've never seen anyone check that.
|
|
|
|
*/
|
|
|
|
|
2020-03-31 21:59:25 +02:00
|
|
|
ret = pipecommon_semtake(&dev->d_bfsem);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* The close will not be performed if the task was canceled */
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2008-07-26 15:12:11 +02:00
|
|
|
|
2015-01-30 18:14:24 +01:00
|
|
|
/* Decrement the number of references on the pipe. Check if there are
|
2015-01-31 19:05:01 +01:00
|
|
|
* still outstanding references to the pipe.
|
2015-01-30 18:14:24 +01:00
|
|
|
*/
|
|
|
|
|
2019-08-06 15:31:57 +02:00
|
|
|
/* Check if the decremented inode reference count would go to zero */
|
2008-07-26 15:12:11 +02:00
|
|
|
|
2019-08-06 15:31:57 +02:00
|
|
|
if (inode->i_crefs > 1)
|
2008-07-26 15:12:11 +02:00
|
|
|
{
|
2019-08-06 15:31:57 +02:00
|
|
|
/* More references.. If opened for writing, decrement the count of
|
2015-01-30 18:14:24 +01:00
|
|
|
* writers on the pipe instance.
|
|
|
|
*/
|
2008-07-26 15:12:11 +02:00
|
|
|
|
|
|
|
if ((filep->f_oflags & O_WROK) != 0)
|
|
|
|
{
|
2020-03-31 21:59:25 +02:00
|
|
|
/* If there are no longer any writers on the pipe, then notify all
|
|
|
|
* of the waiting readers that they must return end-of-file.
|
2008-07-26 15:12:11 +02:00
|
|
|
*/
|
|
|
|
|
2008-07-27 16:58:36 +02:00
|
|
|
if (--dev->d_nwriters <= 0)
|
2008-07-26 15:12:11 +02:00
|
|
|
{
|
2020-09-26 14:25:03 +02:00
|
|
|
/* Inform poll readers that other end closed. */
|
|
|
|
|
|
|
|
pipecommon_pollnotify(dev, POLLHUP);
|
|
|
|
|
2020-05-17 15:56:21 +02:00
|
|
|
while (nxsem_get_value(&dev->d_rdsem, &sval) == 0 && sval < 0)
|
2008-07-26 16:02:46 +02:00
|
|
|
{
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&dev->d_rdsem);
|
2008-07-26 16:02:46 +02:00
|
|
|
}
|
2015-05-12 15:41:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If opened for reading, decrement the count of readers on the pipe
|
|
|
|
* instance.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((filep->f_oflags & O_RDOK) != 0)
|
|
|
|
{
|
|
|
|
if (--dev->d_nreaders <= 0)
|
|
|
|
{
|
|
|
|
if (PIPE_IS_POLICY_0(dev->d_flags))
|
|
|
|
{
|
|
|
|
/* Inform poll writers that other end closed. */
|
|
|
|
|
|
|
|
pipecommon_pollnotify(dev, POLLERR);
|
2021-10-20 06:25:30 +02:00
|
|
|
while (nxsem_get_value(&dev->d_wrsem, &sval) == 0
|
|
|
|
&& sval < 0)
|
|
|
|
{
|
|
|
|
nxsem_post(&dev->d_wrsem);
|
|
|
|
}
|
2015-05-12 15:41:12 +02:00
|
|
|
}
|
2008-07-26 15:12:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-30 18:14:24 +01:00
|
|
|
|
2015-11-22 15:38:55 +01:00
|
|
|
/* What is the buffer management policy? Do we free the buffer when the
|
2015-01-31 19:05:01 +01:00
|
|
|
* last client closes the pipe policy 0, or when the buffer becomes empty.
|
|
|
|
* In the latter case, the buffer data will remain valid and can be
|
|
|
|
* obtained when the pipe is re-opened.
|
2015-01-30 18:14:24 +01:00
|
|
|
*/
|
|
|
|
|
2015-01-31 19:05:01 +01:00
|
|
|
else if (PIPE_IS_POLICY_0(dev->d_flags) || dev->d_wrndx == dev->d_rdndx)
|
2008-07-27 16:58:36 +02:00
|
|
|
{
|
2015-01-30 18:14:24 +01:00
|
|
|
/* Policy 0 or the buffer is empty ... deallocate the buffer now. */
|
2008-07-27 16:58:36 +02:00
|
|
|
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(dev->d_buffer);
|
2008-07-27 16:58:36 +02:00
|
|
|
dev->d_buffer = NULL;
|
|
|
|
|
|
|
|
/* And reset all counts and indices */
|
2014-04-13 22:32:20 +02:00
|
|
|
|
2008-07-27 16:58:36 +02:00
|
|
|
dev->d_wrndx = 0;
|
|
|
|
dev->d_rdndx = 0;
|
|
|
|
dev->d_nwriters = 0;
|
2015-05-12 15:41:12 +02:00
|
|
|
dev->d_nreaders = 0;
|
2015-01-31 19:05:01 +01:00
|
|
|
|
2015-02-18 16:34:58 +01:00
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
2015-01-31 19:05:01 +01:00
|
|
|
/* If, in addition, we have been unlinked, then also need to free the
|
|
|
|
* device structure as well to prevent a memory leak.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (PIPE_IS_UNLINKED(dev->d_flags))
|
|
|
|
{
|
|
|
|
pipecommon_freedev(dev);
|
|
|
|
return OK;
|
|
|
|
}
|
2015-02-18 16:34:58 +01:00
|
|
|
#endif
|
2016-07-18 16:00:56 +02:00
|
|
|
}
|
2008-07-26 15:12:11 +02:00
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&dev->d_bfsem);
|
2008-07-26 15:12:11 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pipecommon_read
|
|
|
|
****************************************************************************/
|
2008-08-10 18:57:15 +02:00
|
|
|
|
2008-07-26 15:12:11 +02:00
|
|
|
ssize_t pipecommon_read(FAR struct file *filep, FAR char *buffer, size_t len)
|
|
|
|
{
|
2016-05-26 18:01:15 +02:00
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct pipe_dev_s *dev = inode->i_private;
|
2009-09-15 22:55:06 +02:00
|
|
|
#ifdef CONFIG_DEV_PIPEDUMP
|
2016-05-26 18:01:15 +02:00
|
|
|
FAR uint8_t *start = (FAR uint8_t *)buffer;
|
2009-09-15 22:55:06 +02:00
|
|
|
#endif
|
2016-05-26 18:01:15 +02:00
|
|
|
ssize_t nread = 0;
|
|
|
|
int sval;
|
|
|
|
int ret;
|
2008-07-26 15:12:11 +02:00
|
|
|
|
2015-01-30 18:14:24 +01:00
|
|
|
DEBUGASSERT(dev);
|
2008-07-26 15:12:11 +02:00
|
|
|
|
2015-03-13 14:21:06 +01:00
|
|
|
if (len == 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-07-26 15:12:11 +02:00
|
|
|
/* Make sure that we have exclusive access to the device structure */
|
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
ret = nxsem_wait(&dev->d_bfsem);
|
|
|
|
if (ret < 0)
|
2008-07-26 15:12:11 +02:00
|
|
|
{
|
2020-03-31 21:59:25 +02:00
|
|
|
/* May fail because a signal was received or if the task was
|
|
|
|
* canceled.
|
|
|
|
*/
|
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
return ret;
|
2008-07-26 15:12:11 +02:00
|
|
|
}
|
|
|
|
|
2008-07-26 16:02:46 +02:00
|
|
|
/* If the pipe is empty, then wait for something to be written to it */
|
2008-07-26 15:12:11 +02:00
|
|
|
|
2008-07-27 16:58:36 +02:00
|
|
|
while (dev->d_wrndx == dev->d_rdndx)
|
2008-07-26 15:12:11 +02:00
|
|
|
{
|
2020-09-21 11:41:20 +02:00
|
|
|
/* If there are no writers on the pipe, then return end of file */
|
2008-07-26 15:12:11 +02:00
|
|
|
|
2020-09-21 11:41:20 +02:00
|
|
|
if (dev->d_nwriters <= 0)
|
2008-07-26 15:12:11 +02:00
|
|
|
{
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&dev->d_bfsem);
|
2020-09-21 11:41:20 +02:00
|
|
|
return 0;
|
2008-07-26 15:12:11 +02:00
|
|
|
}
|
|
|
|
|
2020-09-21 11:41:20 +02:00
|
|
|
/* If O_NONBLOCK was set, then return EGAIN */
|
2008-07-26 15:12:11 +02:00
|
|
|
|
2020-09-21 11:41:20 +02:00
|
|
|
if (filep->f_oflags & O_NONBLOCK)
|
2008-07-26 15:12:11 +02:00
|
|
|
{
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&dev->d_bfsem);
|
2020-09-21 11:41:20 +02:00
|
|
|
return -EAGAIN;
|
2008-07-26 15:12:11 +02:00
|
|
|
}
|
|
|
|
|
2008-07-26 16:02:46 +02:00
|
|
|
/* Otherwise, wait for something to be written to the pipe */
|
2008-07-26 15:12:11 +02:00
|
|
|
|
|
|
|
sched_lock();
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&dev->d_bfsem);
|
2017-10-04 23:22:27 +02:00
|
|
|
ret = nxsem_wait(&dev->d_rdsem);
|
2008-07-26 15:12:11 +02:00
|
|
|
sched_unlock();
|
2008-11-17 21:24:28 +01:00
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
if (ret < 0 || (ret = nxsem_wait(&dev->d_bfsem)) < 0)
|
2008-07-26 15:12:11 +02:00
|
|
|
{
|
2020-03-31 21:59:25 +02:00
|
|
|
/* May fail because a signal was received or if the task was
|
|
|
|
* canceled.
|
|
|
|
*/
|
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
return ret;
|
2008-07-26 15:12:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-17 16:47:40 +02:00
|
|
|
/* Then return whatever is available in the pipe (which is at least one
|
|
|
|
* byte).
|
|
|
|
*/
|
2008-07-26 15:12:11 +02:00
|
|
|
|
|
|
|
nread = 0;
|
2016-05-26 18:01:15 +02:00
|
|
|
while ((size_t)nread < len && dev->d_wrndx != dev->d_rdndx)
|
2008-07-26 15:12:11 +02:00
|
|
|
{
|
2008-07-27 16:58:36 +02:00
|
|
|
*buffer++ = dev->d_buffer[dev->d_rdndx];
|
2016-07-19 21:34:18 +02:00
|
|
|
if (++dev->d_rdndx >= dev->d_bufsize)
|
2008-07-26 15:12:11 +02:00
|
|
|
{
|
2014-04-13 22:32:20 +02:00
|
|
|
dev->d_rdndx = 0;
|
2008-07-26 15:12:11 +02:00
|
|
|
}
|
2016-07-18 16:00:56 +02:00
|
|
|
|
2008-07-26 15:12:11 +02:00
|
|
|
nread++;
|
|
|
|
}
|
|
|
|
|
2020-09-26 14:25:03 +02:00
|
|
|
/* Notify all poll/select waiters that they can write to the FIFO */
|
|
|
|
|
|
|
|
pipecommon_pollnotify(dev, POLLOUT);
|
|
|
|
|
2020-05-17 16:47:40 +02:00
|
|
|
/* Notify all waiting writers that bytes have been removed from the
|
|
|
|
* buffer.
|
|
|
|
*/
|
2008-07-26 15:12:11 +02:00
|
|
|
|
2020-05-17 15:56:21 +02:00
|
|
|
while (nxsem_get_value(&dev->d_wrsem, &sval) == 0 && sval < 0)
|
2008-07-26 15:12:11 +02:00
|
|
|
{
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&dev->d_wrsem);
|
2008-07-26 15:12:11 +02:00
|
|
|
}
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&dev->d_bfsem);
|
2009-09-15 22:55:06 +02:00
|
|
|
pipe_dumpbuffer("From PIPE:", start, nread);
|
2008-11-17 21:24:28 +01:00
|
|
|
return nread;
|
2008-07-26 15:12:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pipecommon_write
|
|
|
|
****************************************************************************/
|
2008-08-10 18:57:15 +02:00
|
|
|
|
2016-05-26 18:01:15 +02:00
|
|
|
ssize_t pipecommon_write(FAR struct file *filep, FAR const char *buffer,
|
|
|
|
size_t len)
|
2008-07-26 15:12:11 +02:00
|
|
|
{
|
2016-05-26 18:01:15 +02:00
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct pipe_dev_s *dev = inode->i_private;
|
|
|
|
ssize_t nwritten = 0;
|
|
|
|
ssize_t last;
|
|
|
|
int nxtwrndx;
|
|
|
|
int sval;
|
2017-10-04 23:22:27 +02:00
|
|
|
int ret;
|
2008-07-26 15:12:11 +02:00
|
|
|
|
2015-01-30 18:14:24 +01:00
|
|
|
DEBUGASSERT(dev);
|
2015-10-10 18:41:00 +02:00
|
|
|
pipe_dumpbuffer("To PIPE:", (FAR uint8_t *)buffer, len);
|
2008-07-26 15:12:11 +02:00
|
|
|
|
2018-08-26 15:17:31 +02:00
|
|
|
/* Handle zero-length writes */
|
|
|
|
|
2015-03-13 14:21:06 +01:00
|
|
|
if (len == 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-08-26 15:17:31 +02:00
|
|
|
/* REVISIT: "If all file descriptors referring to the read end of a pipe
|
|
|
|
* have been closed, then a write will cause a SIGPIPE signal to be
|
|
|
|
* generated for the calling process. If the calling process is ignoring
|
|
|
|
* this signal, then write(2) fails with the error EPIPE."
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (dev->d_nreaders <= 0)
|
|
|
|
{
|
|
|
|
return -EPIPE;
|
|
|
|
}
|
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
/* At present, this method cannot be called from interrupt handlers. That
|
2020-03-31 21:59:25 +02:00
|
|
|
* is because it calls nxsem_wait() and nxsem_wait() cannot be called from
|
|
|
|
* interrupt level. This actually happens fairly commonly IF [a-z]err()
|
|
|
|
* is called from interrupt handlers and stdout is being redirected via a
|
|
|
|
* pipe. In that case, the debug output will try to go out the pipe
|
|
|
|
* (interrupt handlers should use the _err() APIs).
|
2009-09-14 00:32:01 +02:00
|
|
|
*
|
|
|
|
* On the other hand, it would be very valuable to be able to feed the pipe
|
|
|
|
* from an interrupt handler! TODO: Consider disabling interrupts instead
|
2020-03-31 21:59:25 +02:00
|
|
|
* of taking semaphores so that pipes can be written from interrupt
|
|
|
|
* handlers.
|
2009-09-14 00:32:01 +02:00
|
|
|
*/
|
|
|
|
|
2015-04-23 15:29:15 +02:00
|
|
|
DEBUGASSERT(up_interrupt_context() == false);
|
2009-09-14 00:32:01 +02:00
|
|
|
|
2008-07-26 15:12:11 +02:00
|
|
|
/* Make sure that we have exclusive access to the device structure */
|
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
ret = nxsem_wait(&dev->d_bfsem);
|
|
|
|
if (ret < 0)
|
2008-07-26 15:12:11 +02:00
|
|
|
{
|
2020-03-31 21:59:25 +02:00
|
|
|
/* May fail because a signal was received or if the task was
|
|
|
|
* canceled.
|
|
|
|
*/
|
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
return ret;
|
2008-07-26 15:12:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Loop until all of the bytes have been written */
|
|
|
|
|
|
|
|
last = 0;
|
2015-10-10 18:41:00 +02:00
|
|
|
for (; ; )
|
2008-07-26 15:12:11 +02:00
|
|
|
{
|
|
|
|
/* Calculate the write index AFTER the next byte is written */
|
|
|
|
|
2008-07-27 16:58:36 +02:00
|
|
|
nxtwrndx = dev->d_wrndx + 1;
|
2016-07-19 21:34:18 +02:00
|
|
|
if (nxtwrndx >= dev->d_bufsize)
|
2008-07-26 15:12:11 +02:00
|
|
|
{
|
|
|
|
nxtwrndx = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Would the next write overflow the circular buffer? */
|
|
|
|
|
2008-07-27 16:58:36 +02:00
|
|
|
if (nxtwrndx != dev->d_rdndx)
|
2008-07-26 15:12:11 +02:00
|
|
|
{
|
|
|
|
/* No... copy the byte */
|
|
|
|
|
2008-07-27 16:58:36 +02:00
|
|
|
dev->d_buffer[dev->d_wrndx] = *buffer++;
|
|
|
|
dev->d_wrndx = nxtwrndx;
|
2008-07-26 15:12:11 +02:00
|
|
|
|
|
|
|
/* Is the write complete? */
|
|
|
|
|
2016-05-26 18:01:15 +02:00
|
|
|
nwritten++;
|
|
|
|
if ((size_t)nwritten >= len)
|
2008-07-26 15:12:11 +02:00
|
|
|
{
|
2020-09-26 14:25:03 +02:00
|
|
|
/* Notify all poll/select waiters that they can read from the
|
|
|
|
* FIFO.
|
|
|
|
*/
|
|
|
|
|
|
|
|
pipecommon_pollnotify(dev, POLLIN);
|
|
|
|
|
2020-05-17 16:47:40 +02:00
|
|
|
/* Yes.. Notify all of the waiting readers that more data is
|
|
|
|
* available.
|
|
|
|
*/
|
2008-07-26 15:12:11 +02:00
|
|
|
|
2020-05-17 15:56:21 +02:00
|
|
|
while (nxsem_get_value(&dev->d_rdsem, &sval) == 0 && sval < 0)
|
2008-07-26 15:12:11 +02:00
|
|
|
{
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&dev->d_rdsem);
|
2008-07-26 15:12:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the number of bytes written */
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&dev->d_bfsem);
|
2008-07-26 15:12:11 +02:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-17 16:47:40 +02:00
|
|
|
/* There is not enough room for the next byte. Was anything
|
|
|
|
* written in this pass?
|
|
|
|
*/
|
2008-07-26 15:12:11 +02:00
|
|
|
|
|
|
|
if (last < nwritten)
|
|
|
|
{
|
2020-09-26 14:25:03 +02:00
|
|
|
/* Notify all poll/select waiters that they can read from the
|
|
|
|
* FIFO.
|
|
|
|
*/
|
|
|
|
|
|
|
|
pipecommon_pollnotify(dev, POLLIN);
|
|
|
|
|
2020-05-17 16:47:40 +02:00
|
|
|
/* Yes.. Notify all of the waiting readers that more data is
|
|
|
|
* available.
|
|
|
|
*/
|
2008-07-26 15:12:11 +02:00
|
|
|
|
2020-05-17 15:56:21 +02:00
|
|
|
while (nxsem_get_value(&dev->d_rdsem, &sval) == 0 && sval < 0)
|
2008-07-26 15:12:11 +02:00
|
|
|
{
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&dev->d_rdsem);
|
2008-07-26 15:12:11 +02:00
|
|
|
}
|
|
|
|
}
|
2016-07-26 18:09:35 +02:00
|
|
|
|
2008-07-26 15:12:11 +02:00
|
|
|
last = nwritten;
|
|
|
|
|
2020-05-17 16:47:40 +02:00
|
|
|
/* If O_NONBLOCK was set, then return partial bytes written or
|
|
|
|
* EGAIN.
|
|
|
|
*/
|
2008-07-26 15:12:11 +02:00
|
|
|
|
|
|
|
if (filep->f_oflags & O_NONBLOCK)
|
|
|
|
{
|
|
|
|
if (nwritten == 0)
|
|
|
|
{
|
|
|
|
nwritten = -EAGAIN;
|
|
|
|
}
|
2016-07-26 18:09:35 +02:00
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&dev->d_bfsem);
|
2008-07-26 15:12:11 +02:00
|
|
|
return nwritten;
|
|
|
|
}
|
|
|
|
|
2021-02-25 14:34:37 +01:00
|
|
|
/* There is more to be written.. wait for data to be removed from
|
2020-03-31 21:59:25 +02:00
|
|
|
* the pipe
|
|
|
|
*/
|
2008-07-26 15:12:11 +02:00
|
|
|
|
|
|
|
sched_lock();
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&dev->d_bfsem);
|
2020-03-31 21:59:25 +02:00
|
|
|
ret = nxsem_wait(&dev->d_wrsem);
|
2008-07-26 15:12:11 +02:00
|
|
|
sched_unlock();
|
2020-03-31 21:59:25 +02:00
|
|
|
|
2021-10-20 06:25:30 +02:00
|
|
|
if (dev->d_nreaders <= 0)
|
|
|
|
{
|
|
|
|
ret = -EPIPE;
|
|
|
|
}
|
|
|
|
|
2020-03-31 21:59:25 +02:00
|
|
|
if (ret < 0 || (ret = nxsem_wait(&dev->d_bfsem)) < 0)
|
|
|
|
{
|
|
|
|
/* Either call nxsem_wait may fail because a signal was
|
|
|
|
* received or if the task was canceled.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return nwritten == 0 ? (ssize_t)ret : nwritten;
|
|
|
|
}
|
2008-07-26 15:12:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-17 21:24:28 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: pipecommon_poll
|
|
|
|
****************************************************************************/
|
|
|
|
|
2008-11-19 19:43:50 +01:00
|
|
|
int pipecommon_poll(FAR struct file *filep, FAR struct pollfd *fds,
|
2009-12-15 15:25:14 +01:00
|
|
|
bool setup)
|
2008-11-17 21:24:28 +01:00
|
|
|
{
|
2008-11-18 00:23:45 +01:00
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct pipe_dev_s *dev = inode->i_private;
|
|
|
|
pollevent_t eventset;
|
|
|
|
pipe_ndx_t nbytes;
|
2020-03-31 21:59:25 +02:00
|
|
|
int ret;
|
2008-11-18 00:23:45 +01:00
|
|
|
int i;
|
2008-11-17 21:24:28 +01:00
|
|
|
|
2015-01-30 18:14:24 +01:00
|
|
|
DEBUGASSERT(dev && fds);
|
2008-11-17 21:24:28 +01:00
|
|
|
|
2008-11-19 19:43:50 +01:00
|
|
|
/* Are we setting up the poll? Or tearing it down? */
|
2008-11-17 21:24:28 +01:00
|
|
|
|
2020-03-31 21:59:25 +02:00
|
|
|
ret = pipecommon_semtake(&dev->d_bfsem);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-11-19 19:43:50 +01:00
|
|
|
if (setup)
|
2008-11-17 21:24:28 +01:00
|
|
|
{
|
2008-11-19 19:43:50 +01:00
|
|
|
/* This is a request to set up the poll. Find an available
|
|
|
|
* slot for the poll structure reference
|
2008-11-17 21:24:28 +01:00
|
|
|
*/
|
|
|
|
|
2008-11-19 19:43:50 +01:00
|
|
|
for (i = 0; i < CONFIG_DEV_PIPE_NPOLLWAITERS; i++)
|
2008-11-17 21:24:28 +01:00
|
|
|
{
|
2008-11-19 19:43:50 +01:00
|
|
|
/* Find an available slot */
|
2008-11-17 21:24:28 +01:00
|
|
|
|
2008-11-19 19:43:50 +01:00
|
|
|
if (!dev->d_fds[i])
|
|
|
|
{
|
|
|
|
/* Bind the poll structure and this slot */
|
2008-11-17 21:24:28 +01:00
|
|
|
|
2008-11-19 19:43:50 +01:00
|
|
|
dev->d_fds[i] = fds;
|
2009-06-17 01:23:31 +02:00
|
|
|
fds->priv = &dev->d_fds[i];
|
2008-11-19 19:43:50 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-11-17 21:24:28 +01:00
|
|
|
|
2008-11-19 19:43:50 +01:00
|
|
|
if (i >= CONFIG_DEV_PIPE_NPOLLWAITERS)
|
|
|
|
{
|
2009-06-17 01:23:31 +02:00
|
|
|
fds->priv = NULL;
|
2008-11-19 19:43:50 +01:00
|
|
|
ret = -EBUSY;
|
|
|
|
goto errout;
|
|
|
|
}
|
2008-11-17 21:24:28 +01:00
|
|
|
|
2008-11-19 19:43:50 +01:00
|
|
|
/* Should immediately notify on any of the requested events?
|
|
|
|
* First, determine how many bytes are in the buffer
|
|
|
|
*/
|
2008-11-17 21:24:28 +01:00
|
|
|
|
|
|
|
if (dev->d_wrndx >= dev->d_rdndx)
|
|
|
|
{
|
|
|
|
nbytes = dev->d_wrndx - dev->d_rdndx;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
From c73dd8973accd312ca7675fde044df80e9cc62d5 Mon Sep 17 00:00:00 2001
From: Jussi Kivilinna <jussi.kivilinna@haltian.com>
Date: Thu, 7 Dec 2017 13:00:14 +0200
Subject: [PATCH] drivers/pipes: poll: fix off-by-one error in calculation of bytes in the buffer
Buffer calculation in pipe poll setup is off-by-one when read indexis larger than write index. This causes poll() not getting POLLINwhen buffer has one byte as calculation gives zero bytes in buffer.
Reproducible with:
{
char buf[8] = { 0, };
int fds[2];
struct pollfd in_pfd;
pipe2(fds, 8);
write(fds[1], buf, 7);
read(fds[0], buf, 7);
write(fds[1], buf, 1);
in_pfd.fd = fds[0];
in_pfd.events = POLLIN;
ret = poll(&in_pfd, 1, -1); // pipe bug => stuck waiting
assert(ret == 1);
}
2017-12-07 14:12:11 +01:00
|
|
|
nbytes = dev->d_bufsize + dev->d_wrndx - dev->d_rdndx;
|
2008-11-17 21:24:28 +01:00
|
|
|
}
|
|
|
|
|
2015-05-12 15:41:12 +02:00
|
|
|
/* Notify the POLLOUT event if the pipe is not full, but only if
|
2018-08-26 15:27:19 +02:00
|
|
|
* there is readers.
|
|
|
|
*/
|
2008-11-17 21:24:28 +01:00
|
|
|
|
|
|
|
eventset = 0;
|
2018-08-26 15:27:19 +02:00
|
|
|
if ((filep->f_oflags & O_WROK) && (nbytes < (dev->d_bufsize - 1)))
|
2008-11-17 21:24:28 +01:00
|
|
|
{
|
|
|
|
eventset |= POLLOUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Notify the POLLIN event if the pipe is not empty */
|
|
|
|
|
2018-08-26 15:27:19 +02:00
|
|
|
if ((filep->f_oflags & O_RDOK) && (nbytes > 0))
|
2008-11-17 21:24:28 +01:00
|
|
|
{
|
|
|
|
eventset |= POLLIN;
|
|
|
|
}
|
|
|
|
|
2015-05-12 15:41:12 +02:00
|
|
|
/* Notify the POLLHUP event if the pipe is empty and no writers */
|
|
|
|
|
|
|
|
if (nbytes == 0 && dev->d_nwriters <= 0)
|
|
|
|
{
|
|
|
|
eventset |= POLLHUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Change POLLOUT to POLLERR, if no readers and policy 0. */
|
|
|
|
|
2021-01-01 22:18:00 +01:00
|
|
|
if ((eventset & POLLOUT) &&
|
2015-05-12 15:41:12 +02:00
|
|
|
PIPE_IS_POLICY_0(dev->d_flags) &&
|
|
|
|
dev->d_nreaders <= 0)
|
|
|
|
{
|
|
|
|
eventset |= POLLERR;
|
|
|
|
}
|
|
|
|
|
2008-11-17 21:24:28 +01:00
|
|
|
if (eventset)
|
|
|
|
{
|
|
|
|
pipecommon_pollnotify(dev, eventset);
|
|
|
|
}
|
|
|
|
}
|
2008-11-19 19:43:50 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* This is a request to tear down the poll. */
|
|
|
|
|
2016-05-26 18:01:15 +02:00
|
|
|
FAR struct pollfd **slot = (FAR struct pollfd **)fds->priv;
|
2008-11-19 19:43:50 +01:00
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2008-11-19 19:43:50 +01:00
|
|
|
if (!slot)
|
|
|
|
{
|
|
|
|
ret = -EIO;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Remove all memory of the poll setup */
|
|
|
|
|
|
|
|
*slot = NULL;
|
2009-06-17 01:23:31 +02:00
|
|
|
fds->priv = NULL;
|
2008-11-19 19:43:50 +01:00
|
|
|
}
|
2008-11-17 21:24:28 +01:00
|
|
|
|
2008-11-19 19:43:50 +01:00
|
|
|
errout:
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&dev->d_bfsem);
|
2008-11-19 19:43:50 +01:00
|
|
|
return ret;
|
2008-11-17 21:24:28 +01:00
|
|
|
}
|
|
|
|
|
2016-03-21 01:44:44 +01:00
|
|
|
/****************************************************************************
|
2015-01-30 18:14:24 +01:00
|
|
|
* Name: pipecommon_ioctl
|
2016-03-21 01:44:44 +01:00
|
|
|
****************************************************************************/
|
2015-01-30 18:14:24 +01:00
|
|
|
|
2016-03-21 01:11:13 +01:00
|
|
|
int pipecommon_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
|
2015-01-30 18:14:24 +01:00
|
|
|
{
|
2016-05-26 18:01:15 +02:00
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct pipe_dev_s *dev = inode->i_private;
|
|
|
|
int ret = -EINVAL;
|
2015-01-30 18:14:24 +01:00
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2016-03-21 01:11:13 +01:00
|
|
|
/* Some sanity checking */
|
2015-01-30 18:14:24 +01:00
|
|
|
|
2016-03-21 01:11:13 +01:00
|
|
|
if (dev == NULL)
|
2015-01-30 18:14:24 +01:00
|
|
|
{
|
2019-12-07 06:36:33 +01:00
|
|
|
return -EBADF;
|
2016-03-21 01:11:13 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-03-31 21:59:25 +02:00
|
|
|
ret = pipecommon_semtake(&dev->d_bfsem);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2016-03-21 01:11:13 +01:00
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case PIPEIOC_POLICY:
|
2015-01-31 19:05:01 +01:00
|
|
|
{
|
2016-03-21 01:11:13 +01:00
|
|
|
if (arg != 0)
|
|
|
|
{
|
|
|
|
PIPE_POLICY_1(dev->d_flags);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PIPE_POLICY_0(dev->d_flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = OK;
|
2015-01-31 19:05:01 +01:00
|
|
|
}
|
2016-03-21 01:11:13 +01:00
|
|
|
break;
|
|
|
|
|
2016-07-25 22:06:32 +02:00
|
|
|
case FIONWRITE: /* Number of bytes waiting in send queue */
|
|
|
|
case FIONREAD: /* Number of bytes available for reading */
|
2016-03-21 01:11:13 +01:00
|
|
|
{
|
|
|
|
int count;
|
|
|
|
|
2016-07-25 22:06:32 +02:00
|
|
|
/* Determine the number of bytes written to the buffer. This is,
|
|
|
|
* of course, also the number of bytes that may be read from the
|
|
|
|
* buffer.
|
|
|
|
*
|
|
|
|
* d_rdndx - index to remove next byte from the buffer
|
|
|
|
* d_wrndx - Index to next location to add a byte to the buffer.
|
|
|
|
*/
|
2016-03-21 01:11:13 +01:00
|
|
|
|
|
|
|
if (dev->d_wrndx < dev->d_rdndx)
|
|
|
|
{
|
2016-07-19 21:34:18 +02:00
|
|
|
count = (dev->d_bufsize - dev->d_rdndx) + dev->d_wrndx;
|
2016-03-21 01:11:13 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
count = dev->d_wrndx - dev->d_rdndx;
|
|
|
|
}
|
|
|
|
|
2016-06-13 22:01:32 +02:00
|
|
|
*(FAR int *)((uintptr_t)arg) = count;
|
2016-03-21 01:11:13 +01:00
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2016-07-25 22:06:32 +02:00
|
|
|
/* Free space in buffer */
|
|
|
|
|
|
|
|
case FIONSPACE:
|
2015-01-31 19:05:01 +01:00
|
|
|
{
|
2016-03-21 01:11:13 +01:00
|
|
|
int count;
|
|
|
|
|
2016-07-25 22:06:32 +02:00
|
|
|
/* Determine the number of bytes free in the buffer.
|
|
|
|
*
|
|
|
|
* d_rdndx - index to remove next byte from the buffer
|
|
|
|
* d_wrndx - Index to next location to add a byte to the buffer.
|
|
|
|
*/
|
2016-03-21 01:11:13 +01:00
|
|
|
|
|
|
|
if (dev->d_wrndx < dev->d_rdndx)
|
|
|
|
{
|
|
|
|
count = (dev->d_rdndx - dev->d_wrndx) - 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-07-19 21:34:18 +02:00
|
|
|
count = ((dev->d_bufsize - dev->d_wrndx) + dev->d_rdndx) - 1;
|
2016-03-21 01:11:13 +01:00
|
|
|
}
|
|
|
|
|
2016-06-13 22:01:32 +02:00
|
|
|
*(FAR int *)((uintptr_t)arg) = count;
|
2016-03-21 01:11:13 +01:00
|
|
|
ret = 0;
|
2015-01-31 19:05:01 +01:00
|
|
|
}
|
2016-03-21 01:11:13 +01:00
|
|
|
break;
|
2015-01-31 19:05:01 +01:00
|
|
|
|
2016-03-21 01:11:13 +01:00
|
|
|
default:
|
2020-03-31 21:59:25 +02:00
|
|
|
ret = -ENOTTY;
|
2016-03-21 01:11:13 +01:00
|
|
|
break;
|
2015-01-30 18:14:24 +01:00
|
|
|
}
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&dev->d_bfsem);
|
2016-03-21 01:11:13 +01:00
|
|
|
return ret;
|
2015-01-30 18:14:24 +01:00
|
|
|
}
|
2015-01-31 19:05:01 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pipecommon_unlink
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-02-18 16:34:58 +01:00
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
2015-01-31 20:19:23 +01:00
|
|
|
int pipecommon_unlink(FAR struct inode *inode)
|
2015-01-31 19:05:01 +01:00
|
|
|
{
|
2015-01-31 20:19:23 +01:00
|
|
|
FAR struct pipe_dev_s *dev;
|
2015-01-31 19:05:01 +01:00
|
|
|
|
2015-01-31 20:19:23 +01:00
|
|
|
DEBUGASSERT(inode && inode->i_private);
|
2015-01-31 20:24:59 +01:00
|
|
|
dev = (FAR struct pipe_dev_s *)inode->i_private;
|
2015-01-31 19:05:01 +01:00
|
|
|
|
|
|
|
/* Mark the pipe unlinked */
|
|
|
|
|
|
|
|
PIPE_UNLINK(dev->d_flags);
|
|
|
|
|
|
|
|
/* Are the any open references to the driver? */
|
|
|
|
|
2019-08-06 15:31:57 +02:00
|
|
|
if (inode->i_crefs == 1)
|
2015-01-31 19:05:01 +01:00
|
|
|
{
|
|
|
|
/* No.. free the buffer (if there is one) */
|
|
|
|
|
|
|
|
if (dev->d_buffer)
|
|
|
|
{
|
|
|
|
kmm_free(dev->d_buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* And free the device structure. */
|
|
|
|
|
|
|
|
pipecommon_freedev(dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
2015-02-18 16:34:58 +01:00
|
|
|
#endif
|
2015-01-31 19:05:01 +01:00
|
|
|
|
2016-07-19 21:34:18 +02:00
|
|
|
#endif /* CONFIG_PIPES */
|