示例#1
0
文件: Process.cs 项目: zixing131/mono
        bool StartWithCreateProcess(ProcessStartInfo startInfo)
        {
            if (startInfo.StandardOutputEncoding != null && !startInfo.RedirectStandardOutput)
            {
                throw new InvalidOperationException(SR.GetString(SR.StandardOutputEncodingNotAllowed));
            }

            if (startInfo.StandardErrorEncoding != null && !startInfo.RedirectStandardError)
            {
                throw new InvalidOperationException(SR.GetString(SR.StandardErrorEncodingNotAllowed));
            }

            if (this.disposed)
            {
                throw new ObjectDisposedException(GetType().Name);
            }

            var procInfo = new ProcInfo();

            if (startInfo.HaveEnvVars)
            {
                List <string> envVariables = null;
                StringBuilder sb           = null;

                foreach (DictionaryEntry de in startInfo.EnvironmentVariables)
                {
                    if (de.Value == null)
                    {
                        continue;
                    }

                    if (envVariables == null)
                    {
                        envVariables = new List <string> ();
                    }

                    if (sb == null)
                    {
                        sb = new StringBuilder();
                    }
                    else
                    {
                        sb.Clear();
                    }

                    sb.Append((string)de.Key);
                    sb.Append('=');
                    sb.Append((string)de.Value);

                    envVariables.Add(sb.ToString());
                }

                procInfo.envVariables = envVariables?.ToArray();
            }

            MonoIOError error;
            IntPtr      stdin_read = IntPtr.Zero, stdin_write = IntPtr.Zero;
            IntPtr      stdout_read = IntPtr.Zero, stdout_write = IntPtr.Zero;
            IntPtr      stderr_read = IntPtr.Zero, stderr_write = IntPtr.Zero;

            try {
                if (startInfo.RedirectStandardInput)
                {
                    CreatePipe(out stdin_read, out stdin_write, true);
                }
                else
                {
                    stdin_read  = MonoIO.ConsoleInput;
                    stdin_write = IntPtr.Zero;
                }

                if (startInfo.RedirectStandardOutput)
                {
                    CreatePipe(out stdout_read, out stdout_write, false);
                }
                else
                {
                    stdout_read  = IntPtr.Zero;
                    stdout_write = MonoIO.ConsoleOutput;
                }

                if (startInfo.RedirectStandardError)
                {
                    CreatePipe(out stderr_read, out stderr_write, false);
                }
                else
                {
                    stderr_read  = IntPtr.Zero;
                    stderr_write = MonoIO.ConsoleError;
                }

                FillUserInfo(startInfo, ref procInfo);

                //
                // FIXME: For redirected pipes we need to send descriptors of
                // stdin_write, stdout_read, stderr_read to child process and
                // close them there (fork makes exact copy of parent's descriptors)
                //
                if (!CreateProcess_internal(startInfo, stdin_read, stdout_write, stderr_write, ref procInfo))
                {
                    throw new Win32Exception(-procInfo.pid, "ApplicationName='" + startInfo.FileName + "', CommandLine='" + startInfo.Arguments +
                                             "', CurrentDirectory='" + startInfo.WorkingDirectory + "', Native error= " + Win32Exception.GetErrorMessage(-procInfo.pid));
                }
            } catch {
                if (startInfo.RedirectStandardInput)
                {
                    if (stdin_read != IntPtr.Zero)
                    {
                        MonoIO.Close(stdin_read, out error);
                    }
                    if (stdin_write != IntPtr.Zero)
                    {
                        MonoIO.Close(stdin_write, out error);
                    }
                }

                if (startInfo.RedirectStandardOutput)
                {
                    if (stdout_read != IntPtr.Zero)
                    {
                        MonoIO.Close(stdout_read, out error);
                    }
                    if (stdout_write != IntPtr.Zero)
                    {
                        MonoIO.Close(stdout_write, out error);
                    }
                }

                if (startInfo.RedirectStandardError)
                {
                    if (stderr_read != IntPtr.Zero)
                    {
                        MonoIO.Close(stderr_read, out error);
                    }
                    if (stderr_write != IntPtr.Zero)
                    {
                        MonoIO.Close(stderr_write, out error);
                    }
                }

                throw;
            } finally {
                if (procInfo.Password != IntPtr.Zero)
                {
                    Marshal.ZeroFreeBSTR(procInfo.Password);
                    procInfo.Password = IntPtr.Zero;
                }
            }

            SetProcessHandle(new SafeProcessHandle(procInfo.process_handle, true));
            SetProcessId(procInfo.pid);

#pragma warning disable 618

            if (startInfo.RedirectStandardInput)
            {
                MonoIO.Close(stdin_read, out error);

#if MOBILE
                var stdinEncoding = Encoding.Default;
#else
                var stdinEncoding = Console.InputEncoding;
#endif
                standardInput = new StreamWriter(new FileStream(stdin_write, FileAccess.Write, true, 8192), stdinEncoding)
                {
                    AutoFlush = true
                };
            }

            if (startInfo.RedirectStandardOutput)
            {
                MonoIO.Close(stdout_write, out error);

                Encoding stdoutEncoding = startInfo.StandardOutputEncoding ?? Console.Out.Encoding;

                standardOutput = new StreamReader(new FileStream(stdout_read, FileAccess.Read, true, 8192), stdoutEncoding, true);
            }

            if (startInfo.RedirectStandardError)
            {
                MonoIO.Close(stderr_write, out error);

                Encoding stderrEncoding = startInfo.StandardErrorEncoding ?? Console.Out.Encoding;

                standardError = new StreamReader(new FileStream(stderr_read, FileAccess.Read, true, 8192), stderrEncoding, true);
            }
#pragma warning restore

            return(true);
        }
示例#2
0
        internal static void CheckValidCounterLayout(CounterCreationDataCollection counterData)
        {
            // Ensure that there are no duplicate counter names being created
            Hashtable h = new Hashtable();

            for (int i = 0; i < counterData.Count; i++)
            {
                if (counterData[i].CounterName == null || counterData[i].CounterName.Length == 0)
                {
                    throw new ArgumentException(SR.InvalidCounterName);
                }

                int currentSampleType = (int)counterData[i].CounterType;
                if ((currentSampleType == Interop.Kernel32.PerformanceCounterOptions.PERF_AVERAGE_BULK) ||
                    (currentSampleType == Interop.Kernel32.PerformanceCounterOptions.PERF_100NSEC_MULTI_TIMER) ||
                    (currentSampleType == Interop.Kernel32.PerformanceCounterOptions.PERF_100NSEC_MULTI_TIMER_INV) ||
                    (currentSampleType == Interop.Kernel32.PerformanceCounterOptions.PERF_COUNTER_MULTI_TIMER) ||
                    (currentSampleType == Interop.Kernel32.PerformanceCounterOptions.PERF_COUNTER_MULTI_TIMER_INV) ||
                    (currentSampleType == Interop.Kernel32.PerformanceCounterOptions.PERF_RAW_FRACTION) ||
                    (currentSampleType == Interop.Kernel32.PerformanceCounterOptions.PERF_SAMPLE_FRACTION) ||
                    (currentSampleType == Interop.Kernel32.PerformanceCounterOptions.PERF_AVERAGE_TIMER))
                {
                    if (counterData.Count <= (i + 1))
                    {
                        throw new InvalidOperationException(SR.CounterLayout);
                    }
                    else
                    {
                        currentSampleType = (int)counterData[i + 1].CounterType;


                        if (!PerformanceCounterLib.IsBaseCounter(currentSampleType))
                        {
                            throw new InvalidOperationException(SR.CounterLayout);
                        }
                    }
                }
                else if (PerformanceCounterLib.IsBaseCounter(currentSampleType))
                {
                    if (i == 0)
                    {
                        throw new InvalidOperationException(SR.CounterLayout);
                    }
                    else
                    {
                        currentSampleType = (int)counterData[i - 1].CounterType;

                        if (
                            (currentSampleType != Interop.Kernel32.PerformanceCounterOptions.PERF_AVERAGE_BULK) &&
                            (currentSampleType != Interop.Kernel32.PerformanceCounterOptions.PERF_100NSEC_MULTI_TIMER) &&
                            (currentSampleType != Interop.Kernel32.PerformanceCounterOptions.PERF_100NSEC_MULTI_TIMER_INV) &&
                            (currentSampleType != Interop.Kernel32.PerformanceCounterOptions.PERF_COUNTER_MULTI_TIMER) &&
                            (currentSampleType != Interop.Kernel32.PerformanceCounterOptions.PERF_COUNTER_MULTI_TIMER_INV) &&
                            (currentSampleType != Interop.Kernel32.PerformanceCounterOptions.PERF_RAW_FRACTION) &&
                            (currentSampleType != Interop.Kernel32.PerformanceCounterOptions.PERF_SAMPLE_FRACTION) &&
                            (currentSampleType != Interop.Kernel32.PerformanceCounterOptions.PERF_AVERAGE_TIMER))
                        {
                            throw new InvalidOperationException(SR.CounterLayout);
                        }
                    }
                }

                if (h.ContainsKey(counterData[i].CounterName))
                {
                    throw new ArgumentException(SR.Format(SR.DuplicateCounterName, counterData[i].CounterName));
                }
                else
                {
                    h.Add(counterData[i].CounterName, string.Empty);

                    // Ensure that all counter help strings aren't null or empty
                    if (counterData[i].CounterHelp == null || counterData[i].CounterHelp.Length == 0)
                    {
                        counterData[i].CounterHelp = counterData[i].CounterName;
                    }
                }
            }
        }