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
|
#include <fcntl.h>
#include "libbb.h"
typedef struct {
PROCESS_INFORMATION piProcInfo;
HANDLE pipe[2];
char mode;
int fd;
} pipe_data;
static pipe_data *pipes = NULL;
static int num_pipes = 0;
static int mingw_pipe(HANDLE *readwrite)
{
SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(sa); /* Length in bytes */
sa.bInheritHandle = 1; /* the child must inherit these handles */
sa.lpSecurityDescriptor = NULL;
if ( !CreatePipe (&readwrite[0], &readwrite[1], &sa, 1 << 13) ) {
return -1;
}
return 0;
}
static pipe_data *find_pipe(void)
{
int i;
pipe_data *p = NULL;
/* find an unused pipe structure */
for ( i=0; i<num_pipes; ++i ) {
if ( pipes[i].mode == '\0' ) {
p = pipes+i;
break;
}
}
if ( p == NULL ) {
/* need to extend array */
if ( (p=realloc(pipes, sizeof(pipe_data)*(num_pipes+10))) == NULL ) {
return NULL;
}
pipes = p;
for ( i=0; i<10; ++i ) {
memset(pipes+num_pipes+i, 0, sizeof(pipe_data));
}
p = pipes + num_pipes;
num_pipes += 10;
}
p->pipe[0] = INVALID_HANDLE_VALUE;
p->pipe[1] = INVALID_HANDLE_VALUE;
return p;
}
FILE *mingw_popen(const char *cmd, const char *mode)
{
pipe_data *p;
FILE *fptr = NULL;
STARTUPINFO siStartInfo;
int success;
int fd;
int len, count;
int ip, ic;
char *cmd_buff = NULL;
const char *s;
char *t;
if ( cmd == NULL || *cmd == '\0' || mode == NULL ||
(*mode != 'r' && *mode != 'w') ) {
return NULL;
}
/* find an unused pipe structure */
if ( (p=find_pipe()) == NULL ) {
return NULL;
}
/* count double quotes */
count = 0;
for ( s=cmd; *s; ++s ) {
if ( *s == '"' ) {
++count;
}
}
len = strlen(cmd) + 10 + count;
if ( (cmd_buff=malloc(len)) == NULL ) {
return NULL;
}
/* escape double quotes */
strcpy(cmd_buff, "sh -c \"");
for ( s=cmd,t=cmd_buff+strlen(cmd_buff); *s; ++s ) {
if ( *s == '"' ) {
*t++ = '\\';
}
*t++ = *s;
}
*t++ = '"';
*t = '\0';
/* Create the pipe */
if ( mingw_pipe(p->pipe) == -1 ) {
goto finito;
}
/* index of parent end of pipe */
ip = !(*mode == 'r');
/* index of child end of pipe */
ic = (*mode == 'r');
/* Make the parent end of the pipe non-inheritable */
SetHandleInformation(p->pipe[ip], HANDLE_FLAG_INHERIT, 0);
/* Now create the child process */
ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
siStartInfo.cb = sizeof(STARTUPINFO);
if ( *mode == 'r' ) {
siStartInfo.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
siStartInfo.hStdOutput = p->pipe[ic];
}
else {
siStartInfo.hStdInput = p->pipe[ic];
siStartInfo.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
}
siStartInfo.hStdError = GetStdHandle(STD_ERROR_HANDLE);
siStartInfo.wShowWindow = SW_HIDE;
siStartInfo.dwFlags = STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW;
success = CreateProcess(NULL,
(LPTSTR)cmd_buff, /* command line */
NULL, /* process security attributes */
NULL, /* primary thread security attributes */
TRUE, /* handles are inherited */
0, /* creation flags */
NULL, /* use parent's environment */
NULL, /* use parent's current directory */
&siStartInfo, /* STARTUPINFO pointer */
&p->piProcInfo); /* receives PROCESS_INFORMATION */
if ( !success ) {
goto finito;
}
/* close child end of pipe */
CloseHandle(p->pipe[ic]);
p->pipe[ic] = INVALID_HANDLE_VALUE;
if ( *mode == 'r' ) {
fd = _open_osfhandle((intptr_t)p->pipe[ip], _O_RDONLY|_O_BINARY);
fptr = _fdopen(fd, "rb");
}
else {
fd = _open_osfhandle((intptr_t)p->pipe[ip], _O_WRONLY|_O_BINARY);
fptr = _fdopen(fd, "wb");
}
finito:
if ( !fptr ) {
if ( p->pipe[0] != INVALID_HANDLE_VALUE ) {
CloseHandle(p->pipe[0]);
}
if ( p->pipe[1] != INVALID_HANDLE_VALUE ) {
CloseHandle(p->pipe[1]);
}
}
else {
p->mode = *mode;
p->fd = fd;
}
free(cmd_buff);
return fptr;
}
/*
* Open a pipe to a command where the file descriptor fd0 is used
* as input to the command (read mode) or as the destination of the
* output from the command (write mode). The pid of the command is
* returned in the variable pid, which can be NULL.
*/
int mingw_popen_fd(const char *cmd, const char *mode, int fd0, pid_t *pid)
{
pipe_data *p;
STARTUPINFO siStartInfo;
int success;
int fd = -1;
int ip, ic;
if ( cmd == NULL || *cmd == '\0' || mode == NULL ||
(*mode != 'r' && *mode != 'w') ) {
return -1;
}
/* find an unused pipe structure */
if ( (p=find_pipe()) == NULL ) {
return -1;
}
/* Create the pipe */
if ( mingw_pipe(p->pipe) == -1 ) {
goto finito;
}
/* index of parent end of pipe */
ip = !(*mode == 'r');
/* index of child end of pipe */
ic = (*mode == 'r');
/* Make the parent end of the pipe non-inheritable */
SetHandleInformation(p->pipe[ip], HANDLE_FLAG_INHERIT, 0);
/* Now create the child process */
ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
siStartInfo.cb = sizeof(STARTUPINFO);
if ( *mode == 'r' ) {
siStartInfo.hStdInput = (HANDLE)_get_osfhandle(fd0);
siStartInfo.hStdOutput = p->pipe[ic];
}
else {
siStartInfo.hStdInput = p->pipe[ic];
siStartInfo.hStdOutput = (HANDLE)_get_osfhandle(fd0);
}
siStartInfo.hStdError = GetStdHandle(STD_ERROR_HANDLE);
siStartInfo.wShowWindow = SW_HIDE;
siStartInfo.dwFlags = STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW;
success = CreateProcess(NULL,
(LPTSTR)cmd, /* command line */
NULL, /* process security attributes */
NULL, /* primary thread security attributes */
TRUE, /* handles are inherited */
0, /* creation flags */
NULL, /* use parent's environment */
NULL, /* use parent's current directory */
&siStartInfo, /* STARTUPINFO pointer */
&p->piProcInfo); /* receives PROCESS_INFORMATION */
if ( !success ) {
goto finito;
}
/* close child end of pipe */
CloseHandle(p->pipe[ic]);
p->pipe[ic] = INVALID_HANDLE_VALUE;
if ( *mode == 'r' ) {
fd = _open_osfhandle((intptr_t)p->pipe[ip], _O_RDONLY|_O_BINARY);
}
else {
fd = _open_osfhandle((intptr_t)p->pipe[ip], _O_WRONLY|_O_BINARY);
}
finito:
if ( fd == -1 ) {
if ( p->pipe[0] != INVALID_HANDLE_VALUE ) {
CloseHandle(p->pipe[0]);
}
if ( p->pipe[1] != INVALID_HANDLE_VALUE ) {
CloseHandle(p->pipe[1]);
}
}
else {
p->mode = *mode;
p->fd = fd;
if ( pid ) {
*pid = (pid_t)p->piProcInfo.dwProcessId;
}
}
return fd;
}
int mingw_pclose(FILE *fp)
{
int i, ip, fd;
pipe_data *p = NULL;
DWORD ret;
if ( fp == NULL ) {
return -1;
}
/* find struct containing fd */
fd = fileno(fp);
for ( i=0; i<num_pipes; ++i ) {
if ( pipes[i].mode && pipes[i].fd == fd ) {
p = pipes+i;
break;
}
}
if ( p == NULL ) {
/* no pipe data, maybe fd isn't a pipe? */
return -1;
}
fclose(fp);
ip = !(p->mode == 'r');
CloseHandle(p->pipe[ip]);
ret = WaitForSingleObject(p->piProcInfo.hProcess, INFINITE);
CloseHandle(p->piProcInfo.hProcess);
CloseHandle(p->piProcInfo.hThread);
p->mode = '\0';
return (ret == WAIT_OBJECT_0) ? 0 : -1;
}
|