From 70786fa758276b28fdd02e40425a09da04aa2696 Mon Sep 17 00:00:00 2001 From: Xiang Xiao Date: Wed, 5 Feb 2020 22:04:34 +0800 Subject: [PATCH] drivers/net/telnet.c: telnet driver should return -EAGAIN if O_NONBLOCK Telnet driver should return -EAGAIN is O_NONBLOCK is active also should report -EPIPE first Change-Id: I7ad2df15377c7bec8e22d0f5d1b54f7ce33eb0db Signed-off-by: Xiang Xiao --- drivers/net/telnet.c | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/drivers/net/telnet.c b/drivers/net/telnet.c index 881abba080..c8e7ad2f94 100644 --- a/drivers/net/telnet.c +++ b/drivers/net/telnet.c @@ -692,14 +692,6 @@ static int telnet_open(FAR struct file *filep) ninfo("td_crefs: %d\n", priv->td_crefs); - /* O_NONBLOCK is not supported */ - - if (filep->f_oflags & O_NONBLOCK) - { - ret = -ENOSYS; - goto errout; - } - /* Get exclusive access to the device structures */ ret = nxsem_wait(&priv->td_exclsem); @@ -871,7 +863,7 @@ static ssize_t telnet_read(FAR struct file *filep, FAR char *buffer, { FAR struct inode *inode = filep->f_inode; FAR struct telnet_dev_s *priv = inode->i_private; - ssize_t ret; + ssize_t ret = 0; ninfo("len: %d\n", len); @@ -888,21 +880,22 @@ static ssize_t telnet_read(FAR struct file *filep, FAR char *buffer, if (priv->td_pending == 0) { - if (filep->f_oflags & O_NONBLOCK) - { - return 0; - } - - /* Wait for new data (or error) */ - - nxsem_wait_uninterruptible(&priv->td_iosem); - /* poll fds.revents contains last poll status in case of error */ if ((priv->fds.revents & (POLLHUP | POLLERR)) != 0) { return -EPIPE; } + + if (filep->f_oflags & O_NONBLOCK) + { + return -EAGAIN; + } + + /* Wait for new data (or error) */ + + nxsem_wait_uninterruptible(&priv->td_iosem); + continue; } /* Take exclusive access to data buffer */ @@ -1304,7 +1297,10 @@ static int telnet_poll(FAR struct file *filep, FAR struct pollfd *fds, /* Yes.. then signal the poll logic */ fds->revents |= (POLLRDNORM & fds->events); - nxsem_post(fds->sem); + if (fds->revents) + { + nxsem_post(fds->sem); + } } /* Then let psock_poll() do the heavy lifting */