Set close on exec for client socket
authorHwankyu Jhun <h.jhun@samsung.com>
Tue, 12 Sep 2023 05:58:06 +0000 (14:58 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Tue, 12 Sep 2023 06:31:54 +0000 (15:31 +0900)
To close the client socket fd, this patch sets O_CLOEXEC flag using fcntl().

Change-Id: I90f60c23ffc8aa0e62c93e26b4494493fb08eedb
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
src/client-socket-internal.cc
src/client-socket-internal.hh
src/server-socket-internal.cc

index c93234290a08961171cd01b1b9017e5b23198854..2e8fd95bb66631c943cc97cf5292062fe34b2fe4 100644 (file)
@@ -39,6 +39,7 @@ ClientSocket::ClientSocket() {
 }
 
 ClientSocket::ClientSocket(int fd) : fd_(fd) {
+  SetCloseOnExec();
 }
 
 ClientSocket::~ClientSocket() {
@@ -46,6 +47,12 @@ ClientSocket::~ClientSocket() {
     Close();
 }
 
+void ClientSocket::SetCloseOnExec() {
+  int flags = fcntl(fd_, F_GETFL, 0);
+  fcntl(fd_, F_SETFL, flags | O_CLOEXEC);
+  _I("Close on exec. fd(%d)", fd_);
+}
+
 void ClientSocket::Close() {
   if (fd_ > -1) {
     close(fd_);
index 2446e389b1f60e822fbb70e94693351b09fc0feb..772b45245afd26dc7d98e49232c59e8245ca1e8d 100644 (file)
@@ -42,6 +42,7 @@ class ClientSocket {
   int GetFd() const;
   int RemoveFd();
   void SetNonblock();
+  void SetCloseOnExec();
 
  private:
   int fd_;
index 713e908b87718c4c93f9faf9a183ffdff29aee56..5f58e34d0d7f4122d1dbc367d39b8b9ff0eb18bc 100644 (file)
@@ -71,7 +71,7 @@ int ServerSocket::Listen(int backlog) {
 void ServerSocket::SetCloseOnExec() {
   int flags = fcntl(fd_, F_GETFL, 0);
   fcntl(fd_, F_SETFL, flags | O_CLOEXEC);
-  _I("Close on exec");
+  _I("Close on exec. fd(%d)", fd_);
 }
 
 void ServerSocket::Close() {