sockets should use CLOEXEC ticket cool#8016

Change-Id: I5f59ed9446a1fda301bb4791f98ca7c1c137e606
Signed-off-by: Michael Meeks <michael.meeks@collabora.com>
This commit is contained in:
Michael Meeks 2024-01-27 12:30:22 +00:00
parent 65bd383000
commit fa80ecd2d0
4 changed files with 12 additions and 9 deletions

View file

@ -18,9 +18,12 @@
#include <poll.h>
#ifndef __linux__
#ifndef SOCK_NONBLOCK
#define SOCK_NONBLOCK 0x100
#endif
# ifndef SOCK_NONBLOCK
# define SOCK_NONBLOCK 0x100
# endif
# ifndef SOCK_CLOEXEC
# define SOCK_CLOEXEC 0x200
# endif
#endif
void fakeSocketSetLoggingCallback(void (*)(const std::string&));

View file

@ -119,7 +119,7 @@ connect(const std::string& host, const std::string& port, const bool isSSL,
if (ai->ai_addrlen && ai->ai_addr)
{
int fd = ::socket(ai->ai_addr->sa_family, SOCK_STREAM | SOCK_NONBLOCK, 0);
int fd = ::socket(ai->ai_addr->sa_family, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
if (fd < 0)
{
LOG_SYS("Failed to create socket");

View file

@ -72,7 +72,7 @@ int Socket::createSocket(Socket::Type type)
default: assert(!"Unknown Socket::Type"); break;
}
return socket(domain, SOCK_STREAM | SOCK_NONBLOCK, 0);
return socket(domain, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
#else
(void) type;
return fakeSocketSocket();
@ -587,7 +587,7 @@ bool SocketPoll::insertNewUnixSocket(
const std::vector<int>* shareFDs)
{
LOG_DBG("Connecting to local UDS " << location);
const int fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0);
const int fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
if (fd < 0)
{
LOG_SYS("Failed to connect to unix socket at " << location);
@ -889,7 +889,7 @@ std::shared_ptr<Socket> ServerSocket::accept()
struct sockaddr_in6 clientInfo;
socklen_t addrlen = sizeof(clientInfo);
const int rc = ::accept4(getFD(), (struct sockaddr *)&clientInfo, &addrlen, SOCK_NONBLOCK);
const int rc = ::accept4(getFD(), (struct sockaddr *)&clientInfo, &addrlen, SOCK_NONBLOCK | SOCK_CLOEXEC);
#else
const int rc = fakeSocketAccept4(getFD());
#endif
@ -982,7 +982,7 @@ bool Socket::isLocal() const
std::shared_ptr<Socket> LocalServerSocket::accept()
{
const int rc = ::accept4(getFD(), nullptr, nullptr, SOCK_NONBLOCK);
const int rc = ::accept4(getFD(), nullptr, nullptr, SOCK_NONBLOCK | SOCK_CLOEXEC);
try
{
LOG_DBG("Accepted prisoner socket #" << rc << ", creating socket object.");

View file

@ -271,7 +271,7 @@ inline int connectToLocalServer(int portNumber, int socketTimeOutMS, bool blocki
int socketFD = 0;
struct sockaddr_in serv_addr;
if ((socketFD = socket(AF_INET, SOCK_STREAM, 0)) < 0)
if ((socketFD = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0)) < 0)
{
LOG_ERR("helpers::connectToLocalServer: Server client could not be created.");
return -1;