openocd: fix issue in WIN32 with TCP adapters

Issue: server_quit is called before adapter_quit:
  In WIN32 only in server_quit we do an WSACleanup,
  which terminates/closes all active sockets.
  So if the adapter is TCP based, the adapter.quit handler
  will fail if it will need to send some commands through TCP.

Example: close_socket in jtag_vpi_quit will fail in WIN32
  because the socket is already closed
  and the errno is set as "Bad File Descriptor"

To fix that we introduced new functions called server_host_os_entry/quit
to manage specific OS setup (hence WSA for sockets in WINDOWS) in order
to delay WSACleanup after adapter_quit().

Change-Id: Ie4afacafe123857f6ae300e376bdfcf0d8c027ac
Signed-off-by: Tarek BOCHKATI <tarek.bouchkati@gmail.com>
Reviewed-on: http://openocd.zylin.com/5456
Tested-by: jenkins
Reviewed-by: Antonio Borneo <borneo.antonio@gmail.com>
This commit is contained in:
Tarek BOCHKATI
2020-04-23 11:00:31 +01:00
committed by Antonio Borneo
parent 7e78c04f1c
commit 9a690c6bdb
3 changed files with 22 additions and 2 deletions

View File

@@ -631,7 +631,7 @@ static void sigkey_handler(int sig)
#endif
int server_preinit(void)
int server_host_os_entry(void)
{
/* this currently only calls WSAStartup on native win32 systems
* before any socket operations are performed.
@@ -647,7 +647,21 @@ int server_preinit(void)
LOG_ERROR("Failed to Open Winsock");
return ERROR_FAIL;
}
#endif
return ERROR_OK;
}
int server_host_os_close(void)
{
#ifdef _WIN32
WSACleanup();
#endif
return ERROR_OK;
}
int server_preinit(void)
{
#ifdef _WIN32
/* register ctrl-c handler */
SetConsoleCtrlHandler(ControlHandler, TRUE);
@@ -688,7 +702,6 @@ int server_quit(void)
target_quit();
#ifdef _WIN32
WSACleanup();
SetConsoleCtrlHandler(ControlHandler, FALSE);
return ERROR_OK;