Skip to content

Commit

Permalink
Update net_timedwait() and net_lockedwait() call sites to handle nega…
Browse files Browse the repository at this point in the history
…ted errno in return value
  • Loading branch information
Jussi Kivilinna authored and gregory-nutt committed Sep 4, 2017
1 parent 23edfe2 commit 547733c
Show file tree
Hide file tree
Showing 27 changed files with 79 additions and 90 deletions.
2 changes: 1 addition & 1 deletion net/arp/arp_notify.c
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ int arp_wait(FAR struct arp_notify_s *notify, FAR struct timespec *timeout)
*/

ret = net_timedwait(&notify->nt_sem, &abstime);
errcode = ((ret < 0) ? get_errno() : 0);
errcode = ((ret < 0) ? -ret : 0);
}
while (ret < 0 && errcode == EINTR);

Expand Down
2 changes: 1 addition & 1 deletion net/ieee802154/ieee802154_sendto.c
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ ssize_t psock_ieee802154_sendto(FAR struct socket *psock, FAR const void *buf,
}

/* If net_lockedwait failed, then we were probably reawakened by a signal. In
* this case, net_lockedwait will have set errno appropriately.
* this case, net_lockedwait will have returned negated errno appropriately.
*/

if (ret < 0)
Expand Down
8 changes: 6 additions & 2 deletions net/igmp/igmp_msg.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ void igmp_schedmsg(FAR struct igmp_group_s *group, uint8_t msgid)

void igmp_waitmsg(FAR struct igmp_group_s *group, uint8_t msgid)
{
int ret;

/* Schedule to send the message */

net_lock();
Expand All @@ -105,14 +107,16 @@ void igmp_waitmsg(FAR struct igmp_group_s *group, uint8_t msgid)
{
/* Wait for the semaphore to be posted */

while (net_lockedwait(&group->sem) != 0)
while ((ret = net_lockedwait(&group->sem)) < 0)
{
/* The only error that should occur from net_lockedwait() is if
* the wait is awakened by a signal.
*/

ASSERT(get_errno() == EINTR);
ASSERT(ret == -EINTR);
}

UNUSED(ret);
}

/* The message has been sent and we are no longer waiting */
Expand Down
6 changes: 2 additions & 4 deletions net/inet/inet_recvfrom.c
Original file line number Diff line number Diff line change
Expand Up @@ -1131,8 +1131,6 @@ static void inet_recvfrom_initialize(FAR struct socket *psock, FAR void *buf,
#if defined(NET_UDP_HAVE_STACK) || defined(NET_TCP_HAVE_STACK)
static ssize_t inet_recvfrom_result(int result, struct inet_recvfrom_s *pstate)
{
int save_errno = get_errno(); /* In case something we do changes it */

/* Check for a error/timeout detected by the interrupt handler. Errors are
* signaled by negative errno values for the rcv length
*/
Expand All @@ -1147,12 +1145,12 @@ static ssize_t inet_recvfrom_result(int result, struct inet_recvfrom_s *pstate)
}

/* If net_lockedwait failed, then we were probably reawakened by a signal. In
* this case, net_lockedwait will have set errno appropriately.
* this case, net_lockedwait will have returned negated errno appropriately.
*/

if (result < 0)
{
return -save_errno;
return result;
}

return pstate->ir_recvlen;
Expand Down
8 changes: 6 additions & 2 deletions net/pkt/pkt_conn.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,20 @@ static dq_queue_t g_active_pkt_connections;

static inline void _pkt_semtake(sem_t *sem)
{
int ret;

/* Take the semaphore (perhaps waiting) */

while (net_lockedwait(sem) != 0)
while ((ret = net_lockedwait(sem)) < 0)
{
/* The only case that an error should occur here is if
* the wait was awakened by a signal.
*/

DEBUGASSERT(get_errno() == EINTR);
DEBUGASSERT(ret == -EINTR);
}

UNUSED(ret);
}

#define _pkt_semgive(sem) sem_post(sem)
Expand Down
6 changes: 2 additions & 4 deletions net/pkt/pkt_recvfrom.c
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,6 @@ static void pkt_recvfrom_initialize(FAR struct socket *psock, FAR void *buf,

static ssize_t pkt_recvfrom_result(int result, struct pkt_recvfrom_s *pstate)
{
int save_errno = get_errno(); /* In case something we do changes it */

/* Check for a error/timeout detected by the interrupt handler. Errors are
* signaled by negative errno values for the rcv length
*/
Expand All @@ -312,12 +310,12 @@ static ssize_t pkt_recvfrom_result(int result, struct pkt_recvfrom_s *pstate)
}

/* If net_lockedwait failed, then we were probably reawakened by a signal. In
* this case, net_lockedwait will have set errno appropriately.
* this case, net_lockedwait will have returned negated errno appropriately.
*/

if (result < 0)
{
return -save_errno;
return result;
}

return pstate->pr_recvlen;
Expand Down
2 changes: 1 addition & 1 deletion net/pkt/pkt_send.c
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ ssize_t psock_pkt_send(FAR struct socket *psock, FAR const void *buf,
}

/* If net_lockedwait failed, then we were probably reawakened by a signal. In
* this case, net_lockedwait will have set errno appropriately.
* this case, net_lockedwait will have returned negated errno appropriately.
*/

if (ret < 0)
Expand Down
2 changes: 1 addition & 1 deletion net/sixlowpan/sixlowpan_send.c
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ int sixlowpan_send(FAR struct net_driver_s *dev,
ret = net_lockedwait(&sinfo.s_waitsem);
if (ret < 0)
{
sinfo.s_result = -get_errno();
sinfo.s_result = ret;
}

/* Make sure that no further interrupts are processed */
Expand Down
2 changes: 1 addition & 1 deletion net/sixlowpan/sixlowpan_tcpsend.c
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ static int sixlowpan_send_packet(FAR struct socket *psock,
ret = net_lockedwait(&sinfo.s_waitsem);
if (ret < 0)
{
sinfo.s_result = -get_errno();
sinfo.s_result = ret;
}

/* Make sure that no further interrupts are processed */
Expand Down
8 changes: 6 additions & 2 deletions net/socket/net_sockets.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,20 @@

static void _net_semtake(FAR struct socketlist *list)
{
int ret;

/* Take the semaphore (perhaps waiting) */

while (net_lockedwait(&list->sl_sem) != 0)
while ((ret = net_lockedwait(&list->sl_sem)) < 0)
{
/* The only case that an error should occr here is if
* the wait was awakened by a signal.
*/

DEBUGASSERT(get_errno() == EINTR);
DEBUGASSERT(ret == -EINTR);
}

UNUSED(ret);
}

#define _net_semgive(list) sem_post(&list->sl_sem)
Expand Down
18 changes: 2 additions & 16 deletions net/tcp/tcp_accept.c
Original file line number Diff line number Diff line change
Expand Up @@ -298,20 +298,6 @@ int psock_tcp_accept(FAR struct socket *psock, FAR struct sockaddr *addr,
*/

ret = net_lockedwait(&state.acpt_sem);
if (ret < 0)
{
/* The value returned by net_lockedwait() the same as the value
* returned by sem_wait(): Zero (OK) is returned on success; -1
* (ERROR) is returned on a failure with the errno value set
* appropriately.
*
* We have to preserve the errno value here because it may be
* altered by intervening operations.
*/

ret = -get_errno();
DEBUGASSERT(ret < 0);
}

/* Make sure that no further events are processed */

Expand All @@ -335,8 +321,8 @@ int psock_tcp_accept(FAR struct socket *psock, FAR struct sockaddr *addr,
}

/* If net_lockedwait failed, then we were probably reawakened by a
* signal. In this case, logic above will have set 'ret' to the
* errno value returned by net_lockedwait().
* signal. In this case, net_lockedwait will have returned negated
* errno appropriately.
*/

if (ret < 0)
Expand Down
8 changes: 2 additions & 6 deletions net/tcp/tcp_connect.c
Original file line number Diff line number Diff line change
Expand Up @@ -372,13 +372,9 @@ int psock_tcp_connect(FAR struct socket *psock,

(void)sem_destroy(&state.tc_sem);

/* If net_lockedwait failed, recover the negated error (probably -EINTR) */
/* If net_lockedwait failed, negated errno was returned. */

if (ret < 0)
{
ret = -get_errno();
}
else
if (ret >= 0)
{
/* If the wait succeeded, then get the new error value from
* the state structure
Expand Down
3 changes: 2 additions & 1 deletion net/tcp/tcp_send_buffered.c
Original file line number Diff line number Diff line change
Expand Up @@ -1093,7 +1093,8 @@ ssize_t psock_tcp_send(FAR struct socket *psock, FAR const void *buf,
}

/* If net_lockedwait failed, then we were probably reawakened by a signal.
* In this case, net_lockedwait will have set errno appropriately.
* In this case, net_lockedwait will have returned negated errno
* appropriately.
*/

if (ret < 0)
Expand Down
2 changes: 1 addition & 1 deletion net/tcp/tcp_send_unbuffered.c
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,7 @@ ssize_t psock_tcp_send(FAR struct socket *psock,
}

/* If net_lockedwait failed, then we were probably reawakened by a signal. In
* this case, net_lockedwait will have set errno appropriately.
* this case, net_lockedwait will have returned negated errno appropriately.
*/

if (ret < 0)
Expand Down
2 changes: 1 addition & 1 deletion net/tcp/tcp_wrbuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ FAR struct tcp_wrbuffer_s *tcp_wrbuffer_alloc(void)
* buffer
*/

DEBUGVERIFY(net_lockedwait(&g_wrbuffer.sem));
DEBUGVERIFY(net_lockedwait(&g_wrbuffer.sem)); /* TODO: Handle EINTR. */

/* Now, we are guaranteed to have a write buffer structure reserved
* for us in the free list.
Expand Down
8 changes: 6 additions & 2 deletions net/udp/udp_conn.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,20 @@ static uint16_t g_last_udp_port;

static inline void _udp_semtake(FAR sem_t *sem)
{
int ret;

/* Take the semaphore (perhaps waiting) */

while (net_lockedwait(sem) != 0)
while ((ret = net_lockedwait(sem)) < 0)
{
/* The only case that an error should occur here is if
* the wait was awakened by a signal.
*/

ASSERT(get_errno() == EINTR);
ASSERT(ret == -EINTR);
}

UNUSED(ret);
}

#define _udp_semgive(sem) sem_post(sem)
Expand Down
4 changes: 2 additions & 2 deletions net/usrsock/usrsock_bind.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,9 @@ int usrsock_bind(FAR struct socket *psock,
{
/* Wait for completion of request. */

while (net_lockedwait(&state.recvsem) != OK)
while ((ret = net_lockedwait(&state.recvsem)) < 0)
{
DEBUGASSERT(*get_errno_ptr() == EINTR);
DEBUGASSERT(ret == -EINTR);
}

ret = state.result;
Expand Down
4 changes: 2 additions & 2 deletions net/usrsock/usrsock_close.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,9 @@ int usrsock_close(FAR struct usrsock_conn_s *conn)
{
/* Wait for completion of request. */

while (net_lockedwait(&state.recvsem) != OK)
while ((ret = net_lockedwait(&state.recvsem)) < OK)
{
DEBUGASSERT(*get_errno_ptr() == EINTR);
DEBUGASSERT(ret == -EINTR);
}

ret = state.result;
Expand Down
6 changes: 4 additions & 2 deletions net/usrsock/usrsock_conn.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,17 @@ static dq_queue_t g_active_usrsock_connections;

static void _usrsock_semtake(FAR sem_t *sem)
{
int ret;

/* Take the semaphore (perhaps waiting) */

while (net_lockedwait(sem) != 0)
while ((ret = net_lockedwait(sem)) < 0)
{
/* The only case that an error should occur here is if
* the wait was awakened by a signal.
*/

DEBUGASSERT(*get_errno_ptr() == EINTR);
DEBUGASSERT(ret == -EINTR);
}
}

Expand Down
6 changes: 3 additions & 3 deletions net/usrsock/usrsock_connect.c
Original file line number Diff line number Diff line change
Expand Up @@ -228,13 +228,13 @@ int usrsock_connect(FAR struct socket *psock,
{
/* Wait for completion of request (or signal). */

if (net_lockedwait(&state.recvsem) != OK)
ret = net_lockedwait(&state.recvsem);
if (ret < 0)
{
DEBUGASSERT(*get_errno_ptr() == EINTR);
DEBUGASSERT(ret == -EINTR);

/* Wait interrupted, exit early. */

ret = -EINTR;
goto errout_teardown;
}

Expand Down
13 changes: 6 additions & 7 deletions net/usrsock/usrsock_dev.c
Original file line number Diff line number Diff line change
Expand Up @@ -1026,9 +1026,7 @@ static int usrsockdev_close(FAR struct file *filep)
ret = net_timedwait(&dev->req.sem, &abstime);
if (ret < 0)
{
ret = *get_errno_ptr();

if (ret != ETIMEDOUT && ret != EINTR)
if (ret != -ETIMEDOUT && ret != -EINTR)
{
ninfo("net_timedwait errno: %d\n", ret);
DEBUGASSERT(false);
Expand Down Expand Up @@ -1179,6 +1177,7 @@ int usrsockdev_do_request(FAR struct usrsock_conn_s *conn,
{
FAR struct usrsockdev_s *dev = conn->dev;
FAR struct usrsock_request_common_s *req_head = iov[0].iov_base;
int ret;

if (!dev)
{
Expand Down Expand Up @@ -1209,9 +1208,9 @@ int usrsockdev_do_request(FAR struct usrsock_conn_s *conn,

/* Set outstanding request for daemon to handle. */

while (net_lockedwait(&dev->req.sem) != OK)
while ((ret = net_lockedwait(&dev->req.sem)) < 0)
{
DEBUGASSERT(*get_errno_ptr() == EINTR);
DEBUGASSERT(ret == -EINTR);
}

if (usrsockdev_is_opened(dev))
Expand All @@ -1228,9 +1227,9 @@ int usrsockdev_do_request(FAR struct usrsock_conn_s *conn,

/* Wait ack for request. */

while (net_lockedwait(&dev->req.acksem) != OK)
while ((ret = net_lockedwait(&dev->req.acksem)) < 0)
{
DEBUGASSERT(*get_errno_ptr() == EINTR);
DEBUGASSERT(ret == -EINTR);
}
}
else
Expand Down
4 changes: 2 additions & 2 deletions net/usrsock/usrsock_getsockname.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,9 @@ int usrsock_getsockname(FAR struct socket *psock,
{
/* Wait for completion of request. */

while (net_lockedwait(&state.reqstate.recvsem) != OK)
while ((ret = net_lockedwait(&state.reqstate.recvsem)) < 0)
{
DEBUGASSERT(*get_errno_ptr() == EINTR);
DEBUGASSERT(ret == -EINTR);
}

ret = state.reqstate.result;
Expand Down
4 changes: 2 additions & 2 deletions net/usrsock/usrsock_getsockopt.c
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,9 @@ int usrsock_getsockopt(FAR struct usrsock_conn_s *conn, int level, int option,
{
/* Wait for completion of request. */

while (net_lockedwait(&state.reqstate.recvsem) != OK)
while ((ret = net_lockedwait(&state.reqstate.recvsem)) < 0)
{
DEBUGASSERT(*get_errno_ptr() == EINTR);
DEBUGASSERT(ret == -EINTR);
}

ret = state.reqstate.result;
Expand Down
Loading

0 comments on commit 547733c

Please sign in to comment.