-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecution.c
More file actions
186 lines (179 loc) · 5.65 KB
/
execution.c
File metadata and controls
186 lines (179 loc) · 5.65 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
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdbool.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <fcntl.h>
#include "ast.h"
#include "logger.h"
#include "bulitins.h"
int32_t execution(AST *ast, bool forked, bool parallel)
{
if (ast == NULL)
return EXIT_FAILURE;
AST ast_value = *ast;
switch (ast_value.tag)
{
case AST_COMMAND:
{
struct AST_COMMAND command = ast_value.data.AST_COMMAND;
if (command.argc == 0)
logger(LOG_ERROR, "No command provided.\n");
char **arguments = calloc(sizeof(char *) * (command.argc + 1), sizeof(char *));
arguments[0] = command.executable;
if (command.argc > 1)
for (size_t i = 1; i <= command.argc - 1; i++)
{
struct AST_ARGUMENT argument = command.arguments[i]->data.AST_ARGUMENT;
arguments[i] = argument.value;
}
arguments[command.argc] = NULL;
// Because of the spec, our builtins are preferred over system commands
if (scan_builtin(command.executable))
return run_builtin(command.argc, arguments);
if (forked)
{
{
int32_t result = execvpe(command.executable, arguments, environ);
if (result == -1)
logger(LOG_WARNING, "Failed to execute command.\n");
exit(result);
}
exit(execvpe(command.executable, arguments, environ));
}
else
{
pid_t pid = fork();
if (pid == -1)
logger(LOG_ERROR, "Failed to fork.\n");
if (pid == 0)
{
int32_t result = execvpe(command.executable, arguments, environ);
if (result == -1)
logger(LOG_WARNING, "Failed to execute command.\n");
exit(result);
}
}
return EXIT_SUCCESS;
break;
}
case AST_ARGUMENT:
logger(LOG_ERROR, "Unreachable code reached. Arguments should always be leaf nodes.\n");
break;
case AST_REDIRECTION:
{
struct AST_REDIRECTION redirection = ast_value.data.AST_REDIRECTION;
pid_t pid = fork();
if (pid == -1)
logger(LOG_ERROR, "Failed to fork.\n");
if (pid == 0)
{
int32_t file_descriptor = -1;
switch (redirection.AST_REDIRECTION_TYPE)
{
case AST_REDIRECTION_APPEND_LEFT:
file_descriptor = open(redirection.file, O_RDONLY);
break;
case AST_REDIRECTION_APPEND_RIGHT:
file_descriptor = open(redirection.file, O_WRONLY | O_CREAT | O_APPEND, 0644);
break;
case AST_REDIRECTION_LEFT:
file_descriptor = open(redirection.file, O_RDONLY);
break;
case AST_REDIRECTION_RIGHT:
file_descriptor = open(redirection.file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
break;
default:
logger(LOG_ERROR, "Unknown redirection type\n");
break;
}
if (file_descriptor == -1)
logger(LOG_ERROR, "Failed to open file\n");
if (dup2(file_descriptor, redirection.AST_REDIRECTION_TYPE == AST_REDIRECTION_LEFT ? STDIN_FILENO : STDOUT_FILENO) == -1)
logger(LOG_ERROR, "Failed to duplicate file descriptor\n");
exit(execution(redirection.command, true, true));
}
int32_t status = 0;
waitpid(pid, &status, 0);
return WEXITSTATUS(status);
break;
}
logger(LOG_ERROR, "Redirection not implemented\n");
break;
case AST_PIPE:
{
pid_t left_pid = fork(), right_pid = -1;
int32_t left_status = 0, right_status = 0;
int32_t pipe_between_process[2];
pipe(pipe_between_process);
if (left_pid == -1)
logger(LOG_ERROR, "Failed to fork left leaf.\n");
if (left_pid == 0)
{
dup2(pipe_between_process[1], STDOUT_FILENO);
close(pipe_between_process[0]);
exit(execution(ast_value.data.AST_PIPE.left, 0, true));
}
right_pid = fork();
if (right_pid == -1)
logger(LOG_ERROR, "Failed to fork right leaf.\n");
if (right_pid == 0)
{
dup2(pipe_between_process[0], STDIN_FILENO);
close(pipe_between_process[1]);
exit(execution(ast_value.data.AST_PIPE.right, 0, true));
}
close(pipe_between_process[0]);
close(pipe_between_process[1]);
waitpid(left_pid, &left_status, 0);
waitpid(right_pid, &right_status, 0);
return WEXITSTATUS(left_status) | WEXITSTATUS(right_status);
break;
}
case AST_LIST:
{
struct AST_LIST list = ast_value.data.AST_LIST;
// Left leaf
pid_t left_pid = fork(), right_pid = -1;
int32_t left_status = 0, right_status = 0;
if (left_pid == -1)
logger(LOG_ERROR, "Failed to fork left leaf.\n");
if (left_pid == 0)
exit(execution(list.left, 0, true));
if (!(list.AST_LIST_TYPE == AST_LIST_PARALLEL))
waitpid(left_pid, &left_status, 0);
// Right leaf
// If the left leaf successes and the list is OR, don't execute the right leaf
// If the left leaf fails and the list is AND, don't execute the right leaf
if (!((WEXITSTATUS(left_status) == EXIT_SUCCESS && list.AST_LIST_TYPE == AST_LIST_OR) ||
(WEXITSTATUS(left_status) != EXIT_SUCCESS && list.AST_LIST_TYPE == AST_LIST_AND)))
{
right_pid = fork();
if (right_pid == -1)
logger(LOG_ERROR, "Failed to fork right leaf.\n");
if (right_pid == 0)
exit(execution(list.right, 0, true));
}
if (list.AST_LIST_TYPE == AST_LIST_PARALLEL)
waitpid(left_pid, &left_status, 0);
else
waitpid(right_pid, &right_status, 0);
return WEXITSTATUS(left_status) | WEXITSTATUS(right_status);
break;
}
case AST_FD:
logger(LOG_ERROR, "File descriptors not implemented\n");
break;
case AST_LITERAL:
logger(LOG_ERROR, "Literals not implemented\n");
break;
default:
logger(LOG_ERROR, "Unknown AST tag\n");
break;
}
return EXIT_FAILURE;
}