From d4e7fe55c76b9b3544e25b065d702835502d8234 Mon Sep 17 00:00:00 2001 From: Tiago Medicci Serrano Date: Thu, 27 Apr 2023 16:25:06 -0300 Subject: [PATCH] drivers/pipe: fix blocking file_pipe Similar to the fix introduced by 4d6a8663fa0932a8c1b40639464f68af670f0808, it's necessary to set one end of the file_pipe as non-blocking temporarily while opening the other end to avoid it blocking unexpectedily. --- drivers/pipes/pipe.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/drivers/pipes/pipe.c b/drivers/pipes/pipe.c index 5f62473bca..5ff22b044b 100644 --- a/drivers/pipes/pipe.c +++ b/drivers/pipes/pipe.c @@ -192,6 +192,7 @@ int file_pipe(FAR struct file *filep[2], size_t bufsize, int flags) { char devname[32]; int ret; + bool blocking; /* Register a new pipe device */ @@ -201,14 +202,29 @@ 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 | flags); + ret = file_open(filep[1], devname, O_WRONLY | O_NONBLOCK | flags); if (ret < 0) { goto errout_with_driver; } + /* Clear O_NONBLOCK if it was set previously */ + + if (blocking) + { + ret = file_fcntl(filep[1], F_SETFL, flags & (~O_NONBLOCK)); + if (ret < 0) + { + goto errout_with_driver; + } + } + /* Get a read file descriptor */ ret = file_open(filep[0], devname, O_RDONLY | flags);