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>
|
2024-08-23 09:03:57 +02:00
|
|
|
#include <sys/param.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-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
|
|
|
|
|
2023-01-06 13:51:14 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2023-08-08 06:48:40 +02:00
|
|
|
* Name: pipecommon_wakeup
|
2023-01-06 13:51:14 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2023-08-08 06:48:40 +02:00
|
|
|
static void pipecommon_wakeup(FAR sem_t *sem)
|
2023-01-06 13:51:14 +01:00
|
|
|
{
|
2023-08-08 06:48:40 +02:00
|
|
|
int sval;
|
|
|
|
|
|
|
|
while (nxsem_get_value(sem, &sval) == OK && sval <= 0)
|
2023-01-06 13:51:14 +01:00
|
|
|
{
|
2023-08-08 06:48:40 +02:00
|
|
|
nxsem_post(sem);
|
2023-01-06 13:51:14 +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
|
|
|
|
2023-11-22 10:59:30 +01:00
|
|
|
dev = kmm_zalloc(sizeof(struct pipe_dev_s));
|
2008-07-26 15:12:11 +02:00
|
|
|
if (dev)
|
|
|
|
{
|
|
|
|
/* Initialize the private structure */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_init(&dev->d_bflock);
|
2017-10-03 20:51:15 +02:00
|
|
|
nxsem_init(&dev->d_rdsem, 0, 0);
|
|
|
|
nxsem_init(&dev->d_wrsem, 0, 0);
|
2023-08-08 06:48:40 +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
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_destroy(&dev->d_bflock);
|
2017-10-03 23:35:24 +02:00
|
|
|
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 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
|
2022-09-06 08:18:45 +02:00
|
|
|
* nxmutex_lock() call should fail if we are awakened by a signal or if the
|
2020-03-31 21:59:25 +02:00
|
|
|
* thread was canceled.
|
2011-03-02 01:33:42 +01:00
|
|
|
*/
|
2008-07-26 15:12:11 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&dev->d_bflock);
|
2017-10-04 23:22:27 +02:00
|
|
|
if (ret < 0)
|
2008-07-26 15:12:11 +02:00
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
ferr("ERROR: nxmutex_lock failed: %d\n", ret);
|
2017-10-04 23:22:27 +02:00
|
|
|
return ret;
|
2011-03-02 01:33:42 +01:00
|
|
|
}
|
|
|
|
|
2024-03-06 09:39:48 +01:00
|
|
|
/* If d_buffer is not initialized, init it. */
|
2008-07-27 16:58:36 +02:00
|
|
|
|
2024-03-06 09:39:48 +01:00
|
|
|
if (!circbuf_is_init(&dev->d_buffer))
|
2011-03-02 01:33:42 +01:00
|
|
|
{
|
2023-08-08 06:48:40 +02:00
|
|
|
ret = circbuf_init(&dev->d_buffer, NULL, dev->d_bufsize);
|
|
|
|
if (ret < 0)
|
2008-07-27 16:58:36 +02:00
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&dev->d_bflock);
|
2023-08-08 06:48:40 +02:00
|
|
|
return ret;
|
2008-07-27 16:58:36 +02:00
|
|
|
}
|
2011-03-02 01:33:42 +01:00
|
|
|
}
|
2008-07-27 16:58:36 +02:00
|
|
|
|
2023-11-22 10:59:30 +01:00
|
|
|
dev->d_crefs++;
|
|
|
|
|
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
|
|
|
|
2023-03-05 20:45:51 +01:00
|
|
|
/* If this is the first writer, then the n-readers 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)
|
|
|
|
{
|
2023-08-08 06:48:40 +02:00
|
|
|
pipecommon_wakeup(&dev->d_rdsem);
|
2008-07-26 15:12:11 +02:00
|
|
|
}
|
2011-03-02 01:33:42 +01:00
|
|
|
}
|
|
|
|
|
2023-03-05 20:45:51 +01:00
|
|
|
while ((filep->f_oflags & O_NONBLOCK) == 0 && /* Blocking */
|
|
|
|
(filep->f_oflags & O_RDWR) == O_WRONLY && /* Write-only */
|
|
|
|
dev->d_nreaders < 1 && /* No readers on the pipe */
|
2023-08-08 06:48:40 +02:00
|
|
|
circbuf_is_empty(&dev->d_buffer)) /* Buffer is empty */
|
2023-03-05 20:45:51 +01:00
|
|
|
{
|
|
|
|
/* If opened for write-only, then wait for at least one reader
|
|
|
|
* on the pipe.
|
|
|
|
*/
|
|
|
|
|
|
|
|
nxmutex_unlock(&dev->d_bflock);
|
|
|
|
|
|
|
|
/* NOTE: d_wrsem is normally used to check if the write buffer is full
|
|
|
|
* and wait for it being read and being able to receive more data. But,
|
|
|
|
* until the first reader has opened the pipe, the meaning is different
|
|
|
|
* and it is used prevent O_WRONLY open calls from returning until
|
|
|
|
* there is at least one reader on the pipe.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = nxsem_wait(&dev->d_wrsem);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
ferr("ERROR: nxsem_wait failed: %d\n", ret);
|
|
|
|
|
|
|
|
/* Immediately close the pipe that we just opened */
|
|
|
|
|
|
|
|
pipecommon_close(filep);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The nxmutex_lock() call should fail if we are awakened by a
|
|
|
|
* signal or if the task is canceled.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = nxmutex_lock(&dev->d_bflock);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
ferr("ERROR: nxmutex_lock failed: %d\n", ret);
|
|
|
|
|
|
|
|
/* Immediately close the pipe that we just opened */
|
|
|
|
|
|
|
|
pipecommon_close(filep);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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++;
|
2023-03-05 20:45:51 +01:00
|
|
|
|
|
|
|
/* If this is the first reader, then the n-writers semaphore
|
|
|
|
* indicates the number of writers waiting for the first reader.
|
|
|
|
* Wake them all up.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (dev->d_nreaders == 1)
|
|
|
|
{
|
2023-08-08 06:48:40 +02:00
|
|
|
pipecommon_wakeup(&dev->d_wrsem);
|
2023-03-05 20:45:51 +01:00
|
|
|
}
|
2015-05-12 15:41:12 +02:00
|
|
|
}
|
|
|
|
|
2023-03-05 20:45:51 +01:00
|
|
|
while ((filep->f_oflags & O_NONBLOCK) == 0 && /* Blocking */
|
|
|
|
(filep->f_oflags & O_RDWR) == O_RDONLY && /* Read-only */
|
|
|
|
dev->d_nwriters < 1 && /* No writers on the pipe */
|
2023-08-08 06:48:40 +02:00
|
|
|
circbuf_is_empty(&dev->d_buffer)) /* Buffer is empty */
|
2022-02-08 19:39:15 +01:00
|
|
|
{
|
2023-03-05 20:45:51 +01:00
|
|
|
/* If opened for read-only, then wait for either at least one writer
|
|
|
|
* on the pipe.
|
2022-02-08 19:39:15 +01:00
|
|
|
*/
|
2008-07-26 15:12:11 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&dev->d_bflock);
|
2015-01-30 18:14:24 +01:00
|
|
|
|
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);
|
2023-03-05 20:45:51 +01:00
|
|
|
if (ret < 0)
|
2008-07-26 16:24:17 +02:00
|
|
|
{
|
2023-03-05 20:45:51 +01:00
|
|
|
ferr("ERROR: nxsem_wait failed: %d\n", ret);
|
|
|
|
|
|
|
|
/* Immediately close the pipe that we just opened */
|
2008-07-26 16:24:17 +02:00
|
|
|
|
2023-03-05 20:45:51 +01:00
|
|
|
pipecommon_close(filep);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The nxmutex_lock() call should fail if we are awakened by a
|
|
|
|
* signal or if the task is canceled.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = nxmutex_lock(&dev->d_bflock);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
ferr("ERROR: nxmutex_lock 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);
|
2022-02-08 19:39:15 +01:00
|
|
|
return ret;
|
2008-07-26 16:24:17 +02:00
|
|
|
}
|
2011-03-02 01:33:42 +01:00
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&dev->d_bflock);
|
2011-03-02 01:33:42 +01:00
|
|
|
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;
|
2020-03-31 21:59:25 +02:00
|
|
|
int ret;
|
2008-07-26 15:12:11 +02:00
|
|
|
|
2023-11-22 10:59:30 +01:00
|
|
|
DEBUGASSERT(dev && dev->d_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.
|
|
|
|
*/
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&dev->d_bflock);
|
2020-03-31 21:59:25 +02:00
|
|
|
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
|
|
|
|
2023-11-22 10:59:30 +01:00
|
|
|
dev->d_crefs--;
|
|
|
|
if (dev->d_crefs > 0)
|
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. */
|
|
|
|
|
2022-09-19 05:08:57 +02:00
|
|
|
poll_notify(dev->d_fds, CONFIG_DEV_PIPE_NPOLLWAITERS, POLLHUP);
|
2020-09-26 14:25:03 +02:00
|
|
|
|
2023-08-08 06:48:40 +02:00
|
|
|
pipecommon_wakeup(&dev->d_rdsem);
|
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. */
|
|
|
|
|
2022-09-19 05:08:57 +02:00
|
|
|
poll_notify(dev->d_fds, CONFIG_DEV_PIPE_NPOLLWAITERS,
|
|
|
|
POLLERR);
|
2023-08-08 06:48:40 +02:00
|
|
|
pipecommon_wakeup(&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
|
|
|
*/
|
|
|
|
|
2023-08-08 06:48:40 +02:00
|
|
|
else if (PIPE_IS_POLICY_0(dev->d_flags) ||
|
|
|
|
circbuf_is_empty(&dev->d_buffer))
|
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
|
|
|
|
2023-08-08 06:48:40 +02:00
|
|
|
circbuf_uninit(&dev->d_buffer);
|
2008-07-27 16:58:36 +02:00
|
|
|
|
|
|
|
/* And reset all counts and indices */
|
2014-04-13 22:32:20 +02:00
|
|
|
|
2008-07-27 16:58:36 +02:00
|
|
|
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
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&dev->d_bflock);
|
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)
|
|
|
|
{
|
2022-02-08 19:09:59 +01:00
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct pipe_dev_s *dev = inode->i_private;
|
|
|
|
ssize_t nread = 0;
|
2016-05-26 18:01:15 +02:00
|
|
|
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 */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&dev->d_bflock);
|
2017-10-04 23:22:27 +02:00
|
|
|
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
|
|
|
|
2023-08-08 06:48:40 +02:00
|
|
|
while (circbuf_is_empty(&dev->d_buffer))
|
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
|
|
|
|
2024-02-29 04:36:53 +01:00
|
|
|
if (dev->d_nwriters <= 0 && PIPE_IS_POLICY_0(dev->d_flags))
|
2008-07-26 15:12:11 +02:00
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&dev->d_bflock);
|
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
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&dev->d_bflock);
|
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
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&dev->d_bflock);
|
2017-10-04 23:22:27 +02:00
|
|
|
ret = nxsem_wait(&dev->d_rdsem);
|
2008-11-17 21:24:28 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
if (ret < 0 || (ret = nxmutex_lock(&dev->d_bflock)) < 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
|
|
|
|
2023-08-08 06:48:40 +02:00
|
|
|
nread = circbuf_read(&dev->d_buffer, buffer, len);
|
2008-07-26 15:12:11 +02:00
|
|
|
|
2023-01-06 13:51:14 +01:00
|
|
|
/* Notify all poll/select waiters that they can write to the
|
|
|
|
* FIFO when buffer can accept more than d_polloutthrd bytes.
|
|
|
|
*/
|
2020-09-26 14:25:03 +02:00
|
|
|
|
2023-08-08 06:48:40 +02:00
|
|
|
if (circbuf_used(&dev->d_buffer) <= (dev->d_bufsize - dev->d_polloutthrd))
|
2023-01-06 13:51:14 +01:00
|
|
|
{
|
|
|
|
poll_notify(dev->d_fds, CONFIG_DEV_PIPE_NPOLLWAITERS, POLLOUT);
|
|
|
|
}
|
2020-09-26 14:25:03 +02:00
|
|
|
|
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
|
|
|
|
2023-08-08 06:48:40 +02:00
|
|
|
pipecommon_wakeup(&dev->d_wrsem);
|
2008-07-26 15:12:11 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&dev->d_bflock);
|
2023-08-08 06:48:40 +02:00
|
|
|
pipe_dumpbuffer("From PIPE:", buffer, 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;
|
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;
|
|
|
|
}
|
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
/* At present, this method cannot be called from interrupt handlers. That
|
2022-09-06 08:18:45 +02:00
|
|
|
* is because it calls nxmutex_lock() and nxmutex_lock() cannot be called
|
|
|
|
* form 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 */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&dev->d_bflock);
|
2017-10-04 23:22:27 +02:00
|
|
|
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
|
|
|
{
|
2022-02-08 19:39:15 +01: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."
|
|
|
|
*/
|
|
|
|
|
2024-02-29 04:36:53 +01:00
|
|
|
if (dev->d_nreaders <= 0 && PIPE_IS_POLICY_0(dev->d_flags))
|
2022-02-08 19:39:15 +01:00
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&dev->d_bflock);
|
2022-02-08 19:39:15 +01:00
|
|
|
return nwritten == 0 ? -EPIPE : nwritten;
|
|
|
|
}
|
|
|
|
|
2008-07-26 15:12:11 +02:00
|
|
|
/* Would the next write overflow the circular buffer? */
|
|
|
|
|
2023-08-08 06:48:40 +02:00
|
|
|
if (!circbuf_is_full(&dev->d_buffer))
|
2008-07-26 15:12:11 +02:00
|
|
|
{
|
2023-08-08 06:48:40 +02:00
|
|
|
/* Loop until all of the bytes have been written */
|
2008-07-26 15:12:11 +02:00
|
|
|
|
2023-08-08 06:48:40 +02:00
|
|
|
nwritten += circbuf_write(&dev->d_buffer,
|
|
|
|
buffer + nwritten, len - nwritten);
|
2008-07-26 15:12:11 +02:00
|
|
|
|
2023-08-08 06:48:40 +02:00
|
|
|
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
|
2023-01-06 13:51:14 +01:00
|
|
|
* FIFO when buffer used exceeds poll threshold.
|
2020-09-26 14:25:03 +02:00
|
|
|
*/
|
|
|
|
|
2023-08-08 06:48:40 +02:00
|
|
|
if (circbuf_used(&dev->d_buffer) > dev->d_pollinthrd)
|
2023-01-06 13:51:14 +01:00
|
|
|
{
|
|
|
|
poll_notify(dev->d_fds, CONFIG_DEV_PIPE_NPOLLWAITERS,
|
|
|
|
POLLIN);
|
|
|
|
}
|
2020-09-26 14:25:03 +02:00
|
|
|
|
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
|
|
|
|
2023-08-08 06:48:40 +02:00
|
|
|
pipecommon_wakeup(&dev->d_rdsem);
|
2008-07-26 15:12:11 +02:00
|
|
|
|
|
|
|
/* Return the number of bytes written */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&dev->d_bflock);
|
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.
|
|
|
|
*/
|
|
|
|
|
2022-09-19 05:08:57 +02:00
|
|
|
poll_notify(dev->d_fds, CONFIG_DEV_PIPE_NPOLLWAITERS, POLLIN);
|
2020-09-26 14:25:03 +02:00
|
|
|
|
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
|
|
|
|
2023-08-08 06:48:40 +02:00
|
|
|
pipecommon_wakeup(&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
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&dev->d_bflock);
|
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
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&dev->d_bflock);
|
2020-03-31 21:59:25 +02:00
|
|
|
ret = nxsem_wait(&dev->d_wrsem);
|
2022-09-06 08:18:45 +02:00
|
|
|
if (ret < 0 || (ret = nxmutex_lock(&dev->d_bflock)) < 0)
|
2020-03-31 21:59:25 +02:00
|
|
|
{
|
|
|
|
/* 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
|
|
|
{
|
2022-02-08 19:09:59 +01:00
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct pipe_dev_s *dev = inode->i_private;
|
2008-11-18 00:23:45 +01:00
|
|
|
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
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&dev->d_bflock);
|
2020-03-31 21:59:25 +02:00
|
|
|
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)
|
|
|
|
{
|
2022-02-08 19:09:59 +01:00
|
|
|
fds->priv = NULL;
|
|
|
|
ret = -EBUSY;
|
2008-11-19 19:43:50 +01:00
|
|
|
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
|
|
|
|
2023-08-08 06:48:40 +02:00
|
|
|
nbytes = circbuf_used(&dev->d_buffer);
|
2008-11-17 21:24:28 +01:00
|
|
|
|
2023-01-06 13:51:14 +01:00
|
|
|
/* Notify the POLLOUT event if the pipe buffer can accept
|
|
|
|
* more than d_polloutthrd bytes, but only if
|
2018-08-26 15:27:19 +02:00
|
|
|
* there is readers.
|
|
|
|
*/
|
2008-11-17 21:24:28 +01:00
|
|
|
|
|
|
|
eventset = 0;
|
2023-01-06 13:51:14 +01:00
|
|
|
if ((filep->f_oflags & O_WROK) &&
|
2023-08-21 12:07:44 +02:00
|
|
|
nbytes < (dev->d_bufsize - dev->d_polloutthrd))
|
2008-11-17 21:24:28 +01:00
|
|
|
{
|
|
|
|
eventset |= POLLOUT;
|
|
|
|
}
|
|
|
|
|
2023-01-06 13:51:14 +01:00
|
|
|
/* Notify the POLLIN event if buffer used exceeds poll threshold */
|
2008-11-17 21:24:28 +01:00
|
|
|
|
2023-01-06 13:51:14 +01:00
|
|
|
if ((filep->f_oflags & O_RDOK) && (nbytes > dev->d_pollinthrd))
|
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;
|
|
|
|
}
|
|
|
|
|
2023-11-19 12:19:53 +01:00
|
|
|
poll_notify(&fds, 1, eventset);
|
2008-11-17 21:24:28 +01:00
|
|
|
}
|
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)
|
|
|
|
{
|
2022-02-08 19:09:59 +01:00
|
|
|
ret = -EIO;
|
2008-11-19 19:43:50 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Remove all memory of the poll setup */
|
|
|
|
|
2022-02-08 19:09:59 +01:00
|
|
|
*slot = NULL;
|
|
|
|
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:
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&dev->d_bflock);
|
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
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&dev->d_bflock);
|
2020-03-31 21:59:25 +02:00
|
|
|
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;
|
|
|
|
|
2023-01-06 13:51:14 +01:00
|
|
|
case PIPEIOC_POLLINTHRD:
|
|
|
|
{
|
|
|
|
pipe_ndx_t threshold = (pipe_ndx_t)arg;
|
|
|
|
if (threshold >= dev->d_bufsize)
|
|
|
|
{
|
|
|
|
ret = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
dev->d_pollinthrd = threshold;
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PIPEIOC_POLLOUTTHRD:
|
|
|
|
{
|
|
|
|
pipe_ndx_t threshold = (pipe_ndx_t)arg;
|
|
|
|
if (threshold >= dev->d_bufsize)
|
|
|
|
{
|
|
|
|
ret = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
dev->d_polloutthrd = threshold;
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2023-06-29 05:35:25 +02:00
|
|
|
case PIPEIOC_PEEK:
|
|
|
|
{
|
|
|
|
FAR struct pipe_peek_s *peek = (FAR struct pipe_peek_s *)arg;
|
|
|
|
|
|
|
|
DEBUGASSERT(peek && peek->buf);
|
|
|
|
|
2024-02-01 04:14:27 +01:00
|
|
|
ret = circbuf_peekat(&dev->d_buffer,
|
|
|
|
dev->d_buffer.tail + peek->offset,
|
|
|
|
peek->buf, peek->size);
|
2023-06-29 05:35:25 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2024-08-23 09:03:57 +02:00
|
|
|
case PIPEIOC_SETSIZE:
|
|
|
|
{
|
|
|
|
size_t size = (size_t)arg;
|
|
|
|
if (size == 0)
|
|
|
|
{
|
|
|
|
ret = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
size = MIN(size, CONFIG_DEV_PIPE_MAXSIZE);
|
|
|
|
ret = circbuf_resize(&dev->d_buffer, size);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
dev->d_bufsize = size;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PIPEIOC_GETSIZE:
|
|
|
|
{
|
|
|
|
ret = dev->d_bufsize;
|
|
|
|
}
|
|
|
|
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
|
|
|
{
|
2023-08-08 06:48:40 +02:00
|
|
|
*(FAR int *)((uintptr_t)arg) = circbuf_used(&dev->d_buffer);
|
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
|
|
|
{
|
2023-08-08 06:48:40 +02:00
|
|
|
*(FAR int *)((uintptr_t)arg) = circbuf_space(&dev->d_buffer);
|
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
|
|
|
|
2023-01-16 16:59:41 +01:00
|
|
|
case BIOC_FLUSH:
|
|
|
|
ret = -EINVAL;
|
|
|
|
break;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&dev->d_bflock);
|
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;
|
2023-11-22 10:59:30 +01:00
|
|
|
int ret;
|
2015-01-31 19:05:01 +01:00
|
|
|
|
2023-08-28 11:17:05 +02:00
|
|
|
DEBUGASSERT(inode->i_private);
|
2023-08-29 06:16:49 +02:00
|
|
|
dev = inode->i_private;
|
2015-01-31 19:05:01 +01:00
|
|
|
|
2023-11-22 10:59:30 +01:00
|
|
|
ret = nxmutex_lock(&dev->d_bflock);
|
|
|
|
if (ret < 0)
|
2015-01-31 19:05:01 +01:00
|
|
|
{
|
2023-11-22 10:59:30 +01:00
|
|
|
return ret;
|
|
|
|
}
|
2015-01-31 19:05:01 +01:00
|
|
|
|
2023-11-22 10:59:30 +01:00
|
|
|
if (dev->d_crefs <= 0)
|
|
|
|
{
|
2023-08-08 06:48:40 +02:00
|
|
|
circbuf_uninit(&dev->d_buffer);
|
2015-01-31 19:05:01 +01:00
|
|
|
pipecommon_freedev(dev);
|
2023-11-22 10:59:30 +01:00
|
|
|
return OK;
|
2015-01-31 19:05:01 +01:00
|
|
|
}
|
|
|
|
|
2023-11-22 10:59:30 +01:00
|
|
|
/* Mark the pipe unlinked */
|
|
|
|
|
|
|
|
PIPE_UNLINK(dev->d_flags);
|
|
|
|
nxmutex_unlock(&dev->d_bflock);
|
|
|
|
|
2015-01-31 19:05:01 +01:00
|
|
|
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 */
|