LibIPC: Get client/server PIDs using getsockopt(SO_PEERCRED)

Instead of passing the PIDs back and forth in a handshake "Greet"
message, just use getsockopt(SO_PEERCRED) on both sides to get the same
information from the kernel.

This is a nice little simplification of the IPC protocol, although it
does not get rid of the handshake since we still have to pass the
"client ID" from the server to each client so they know how to refer
to themselves. This might not be necessary and we might be able to get
rid of this later on.
This commit is contained in:
Andreas Kling 2019-12-06 18:39:59 +01:00
parent 23e802518d
commit f93c0dc489
Notes: sideshowbarker 2024-07-19 10:57:18 +09:00
11 changed files with 27 additions and 20 deletions

View file

@ -9,8 +9,7 @@ AClientConnection::AClientConnection()
void AClientConnection::handshake()
{
auto response = send_sync<AudioServer::Greet>(getpid());
set_server_pid(response->server_pid());
auto response = send_sync<AudioServer::Greet>();
set_my_client_id(response->client_id());
}

View file

@ -20,8 +20,7 @@ GWindowServerConnection& GWindowServerConnection::the()
void GWindowServerConnection::handshake()
{
auto response = send_sync<WindowServer::Greet>(getpid());
set_server_pid(response->server_pid());
auto response = send_sync<WindowServer::Greet>();
set_my_client_id(response->client_id());
GDesktop::the().did_receive_screen_rect({}, response->screen_rect());
}

View file

@ -55,6 +55,13 @@ public:
, m_socket(socket)
, m_client_id(client_id)
{
ASSERT(socket.is_connected());
ucred creds;
socklen_t creds_size = sizeof(creds);
if (getsockopt(m_socket->fd(), SOL_SOCKET, SO_PEERCRED, &creds, &creds_size) < 0) {
ASSERT_NOT_REACHED();
}
m_client_pid = creds.pid;
add_child(socket);
m_socket->on_ready_to_read = [this] { drain_messages_from_client(); };
}
@ -142,7 +149,6 @@ public:
int client_id() const { return m_client_id; }
pid_t client_pid() const { return m_client_pid; }
void set_client_pid(pid_t pid) { m_client_pid = pid; }
virtual void die() = 0;

View file

@ -39,12 +39,19 @@ public:
usleep(10000);
--retries;
}
ucred creds;
socklen_t creds_size = sizeof(creds);
if (getsockopt(m_connection->fd(), SOL_SOCKET, SO_PEERCRED, &creds, &creds_size) < 0) {
ASSERT_NOT_REACHED();
}
m_server_pid = creds.pid;
ASSERT(m_connection->is_connected());
}
virtual void handshake() = 0;
void set_server_pid(pid_t pid) { m_server_pid = pid; }
pid_t server_pid() const { return m_server_pid; }
void set_my_client_id(int id) { m_my_client_id = id; }
int my_client_id() const { return m_my_client_id; }

View file

@ -12,8 +12,7 @@ Client::Client()
void Client::handshake()
{
auto response = send_sync<ProtocolServer::Greet>(getpid());
set_server_pid(response->server_pid());
auto response = send_sync<ProtocolServer::Greet>();
set_my_client_id(response->client_id());
}

View file

@ -48,10 +48,9 @@ void ASClientConnection::did_change_muted_state(Badge<ASMixer>, bool muted)
post_message(AudioClient::MutedStateChanged(muted));
}
OwnPtr<AudioServer::GreetResponse> ASClientConnection::handle(const AudioServer::Greet& message)
OwnPtr<AudioServer::GreetResponse> ASClientConnection::handle(const AudioServer::Greet&)
{
set_client_pid(message.client_pid());
return make<AudioServer::GreetResponse>(getpid(), client_id());
return make<AudioServer::GreetResponse>(client_id());
}
OwnPtr<AudioServer::GetMainMixVolumeResponse> ASClientConnection::handle(const AudioServer::GetMainMixVolume&)

View file

@ -1,7 +1,7 @@
endpoint AudioServer = 85
{
// Basic protocol
Greet(i32 client_pid) => (i32 server_pid, i32 client_id)
Greet() => (i32 client_id)
// Mixer functions
SetMuted(bool muted) => ()

View file

@ -65,10 +65,9 @@ void PSClientConnection::did_progress_download(Badge<Download>, Download& downlo
post_message(ProtocolClient::DownloadProgress(download.id(), download.total_size(), download.downloaded_size()));
}
OwnPtr<ProtocolServer::GreetResponse> PSClientConnection::handle(const ProtocolServer::Greet& message)
OwnPtr<ProtocolServer::GreetResponse> PSClientConnection::handle(const ProtocolServer::Greet&)
{
set_client_pid(message.client_pid());
return make<ProtocolServer::GreetResponse>(getpid(), client_id());
return make<ProtocolServer::GreetResponse>(client_id());
}
OwnPtr<ProtocolServer::DisownSharedBufferResponse> PSClientConnection::handle(const ProtocolServer::DisownSharedBuffer& message)

View file

@ -1,7 +1,7 @@
endpoint ProtocolServer = 9
{
// Basic protocol
Greet(i32 client_pid) => (i32 server_pid, i32 client_id)
Greet() => (i32 client_id)
// FIXME: It would be nice if the kernel provided a way to avoid this
DisownSharedBuffer(i32 shared_buffer_id) => ()

View file

@ -598,10 +598,9 @@ void WSClientConnection::handle(const WindowServer::WM_SetWindowMinimized& messa
window.set_minimized(message.minimized());
}
OwnPtr<WindowServer::GreetResponse> WSClientConnection::handle(const WindowServer::Greet& message)
OwnPtr<WindowServer::GreetResponse> WSClientConnection::handle(const WindowServer::Greet&)
{
set_client_pid(message.client_pid());
return make<WindowServer::GreetResponse>(getpid(), client_id(), WSScreen::the().rect());
return make<WindowServer::GreetResponse>(client_id(), WSScreen::the().rect());
}
bool WSClientConnection::is_showing_modal_window() const

View file

@ -1,6 +1,6 @@
endpoint WindowServer = 2
{
Greet(i32 client_pid) => (i32 server_pid, i32 client_id, Rect screen_rect)
Greet() => (i32 client_id, Rect screen_rect)
CreateMenubar() => (i32 menubar_id)
DestroyMenubar(i32 menubar_id) => ()