-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathexec.cc
537 lines (450 loc) · 12.1 KB
/
exec.cc
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
/******************************************************************************
Copyright 2011 Todd Sundsted. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY TODD SUNDSTED ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
EVENT SHALL TODD SUNDSTED OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are
those of the authors and should not be interpreted as representing official
policies, either expressed or implied, of Todd Sundsted.
*****************************************************************************/
#include <errno.h>
#include <sys/stat.h>
#include "my-fcntl.h"
#include "my-signal.h"
#include "my-stdlib.h"
#include "my-string.h"
#include "my-unistd.h"
#include "net_multi.h"
#include "exec.h"
#include "functions.h"
#include "list.h"
#include "log.h"
#include "storage.h"
#include "structures.h"
#include "streams.h"
#include "tasks.h"
#include "utils.h"
typedef enum {
TWS_CONTINUE, /* The task is running and has not yet
* stopped or been killed.
*/
TWS_STOP, /* The task has stopped. This status
* is final.
*/
TWS_KILL /* The task has been killed. This
* status is final.
*/
} task_waiting_status;
typedef struct task_waiting_on_exec {
const char *cmd;
const char **args;
const char *in;
int len;
pid_t pid;
task_waiting_status status;
int code;
int fin;
int fout;
int ferr;
Stream *sout;
Stream *serr;
vm the_vm;
} task_waiting_on_exec;
static task_waiting_on_exec *process_table[EXEC_MAX_PROCESSES];
volatile static sig_atomic_t sigchild_interrupt = 0;
static sigset_t block_sigchld;
#define BLOCK_SIGCHLD sigprocmask(SIG_BLOCK, &block_sigchld, NULL)
#define UNBLOCK_SIGCHLD sigprocmask(SIG_UNBLOCK, &block_sigchld, NULL)
static task_waiting_on_exec *
malloc_task_waiting_on_exec()
{
task_waiting_on_exec *tw =
(task_waiting_on_exec *)mymalloc(sizeof(task_waiting_on_exec), M_TASK);
tw->cmd = NULL;
tw->args = NULL;
tw->in = NULL;
tw->status = TWS_CONTINUE;
tw->code = 0;
tw->sout = new_stream(1000);
tw->serr = new_stream(1000);
return tw;
}
static void
free_task_waiting_on_exec(task_waiting_on_exec * tw)
{
int i;
if (tw->cmd)
free_str(tw->cmd);
if (tw->args) {
for (i = 0; tw->args[i]; i++)
free_str(tw->args[i]);
myfree(tw->args, M_ARRAY);
}
if (tw->in)
free_str(tw->in);
close(tw->fout);
close(tw->ferr);
network_unregister_fd(tw->fout);
network_unregister_fd(tw->ferr);
if (tw->sout)
free_stream(tw->sout);
if (tw->serr)
free_stream(tw->serr);
myfree(tw, M_TASK);
}
static task_enum_action
exec_waiter_enumerator(task_closure closure, void *data)
{
task_enum_action action = TEA_CONTINUE;
BLOCK_SIGCHLD;
int i;
for (i = 0; i < EXEC_MAX_PROCESSES; i++) {
if (process_table[i]) {
if (TWS_KILL != process_table[i]->status) {
action = (*closure) (process_table[i]->the_vm,
process_table[i]->cmd,
data);
if (TEA_KILL == action)
process_table[i]->status = TWS_KILL;
if (TEA_CONTINUE != action)
break;
}
}
}
UNBLOCK_SIGCHLD;
return action;
}
static int
write_all(int fd, const char *buffer, size_t length)
{
ssize_t count;
while (length) {
if ((count = write(fd, buffer, length)) < 0)
return -1;
buffer += count;
length -= count;
}
fsync(fd);
return 1;
}
static void
stdout_readable(int fd, void *data)
{
task_waiting_on_exec *tw = (task_waiting_on_exec *)data;
char buffer[1000];
int n;
while ((n = read(fd, buffer, sizeof(buffer))) > 0) {
stream_add_string(tw->sout, raw_bytes_to_binary(buffer, n));
}
}
static void
stderr_readable(int fd, void *data)
{
task_waiting_on_exec *tw = (task_waiting_on_exec *)data;
char buffer[1000];
int n;
while ((n = read(fd, buffer, sizeof(buffer))) > 0) {
stream_add_string(tw->serr, raw_bytes_to_binary(buffer, n));
}
}
static pid_t
fork_and_exec(const char *cmd, const char *const args[], const char *const env[],
int *in, int *out, int *err)
{
pid_t pid;
int pipeIn[2];
int pipeOut[2];
int pipeErr[2];
if (pipe(pipeIn) < 0) {
log_perror("EXEC: Couldn't create pipe - in");
goto fail;
}
else if (pipe(pipeOut) < 0) {
log_perror("EXEC: Couldn't create pipe - out");
goto close_in;
}
else if (pipe(pipeErr) < 0) {
log_perror("EXEC: Couldn't create pipe - err");
goto close_out;
}
else if ((pid = fork()) < 0) {
log_perror("EXEC: Couldn't fork");
goto close_err;
}
else if (0 == pid) { /* child */
int status;
if ((status = dup2(pipeIn[0], STDIN_FILENO)) < 0) {
perror("dup2");
exit(status);
}
if ((status = dup2(pipeOut[1], STDOUT_FILENO)) < 0) {
perror("dup2");
exit(status);
}
if ((status = dup2(pipeErr[1], STDERR_FILENO)) < 0) {
perror("dup2");
exit(status);
}
close(pipeIn[1]);
close(pipeOut[0]);
close(pipeErr[0]);
status = execve(cmd, (char *const *)args, (char *const *)env);
perror("execve");
exit(status);
}
close(pipeIn[0]);
close(pipeOut[1]);
close(pipeErr[1]);
*in = pipeIn[1];
*out = pipeOut[0];
*err = pipeErr[0];
return pid;
close_err:
close(pipeErr[0]);
close(pipeErr[1]);
close_out:
close(pipeOut[0]);
close(pipeOut[1]);
close_in:
close(pipeIn[0]);
close(pipeIn[1]);
fail:
return 0;
}
static int
set_nonblocking(int fd)
{
int flags;
if ((flags = fcntl(fd, F_GETFL, 0)) < 0
|| fcntl(fd, F_SETFL, flags | NONBLOCK_FLAG) < 0)
return 0;
else
return 1;
}
static enum error
exec_waiter_suspender(vm the_vm, void *data)
{
task_waiting_on_exec *tw = (task_waiting_on_exec *)data;
enum error error = E_QUOTA;
BLOCK_SIGCHLD;
int i;
for (i = 0; i < EXEC_MAX_PROCESSES; i++) {
if (process_table[i] == NULL) {
process_table[i] = tw;
break;
}
}
if (i == EXEC_MAX_PROCESSES) {
error = E_QUOTA;
goto free_task_waiting_on_exec;
}
static const char *env[] = { "PATH=/bin:/usr/bin", NULL };
if ((tw->pid = fork_and_exec(tw->cmd, tw->args, env, &tw->fin, &tw->fout, &tw->ferr)) == 0) {
error = E_EXEC;
goto clear_process_slot;
}
oklog("EXEC: %s (%d)...\n", tw->cmd, tw->pid);
set_nonblocking(tw->fin);
set_nonblocking(tw->fout);
set_nonblocking(tw->ferr);
if (tw->in) {
if (write_all(tw->fin, tw->in, tw->len) < 0) {
error = E_EXEC;
goto close_fin;
}
}
close(tw->fin);
network_register_fd(tw->fout, stdout_readable, NULL, tw);
network_register_fd(tw->ferr, stderr_readable, NULL, tw);
tw->the_vm = the_vm;
/* success */
UNBLOCK_SIGCHLD;
return E_NONE;
close_fin:
close(tw->fin);
clear_process_slot:
process_table[i] = NULL;
free_task_waiting_on_exec:
free_task_waiting_on_exec(tw);
/* fail */
UNBLOCK_SIGCHLD;
return error;
}
static package
bf_exec(Var arglist, Byte next, void *vdata, Objid progr)
{
package pack;
const char *cmd = 0;
const char **args = 0;
task_waiting_on_exec *tw = 0;
const char *in = 0;
int len;
/* The first argument must be a list of strings. The first string
* is the command (required). The rest are command line arguments
* to the command.
*/
Var v;
int i, c;
FOR_EACH(v, arglist.v.list[1], i, c) {
if (TYPE_STR != v.type) {
pack = make_error_pack(E_INVARG);
goto free_arglist;
}
}
/* check for the empty list */
if (1 == i) {
pack = make_error_pack(E_INVARG);
goto free_arglist;
}
/* check the path */
cmd = arglist.v.list[1].v.list[1].v.str;
if (0 == strlen(cmd)) {
pack = make_raise_pack(E_INVARG, "Invalid path", var_ref(zero));
goto free_arglist;
}
if (('/' == cmd[0])
|| (1 < strlen(cmd) && '.' == cmd[0] && '.' == cmd[1])) {
pack = make_raise_pack(E_INVARG, "Invalid path", var_ref(zero));
goto free_arglist;
}
if (strstr(cmd, "/.") || strstr(cmd, "./")) {
pack = make_raise_pack(E_INVARG, "Invalid path", var_ref(zero));
goto free_arglist;
}
/* prepend the exec subdirectory path */
static Stream *s;
if (!s)
s = new_stream(strlen(EXEC_SUBDIR) * 2);
stream_add_string(s, EXEC_SUBDIR);
stream_add_string(s, cmd);
cmd = str_dup(reset_stream(s));
/* clean input */
in = NULL;
len = 0;
if (listlength(arglist) > 1) {
if ((in = binary_to_raw_bytes(arglist.v.list[2].v.str, &len)) == NULL) {
pack = make_error_pack(E_INVARG);
goto free_cmd;
}
in = str_dup(in);
}
/* check perms */
if (!is_wizard(progr)) {
pack = make_error_pack(E_PERM);
goto free_in;
}
/* stat the command */
struct stat buf;
if (stat(cmd, &buf) != 0) {
pack = make_raise_pack(E_INVARG, "Does not exist", var_ref(zero));
goto free_in;
}
if (!S_ISREG(buf.st_mode)) {
pack = make_raise_pack(E_INVARG, "Is not a file", var_ref(zero));
goto free_in;
}
args = (const char **)mymalloc(sizeof(const char *) * i, M_ARRAY);
FOR_EACH(v, arglist.v.list[1], i, c)
args[i - 1] = str_dup(v.v.str);
args[i - 1] = NULL;
tw = malloc_task_waiting_on_exec();
tw->cmd = cmd;
tw->args = args;
tw->in = in;
tw->len = len;
free_var(arglist);
return make_suspend_pack(exec_waiter_suspender, tw);
free_in:
if (in)
free_str(in);
free_cmd:
free_str(cmd);
free_arglist:
free_var(arglist);
/* fail */
return pack;
}
/*
* Called from child_completed_signal() in server.c.
* SIGCHLD is already blocked.
*/
pid_t
exec_complete(pid_t pid, int code)
{
task_waiting_on_exec *tw = NULL;
int i;
for (i = 0; i < EXEC_MAX_PROCESSES; i++)
if (process_table[i] && process_table[i]->pid == pid) {
tw = process_table[i];
break;
}
if (tw) {
sigchild_interrupt = 1;
if (TWS_CONTINUE == tw->status) {
tw->status = TWS_STOP;
tw->code = code;
}
return pid;
}
/* We wind up here if the child process was a checkpoint process,
* or if an exec task was explicitly killed while the process
* itself was still executing.
*/
return 0;
}
/*
* Called from main_loop() in server.c.
*/
void
deal_with_child_exit(void)
{
if (!sigchild_interrupt)
return;
BLOCK_SIGCHLD;
sigchild_interrupt = 0;
task_waiting_on_exec *tw = NULL;
int i;
for (i = 0; i < EXEC_MAX_PROCESSES; i++) {
tw = process_table[i];
if (tw && TWS_STOP == tw->status) {
Var v;
v = new_list(3);
v.v.list[1].type = TYPE_INT;
v.v.list[1].v.num = tw->code;
stdout_readable(tw->fout, tw);
v.v.list[2].type = TYPE_STR;
v.v.list[2].v.str = str_dup(reset_stream(tw->sout));
stderr_readable(tw->ferr, tw);
v.v.list[3].type = TYPE_STR;
v.v.list[3].v.str = str_dup(reset_stream(tw->serr));
resume_task(tw->the_vm, v);
}
if (tw && TWS_CONTINUE != tw->status) {
free_task_waiting_on_exec(tw);
process_table[i] = NULL;
}
}
UNBLOCK_SIGCHLD;
}
void
register_exec(void)
{
sigemptyset(&block_sigchld);
sigaddset(&block_sigchld, SIGCHLD);
register_task_queue(exec_waiter_enumerator);
register_function("exec", 1, 2, bf_exec, TYPE_LIST, TYPE_STR);
}