base,cpu,dev: Simplify ListenSocket::listen().
Remove the "reuse" parameter which default to true and was always also explicitly set to true. Tidy up the code itself slightly, mostly by using "panic_if" to remove some nesting. Change-Id: Ie23971aabf2fe4252d27f1887468360722a72379 Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/69159 Maintainer: Gabe Black <gabeblack@google.com> Reviewed-by: Yu-hsin Wang <yuhsingw@google.com> Tested-by: kokoro <noreply+kokoro@google.com>
This commit is contained in:
@@ -417,7 +417,7 @@ BaseRemoteGDB::listen()
|
||||
return;
|
||||
}
|
||||
|
||||
while (!listener.listen(_port, true)) {
|
||||
while (!listener.listen(_port)) {
|
||||
DPRINTF(GDBMisc, "Can't bind port %d\n", _port);
|
||||
_port++;
|
||||
}
|
||||
|
||||
@@ -185,24 +185,20 @@ ListenSocket::~ListenSocket()
|
||||
|
||||
// Create a socket and configure it for listening
|
||||
bool
|
||||
ListenSocket::listen(int port, bool reuse)
|
||||
ListenSocket::listen(int port)
|
||||
{
|
||||
if (listening)
|
||||
panic("Socket already listening!");
|
||||
panic_if(listening, "Socket already listening!");
|
||||
|
||||
// only create socket if not already created by a previous call
|
||||
if (fd == -1) {
|
||||
fd = socketCloexec(PF_INET, SOCK_STREAM, 0);
|
||||
if (fd < 0)
|
||||
panic("Can't create socket:%s !", strerror(errno));
|
||||
panic_if(fd < 0, "Can't create socket:%s !", strerror(errno));
|
||||
}
|
||||
|
||||
if (reuse) {
|
||||
int i = 1;
|
||||
if (::setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&i,
|
||||
sizeof(i)) < 0)
|
||||
panic("ListenSocket(listen): setsockopt() SO_REUSEADDR failed!");
|
||||
}
|
||||
int i = 1;
|
||||
int ret = ::setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
|
||||
panic_if(ret < 0,
|
||||
"ListenSocket(listen): setsockopt() SO_REUSEADDR failed!");
|
||||
|
||||
struct sockaddr_in sockaddr;
|
||||
sockaddr.sin_family = PF_INET;
|
||||
@@ -211,16 +207,16 @@ ListenSocket::listen(int port, bool reuse)
|
||||
sockaddr.sin_port = htons(port);
|
||||
// finally clear sin_zero
|
||||
std::memset(&sockaddr.sin_zero, 0, sizeof(sockaddr.sin_zero));
|
||||
int ret = ::bind(fd, (struct sockaddr *)&sockaddr, sizeof (sockaddr));
|
||||
ret = ::bind(fd, (struct sockaddr *)&sockaddr, sizeof (sockaddr));
|
||||
if (ret != 0) {
|
||||
if (ret == -1 && errno != EADDRINUSE)
|
||||
panic("ListenSocket(listen): bind() failed!");
|
||||
panic_if(ret == -1 && errno != EADDRINUSE,
|
||||
"ListenSocket(listen): bind() failed!");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (::listen(fd, 1) == -1) {
|
||||
if (errno != EADDRINUSE)
|
||||
panic("ListenSocket(listen): listen() failed!");
|
||||
panic_if(errno != EADDRINUSE,
|
||||
"ListenSocket(listen): listen() failed!");
|
||||
// User may decide to retry with a different port later; however, the
|
||||
// socket is already bound to a port and the next bind will surely
|
||||
// fail. We'll close the socket and reset fd to -1 so our user can
|
||||
|
||||
@@ -106,7 +106,7 @@ class ListenSocket
|
||||
|
||||
virtual int accept();
|
||||
|
||||
virtual bool listen(int port, bool reuse = true);
|
||||
virtual bool listen(int port);
|
||||
|
||||
int getfd() const { return fd; }
|
||||
bool islistening() const { return listening; }
|
||||
|
||||
@@ -162,19 +162,6 @@ TEST(SocketTest, ListenToPort)
|
||||
EXPECT_FALSE(listen_socket.allDisabled());
|
||||
}
|
||||
|
||||
TEST(SocketTest, ListenToPortReuseFalse)
|
||||
{
|
||||
MockListenSocket listen_socket;
|
||||
/*
|
||||
* The ListenSocket object should have the same state regardless as to
|
||||
* whether reuse is true or false (it is true by default).
|
||||
*/
|
||||
EXPECT_TRUE(listen_socket.listen(TestPort1, false));
|
||||
EXPECT_NE(-1, listen_socket.getfd());
|
||||
EXPECT_TRUE(listen_socket.islistening());
|
||||
EXPECT_FALSE(listen_socket.allDisabled());
|
||||
}
|
||||
|
||||
TEST(SocketTest, RelistenWithSameInstanceSamePort)
|
||||
{
|
||||
MockListenSocket listen_socket;
|
||||
@@ -185,7 +172,9 @@ TEST(SocketTest, RelistenWithSameInstanceSamePort)
|
||||
*/
|
||||
gtestLogOutput.str("");
|
||||
EXPECT_ANY_THROW(listen_socket.listen(TestPort1));
|
||||
std::string expected = "panic: Socket already listening!\n";
|
||||
std::string expected =
|
||||
"panic: panic condition listening occurred: "
|
||||
"Socket already listening!\n";
|
||||
std::string actual = gtestLogOutput.str();
|
||||
EXPECT_EQ(expected, actual);
|
||||
}
|
||||
@@ -201,7 +190,9 @@ TEST(SocketTest, RelistenWithSameInstanceDifferentPort)
|
||||
gtestLogOutput.str("");
|
||||
EXPECT_ANY_THROW(listen_socket.listen(TestPort2));
|
||||
|
||||
std::string expected = "panic: Socket already listening!\n";
|
||||
std::string expected =
|
||||
"panic: panic condition listening occurred: "
|
||||
"Socket already listening!\n";
|
||||
std::string actual = gtestLogOutput.str();
|
||||
EXPECT_EQ(expected, actual);
|
||||
}
|
||||
|
||||
@@ -164,7 +164,7 @@ VncServer::listen(int port)
|
||||
return;
|
||||
}
|
||||
|
||||
while (!listener.listen(port, true)) {
|
||||
while (!listener.listen(port)) {
|
||||
DPRINTF(VNC,
|
||||
"can't bind address vnc server port %d in use PID %d\n",
|
||||
port, getpid());
|
||||
|
||||
@@ -45,8 +45,7 @@ NativeTrace::NativeTrace(const Params &p)
|
||||
fatal("All listeners are disabled!");
|
||||
|
||||
int port = 8000;
|
||||
while (!native_listener.listen(port, true))
|
||||
{
|
||||
while (!native_listener.listen(port)) {
|
||||
DPRINTF(GDBMisc, "Can't bind port %d\n", port);
|
||||
port++;
|
||||
}
|
||||
|
||||
@@ -259,7 +259,7 @@ class TapListener
|
||||
void
|
||||
TapListener::listen()
|
||||
{
|
||||
while (!listener.listen(port, true)) {
|
||||
while (!listener.listen(port)) {
|
||||
DPRINTF(Ethernet, "TapListener(listen): Can't bind port %d\n", port);
|
||||
port++;
|
||||
}
|
||||
|
||||
@@ -175,7 +175,7 @@ Terminal::listen(int port)
|
||||
return;
|
||||
}
|
||||
|
||||
while (!listener.listen(port, true)) {
|
||||
while (!listener.listen(port)) {
|
||||
DPRINTF(Terminal,
|
||||
": can't bind address terminal port %d inuse PID %d\n",
|
||||
port, getpid());
|
||||
|
||||
Reference in New Issue
Block a user