AppExecutor.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections;
  29. using System.Diagnostics;
  30. using System.IO;
  31. using System.Reflection;
  32. using System.Text;
  33. using System.Threading;
  34. using log4net;
  35. namespace OpenSim.GridLaunch
  36. {
  37. internal partial class AppExecutor : IDisposable
  38. {
  39. // How long to wait for process to shut down by itself
  40. private static readonly int shutdownWaitSeconds = 10;
  41. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  42. //private StreamWriter Input { get { return process.StandardInput; } }
  43. //private StreamReader Output { get { return process.StandardOutput; } }
  44. //private StreamReader Error { get { return process.StandardError; } }
  45. private StreamWriter Input { get; set; }
  46. private StreamReader Output { get; set; }
  47. private StreamReader Error { get; set; }
  48. private object processLock = new object();
  49. private bool isRunning = false;
  50. public bool IsRunning { get { return isRunning; } }
  51. private string file;
  52. public string File { get { return file; } }
  53. Process process;
  54. public AppExecutor(string File)
  55. {
  56. file = File;
  57. }
  58. #region Dispose of unmanaged resources
  59. ~AppExecutor()
  60. {
  61. Dispose();
  62. }
  63. private bool isDisposed = false;
  64. public void Dispose()
  65. {
  66. if (!isDisposed)
  67. {
  68. isDisposed = true;
  69. Stop();
  70. }
  71. }
  72. #endregion
  73. #region Start / Stop process
  74. public void Start()
  75. {
  76. if (isDisposed)
  77. throw new ApplicationException("Attempt to start process in Disposed instance of AppExecutor.");
  78. // Stop before starting
  79. Stop();
  80. lock (processLock)
  81. {
  82. isRunning = true;
  83. m_log.InfoFormat("Starting \"{0}\".", file);
  84. // Start the process
  85. process = new Process();
  86. process.StartInfo.FileName = file;
  87. process.StartInfo.Arguments = "";
  88. process.StartInfo.CreateNoWindow = true;
  89. process.StartInfo.UseShellExecute = false;
  90. process.StartInfo.ErrorDialog = false;
  91. process.EnableRaisingEvents = true;
  92. // Redirect all standard input/output/errors
  93. process.StartInfo.RedirectStandardInput = true;
  94. process.StartInfo.RedirectStandardOutput = true;
  95. process.StartInfo.RedirectStandardError = true;
  96. // Start process
  97. process.Start();
  98. Input = process.StandardInput;
  99. Output = process.StandardOutput;
  100. Error = process.StandardError;
  101. // Start data copying
  102. timer_Start();
  103. // We will flush manually
  104. //Input.AutoFlush = false;
  105. }
  106. }
  107. public void Stop()
  108. {
  109. // Shut down process
  110. // We will ignore some exceptions here, against good programming practice... :)
  111. lock (processLock)
  112. {
  113. // Running?
  114. if (!isRunning)
  115. return;
  116. isRunning = false;
  117. timer_Stop();
  118. m_log.InfoFormat("Stopping \"{0}\".", file);
  119. // Send exit command to console
  120. try
  121. {
  122. if (Input != null)
  123. {
  124. _writeLine("");
  125. _writeLine("exit");
  126. _writeLine("quit");
  127. // Wait for process to exit
  128. process.WaitForExit(1000 * shutdownWaitSeconds);
  129. }
  130. }
  131. catch (Exception ex)
  132. {
  133. m_log.ErrorFormat("Exeption asking \"{0}\" to shut down: {1}", file, ex.ToString());
  134. }
  135. try
  136. {
  137. // Forcefully kill it
  138. if (process.HasExited != true)
  139. process.Kill();
  140. }
  141. catch (Exception ex)
  142. {
  143. m_log.ErrorFormat("Exeption killing \"{0}\": {1}", file, ex.ToString());
  144. }
  145. try
  146. {
  147. // Free resources
  148. process.Close();
  149. }
  150. catch (Exception ex)
  151. {
  152. m_log.ErrorFormat("Exeption freeing resources for \"{0}\": {1}", file, ex.ToString());
  153. }
  154. // Dispose of stream and process object
  155. //SafeDisposeOf(Input);
  156. //SafeDisposeOf(Output);
  157. //SafeDisposeOf(Error);
  158. Program.SafeDisposeOf(process);
  159. }
  160. // Done stopping process
  161. }
  162. #endregion
  163. #region Write to stdInput
  164. public void Write(string Text)
  165. {
  166. // Lock so process won't shut down while we write, and that we won't write while proc is shutting down
  167. lock (processLock)
  168. {
  169. _write(Text);
  170. }
  171. }
  172. public void _write(string Text)
  173. {
  174. if (Input != null)
  175. {
  176. try
  177. {
  178. Input.Write(Text);
  179. Input.Flush();
  180. }
  181. catch (Exception ex)
  182. {
  183. m_log.ErrorFormat("Exeption sending text \"{0}\" to \"{1}\": {2}", file, Text, ex.ToString());
  184. }
  185. }
  186. }
  187. public void WriteLine(string Text)
  188. {
  189. // Lock so process won't shut down while we write, and that we won't write while proc is shutting down
  190. lock (processLock)
  191. {
  192. _writeLine(Text);
  193. }
  194. }
  195. public void _writeLine(string Text)
  196. {
  197. if (Input != null)
  198. {
  199. try
  200. {
  201. m_log.DebugFormat("\"{0}\": Sending: \"{1}\"", file, Text);
  202. Input.WriteLine(Text);
  203. Input.Flush();
  204. }
  205. catch (Exception ex)
  206. {
  207. m_log.ErrorFormat("Exeption sending text \"{0}\" to \"{1}\": {2}", file, Text, ex.ToString());
  208. }
  209. }
  210. }
  211. #endregion
  212. }
  213. }