From 1ba065db87023ab9854d9e431c53d45e26493793 Mon Sep 17 00:00:00 2001 From: Xiang Xiao Date: Tue, 27 Feb 2024 04:31:20 +0800 Subject: [PATCH] drivers/pipe: Change to the block mode by file_ioctl(FIONBIO) it's simpler and safer than file_fcntl(F_SETFL) Signed-off-by: Xiang Xiao --- drivers/pipes/pipe.c | 16 +++++++--------- net/local/local_fifo.c | 3 ++- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/drivers/pipes/pipe.c b/drivers/pipes/pipe.c index 61918b8f5e9..4220db4d7ac 100644 --- a/drivers/pipes/pipe.c +++ b/drivers/pipes/pipe.c @@ -31,6 +31,7 @@ #include #include #include +#include #include #include @@ -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 */ @@ -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); @@ -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; @@ -284,6 +281,7 @@ errout_with_driver: int pipe2(int fd[2], int flags) { char devname[32]; + int nonblock = !!(flags & O_NONBLOCK); int ret; /* Register a new pipe device */ @@ -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; diff --git a/net/local/local_fifo.c b/net/local/local_fifo.c index 4f555c57fd2..c862705b703 100644 --- a/net/local/local_fifo.c +++ b/net/local/local_fifo.c @@ -294,7 +294,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;