mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 07:17: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:
parent
c52ea3dad5
commit
972e5d7292
2 changed files with 26 additions and 0 deletions
|
@ -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 {};
|
HashMap<String, int> LocalSocket::s_overtaken_sockets {};
|
||||||
bool LocalSocket::s_overtaken_sockets_parsed { false };
|
bool LocalSocket::s_overtaken_sockets_parsed { false };
|
||||||
|
|
||||||
|
|
|
@ -16,6 +16,7 @@ public:
|
||||||
virtual ~LocalSocket() override;
|
virtual ~LocalSocket() override;
|
||||||
|
|
||||||
static RefPtr<LocalSocket> take_over_accepted_socket_from_system_server(String const& socket_path = String());
|
static RefPtr<LocalSocket> take_over_accepted_socket_from_system_server(String const& socket_path = String());
|
||||||
|
pid_t peer_pid() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
explicit LocalSocket(Object* parent = nullptr);
|
explicit LocalSocket(Object* parent = nullptr);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue