Executor.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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.Runtime.InteropServices;
  33. using System.Security.Permissions;
  34. using System.Security.Principal;
  35. using System.Threading;
  36. namespace System.CodeDom.Compiler {
  37. [PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)]
  38. public sealed class Executor {
  39. class ProcessResultReader
  40. {
  41. StreamReader reader;
  42. string file;
  43. public ProcessResultReader (StreamReader reader, string file)
  44. {
  45. this.reader = reader;
  46. this.file = file;
  47. }
  48. public void Read ()
  49. {
  50. StreamWriter sw = new StreamWriter (file);
  51. try
  52. {
  53. string line;
  54. while ((line = reader.ReadLine()) != null)
  55. sw.WriteLine (line);
  56. }
  57. finally
  58. {
  59. sw.Close ();
  60. }
  61. }
  62. }
  63. private Executor ()
  64. {
  65. }
  66. public static void ExecWait (string cmd, TempFileCollection tempFiles)
  67. {
  68. string outputName = null;
  69. string errorName = null;
  70. ExecWaitWithCapture (cmd, Environment.CurrentDirectory, tempFiles, ref outputName, ref errorName);
  71. }
  72. [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
  73. [SecurityPermission (SecurityAction.Assert, ControlPrincipal = true)] // UnmanagedCode "covers" more than ControlPrincipal
  74. public static Int32 ExecWaitWithCapture (IntPtr userToken, string cmd, string currentDir, TempFileCollection tempFiles, ref string outputName, ref string errorName)
  75. {
  76. #if NET_2_0
  77. // WindowsImpersonationContext implements IDisposable only in 2.0
  78. using (WindowsImpersonationContext context = WindowsIdentity.Impersonate (userToken)) {
  79. return InternalExecWaitWithCapture (cmd, currentDir, tempFiles, ref outputName, ref errorName);
  80. }
  81. #else
  82. int result = -1;
  83. WindowsImpersonationContext context = WindowsIdentity.Impersonate (userToken);
  84. try {
  85. result = InternalExecWaitWithCapture (cmd, currentDir, tempFiles, ref outputName, ref errorName);
  86. }
  87. finally {
  88. context.Undo ();
  89. context = null;
  90. }
  91. return result;
  92. #endif
  93. }
  94. public static Int32 ExecWaitWithCapture (IntPtr userToken, string cmd, TempFileCollection tempFiles, ref string outputName, ref string errorName)
  95. {
  96. return ExecWaitWithCapture (userToken, cmd, Environment.CurrentDirectory, tempFiles, ref outputName, ref errorName);
  97. }
  98. [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
  99. public static Int32 ExecWaitWithCapture (string cmd, string currentDir, TempFileCollection tempFiles, ref string outputName, ref string errorName )
  100. {
  101. return InternalExecWaitWithCapture (cmd, currentDir, tempFiles, ref outputName, ref errorName);
  102. }
  103. [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
  104. public static Int32 ExecWaitWithCapture (string cmd, TempFileCollection tempFiles, ref string outputName, ref string errorName)
  105. {
  106. return InternalExecWaitWithCapture (cmd, Environment.CurrentDirectory, tempFiles, ref outputName, ref errorName);
  107. }
  108. private static int InternalExecWaitWithCapture (string cmd, string currentDir, TempFileCollection tempFiles, ref string outputName, ref string errorName)
  109. {
  110. if ((cmd == null) || (cmd.Length == 0))
  111. throw new ExternalException (Locale.GetText ("No command provided for execution."));
  112. if (outputName == null)
  113. outputName = tempFiles.AddExtension ("out");
  114. if (errorName == null)
  115. errorName = tempFiles.AddExtension ("err");
  116. int exit_code = -1;
  117. Process proc = new Process ();
  118. proc.StartInfo.FileName = cmd;
  119. proc.StartInfo.CreateNoWindow = true;
  120. proc.StartInfo.UseShellExecute = false;
  121. proc.StartInfo.RedirectStandardOutput = true;
  122. proc.StartInfo.RedirectStandardError = true;
  123. proc.StartInfo.WorkingDirectory = currentDir;
  124. try {
  125. proc.Start();
  126. ProcessResultReader outReader = new ProcessResultReader (proc.StandardOutput, outputName);
  127. ProcessResultReader errReader = new ProcessResultReader (proc.StandardError, errorName);
  128. Thread t = new Thread (new ThreadStart (errReader.Read));
  129. t.Start ();
  130. outReader.Read ();
  131. t.Join ();
  132. proc.WaitForExit();
  133. }
  134. finally {
  135. exit_code = proc.ExitCode;
  136. // the handle is cleared in Close (so no ExitCode)
  137. proc.Close ();
  138. }
  139. return exit_code;
  140. }
  141. }
  142. }