diff options
author | Timothy <timmot@users.noreply.github.com> | 2021-07-01 01:58:34 -0700 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-07-01 17:49:18 +0200 |
commit | 972e5d72922dc8817c37c89796619d1de4f54e14 (patch) | |
tree | 1a843ec86f97e1dab7c43589f4440e4ad0982bdf /Userland/Libraries/LibCore | |
parent | c52ea3dad5a302a7abe6946fb37bd8b58990e011 (diff) | |
download | serenity-972e5d72922dc8817c37c89796619d1de4f54e14.zip |
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.
Diffstat (limited to 'Userland/Libraries/LibCore')
-rw-r--r-- | Userland/Libraries/LibCore/LocalSocket.cpp | 25 | ||||
-rw-r--r-- | Userland/Libraries/LibCore/LocalSocket.h | 1 |
2 files changed, 26 insertions, 0 deletions
diff --git a/Userland/Libraries/LibCore/LocalSocket.cpp b/Userland/Libraries/LibCore/LocalSocket.cpp index 27034e6c41..560a1d036c 100644 --- a/Userland/Libraries/LibCore/LocalSocket.cpp +++ b/Userland/Libraries/LibCore/LocalSocket.cpp @@ -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 }; diff --git a/Userland/Libraries/LibCore/LocalSocket.h b/Userland/Libraries/LibCore/LocalSocket.h index 73c7a89ca1..05b293b0ee 100644 --- a/Userland/Libraries/LibCore/LocalSocket.h +++ b/Userland/Libraries/LibCore/LocalSocket.h @@ -16,6 +16,7 @@ public: virtual ~LocalSocket() override; static RefPtr<LocalSocket> take_over_accepted_socket_from_system_server(String const& socket_path = String()); + pid_t peer_pid() const; private: explicit LocalSocket(Object* parent = nullptr); |