-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexeccmd.c
274 lines (214 loc) · 5.01 KB
/
execcmd.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/queue.h>
#include <error.h>
#include <errno.h>
#include "execcmd.h"
#include "common.h"
#include "commands.h"
#include "state.h"
pid_t exec_cmd(struct cmd *c, int inpipe, int outpipe);
int exec_pipe(struct cmdpipe *cp);
/* Executes a group of commands. */
int exec_group(struct cmdgrp *cg) {
int result = 0;
struct pipeentry *p;
STAILQ_FOREACH(p, &cg->subcmds, entries) {
result = exec_pipe(p->p);
// return value of the last command
set_retval(result);
// pipeline terminated by a signal
if (result > SIG_VAL) {
// command group must be terminated as well
break;
}
}
return (result);
}
/* Executes a command pipeline. */
int exec_pipe(struct cmdpipe *cp) {
int result = 0;
pid_t last_pid = -1;
// vars for pipe descriptor manipulation
int prev_out = -1;
int fd[2];
int ind = 0;
// connect and execute all commands
struct cmdentry *c;
STAILQ_FOREACH(c, &cp->commands, entries) {
// io file descriptors
int infd = -1;
int outfd = -1;
// input of the previous command (or -1)
infd = prev_out;
// open output file descriptor (unless it's the last command)
if (ind != (cp->cmdc - 1)) {
int piperes = pipe(fd);
if (piperes == -1) {
err(1, "cannot create pipe: ");
}
// writing
outfd = fd[1];
// reading
prev_out = fd[0];
}
last_pid = exec_cmd(c->command, infd, outfd);
ind++;
}
// check exit status of the last command
int wstat;
if (last_pid != 0) {
waitpid(last_pid, &wstat, 0);
if (WIFEXITED(wstat)) {
// exited normally
result = WEXITSTATUS(wstat);
}
else if (WIFSIGNALED(wstat)) {
int sig = WTERMSIG(wstat);
fprintf(stderr, "Killed by signal %d\n", sig);
result = sig + SIG_VAL;
}
else if (WIFSTOPPED(wstat)) {
int sig = WSTOPSIG(wstat);
fprintf(stderr, "Stopped by signal %d\n",
WSTOPSIG(wstat));
result = sig + SIG_VAL;
}
else {
fprintf(stderr,
"Child terminated with unknown status\n");
}
}
else {
// internal commands should set retval
result = get_retval();
}
// wait for the other pipe processes
while (wait(NULL) != -1);
return (result);
}
char **args_to_list(struct cmd *c);
/* Executes a command. */
pid_t exec_cmd(struct cmd *c, int inpipe, int outpipe) {
// separate handling of internal commands
if (is_intern_cmd(c->path)) {
char **argv = args_to_list(c);
// argc is number of commands, not counting cmd path
run_intern_cmd(c->path, c->argc + 1, argv);
free(argv);
return (0);
}
pid_t pid = fork();
ERR_EXIT(pid == -1);
if (pid == 0) {
// child
if (inpipe != -1) {
// connect pipe input
ERR_EXIT(dup2(inpipe, 0) == -1);
if (inpipe != 0) {
close(inpipe);
}
}
if (outpipe != -1) {
// connect pipe output
ERR_EXIT(dup2(outpipe, 1) == -1);
if (outpipe != 1) {
close(outpipe);
}
}
// redirection - read
if (c->inpath != NULL) {
int indes = open(c->inpath, O_RDONLY);
if (indes == -1) {
fprintf(stderr, "%s: %s\n", c->path,
strerror(errno));
return (1);
}
ERR_EXIT(dup2(indes, 0) == -1);
if (indes != 0) {
close(indes);
}
}
// redirection - write (or append)
if (c->outpath != NULL) {
int flags;
if (c->isappend) {
flags = O_WRONLY | O_CREAT | O_APPEND;
}
else {
flags = O_WRONLY | O_CREAT | O_TRUNC;
}
int outdes = open(c->outpath, flags, 0666);
if (outdes == -1) {
fprintf(stderr, "%s: %s\n", c->path,
strerror(errno));
return (1);
}
ERR_EXIT(dup2(outdes, 1) == -1)
if (outdes != 1) {
close(outdes);
}
}
char **args = args_to_list(c);
execvp(c->path, args);
// invalid execution
err(127, c->path);
}
// close descriptors in parent
if (inpipe != -1) {
close(inpipe);
}
if (outpipe != -1) {
close(outpipe);
}
return (pid);
}
/* Gets command name without path for argv[0]. */
char *get_command_name(char *path) {
if (path == NULL) {
return (NULL);
}
size_t len = strlen(path);
size_t last_sep = len;
// find position of the last '/'
for (size_t p_ind = 0; p_ind < len; p_ind++) {
if (path[p_ind] == '/') {
last_sep = p_ind;
}
}
// possibly invalid command path
if (last_sep == len) {
char *res = strdup(path);
ERR_EXIT(res == NULL);
return (res);
}
// copy substring
char *res = malloc(sizeof (char) * (len - last_sep));
ERR_EXIT(res == NULL);
strcpy(res, &path[last_sep + 1]);
return (res);
}
/* Convert argument queue to argument array. */
char **args_to_list(struct cmd *c) {
char **args = malloc((c->argc + 2) * sizeof (char *));
ERR_EXIT(args == NULL);
int argind = 0;
// first argument is the command name by convention
args[argind++] = get_command_name(c->path);
// duplicate argument strings
struct argentry *a;
STAILQ_FOREACH(a, &c->argv, entries) {
char *dup = strdup(a->text);
ERR_EXIT(dup == NULL);
args[argind++] = dup;
}
// terminating NULL (convention)
args[argind] = (char *) NULL;
return (args);
}