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