Hi Ying,

Actually, the 'done' flag has been set in the particular case (since the
'sk->sk_state' changed to 'TIPC_DISCONNECTING' after receiving a rejection
of its SYN from server...) and what we want to achieve is the error code
from the 'sock_error(sk)' to be returned to user correctly.
For your code, there is no difference, the function would still return '0'
for the said case.
I considered an alternative that:

                done = sk_wait_event(sk, timeo_p,
                                     sk->sk_state != TIPC_CONNECTING,
&wait);
                remove_wait_queue(sk_sleep(sk), &wait);
        } while (!done);
-       return 0;
+      return sock_err(sk);

but this could get more concerns or we should check the socket state at the
return e.g. 
        return (sk->sk_state != TIPC_ESTABLISHED) ? sock_err(sk) : 0;

and the fact is that we have this code already in the while statement, so I
have decided to go with the code below.

BR/Tuong

-----Original Message-----
From: Xue, Ying <ying....@windriver.com> 
Sent: Wednesday, December 25, 2019 8:48 PM
To: Tuong Lien <tuong.t.l...@dektech.com.au>;
tipc-discussion@lists.sourceforge.net; jon.ma...@ericsson.com;
ma...@donjonn.com
Subject: RE: [net] tipc: fix wrong connect() return code

Probably below change is more easily understandable:

diff --git a/net/tipc/socket.c b/net/tipc/socket.c
index 6552f98..358cc55 100644
--- a/net/tipc/socket.c
+++ b/net/tipc/socket.c
@@ -2435,7 +2435,7 @@ static int tipc_wait_for_connect(struct socket *sock,
long *timeo_p)
                done = sk_wait_event(sk, timeo_p,
                                     sk->sk_state != TIPC_CONNECTING,
&wait);
                remove_wait_queue(sk_sleep(sk), &wait);
-       } while (!done);
+       } while (!done || sk->sk_err);
        return 0;
 }

-----Original Message-----
From: Tuong Lien [mailto:tuong.t.l...@dektech.com.au] 
Sent: Tuesday, December 24, 2019 4:06 PM
To: tipc-discussion@lists.sourceforge.net; jon.ma...@ericsson.com;
ma...@donjonn.com; Xue, Ying
Subject: [net] tipc: fix wrong connect() return code

The current 'tipc_wait_for_connect()' function makes a loop and waits
for the condition 'sk->sk_state != TIPC_CONNECTING' to conclude if the
connecting has done. However, when the condition is met, it always
returns '0' even in the case the connecting was actually failed (e.g.
refused because the server socket has closed...) and the socket state
was set to 'TIPC_DISCONNECTING'.
This results in a wrong return code for the 'connect()' call from user,
making it believe that the connection is established and goes ahead
with more actions e.g. building & sending a message but then finally
gets an unexpected result (e.g. '-EPIPE').

This commit fixes the issue by returning the corresponding error code
if any when the wait process is waken up.

Signed-off-by: Tuong Lien <tuong.t.l...@dektech.com.au>
---
 net/tipc/socket.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/net/tipc/socket.c b/net/tipc/socket.c
index 8b1daf3634b0..2e5faf89ef80 100644
--- a/net/tipc/socket.c
+++ b/net/tipc/socket.c
@@ -2428,7 +2428,7 @@ static int tipc_wait_for_connect(struct socket *sock,
long *timeo_p)
 {
        DEFINE_WAIT_FUNC(wait, woken_wake_function);
        struct sock *sk = sock->sk;
-       int done;
+       int done = 0;
 
        do {
                int err = sock_error(sk);
@@ -2438,12 +2438,14 @@ static int tipc_wait_for_connect(struct socket
*sock, long *timeo_p)
                        return -ETIMEDOUT;
                if (signal_pending(current))
                        return sock_intr_errno(*timeo_p);
+               if (done)
+                       return 0;
 
                add_wait_queue(sk_sleep(sk), &wait);
                done = sk_wait_event(sk, timeo_p,
                                     sk->sk_state != TIPC_CONNECTING,
&wait);
                remove_wait_queue(sk_sleep(sk), &wait);
-       } while (!done);
+       } while (1);
        return 0;
 }
 
-- 
2.13.7




_______________________________________________
tipc-discussion mailing list
tipc-discussion@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/tipc-discussion

Reply via email to