-
Notifications
You must be signed in to change notification settings - Fork 0
/
executor.c
395 lines (351 loc) · 9.85 KB
/
executor.c
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
//
// executor.c
// yxsh
//
// Created by Kirill on 02.04.2018.
// Copyright © 2018 Kirill. All rights reserved.
//
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include "builtin.h"
#include "executor.h"
static bool setup_redirects(command_t*);
static bool switch_file_descriptor(int, int, char*);
static bool set_input_file(char*);
static bool set_output_file(char*, int);
static void execute_fork(command_t*, pid_t);
static void execute_parent(tasks_env_t*, pid_t, commandline_t*, size_t);
static pid_t execute_pipeline_command(command_t* cmd, int*, pid_t);
static size_t prepare_pipeline(commandline_t*, size_t);
static void execute_pipeline(tasks_env_t*, commandline_t*, size_t);
static void execute_command(tasks_env_t*, commandline_t*, size_t);
static char* form_display(commandline_t*, size_t, size_t);
/**
* Setup input/output redirects if presented.
*
* @param cmd Target command.
*/
static bool setup_redirects(command_t* cmd) {
if (cmd->infile && !set_input_file(cmd->infile))
return false;
if (cmd->outfile && !set_output_file(cmd->outfile, cmd->flags))
return false;
if (cmd->flags & FLAG_IN_PIPE
&& !switch_file_descriptor(cmd->pipes[0], STDIN_FILENO,
"Cannot redirect input pipe"))
return false;
if (cmd->flags & FLAG_OUT_PIPE
&& !switch_file_descriptor(cmd->pipes[1], STDOUT_FILENO,
"Cannot redirect output pipe"))
return false;
return true;
}
/**
* Switches file descriptors using dup2(2) function.
*
* @param oldfd Old file descriptor.
* @param newfd Purpose to copy descriptor.
* @param err Error message if it occurred.
*
* @return true if success.
*/
static bool switch_file_descriptor(int oldfd, int newfd, char* err) {
if (dup2(oldfd, newfd) == -1) {
perror(err);
close(oldfd);
return false;
}
close(oldfd);
return true;
}
/**
* Redirects input from file to STDIN descriptor.
*
* @param infile Input file name.
*
* @return true if success.
*/
static bool set_input_file(char* infile) {
int file = open(infile, O_RDONLY);
if (file == -1) {
perror("yxsh: Cannot open input file");
return false;
}
return switch_file_descriptor(file, STDIN_FILENO,
"yxsh: Cannot set input file");
}
/**
* Redirects output from STDOUT to file.
*
* @param outfile Output file name.
* @param redirects Flags for redirects.
*
* @return true if success.
*/
static bool set_output_file(char* outfile, int redirects) {
int file;
int flags = O_WRONLY | O_CREAT;
if (redirects & FLAG_MERGE_OUT) {
flags |= O_APPEND;
if ((file = open(outfile, flags, (mode_t)0644)) == -1) {
perror("yxsh: Cannot open err output file");
return false;
}
if (!switch_file_descriptor(file, STDERR_FILENO,
"yxsh: Cannot set stderr file"))
return false;
} else if (redirects & FLAG_APPLY_FILE)
flags |= O_APPEND;
else
flags |= O_CREAT | O_TRUNC;
if ((file = open(outfile, flags, (mode_t)0644)) == -1) {
perror("yxsh: Cannot open output file");
return false;
}
return switch_file_descriptor(file, STDOUT_FILENO,
"yxsh: Cannot set output file");
}
/**
* The work is done with a fork process part.
*
* @param cmd Command for execution.
* @param pgid Process group ID.
*/
static void execute_fork(command_t* cmd, pid_t pgid) {
signal(SIGINT, SIG_DFL);
signal(SIGQUIT, SIG_DFL);
signal(SIGTSTP, SIG_DFL);
signal(SIGCHLD, SIG_DFL);
setup_redirects(cmd);
setpgid(0, pgid);
if (!(cmd->flags & (FLAG_BACKGROUND | FLAG_IN_PIPE)) && !cmd->infile) {
if (!setup_terminal(getpgrp()))
return;
}
if (execvp(cmd->cmdargs[0], cmd->cmdargs))
perror("yxsh: Cannot execute");
exit(0);
}
/**
* The work is done with a parent process part.
*
* @param env Current environment.
* @param pid Process ID of sub process.
* @param commandline Current command line.
* @param pos Position of command.
*/
static void execute_parent(tasks_env_t* env, pid_t pid,
commandline_t* commandline, size_t pos) {
command_t* cmd = &commandline->cmds[pos];
char* display;
if (!(cmd->flags & FLAG_BACKGROUND)) {
signal(SIGINT, SIG_IGN);
signal(SIGQUIT, SIG_IGN);
signal(SIGTSTP, SIG_IGN);
signal(SIGCHLD, SIG_DFL);
}
display = form_display(commandline, pos, 1);
if (!display
|| !tasks_has_free(env)
|| !tasks_run_task(env, pid, cmd->flags & FLAG_BACKGROUND, display)) {
fprintf(stderr, "yxsh: Not enougth space to run task in background.\n");
if (display)
free(display);
killpg(pid, SIGHUP);
waitpid(pid, NULL, WUNTRACED);
}
setup_terminal(getpgrp());
}
/**
* Executes command normally.
*
* @param env Current environment.
* @param commandline Current command line.
* @param pos Position of command.
*/
static void execute_command(tasks_env_t* env, commandline_t* commandline, size_t pos) {
pid_t pid = fork();
switch (pid) {
case -1:
perror("yxsh: Cannot create fork");
return;
case 0:
execute_fork(&commandline->cmds[pos], 0);
return;
default:
setpgid(pid, pid);
execute_parent(env, pid, commandline, pos);
}
}
/**
* Creates pipes and execute fork.
*
* @param cmd Command.
* @param out_pipe Previous output pipe.
* @param pgid Process group ID.
*
* @return 0 if fork process, -1 if error and other if current process.
*/
static pid_t execute_pipeline_command(command_t* cmd, int* out_pipe, pid_t pgid) {
if (pipe(cmd->pipes)) {
perror("yxsh: Cannot create pipeline");
return false;
}
pid_t pid = fork();
switch (pid) {
case -1:
perror("yxsh: Cannot create fork");
break;
case 0:
cmd->pipes[0] = (*out_pipe);
execute_fork(cmd, pgid);
return pid;
}
if (cmd->flags & FLAG_IN_PIPE) {
close((*out_pipe));
}
close(cmd->pipes[1]);
(*out_pipe) = cmd->pipes[0];
return pid;
}
/**
* Sets background flag to all commands in pipeline the same as last command.
*
* @param commandline Current command line.
* @param begin Index of pipeline first command.
*
* @return Amount of commands in pipeline.
*/
static size_t prepare_pipeline(commandline_t* commandline, size_t begin) {
// Search for last pipeline command
size_t end = begin;
size_t pos = begin;
while (end < commandline->ncmds
&& commandline->cmds[++end].flags & (FLAG_IN_PIPE | FLAG_OUT_PIPE));
end--;
// Sets background flag for all commands the same as last command
char bg = (char) (commandline->cmds[end].flags & FLAG_BACKGROUND);
for (; pos < end; pos++)
commandline->cmds[pos].flags |= bg;
return end - begin + 1;
}
/**
* Forms string to display in jobs list.
*
* @param commandline Current command line.
* @param begin Index of pipeline first command.
* @param num Amount of commands in line.
*
* @return Display string.
*/
static char* form_display(commandline_t* commandline, size_t begin, size_t num) {
size_t len = 0;
size_t pos;
size_t str_pos = 0;
size_t str_len;
command_t* cmd;
char* display;
for (size_t i = begin; i < num + begin; i++) {
cmd = &(commandline->cmds[i]);
pos = 0;
while (pos < MAXARGS && cmd->cmdargs[pos])
len += strlen(cmd->cmdargs[pos++]) + 1;
// 3 additional for pipeline symbols: " | "
len += 2;
}
display = (char*) malloc(sizeof(char) * len);
if (!display) {
perror("yxsh: Cannot form display string");
return NULL;
}
for (size_t i = begin; i < num + begin; i++) {
cmd = &(commandline->cmds[i]);
pos = 0;
while (pos < MAXARGS && cmd->cmdargs[pos]) {
str_len = strlen(cmd->cmdargs[pos]);
memcpy(display + str_pos, cmd->cmdargs[pos], str_len);
str_pos += str_len;
display[str_pos++] = ' ';
pos++;
}
display[str_pos++] = '|';
display[str_pos++] = ' ';
}
// Remove 3 with final pipeline symbols: " | "
display[str_pos - 3] = '\0';
return display;
}
/**
* Executes commands pipeline.
*
* @param env Current environment.
* @param commandline Current command line.
* @param begin Index of pipeline first command.
*/
static void execute_pipeline(tasks_env_t* env, commandline_t* commandline, size_t begin) {
size_t pos = begin;
pid_t pid;
pid_t main_pid = 0;
char* display;
int out_pipe = -1;
size_t amount = prepare_pipeline(commandline, begin);
bool bg = commandline->cmds[begin].flags & FLAG_BACKGROUND;
pid_t* pids = (pid_t*) malloc(sizeof(pid_t) * amount);
if (!bg) {
signal(SIGINT, SIG_IGN);
signal(SIGQUIT, SIG_IGN);
signal(SIGTSTP, SIG_IGN);
signal(SIGCHLD, SIG_DFL);
}
while (pos < amount + begin) {
pid = execute_pipeline_command(&commandline->cmds[pos], &out_pipe, main_pid);
if (pid == -1 || !pid) {
if (out_pipe != -1)
close(out_pipe);
return;
}
if (pos == begin)
main_pid = pid;
setpgid(pid, main_pid);
pids[pos - begin] = pid;
pos++;
}
close(out_pipe);
display = form_display(commandline, begin, amount);
if (!display
|| !tasks_has_free(env)
|| !tasks_run_pipeline(env, main_pid, pids, bg, amount, display)) {
fprintf(stderr, "yxsh: Not enougth space to run task in background.\n");
if (display)
free(display);
killpg(pid, SIGHUP);
waitpid(pid, NULL, WUNTRACED);
}
setup_terminal(getpgrp());
}
bool execute(tasks_env_t* env, commandline_t* commandline) {
for (size_t i = 0; i < commandline->ncmds; i++) {
command_t* cmd = &commandline->cmds[i];
int result = try_builtin(env, cmd);
if (result == BUILTIN_EXECUTED)
return true;
if (result == BUILTIN_EXIT)
return false;
if (cmd->flags & FLAG_IN_PIPE)
continue;
if (cmd->flags & FLAG_OUT_PIPE)
execute_pipeline(env, commandline, i);
else
execute_command(env, commandline, i);
}
return true;
}