|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.ComponentModel; |
| 5 | +using System.Runtime.InteropServices; |
| 6 | +using Microsoft.Win32.SafeHandles; |
| 7 | + |
| 8 | +namespace System.Diagnostics |
| 9 | +{ |
| 10 | + public partial class Process |
| 11 | + { |
| 12 | + /// <summary> |
| 13 | + /// Reads from both standard output and standard error pipes using Unix poll-based multiplexing |
| 14 | + /// with non-blocking reads. |
| 15 | + /// </summary> |
| 16 | + private static void ReadPipes( |
| 17 | + SafeFileHandle outputHandle, |
| 18 | + SafeFileHandle errorHandle, |
| 19 | + int timeoutMs, |
| 20 | + ref byte[] outputBuffer, |
| 21 | + ref int outputBytesRead, |
| 22 | + ref byte[] errorBuffer, |
| 23 | + ref int errorBytesRead) |
| 24 | + { |
| 25 | + int outputFd = outputHandle.DangerousGetHandle().ToInt32(); |
| 26 | + int errorFd = errorHandle.DangerousGetHandle().ToInt32(); |
| 27 | + |
| 28 | + if (Interop.Sys.Fcntl.DangerousSetIsNonBlocking((IntPtr)outputFd, 1) != 0 || Interop.Sys.Fcntl.DangerousSetIsNonBlocking((IntPtr)errorFd, 1) != 0) |
| 29 | + { |
| 30 | + throw new Win32Exception(); |
| 31 | + } |
| 32 | + |
| 33 | + Span<Interop.PollEvent> pollFds = stackalloc Interop.PollEvent[2]; |
| 34 | + |
| 35 | + long deadline = timeoutMs >= 0 |
| 36 | + ? Environment.TickCount64 + timeoutMs |
| 37 | + : long.MaxValue; |
| 38 | + |
| 39 | + bool outputDone = false, errorDone = false; |
| 40 | + while (!outputDone || !errorDone) |
| 41 | + { |
| 42 | + int numFds = 0; |
| 43 | + |
| 44 | + int outputIndex = -1; |
| 45 | + int errorIndex = -1; |
| 46 | + |
| 47 | + if (!outputDone) |
| 48 | + { |
| 49 | + outputIndex = numFds; |
| 50 | + pollFds[numFds].FileDescriptor = outputFd; |
| 51 | + pollFds[numFds].Events = Interop.PollEvents.POLLIN; |
| 52 | + pollFds[numFds].TriggeredEvents = Interop.PollEvents.POLLNONE; |
| 53 | + numFds++; |
| 54 | + } |
| 55 | + |
| 56 | + if (!errorDone) |
| 57 | + { |
| 58 | + errorIndex = numFds; |
| 59 | + pollFds[numFds].FileDescriptor = errorFd; |
| 60 | + pollFds[numFds].Events = Interop.PollEvents.POLLIN; |
| 61 | + pollFds[numFds].TriggeredEvents = Interop.PollEvents.POLLNONE; |
| 62 | + numFds++; |
| 63 | + } |
| 64 | + |
| 65 | + int pollTimeout; |
| 66 | + if (!TryGetRemainingTimeout(deadline, timeoutMs, out pollTimeout)) |
| 67 | + { |
| 68 | + throw new TimeoutException(); |
| 69 | + } |
| 70 | + |
| 71 | + unsafe |
| 72 | + { |
| 73 | + uint triggered; |
| 74 | + fixed (Interop.PollEvent* pPollFds = pollFds) |
| 75 | + { |
| 76 | + Interop.Error error = Interop.Sys.Poll(pPollFds, (uint)numFds, pollTimeout, &triggered); |
| 77 | + if (error != Interop.Error.SUCCESS) |
| 78 | + { |
| 79 | + if (error == Interop.Error.EINTR) |
| 80 | + { |
| 81 | + // We don't re-issue the poll immediately because we need to check |
| 82 | + // if we've already exceeded the overall timeout. |
| 83 | + continue; |
| 84 | + } |
| 85 | + |
| 86 | + throw new Win32Exception(Interop.Sys.ConvertErrorPalToPlatform(error)); |
| 87 | + } |
| 88 | + |
| 89 | + if (triggered == 0) |
| 90 | + { |
| 91 | + throw new TimeoutException(); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + for (int i = 0; i < numFds; i++) |
| 97 | + { |
| 98 | + if (pollFds[i].TriggeredEvents == Interop.PollEvents.POLLNONE) |
| 99 | + { |
| 100 | + continue; |
| 101 | + } |
| 102 | + |
| 103 | + bool isError = i == errorIndex; |
| 104 | + SafeFileHandle currentHandle = isError ? errorHandle : outputHandle; |
| 105 | + ref byte[] currentBuffer = ref (isError ? ref errorBuffer : ref outputBuffer); |
| 106 | + ref int currentBytesRead = ref (isError ? ref errorBytesRead : ref outputBytesRead); |
| 107 | + ref bool currentDone = ref (isError ? ref errorDone : ref outputDone); |
| 108 | + |
| 109 | + int bytesRead = ReadNonBlocking(currentHandle, currentBuffer, currentBytesRead); |
| 110 | + if (bytesRead > 0) |
| 111 | + { |
| 112 | + currentBytesRead += bytesRead; |
| 113 | + |
| 114 | + if (currentBytesRead == currentBuffer.Length) |
| 115 | + { |
| 116 | + RentLargerBuffer(ref currentBuffer, currentBytesRead); |
| 117 | + } |
| 118 | + } |
| 119 | + else if (bytesRead == 0) |
| 120 | + { |
| 121 | + // EOF: pipe write end was closed. |
| 122 | + currentDone = true; |
| 123 | + } |
| 124 | + // bytesRead < 0 means EAGAIN — nothing available yet, let poll retry. |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + /// <summary> |
| 130 | + /// Performs a non-blocking read from the given handle into the buffer starting at the specified offset. |
| 131 | + /// Returns the number of bytes read, 0 for EOF, or -1 for EAGAIN (nothing available yet). |
| 132 | + /// </summary> |
| 133 | + private static unsafe int ReadNonBlocking(SafeFileHandle handle, byte[] buffer, int offset) |
| 134 | + { |
| 135 | + fixed (byte* pBuffer = buffer) |
| 136 | + { |
| 137 | + int bytesRead = Interop.Sys.Read(handle, pBuffer + offset, buffer.Length - offset); |
| 138 | + if (bytesRead < 0) |
| 139 | + { |
| 140 | + Interop.ErrorInfo errorInfo = Interop.Sys.GetLastErrorInfo(); |
| 141 | + if (errorInfo.Error == Interop.Error.EAGAIN) |
| 142 | + { |
| 143 | + return -1; |
| 144 | + } |
| 145 | + |
| 146 | + throw new Win32Exception(errorInfo.RawErrno); |
| 147 | + } |
| 148 | + |
| 149 | + return bytesRead; |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments