summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGunnar Beutner <gbeutner@serenityos.org>2021-05-16 12:13:19 +0200
committerAndreas Kling <kling@serenityos.org>2021-05-17 13:32:19 +0200
commit07341c359487c45e6c8a8187f18d5271536c798b (patch)
tree95f24629e5c3108ee7af3fb2104ef08a662d1001
parent89956cb0d66f24bfcee312dcd35f78b5dbc9e1b9 (diff)
downloadserenity-07341c359487c45e6c8a8187f18d5271536c798b.zip
LibCore: Close accepted sockets on exec() and make them non-blocking
Previously accept() would copy the listener socket's cloexec and non-blocking flag. With that fixed however TCPServer and LocalServer now leak file descriptors into child processes and are blocking.
-rw-r--r--Base/res/icons/16x16/app-analog-clock.pngbin357 -> 10480 bytes
-rw-r--r--Userland/Libraries/LibCore/LocalServer.cpp10
-rw-r--r--Userland/Libraries/LibCore/TCPServer.cpp13
3 files changed, 22 insertions, 1 deletions
diff --git a/Base/res/icons/16x16/app-analog-clock.png b/Base/res/icons/16x16/app-analog-clock.png
index e57522add3..42d63fbda7 100644
--- a/Base/res/icons/16x16/app-analog-clock.png
+++ b/Base/res/icons/16x16/app-analog-clock.png
Binary files differ
diff --git a/Userland/Libraries/LibCore/LocalServer.cpp b/Userland/Libraries/LibCore/LocalServer.cpp
index 1d3d6099ca..1acb063cf1 100644
--- a/Userland/Libraries/LibCore/LocalServer.cpp
+++ b/Userland/Libraries/LibCore/LocalServer.cpp
@@ -141,12 +141,22 @@ RefPtr<LocalSocket> LocalServer::accept()
VERIFY(m_listening);
sockaddr_un un;
socklen_t un_size = sizeof(un);
+#ifndef AK_OS_MACOS
+ int accepted_fd = ::accept4(m_fd, (sockaddr*)&un, &un_size, SOCK_NONBLOCK | SOCK_CLOEXEC);
+#else
int accepted_fd = ::accept(m_fd, (sockaddr*)&un, &un_size);
+#endif
if (accepted_fd < 0) {
perror("accept");
return nullptr;
}
+#ifdef AK_OS_MACOS
+ int option = 1;
+ ioctl(m_fd, FIONBIO, &option);
+ (void)fcntl(accepted_fd, F_SETFD, FD_CLOEXEC);
+#endif
+
return LocalSocket::construct(accepted_fd);
}
diff --git a/Userland/Libraries/LibCore/TCPServer.cpp b/Userland/Libraries/LibCore/TCPServer.cpp
index f242e9165b..97b4d397b0 100644
--- a/Userland/Libraries/LibCore/TCPServer.cpp
+++ b/Userland/Libraries/LibCore/TCPServer.cpp
@@ -9,12 +9,12 @@
#include <LibCore/Notifier.h>
#include <LibCore/TCPServer.h>
#include <LibCore/TCPSocket.h>
+#include <fcntl.h>
#include <stdio.h>
#include <sys/socket.h>
#include <unistd.h>
#ifndef SOCK_NONBLOCK
-# include <fcntl.h>
# include <sys/ioctl.h>
#endif
namespace Core {
@@ -69,12 +69,23 @@ RefPtr<TCPSocket> TCPServer::accept()
VERIFY(m_listening);
sockaddr_in in;
socklen_t in_size = sizeof(in);
+#ifndef AK_OS_MACOS
+ int accepted_fd = ::accept4(m_fd, (sockaddr*)&in, &in_size, SOCK_NONBLOCK | SOCK_CLOEXEC);
+#else
int accepted_fd = ::accept(m_fd, (sockaddr*)&in, &in_size);
+#endif
+
if (accepted_fd < 0) {
perror("accept");
return nullptr;
}
+#ifdef AK_OS_MACOS
+ int option = 1;
+ (void)ioctl(m_fd, FIONBIO, &option);
+ (void)fcntl(accepted_fd, F_SETFD, FD_CLOEXEC);
+#endif
+
return TCPSocket::construct(accepted_fd);
}