Skip to content
Snippets Groups Projects
Commit 69f23482 authored by anonymouspage's avatar anonymouspage
Browse files

Use shutdown() ahead of close() for rlogin/ssh

Using only close() on a socket has a few issues:

 (1) poll is not required to wake on such events leading to hangs
     if infinite timeouts are used.

     This can be observed in macOS:

     (a) syncterm -iC
     (b) connect via telnet to some system
     (c) use the menu to disconnect from the system

     Note: socket must be idle to reproduce.

 (2) If a blocked recv/send receives an error in response to a close
     from another thread, the context of the socket has been lost: the
     descriptor is free to be reallocated under the thread performing
     I/O by the system. Any retry logic that may be written in response
     to the error condition could lead to operations being performed
     on a valid but incorrect resource.

 (3) Processes that inherited the socket via fork() will still have
     a valid descriptor to the socket. i.e., the socket does not
     globally shutdown via a call to close(); however, shutdown()
     acts on the connection by triggering a TCP FIN. This will lead
     to the actual shutdown of the socket by the remote connection
     as well.

Solution:

Use shutdown() ahead of close(). This will initiate a proper shutdown
sequence on the socket while leaving the descriptor open. poll() will
wake, and a subsequent read or write will yield the desired EOF/EPIPE.
parent bb233b89
No related branches found
No related tags found
No related merge requests found
Pipeline #3291 passed
......@@ -212,11 +212,12 @@ int rlogin_close(void)
char garbage[1024];
conn_api.terminate=1;
closesocket(rlogin_sock);
shutdown(rlogin_sock, SHUT_RDWR);
while(conn_api.input_thread_running == 1 || conn_api.output_thread_running == 1) {
conn_recv_upto(garbage, sizeof(garbage), 0);
SLEEP(1);
}
closesocket(rlogin_sock);
destroy_conn_buf(&conn_inbuf);
destroy_conn_buf(&conn_outbuf);
FREE_AND_NULL(conn_api.rd_buf);
......
......@@ -355,6 +355,7 @@ int ssh_close(void)
SLEEP(1);
}
cl.DestroySession(ssh_session);
shutdown(SHUT_RDWR, ssh_sock);
closesocket(ssh_sock);
ssh_sock=INVALID_SOCKET;
destroy_conn_buf(&conn_inbuf);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment