-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutor.c
More file actions
296 lines (282 loc) · 7.9 KB
/
executor.c
File metadata and controls
296 lines (282 loc) · 7.9 KB
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
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "fsa.h"
#include "executor.h"
#include "logger.h"
#include "multiproc.h"
// allocate new simplecmd
simplecmd new_simplecmd(buffer_t buf, int argc) {
if(!argc) {
assert(buf.length == 0);
for(const char *c = "cat"; *c != '\0'; ++c)
addch_buffer(&buf, *c);
}
simplecmd s = {
.fd_stdin = STDIN_FILENO,
.fd_stdout = STDOUT_FILENO,
.fd_stderr = STDERR_FILENO,
.stdout_flag = FLAG_OUTPUT_WR,
.stderr_flag = FLAG_OUTPUT_WR,
.argv = malloc(sizeof(char *) * (argc + 1)),
.argc = argc,
.ret = 0,
.in_background = false,
.fork_id = -1,
.fd_fork = -1,
.input_fname = stdin,
.output_fname = stdout,
.error_fname = stderr,
.next = (cmd_linker){
.operation = CMD_NOOP,
.cmd = NULL,
},
};
assert(s.argv != NULL);
buffer_t argument = new_buffer(1026);
char *c = buf.str;
for(int i = 0; i < argc; ++i) {
while(*c != BUF_SEP && *c != '\0') {
addch_buffer(&argument, *c);
++c;
}
s.argv[i] = strdup(argument.str);
empty_buffer(&argument);
++c;
}
s.argv[s.argc] = NULL;
free_buffer(argument);
return s;
}
// get string representation of a ..._fname field, i.e.
// a const char* or a FILE *
const char *get_filename(const void *filename) {
if(isfile(filename))return filename;
if(filename==stdin)return"stdin";
if(filename==stdout)return"stdout";
if(filename==stderr)return"stderr";
return NULL;
}
// debug output of all relevant fields of one simplecmd
void dump_simplecmd(simplecmd s) {
pr_log("----------------------\n");
pr_log("SIMPLECOMMAND DUMP\n");
pr_log("input %d\n", s.fd_stdin);
pr_log("output %d\n", s.fd_stdout);
pr_log("error %d\n", s.fd_stderr);
pr_log("argc == %d\n", s.argc);
pr_log("argv:\n");
for(int i = 0; i < s.argc; ++i) {
pr_log("%d\t'%s'\n", i, s.argv[i]);
}
pr_log("input file: %s\n", get_filename(s.input_fname));
pr_log("output file: %s\n", get_filename(s.output_fname));
pr_log("error file: %s\n", get_filename(s.error_fname));
pr_log("in_background==%s\n", s.in_background?"true":"false");
pr_log("ret==%d\n", s.ret);
pr_log("next=={%d,%p}\n", s.next.operation, s.next.cmd);
if(s.next.cmd != NULL)
dump_simplecmd(*s.next.cmd);
fflush(stdout);
}
// deallocate simplecmd
void free_simplecmd(simplecmd s) {
if(s.next.cmd != NULL)
free_simplecmd(*s.next.cmd);
for(int i = 0; i < s.argc; ++i)
free(s.argv[i]);
if(isfile(s.input_fname))
free(s.input_fname);
if(isfile(s.output_fname))
free(s.output_fname);
if(isfile(s.error_fname))
free(s.error_fname);
free(s.argv);
}
// assign file/stream pointer to the file/stream pointer field
// manages allocation/assignment problems
void assign_stream(void **stream, void *assigned) {
if(isfile(*stream) && !isfile(assigned))
free(*stream),*stream=assigned;
else if(!isfile(*stream) && isfile(assigned))
*stream = strdup(assigned);
else
*stream = assigned;
}
// sets up stdin, stdout and stderr
void set_streams(simplecmd *s) {
if(s->fork_id >= 0 && !isfile(s->error_fname)) {
create_temp(s);
dup_stream(s->fd_fork, STDERR_FILENO);
}
int failures = 0;
s->fd_stdin = set_stdin(s->input_fname);
if(s->fd_stdin == -1) {
failures |= 1;
fprintf(stderr, "Read failed: %s\n", get_filename(s->input_fname));
}
s->fd_stdout = set_stdout(s->output_fname, s->stdout_flag);
if(s->fd_stdout == -1) {
failures |= 2;
fprintf(stderr, "Write failed: %s\n", get_filename(s->output_fname));
}
s->fd_stderr = set_stderr(s->error_fname, s->stderr_flag);
if(s->fd_stderr == -1) {
failures |= 4;
/* fprintf(stderr, "Error failed: %s\n", get_filename(s->error_fname)); */
}
if(failures)s->ret=-1;
}
// dup stdin, stdout, stderr to whatever the redirection is set
void dup_streams(simplecmd *s) {
int failures = 0;
int ret;
assert(fd_is_valid(s->fd_stdin));
ret=dup_stream(s->fd_stdin, STDIN_FILENO);
if(ret==-1)failures|=1;
assert(fd_is_valid(s->fd_stdout));
ret=dup_stream(s->fd_stdout, STDOUT_FILENO);
if(ret==-1)failures|=2;
assert(fd_is_valid(s->fd_stderr));
ret=dup_stream(s->fd_stderr, STDERR_FILENO);
if(ret==-1)failures|=4;
if(failures)s->ret=-1;
if(s->fork_id >= 0 && !isfile(s->output_fname)) {
if(s->fd_fork == -1)
create_temp(s);
dup_stream(s->fd_fork, STDOUT_FILENO);
}
}
// unused. a small overhead of using command operations
void pipe_simplecmds(simplecmd *s1, simplecmd *s2) {
if(s1->input_fname!=stdin)return;
if(s1->output_fname==stdout)pipe_streams(s1->fd_stdout,s2->fd_stdin);
if(s1->error_fname==stdout)pipe_streams(s1->fd_stderr,s2->fd_stdin);
}
// opposite to set_stream. closes files, temporaries and similar things
void reset_streams(simplecmd *s) {
if(s->fork_id >= 0) {
if(s->fd_fork == -1)
create_temp(s);
close_temp(s);
}
if(s->fd_stdin != -1)
close_file(s->fd_stdin),s->fd_stdin = STDIN_FILENO;
if(s->fd_stdout != -1)
close_file(s->fd_stdout),s->fd_stdout = STDOUT_FILENO;
if(s->fd_stderr != -1)
close_file(s->fd_stderr),s->fd_stderr = STDERR_FILENO;
if(errno) {
/* perror("error"); */
errno=0;
}
}
// a separated part of the execition process.
// a few checks + setting up streams etc.
void execution_setup_simplecmd(simplecmd *s) {
if(s == NULL)
return;
assert(s->next.operation != CMD_INVALID);
if(s->in_background && (s->next.cmd != NULL && s->next.operation != CMD_NOOP)) {
fprintf(stderr, "error: cannot execute lcommand in parallel");
exit(1);
}
pr_log("EXECUTING COMMAND\n");
assert(!errno);
set_streams(s);
dump_simplecmd(*s);
}
// forkable part of the execution.
void execute_with_result(simplecmd *s) {
if(s == NULL)
return;
if(!s->ret) {
pid_t parent = fork();
if(errno) {
/* perror("failed to fork"); */
errno=0;
}
if(parent) {
int loc;
pid_t q = wait(&loc);
#ifdef STACSCHECK
s->ret = (loc<0)?loc:0;
#else
s->ret = WEXITSTATUS(loc);
#endif
} else {
dup_streams(s);
if(execvp(s->argv[0], s->argv) < 0) {
#ifdef STACSCHECK
fprintf(stderr, "Execute failed: %s\n", s->argv[0]);
free_simplecmd(*s);
exit(1);
#endif
}
reset_streams(s);
close_log();
}
}
return;
// small overhead of using different operations between commands
// although unused, is important for structuring ideas and keeping code
// flexible
switch(s->next.operation) {
case CMD_PIPE:
if(s->next.cmd == NULL) {
fprintf(stderr, "error: no program after pipe\n");
exit(1);
}
execution_setup_simplecmd(s->next.cmd);
pipe_simplecmds(s, s->next.cmd);
execute_with_result(s->next.cmd);
execution_finish_simplecmd(s);
break;
case CMD_NOOP:
execute_simplecmd(s->next.cmd);
break;
case CMD_AND:
if(!s->ret)
execute_simplecmd(s->next.cmd);
else if(s->next.cmd != NULL)
execute_simplecmd(s->next.cmd->next.cmd);
break;
case CMD_OR:
if(!s->ret)
execute_simplecmd(s->next.cmd);
else if(s->next.cmd != NULL)
execute_simplecmd(s->next.cmd->next.cmd);
break;
}
}
// last part of the execution. mostly deallocating/closing things whose lifetime
// logically ends after execution.
void execution_finish_simplecmd(simplecmd *s) {
if(s == NULL)
return;
reset_streams(s);
if(s->ret && (s->next.operation == CMD_NOOP || s->next.operation == CMD_PIPE)) {
assert(!errno);
#ifndef STACSCHECK
fprintf(stderr, "shell returns %d\n", s->ret);
#endif
}
if(errno != 0) {
perror("error");
errno = 0;
}
}
// all parts of the execution together
void execute_simplecmd(simplecmd *s) {
if(errno) {
/* perror("before execution"); */
errno=0;
}
execution_setup_simplecmd(s);
execute_with_result(s);
execution_finish_simplecmd(s);
}