summaryrefslogtreecommitdiff
path: root/Userland/Services/InspectorServer/InspectableProcess.cpp
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2021-05-13 22:34:57 +0200
committerAndreas Kling <kling@serenityos.org>2021-05-13 23:28:40 +0200
commit3d3a5b431f1836b6905e5ac2408389916b3e1ce1 (patch)
tree59ea755abac22054ac43de3878b1fe694ed3a32d /Userland/Services/InspectorServer/InspectableProcess.cpp
parent3c3b384c807ebd58a4e437543debaf70b84aab39 (diff)
downloadserenity-3d3a5b431f1836b6905e5ac2408389916b3e1ce1.zip
Services: Add InspectorServer to reverse the direction of Inspector
This service daemon will act as an intermediary between the Inspector program and the inspectable programs it wants to inspect. Programs can make themselves available for inspection by connecting to /tmp/portal/inspectables using the Core::EventLoop RPC protocol.
Diffstat (limited to 'Userland/Services/InspectorServer/InspectableProcess.cpp')
-rw-r--r--Userland/Services/InspectorServer/InspectableProcess.cpp77
1 files changed, 77 insertions, 0 deletions
diff --git a/Userland/Services/InspectorServer/InspectableProcess.cpp b/Userland/Services/InspectorServer/InspectableProcess.cpp
new file mode 100644
index 0000000000..a064acd415
--- /dev/null
+++ b/Userland/Services/InspectorServer/InspectableProcess.cpp
@@ -0,0 +1,77 @@
+/*
+ * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include "InspectableProcess.h"
+
+namespace InspectorServer {
+
+HashMap<pid_t, NonnullOwnPtr<InspectableProcess>> g_processes;
+
+InspectableProcess* InspectableProcess::from_pid(pid_t pid)
+{
+ return g_processes.get(pid).value_or(nullptr);
+}
+
+InspectableProcess::InspectableProcess(pid_t pid, NonnullRefPtr<Core::LocalSocket> socket)
+ : m_pid(pid)
+ , m_socket(move(socket))
+{
+ m_socket->set_blocking(true);
+ m_socket->on_ready_to_read = [this] {
+ auto buffer = m_socket->read(1);
+ if (m_socket->eof()) {
+ g_processes.remove(m_pid);
+ return;
+ }
+ };
+}
+
+InspectableProcess::~InspectableProcess()
+{
+}
+
+String InspectableProcess::wait_for_response()
+{
+ if (m_socket->eof()) {
+ dbgln("InspectableProcess disconnected: PID {}", m_pid);
+ m_socket->close();
+ return {};
+ }
+
+ u32 length {};
+ auto nread = m_socket->read((u8*)&length, sizeof(length));
+ if (nread != sizeof(length)) {
+ dbgln("InspectableProcess got malformed data: PID {}", m_pid);
+ m_socket->close();
+ return {};
+ }
+
+ ByteBuffer data;
+ size_t remaining_bytes = length;
+
+ while (remaining_bytes) {
+ auto packet = m_socket->read(remaining_bytes);
+ if (packet.size() == 0)
+ break;
+ data.append(packet.data(), packet.size());
+ remaining_bytes -= packet.size();
+ }
+
+ VERIFY(data.size() == length);
+ dbgln("Got data size {} and read that many bytes", length);
+
+ return String::copy(data);
+}
+
+void InspectableProcess::send_request(JsonObject const& request)
+{
+ auto serialized = request.to_string();
+ auto length = serialized.length();
+ m_socket->write((u8 const*)&length, sizeof(length));
+ m_socket->write(serialized);
+}
+
+}