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
|
// slang-unix-process-util.cpp
#include "../slang-process-util.h"
#include "../slang-common.h"
#include "../slang-string-util.h"
#include <stdio.h>
#include <stdlib.h>
//#include <dirent.h>
#include <errno.h>
#include <poll.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <time.h>
namespace Slang {
/* static */UnownedStringSlice ProcessUtil::getExecutableSuffix()
{
#if __CYGWIN__
return UnownedStringSlice::fromLiteral(".exe");
#else
return UnownedStringSlice::fromLiteral("");
#endif
}
/* static */void ProcessUtil::appendCommandLineEscaped(const UnownedStringSlice& slice, StringBuilder& out)
{
// TODO(JS): This escaping is not complete... !
if (slice.indexOf(' ') >= 0 || slice.indexOf('"') >= 0)
{
out << "\"";
const char* cur = slice.begin();
const char* end = slice.end();
while (cur < end)
{
char c = *cur++;
switch (c)
{
case '\"':
{
// Escape quotes.
out << "\\\"";
break;
}
default:
out.append(c);
}
}
out << "\"";
return;
}
out << slice;
}
/* static */String ProcessUtil::getCommandLineString(const CommandLine& commandLine)
{
// When outputting the command line we potentially need to escape the path to the
// command and args - that aren't already explicitly marked as escaped.
StringBuilder cmd;
appendCommandLineEscaped(commandLine.m_executable.getUnownedSlice(), cmd);
for (const auto& arg : commandLine.m_args)
{
cmd << " ";
if (arg.type == CommandLine::ArgType::Unescaped)
{
appendCommandLineEscaped(arg.value.getUnownedSlice(), cmd);
}
else
{
cmd << arg.value;
}
}
return cmd.ToString();
}
/* static */SlangResult ProcessUtil::execute(const CommandLine& commandLine, ExecuteResult& outExecuteResult)
{
outExecuteResult.init();
List<char const*> argPtrs;
// Add the command
argPtrs.add(commandLine.m_executable.getBuffer());
// Add all the args - they don't need any explicit escaping
for (auto arg : commandLine.m_args)
{
// All args for this target must be unescaped
SLANG_ASSERT(arg.type == CommandLine::ArgType::Unescaped);
argPtrs.add(arg.value.getBuffer());
}
// Terminate with a null
argPtrs.add(nullptr);
int stdoutPipe[2];
int stderrPipe[2];
if (pipe(stdoutPipe) == -1)
{
fprintf(stderr, "error: `pipe` failed\n");
return SLANG_FAIL;
}
if (pipe(stderrPipe) == -1)
{
fprintf(stderr, "error: `pipe` failed\n");
return SLANG_FAIL;
}
pid_t childProcessID = fork();
if (childProcessID == -1)
{
fprintf(stderr, "error: `fork` failed\n");
return SLANG_FAIL;
}
if (childProcessID == 0)
{
// We are the child process.
dup2(stdoutPipe[1], STDOUT_FILENO);
dup2(stderrPipe[1], STDERR_FILENO);
close(stdoutPipe[0]);
close(stdoutPipe[1]);
close(stderrPipe[0]);
close(stderrPipe[1]);
execvp(argPtrs[0], (char* const*)&argPtrs[0]);
// If we get here, then `exec` failed
fprintf(stderr, "error: `exec` failed\n");
return SLANG_FAIL;
}
else
{
// We are the parent process
close(stdoutPipe[1]);
close(stderrPipe[1]);
int stdoutFD = stdoutPipe[0];
int stderrFD = stderrPipe[0];
pollfd pollInfos[2];
nfds_t pollInfoCount = 2;
pollInfos[0].fd = stdoutFD;
pollInfos[0].events = POLLIN;
pollInfos[0].revents = 0;
pollInfos[1].fd = stderrFD;
pollInfos[1].events = POLLIN;
pollInfos[1].revents = 0;
int remainingCount = 2;
int iterations = 0;
while (remainingCount)
{
// Safeguard against infinite loop:
iterations++;
if (iterations > 10000)
{
fprintf(stderr, "poll(): %d iterations\n", iterations);
return SLANG_FAIL;
}
// Set a timeout of twenty seconds;
// we really shouldn't wait too long...
int pollTimeout = 20000;
int pollResult = poll(pollInfos, pollInfoCount, pollTimeout);
if (pollResult <= 0)
{
// If there was a signal that got in
// the way, then retry...
if (pollResult == -1 && errno == EINTR)
continue;
// timeout or error...
fprintf(stderr, "error: `poll` failed or timed out\n");
return SLANG_FAIL;
}
enum { kBufferSize = 1024 };
char buffer[kBufferSize];
if (pollInfos[0].revents)
{
auto count = read(stdoutFD, buffer, kBufferSize);
if (count <= 0)
{
// end-of-file
close(stdoutFD);
pollInfos[0].fd = -1;
remainingCount--;
}
outExecuteResult.standardOutput.append(buffer, buffer + count);
}
if (pollInfos[1].revents)
{
auto count = read(stderrFD, buffer, kBufferSize);
if (count <= 0)
{
// end-of-file
close(stderrFD);
pollInfos[1].fd = -1;
remainingCount--;
}
outExecuteResult.standardError.append(buffer, buffer + count);
}
}
int childStatus = 0;
iterations = 0;
for (;;)
{
// Safeguard against infinite loop:
iterations++;
if (iterations > 10000)
{
fprintf(stderr, "waitpid(): %d iterations\n", iterations);
return SLANG_FAIL;
}
pid_t terminatedProcessID = waitpid(childProcessID, &childStatus, 0);
if (terminatedProcessID == -1)
{
fprintf(stderr, "error: `waitpid` failed\n");
return SLANG_FAIL;
}
if (terminatedProcessID == childProcessID)
{
if (WIFEXITED(childStatus))
{
outExecuteResult.resultCode = (int)(int8_t)WEXITSTATUS(childStatus);
}
else
{
outExecuteResult.resultCode = 1;
}
return SLANG_OK;
}
}
}
return SLANG_FAIL;
}
/* static */uint64_t ProcessUtil::getClockFrequency()
{
return 1000000000;
}
/* static */uint64_t ProcessUtil::getClockTick()
{
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
return uint64_t(now.tv_sec) * 1000000000 + now.tv_nsec;
}
} // namespace Slang
|