Repository navigation
Expand file tree
/
Copy pathexec.c
More file actions
167 lines (144 loc) · 3.66 KB
/
Copy pathexec.c
File metadata and controls
167 lines (144 loc) · 3.66 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
#include "shell/exec/exec.h"
#include "shell/builtins/builtins.h"
#include "shell/path/path.h"
#include "shell/redir/redir.h"
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
static void close_pipes(int (*pipes)[2], int n) {
for (int i = 0; i < n; ++i) {
close(pipes[i][0]);
close(pipes[i][1]);
}
}
int shell_execute_pipeline(const ShellPipeline *p, ShellContext *ctx) {
(void)ctx;
if (!p || p->count <= 0)
return -1;
if (p->count == 1) {
const ShellCommand *c = &p->cmds[0];
if (!c->argv || !c->argv[0])
return 0;
BuiltinKind bk = builtins_classify(c->argv[0]);
if (bk != BUILTIN_NONE && !builtin_requires_parent(bk)) {
pid_t pid = fork();
if (pid < 0) {
perror("fork");
return -1;
}
if (pid == 0) {
if (shell_redir_apply(&c->redir, NULL, NULL) != 0)
_exit(1);
builtin_run(bk, c, ctx, 1, NULL);
_exit(0);
}
int status = 0;
waitpid(pid, &status, 0);
return 0;
}
char resolved[4096];
if (!path_resolve_executable(c->argv[0], resolved, sizeof(resolved))) {
printf("%s: command not found\n", c->argv[0]);
return 0;
}
pid_t pid = fork();
if (pid < 0) {
perror("fork");
return -1;
}
if (pid == 0) {
if (shell_redir_apply(&c->redir, NULL, NULL) != 0)
_exit(1);
execv(resolved, c->argv);
fprintf(stderr, "%s: failed to execute\n", c->argv[0]);
_exit(127);
}
int status = 0;
waitpid(pid, &status, 0);
return 0;
}
int stages = p->count;
int pipes_count = stages - 1;
int(*pipes)[2] = malloc(sizeof(int[2]) * (size_t)pipes_count);
if (!pipes) {
perror("malloc");
return -1;
}
for (int i = 0; i < pipes_count; ++i) {
if (pipe(pipes[i]) != 0) {
perror("pipe");
for (int j = 0; j < i; ++j) {
close(pipes[j][0]);
close(pipes[j][1]);
}
free(pipes);
return -1;
}
}
pid_t *pids = calloc((size_t)stages, sizeof(pid_t));
if (!pids) {
perror("calloc");
close_pipes(pipes, pipes_count);
free(pipes);
return -1;
}
int started = 0;
for (int si = 0; si < stages; ++si) {
pid_t pid = fork();
if (pid < 0) {
perror("fork");
/* kill already started */
for (int k = 0; k < started; ++k)
kill(pids[k], SIGKILL);
break;
}
if (pid == 0) {
/* stdin from previous pipe */
if (si > 0) {
if (dup2(pipes[si - 1][0], STDIN_FILENO) < 0) {
perror("dup2");
_exit(1);
}
}
/* stdout to next pipe */
if (si < stages - 1) {
if (dup2(pipes[si][1], STDOUT_FILENO) < 0) {
perror("dup2");
_exit(1);
}
}
close_pipes(pipes, pipes_count);
const ShellCommand *c = &p->cmds[si];
if (!c->argv || !c->argv[0])
_exit(0);
if (shell_redir_apply(&c->redir, NULL, NULL) != 0)
_exit(1);
BuiltinKind bk = builtins_classify(c->argv[0]);
if (bk != BUILTIN_NONE) {
builtin_run(bk, c, ctx, 1, NULL);
_exit(0);
}
char resolved[4096];
if (!path_resolve_executable(c->argv[0], resolved, sizeof(resolved))) {
fprintf(stderr, "%s: command not found\n", c->argv[0]);
_exit(127);
}
execv(resolved, c->argv);
fprintf(stderr, "%s: failed to execute\n", c->argv[0]);
_exit(127);
}
pids[si] = pid;
started++;
}
close_pipes(pipes, pipes_count);
free(pipes);
for (int i = 0; i < started; ++i) {
int status = 0;
waitpid(pids[i], &status, 0);
}
free(pids);
return 0;
}