2008-07-26 15:12:11 +02:00
|
|
|
/****************************************************************************
|
2009-10-18 15:52:21 +02:00
|
|
|
* drivers/pipes/pipe.c
|
2008-07-26 15:12:11 +02:00
|
|
|
*
|
2021-03-04 07:10:42 +01: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
|
|
|
*
|
2021-03-04 07:10:42 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2008-07-26 15:12:11 +02:00
|
|
|
*
|
2021-03-04 07:10:42 +01: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>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
2016-07-20 19:57:18 +02:00
|
|
|
|
2008-07-26 15:12:11 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
2021-05-18 08:59:14 +02:00
|
|
|
#include <assert.h>
|
2008-07-26 15:12:11 +02:00
|
|
|
#include <errno.h>
|
2024-02-26 21:31:20 +01:00
|
|
|
#include <sys/ioctl.h>
|
2008-07-26 15:12:11 +02:00
|
|
|
|
2016-07-20 19:57:18 +02:00
|
|
|
#include <nuttx/fs/fs.h>
|
2022-09-06 08:18:45 +02:00
|
|
|
#include <nuttx/mutex.h>
|
2016-07-20 19:57:18 +02:00
|
|
|
|
2008-08-10 18:57:15 +02:00
|
|
|
#include "pipe_common.h"
|
2008-07-26 15:12:11 +02:00
|
|
|
|
2008-07-26 16:02:46 +02:00
|
|
|
#if CONFIG_DEV_PIPE_SIZE > 0
|
2008-07-26 15:12:11 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int pipe_close(FAR struct file *filep);
|
|
|
|
|
2023-07-18 14:32:59 +02:00
|
|
|
static int pipe_mmap(FAR struct file *filep,
|
|
|
|
FAR struct mm_map_entry_s *entry);
|
|
|
|
|
2008-07-26 15:12:11 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-02-05 17:03:15 +01:00
|
|
|
static const struct file_operations g_pipe_fops =
|
2008-07-26 15:12:11 +02:00
|
|
|
{
|
2022-01-10 15:51:17 +01:00
|
|
|
pipecommon_open, /* open */
|
|
|
|
pipe_close, /* close */
|
|
|
|
pipecommon_read, /* read */
|
|
|
|
pipecommon_write, /* write */
|
|
|
|
NULL, /* seek */
|
|
|
|
pipecommon_ioctl, /* ioctl */
|
2023-07-18 14:32:59 +02:00
|
|
|
pipe_mmap, /* mmap */
|
2023-01-02 18:06:12 +01:00
|
|
|
NULL, /* truncate */
|
2022-01-10 15:51:17 +01:00
|
|
|
pipecommon_poll /* poll */
|
2008-07-26 15:12:11 +02:00
|
|
|
};
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
static mutex_t g_pipelock = NXMUTEX_INITIALIZER;
|
|
|
|
static int g_pipeno;
|
2008-07-26 15:12:11 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pipe_allocate
|
|
|
|
****************************************************************************/
|
2009-09-13 18:15:46 +02:00
|
|
|
|
2008-07-26 15:12:11 +02:00
|
|
|
static inline int pipe_allocate(void)
|
|
|
|
{
|
2022-02-05 23:25:07 +01:00
|
|
|
int ret;
|
2008-07-27 16:58:36 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&g_pipelock);
|
2022-02-05 23:25:07 +01:00
|
|
|
if (ret < 0)
|
2008-07-26 15:12:11 +02:00
|
|
|
{
|
2022-02-05 23:25:07 +01:00
|
|
|
return ret;
|
2008-07-26 15:12:11 +02:00
|
|
|
}
|
2012-07-15 16:56:25 +02:00
|
|
|
|
2022-02-05 23:25:07 +01:00
|
|
|
ret = g_pipeno++;
|
|
|
|
if (g_pipeno < 0)
|
2008-07-26 15:12:11 +02:00
|
|
|
{
|
2022-02-05 23:25:07 +01:00
|
|
|
g_pipeno = 0;
|
2008-07-26 15:12:11 +02:00
|
|
|
}
|
2022-02-05 23:25:07 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&g_pipelock);
|
2022-02-05 23:25:07 +01:00
|
|
|
return ret;
|
2008-07-26 15:12:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pipe_close
|
|
|
|
****************************************************************************/
|
2009-09-13 18:15:46 +02:00
|
|
|
|
2008-07-26 15:12:11 +02:00
|
|
|
static int pipe_close(FAR struct file *filep)
|
|
|
|
{
|
2015-01-31 19:05:01 +01:00
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct pipe_dev_s *dev = inode->i_private;
|
|
|
|
int ret;
|
2008-07-26 15:12:11 +02:00
|
|
|
|
2015-01-31 19:05:01 +01:00
|
|
|
DEBUGASSERT(dev);
|
2008-07-26 15:12:11 +02:00
|
|
|
|
|
|
|
/* Perform common close operations */
|
|
|
|
|
2018-09-15 18:49:41 +02:00
|
|
|
ret = pipecommon_close(filep);
|
2019-08-06 15:31:57 +02:00
|
|
|
if (ret == 0 && inode->i_crefs == 1)
|
2008-07-26 15:12:11 +02:00
|
|
|
{
|
2008-07-27 21:26:57 +02:00
|
|
|
/* Release the pipe when there are no further open references to it. */
|
2008-07-27 16:58:36 +02:00
|
|
|
|
2022-02-05 23:25:07 +01:00
|
|
|
pipecommon_freedev(dev);
|
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 ret;
|
|
|
|
}
|
|
|
|
|
2023-07-18 14:32:59 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: pipe_mmap
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int pipe_mmap(FAR struct file *filep,
|
|
|
|
FAR struct mm_map_entry_s *entry)
|
|
|
|
{
|
|
|
|
UNUSED(filep);
|
|
|
|
UNUSED(entry);
|
|
|
|
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2008-07-26 15:12:11 +02:00
|
|
|
/****************************************************************************
|
2021-01-03 06:51:38 +01:00
|
|
|
* Name: pipe_register
|
2008-07-26 15:12:11 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2021-01-03 06:51:38 +01:00
|
|
|
static int pipe_register(size_t bufsize, int flags,
|
|
|
|
FAR char *devname, size_t namesize)
|
2008-07-26 15:12:11 +02:00
|
|
|
{
|
2021-01-03 06:51:38 +01:00
|
|
|
FAR struct pipe_dev_s *dev;
|
2008-07-26 15:12:11 +02:00
|
|
|
int pipeno;
|
|
|
|
int ret;
|
|
|
|
|
2008-07-27 16:58:36 +02:00
|
|
|
/* Allocate a minor number for the pipe device */
|
2008-07-26 15:12:11 +02:00
|
|
|
|
2008-07-27 16:58:36 +02:00
|
|
|
pipeno = pipe_allocate();
|
|
|
|
if (pipeno < 0)
|
2008-07-26 15:12:11 +02:00
|
|
|
{
|
2022-02-05 23:25:07 +01:00
|
|
|
return pipeno;
|
2008-07-26 15:12:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Create a pathname to the pipe device */
|
|
|
|
|
2022-02-05 18:10:30 +01:00
|
|
|
snprintf(devname, namesize, CONFIG_DEV_PIPE_VFS_PATH"/%d", pipeno);
|
2008-07-26 15:12:11 +02:00
|
|
|
|
2022-02-05 23:25:07 +01:00
|
|
|
/* Allocate and initialize a new device structure instance */
|
2008-07-26 15:12:11 +02:00
|
|
|
|
2022-02-05 23:25:07 +01:00
|
|
|
dev = pipecommon_allocdev(bufsize);
|
|
|
|
if (dev == NULL)
|
2008-07-26 15:12:11 +02:00
|
|
|
{
|
2022-02-05 23:25:07 +01:00
|
|
|
return -ENOMEM;
|
2008-07-27 16:58:36 +02:00
|
|
|
}
|
2012-07-15 16:56:25 +02:00
|
|
|
|
2022-02-05 23:25:07 +01:00
|
|
|
/* Register the pipe device */
|
2021-01-03 06:51:38 +01:00
|
|
|
|
2023-06-07 15:57:30 +02:00
|
|
|
ret = register_pipedriver(devname, &g_pipe_fops, 0666, (FAR void *)dev);
|
2022-02-05 23:25:07 +01:00
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
pipecommon_freedev(dev);
|
|
|
|
}
|
2021-01-03 06:51:38 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2023-03-05 20:45:51 +01:00
|
|
|
* Name: file_pipe
|
2021-01-03 06:51:38 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2023-03-05 20:45:51 +01:00
|
|
|
* file_pipe() creates a pair of file descriptors, pointing to a pipe
|
|
|
|
* inode, and places them in the array pointed to by 'filep'. filep[0]
|
|
|
|
* is for reading, filep[1] is for writing.
|
2021-01-03 06:51:38 +01:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
2023-03-05 20:45:51 +01:00
|
|
|
* filep[2] - The user provided array in which to catch the pipe file
|
2021-01-03 06:51:38 +01:00
|
|
|
* descriptors
|
2023-03-05 20:45:51 +01:00
|
|
|
* bufsize - The size of the in-memory, circular buffer in bytes.
|
2021-01-03 06:51:38 +01:00
|
|
|
* flags - The file status flags.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
2023-03-05 20:45:51 +01:00
|
|
|
* 0 is returned on success; a negated errno value is returned on a
|
|
|
|
* failure.
|
2021-01-03 06:51:38 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int file_pipe(FAR struct file *filep[2], size_t bufsize, int flags)
|
|
|
|
{
|
2022-02-05 23:25:07 +01:00
|
|
|
char devname[32];
|
2024-02-26 21:31:20 +01:00
|
|
|
int nonblock = !!(flags & O_NONBLOCK);
|
2021-01-03 06:51:38 +01:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Register a new pipe device */
|
|
|
|
|
|
|
|
ret = pipe_register(bufsize, flags, devname, sizeof(devname));
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get a write file descriptor */
|
|
|
|
|
2023-04-27 21:25:06 +02:00
|
|
|
ret = file_open(filep[1], devname, O_WRONLY | O_NONBLOCK | flags);
|
2021-01-03 06:51:38 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout_with_driver;
|
|
|
|
}
|
|
|
|
|
2023-04-27 21:25:06 +02:00
|
|
|
/* Clear O_NONBLOCK if it was set previously */
|
|
|
|
|
2024-02-26 21:31:20 +01:00
|
|
|
if (!nonblock)
|
2023-04-27 21:25:06 +02:00
|
|
|
{
|
2024-02-26 21:31:20 +01:00
|
|
|
ret = file_ioctl(filep[1], FIONBIO, &nonblock);
|
2023-04-27 21:25:06 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout_with_driver;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-03 06:51:38 +01:00
|
|
|
/* Get a read file descriptor */
|
|
|
|
|
|
|
|
ret = file_open(filep[0], devname, O_RDONLY | flags);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout_with_wrfd;
|
|
|
|
}
|
|
|
|
|
2022-02-05 23:25:07 +01:00
|
|
|
/* Remove the pipe name from file system */
|
|
|
|
|
2023-06-07 15:57:30 +02:00
|
|
|
unregister_pipedriver(devname);
|
2021-01-03 06:51:38 +01:00
|
|
|
return OK;
|
|
|
|
|
|
|
|
errout_with_wrfd:
|
|
|
|
file_close(filep[1]);
|
|
|
|
|
|
|
|
errout_with_driver:
|
2023-06-07 15:57:30 +02:00
|
|
|
unregister_pipedriver(devname);
|
2021-01-03 06:51:38 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-03-05 20:45:51 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: pipe2
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* pipe2() creates a pair of file descriptors, pointing to a pipe inode,
|
|
|
|
* and places them in the array pointed to by 'fd'. fd[0] is for reading,
|
|
|
|
* fd[1] is for writing.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* fd[2] - The user provided array in which to catch the pipe file
|
|
|
|
* descriptors
|
|
|
|
* flags - The file status flags.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 is returned on success; -1 (ERROR) is returned on a failure
|
|
|
|
* with the errno value set appropriately.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-10-26 20:02:09 +02:00
|
|
|
int pipe2(int fd[2], int flags)
|
2021-01-03 06:51:38 +01:00
|
|
|
{
|
2022-02-05 23:25:07 +01:00
|
|
|
char devname[32];
|
2024-02-26 21:31:20 +01:00
|
|
|
int nonblock = !!(flags & O_NONBLOCK);
|
2021-01-03 06:51:38 +01:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Register a new pipe device */
|
|
|
|
|
2022-10-26 20:02:09 +02:00
|
|
|
ret = pipe_register(CONFIG_DEV_PIPE_SIZE, flags, devname, sizeof(devname));
|
2021-01-03 06:51:38 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2022-10-26 20:02:09 +02:00
|
|
|
set_errno(-ret);
|
|
|
|
return ERROR;
|
2021-01-03 06:51:38 +01:00
|
|
|
}
|
2013-09-29 00:50:07 +02:00
|
|
|
|
2023-03-05 20:45:51 +01:00
|
|
|
/* Get a write file descriptor setting O_NONBLOCK temporarily */
|
2008-07-26 15:12:11 +02:00
|
|
|
|
2023-03-05 20:45:51 +01:00
|
|
|
fd[1] = open(devname, O_WRONLY | O_NONBLOCK | flags);
|
2013-09-29 00:50:07 +02:00
|
|
|
if (fd[1] < 0)
|
2008-07-26 15:12:11 +02:00
|
|
|
{
|
|
|
|
goto errout_with_driver;
|
|
|
|
}
|
|
|
|
|
2023-03-05 20:45:51 +01:00
|
|
|
/* Clear O_NONBLOCK if it was set previously */
|
|
|
|
|
2024-02-26 21:31:20 +01:00
|
|
|
if (!nonblock)
|
2023-03-05 20:45:51 +01:00
|
|
|
{
|
2024-02-26 21:31:20 +01:00
|
|
|
ret = ioctl(fd[1], FIONBIO, &nonblock);
|
2023-03-05 20:45:51 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout_with_driver;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-26 15:12:11 +02:00
|
|
|
/* Get a read file descriptor */
|
|
|
|
|
2022-10-26 20:02:09 +02:00
|
|
|
fd[0] = open(devname, O_RDONLY | flags);
|
2013-09-29 00:50:07 +02:00
|
|
|
if (fd[0] < 0)
|
2008-07-26 15:12:11 +02:00
|
|
|
{
|
|
|
|
goto errout_with_wrfd;
|
|
|
|
}
|
|
|
|
|
2022-02-05 23:25:07 +01:00
|
|
|
/* Remove the pipe name from file system */
|
|
|
|
|
2023-06-07 15:57:30 +02:00
|
|
|
unregister_pipedriver(devname);
|
2008-07-26 15:12:11 +02:00
|
|
|
return OK;
|
|
|
|
|
|
|
|
errout_with_wrfd:
|
2020-10-28 15:54:40 +01:00
|
|
|
nx_close(fd[1]);
|
2015-01-31 19:05:01 +01:00
|
|
|
|
2008-07-26 15:12:11 +02:00
|
|
|
errout_with_driver:
|
2023-06-07 15:57:30 +02:00
|
|
|
unregister_pipedriver(devname);
|
2022-10-26 20:02:09 +02:00
|
|
|
return ERROR;
|
2008-07-26 15:12:11 +02:00
|
|
|
}
|
|
|
|
|
2008-07-26 16:02:46 +02:00
|
|
|
#endif /* CONFIG_DEV_PIPE_SIZE > 0 */
|