Skip to content

Commit

Permalink
drivers/pipe: Change to the block mode by file_ioctl(FIONBIO)
Browse files Browse the repository at this point in the history
it's simpler and safer than file_fcntl(F_SETFL)

Change-Id: I78c596a9b20489bfbd26332f7226cda68474ae54
Signed-off-by: Xiang Xiao <[email protected]>
  • Loading branch information
xiaoxiang781216 committed Mar 6, 2024
1 parent 7af16de commit 5eb4336
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
16 changes: 7 additions & 9 deletions drivers/pipes/pipe.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <fcntl.h>
#include <assert.h>
#include <errno.h>
#include <sys/ioctl.h>

#include <nuttx/fs/fs.h>
#include <nuttx/mutex.h>
Expand Down Expand Up @@ -207,8 +208,8 @@ static int pipe_register(size_t bufsize, int flags,
int file_pipe(FAR struct file *filep[2], size_t bufsize, int flags)
{
char devname[32];
int nonblock = !!(flags & O_NONBLOCK);
int ret;
bool blocking;

/* Register a new pipe device */

Expand All @@ -218,10 +219,6 @@ int file_pipe(FAR struct file *filep[2], size_t bufsize, int flags)
return ret;
}

/* Check for the O_NONBLOCK bit on flags */

blocking = (flags & O_NONBLOCK) == 0;

/* Get a write file descriptor */

ret = file_open(filep[1], devname, O_WRONLY | O_NONBLOCK | flags);
Expand All @@ -232,9 +229,9 @@ int file_pipe(FAR struct file *filep[2], size_t bufsize, int flags)

/* Clear O_NONBLOCK if it was set previously */

if (blocking)
if (!nonblock)
{
ret = file_fcntl(filep[1], F_SETFL, flags & (~O_NONBLOCK));
ret = file_ioctl(filep[1], FIONBIO, &nonblock);
if (ret < 0)
{
goto errout_with_driver;
Expand Down Expand Up @@ -284,6 +281,7 @@ int file_pipe(FAR struct file *filep[2], size_t bufsize, int flags)
int pipe2(int fd[2], int flags)
{
char devname[32];
int nonblock = !!(flags & O_NONBLOCK);
int ret;

/* Register a new pipe device */
Expand All @@ -305,9 +303,9 @@ int pipe2(int fd[2], int flags)

/* Clear O_NONBLOCK if it was set previously */

if ((flags & O_NONBLOCK) == 0)
if (!nonblock)
{
ret = fcntl(fd[1], F_SETFL, flags & (~O_NONBLOCK));
ret = ioctl(fd[1], FIONBIO, &nonblock);
if (ret < 0)
{
goto errout_with_driver;
Expand Down
3 changes: 2 additions & 1 deletion net/local/local_fifo.c
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,8 @@ static int local_tx_open(FAR struct local_conn_s *conn, FAR const char *path,

if (nonblock == false)
{
ret = file_fcntl(&conn->lc_outfile, F_SETFL, O_WRONLY);
ret = nonblock;
ret = file_ioctl(&conn->lc_outfile, FIONBIO, &ret);
if (ret < 0)
{
return ret;
Expand Down

0 comments on commit 5eb4336

Please sign in to comment.