forked from IronLanguages/ironpython3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_subprocess.cs
More file actions
493 lines (415 loc) · 20 KB
/
_subprocess.cs
File metadata and controls
493 lines (415 loc) · 20 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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/* ****************************************************************************
*
* Copyright (c) Microsoft Corporation.
*
* This source code is subject to terms and conditions of the Microsoft Public License. A
* copy of the license can be found in the License.html file at the root of this distribution. If
* you cannot locate the Microsoft Public License, please send an email to
* ironpy@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
* by the terms of the Microsoft Public License.
*
* You must not remove this notice, or any other, from this software.
*
*
* ***************************************************************************/
#if FEATURE_PROCESS
#if CLR2
using Microsoft.Scripting.Math;
#else
using System.Numerics;
#endif
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Text;
using System.Runtime.InteropServices;
using Microsoft.Scripting.Runtime;
using IronPython.Runtime;
using IronPython.Runtime.Exceptions;
using IronPython.Runtime.Operations;
[assembly: PythonModule("_subprocess", typeof(IronPython.Modules.PythonSubprocess))]
namespace IronPython.Modules {
[PythonType("_subprocess")]
public static class PythonSubprocess {
public const string __doc__ = "_subprocess Module";
#region Public API
public static PythonTuple CreatePipe(
CodeContext context,
object pSec /*Python passes None*/,
int bufferSize) {
IntPtr hReadPipe;
IntPtr hWritePipe;
SECURITY_ATTRIBUTES pSecA = new SECURITY_ATTRIBUTES();
pSecA.nLength = Marshal.SizeOf(pSecA);
if (pSec != null) {
/* If pSec paseed in from Python is not NULL
* there needs to be some conversion done here...*/
}
// TODO: handle failures
CreatePipePI(
out hReadPipe,
out hWritePipe,
ref pSecA,
(uint)bufferSize);
return PythonTuple.MakeTuple(
new PythonSubprocessHandle(hReadPipe),
new PythonSubprocessHandle(hWritePipe)
);
}
public static PythonTuple CreateProcess(
CodeContext context,
string applicationName,
string commandLineArgs,
object pSec /*subprocess.py passes None*/,
object tSec /*subprocess.py passes None*/,
int? bInheritHandles,
uint? dwCreationFlags,
object lpEnvironment,
string lpCurrentDirectory,
object lpStartupInfo /* subprocess.py passes STARTUPINFO*/) {
object dwFlags = PythonOps.GetBoundAttr(context, lpStartupInfo, "dwFlags"); //public Int32 dwFlags;
object hStdInput = PythonOps.GetBoundAttr(context, lpStartupInfo, "hStdInput"); //public IntPtr hStdInput;
object hStdOutput = PythonOps.GetBoundAttr(context, lpStartupInfo, "hStdOutput"); //public IntPtr hStdOutput;
object hStdError = PythonOps.GetBoundAttr(context, lpStartupInfo, "hStdError"); //public IntPtr hStdError;
object wShowWindow = PythonOps.GetBoundAttr(context, lpStartupInfo, "wShowWindow"); //Int16 wShowWindow;
Int32 dwFlagsInt32 = dwFlags != null ? Converter.ConvertToInt32(dwFlags) : 0;
IntPtr hStdInputIntPtr = hStdInput != null ? new IntPtr(Converter.ConvertToInt32(hStdInput)) : IntPtr.Zero;
IntPtr hStdOutputIntPtr = hStdOutput != null ? new IntPtr(Converter.ConvertToInt32(hStdOutput)) : IntPtr.Zero;
IntPtr hStdErrorIntPtr = hStdError != null ? new IntPtr(Converter.ConvertToInt32(hStdError)) : IntPtr.Zero;
Int16 wShowWindowInt16 = wShowWindow != null ? Converter.ConvertToInt16(wShowWindow) : (short)0;
STARTUPINFO startupInfo = new STARTUPINFO();
startupInfo.dwFlags = dwFlagsInt32;
startupInfo.hStdInput = hStdInputIntPtr;
startupInfo.hStdOutput = hStdOutputIntPtr;
startupInfo.hStdError = hStdErrorIntPtr;
startupInfo.wShowWindow = wShowWindowInt16;
// No special security
SECURITY_ATTRIBUTES pSecSA = new SECURITY_ATTRIBUTES();
pSecSA.nLength = Marshal.SizeOf(pSecSA);
SECURITY_ATTRIBUTES tSecSA = new SECURITY_ATTRIBUTES();
tSecSA.nLength = Marshal.SizeOf(tSecSA);
if (pSec != null) {
/* If pSec paseed in from Python is not NULL
* there needs to be some conversion done here...*/
}
if (tSec != null) {
/* If tSec paseed in from Python is not NULL
* there needs to be some conversion done here...*/
}
// If needed convert lpEnvironment Dictionary to lpEnvironmentIntPtr
string lpEnvironmentStr = EnvironmentToNative(context, lpEnvironment);
PROCESS_INFORMATION lpProcessInformation = new PROCESS_INFORMATION();
bool result = CreateProcessPI(
String.IsNullOrEmpty(applicationName) ? null : applicationName/*applicationNameHelper*//*processStartInfo.FileName*/,
String.IsNullOrEmpty(commandLineArgs) ? null : commandLineArgs/*commandLineArgsHelper*//*processStartInfo.Arguments*/,
ref pSecSA, ref tSecSA,
bInheritHandles.HasValue && bInheritHandles.Value > 0 ? true : false,
dwCreationFlags.HasValue ? dwCreationFlags.Value : 0,
lpEnvironmentStr,
lpCurrentDirectory,
ref startupInfo,
out lpProcessInformation);
if (!result) {
int error = Marshal.GetLastWin32Error();
throw PythonExceptions.CreateThrowable(PythonExceptions.WindowsError, error, CTypes.FormatError(error));
}
IntPtr hp = lpProcessInformation.hProcess;
IntPtr ht = lpProcessInformation.hThread;
int pid = lpProcessInformation.dwProcessId;
int tid = lpProcessInformation.dwThreadId;
return PythonTuple.MakeTuple(
new PythonSubprocessHandle(hp, true),
new PythonSubprocessHandle(ht),
pid, tid);
}
private static string EnvironmentToNative(CodeContext context, object environment) {
if (environment == null) {
return null;
}
var dict = environment as PythonDictionary ?? new PythonDictionary(context, environment);
var res = new StringBuilder();
foreach (var keyValue in dict) {
res.Append(keyValue.Key);
res.Append('=');
res.Append(keyValue.Value);
res.Append('\0');
}
return res.ToString();
}
/// <summary>
/// Duplicates a subprocess handle which was created for piping.
///
/// This is only called when we're duplicating the handle to make it inheritable to the child process. In CPython
/// the parent handle is always reliably garbage collected. Because we know this handle is not going to be
/// used we close the handle being duplicated.
/// </summary>
public static PythonSubprocessHandle DuplicateHandle(CodeContext context,
BigInteger sourceProcess,
PythonSubprocessHandle handle,
BigInteger targetProcess,
int desiredAccess,
bool inherit_handle,
object DUPLICATE_SAME_ACCESS) {
if (handle._duplicated) {
// more ref counting issues - when stderr is set to subprocess.STDOUT we can't close the target handle so we need
// to track this situation.
return DuplicateHandle(context, sourceProcess, (BigInteger)handle, targetProcess, desiredAccess, inherit_handle, DUPLICATE_SAME_ACCESS);
}
var res = DuplicateHandle(context, sourceProcess, (BigInteger)handle, targetProcess, desiredAccess, inherit_handle, DUPLICATE_SAME_ACCESS);
res._duplicated = true;
handle.Close();
return res;
}
public static PythonSubprocessHandle DuplicateHandle(
CodeContext context,
BigInteger sourceProcess,
BigInteger handle,
BigInteger targetProcess,
int desiredAccess,
bool inherit_handle,
object DUPLICATE_SAME_ACCESS) {
IntPtr currentProcessIntPtr = new IntPtr((long)sourceProcess);
IntPtr handleIntPtr = new IntPtr((long)handle);
IntPtr currentProcess2IntPtr = new IntPtr((long)targetProcess);
IntPtr lpTargetHandle;
bool sameAccess = DUPLICATE_SAME_ACCESS != null && Converter.ConvertToBoolean(DUPLICATE_SAME_ACCESS);
// TODO: handle failures
DuplicateHandlePI(
currentProcessIntPtr,
handleIntPtr,
currentProcess2IntPtr,
out lpTargetHandle,
Converter.ConvertToUInt32(desiredAccess),
inherit_handle,
sameAccess ? (uint)DuplicateOptions.DUPLICATE_SAME_ACCESS : (uint)DuplicateOptions.DUPLICATE_CLOSE_SOURCE
);
return new PythonSubprocessHandle(lpTargetHandle); //Converter.ConvertToBigInteger( lpTargetHandle.ToInt32());
}
public static PythonSubprocessHandle GetCurrentProcess() {
IntPtr id = GetCurrentProcessPI();
return new PythonSubprocessHandle(id);
}
public static int GetExitCodeProcess(PythonSubprocessHandle hProcess) {
if (hProcess._isProcess && hProcess._closed) {
// deal with finalization & resurrection oddness... see PythonSubprocessHandle finalizer
return hProcess._exitCode;
}
IntPtr hProcessIntPtr = new IntPtr(Converter.ConvertToInt32(hProcess));
int exitCode = int.MinValue;
GetExitCodeProcessPI(hProcessIntPtr, out exitCode);
return exitCode;
}
public static string GetModuleFileName(object ignored) {
// Alternative Managed API: System.Diagnostics.ProcessModule.FileName or System.Reflection.Module.FullyQualifiedName.
return System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
}
public static object GetStdHandle(int STD_OUTPUT_HANDLE) {
return GetStdHandlePI(STD_OUTPUT_HANDLE).ToPython();
}
public static int GetVersion() {
return GetVersionPI();
}
public static bool TerminateProcess(
PythonSubprocessHandle handle,
object uExitCode) {
// Alternative Managed API: System.Diagnostics.Process.Kill()
IntPtr hProcessIntPtr = new IntPtr(Converter.ConvertToInt32(handle));
uint uExitCodeUint = Converter.ConvertToUInt32(uExitCode);
bool result = TerminateProcessPI(
hProcessIntPtr,
uExitCodeUint);
return result;
}
public static int WaitForSingleObject(PythonSubprocessHandle handle, int dwMilliseconds) {
return WaitForSingleObjectPI(handle, dwMilliseconds);
}
#endregion
#region struct's and enum's
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal struct STARTUPINFO {
public Int32 cb;
public string lpReserved;
public string lpDesktop;
public string lpTitle;
public Int32 dwX;
public Int32 dwY;
public Int32 dwXSize;
public Int32 dwYSize;
public Int32 dwXCountChars;
public Int32 dwYCountChars;
public Int32 dwFillAttribute;
public Int32 dwFlags;
public Int16 wShowWindow;
public Int16 cbReserved2;
public IntPtr lpReserved2;
public IntPtr hStdInput;
public IntPtr hStdOutput;
public IntPtr hStdError;
}
[StructLayout(LayoutKind.Sequential)]
internal struct PROCESS_INFORMATION {
public IntPtr hProcess;
public IntPtr hThread;
public int dwProcessId;
public int dwThreadId;
}
[StructLayout(LayoutKind.Sequential)]
internal /*class*/ struct SECURITY_ATTRIBUTES {
public int nLength;
public IntPtr lpSecurityDescriptor;
public int bInheritHandle;
}
[Flags]
internal enum DuplicateOptions : uint {
DUPLICATE_CLOSE_SOURCE = (0x00000001),// Closes the source handle. This occurs regardless of any error status returned.
DUPLICATE_SAME_ACCESS = (0x00000002), //Ignores the dwDesiredAccess parameter. The duplicate handle has the same access as the source handle.
}
#endregion
#region Privates / PInvokes
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("kernel32.dll", EntryPoint = "CreateProcess", SetLastError = true)]
private static extern bool CreateProcessPI(string lpApplicationName,
string lpCommandLine, ref SECURITY_ATTRIBUTES lpProcessAttributes,
ref SECURITY_ATTRIBUTES lpThreadAttributes, [MarshalAs(UnmanagedType.Bool)]bool bInheritHandles,
uint dwCreationFlags, string lpEnvironment, string lpCurrentDirectory,
[In] ref STARTUPINFO lpStartupInfo,
out PROCESS_INFORMATION lpProcessInformation);
[DllImport("kernel32.dll", EntryPoint = "CreatePipe")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool CreatePipePI(out IntPtr hReadPipe, out IntPtr hWritePipe,
ref SECURITY_ATTRIBUTES lpPipeAttributes, uint nSize);
[DllImport("kernel32.dll", SetLastError = true, EntryPoint = "DuplicateHandle")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool DuplicateHandlePI(IntPtr hSourceProcessHandle,
IntPtr hSourceHandle, IntPtr hTargetProcessHandle, out IntPtr lpTargetHandle,
uint dwDesiredAccess, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle, uint dwOptions);
[DllImport("kernel32.dll", EntryPoint = "GetCurrentProcess")]
private static extern IntPtr GetCurrentProcessPI();
[DllImport("kernel32.dll", SetLastError = true, EntryPoint = "GetExitCodeProcess")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool GetExitCodeProcessPI(IntPtr hProcess, out /*uint*/ int lpExitCode);
[DllImport("kernel32.dll", SetLastError = true, EntryPoint = "GetStdHandle")]
private static extern IntPtr GetStdHandlePI(int nStdHandle);
[DllImport("kernel32.dll", EntryPoint = "GetVersion")]
[SuppressMessage("Microsoft.Usage", "CA2205:UseManagedEquivalentsOfWin32Api")]
private static extern int GetVersionPI();
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("kernel32.dll", SetLastError = true, EntryPoint = "CloseHandle")]
internal static extern bool CloseHandle(IntPtr hObject);
[DllImport("kernel32.dll", SetLastError = true, EntryPoint = "TerminateProcess")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool TerminateProcessPI(IntPtr hProcess, uint uExitCode);
[DllImport("kernel32", CharSet = CharSet.Ansi,
SetLastError = true, ExactSpelling = true, EntryPoint = "WaitForSingleObject")]
internal static extern int WaitForSingleObjectPI(IntPtr hHandle, int dwMilliseconds);
#endregion
#region Constants
public const int CREATE_NEW_CONSOLE = 16;
public const int CREATE_NEW_PROCESS_GROUP = 512;
public const int DUPLICATE_SAME_ACCESS = 2;
public const int INFINITE = -1;
public const int STARTF_USESHOWWINDOW = 1;
public const int STARTF_USESTDHANDLES = 256;
public const int STD_ERROR_HANDLE = -12;
public const int STD_INPUT_HANDLE = -10;
public const int STD_OUTPUT_HANDLE = -11;
public const int SW_HIDE = 0;
public const int WAIT_OBJECT_0 = 0;
public const int PIPE = -1;
public const int STDOUT = -2;
#endregion
}
[PythonType("_subprocess_handle")]
public class PythonSubprocessHandle {
private readonly IntPtr _internalHandle;
internal bool _closed;
internal bool _duplicated, _isProcess;
internal int _exitCode;
private static List<PythonSubprocessHandle> _active = new List<PythonSubprocessHandle>();
internal PythonSubprocessHandle(IntPtr handle) {
_internalHandle = handle;
}
internal PythonSubprocessHandle(IntPtr handle, bool isProcess) {
_internalHandle = handle;
_isProcess = isProcess;
}
~PythonSubprocessHandle() {
if (_isProcess) {
lock (_active) {
// we need to deal w/ order of finalization and the fact that Popen will resurrect
// it's self and want to be able to poll us for exit. Therefore we can't close until
// the process has really exited (and we've captured that exit code). So we keep
// resurrecting ourselves until it finally happens.
int insertion = -1;
for (int i = 0; i < _active.Count; i++) {
if (_active[i] == null) {
insertion = i;
} else if (_active[i].PollForExit()) {
_active[i] = null;
insertion = i;
if (_active[i] == this) {
// we've exited, and we're removed from the list
Close();
return;
}
} else if (_active[i] == this) {
// we haven't exited
return;
}
}
// we're not in the list.
if (!PollForExit()) {
// resurrect ourselves - this is to account for subprocess.py's resurrection of
// handles. We cannot close our handle until we have been successfully polled
// for the end of the process.
if (insertion != -1) {
_active[insertion] = this;
} else {
_active.Add(this);
}
return;
} else {
Close();
}
}
}
Close();
}
private bool PollForExit() {
if (PythonSubprocess.WaitForSingleObjectPI(_internalHandle, 0) == PythonSubprocess.WAIT_OBJECT_0) {
PythonSubprocess.GetExitCodeProcessPI(_internalHandle, out _exitCode);
return true;
}
return false;
}
public void Close() {
lock (this) {
if (!_closed) {
PythonSubprocess.CloseHandle(_internalHandle);
_closed = true;
GC.SuppressFinalize(this);
}
}
}
public object Detach(CodeContext context) {
lock (this) {
if (!_closed) {
_closed = true;
GC.SuppressFinalize(this);
return _internalHandle.ToPython();
}
}
return -1;
}
public static implicit operator int(PythonSubprocessHandle type) {
return type._internalHandle.ToInt32(); // ToPython()
}
public static implicit operator BigInteger(PythonSubprocessHandle type) {
return type._internalHandle.ToInt32(); // ToPython()
}
public static implicit operator IntPtr(PythonSubprocessHandle type) {
return type._internalHandle; // ToPython()
}
}
}
#endif