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
|
// file : butl/process.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd
// license : MIT; see accompanying LICENSE file
#include <butl/process>
#ifndef _WIN32
# include <unistd.h> // execvp, fork, dup2, pipe, chdir, *_FILENO
# include <sys/wait.h> // waitpid
#else
# ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
# endif
# include <windows.h> // CreatePipe, CreateProcess
# include <io.h> // _open_osfhandle
# include <fcntl.h> // _O_TEXT
#endif
#include <cassert>
using namespace std;
namespace butl
{
#ifndef _WIN32
process::
process (const char* cwd, char const* args[], int in, int out, int err)
{
int out_fd[2] = {in, 0};
int in_ofd[2] = {0, out};
int in_efd[2] = {0, err};
if ((in == -1 && pipe (out_fd) == -1) ||
(out == -1 && pipe (in_ofd) == -1) ||
(err == -1 && pipe (in_efd) == -1))
throw process_error (errno, false);
id = fork ();
if (id == -1)
throw process_error (errno, false);
if (id == 0)
{
// Child. If requested, close the write end of the pipe and duplicate
// the read end to stdin. Then close the original read end descriptor.
//
if (in != STDIN_FILENO)
{
if ((in == -1 && close (out_fd[1]) == -1) ||
dup2 (out_fd[0], STDIN_FILENO) == -1 ||
(in == -1 && close (out_fd[0]) == -1))
throw process_error (errno, true);
}
// Do the same for the stdout if requested.
//
if (out != STDOUT_FILENO)
{
if ((out == -1 && close (in_ofd[0]) == -1) ||
dup2 (in_ofd[1], STDOUT_FILENO) == -1 ||
(out == -1 && close (in_ofd[1]) == -1))
throw process_error (errno, true);
}
// Do the same for the stderr if requested.
//
if (err != STDERR_FILENO)
{
if ((err == -1 && close (in_efd[0]) == -1) ||
dup2 (in_efd[1], STDERR_FILENO) == -1 ||
(err == -1 && close (in_efd[1]) == -1))
throw process_error (errno, true);
}
// Change current working directory if requested.
//
if (cwd != nullptr && chdir (cwd) != 0)
throw process_error (errno, true);
if (execvp (args[0], const_cast<char**> (&args[0])) == -1)
throw process_error (errno, true);
}
else
{
// Parent. Close the other ends of the pipes.
//
if ((in == -1 && close (out_fd[0]) == -1) ||
(out == -1 && close (in_ofd[1]) == -1) ||
(err == -1 && close (in_efd[1]) == -1))
throw process_error (errno, false);
}
this->out_fd = in == -1 ? out_fd[1] : -1;
this->in_ofd = out == -1 ? in_ofd[0] : -1;
this->in_efd = err == -1 ? in_efd[0] : -1;
}
process::
process (const char* cwd, char const* args[], process& in, int out, int err)
: process (cwd, args, in.in_ofd, out, err)
{
assert (in.in_ofd != -1); // Should be a pipe.
close (in.in_ofd); // Close it on our side.
}
bool process::
wait ()
{
if (id != 0)
{
int r (waitpid (id, &status, 0));
id = 0; // We have tried.
if (r == -1)
throw process_error (errno, false);
}
return WIFEXITED (status) && WEXITSTATUS (status) == 0;
}
#else // _WIN32
static void
print_error (char const* name)
{
LPTSTR msg;
DWORD e (GetLastError());
if (!FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
0,
e,
MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &msg,
0,
0))
{
cerr << name << ": error: unknown error code " << e << endl;
return;
}
cerr << name << ": error: " << msg << endl;
LocalFree (msg);
}
static process_info
start_process (char const* args[], char const* name, bool err, bool out)
{
HANDLE out_h[2];
HANDLE in_eh[2];
HANDLE in_oh[2];
SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof (SECURITY_ATTRIBUTES);
sa.bInheritHandle = true;
sa.lpSecurityDescriptor = 0;
if (!CreatePipe (&out_h[0], &out_h[1], &sa, 0) ||
!SetHandleInformation (out_h[1], HANDLE_FLAG_INHERIT, 0))
{
print_error (name);
throw process_failure ();
}
if (err)
{
if (!CreatePipe (&in_eh[0], &in_eh[1], &sa, 0) ||
!SetHandleInformation (in_eh[0], HANDLE_FLAG_INHERIT, 0))
{
print_error (name);
throw process_failure ();
}
}
if (out)
{
if (!CreatePipe (&in_oh[0], &in_oh[1], &sa, 0) ||
!SetHandleInformation (in_oh[0], HANDLE_FLAG_INHERIT, 0))
{
print_error (name);
throw process_failure ();
}
}
// Create the process.
//
path file (args[0]);
// Do PATH search.
//
if (file.directory ().empty ())
file = path_search (file);
if (file.empty ())
{
cerr << args[0] << ": error: file not found" << endl;
throw process_failure ();
}
// Serialize the arguments to string.
//
string cmd_line;
for (char const** p (args); *p != 0; ++p)
{
if (p != args)
cmd_line += ' ';
// On Windows we need to protect values with spaces using quotes.
// Since there could be actual quotes in the value, we need to
// escape them.
//
string a (*p);
bool quote (a.find (' ') != string::npos);
if (quote)
cmd_line += '"';
for (size_t i (0); i < a.size (); ++i)
{
if (a[i] == '"')
cmd_line += "\\\"";
else
cmd_line += a[i];
}
if (quote)
cmd_line += '"';
}
// Prepare other info.
//
STARTUPINFO si;
PROCESS_INFORMATION pi;
memset (&si, 0, sizeof (STARTUPINFO));
memset (&pi, 0, sizeof (PROCESS_INFORMATION));
si.cb = sizeof(STARTUPINFO);
if (err)
si.hStdError = in_eh[1];
else
si.hStdError = GetStdHandle (STD_ERROR_HANDLE);
if (out)
si.hStdOutput = in_oh[1];
else
si.hStdOutput = GetStdHandle (STD_OUTPUT_HANDLE);
si.hStdInput = out_h[0];
si.dwFlags |= STARTF_USESTDHANDLES;
if (!CreateProcess (
file.string ().c_str (),
const_cast<char*> (cmd_line.c_str ()),
0, // Process security attributes.
0, // Primary thread security attributes.
true, // Inherit handles.
0, // Creation flags.
0, // Use our environment.
0, // Use our current directory.
&si,
&pi))
{
print_error (name);
throw process_failure ();
}
CloseHandle (pi.hThread);
CloseHandle (out_h[0]);
if (err)
CloseHandle (in_eh[1]);
if (out)
CloseHandle (in_oh[1]);
process_info r;
r.id = pi.hProcess;
r.out_fd = _open_osfhandle ((intptr_t) (out_h[1]), 0);
if (r.out_fd == -1)
{
cerr << name << ": error: unable to obtain C file handle" << endl;
throw process_failure ();
}
if (err)
{
// Pass _O_TEXT to get newline translation.
//
r.in_efd = _open_osfhandle ((intptr_t) (in_eh[0]), _O_TEXT);
if (r.in_efd == -1)
{
cerr << name << ": error: unable to obtain C file handle" << endl;
throw process_failure ();
}
}
else
r.in_efd = 0;
if (out)
{
// Pass _O_TEXT to get newline translation.
//
r.in_ofd = _open_osfhandle ((intptr_t) (in_oh[0]), _O_TEXT);
if (r.in_ofd == -1)
{
cerr << name << ": error: unable to obtain C file handle" << endl;
throw process_failure ();
}
}
else
r.in_ofd = 0;
return r;
}
static bool
wait_process (process_info pi, char const* name)
{
DWORD status;
if (WaitForSingleObject (pi.id, INFINITE) != WAIT_OBJECT_0 ||
!GetExitCodeProcess (pi.id, &status))
{
print_error (name);
throw process_failure ();
}
CloseHandle (pi.id);
return status == 0;
}
#endif // _WIN32
}
|