forked from rubinius/rubinius
/
agent.cpp
370 lines (299 loc) · 9.07 KB
/
agent.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
370
#include <string.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include "util/bert.hpp"
#include "vm.hpp"
#include "configuration.hpp"
#include "agent.hpp"
#include "exception.hpp"
#include "call_frame.hpp"
#include "config.h"
#include "agent_components.hpp"
#include "environment.hpp"
#include <ostream>
#include <sstream>
#include <fstream>
#ifdef __APPLE__
#include <mach-o/dyld.h>
#endif
namespace rubinius {
QueryAgent::QueryAgent(SharedState& shared, VM* state)
: Thread()
, shared_(shared)
, state_(state)
, running_(false)
, port_(0)
, server_fd_(-1)
, verbose_(false)
, max_fd_(0)
, vars_(0)
{
FD_ZERO(&fds_);
vars_ = new agent::VariableAccess(state, shared);
if(pipe(control_) != 0) {
perror("pipe");
rubinius::abort();
}
add_fd(read_control());
loopback_[0] = -1;
loopback_[1] = -1;
}
bool QueryAgent::setup_local() {
if(socketpair(AF_UNIX, SOCK_STREAM, 0, loopback_)) {
return false;
}
add_fd(loopback_[0]);
sockets_.push_back(loopback_[0]);
return true;
}
bool QueryAgent::bind(int port) {
if(server_fd_ > 0) return false;
server_fd_ = ::socket(AF_INET, SOCK_STREAM, 0);
if(server_fd_ == -1) {
std::cerr << "[QA: Unable to create socket: " << strerror(errno) << "]\n";
return false;
}
// To avoid TIME_WAIT / EADDRINUSE
int on = 1;
setsockopt(server_fd_, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
struct sockaddr_in sin = {0};
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port = htons(port);
if(::bind(server_fd_, (struct sockaddr*)&sin, sizeof(sin)) == -1) {
std::cerr << "[QA: Unable to bind socket: " << strerror(errno) << "]\n";
return false;
}
if(port == 0) {
socklen_t len = sizeof(sin);
if(getsockname(server_fd_, (struct sockaddr*)&sin, &len) == -1) {
std::cerr << "[QA: Unable to resolve random local port]\n";
return false;
}
port_ = ntohs(sin.sin_port);
} else {
port_ = port;
}
if(::listen(server_fd_, cBackLog) == -1) {
std::cerr << "[QA: Unable to listen on socket: " << strerror(errno) << "]\n";
return false;
}
if(verbose_) {
std::cerr << "[QA: Bound to port " << ntohs(sin.sin_port) << "]\n";
}
add_fd(server_fd_);
make_discoverable();
return true;
}
bool QueryAgent::process_commands(int client) {
bert::IOReader reader(client);
bert::Decoder<bert::IOReader> decoder(reader);
bert::IOWriter writer(client);
bert::Encoder<bert::IOWriter> encoder(writer);
int ver = decoder.read_version();
if(ver != 131) return false;
bert::Value* val = decoder.next_value();
if(!val || reader.eof_p()) return false;
encoder.write_version();
if(val->type() == bert::Tuple) {
bert::Value* cmd = val->get_element(0);
if(cmd->equal_atom("set_config")) {
if(val->elements()->size() == 3) {
bert::Value* key = val->get_element(1);
bert::Value* value = val->get_element(2);
if(key->type() == bert::Binary) {
agent::Output output(writer);
vars_->set_path(output, key->string(), value);
return true;
}
}
encoder.write_atom("error");
return true;
} else if(cmd->equal_atom("get_config")) {
if(val->elements()->size() == 2) {
bert::Value* key = val->get_element(1);
if(key->type() == bert::Binary) {
agent::Output output(writer);
vars_->read_path(output, key->string());
return true;
}
}
encoder.write_atom("error");
return true;
// If the client ask is the loopback client, give it some extra
// commands.
} else if(client == loopback_[0]) {
if(cmd->equal_atom("bind")) {
if(val->elements()->size() == 2) {
bert::Value* port = val->get_element(1);
if(port->integer_p()) {
if(bind(port->integer())) {
encoder.write_atom("ok");
return true;
}
}
}
encoder.write_atom("error");
return true;
}
}
} else if(val->equal_atom("close")) {
encoder.write_atom("bye");
return false;
}
encoder.write_atom("unknown");
return true;
}
void QueryAgent::wakeup() {
char buf = '!';
if(write(write_control(), &buf, 1) < 0) {
std::cerr << "[QA: Write error: " << strerror(errno) << "]\n";
}
}
void QueryAgent::perform() {
running_ = true;
while(1) {
fd_set read_fds = fds_;
int ret = select(max_fd_ + 1, &read_fds, 0, 0, 0);
if(ret < 0) {
if(errno == EINTR || errno == EAGAIN) continue;
std::cerr << "[QA: Select error: " << strerror(errno) << "]\n";
running_ = false;
return;
} else if(ret == 0) {
continue;
}
if(FD_ISSET(read_control(), &read_fds)) {
// Noop, just a wake up!
// Read the one byte written though so we don't clog up the
// pipe and have to use the ponies later.
char buf;
if(read(read_control(), &buf, 1) < 0) {
std::cerr << "[QA: Read error: " << strerror(errno) << "]\n";
}
} else if(server_fd_ > 0 && FD_ISSET(server_fd_, &read_fds)) {
// now accept an incoming connection
struct sockaddr_in sin;
socklen_t addr_size = sizeof(sin);
int client = accept(server_fd_, (struct sockaddr *)&sin, &addr_size);
if(verbose_) {
std::cerr << "[QA: Connection from " << inet_ntoa(sin.sin_addr)
<< ":" << ntohs(sin.sin_port) << "]\n";
}
bert::IOWriter writer(client);
bert::Encoder<bert::IOWriter> encoder(writer);
encoder.write_version();
encoder.write_tuple(2);
encoder.write_atom("hello_query_agent");
encoder.write_binary(RBX_HOST);
add_fd(client);
sockets_.push_back(client);
} else {
bool found = false;
for(std::vector<int>::iterator i = sockets_.begin();
i != sockets_.end(); /* nothing */) {
int client = *i;
if(FD_ISSET(client, &read_fds)) {
found = true;
if(!process_commands(client)) {
if(verbose_) {
struct sockaddr_in sin;
socklen_t len = sizeof(sin);
getpeername(*i, (struct sockaddr*)&sin, &len);
std::cerr << "[QA: Disconnected " << inet_ntoa(sin.sin_addr)
<< ":" << ntohs(sin.sin_port) << "]\n";
}
remove_fd(client);
close(client);
i = sockets_.erase(i);
continue;
}
}
i++;
}
if(!found) {
std::cerr << "[QA: Corruption in select set detected.]\n";
running_ = false;
return;
}
}
}
}
static char tmp_path[PATH_MAX];
static void remove_tmp_path(void) {
if(tmp_path[0]) unlink(tmp_path);
// Ignore any errors, this is happening at shutdown.
}
void QueryAgent::on_fork() {
// "clear" tmp_path
tmp_path[0] = 0;
}
void QueryAgent::cleanup() {
remove_tmp_path();
}
void QueryAgent::make_discoverable() {
// Create a tmp file containing the information to be used
// by console to find us.
const char* tmpdir = 0;
if(shared_.config.qa_tmpdir.value.size() > 0) {
tmpdir = shared_.config.qa_tmpdir.value.c_str();
} else {
tmpdir = getenv("TMPDIR");
}
if(tmpdir) {
std::ostringstream ss;
pid_t pid = getpid();
ss << tmpdir << "/rubinius-agent." << pid;
strcpy(tmp_path, ss.str().c_str());
std::ofstream stream(tmp_path);
// Couldn't open the path, for whatever reason.
if(!stream) return;
int argc = shared_.env()->argc();
char** argv = shared_.env()->argv();
if(stream) {
stream << pid << "\n";
stream << port() << "\n";
for(int i = 0; i < argc; i++) {
stream << argv[i] << " ";
}
stream << "\n";
char buf[PATH_MAX];
#ifdef __APPLE__
uint32_t size = PATH_MAX;
if(_NSGetExecutablePath(buf, &size) == 0) {
stream << buf << "\n";
} else if(realpath(argv[0], buf)) {
stream << buf << "\n";
} else {
stream << argv[0] << "\n";
}
#elif defined(__linux__)
{
std::ifstream exe("/proc/self/exe");
if(exe) {
char buf[PATH_MAX];
exe.get(buf, PATH_MAX);
stream << buf << "\n";
} else if(realpath(argv[0], buf)) {
stream << buf << "\n";
} else {
stream << argv[0] << "\n";
}
}
#else
if(realpath(argv[0], buf)) {
stream << buf << "\n";
} else {
stream << argv[0] << "\n";
}
#endif
stream.close();
atexit(remove_tmp_path);
}
}
}
}