private static void KillProcessAndChildProcesses(
     Process processToKill,
     NativeCommandProcessor.ProcessWithParentId[] currentlyRunningProcs)
 {
     try
     {
         NativeCommandProcessor.KillChildProcesses(processToKill.Id, currentlyRunningProcs);
         processToKill.Kill();
     }
     catch (Win32Exception ex1)
     {
         try
         {
             Process.GetProcessById(processToKill.Id).Kill();
         }
         catch (Exception ex2)
         {
             CommandProcessorBase.CheckForSevereException(ex2);
         }
     }
     catch (Exception ex)
     {
         CommandProcessorBase.CheckForSevereException(ex);
     }
 }
 private static void KillProcess(Process processToKill)
 {
     if (NativeCommandProcessor.IsServerSide)
     {
         NativeCommandProcessor.ProcessWithParentId[] currentlyRunningProcs = NativeCommandProcessor.ProcessWithParentId.Construct(Process.GetProcesses());
         NativeCommandProcessor.KillProcessAndChildProcesses(processToKill, currentlyRunningProcs);
     }
     else
     {
         try
         {
             processToKill.Kill();
         }
         catch (Win32Exception ex1)
         {
             try
             {
                 Process.GetProcessById(processToKill.Id).Kill();
             }
             catch (Exception ex2)
             {
                 CommandProcessorBase.CheckForSevereException(ex2);
             }
         }
         catch (Exception ex)
         {
             CommandProcessorBase.CheckForSevereException(ex);
         }
     }
 }
 private static void KillChildProcesses(
     int parentId,
     NativeCommandProcessor.ProcessWithParentId[] currentlyRunningProcs)
 {
     foreach (NativeCommandProcessor.ProcessWithParentId currentlyRunningProc in currentlyRunningProcs)
     {
         if (currentlyRunningProc.ParentId > 0 && currentlyRunningProc.ParentId == parentId)
         {
             NativeCommandProcessor.KillProcessAndChildProcesses(currentlyRunningProc.OriginalProcessInstance, currentlyRunningProcs);
         }
     }
 }
        private static string FindExecutable(string filename)
        {
            StringBuilder pathFound = new StringBuilder(1024);
            IntPtr        num       = (IntPtr)0;

            try
            {
                num = NativeCommandProcessor.FindExecutableW(filename, string.Empty, pathFound);
            }
            catch (IndexOutOfRangeException ex)
            {
                WindowsErrorReporting.FailFast((Exception)ex);
            }
            return((long)num >= 32L ? pathFound.ToString() : (string)null);
        }
        private static bool IsWindowsApplication(string fileName)
        {
            NativeCommandProcessor.SHFILEINFO psfi = new NativeCommandProcessor.SHFILEINFO();
            switch ((int)NativeCommandProcessor.SHGetFileInfo(fileName, 0U, ref psfi, (uint)Marshal.SizeOf((object)psfi), 8192U))
            {
            case 0:
                return(false);

            case 17744:
                return(false);

            case 23117:
                return(false);

            default:
                return(true);
            }
        }
 internal void StopProcessing()
 {
     lock (this.sync)
     {
         if (this.stopped)
         {
             return;
         }
         this.stopped = true;
     }
     if (this.nativeProcess == null || this._runStandAlone)
     {
         return;
     }
     this.inputWriter.Stop();
     if (this.outputReader != null)
     {
         this.outputReader.Stop();
     }
     NativeCommandProcessor.KillProcess(this.nativeProcess);
 }
Exemplo n.º 7
0
        internal CommandProcessorBase LookupCommandProcessor(CommandInfo commandInfo, CommandOrigin commandOrigin, bool? useLocalScope, SessionStateInternal sessionState)
        {
            CommandProcessorBase base2 = null;
            FunctionInfo info3;
            CommandNotFoundException exception6;
            HashSet<string> set = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
            while (((commandInfo.CommandType == CommandTypes.Alias) && !set.Contains(commandInfo.Name)) && ((commandOrigin == CommandOrigin.Internal) || (commandInfo.Visibility == SessionStateEntryVisibility.Public)))
            {
                set.Add(commandInfo.Name);
                AliasInfo info = (AliasInfo) commandInfo;
                commandInfo = info.ResolvedCommand;
                if (commandInfo == null)
                {
                    commandInfo = LookupCommandInfo(info.Definition, commandOrigin, this._context);
                }
                if (commandInfo == null)
                {
                    CommandNotFoundException exception = new CommandNotFoundException(info.Name, null, "AliasNotResolvedException", DiscoveryExceptions.AliasNotResolvedException, new object[] { info.UnresolvedCommandName });
                    throw exception;
                }
                PSSQMAPI.IncrementData(CommandTypes.Alias);
            }
            ShouldRun(this._context, this._context.EngineHostInterface, commandInfo, commandOrigin);
            CommandTypes commandType = commandInfo.CommandType;
            if (commandType <= CommandTypes.ExternalScript)
            {
                switch (commandType)
                {
                    case CommandTypes.Function:
                    case CommandTypes.Filter:
                        goto Label_01E3;

                    case CommandTypes.Cmdlet:
                        base2 = new CommandProcessor((CmdletInfo) commandInfo, this._context);
                        goto Label_0260;

                    case CommandTypes.ExternalScript:
                    {
                        ExternalScriptInfo scriptInfo = (ExternalScriptInfo) commandInfo;
                        scriptInfo.SignatureChecked = true;
                        try
                        {
                            if (!this._context.IsSingleShell)
                            {
                                bool? nullable = useLocalScope;
                                base2 = this.CreateScriptProcessorForMiniShell(scriptInfo, nullable.HasValue ? nullable.GetValueOrDefault() : true, sessionState);
                            }
                            else
                            {
                                bool? nullable2 = useLocalScope;
                                base2 = this.CreateScriptProcessorForSingleShell(scriptInfo, this._context, nullable2.HasValue ? nullable2.GetValueOrDefault() : true, sessionState);
                            }
                            goto Label_0260;
                        }
                        catch (ScriptRequiresSyntaxException exception2)
                        {
                            CommandNotFoundException exception3 = new CommandNotFoundException(exception2.Message, exception2);
                            throw exception3;
                        }
                        catch (PSArgumentException exception4)
                        {
                            CommandNotFoundException exception5 = new CommandNotFoundException(commandInfo.Name, exception4, "ScriptRequiresInvalidFormat", DiscoveryExceptions.ScriptRequiresInvalidFormat, new object[0]);
                            throw exception5;
                        }
                        goto Label_01E3;
                    }
                }
                goto Label_023F;
            }
            if (commandType != CommandTypes.Application)
            {
                if (commandType == CommandTypes.Script)
                {
                    bool? nullable4 = useLocalScope;
                    base2 = CreateCommandProcessorForScript((ScriptInfo) commandInfo, this._context, nullable4.HasValue ? nullable4.GetValueOrDefault() : true, sessionState);
                    goto Label_0260;
                }
                if (commandType == CommandTypes.Workflow)
                {
                    goto Label_01E3;
                }
                goto Label_023F;
            }
            base2 = new NativeCommandProcessor((ApplicationInfo) commandInfo, this._context);
            goto Label_0260;
        Label_01E3:
            info3 = (FunctionInfo) commandInfo;
            bool? nullable3 = useLocalScope;
            base2 = CreateCommandProcessorForScript(info3, this._context, nullable3.HasValue ? nullable3.GetValueOrDefault() : true, sessionState);
            goto Label_0260;
        Label_023F:
            exception6 = new CommandNotFoundException(commandInfo.Name, null, "CommandNotFoundException", DiscoveryExceptions.CommandNotFoundException, new object[0]);
            throw exception6;
        Label_0260:
            PSSQMAPI.IncrementData(commandInfo.CommandType);
            base2.Command.CommandOriginInternal = commandOrigin;
            base2.Command.MyInvocation.InvocationName = commandInfo.Name;
            return base2;
        }
 private void CalculateIORedirection(
     out bool redirectOutput,
     out bool redirectError,
     out bool redirectInput)
 {
     redirectInput  = true;
     redirectOutput = true;
     redirectError  = true;
     if (this.Command.MyInvocation.PipelinePosition == this.Command.MyInvocation.PipelineLength)
     {
         if (this.context.IsTopLevelPipe(this.commandRuntime.OutputPipe))
         {
             redirectOutput = false;
         }
         else
         {
             CommandProcessorBase downstreamCmdlet = this.commandRuntime.OutputPipe.DownstreamCmdlet;
             if (downstreamCmdlet != null && string.Equals(downstreamCmdlet.CommandInfo.Name, "Out-Default", StringComparison.OrdinalIgnoreCase))
             {
                 redirectOutput = false;
             }
         }
     }
     if (!this.CommandRuntime.MergeMyErrorOutputWithSuccess)
     {
         if (this.context.IsTopLevelPipe(this.commandRuntime.ErrorOutputPipe))
         {
             redirectError = false;
         }
         else
         {
             CommandProcessorBase downstreamCmdlet = this.commandRuntime.ErrorOutputPipe.DownstreamCmdlet;
             if (downstreamCmdlet != null && string.Equals(downstreamCmdlet.CommandInfo.Name, "Out-Default", StringComparison.OrdinalIgnoreCase))
             {
                 redirectError = false;
             }
         }
     }
     if (!redirectError && redirectOutput && this.isMiniShell)
     {
         redirectError = true;
     }
     if (this.inputWriter.Count == 0 && !this.Command.MyInvocation.ExpectingInput)
     {
         redirectInput = false;
     }
     if (NativeCommandProcessor.IsServerSide)
     {
         redirectInput  = true;
         redirectOutput = true;
         redirectError  = true;
     }
     else if (NativeCommandProcessor.IsConsoleApplication(this.Path))
     {
         ConsoleVisibility.AllocateHiddenConsole();
         if (ConsoleVisibility.AlwaysCaptureApplicationIO)
         {
             redirectOutput = true;
             redirectError  = true;
         }
     }
     if (redirectInput || redirectOutput)
     {
         return;
     }
     this._runStandAlone = true;
 }
 private static bool IsConsoleApplication(string fileName) => !NativeCommandProcessor.IsWindowsApplication(fileName);
Exemplo n.º 10
0
        internal override void Complete()
        {
            bool redirectOutput;
            bool redirectError;
            bool redirectInput;

            this.CalculateIORedirection(out redirectOutput, out redirectError, out redirectInput);
            bool             soloCommand      = this.Command.MyInvocation.PipelineLength == 1;
            ProcessStartInfo processStartInfo = this.GetProcessStartInfo(redirectOutput, redirectError, redirectInput, soloCommand);

            if (this.Command.Context.CurrentPipelineStopping)
            {
                throw new PipelineStoppedException();
            }
            Exception innerException = (Exception)null;

            try
            {
                if (!redirectOutput)
                {
                    this.Command.Context.EngineHostInterface.NotifyBeginApplication();
                }
                lock (this.sync)
                {
                    if (this.stopped)
                    {
                        throw new PipelineStoppedException();
                    }
                    try
                    {
                        this.nativeProcess           = new Process();
                        this.nativeProcess.StartInfo = processStartInfo;
                        this.nativeProcess.Start();
                    }
                    catch (Win32Exception ex1)
                    {
                        string executable = NativeCommandProcessor.FindExecutable(processStartInfo.FileName);
                        bool   flag       = true;
                        if (!string.IsNullOrEmpty(executable))
                        {
                            if (NativeCommandProcessor.IsConsoleApplication(executable))
                            {
                                ConsoleVisibility.AllocateHiddenConsole();
                            }
                            string arguments = processStartInfo.Arguments;
                            string fileName  = processStartInfo.FileName;
                            processStartInfo.Arguments = "\"" + processStartInfo.FileName + "\" " + processStartInfo.Arguments;
                            processStartInfo.FileName  = executable;
                            try
                            {
                                this.nativeProcess.Start();
                                flag = false;
                            }
                            catch (Win32Exception ex2)
                            {
                                processStartInfo.Arguments = arguments;
                                processStartInfo.FileName  = fileName;
                            }
                        }
                        if (flag)
                        {
                            if (soloCommand && !processStartInfo.UseShellExecute)
                            {
                                processStartInfo.UseShellExecute        = true;
                                processStartInfo.RedirectStandardInput  = false;
                                processStartInfo.RedirectStandardOutput = false;
                                processStartInfo.RedirectStandardError  = false;
                                this.nativeProcess.Start();
                            }
                            else
                            {
                                throw;
                            }
                        }
                    }
                }
                bool flag1;
                if (this.Command.MyInvocation.PipelinePosition < this.Command.MyInvocation.PipelineLength)
                {
                    flag1 = false;
                }
                else
                {
                    flag1 = true;
                    if (!processStartInfo.UseShellExecute)
                    {
                        flag1 = NativeCommandProcessor.IsWindowsApplication(this.nativeProcess.StartInfo.FileName);
                    }
                }
                try
                {
                    if (processStartInfo.RedirectStandardInput)
                    {
                        NativeCommandIOFormat inputFormat = NativeCommandIOFormat.Text;
                        if (this.isMiniShell)
                        {
                            inputFormat = ((MinishellParameterBinderController)this.NativeParameterBinderController).InputFormat;
                        }
                        lock (this.sync)
                        {
                            if (!this.stopped)
                            {
                                this.inputWriter.Start(this.nativeProcess, inputFormat);
                            }
                        }
                    }
                    if (!flag1)
                    {
                        if (!processStartInfo.RedirectStandardOutput)
                        {
                            if (!processStartInfo.RedirectStandardError)
                            {
                                goto label_54;
                            }
                        }
                        lock (this.sync)
                        {
                            if (!this.stopped)
                            {
                                this.outputReader = new ProcessOutputReader(this.nativeProcess, this.Path, redirectOutput, redirectError);
                                this.outputReader.Start();
                            }
                        }
                        if (this.outputReader != null)
                        {
                            this.ProcessOutputHelper();
                        }
                    }
                }
                catch (Exception ex)
                {
                    NativeCommandProcessor.KillProcess(this.nativeProcess);
                    throw;
                }
                finally
                {
                    if (!flag1)
                    {
                        this.nativeProcess.WaitForExit();
                        this.inputWriter.Done();
                        if (this.outputReader != null)
                        {
                            this.outputReader.Done();
                        }
                        this.Command.Context.SetVariable("global:LASTEXITCODE", (object)this.nativeProcess.ExitCode);
                        if (this.nativeProcess.ExitCode != 0)
                        {
                            this.commandRuntime.PipelineProcessor.ExecutionFailed = true;
                        }
                    }
                }
            }
            catch (Win32Exception ex)
            {
                innerException = (Exception)ex;
            }
            catch (PipelineStoppedException ex)
            {
                throw;
            }
            catch (Exception ex)
            {
                CommandProcessorBase.CheckForSevereException(ex);
                innerException = ex;
            }
            finally
            {
                if (!redirectOutput)
                {
                    this.Command.Context.EngineHostInterface.NotifyEndApplication();
                }
                this.CleanUp();
            }
label_54:
            if (innerException != null)
            {
                string message = ResourceManagerCache.FormatResourceString("Parser", "ProgramFailedToExecute", (object)this.NativeCommandName, (object)innerException.Message, (object)this.Command.MyInvocation.PositionMessage);
                if (message == null)
                {
                    message = StringUtil.Format("Program '{0}' failed to execute: {1}{2}", (object)this.NativeCommandName, (object)innerException.Message, (object)this.Command.MyInvocation.PositionMessage);
                }
                ApplicationFailedException applicationFailedException = new ApplicationFailedException(message, innerException);
                NativeCommandProcessor.tracer.TraceException((Exception)applicationFailedException);
                throw applicationFailedException;
            }
        }