/
shellcmd.c
344 lines (315 loc) · 7.55 KB
/
shellcmd.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#include <stdio.h>
#include <errno.h>
#include <malloc.h>
#include <unistd.h>
#include <sys/types.h>
#include <poll.h>
/**
* mypopen will run the p_cmdstring as a shell command.
*
* mypopen will return 0 if succeed, and *p_pid will be the child process's pid.
* *p_stdout_fd will be the fd of the child process's standard output stream.
* And *p_stderr_fd will be the fd of the child process's standard error stream.
*
* If mypopen failed, p_pid, p_stdout_fd, p_stderr_fd are undifined and should
* be ignored.
*
* If succeed, p_stdout_fd and p_stderr_fd can be read the obtain the standard
* output and error of the command. You must ensure to invoke mypclose to wait
* for the exit of the child process.
*
*/
static int mypopen(const char *p_cmdstring, int *p_pid, int *p_stdout_fd, int *p_stderr_fd)
{
int rc = 0;
int pid = 0;
int out[2];
int err[2];
rc = pipe(out);
if (rc < 0)
{
goto l_out;
}
rc = pipe(err);
if (rc < 0)
{
goto l_error_err;
}
if ((pid = fork()) < 0) {
rc = pid;
goto l_error_fork;
} else if (pid == 0) { /* child */
close(0);
close(out[0]);
close(err[0]);
if (out[1] != STDOUT_FILENO) {
dup2(out[1], STDOUT_FILENO);
close(out[1]);
}
if (err[1] != STDERR_FILENO) {
dup2(err[1], STDERR_FILENO);
close(err[1]);
}
execl("/bin/sh", "sh", "-c", p_cmdstring, NULL);
_exit(127);
}
/* parent continues... */
close(out[1]);
close(err[1]);
*p_pid = pid;
*p_stdout_fd = out[0];
*p_stderr_fd = err[0];
l_out:
return rc;
l_error_fork:
close(err[0]);
close(err[1]);
l_error_err:
close(out[0]);
close(out[1]);
goto l_out;
}
static int mypclose(int pid, int stdout_fd, int stderr_fd)
{
int status = 0;
close(stdout_fd);
close(stderr_fd);
waitpid(pid, &status, 0);
return status >> 8;
}
static int read_output(int fd, char **pp_buf, int *p_offset,
int *p_size)
{
int rc = 0;
int offset = *p_offset;
int size = *p_size;
char *p_buf = *pp_buf;
char *p_new_buf = NULL;
const int buf_unit = 1024;
if (p_buf == NULL)
{
p_buf = malloc(buf_unit);
if (p_buf == NULL)
{
rc = -1;
goto l_out;
}
size = buf_unit;
offset = 0;
}
if (*p_offset == *p_size)
{
// Expand the buf.
p_new_buf = realloc(p_buf, size + buf_unit);
if (p_new_buf == NULL)
{
rc = -1;
goto l_fail;
}
p_buf = p_new_buf;
size += buf_unit;
}
rc = read(fd, p_buf + offset, size - offset);
if (rc < 0)
{
goto l_fail;
}
offset += rc;
l_out:
*pp_buf = p_buf;
*p_offset = offset;
*p_size = size;
return rc;
l_fail:
free(p_buf);
p_buf = NULL;
size = 0;
offset = 0;
goto l_out;
}
static int read_all_output(
int stdout_fd,
int stderr_fd,
char **pp_stdout,
int *p_stdout_size,
char **pp_stderr,
int *p_stderr_size)
{
int rc = 0;
fd_set rfds;
struct pollfd fds[2];
char *p_stdout_buf = NULL;
char *p_stderr_buf = NULL;
int stdout_offset = 0;
int stderr_offset = 0;
int stdout_buf_size = 0;
int stderr_buf_size = 0;
int maxfd = 0;
int stdout_finished = 0;
int stderr_finished = 0;
fds[0].fd = stdout_fd;
fds[0].events = POLLIN | POLLPRI;
fds[1].fd = stderr_fd;
fds[1].events = POLLIN | POLLPRI;
while (!stdout_finished || !stderr_finished)
{
rc = poll(fds, 2, -1);
if (rc == -1)
{
rc = -errno;
goto l_fail;
}
if (rc == 0)
{
// No fd is ready.
break;
}
if ((fds[0].revents & POLLIN) || (fds[0].revents & POLLPRI) || (fds[0].revents & POLLHUP))
{
// Read standard output.
if (NULL != pp_stdout)
{
rc = read_output(stdout_fd, &p_stdout_buf, &stdout_offset, &stdout_buf_size);
}
else
{
rc = read_output(stdout_fd, NULL, &stdout_offset, &stdout_buf_size);
}
if (rc < 0)
{
goto l_fail;
}
if (rc == 0)
{
stdout_finished = 1;
}
}
else if (fds[0].revents & POLLERR)
{
rc = -1;
goto l_fail;
}
if ((fds[1].revents & POLLIN) || (fds[1].revents & POLLPRI) || (fds[1].revents & POLLHUP))
{
// Read error output.
if (NULL != pp_stdout)
{
rc = read_output(stderr_fd, &p_stderr_buf, &stderr_offset, &stderr_buf_size);
}
else
{
rc = read_output(stderr_fd, NULL, &stderr_offset, &stderr_buf_size);
}
if (rc < 0)
{
goto l_fail;
}
if (rc == 0)
{
stderr_finished = 1;
}
}
else if (fds[1].revents & POLLERR)
{
rc = -1;
goto l_fail;
}
}
l_out:
if (NULL != pp_stdout)
{
*pp_stdout = p_stdout_buf;
}
if (NULL != p_stdout_size)
{
*p_stdout_size = stdout_offset;
}
if (NULL != pp_stderr)
{
*pp_stderr = p_stderr_buf;
}
if (NULL != p_stderr_size)
{
*p_stderr_size = stderr_offset;
}
return rc;
l_fail:
if (p_stdout_buf != NULL)
{
free(p_stdout_buf);
p_stdout_buf = NULL;
stdout_offset = 0;
}
if (p_stderr_buf != NULL)
{
free(p_stderr_buf);
p_stderr_buf = NULL;
stderr_offset = 0;
}
goto l_out;
}
/**
* shellcmd will run the p_cmd as a shell command.
* If return code is 0, then *p_exitcode is the exit status of the shell command,
* *pp_stdout is the standard output buffer, *p_stdout_size is the size of the
* standard output buffer, pp_stderr is the error output buffer, *p_stderr_size
* is the size of the error output buffer.
*
* If failed, all the output parameters' values are undifined and should
* be ignored.
*
* If succeed, *pp_stdout and *pp_stderr must be freed manually.
*
*/
int shellcmd(char *p_cmd, int *p_exitcode, char **pp_stdout,
int *p_stdout_size, char **pp_stderr, int *p_stderr_size)
{
int rc = 0;
int pid = 0;
int stdout_fd = 0;
int stderr_fd = 0;
rc = mypopen(p_cmd, &pid, &stdout_fd, &stderr_fd);
if (rc < 0)
{
return rc;
}
rc = read_all_output(
stdout_fd,
stderr_fd,
pp_stdout,
p_stdout_size,
pp_stderr,
p_stderr_size);
*p_exitcode = mypclose(pid, stdout_fd, stderr_fd);
return rc;
}
int main()
{
int rc = 0;
int exitcode;
char *p_stdout = NULL;
char *p_stderr = NULL;
int stdout_size = 0;
int stderr_size = 0;
rc = shellcmd("ls -l /home;ls notexist", &exitcode, &p_stdout, &stdout_size, &p_stderr,
&stderr_size);
if (rc < 0)
{
printf("failed\n");
return rc;
}
printf("std size:%d\n", stdout_size);
printf("%s", p_stdout);
printf("stderr size:%d\n", stderr_size);
printf("%s", p_stderr);
printf("exit code:%d\n", exitcode);
if (p_stdout != NULL)
{
free(p_stdout);
}
if (p_stderr != NULL)
{
free(p_stderr);
}
return rc;
}