void ProcessExited(ProcessAsyncOperation oper) { lock (this) { Counters.ExternalHostProcesses--; // Remove all callbacks from existing objects foreach (object ob in remoteObjects) { RemotingService.UnregisterMethodCallback(ob, "Dispose"); } remoteObjects.Clear(); exitedEvent.Set(); // If the remote process crashes, a thread may be left hung in WaitForExit. This will awaken it. exitRequestEvent.Set(); if (oper != process) { return; } // The process suddently died runningEvent.Reset(); processHost = null; process = null; references = 0; } }
public ProcessMonitor(OperationConsole console, ProcessAsyncOperation operation, EventHandler exited) { this.exited = exited; this.operation = operation; this.console = console; operation.Task.ContinueWith(t => OnOperationCompleted(), console.CancellationToken); cancelRegistration = console.CancellationToken.Register(operation.Cancel); }
Task StartRemoteProcess() { return(Task.Run(() => { var cmd = Runtime.ProcessService.CreateCommand(exePath); cmd.Arguments = ((IPEndPoint)listener.LocalEndpoint).Port + " " + DebugMode; process = executionHandler.Execute(cmd, console); process.Task.ContinueWith(t => ProcessExited()); })); }
Task StartRemoteProcess() { return(Task.Run(() => { var cmd = Runtime.ProcessService.CreateCommand(exePath); cmd.Arguments = ((IPEndPoint)listener.LocalEndpoint).Port + " " + DebugMode; // Explicitly propagate the PATH var to the process. It ensures that tools required // to run XS are also in the PATH for remote processes. cmd.EnvironmentVariables ["PATH"] = Environment.GetEnvironmentVariable("PATH"); process = executionHandler.Execute(cmd, console); process.Task.ContinueWith(t => ProcessExited()); })); }
Task StartRemoteProcess() { return(Task.Run(() => { var cmd = Runtime.ProcessService.CreateCommand(exePath); cmd.Arguments = ((IPEndPoint)listener.LocalEndpoint).Port + " " + DebugMode; if (!string.IsNullOrEmpty(workingDirectory)) { cmd.WorkingDirectory = workingDirectory; } // Explicitly propagate the PATH var to the process. It ensures that tools required // to run XS are also in the PATH for remote processes. cmd.EnvironmentVariables ["PATH"] = Environment.GetEnvironmentVariable("PATH"); cmd.ProcessExecutionArchitecture = ProcessExecutionArchitecture; process = executionHandler.Execute(cmd, console); process.Task.ContinueWith(t => ProcessExited(), mainCancelSource.Token); })); }
public new void Start () { CheckDisposed (); base.Start (); var cs = new CancellationTokenSource (); operation = new ProcessAsyncOperation (Task, cs); cs.Token.Register (Cancel); captureOutputTask = Task.Run (CaptureOutput); if (ErrorStreamChanged != null) { captureErrorTask = Task.Run (CaptureError); } else { endEventErr.Set (); } operation.ProcessId = Id; }
void WaitTimeout(object sender, System.Timers.ElapsedEventArgs args) { try { ProcessAsyncOperation oldProcess; lock (this) { if (references > 0) { stopping = false; return; } uint waited = (uint)(DateTime.Now - lastReleaseTime).TotalMilliseconds; if (waited < stopDelay) { timer.Interval = stopDelay - waited; timer.Enabled = true; return; } runningEvent.Reset(); exitedEvent.Reset(); exitRequestEvent.Set(); oldProcess = process; processHost = null; process = null; stopping = false; } if (!exitedEvent.WaitOne(shutdownTimeout, false)) { try { oldProcess.Cancel(); } catch { } } } catch (Exception ex) { LoggingService.LogError(ex.ToString()); } }
public new void Start() { CheckDisposed(); base.Start(); var cs = new CancellationTokenSource(); operation = new ProcessAsyncOperation(Task, cs); cs.Token.Register(Cancel); captureOutputTask = Task.Run(CaptureOutput); if (ErrorStreamChanged != null) { captureErrorTask = Task.Run(CaptureError); } else { endEventErr.Set(); } operation.ProcessId = Id; }
public new void Start () { CheckDisposed (); base.Start (); captureOutputThread = new Thread (new ThreadStart(CaptureOutput)); captureOutputThread.Name = "Process output reader"; captureOutputThread.IsBackground = true; captureOutputThread.Start (); var cs = new CancellationTokenSource (); operation = new ProcessAsyncOperation (Task, cs); cs.Token.Register (Cancel); if (ErrorStreamChanged != null) { captureErrorThread = new Thread (new ThreadStart(CaptureError)); captureErrorThread.Name = "Process error reader"; captureErrorThread.IsBackground = true; captureErrorThread.Start (); } else { endEventErr.Set (); } operation.ProcessId = Id; }
public void Start (IList<string> userAssemblyPaths = null) { lock (this) { if (starting) return; starting = true; exitRequestEvent.Reset (); RemotingService.RegisterRemotingChannel (); BinaryFormatter bf = new BinaryFormatter (); ObjRef oref = RemotingServices.Marshal (this); MemoryStream ms = new MemoryStream (); bf.Serialize (ms, oref); string sref = Convert.ToBase64String (ms.ToArray ()); string tmpFile = null; if (executionHandlerFactory == null) executionHandlerFactory = Runtime.SystemAssemblyService.CurrentRuntime.GetExecutionHandler (); try { string location = Path.GetDirectoryName (System.Reflection.Assembly.GetExecutingAssembly ().Location); location = Path.Combine (location, "mdhost.exe"); tmpFile = Path.GetTempFileName (); StreamWriter sw = new StreamWriter (tmpFile); sw.WriteLine (sref); sw.WriteLine (Process.GetCurrentProcess ().Id); sw.WriteLine (Runtime.SystemAssemblyService.CurrentRuntime.RuntimeId); // Explicitly load Mono.Addins since the target runtime may not have it installed sw.WriteLine (2); sw.WriteLine (typeof(AddinManager).Assembly.Location); sw.WriteLine (typeof(Mono.Addins.Setup.SetupService).Assembly.Location); sw.Close (); string arguments = string.Format ("{0} \"{1}\"", id, tmpFile); DotNetExecutionCommand cmd = new DotNetExecutionCommand (location, arguments, AppDomain.CurrentDomain.BaseDirectory); if (userAssemblyPaths != null) cmd.UserAssemblyPaths = userAssemblyPaths; cmd.DebugMode = isDebugMode; ProcessHostConsole cons = new ProcessHostConsole (); var p = process = executionHandlerFactory.Execute (cmd, cons); Counters.ExternalHostProcesses++; process.Task.ContinueWith ((t) => ProcessExited (p)); } catch (Exception ex) { if (tmpFile != null) { try { File.Delete (tmpFile); } catch { } } LoggingService.LogError (ex.ToString ()); throw; } } }
void WaitTimeout (object sender, System.Timers.ElapsedEventArgs args) { try { ProcessAsyncOperation oldProcess; lock (this) { if (references > 0) { stopping = false; return; } uint waited = (uint) (DateTime.Now - lastReleaseTime).TotalMilliseconds; if (waited < stopDelay) { timer.Interval = stopDelay - waited; timer.Enabled = true; return; } runningEvent.Reset (); exitedEvent.Reset (); exitRequestEvent.Set (); oldProcess = process; processHost = null; process = null; stopping = false; } if (!exitedEvent.WaitOne (shutdownTimeout, false)) { try { oldProcess.Cancel (); } catch { } } } catch (Exception ex) { LoggingService.LogError (ex.ToString ()); } }
void ProcessExited (ProcessAsyncOperation oper) { lock (this) { Counters.ExternalHostProcesses--; // Remove all callbacks from existing objects foreach (object ob in remoteObjects) RemotingService.UnregisterMethodCallback (ob, "Dispose"); remoteObjects.Clear (); exitedEvent.Set (); // If the remote process crashes, a thread may be left hung in WaitForExit. This will awaken it. exitRequestEvent.Set (); if (oper != process) return; // The process suddently died runningEvent.Reset (); processHost = null; process = null; references = 0; } }
public void Start(IList <string> userAssemblyPaths = null, OperationConsole console = null) { lock (this) { if (starting) { return; } starting = true; exitRequestEvent.Reset(); RemotingService.RegisterRemotingChannel(); BinaryFormatter bf = new BinaryFormatter(); ObjRef oref = RemotingServices.Marshal(this); MemoryStream ms = new MemoryStream(); bf.Serialize(ms, oref); string sref = Convert.ToBase64String(ms.ToArray()); string tmpFile = null; if (executionHandlerFactory == null) { executionHandlerFactory = Runtime.SystemAssemblyService.CurrentRuntime.GetExecutionHandler(); } try { string location = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); location = Path.Combine(location, "mdhost.exe"); tmpFile = Path.GetTempFileName(); StreamWriter sw = new StreamWriter(tmpFile); sw.WriteLine(sref); sw.WriteLine(Process.GetCurrentProcess().Id); sw.WriteLine(Runtime.SystemAssemblyService.CurrentRuntime.RuntimeId); // Explicitly load Mono.Addins since the target runtime may not have it installed sw.WriteLine(2); sw.WriteLine(typeof(AddinManager).Assembly.Location); sw.WriteLine(typeof(Mono.Addins.Setup.SetupService).Assembly.Location); sw.Close(); string arguments = string.Format("{0} \"{1}\"", id, tmpFile); DotNetExecutionCommand cmd = new DotNetExecutionCommand(location, arguments, AppDomain.CurrentDomain.BaseDirectory); if (userAssemblyPaths != null) { cmd.UserAssemblyPaths = userAssemblyPaths; } cmd.DebugMode = isDebugMode; OperationConsole cons = console ?? new ProcessHostConsole(); var p = process = executionHandlerFactory.Execute(cmd, cons); Counters.ExternalHostProcesses.Inc(1); process.Task.ContinueWith((t) => ProcessExited(p)); } catch (Exception ex) { if (tmpFile != null) { try { File.Delete(tmpFile); } catch { } } LoggingService.LogError(ex.ToString()); throw; } } }
public ProcessAdapter (ProcessAsyncOperation oper, string name) { this.oper = oper; this.name = name; oper.Task.ContinueWith (t => { if (Exited != null) Exited (this, EventArgs.Empty); }, Runtime.MainTaskScheduler); }
public ProcessMonitor (OperationConsole console, ProcessAsyncOperation operation, EventHandler exited) { this.exited = exited; this.operation = operation; this.console = console; operation.Task.ContinueWith (t => OnOperationCompleted ()); cancelRegistration = console.CancellationToken.Register (operation.Cancel); }
protected override void OnRun (DebuggerStartInfo startInfo) { lock (gdbLock) { // Create a script to be run in a terminal string script = Path.GetTempFileName (); string ttyfile = Path.GetTempFileName (); string ttyfileDone = ttyfile + "_done"; string tty; try { File.WriteAllText (script, "tty > " + ttyfile + "\ntouch " + ttyfileDone + "\nsleep 10000d"); Mono.Unix.Native.Syscall.chmod (script, FilePermissions.ALLPERMS); console = Runtime.ProcessService.StartConsoleProcess (script, "", ".", ExternalConsoleFactory.Instance.CreateConsole (true), null); DateTime tim = DateTime.Now; while (!File.Exists (ttyfileDone)) { System.Threading.Thread.Sleep (100); if ((DateTime.Now - tim).TotalSeconds > 10) throw new InvalidOperationException ("Console could not be created."); } tty = File.ReadAllText (ttyfile).Trim (' ','\n'); } finally { try { if (File.Exists (script)) File.Delete (script); if (File.Exists (ttyfile)) File.Delete (ttyfile); if (File.Exists (ttyfileDone)) File.Delete (ttyfileDone); } catch { // Ignore } } StartGdb (); // Initialize the terminal RunCommand ("-inferior-tty-set", Escape (tty)); try { RunCommand ("-file-exec-and-symbols", Escape (startInfo.Command)); } catch { FireTargetEvent (TargetEventType.TargetExited, null); throw; } RunCommand ("-environment-cd", Escape (startInfo.WorkingDirectory)); // Set inferior arguments if (!string.IsNullOrEmpty (startInfo.Arguments)) RunCommand ("-exec-arguments", startInfo.Arguments); if (startInfo.EnvironmentVariables != null) { foreach (var v in startInfo.EnvironmentVariables) RunCommand ("-gdb-set", "environment", v.Key, v.Value); } currentProcessName = startInfo.Command + " " + startInfo.Arguments; CheckIsMonoProcess (); OnStarted (); RunCommand ("-exec-run"); } }
public override void Dispose () { if (console != null && !console.IsCompleted) { console.Cancel (); console = null; } if (thread != null) thread.Abort (); }