mirror of
https://github.com/RGBCube/serenity
synced 2025-05-15 10:14:58 +00:00
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:
parent
23e802518d
commit
f93c0dc489
11 changed files with 27 additions and 20 deletions
|
@ -9,8 +9,7 @@ AClientConnection::AClientConnection()
|
||||||
|
|
||||||
void AClientConnection::handshake()
|
void AClientConnection::handshake()
|
||||||
{
|
{
|
||||||
auto response = send_sync<AudioServer::Greet>(getpid());
|
auto response = send_sync<AudioServer::Greet>();
|
||||||
set_server_pid(response->server_pid());
|
|
||||||
set_my_client_id(response->client_id());
|
set_my_client_id(response->client_id());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -20,8 +20,7 @@ GWindowServerConnection& GWindowServerConnection::the()
|
||||||
|
|
||||||
void GWindowServerConnection::handshake()
|
void GWindowServerConnection::handshake()
|
||||||
{
|
{
|
||||||
auto response = send_sync<WindowServer::Greet>(getpid());
|
auto response = send_sync<WindowServer::Greet>();
|
||||||
set_server_pid(response->server_pid());
|
|
||||||
set_my_client_id(response->client_id());
|
set_my_client_id(response->client_id());
|
||||||
GDesktop::the().did_receive_screen_rect({}, response->screen_rect());
|
GDesktop::the().did_receive_screen_rect({}, response->screen_rect());
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,6 +55,13 @@ public:
|
||||||
, m_socket(socket)
|
, m_socket(socket)
|
||||||
, m_client_id(client_id)
|
, 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);
|
add_child(socket);
|
||||||
m_socket->on_ready_to_read = [this] { drain_messages_from_client(); };
|
m_socket->on_ready_to_read = [this] { drain_messages_from_client(); };
|
||||||
}
|
}
|
||||||
|
@ -142,7 +149,6 @@ public:
|
||||||
|
|
||||||
int client_id() const { return m_client_id; }
|
int client_id() const { return m_client_id; }
|
||||||
pid_t client_pid() const { return m_client_pid; }
|
pid_t client_pid() const { return m_client_pid; }
|
||||||
void set_client_pid(pid_t pid) { m_client_pid = pid; }
|
|
||||||
|
|
||||||
virtual void die() = 0;
|
virtual void die() = 0;
|
||||||
|
|
||||||
|
|
|
@ -39,12 +39,19 @@ public:
|
||||||
usleep(10000);
|
usleep(10000);
|
||||||
--retries;
|
--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());
|
ASSERT(m_connection->is_connected());
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void handshake() = 0;
|
virtual void handshake() = 0;
|
||||||
|
|
||||||
void set_server_pid(pid_t pid) { m_server_pid = pid; }
|
|
||||||
pid_t server_pid() const { return m_server_pid; }
|
pid_t server_pid() const { return m_server_pid; }
|
||||||
void set_my_client_id(int id) { m_my_client_id = id; }
|
void set_my_client_id(int id) { m_my_client_id = id; }
|
||||||
int my_client_id() const { return m_my_client_id; }
|
int my_client_id() const { return m_my_client_id; }
|
||||||
|
|
|
@ -12,8 +12,7 @@ Client::Client()
|
||||||
|
|
||||||
void Client::handshake()
|
void Client::handshake()
|
||||||
{
|
{
|
||||||
auto response = send_sync<ProtocolServer::Greet>(getpid());
|
auto response = send_sync<ProtocolServer::Greet>();
|
||||||
set_server_pid(response->server_pid());
|
|
||||||
set_my_client_id(response->client_id());
|
set_my_client_id(response->client_id());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -48,10 +48,9 @@ void ASClientConnection::did_change_muted_state(Badge<ASMixer>, bool muted)
|
||||||
post_message(AudioClient::MutedStateChanged(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>(client_id());
|
||||||
return make<AudioServer::GreetResponse>(getpid(), client_id());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
OwnPtr<AudioServer::GetMainMixVolumeResponse> ASClientConnection::handle(const AudioServer::GetMainMixVolume&)
|
OwnPtr<AudioServer::GetMainMixVolumeResponse> ASClientConnection::handle(const AudioServer::GetMainMixVolume&)
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
endpoint AudioServer = 85
|
endpoint AudioServer = 85
|
||||||
{
|
{
|
||||||
// Basic protocol
|
// Basic protocol
|
||||||
Greet(i32 client_pid) => (i32 server_pid, i32 client_id)
|
Greet() => (i32 client_id)
|
||||||
|
|
||||||
// Mixer functions
|
// Mixer functions
|
||||||
SetMuted(bool muted) => ()
|
SetMuted(bool muted) => ()
|
||||||
|
|
|
@ -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()));
|
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>(client_id());
|
||||||
return make<ProtocolServer::GreetResponse>(getpid(), client_id());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
OwnPtr<ProtocolServer::DisownSharedBufferResponse> PSClientConnection::handle(const ProtocolServer::DisownSharedBuffer& message)
|
OwnPtr<ProtocolServer::DisownSharedBufferResponse> PSClientConnection::handle(const ProtocolServer::DisownSharedBuffer& message)
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
endpoint ProtocolServer = 9
|
endpoint ProtocolServer = 9
|
||||||
{
|
{
|
||||||
// Basic protocol
|
// 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
|
// FIXME: It would be nice if the kernel provided a way to avoid this
|
||||||
DisownSharedBuffer(i32 shared_buffer_id) => ()
|
DisownSharedBuffer(i32 shared_buffer_id) => ()
|
||||||
|
|
|
@ -598,10 +598,9 @@ void WSClientConnection::handle(const WindowServer::WM_SetWindowMinimized& messa
|
||||||
window.set_minimized(message.minimized());
|
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>(client_id(), WSScreen::the().rect());
|
||||||
return make<WindowServer::GreetResponse>(getpid(), client_id(), WSScreen::the().rect());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool WSClientConnection::is_showing_modal_window() const
|
bool WSClientConnection::is_showing_modal_window() const
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
endpoint WindowServer = 2
|
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)
|
CreateMenubar() => (i32 menubar_id)
|
||||||
DestroyMenubar(i32 menubar_id) => ()
|
DestroyMenubar(i32 menubar_id) => ()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue