예제 #1
0
        private void RunInVm()
        {
            // (1) Serialize sandboxed prosess info.
            SerializeSandboxedProcessInfoToFile();

            // (2) Create and serialize run request.
            var runRequest = new RunRequest
            {
                AbsolutePath     = m_tool.ExecutablePath,
                Arguments        = m_tool.CreateArguments(GetSandboxedProcessInfoFile(), GetSandboxedProcessResultsFile()),
                WorkingDirectory = GetOutputDirectory()
            };

            VmSerializer.SerializeToFile(RunRequestPath, runRequest);

            // (2) Create a process to execute VmCommandProxy.
            string arguments = $"{VmCommands.Run} /{VmCommands.Params.InputJsonFile}:\"{RunRequestPath}\" /{VmCommands.Params.OutputJsonFile}:\"{RunOutputPath}\"";
            var    process   = CreateVmCommandProxyProcess(arguments);

            LogExternalExecution($"call (wd: {process.StartInfo.WorkingDirectory}) {m_vmInitializer.VmCommandProxy} {arguments}");

            m_processExecutor = new AsyncProcessExecutor(
                process,
                TimeSpan.FromMilliseconds(-1), // Timeout should only be applied to the process that the external tool executes.
                line => AppendLineIfNotNull(m_output, line),
                line => AppendLineIfNotNull(m_error, line),
                SandboxedProcessInfo.Provenance,
                message => LogExternalExecution(message));

            m_processExecutor.Start();
        }
예제 #2
0
        private static int Run(string inputFile, string outputFile)
        {
            Console.WriteLine($"Read request from '{inputFile}'");

            RunRequest request = VmSerializer.DeserializeFromFile <RunRequest>(inputFile);

            Console.WriteLine($"Run request '{request.AbsolutePath} {request.Arguments}'");

            var stdOut = new StringBuilder();
            var stdErr = new StringBuilder();

            using (var process = new Process
            {
                StartInfo = new ProcessStartInfo
                {
                    FileName = request.AbsolutePath,
                    Arguments = request.Arguments,
                    WorkingDirectory = request.WorkingDirectory,
                    RedirectStandardError = true,
                    RedirectStandardOutput = true,
                    UseShellExecute = false,
                    CreateNoWindow = true
                },

                EnableRaisingEvents = true
            })
            {
                process.OutputDataReceived += (s, e) => stdOut.AppendLine(e.Data);
                process.ErrorDataReceived  += (s, e) => stdErr.AppendLine(e.Data);
                process.Start();
                process.BeginOutputReadLine();
                process.BeginErrorReadLine();
                process.WaitForExit();

                Console.WriteLine($"Finish request '{request.AbsolutePath} {request.Arguments}'");

                var stdOutPath = Path.Combine(request.WorkingDirectory, "vm.std.out");
                var stdErrPath = Path.Combine(request.WorkingDirectory, "vm.std.err");
                File.WriteAllText(stdOutPath, stdOut.ToString());
                File.WriteAllText(stdErrPath, stdErr.ToString());

                var result = new RunResult
                {
                    StdOut           = stdOut.ToString(),
                    StdErr           = stdErr.ToString(),
                    ProcessStateInfo = new ProcessStateInfo
                    {
                        StdOutPath        = stdOutPath,
                        StdErrPath        = stdErrPath,
                        ExitCode          = process.ExitCode,
                        ProcessState      = process.HasExited ? ProcessState.Exited : ProcessState.Unknown,
                        TerminationReason = ProcessTerminationReason.None
                    }
                };

                Console.WriteLine($"Write result to '{outputFile}'");

                VmSerializer.SerializeToFile(outputFile, result);

                return(0);
            }
        }