Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "RemoteProcess.h"
28#include "RemoteObject.h"
29#include "RemoteObjectGraphModel.h"
30#include "RemoteObjectPropertyModel.h"
31#include <stdio.h>
32#include <stdlib.h>
33
34RemoteProcess::RemoteProcess(pid_t pid)
35 : m_pid(pid)
36 , m_object_graph_model(RemoteObjectGraphModel::create(*this))
37 , m_socket(Core::LocalSocket::construct())
38{
39}
40
41void RemoteProcess::handle_identify_response(const JsonObject& response)
42{
43 int pid = response.get("pid").to_int();
44 ASSERT(pid == m_pid);
45
46 m_process_name = response.get("process_name").as_string_or({});
47
48 if (on_update)
49 on_update();
50}
51
52void RemoteProcess::handle_get_all_objects_response(const JsonObject& response)
53{
54 // FIXME: It would be good if we didn't have to make a local copy of the array value here!
55 auto objects = response.get("objects");
56 auto& object_array = objects.as_array();
57
58 NonnullOwnPtrVector<RemoteObject> remote_objects;
59 HashMap<String, RemoteObject*> objects_by_address;
60
61 for (auto& value : object_array.values()) {
62 ASSERT(value.is_object());
63 auto& object = value.as_object();
64 auto remote_object = make<RemoteObject>();
65 remote_object->address = object.get("address").to_string();
66 remote_object->parent_address = object.get("parent").to_string();
67 remote_object->name = object.get("name").to_string();
68 remote_object->class_name = object.get("class_name").to_string();
69 remote_object->json = object;
70 objects_by_address.set(remote_object->address, remote_object);
71 remote_objects.append(move(remote_object));
72 }
73
74 for (size_t i = 0; i < remote_objects.size(); ++i) {
75 auto& remote_object = remote_objects.ptr_at(i);
76 auto* parent = objects_by_address.get(remote_object->parent_address).value_or(nullptr);
77 if (!parent) {
78 m_roots.append(move(remote_object));
79 } else {
80 remote_object->parent = parent;
81 parent->children.append(move(remote_object));
82 }
83 }
84
85 m_object_graph_model->update();
86
87 if (on_update)
88 on_update();
89}
90
91void RemoteProcess::send_request(const JsonObject& request)
92{
93 auto serialized = request.to_string();
94 i32 length = serialized.length();
95 m_socket->write((const u8*)&length, sizeof(length));
96 m_socket->write(serialized);
97}
98
99void RemoteProcess::update()
100{
101 m_socket->on_connected = [this] {
102 dbg() << "Connected to PID " << m_pid;
103
104 {
105 JsonObject request;
106 request.set("type", "Identify");
107 send_request(request);
108 }
109
110 {
111 JsonObject request;
112 request.set("type", "GetAllObjects");
113 send_request(request);
114 }
115 };
116
117 m_socket->on_ready_to_read = [this] {
118 if (m_socket->eof()) {
119 dbg() << "Disconnected from PID " << m_pid;
120 m_socket->close();
121 return;
122 }
123
124 u32 length;
125 int nread = m_socket->read((u8*)&length, sizeof(length));
126 ASSERT(nread == sizeof(length));
127
128 auto data = m_socket->read(length);
129 ASSERT(data.size() == length);
130
131 dbg() << "Got packet size " << length << " and read that many bytes";
132
133 auto json_value = JsonValue::from_string(data);
134 ASSERT(json_value.is_object());
135
136 dbg() << "Got JSON response " << json_value.to_string();
137
138 auto& response = json_value.as_object();
139
140 auto response_type = response.get("type").as_string_or({});
141 if (response_type.is_null())
142 return;
143
144 if (response_type == "GetAllObjects") {
145 handle_get_all_objects_response(response);
146 return;
147 }
148
149 if (response_type == "Identify") {
150 handle_identify_response(response);
151 return;
152 }
153 };
154
155 auto success = m_socket->connect(Core::SocketAddress::local(String::format("/tmp/rpc.%d", m_pid)));
156 if (!success) {
157 fprintf(stderr, "Couldn't connect to PID %d\n", m_pid);
158 exit(1);
159 }
160}