-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDemo.Shell.Runner.pas
More file actions
356 lines (296 loc) · 10.6 KB
/
Demo.Shell.Runner.pas
File metadata and controls
356 lines (296 loc) · 10.6 KB
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
345
346
347
348
349
350
351
352
353
354
355
356
unit Demo.Shell.Runner;
interface
uses
System.SysUtils, System.Classes, Winapi.Windows;
type
/// <summary>
/// Outcome of a single Run() call.
/// <para>
/// Success = True when the process actually launched and ran to completion
/// (regardless of its exit code). The exit code, stdout and stderr are valid.
/// </para>
/// <para>
/// Success = False with TimedOut = True means the process was forcefully
/// terminated by the runner because it exceeded the timeout. StdOut and
/// StdErr may contain partial output gathered up to that point.
/// </para>
/// <para>
/// Success = False with TimedOut = False means the process could not be
/// launched at all (executable not found, working directory invalid, ...).
/// ErrorMessage describes the cause.
/// </para>
/// </summary>
TShellRunResult = record
Success: Boolean;
ExitCode: Integer;
StdOut: string;
StdErr: string;
TimedOut: Boolean;
ErrorMessage: string;
end;
/// <summary>
/// Generic blocking process runner with stdout / stderr capture and a
/// hard timeout. Reusable by any future command plugin that needs to
/// shell out to an external binary (/git, /npm, /docker logs, ...).
/// </summary>
IShellRunner = interface
['{3D4E1A8F-7B2C-4F94-8D5E-A2C9B7D4F6E1}']
function Run(const AExecutable: string; const AArgs: TArray<string>;
const AWorkingDir: string; const ATimeoutMs: Cardinal): TShellRunResult;
end;
/// <summary>
/// Default Windows implementation. Uses CreateProcess + anonymous pipes.
/// Stdout and stderr are captured concurrently by two reader threads to
/// avoid the well-known pipe-buffer deadlock.
/// </summary>
TShellRunner = class(TInterfacedObject, IShellRunner)
public
function Run(const AExecutable: string; const AArgs: TArray<string>;
const AWorkingDir: string; const ATimeoutMs: Cardinal): TShellRunResult;
end;
implementation
{$REGION 'Dev notes'}
(*
Developer Note - Shell runner
Why a new abstraction
The project ships TProcessExecute (Windows.Process.Execution.pas), but
it only calls ShellExecute and discards stdout. Command plugins that
want the OUTPUT of a process (git, npm, docker, ...) need a different
primitive: launch, wait, capture both streams, enforce a timeout,
surface the exit code.
IShellRunner is that primitive. It deliberately stays generic so that
future plugins can share it without each one re-implementing pipe
management.
Why two reader threads
Anonymous pipe buffers on Windows are typically 4 KB to 64 KB. If a
child process writes a lot to stdout while its stderr buffer is full
(or vice versa), it blocks on WriteFile. The parent must then drain
both pipes concurrently, otherwise it deadlocks: the parent waits on
the process, the process waits for the parent to read.
The runner spawns one TPipeReader per pipe; each reader owns its
handle and closes it on destruction. ReadFile in the reader exits
naturally when the write end is closed (child exit or termination).
Timeout policy
Run waits on the process handle for ATimeoutMs. On WAIT_TIMEOUT it
calls TerminateProcess, then waits indefinitely for the child to
actually be gone (which closes its write ends). Reader threads then
exit, the runner gathers partial output and returns TimedOut = True.
The caller decides what timeout is acceptable. A demo /git plugin
typically uses 5 s; a /docker logs plugin might allow 30 s.
Command-line quoting
Args are wrapped in "..." when they contain space, tab or quote.
Embedded quotes are escaped as \". This is sufficient for typical
git refs, file paths and line ranges, but NOT a general-purpose
Win32 quoting routine. Plugin services should additionally validate
args against option-injection (refusing args that start with '-').
UTF-8 assumption
Both pipes are decoded as UTF-8. For git, callers should pass
"-c core.quotepath=false -c i18n.logoutputencoding=utf-8" through
AArgs to force UTF-8 output regardless of the user's git config.
For other tools, this concern moves to the calling service.
Decoding is non-strict: see DecodeUtf8Lossy. Both TEncoding.UTF8
and TEncoding.GetEncoding(CP_UTF8) instantiate the same
TUTF8Encoding class, which runs MultiByteToWideChar with
MB_ERR_INVALID_CHARS and raises EEncodingError on the first
malformed byte ("Aucun mappage pour le caractère Unicode..." on
a French Windows). git diff emits file content byte-for-byte and
i18n.logoutputencoding has no effect on it, so any tracked file
saved in CP1252 (or any non-UTF-8 encoding) would otherwise blow
up the whole call. Calling MultiByteToWideChar directly with
dwFlags = 0 substitutes U+FFFD and lets the caller surface
whatever git produced.
*)
{$ENDREGION}
type
TPipeReader = class(TThread)
strict private
FPipe: THandle;
FBuffer: TBytesStream;
protected
procedure Execute; override;
public
constructor Create(APipe: THandle);
destructor Destroy; override;
function GetBytes: TBytes;
end;
{ TPipeReader }
constructor TPipeReader.Create(APipe: THandle);
begin
inherited Create(False);
FPipe := APipe;
FBuffer := TBytesStream.Create;
end;
destructor TPipeReader.Destroy;
begin
if FPipe <> 0 then
CloseHandle(FPipe);
FBuffer.Free;
inherited;
end;
procedure TPipeReader.Execute;
const
CHUNK = 4096;
var
Buf: array[0..CHUNK - 1] of Byte;
NumRead: DWORD;
begin
while not Terminated do
begin
NumRead := 0;
if not ReadFile(FPipe, Buf, CHUNK, NumRead, nil) then
Break;
if NumRead = 0 then
Break;
FBuffer.WriteBuffer(Buf, NumRead);
end;
end;
function TPipeReader.GetBytes: TBytes;
begin
Result := Copy(FBuffer.Bytes, 0, FBuffer.Size);
end;
{--- NOTE: Lossy UTF-8 decode.
TEncoding.UTF8 (and TEncoding.GetEncoding(CP_UTF8), which returns the
same TUTF8Encoding class) is built with MB_ERR_INVALID_CHARS, so any
invalid byte sequence raises EEncodingError.
Going through MultiByteToWideChar with dwFlags = 0 lets Windows
substitute invalid sequences with U+FFFD instead of failing. }
function DecodeUtf8Lossy(const Bytes: TBytes): string;
begin
if Length(Bytes) = 0 then
Exit('');
var WLen := MultiByteToWideChar(CP_UTF8, 0,
PAnsiChar(@Bytes[0]), Length(Bytes), nil, 0);
if WLen <= 0 then
Exit('');
SetLength(Result, WLen);
MultiByteToWideChar(CP_UTF8, 0,
PAnsiChar(@Bytes[0]), Length(Bytes), PChar(Result), WLen);
end;
function QuoteIfNeeded(const S: string): string;
begin
if S.IsEmpty then
Exit('""');
if S.IndexOfAny([' ', #9, '"']) < 0 then
Exit(S);
Result := '"' + S.Replace('"', '\"') + '"';
end;
function BuildCommandLine(const Exe: string;
const Args: TArray<string>): string;
begin
Result := QuoteIfNeeded(Exe);
for var A in Args do
Result := Result + ' ' + QuoteIfNeeded(A);
end;
function MakeFail(const AMessage: string): TShellRunResult;
begin
Result := Default(TShellRunResult);
Result.Success := False;
Result.ErrorMessage := AMessage;
end;
{ TShellRunner }
function TShellRunner.Run(const AExecutable: string;
const AArgs: TArray<string>; const AWorkingDir: string;
const ATimeoutMs: Cardinal): TShellRunResult;
var
SecAttr: TSecurityAttributes;
OutR, OutW, ErrR, ErrW: THandle;
SI: TStartupInfo;
PI: TProcessInformation;
WorkDirPtr: PChar;
CmdLine: string;
CmdLineBuf: array of Char;
ExitCode: DWORD;
WaitRes: DWORD;
begin
Result := Default(TShellRunResult);
SecAttr.nLength := SizeOf(SecAttr);
SecAttr.lpSecurityDescriptor := nil;
SecAttr.bInheritHandle := True;
OutR := 0; OutW := 0; ErrR := 0; ErrW := 0;
if not CreatePipe(OutR, OutW, @SecAttr, 0) then
Exit(MakeFail('CreatePipe (stdout) failed'));
if not SetHandleInformation(OutR, HANDLE_FLAG_INHERIT, 0) then
begin
CloseHandle(OutR);
CloseHandle(OutW);
Exit(MakeFail('SetHandleInformation (stdout) failed'));
end;
if not CreatePipe(ErrR, ErrW, @SecAttr, 0) then
begin
CloseHandle(OutR);
CloseHandle(OutW);
Exit(MakeFail('CreatePipe (stderr) failed'));
end;
if not SetHandleInformation(ErrR, HANDLE_FLAG_INHERIT, 0) then
begin
CloseHandle(OutR); CloseHandle(OutW);
CloseHandle(ErrR); CloseHandle(ErrW);
Exit(MakeFail('SetHandleInformation (stderr) failed'));
end;
CmdLine := BuildCommandLine(AExecutable, AArgs);
{--- CreateProcessW requires a writable command-line buffer. }
SetLength(CmdLineBuf, Length(CmdLine) + 1);
Move(PChar(CmdLine)^, CmdLineBuf[0],
(Length(CmdLine) + 1) * SizeOf(Char));
ZeroMemory(@SI, SizeOf(SI));
SI.cb := SizeOf(SI);
SI.dwFlags := STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW;
SI.wShowWindow := SW_HIDE;
SI.hStdInput := GetStdHandle(STD_INPUT_HANDLE);
SI.hStdOutput := OutW;
SI.hStdError := ErrW;
ZeroMemory(@PI, SizeOf(PI));
if AWorkingDir = '' then
WorkDirPtr := nil
else
WorkDirPtr := PChar(AWorkingDir);
if not CreateProcess(nil, PChar(@CmdLineBuf[0]), nil, nil, True,
CREATE_NO_WINDOW, nil, WorkDirPtr, SI, PI) then
begin
var LastErr := GetLastError;
CloseHandle(OutR); CloseHandle(OutW);
CloseHandle(ErrR); CloseHandle(ErrW);
Exit(MakeFail(SysErrorMessage(LastErr)));
end;
{--- The parent must close its inheritable copies of the write ends so
that ReadFile returns EOF once the child terminates. }
CloseHandle(OutW); OutW := 0;
CloseHandle(ErrW); ErrW := 0;
{--- Each reader takes ownership of its read handle and closes it on
destruction. }
var OutReader := TPipeReader.Create(OutR); OutR := 0;
var ErrReader := TPipeReader.Create(ErrR); ErrR := 0;
try
WaitRes := WaitForSingleObject(PI.hProcess, ATimeoutMs);
if WaitRes = WAIT_TIMEOUT then
begin
TerminateProcess(PI.hProcess, 1);
WaitForSingleObject(PI.hProcess, INFINITE);
Result.TimedOut := True;
end;
OutReader.WaitFor;
ErrReader.WaitFor;
Result.StdOut := DecodeUtf8Lossy(OutReader.GetBytes);
Result.StdErr := DecodeUtf8Lossy(ErrReader.GetBytes);
finally
OutReader.Free;
ErrReader.Free;
end;
ExitCode := 1;
GetExitCodeProcess(PI.hProcess, ExitCode);
CloseHandle(PI.hThread);
CloseHandle(PI.hProcess);
if Result.TimedOut then
begin
Result.Success := False;
Result.ExitCode := -1;
Result.ErrorMessage :=
Format('Process timed out after %d ms', [ATimeoutMs]);
end
else
begin
Result.Success := True;
Result.ExitCode := Integer(ExitCode);
end;
end;
end.