1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 11:47:35 +00:00

LibCore: Add peer pid retrieval for LocalSocket

This will allow programs connected over unix sockets to retrieve the
pid of their peers in a nice way.
This commit is contained in:
Timothy 2021-07-01 01:58:34 -07:00 committed by Andreas Kling
parent c52ea3dad5
commit 972e5d7292
2 changed files with 26 additions and 0 deletions

View file

@ -53,6 +53,31 @@ LocalSocket::~LocalSocket()
{
}
pid_t LocalSocket::peer_pid() const
{
#ifdef AK_OS_MACOS
pid_t pid;
socklen_t pid_size = sizeof(pid);
if (getsockopt(fd(), SOL_LOCAL, LOCAL_PEERPID, &pid, &pid_size) < 0) {
dbgln("LocalSocket: getsockopt failed, {}", strerror(errno));
VERIFY_NOT_REACHED();
}
return pid;
#else
struct ucred creds = {};
socklen_t creds_size = sizeof(creds);
if (getsockopt(fd(), SOL_SOCKET, SO_PEERCRED, &creds, &creds_size) < 0) {
dbgln("LocalSocket: getsockopt failed, {}", strerror(errno));
VERIFY_NOT_REACHED();
}
return creds.pid;
#endif
}
HashMap<String, int> LocalSocket::s_overtaken_sockets {};
bool LocalSocket::s_overtaken_sockets_parsed { false };