Executor.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //
  2. // System.CodeDom.Compiler.Executor.cs
  3. //
  4. // Authors:
  5. // Andreas Nahr ([email protected])
  6. // Sebastien Pouliot <[email protected]>
  7. //
  8. // (C) 2003 Andreas Nahr
  9. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System.Diagnostics;
  31. using System.IO;
  32. using System.Security.Permissions;
  33. using System.Security.Principal;
  34. using System.Threading;
  35. namespace System.CodeDom.Compiler {
  36. [PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)]
  37. public sealed class Executor {
  38. class ProcessResultReader
  39. {
  40. StreamReader reader;
  41. string file;
  42. public ProcessResultReader (StreamReader reader, string file)
  43. {
  44. this.reader = reader;
  45. this.file = file;
  46. }
  47. public void Read ()
  48. {
  49. StreamWriter sw = new StreamWriter (file);
  50. try
  51. {
  52. string line;
  53. while ((line = reader.ReadLine()) != null)
  54. sw.WriteLine (line);
  55. }
  56. finally
  57. {
  58. sw.Close ();
  59. }
  60. }
  61. }
  62. private Executor ()
  63. {
  64. }
  65. public static void ExecWait (string cmd, TempFileCollection tempFiles)
  66. {
  67. string outputName = null;
  68. string errorName = null;
  69. ExecWaitWithCapture ((IntPtr)(-1), cmd, Environment.CurrentDirectory, tempFiles, ref outputName, ref errorName);
  70. }
  71. public static Int32 ExecWaitWithCapture (IntPtr userToken, string cmd, string currentDir, TempFileCollection tempFiles, ref string outputName, ref string errorName)
  72. {
  73. if (outputName == null)
  74. outputName = tempFiles.AddExtension ("out");
  75. if (errorName == null)
  76. errorName = tempFiles.AddExtension ("err");
  77. int exit_code = -1;
  78. WindowsImpersonationContext context = null;
  79. try {
  80. if (userToken != (IntPtr)(-1)) {
  81. context = WindowsIdentity.Impersonate (userToken);
  82. }
  83. Process proc = new Process ();
  84. proc.StartInfo.FileName = cmd;
  85. proc.StartInfo.CreateNoWindow = true;
  86. proc.StartInfo.UseShellExecute = false;
  87. proc.StartInfo.RedirectStandardOutput = true;
  88. proc.StartInfo.RedirectStandardError = true;
  89. proc.StartInfo.WorkingDirectory = currentDir;
  90. try {
  91. proc.Start();
  92. ProcessResultReader outReader = new ProcessResultReader (proc.StandardOutput, outputName);
  93. ProcessResultReader errReader = new ProcessResultReader (proc.StandardError, errorName);
  94. Thread t = new Thread (new ThreadStart (errReader.Read));
  95. t.Start ();
  96. outReader.Read ();
  97. t.Join ();
  98. proc.WaitForExit();
  99. }
  100. finally {
  101. proc.Close ();
  102. exit_code = proc.ExitCode;
  103. }
  104. }
  105. finally {
  106. if (context != null) {
  107. context.Undo ();
  108. context = null;
  109. }
  110. }
  111. return exit_code;
  112. }
  113. public static Int32 ExecWaitWithCapture (IntPtr userToken, string cmd, TempFileCollection tempFiles, ref string outputName, ref string errorName)
  114. {
  115. return ExecWaitWithCapture (userToken, cmd, Environment.CurrentDirectory, tempFiles, ref outputName, ref errorName);
  116. }
  117. public static Int32 ExecWaitWithCapture (string cmd, string currentDir, TempFileCollection tempFiles, ref string outputName, ref string errorName )
  118. {
  119. return ExecWaitWithCapture ((IntPtr)(-1), cmd, currentDir, tempFiles, ref outputName, ref errorName);
  120. }
  121. public static Int32 ExecWaitWithCapture (string cmd, TempFileCollection tempFiles, ref string outputName, ref string errorName)
  122. {
  123. return ExecWaitWithCapture ((IntPtr)(-1), cmd, Environment.CurrentDirectory, tempFiles, ref outputName, ref errorName);
  124. }
  125. }
  126. }