~pixelherodev/zyg

ref: 27187f8391f97f3e6f69df47ee5ad9337d9975a0 zyg/queue.c -rw-r--r-- 9.1 KiB
27187f83Noam Preil fixes 3 months ago
                                                                                
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <sys/select.h>

#include "c3/util.h"
#include "c3/stb.h"
#include "c3/tags.h"
#include "c3/c3.h"

#include "package.h"
#include "context.h"
#include "queue.h"
#include "parser.h"

struct job {
	enum {
		QUEUE_UNSTARTED,
		QUEUE_RUNNING,
		QUEUE_FAILED,
		QUEUE_SUCCESS,
		QUEUE_CRASHED,
	} state;
	// index into context trees list
	size_t index;
	// The rest of the structure is only defined if state == QUEUE_RUNNING
	pid_t pid;
	int in, out;
};

static struct{
	struct context *ctx;
	enum queue_mode mode;
	struct job *jobs;
	// if 1, the executing code is in a subprocess
	int in_child;
} state;

int
queue_init(struct context *ctx, enum queue_mode mode)
{
	state.ctx = ctx;
	state.mode = mode;
	state.jobs = NULL;
	return 1;
}

// -1 if none
static uint32_t
queue_find_tire(char *path)
{
	uint32_t i;
	for(i = 0; i < stb_sb_count(state.ctx->trees); i += 1)
		if(strcmp(state.ctx->trees[i].path, path) == 0)
			return i;
	return -1;
}

int
queue_job_parent(char *path)
{
	ssize_t len;
	char buf[2];
	buf[0] = 0;
	len = strlen(path);
	if(len > 0xFFFF)
		FATAL("string too long", 0);
	if(write(state.in_child, buf, 1) != 1)
		FATAL("Failed to write import message type", 0);
	buf[0] = len & 0xFF;
	buf[1] = (len >> 8) & 0xFF;
	if(write(state.in_child, buf, 2) != 2)
		FATAL("Failed to write import path size (%d)", len);
	if(write(state.in_child, path, len) != len)
		FATAL("Failed to write import path",0);
	return 0;
}

int
queue_job(char *path)
{
	struct job job;
	uint32_t i;
	struct pathed_tire p;
	if(state.in_child)
		return queue_job_parent(path);
	path = strdup(path);
	if(path == NULL)
		FATAL("Out of memory", 0);
	i = queue_find_tire(path);
	if(state.mode == QUEUE_PARSER){
		// Don't duplicate job in queue
		if(i != UINT32_MAX){
			free(path);
			return 1;
		}
		p.path = path;
		p.tree = NULL;
		i = stb_sb_count(state.ctx->trees);
		if(!stb_sb_ensure_capacity(&state.ctx->trees, 1, sizeof(struct pathed_tire))){
			ERROR("Out of memory", 0);
			free(path);
			return 0;
		}
		state.ctx->trees[i] = p;
		stb_sb_increment(state.ctx->trees, 1);
	}
	if(state.mode == QUEUE_ANALYSIS){
		if(i == UINT32_MAX){
			ERROR("Unable to find '%s' parsetree", path);
			free(path);
			return 0;
		}
		free(path);
	}
	job.state = QUEUE_UNSTARTED;
	job.index = i;
	stb_sb_ensure_capacity(&state.jobs, 1, sizeof(struct job));
	state.jobs[stb_sb_count(state.jobs)] = job;
	stb_sb_increment(state.jobs, 1);
	return 1;
}

uint32_t
queue_get_next(void)
{
	uint32_t i;
	for(i = 0; i < stb_sb_count(state.jobs); i += 1)
    		if(state.jobs[i].state < QUEUE_RUNNING)
        		return i;
	return -1;
}

void
job_parse(struct job *job, struct pathed_tire *p)
{
	p->tree = malloc(sizeof(c3ctx));
	if(p->tree == NULL){
		ERROR("Out of memory", 0);
		job->state = QUEUE_FAILED;
		return;
	}
	if(!parse(p->path, p->tree)){
    		ERROR("Parsing failed!", 0);
		job->state = QUEUE_FAILED;
		c3free(p->tree);
		p->tree = NULL;
		return;
	}
	job->state = QUEUE_SUCCESS;
}

int analysis(c3ctx *tree, int out);

void
job_analyze(struct job *job, struct pathed_tire *p)
{
	job->state = QUEUE_SUCCESS;
	if(!analysis(p->tree, -1))
		job->state = QUEUE_FAILED;
}

void
queue_parser_child(char *path, int out)
{
	char c = 1;
	c3ctx tree;
	if(!parse(path, &tree))
		FATAL("Parsing of '%s' failed",path);
	if(write(out, &c, 1) != 1)
		FATAL("Failed to write tree message type", 0);
	if(!c3write(&tree, out))
		FATAL("Failed to send tree to parent", 0);
}

void
queue_process(struct job *job)
{
	int from_child[2], to_child[2];
	struct pathed_tire p;
	int buf[2];
	if(job->state != QUEUE_UNSTARTED)
		FATAL("ICE: attempted to run initialized job", 0);
	if(pipe(from_child) != 0 || pipe(to_child) != 0){
		ERROR("Failed to create pipes", 0);
		job->state = QUEUE_CRASHED;
		return;
	}
	job->in = from_child[0];
	job->out = to_child[1];
	job->state = QUEUE_RUNNING;
	job->pid = fork();
	p = state.ctx->trees[job->index];
	fflush(NULL);
	if(job->pid == 0){
		state.in_child = from_child[1];
		if(state.mode == QUEUE_PARSER)
			queue_parser_child(p.path, from_child[1]);
		else
			if(!analysis(p.tree, from_child[1]))
				exit(1);
		if (read(to_child[0], buf, 1) != 1)
			FATAL("Failed to read suicide signal", 0);
		exit(0);
	} else if(job->pid == -1){
		ERROR("Failed to execute subprocess", 0);
		job->state = QUEUE_CRASHED;
	}
}

void
queue_execute(struct job *job)
{
	struct pathed_tire *p;
	p = &state.ctx->trees[job->index];
	if(state.mode == QUEUE_PARSER)
		job_parse(job, p);
	else
		job_analyze(job, p);
}

uint32_t
queue_active(void)
{
	uint32_t c, i;
	c = 0;
	for(i = 0; i < stb_sb_count(state.jobs); i += 1)
		if(state.jobs[i].state == QUEUE_RUNNING)
			c += 1;
	return c;
}

struct job *
queue_find_job(pid_t pid)
{
	uint32_t i;
	for(i = 0; i < stb_sb_count(state.jobs); i += 1)
		if(state.jobs[i].state == QUEUE_RUNNING && state.jobs[i].pid == pid)
			return &state.jobs[i];
	return NULL;
}

void
queue_fail(struct job *job, char *msg)
{
	ERROR("%s", msg);
	job->state = QUEUE_CRASHED;
	kill(job->pid, SIGKILL);
}

int
read_or_kill(struct job *job, char *buf, ssize_t len)
{
	if(read(job->in, buf, len) != len){
		queue_fail(job, "Failed to read pipe");
		free(buf);
		return 0;
	}
	return 1;
}

void
queue_handle_import(struct job *job)
{
	char *buf;
	char *b2;
	uint16_t len;
	buf = malloc(2);
	if(buf == NULL){
		queue_fail(job, "Out of memory");
		return;
	}
	if(read_or_kill(job, buf, 2) != 1)
		return;
	len = buf[0] | (buf[1] << 8);
	free(buf);
	buf = malloc(len + 1);
	if(buf == NULL){
		queue_fail(job, "Out of memory");
		return;
	}
	if(read_or_kill(job, buf, len) != 1)
		return;
	buf[len] = 0;
	b2 = h_path_resolve(buf);
	free(buf);
	if(!queue_job(b2))
		queue_fail(job, "Failed to queue imported file");
	free(b2);
}

void
queue_handle_tire(struct job *job)
{
	struct pathed_tire *p;
	char c = 3;
	p = &state.ctx->trees[job->index];
	if(p->tree)
		c3free(p->tree);
	p->tree = malloc(sizeof(c3ctx));
	if(p->tree == NULL){
		queue_fail(job, "Out of memory");
		return;
	}
	if(!c3read(p->tree, job->in)){
		queue_fail(job, "Failed to read tree!");
		c3free(p->tree);
		p->tree = NULL;
	}
	if (write(job->out, &c, 1) != 1)
		ERROR("Failed to signal subprocess to die", 0);
}

void
queue_handle_input(struct job *job)
{
	char *buf;
	char c;
	int flags;
	while(1){
		buf = malloc(2);
		if(buf == NULL){
			// Don't mark job as failed; we can retry later, since we didn't read anything.
			ERROR("Out of memory",0);
			break;
		}
		flags = fcntl(job->in, F_GETFL);
		if(flags == -1){
			ERROR("Failed to read flags",0);
			free(buf);
			break;
		}
		flags |= O_NONBLOCK;
		if(fcntl(job->in, F_SETFL, flags) != 0){
			ERROR("Failed to set NONBLOCK!", 0);
			free(buf);
			break;
		}
		c = read(job->in, buf, 1);
		if(c != 1){
			free(buf);
			break;
		}
		flags = flags & ~O_NONBLOCK;
		if(fcntl(job->in, F_SETFL, flags) != 0){
			queue_fail(job, "Failed to unset NONBLOCK");
			free(buf);
			break;
		}
		c = buf[0];
		free(buf);
		if(c == 0)
			queue_handle_import(job);
		else if(c == 1)
			queue_handle_tire(job);
		else{
			queue_fail(job, "Unrecognized message type");
			break;
		}
	}
}

void
queue_handle_input_all(void)
{
	fd_set ssd;
	int highest;
	uint32_t i;
	struct timeval v;
	struct job *job;
	v.tv_sec = 0;
	v.tv_usec = 10000;
	FD_ZERO(&ssd);
	highest = 0;
	for(i = 0; i < stb_sb_count(state.jobs); i += 1){
		job = &state.jobs[i];
		if(job->state == QUEUE_RUNNING && job->in > -1){
			FD_SET(job->in, &ssd);
			if(job->in >= highest)
				highest = job->in + 1;
		}
	}
	if(select(highest, &ssd, NULL, NULL, &v) <= 0)
		return;
	for(i = 0; i < stb_sb_count(state.jobs); i += 1){
		job = &state.jobs[i];
		if(job->state == QUEUE_RUNNING && job->in > -1 && FD_ISSET(job->in, &ssd))
			queue_handle_input(job);
	}
}

void
queue_handle_death(void)
{
	pid_t pid;
	struct job *job;
	int status;
	pid = waitpid(-1, &status, WNOHANG);
	if(pid > 0){
		job = queue_find_job(pid);
		if(job == NULL)
			FATAL("Unable to find job for pid %d\n", pid);
		job->state = QUEUE_SUCCESS;
		if(WIFEXITED(status) == 0 || WEXITSTATUS(status) != 0){
			job->state = WIFEXITED(status) == 0 ? QUEUE_CRASHED : QUEUE_FAILED;
			ERROR("Job %s: %d", job->state == QUEUE_CRASHED ? "crashed" : "failed", pid);
			return;
		}
	}
}

int
queue_status(void)
{
	uint32_t i, j;
	struct job *job;
	struct pathed_tire *p;
	j = 1;
	for(i = 0; i < stb_sb_count(state.jobs); i += 1){
		job = &state.jobs[i];
		p = &state.ctx->trees[job->index];
		if(job->state != QUEUE_SUCCESS){
			if(job->state == QUEUE_CRASHED)
				ERROR("ICE: Job for '%s' crashed!", p->path);
			j = 0;
		}
	}
	return j;
}

int
queue_wait(void)
{
	uint32_t i;
	struct timespec s;
	s.tv_sec = 0;
	s.tv_nsec = 10000;
	i = queue_get_next();
	while(i != UINT32_MAX || queue_active() > 0){
		if(state.ctx->single_process)
			queue_execute(&state.jobs[i]);
		else{
			if(queue_active() < 4 && i != UINT32_MAX)
				queue_process(&state.jobs[i]);
			queue_handle_death();
			queue_handle_input_all();
			nanosleep(&s, NULL);
		}
		i = queue_get_next();
	}
	return queue_status();
}

void
queue_free(void)
{
	stb_sb_free(state.jobs);
}