-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
port.cpp
369 lines (302 loc) · 8.66 KB
/
port.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#include <cassert>
#include <cstddef>
#include <cstring>
#include <iostream>
#include <memory>
#include <vector>
#include "BeDefs.h"
#include "haiku_errors.h"
#include "port.h"
#include "process.h"
#include "server_native.h"
#include "server_servercalls.h"
#include "server_workers.h"
#include "system.h"
Port::Port(int pid, int capacity, const char* name)
{
_info.capacity = capacity;
_info.team = pid;
strncpy(_info.name, name, sizeof(_info.name) - 1);
_info.name[sizeof(_info.name) - 1] = '\0';
_info.queue_count = 0;
_info.total_count = 0;
_info.port = 0;
}
void Port::Write(int code, std::vector<char>&& message)
{
_messages.emplace(code, std::move(message));
++_info.queue_count;
}
std::pair<int, std::vector<char>> Port::Read()
{
assert(_info.queue_count > 0);
--_info.queue_count;
++_info.total_count;
std::vector<char> resultData = std::move(_messages.front().second);
int resultCode = _messages.front().first;
_messages.pop();
return std::make_pair(resultCode, std::move(resultData));
}
intptr_t server_hserver_call_create_port(hserver_context& context, int32 queue_length, const char *name, size_t portNameLength)
{
if (queue_length < 1 || queue_length > HAIKU_PORT_MAX_QUEUE_LENGTH)
{
return B_BAD_VALUE;
}
auto buffer = std::string(portNameLength, '\0');
if (server_read_process_memory(context.pid, (void*)name, buffer.data(), portNameLength) != portNameLength)
{
return B_BAD_ADDRESS;
}
auto newPort = std::make_shared<Port>(context.pid, queue_length, buffer.c_str());
int id;
{
auto& system = System::GetInstance();
auto lock = system.Lock();
id = system.RegisterPort(std::move(newPort));
}
{
auto lock = context.process->Lock();
context.process->AddOwningPort(id);
}
return id;
}
intptr_t server_hserver_call_delete_port(hserver_context& context, int portId)
{
auto& system = System::GetInstance();
std::shared_ptr<Port> port;
{
auto lock = system.Lock();
port = system.GetPort(portId).lock();
if (!port)
{
return B_BAD_PORT_ID;
}
}
{
auto lock = context.process->Lock();
context.process->RemoveOwningPort(portId);
}
{
auto lock = system.Lock();
system.UnregisterPort(portId);
}
return B_OK;
}
intptr_t server_hserver_call_find_port(hserver_context& context, const char *port_name, size_t portNameLength)
{
auto buffer = std::string(portNameLength, '\0');
if (server_read_process_memory(context.pid, (void*)port_name, buffer.data(), portNameLength) != portNameLength)
{
return B_BAD_ADDRESS;
}
{
auto& system = System::GetInstance();
auto lock = system.Lock();
int result = system.FindPort(buffer);
if (result < 0)
{
return B_NAME_NOT_FOUND;
}
return result;
}
}
intptr_t server_hserver_call_get_port_info(hserver_context& context, port_id id, void *info)
{
{
auto& system = System::GetInstance();
auto lock = system.Lock();
auto port = system.GetPort(id).lock();
if (!port)
{
return B_BAD_PORT_ID;
}
if (server_write_process_memory(context.pid, info, &port->GetInfo(), sizeof(haiku_port_info))
!= sizeof(haiku_port_info))
{
return B_BAD_ADDRESS;
}
return B_OK;
}
}
intptr_t server_hserver_call_set_port_owner(hserver_context& context, port_id id, team_id team)
{
std::shared_ptr<Port> port;
std::shared_ptr<Process> oldOwner;
std::shared_ptr<Process> newOwner;
{
auto& system = System::GetInstance();
auto lock = system.Lock();
port = system.GetPort(id).lock();
if (!port)
{
return B_BAD_PORT_ID;
}
oldOwner = system.GetProcess(port->GetInfo().team).lock();
// Owner dead so port probably dead?
if (!oldOwner)
{
std::cerr << "Port " << port->GetName() << " owned by dead process " << port->GetInfo().team << std::endl;
return B_BAD_PORT_ID;
}
newOwner = system.GetProcess(team).lock();
if (!newOwner)
{
return B_BAD_TEAM_ID;
}
}
{
auto lock = oldOwner->Lock();
oldOwner->RemoveOwningPort(id);
}
{
auto lock = newOwner->Lock();
newOwner->AddOwningPort(id);
}
{
auto lock = port->Lock();
port->SetOwner(team);
}
return B_OK;
}
intptr_t server_hserver_call_write_port_etc(hserver_context& context, port_id id, int32 messageCode, const void *msgBuffer,
size_t bufferSize, uint32 flags, unsigned long long timeout)
{
std::weak_ptr<Port> weak_port;
{
auto& system = System::GetInstance();
auto lock = system.Lock();
weak_port = system.GetPort(id);
}
std::vector<char> message(bufferSize);
if (server_read_process_memory(context.pid, (void*)msgBuffer, message.data(), bufferSize)
!= bufferSize)
{
return B_BAD_ADDRESS;
}
{
auto port = weak_port.lock();
if (!port)
{
return B_BAD_PORT_ID;
}
{
auto lock = port->Lock();
if (port->GetInfo().queue_count < port->GetInfo().capacity)
{
port->Write(messageCode, std::move(message));
return B_OK;
}
}
}
bool useTimeout = (bool)(flags & B_TIMEOUT);
if (useTimeout && (timeout == 0))
{
return B_WOULD_BLOCK;
}
const int sleepTimeMicroseconds = 100 * 1000; // 100ms, in microseconds.
while (!useTimeout || (timeout > 0))
{
server_worker_sleep(sleepTimeMicroseconds);
{
auto port = weak_port.lock();
if (!port)
{
return B_BAD_PORT_ID;
}
{
auto lock = port->Lock();
if (port->GetInfo().queue_count < port->GetInfo().capacity)
{
port->Write(messageCode, std::move(message));
return B_OK;
}
}
}
if (timeout > sleepTimeMicroseconds)
{
timeout -= sleepTimeMicroseconds;
}
else
{
return B_TIMED_OUT;
}
}
// Should not reach here.
std::cerr << "server_hserver_call_write_port_etc: Impossible code path reached." << std::endl;
return B_BAD_DATA;
}
intptr_t server_hserver_call_read_port_etc(hserver_context& context,
port_id id, int32* userMessageCode, void *msgBuffer,
size_t bufferSize, uint32 flags, unsigned long long timeout)
{
bool useTimeout = (bool)(flags & B_TIMEOUT);
const int sleepTimeMicroseconds = 100 * 1000; // 100ms, in microseconds.
std::weak_ptr<Port> weak_port;
{
auto& system = System::GetInstance();
auto lock = system.Lock();
weak_port = system.GetPort(id);
}
int32 messageCode;
std::vector<char> message;
{
auto port = weak_port.lock();
if (!port)
{
return B_BAD_PORT_ID;
}
{
auto lock = port->Lock();
if (port->GetInfo().queue_count > 0)
{
std::tie(messageCode, message) = port->Read();
goto good;
}
}
}
if (useTimeout && (timeout == 0))
{
return B_WOULD_BLOCK;
}
while (!useTimeout || (timeout > 0))
{
server_worker_sleep(sleepTimeMicroseconds);
{
auto port = weak_port.lock();
if (!port)
{
return B_BAD_PORT_ID;
}
{
auto lock = port->Lock();
if (port->GetInfo().queue_count < port->GetInfo().capacity)
{
std::tie(messageCode, message) = port->Read();
goto good;
}
}
}
if (timeout > sleepTimeMicroseconds)
{
timeout -= sleepTimeMicroseconds;
}
else
{
return B_TIMED_OUT;
}
}
// Should not reach here.
std::cerr << "server_hserver_call_read_port_etc: Impossible code path reached." << std::endl;
return B_BAD_DATA;
good:
if (context.process->WriteMemory(&userMessageCode, &messageCode, sizeof(messageCode)) != sizeof(int32))
{
return B_BAD_ADDRESS;
}
if (context.process->WriteMemory(msgBuffer, message.data(), std::min(message.size(), bufferSize)) != message.size())
{
return B_BAD_ADDRESS;
}
return B_OK;
}