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
371
372
373
374
375
376
377
|
// SPDX-License-Identifier: GPL-3.0-or-later
#include "spawn.h"
static uv_loop_t *loop;
static uv_pipe_t server_pipe;
static int server_shutdown = 0;
static uv_thread_t thread;
/* spawn outstanding execution structure */
static avl_tree_lock spawn_outstanding_exec_tree;
static char prot_buffer[MAX_COMMAND_LENGTH];
static unsigned prot_buffer_len = 0;
struct spawn_execution_info {
avl avl;
void *handle;
int exit_status;
pid_t pid;
struct spawn_execution_info *next;
};
int spawn_exec_compare(void *a, void *b)
{
struct spawn_execution_info *spwna = a, *spwnb = b;
if (spwna->pid < spwnb->pid) return -1;
if (spwna->pid > spwnb->pid) return 1;
return 0;
}
/* wake up waiter thread to reap the spawned processes */
static uv_mutex_t wait_children_mutex;
static uv_cond_t wait_children_cond;
static uint8_t spawned_processes;
static struct spawn_execution_info *child_waited_list;
static uv_async_t child_waited_async;
static inline struct spawn_execution_info *dequeue_child_waited_list(void)
{
struct spawn_execution_info *exec_info;
uv_mutex_lock(&wait_children_mutex);
if (NULL == child_waited_list) {
exec_info = NULL;
} else {
exec_info = child_waited_list;
child_waited_list = exec_info->next;
}
uv_mutex_unlock(&wait_children_mutex);
return exec_info;
}
static inline void enqueue_child_waited_list(struct spawn_execution_info *exec_info)
{
uv_mutex_lock(&wait_children_mutex);
exec_info->next = child_waited_list;
child_waited_list = exec_info;
uv_mutex_unlock(&wait_children_mutex);
}
static void after_pipe_write(uv_write_t *req, int status)
{
(void)status;
#ifdef SPAWN_DEBUG
fprintf(stderr, "SERVER %s called status=%d\n", __func__, status);
#endif
freez(req->data);
}
static void child_waited_async_cb(uv_async_t *async_handle)
{
uv_buf_t writebuf[2];
int ret;
struct spawn_execution_info *exec_info;
struct write_context *write_ctx;
(void)async_handle;
while (NULL != (exec_info = dequeue_child_waited_list())) {
write_ctx = mallocz(sizeof(*write_ctx));
write_ctx->write_req.data = write_ctx;
write_ctx->header.opcode = SPAWN_PROT_CMD_EXIT_STATUS;
write_ctx->header.handle = exec_info->handle;
write_ctx->exit_status.exec_exit_status = exec_info->exit_status;
writebuf[0] = uv_buf_init((char *) &write_ctx->header, sizeof(write_ctx->header));
writebuf[1] = uv_buf_init((char *) &write_ctx->exit_status, sizeof(write_ctx->exit_status));
#ifdef SPAWN_DEBUG
fprintf(stderr, "SERVER %s SPAWN_PROT_CMD_EXIT_STATUS\n", __func__);
#endif
ret = uv_write(&write_ctx->write_req, (uv_stream_t *) &server_pipe, writebuf, 2, after_pipe_write);
fatal_assert(ret == 0);
freez(exec_info);
}
}
static void wait_children(void *arg)
{
siginfo_t i;
struct spawn_execution_info tmp, *exec_info;
avl *ret_avl;
(void)arg;
while (!server_shutdown) {
uv_mutex_lock(&wait_children_mutex);
while (!spawned_processes) {
uv_cond_wait(&wait_children_cond, &wait_children_mutex);
}
spawned_processes = 0;
uv_mutex_unlock(&wait_children_mutex);
while (!server_shutdown) {
i.si_pid = 0;
if (waitid(P_ALL, (id_t) 0, &i, WEXITED) == -1) {
if (errno != ECHILD)
fprintf(stderr, "SPAWN: Failed to wait: %s\n", strerror(errno));
break;
}
if (i.si_pid == 0) {
fprintf(stderr, "SPAWN: No child exited.\n");
break;
}
#ifdef SPAWN_DEBUG
fprintf(stderr, "SPAWN: Successfully waited for pid:%d.\n", (int) i.si_pid);
#endif
fatal_assert(CLD_EXITED == i.si_code);
tmp.pid = (pid_t)i.si_pid;
while (NULL == (ret_avl = avl_remove_lock(&spawn_outstanding_exec_tree, (avl *)&tmp))) {
fprintf(stderr,
"SPAWN: race condition detected, waiting for child process %d to be indexed.\n",
(int)tmp.pid);
(void)sleep_usec(10000); /* 10 msec */
}
exec_info = (struct spawn_execution_info *)ret_avl;
exec_info->exit_status = i.si_status;
enqueue_child_waited_list(exec_info);
/* wake up event loop */
fatal_assert(0 == uv_async_send(&child_waited_async));
}
}
}
void spawn_protocol_execute_command(void *handle, char *command_to_run, uint16_t command_length)
{
uv_buf_t writebuf[2];
int ret;
avl *avl_ret;
struct spawn_execution_info *exec_info;
struct write_context *write_ctx;
write_ctx = mallocz(sizeof(*write_ctx));
write_ctx->write_req.data = write_ctx;
command_to_run[command_length] = '\0';
#ifdef SPAWN_DEBUG
fprintf(stderr, "SPAWN: executing command '%s'\n", command_to_run);
#endif
if (netdata_spawn(command_to_run, &write_ctx->spawn_result.exec_pid)) {
fprintf(stderr, "SPAWN: Cannot spawn(\"%s\", \"r\").\n", command_to_run);
write_ctx->spawn_result.exec_pid = 0;
} else { /* successfully spawned command */
write_ctx->spawn_result.exec_run_timestamp = now_realtime_sec();
/* record it for when the process finishes execution */
exec_info = mallocz(sizeof(*exec_info));
exec_info->handle = handle;
exec_info->pid = write_ctx->spawn_result.exec_pid;
avl_ret = avl_insert_lock(&spawn_outstanding_exec_tree, (avl *)exec_info);
fatal_assert(avl_ret == (avl *)exec_info);
/* wake up the thread that blocks waiting for processes to exit */
uv_mutex_lock(&wait_children_mutex);
spawned_processes = 1;
uv_cond_signal(&wait_children_cond);
uv_mutex_unlock(&wait_children_mutex);
}
write_ctx->header.opcode = SPAWN_PROT_SPAWN_RESULT;
write_ctx->header.handle = handle;
writebuf[0] = uv_buf_init((char *)&write_ctx->header, sizeof(write_ctx->header));
writebuf[1] = uv_buf_init((char *)&write_ctx->spawn_result, sizeof(write_ctx->spawn_result));
#ifdef SPAWN_DEBUG
fprintf(stderr, "SERVER %s SPAWN_PROT_SPAWN_RESULT\n", __func__);
#endif
ret = uv_write(&write_ctx->write_req, (uv_stream_t *)&server_pipe, writebuf, 2, after_pipe_write);
fatal_assert(ret == 0);
}
static void server_parse_spawn_protocol(unsigned source_len, char *source)
{
unsigned required_len;
struct spawn_prot_header *header;
struct spawn_prot_exec_cmd *payload;
uint16_t command_length;
while (source_len) {
required_len = sizeof(*header);
if (prot_buffer_len < required_len)
copy_to_prot_buffer(prot_buffer, &prot_buffer_len, required_len - prot_buffer_len, &source, &source_len);
if (prot_buffer_len < required_len
|