-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
307 lines (295 loc) · 11 KB
/
main.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#include <stdio.h>
#include <spawn.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdarg.h>
#include <poll.h>
#include <sys/wait.h>
#include <stdbool.h>
#include <fcntl.h>
typedef enum {
PROC_COM_INHERIT = 0, // from parent
PROC_COM_NONE, // use /dev/null
PROC_COM_PIPE, // use pipe
PROC_COM_FD, // use supplied fd for stdXX
PROC_COM_PATH, // use supplied path for stdXX
PROC_COM_STDOUT // same as stdout; used for stderr only!
} ProcComType;
typedef struct {
// input params
ProcComType stdin_type;
ProcComType stdout_type;
ProcComType stderr_type;
char* f_stdin; // stdin file path
char* f_stdout; // stdout file path
char* f_stderr; // stderr file path
// input (PROC_COM_FD) and/or output (PROC_COM_PIPE)
int p_stdin; // stdin pipe fd, negative if not used
int p_stdout; // stdout pipe fd, negative if not used
int p_stderr; // stderr pipe fd, negative if not used
// output params
pid_t pid;
} ProcInfo;
void showError(bool noop, char *fmt,...) {
va_list args;
fprintf(stderr, "ERROR: ");
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
if (fmt[strlen(fmt)-1] != '\n') {
fprintf(stderr, "\n");
}
}
void close_ProcInfo(ProcInfo *ci) {
if (ci->p_stdin >= 0) {
close(ci->p_stdin);
ci->p_stdin = -1;
}
if (ci->p_stdout >= 0) {
close(ci->p_stdout);
ci->p_stdout = -1;
}
if (ci->p_stderr >= 0) {
close(ci->p_stderr);
ci->p_stderr = -1;
}
ci->pid = -1;
}
// create a subprocess and execute it
// args and env are char*[] with last element being NULL
int subprocess(ProcInfo *ci, char* args[], char* env[]) {
int stdin_pipe[2], stdout_pipe[2], stderr_pipe[2];
int rc = 0;
posix_spawn_file_actions_t action;
posix_spawn_file_actions_init(&action);
switch (ci->stdin_type) {
case PROC_COM_PIPE:
if (pipe(stdin_pipe)) {
showError(false, "Failed to create stdin pipe for subprocess %s: %s!", args[0], strerror(errno));
rc = 1;
goto clean_up;
}
// on the child side
posix_spawn_file_actions_addclose(&action, stdin_pipe[1]); // the write end
posix_spawn_file_actions_adddup2(&action, stdin_pipe[0], STDIN_FILENO); // the read end
posix_spawn_file_actions_addclose(&action, stdin_pipe[0]);
break;
case PROC_COM_FD:
if (ci->p_stdin < 0) {
showError(false, "Invalid stdin fd (%d) for subprocess %s: %s!",
ci->p_stdin, args[0], strerror(errno));
rc = 1;
goto clean_up;
}
posix_spawn_file_actions_adddup2(&action, ci->p_stdin, STDIN_FILENO);
if (ci->p_stdin != STDIN_FILENO) {
posix_spawn_file_actions_addclose(&action, ci->p_stdin);
}
break;
case PROC_COM_STDOUT:
showError(false, "Invalid pipe type (PROC_COM_STDOUT) for stdin!");
rc = 1;
goto clean_up;
case PROC_COM_PATH:
if (ci->f_stdin == NULL) {
showError(false, "Empty stdin path for subprocess %s: %s!", args[0], strerror(errno));
rc = 1;
goto clean_up;
}
posix_spawn_file_actions_addopen(&action, STDIN_FILENO, ci->f_stdin, O_RDONLY, 0644);
break;
case PROC_COM_NONE:
posix_spawn_file_actions_addclose(&action, STDIN_FILENO);
break;
case PROC_COM_INHERIT: // no-op
break;
default:
showError(false, "Unknown PipeType (%d) for stdin!", ci->stdin_type);
rc = 1;
goto clean_up;
}
switch (ci->stdout_type) {
case PROC_COM_PIPE:
if (pipe(stdout_pipe)) {
showError(false, "Failed to create stdout pipe for subprocess %s: %s!", args[0], strerror(errno));
rc = 1;
goto clean_up;
}
// on the child side
posix_spawn_file_actions_addclose(&action, stdout_pipe[0]); // the read end
posix_spawn_file_actions_adddup2(&action, stdout_pipe[1], STDOUT_FILENO); // the write end
posix_spawn_file_actions_addclose(&action, stdout_pipe[1]);
break;
case PROC_COM_STDOUT:
showError(false, "Invalid pipe type (PROC_COM_STDOUT) for stdout!");
rc = 1;
goto clean_up;
case PROC_COM_FD:
if (ci->p_stdout < 0) {
showError(false, "Invalid stdout fd (%d) for subprocess %s: %s!",
ci->p_stdout, args[0], strerror(errno));
rc = 1;
goto clean_up;
}
posix_spawn_file_actions_adddup2(&action, ci->p_stdout, STDOUT_FILENO);
if (ci->p_stdout != STDOUT_FILENO) {
posix_spawn_file_actions_addclose(&action, ci->p_stdout);
}
break;
case PROC_COM_PATH:
if (ci->f_stdout == NULL) {
showError(false, "Empty stdout path for subprocess %s: %s!", args[0], strerror(errno));
rc = 1;
goto clean_up;
}
posix_spawn_file_actions_addopen(&action, STDOUT_FILENO, ci->f_stdout, O_CREAT|O_WRONLY|O_TRUNC, 0644);
break;
case PROC_COM_NONE:
posix_spawn_file_actions_addclose(&action, STDOUT_FILENO);
break;
case PROC_COM_INHERIT: // no-op
break;
default:
showError(false, "Unknown PipeType (%d) for stdout!", ci->stdout_type);
rc = 1;
goto clean_up;
}
switch (ci->stderr_type) {
case PROC_COM_PIPE:
if (pipe(stderr_pipe)) {
showError(false, "Failed to create stderr pipe for subprocess %s: %s!", args[0], strerror(errno));
rc = 1;
goto clean_up;
}
// on the child side
posix_spawn_file_actions_addclose(&action, stderr_pipe[0]); // the read end
posix_spawn_file_actions_adddup2(&action, stderr_pipe[1], STDERR_FILENO); // the write end
posix_spawn_file_actions_addclose(&action, stderr_pipe[1]);
break;
case PROC_COM_FD:
if (ci->p_stderr < 0) {
showError(false, "Invalid stderr fd (%d) for subprocess %s: %s!",
ci->p_stderr, args[0], strerror(errno));
rc = 1;
goto clean_up;
}
posix_spawn_file_actions_adddup2(&action, ci->p_stderr, STDERR_FILENO);
if (ci->p_stderr != STDERR_FILENO) {
posix_spawn_file_actions_addclose(&action, ci->p_stderr);
}
break;
case PROC_COM_PATH:
if (ci->f_stderr == NULL) {
showError(false, "Empty stderr path for subprocess %s: %s!", args[0], strerror(errno));
rc = 1;
goto clean_up;
}
posix_spawn_file_actions_addopen(&action, STDERR_FILENO, ci->f_stderr, O_CREAT|O_WRONLY|O_TRUNC, 0644);
break;
case PROC_COM_STDOUT:
if (ci->stdout_type == PROC_COM_INHERIT) { // no-op
break;
} else if (ci->stdout_type != PROC_COM_NONE) {
posix_spawn_file_actions_adddup2(&action, STDOUT_FILENO, STDERR_FILENO);
break;
}
// fall thorugh for ci->stdout_type == PROC_COM_NONE
case PROC_COM_NONE:
posix_spawn_file_actions_addclose(&action, STDERR_FILENO);
break;
case PROC_COM_INHERIT: // no-op
break;
default:
showError(false, "Unknown PipeType (%d) for stderr!", ci->stderr_type);
rc = 1;
goto clean_up;
}
rc = posix_spawnp(&(ci->pid), args[0], &action, NULL, args, env);
if(rc != 0) {
showError(false, "Failed to spawn subprocess %s: %s!", args[0], strerror(errno));
goto clean_up;
}
// close child-side of pipes, and assign returned pipes
if (ci->stdin_type == PROC_COM_PIPE) {
close(stdin_pipe[0]); // the read end
ci->p_stdin = stdin_pipe[1]; // the write end
} else if (ci->stdin_type != PROC_COM_FD) {
ci->p_stdin = -1;
}
if (ci->stdout_type == PROC_COM_PIPE) {
close(stdout_pipe[1]); // the write end
ci->p_stdout = stdout_pipe[0]; // the read end
} else if (ci->stdin_type != PROC_COM_FD) {
ci->p_stdout = -1;
}
if (ci->stderr_type == PROC_COM_PIPE) {
close(stderr_pipe[1]); // the write end
ci->p_stderr = stderr_pipe[0]; // the read end
} else if (ci->stdin_type != PROC_COM_FD) {
// When PROC_COM_STDOUT is used, the caller should use just ci->p_stdout
ci->p_stderr = -1;
}
clean_up:
posix_spawn_file_actions_destroy(&action);
return rc;
}
int main(void) {
ProcInfo ci = {.p_stdin=-1, .p_stdout=-1, .p_stderr=-1,
.stdout_type=PROC_COM_FD, .stderr_type=PROC_COM_PIPE};
ProcInfo ci2 = {.p_stdin=-1, .p_stdout=-1, .p_stderr=-1,
//.f_stderr="wc_out.txt",
.stdin_type=PROC_COM_PIPE, .stdout_type=PROC_COM_PIPE,
.stderr_type=PROC_COM_STDOUT};
char* args[] = {"ls", "/bin", NULL};
char* args2[] = {"wc", NULL};
int exit_code;
printf("Hello World\n");
subprocess(&ci2, args2, NULL);
ci.p_stdout = ci2.p_stdin;
//printf("stdout fd: %d\n", ci.p_stdout);
subprocess(&ci, args, NULL);
char buffer[128];
struct pollfd plist[] = { {ci.p_stderr, POLLIN} };
int p_sz = sizeof(plist) / sizeof(struct pollfd);
int br;
for (int rval; (rval=poll(plist, p_sz, -1)) > 0; ) {
if (plist[0].revents & POLLIN) {
br = read(ci.p_stderr, buffer, sizeof(buffer)-1);
printf("-> read %d bytes from %s stderr:\n", br, args[0]);
buffer[br] = '\0';
printf("%s\n", buffer);
} else {
break; // nothing left to read
}
}
waitpid(ci.pid, &exit_code, 0);
printf("Done %d\n", exit_code);
// close the stiin pipe to allow wc to end!
close(ci2.p_stdin);
ci2.p_stdin = -1;
struct pollfd plist2[] = { {ci2.p_stdout, POLLIN} };
int p2_sz = sizeof(plist2) / sizeof(struct pollfd);
for (int rval; (rval=poll(plist2, p2_sz, -1)) > 0; ) {
if (plist2[0].revents & POLLIN) {
int br = read(ci2.p_stdout, buffer, sizeof(buffer)-1);
printf("-> read %d bytes from %s stdout:\n", br, args2[0]);
buffer[br] = '\0';
printf("%s\n", buffer);
} else {
break; // nothing left to read
}
}
waitpid(ci2.pid, &exit_code, 0);
printf("Done2 %d\n", exit_code);
close_ProcInfo(&ci);
close_ProcInfo(&ci2);
ProcInfo ci3 = {.p_stdin=-1, .p_stdout=-1, .p_stderr=-1,
.f_stdin="wc_out.txt", .stdin_type=PROC_COM_PATH};
subprocess(&ci3, args2, NULL);
waitpid(ci3.pid, &exit_code, 0);
printf("Done3 %d\n", exit_code);
close_ProcInfo(&ci3);
printf("snprintf: %d\n", snprintf(NULL, 0, "This is a test %d!", exit_code));
return 0;
}