mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-12-28 21:54:40 +03:00
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.
This commit is contained in:
parent
89956cb0d6
commit
07341c3594
Notes:
sideshowbarker
2024-07-18 17:57:53 +09:00
Author: https://github.com/gunnarbeutner Commit: https://github.com/SerenityOS/serenity/commit/07341c35948 Pull-request: https://github.com/SerenityOS/serenity/pull/7173 Reviewed-by: https://github.com/awesomekling
Binary file not shown.
Before Width: | Height: | Size: 357 B After Width: | Height: | Size: 10 KiB |
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user