mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-12-28 21:54:40 +03:00
LibCore: Implement LocalSocket::peer_pid
Mostly a copy of Core::LocalSocket::peer_pid.
This commit is contained in:
parent
92be52fd9f
commit
4cad0dd74c
Notes:
sideshowbarker
2024-07-17 20:52:23 +09:00
Author: https://github.com/sin-ack Commit: https://github.com/SerenityOS/serenity/commit/4cad0dd74c2 Pull-request: https://github.com/SerenityOS/serenity/pull/11890
@ -568,6 +568,42 @@ ErrorOr<void> LocalSocket::send_fd(int fd)
|
||||
#endif
|
||||
}
|
||||
|
||||
ErrorOr<pid_t> LocalSocket::peer_pid() const
|
||||
{
|
||||
#ifdef AK_OS_MACOS
|
||||
pid_t pid;
|
||||
socklen_t pid_size = sizeof(pid);
|
||||
#elif defined(__FreeBSD__)
|
||||
struct xucred creds = {};
|
||||
socklen_t creds_size = sizeof(creds);
|
||||
#elif defined(__OpenBSD__)
|
||||
struct sockpeercred creds = {};
|
||||
socklen_t creds_size = sizeof(creds);
|
||||
#else
|
||||
struct ucred creds = {};
|
||||
socklen_t creds_size = sizeof(creds);
|
||||
#endif
|
||||
|
||||
#ifdef AK_OS_MACOS
|
||||
if (getsockopt(m_helper.fd(), SOL_LOCAL, LOCAL_PEERPID, &pid, &pid_size) < 0)
|
||||
#elif defined(__FreeBSD__)
|
||||
if (getsockopt(m_helper.fd(), SOL_LOCAL, LOCAL_PEERCRED, &creds, &creds_size) < 0)
|
||||
#else
|
||||
if (getsockopt(m_helper.fd(), SOL_SOCKET, SO_PEERCRED, &creds, &creds_size) < 0)
|
||||
#endif
|
||||
{
|
||||
return Error::from_syscall("getsockopt", -errno);
|
||||
}
|
||||
|
||||
#ifdef AK_OS_MACOS
|
||||
return pid;
|
||||
#elif defined(__FreeBSD__)
|
||||
return creds.cr_pid;
|
||||
#else
|
||||
return creds.pid;
|
||||
#endif
|
||||
}
|
||||
|
||||
ErrorOr<size_t> LocalSocket::read_without_waiting(Bytes buffer)
|
||||
{
|
||||
return m_helper.read(buffer, MSG_DONTWAIT);
|
||||
|
@ -413,6 +413,7 @@ public:
|
||||
|
||||
ErrorOr<int> receive_fd(int flags);
|
||||
ErrorOr<void> send_fd(int fd);
|
||||
ErrorOr<pid_t> peer_pid() const;
|
||||
ErrorOr<size_t> read_without_waiting(Bytes buffer);
|
||||
|
||||
virtual ~LocalSocket() { close(); }
|
||||
|
Loading…
Reference in New Issue
Block a user