-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_programs.c
More file actions
283 lines (277 loc) · 8.84 KB
/
run_programs.c
File metadata and controls
283 lines (277 loc) · 8.84 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
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/wait.h>
#include <stdbool.h>
#include <limits.h>
#include <string.h>
#include "run_programs.h"
#include "string_manipulations.h"
#include "error.h"
#include "pipes.h"
void handle_commands(char *line, Environment *environment)
{
/* Go line by character by character until end of string or special operator. */
int character = 0;
char *program = line;
char *args = NULL;
char *redirection = NULL;
bool final_round = false;
bool args_set = false;
bool redirections_set = false;
int pid_number = 0;
Process process[MAX_PIDS];
while (true)
{
if (line[character] == '&' || line[character] == '\0')
{
/* If there is real orginal null terminator set flag for break. */
if (line[character] == '\0') {
final_round = true;
}
else if (line[character + 1] == '\0' && line[character] == '&')
{
/* If final is &. */
final_round = true;
}
line[character] = '\0';
/* Check do we have reatched max amount of pids. */
if (pid_number != MAX_PIDS - 1)
{
/* Cleanup all parameters and convert arguments to real argument format. */
clean_line(program);
clean_line(args);
clean_line(redirection);
Launch launch;
launch.environment = environment;
launch.command = program;
launch.args = args;
launch.redirection = redirection;
process[pid_number] = run_program(&launch);
/* Mark mode as apropriate. */
if (redirections_set == true)
{
process[pid_number].mode = MODE_REDIRECT;
}
else
{
process[pid_number].mode = MODE_NORMAL;
}
if (process[pid_number].pid != 0)
{
/* Raise pid number only if there was no error. */
pid_number++;
}
}
else
{
error("We have allready reatched max amount of pids.\n");
}
/* Reset all parameters for next sentence. */
program = line + character + 1;
args_set = false;
redirections_set = false;
redirection = NULL;
args = NULL;
/* Skip until there is no space. This is important when running multiple commands. */
character++;
while (true)
{
if (line[character] == ' ')
{
character++;
}
else
{
/* Degrement one since there is alway ingrement at the end. Litle hacky but quite clear. */
character--;
break;
}
}
/* End if there was real null at the end. */
if (final_round == true)
break;
}
/* And character may be now checked safely since null terminator is allready checked. */
else if (line[character] == ' ' && args_set == false && line[character + 1] != '&')
{
args = line + character + 1;
line[character] = '\0';
args_set = true;
}
else if (line[character] == '>')
{
/* Check for multiple redirection signs. */
if (redirections_set == false)
{
redirection = line + character + 1;
line[character] = '\0';
redirections_set = true;
}
else
{
error("There can be only one redirection sign.\n");
break;
}
}
character++;
}
handle_processes(process, pid_number);
}
Process run_program(Launch *launch)
{
Process process;
process.pid = 0;
process.redirection = 0;
char path_to_run[PATH_MAX];
char *argv[MAX_ARGUMENTS];
PreLaunch prelaunch;
prelaunch.path_to_run = path_to_run;
prelaunch.argv = argv;
prelaunch.process = &process;
if (program_pre_launch_preparations(launch, &prelaunch) != 0) {
return process;
}
process.pid = fork();
/* If the pid is 0 its child process. */
if (process.pid == 0)
{
connect_pipes_child(&process);
execv(path_to_run, argv);
/* In case exec fails. */
error("Creating new process failed.\n");
exit(4);
}
else if (process.pid == -1)
{
/* Fork failed for some reson. */
error(ERROR_MESSAGE);
return process;
}
else
{ /* pid!=0; parent process */
close(process.stdout[1]);
close(process.stderr[1]);
close(process.stdin[0]);
return process;
}
}
int program_pre_launch_preparations(Launch *launch, PreLaunch *prelaunch) {
/* Zero success, one fail. */
convert_to_arguments(launch->args, prelaunch->argv, launch->command);
if (handle_program_path(launch->command, prelaunch->path_to_run, launch->environment) != 0)
{
/* If we can't reach program then its error. */
error("Couldn't find progam binary on launch.\n");
return 1;
}
/* Prepare redirection file. */
if (launch->redirection != NULL)
{
/* Check file is not empty. */
if (launch->redirection[0] == '\0') {
error("There has to be filename after redirection.\n");
return 1;
}
/* Check that path does not contain spaces. */
if (check_for_char(launch->redirection, ' ') != 0) {
error("No spaces allowed in redirection.\n");
return 1;
}
/* https://stackoverflow.com/questions/8516823/redirecting-output-to-a-file-in-c */
char absolute_path[PATH_MAX];
get_absolute_path(launch->redirection, launch->environment->cwd, absolute_path);
if ((prelaunch->process->redirection = open(absolute_path, O_RDWR | O_CREAT | O_TRUNC, 0600)) == 0)
{
return 1;
}
}
/* Crete pipes. */
/* https://stackoverflow.com/questions/7292642/grabbing-output-from-exec */
if (create_pipes(prelaunch->process) == -1)
{
return 1;
}
return 0;
}
int handle_program_path(char *program, char *path_to_run, Environment *environment)
{
/* Zero success, one not found or not accessible. */
struct stat info;
int previous = 0;
int count = 0;
path_to_run[0] = '\0';
if (handle_relative_path(program, environment->cwd, path_to_run) == 0)
{
if (stat(path_to_run, &info) != 0 && info.st_mode == S_IXUSR)
{
return 0;
}
else
{
return 1;
}
}
while (true)
{
/* https://stackoverflow.com/questions/13098620/using-stat-to-check-if-a-file-is-executable-in-c */
if (environment->paths[previous + count] == '\0' || environment->paths[previous + count] == ' ')
{
strncpy(path_to_run, environment->paths + previous, count + 1);
path_to_run[count] = '\0';
/* Fix if trailing slash is missing from the path. */
if (environment->paths[previous + count - 1] != '/')
{
strcat(path_to_run, "/");
}
strncat(path_to_run, program, PATH_MAX - count - 2);
if (stat(path_to_run, &info) == 0 && info.st_mode & S_IXUSR)
{
return 0;
}
else
{
/* Exit if it was final path option. */
if (environment->paths[previous + count] == '\0')
{
break;
}
else
{
/* Prepare for next path. */
previous += count + 1;
count = 0;
continue;
}
}
}
count++;
}
return 1;
}
void handle_processes(Process *process, int pid_amount)
{
/* Handle all processes and wait for them to finnish. */
for (int number = 0; number < pid_amount; number++)
{
waitpid(process[number].pid, 0, 0);
if (process[number].mode != MODE_REDIRECT)
{
/* Write output to a terminal. */
pipe_to_std(process[number].stdout[0], STDOUT_FILENO);
pipe_to_std(process[number].stderr[0], STDERR_FILENO);
}
else
{
close(process[number].redirection);
}
close(process[number].stdout[0]);
close(process[number].stderr[0]);
close(process[number].stdin[1]);
}
}