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:32:39 +0000 (15:32 +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 4fbb3449f298ce22d2b4a3152ca699f4bf23143d..9b0a1072f5d80ace6260c1e913cd2dfbaf052f00 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 9f826405ab7249b92cc1b635857b2b1d83c2de5e..9fd7e4323d35bdeaa3ba39dd9296cb7fc1356095 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() {