Fix SocketListener socket leak issue.
The problem was: if a socket is shared between SocketListener and another
thread, only if the last reference is removed by SocketListener can the socket
be closed, otherwise the socket will leak. This sometimes happens in netd's
dnsproxyd.
This change let the SocketClient own the socket and close the socket when
the SocketClient is destructed.
Change-Id: I2865fbfe9ee4d8b3e43d7e02919dbb2d261f70de
diff --git a/libsysutils/src/SocketClient.cpp b/libsysutils/src/SocketClient.cpp
index 90ca52e..722dcb2 100644
--- a/libsysutils/src/SocketClient.cpp
+++ b/libsysutils/src/SocketClient.cpp
@@ -10,8 +10,9 @@
#include <sysutils/SocketClient.h>
-SocketClient::SocketClient(int socket)
+SocketClient::SocketClient(int socket, bool owned)
: mSocket(socket)
+ , mSocketOwned(owned)
, mPid(-1)
, mUid(-1)
, mGid(-1)
@@ -32,6 +33,13 @@
}
}
+SocketClient::~SocketClient()
+{
+ if (mSocketOwned) {
+ close(mSocket);
+ }
+}
+
int SocketClient::sendMsg(int code, const char *msg, bool addErrno) {
char *buf;
const char* arg;