2 // System.CodeDom.Compiler.Executor.cs
5 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // (C) 2003 Andreas Nahr
9 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
31 using System.Diagnostics;
33 using System.Runtime.InteropServices;
34 using System.Security.Permissions;
35 using System.Security.Principal;
36 using System.Threading;
38 namespace System.CodeDom.Compiler {
40 [PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)]
41 public static class Executor {
43 class ProcessResultReader
48 public ProcessResultReader (StreamReader reader, string file)
56 StreamWriter sw = new StreamWriter (file);
61 while ((line = reader.ReadLine()) != null)
71 public static void ExecWait (string cmd, TempFileCollection tempFiles)
73 string outputName = null;
74 string errorName = null;
75 ExecWaitWithCapture (cmd, Environment.CurrentDirectory, tempFiles, ref outputName, ref errorName);
78 [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
79 [SecurityPermission (SecurityAction.Assert, ControlPrincipal = true)] // UnmanagedCode "covers" more than ControlPrincipal
80 public static Int32 ExecWaitWithCapture (IntPtr userToken, string cmd, string currentDir, TempFileCollection tempFiles, ref string outputName, ref string errorName)
83 // WindowsImpersonationContext implements IDisposable only in 2.0
84 using (WindowsImpersonationContext context = WindowsIdentity.Impersonate (userToken)) {
85 return InternalExecWaitWithCapture (cmd, currentDir, tempFiles, ref outputName, ref errorName);
89 WindowsImpersonationContext context = WindowsIdentity.Impersonate (userToken);
91 result = InternalExecWaitWithCapture (cmd, currentDir, tempFiles, ref outputName, ref errorName);
101 public static Int32 ExecWaitWithCapture (IntPtr userToken, string cmd, TempFileCollection tempFiles, ref string outputName, ref string errorName)
103 return ExecWaitWithCapture (userToken, cmd, Environment.CurrentDirectory, tempFiles, ref outputName, ref errorName);
106 [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
107 public static Int32 ExecWaitWithCapture (string cmd, string currentDir, TempFileCollection tempFiles, ref string outputName, ref string errorName )
109 return InternalExecWaitWithCapture (cmd, currentDir, tempFiles, ref outputName, ref errorName);
112 [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
113 public static Int32 ExecWaitWithCapture (string cmd, TempFileCollection tempFiles, ref string outputName, ref string errorName)
115 return InternalExecWaitWithCapture (cmd, Environment.CurrentDirectory, tempFiles, ref outputName, ref errorName);
118 private static int InternalExecWaitWithCapture (string cmd, string currentDir, TempFileCollection tempFiles, ref string outputName, ref string errorName)
120 if ((cmd == null) || (cmd.Length == 0))
121 throw new ExternalException (Locale.GetText ("No command provided for execution."));
123 if (outputName == null)
124 outputName = tempFiles.AddExtension ("out");
126 if (errorName == null)
127 errorName = tempFiles.AddExtension ("err");
130 Process proc = new Process ();
131 proc.StartInfo.FileName = cmd;
132 proc.StartInfo.CreateNoWindow = true;
133 proc.StartInfo.UseShellExecute = false;
134 proc.StartInfo.RedirectStandardOutput = true;
135 proc.StartInfo.RedirectStandardError = true;
136 proc.StartInfo.WorkingDirectory = currentDir;
141 ProcessResultReader outReader = new ProcessResultReader (proc.StandardOutput, outputName);
142 ProcessResultReader errReader = new ProcessResultReader (proc.StandardError, errorName);
144 Thread t = new Thread (new ThreadStart (errReader.Read));
153 exit_code = proc.ExitCode;
154 // the handle is cleared in Close (so no ExitCode)