From e654d41f1764dcf2856703adb9bca883dfcb13ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Jaramago=20Fern=C3=A1ndez?= Date: Tue, 17 Nov 2020 21:19:14 +0100 Subject: [PATCH] Added 'SO_REUSEADDR' option to socket used to test 'web_port' availability --- lib/ProxySQL_Admin.cpp | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/ProxySQL_Admin.cpp b/lib/ProxySQL_Admin.cpp index f07be99b1..efad8612e 100644 --- a/lib/ProxySQL_Admin.cpp +++ b/lib/ProxySQL_Admin.cpp @@ -5833,6 +5833,7 @@ void ProxySQL_Admin::flush_admin_variables___database_to_runtime(SQLite3DB *db, } else { if (GloWebInterface) { int sfd = 0; + int reuseaddr = 1; struct sockaddr_in tmp_addr; sfd = socket(AF_INET, SOCK_STREAM, 0); @@ -5841,15 +5842,23 @@ void ProxySQL_Admin::flush_admin_variables___database_to_runtime(SQLite3DB *db, tmp_addr.sin_port = htons(variables.web_port); tmp_addr.sin_addr.s_addr = INADDR_ANY; - if (bind(sfd, (struct sockaddr*)&tmp_addr, sizeof(tmp_addr)) == -1) { + if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuseaddr, sizeof(reuseaddr)) == -1) { close(sfd); proxy_error( - "Unable to start WebInterfacePlugin, port '%d' already in use.\n", + "Unable to start WebInterfacePlugin, failed to set 'SO_REUSEADDR' to check port availability.\n", variables.web_port ); } else { - close(sfd); - GloWebInterface->start(variables.web_port); + if (bind(sfd, (struct sockaddr*)&tmp_addr, sizeof(tmp_addr)) == -1) { + close(sfd); + proxy_error( + "Unable to start WebInterfacePlugin, port '%d' already in use.\n", + variables.web_port + ); + } else { + close(sfd); + GloWebInterface->start(variables.web_port); + } } } }