net/SocketDescriptor: add GetPeerPidfd()

This commit is contained in:
Max Kellermann
2024-11-23 08:38:16 +01:00
committed by Max Kellermann
parent 78e643150f
commit 88fa68f030
2 changed files with 33 additions and 0 deletions

@ -8,6 +8,10 @@
#include "IPv6Address.hxx" #include "IPv6Address.hxx"
#include "UniqueSocketDescriptor.hxx" #include "UniqueSocketDescriptor.hxx"
#ifdef __linux__
#include "io/UniqueFileDescriptor.hxx"
#endif
#ifdef _WIN32 #ifdef _WIN32
#include <ws2tcpip.h> #include <ws2tcpip.h>
#else #else
@ -235,6 +239,24 @@ SocketDescriptor::GetPeerCredentials() const noexcept
#endif #endif
#ifdef __linux__
#ifndef SO_PEERPIDFD
#define SO_PEERPIDFD 77
#endif
UniqueFileDescriptor
SocketDescriptor::GetPeerPidfd() const noexcept
{
int pidfd;
if (GetOption(SOL_SOCKET, SO_PEERPIDFD, &pidfd, sizeof(pidfd)) < sizeof(pidfd))
return {};
return UniqueFileDescriptor{pidfd};
}
#endif // __linux__
#ifdef _WIN32 #ifdef _WIN32
bool bool

@ -25,6 +25,7 @@ class StaticSocketAddress;
class IPv4Address; class IPv4Address;
class IPv6Address; class IPv6Address;
class UniqueSocketDescriptor; class UniqueSocketDescriptor;
class UniqueFileDescriptor;
/** /**
* An OO wrapper for a Berkeley or WinSock socket descriptor. * An OO wrapper for a Berkeley or WinSock socket descriptor.
@ -229,6 +230,16 @@ public:
struct ucred GetPeerCredentials() const noexcept; struct ucred GetPeerCredentials() const noexcept;
#endif #endif
#ifdef __linux__
/**
* Get a pidfd for the peer process. Returns an undefined
* instance on error (with errno set).
*
* Requires Linux 6.5.
*/
UniqueFileDescriptor GetPeerPidfd() const noexcept;
#endif // __linux__
bool SetOption(int level, int name, bool SetOption(int level, int name,
const void *value, std::size_t size) const noexcept; const void *value, std::size_t size) const noexcept;