fs: Add file_pipe function
so pty don't need call nx_pipe and then file_detach Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com> Change-Id: Ibb8d108abd76bafe53897e5fca35babcf3e1bae9
This commit is contained in:
parent
4d4cba41f6
commit
a24ff44ae6
|
@ -158,6 +158,83 @@ static int pipe_close(FAR struct file *filep)
|
|||
return ret;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: pipe_register
|
||||
****************************************************************************/
|
||||
|
||||
static int pipe_register(size_t bufsize, int flags,
|
||||
FAR char *devname, size_t namesize)
|
||||
{
|
||||
FAR struct pipe_dev_s *dev;
|
||||
int pipeno;
|
||||
int ret;
|
||||
|
||||
/* Get exclusive access to the pipe allocation data */
|
||||
|
||||
ret = nxsem_wait(&g_pipesem);
|
||||
if (ret < 0)
|
||||
{
|
||||
goto errout;
|
||||
}
|
||||
|
||||
/* Allocate a minor number for the pipe device */
|
||||
|
||||
pipeno = pipe_allocate();
|
||||
if (pipeno < 0)
|
||||
{
|
||||
ret = pipeno;
|
||||
goto errout_with_sem;
|
||||
}
|
||||
|
||||
/* Create a pathname to the pipe device */
|
||||
|
||||
snprintf(devname, namesize, "/dev/pipe%d", pipeno);
|
||||
|
||||
/* Check if the pipe device has already been created */
|
||||
|
||||
if ((g_pipecreated & (1 << pipeno)) == 0)
|
||||
{
|
||||
/* No.. Allocate and initialize a new device structure instance */
|
||||
|
||||
dev = pipecommon_allocdev(bufsize);
|
||||
if (!dev)
|
||||
{
|
||||
ret = -ENOMEM;
|
||||
goto errout_with_pipe;
|
||||
}
|
||||
|
||||
dev->d_pipeno = pipeno;
|
||||
|
||||
/* Register the pipe device */
|
||||
|
||||
ret = register_driver(devname, &pipe_fops, 0666, (FAR void *)dev);
|
||||
if (ret != 0)
|
||||
{
|
||||
nxsem_post(&g_pipesem);
|
||||
goto errout_with_dev;
|
||||
}
|
||||
|
||||
/* Remember that we created this device */
|
||||
|
||||
g_pipecreated |= (1 << pipeno);
|
||||
}
|
||||
|
||||
nxsem_post(&g_pipesem);
|
||||
return OK;
|
||||
|
||||
errout_with_dev:
|
||||
pipecommon_freedev(dev);
|
||||
|
||||
errout_with_pipe:
|
||||
pipe_free(pipeno);
|
||||
|
||||
errout_with_sem:
|
||||
nxsem_post(&g_pipesem);
|
||||
|
||||
errout:
|
||||
return ret;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
@ -186,66 +263,57 @@ static int pipe_close(FAR struct file *filep)
|
|||
*
|
||||
****************************************************************************/
|
||||
|
||||
int nx_pipe(int fd[2], size_t bufsize, int flags)
|
||||
int file_pipe(FAR struct file *filep[2], size_t bufsize, int flags)
|
||||
{
|
||||
FAR struct pipe_dev_s *dev = NULL;
|
||||
char devname[16];
|
||||
int pipeno;
|
||||
int ret;
|
||||
|
||||
/* Get exclusive access to the pipe allocation data */
|
||||
/* Register a new pipe device */
|
||||
|
||||
ret = nxsem_wait(&g_pipesem);
|
||||
ret = pipe_register(bufsize, flags, devname, sizeof(devname));
|
||||
if (ret < 0)
|
||||
{
|
||||
goto errout;
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Allocate a minor number for the pipe device */
|
||||
/* Get a write file descriptor */
|
||||
|
||||
pipeno = pipe_allocate();
|
||||
if (pipeno < 0)
|
||||
ret = file_open(filep[1], devname, O_WRONLY | flags);
|
||||
if (ret < 0)
|
||||
{
|
||||
nxsem_post(&g_pipesem);
|
||||
ret = pipeno;
|
||||
goto errout;
|
||||
goto errout_with_driver;
|
||||
}
|
||||
|
||||
/* Create a pathname to the pipe device */
|
||||
/* Get a read file descriptor */
|
||||
|
||||
snprintf(devname, sizeof(devname), "/dev/pipe%d", pipeno);
|
||||
|
||||
/* Check if the pipe device has already been created */
|
||||
|
||||
if ((g_pipecreated & (1 << pipeno)) == 0)
|
||||
ret = file_open(filep[0], devname, O_RDONLY | flags);
|
||||
if (ret < 0)
|
||||
{
|
||||
/* No.. Allocate and initialize a new device structure instance */
|
||||
|
||||
dev = pipecommon_allocdev(bufsize);
|
||||
if (!dev)
|
||||
{
|
||||
nxsem_post(&g_pipesem);
|
||||
ret = -ENOMEM;
|
||||
goto errout_with_pipe;
|
||||
}
|
||||
|
||||
dev->d_pipeno = pipeno;
|
||||
|
||||
/* Register the pipe device */
|
||||
|
||||
ret = register_driver(devname, &pipe_fops, 0666, (FAR void *)dev);
|
||||
if (ret != 0)
|
||||
{
|
||||
nxsem_post(&g_pipesem);
|
||||
goto errout_with_dev;
|
||||
}
|
||||
|
||||
/* Remember that we created this device */
|
||||
|
||||
g_pipecreated |= (1 << pipeno);
|
||||
goto errout_with_wrfd;
|
||||
}
|
||||
|
||||
nxsem_post(&g_pipesem);
|
||||
return OK;
|
||||
|
||||
errout_with_wrfd:
|
||||
file_close(filep[1]);
|
||||
|
||||
errout_with_driver:
|
||||
unregister_driver(devname);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int nx_pipe(int fd[2], size_t bufsize, int flags)
|
||||
{
|
||||
char devname[16];
|
||||
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 */
|
||||
|
||||
|
@ -272,17 +340,6 @@ errout_with_wrfd:
|
|||
|
||||
errout_with_driver:
|
||||
unregister_driver(devname);
|
||||
|
||||
errout_with_dev:
|
||||
if (dev)
|
||||
{
|
||||
pipecommon_freedev(dev);
|
||||
}
|
||||
|
||||
errout_with_pipe:
|
||||
pipe_free(pipeno);
|
||||
|
||||
errout:
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
|
|
@ -1080,8 +1080,8 @@ static int pty_unlink(FAR struct inode *inode)
|
|||
int pty_register(int minor)
|
||||
{
|
||||
FAR struct pty_devpair_s *devpair;
|
||||
int pipe_a[2];
|
||||
int pipe_b[2];
|
||||
FAR struct file *pipe_a[2];
|
||||
FAR struct file *pipe_b[2];
|
||||
char devname[16];
|
||||
int ret;
|
||||
|
||||
|
@ -1118,56 +1118,24 @@ int pty_register(int minor)
|
|||
* pipe_b: Master sink, slave source (RX, master-to-slave)
|
||||
*/
|
||||
|
||||
ret = nx_pipe(pipe_a, CONFIG_PSEUDOTERM_TXBUFSIZE, 0);
|
||||
pipe_a[0] = &devpair->pp_master.pd_src;
|
||||
pipe_a[1] = &devpair->pp_slave.pd_sink;
|
||||
|
||||
ret = file_pipe(pipe_a, CONFIG_PSEUDOTERM_TXBUFSIZE, 0);
|
||||
if (ret < 0)
|
||||
{
|
||||
goto errout_with_devpair;
|
||||
}
|
||||
|
||||
ret = nx_pipe(pipe_b, CONFIG_PSEUDOTERM_RXBUFSIZE, 0);
|
||||
pipe_b[0] = &devpair->pp_slave.pd_src;
|
||||
pipe_b[1] = &devpair->pp_master.pd_sink;
|
||||
|
||||
ret = file_pipe(pipe_b, CONFIG_PSEUDOTERM_RXBUFSIZE, 0);
|
||||
if (ret < 0)
|
||||
{
|
||||
goto errout_with_pipea;
|
||||
}
|
||||
|
||||
/* Detach the pipe file descriptors (closing them in the process)
|
||||
*
|
||||
* fd[0] is for reading;
|
||||
* fd[1] is for writing.
|
||||
*/
|
||||
|
||||
ret = file_detach(pipe_a[0], &devpair->pp_master.pd_src);
|
||||
if (ret < 0)
|
||||
{
|
||||
goto errout_with_pipeb;
|
||||
}
|
||||
|
||||
pipe_a[0] = -1;
|
||||
|
||||
ret = file_detach(pipe_a[1], &devpair->pp_slave.pd_sink);
|
||||
if (ret < 0)
|
||||
{
|
||||
goto errout_with_pipeb;
|
||||
}
|
||||
|
||||
pipe_a[1] = -1;
|
||||
|
||||
ret = file_detach(pipe_b[0], &devpair->pp_slave.pd_src);
|
||||
if (ret < 0)
|
||||
{
|
||||
goto errout_with_pipeb;
|
||||
}
|
||||
|
||||
pipe_b[0] = -1;
|
||||
|
||||
ret = file_detach(pipe_b[1], &devpair->pp_master.pd_sink);
|
||||
if (ret < 0)
|
||||
{
|
||||
goto errout_with_pipeb;
|
||||
}
|
||||
|
||||
pipe_b[1] = -1;
|
||||
|
||||
/* Register the slave device
|
||||
*
|
||||
* BSD style (deprecated): /dev/ttypN
|
||||
|
@ -1215,42 +1183,12 @@ errout_with_slave:
|
|||
unregister_driver(devname);
|
||||
|
||||
errout_with_pipeb:
|
||||
if (pipe_b[0] >= 0)
|
||||
{
|
||||
close(pipe_b[0]);
|
||||
}
|
||||
else
|
||||
{
|
||||
file_close(&devpair->pp_master.pd_src);
|
||||
}
|
||||
|
||||
if (pipe_b[1] >= 0)
|
||||
{
|
||||
close(pipe_b[1]);
|
||||
}
|
||||
else
|
||||
{
|
||||
file_close(&devpair->pp_slave.pd_sink);
|
||||
}
|
||||
file_close(pipe_b[0]);
|
||||
file_close(pipe_b[1]);
|
||||
|
||||
errout_with_pipea:
|
||||
if (pipe_a[0] >= 0)
|
||||
{
|
||||
close(pipe_a[0]);
|
||||
}
|
||||
else
|
||||
{
|
||||
file_close(&devpair->pp_slave.pd_src);
|
||||
}
|
||||
|
||||
if (pipe_a[1] >= 0)
|
||||
{
|
||||
close(pipe_a[1]);
|
||||
}
|
||||
else
|
||||
{
|
||||
file_close(&devpair->pp_master.pd_sink);
|
||||
}
|
||||
file_close(pipe_a[0]);
|
||||
file_close(pipe_a[1]);
|
||||
|
||||
errout_with_devpair:
|
||||
nxsem_destroy(&devpair->pp_exclsem);
|
||||
|
|
|
@ -1492,6 +1492,7 @@ int nx_unlink(FAR const char *pathname);
|
|||
****************************************************************************/
|
||||
|
||||
#if defined(CONFIG_PIPES) && CONFIG_DEV_PIPE_SIZE > 0
|
||||
int file_pipe(FAR struct file *filep[2], size_t bufsize, int flags);
|
||||
int nx_pipe(int fd[2], size_t bufsize, int flags);
|
||||
#endif
|
||||
|
||||
|
|
Loading…
Reference in New Issue