-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec.c
177 lines (171 loc) · 4.73 KB
/
exec.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
#include <memory.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
#include "tree.h"
int tree_handler(struct cmd_inf *unit);
// настраивается ввод / вывод в фаилы
void stream_adjustment(struct cmd_inf *unit)
{
if (unit->outfile != NULL)
{
char mode[2];
if (unit->append == 1)
strcpy(mode, "a");
else
strcpy(mode, "w");
freopen(unit->outfile, mode, stdout);
}
if (unit->infile)
freopen(unit->infile, "r", stdin);
}
// перематывает древовидную структуру к концу сабшелла
struct cmd_inf *rewind_pipe(struct cmd_inf *unit)
{
if ((unit->pipe == NULL) && (unit->psubcmd == NULL))
return unit;
if (unit->pipe != NULL)
return rewind_pipe(unit->pipe);
if (unit->psubcmd != NULL)
return rewind_pipe(unit->psubcmd);
}
// запускает команды или рекурсивно вызывает сабшелл
int run_shell_or_subshell(struct cmd_inf *unit)
{
int returned_value = 0;
if (unit->argv[0] == NULL)
returned_value = tree_handler(unit->psubcmd);
else
{
if (execvp(unit->argv[0], unit->argv) == -1)
{
printf("error in line %d, file exec.c\n", __LINE__);
return -1;
}
}
return returned_value;
}
// обрабатывает пайп от начала до конца
int pipe_handler(struct cmd_inf *unit)
{
int pid;
int status;
int fd[2];
while (unit != NULL)
{
pipe(fd);
pid = fork();
if (pid == -1)
{
printf("Error with fork \n");
return -1;
}
// сын
if (pid == 0)
{
signal(SIGCHLD, SIG_DFL); //SIG_DFL
stream_adjustment(unit);
if (unit->pipe != NULL)
dup2(fd[1], 1);
close(fd[1]);
close(fd[0]);
if (run_shell_or_subshell(unit) == -1)
{
printf("error in line %d, file tree_handler.c\n", __LINE__);
return -1;
}
exit(0);
}
dup2(fd[0], 0);
close(fd[1]);
close(fd[0]);
unit = unit->pipe;
}
while (wait(&status) != -1);
return status;
}
// главная функция - обработчик дерева
int tree_handler(struct cmd_inf *unit)
{
if (unit == NULL)
return 0;
int status = 0, pid;
// обрабатывается cd (смена директории)
if ((unit->argv[0] != NULL) && (strcmp(unit->argv[0], "cd") == 0))
{
chdir(unit->argv[1]);
status = tree_handler(unit->psubcmd);
if (unit->next != NULL)
if ((unit->type == NXT) || ((unit->type == AND) && (status == 0)) || ((unit->type == OR) && (status != 0)))
status = tree_handler(unit->next);
return status;
}
// pipe
if (unit->pipe != NULL)
{
int in = dup(0);
status = pipe_handler(unit);
dup2(in, 0);
unit = rewind_pipe(unit);
if (unit == NULL)
return status;
if (unit->next != NULL)
if ((unit->type == NXT) || ((unit->type == AND) && (status == 0)) || ((unit->type == OR) && (status != 0)))
status = tree_handler(unit->next);
return status;
}
// Дочерний процесс
pid = fork();
if (pid == 0)
{
stream_adjustment(unit);
// фон
if (unit->backgrnd == 1)
{
signal(SIGCHLD, SIG_IGN);//SIG_IGN
// внук
pid == fork();
if (pid == 0)
{
run_shell_or_subshell(unit);
exit(0);
}
else
{
if (pid == -1)
{
printf("Error with fork \n");
return -1;
}
// kill father
// фоновый процесс берет на себя ос
kill(getpid(), SIGKILL);
}
// не фон
}
else
{
status = run_shell_or_subshell(unit);
if (status != 0)
exit(1);
else
exit(0);
}
// father
}
else
{
if (pid == -1)
{
printf("Error with fork \n");
return -1;
}
wait(&status);
status = WEXITSTATUS(status);
if (unit->next != NULL)
if ((unit->type == NXT) || ((unit->type == AND) && (status == 0)) || ((unit->type == OR) && (status != 0)))
status = tree_handler(unit->next);
return status;
}
}