[System] Fix handle leak in Process::GetProcess.
[mono.git] / mcs / class / System / System.Diagnostics / Process.cs
index 26a5c1ba93daabd7080b7d03f3bb283cf9a8e9c2..b038095093c5b31d60332cd4de3021c0b11e0787 100644 (file)
@@ -331,9 +331,9 @@ namespace System.Diagnostics
 
                                                process_name = ProcessName_internal (handle);
 
-                                               /* If process_name is _still_ null, assume the process has exited */
+                                               /* If process_name is _still_ null, assume the process has exited or is inaccessible */
                                                if (process_name == null)
-                                                       throw new InvalidOperationException ("Process has exited, so the requested information is not available.");
+                                                       throw new InvalidOperationException ("Process has exited or is inaccessible, so the requested information is not available.");
 
                                                /* Strip the suffix (if it exists) simplistically instead of removing
                                                 * any trailing \.???, so we dont get stupid results on sane systems */
@@ -482,7 +482,36 @@ namespace System.Diagnostics
                        if (proc == IntPtr.Zero)
                                throw new ArgumentException ("Can't find process with ID " + processId.ToString ());
 
-                       return (new Process (new SafeProcessHandle (proc, false), processId));
+                       /* The handle returned by GetProcess_internal is owned by its caller, so we must pass true to SafeProcessHandle */
+                       return (new Process (new SafeProcessHandle (proc, true), processId));
+               }
+
+               public static Process[] GetProcessesByName(string processName, string machineName)
+               {
+                       if (machineName == null)
+                               throw new ArgumentNullException ("machineName");
+
+                       if (!IsLocalMachine (machineName))
+                               throw new NotImplementedException ();
+
+                       Process[] processes = GetProcesses ();
+                       if (processes.Length == 0)
+                               return processes;
+
+                       int size = 0;
+
+                       for (int i = 0; i < processes.Length; i++) {
+                               try {
+                                       if (String.Compare (processName, processes[i].ProcessName, true) == 0)
+                                               processes [size++] = processes[i];
+                               } catch (SystemException) {
+                                       /* The process might exit between GetProcesses_internal and GetProcessById */
+                               }
+                       }
+
+                       Array.Resize<Process> (ref processes, size);
+
+                       return processes;
                }
 
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
@@ -821,13 +850,13 @@ namespace System.Diagnostics
                }
 
                [Obsolete ("Process.Start is not supported on the current platform.", true)]
-               public static Process Start(string fileName, string username, SecureString password, string domain)
+               public static Process Start(string fileName, string userName, SecureString password, string domain)
                {
                        throw new PlatformNotSupportedException ("Process.Start is not supported on the current platform.");
                }
 
                [Obsolete ("Process.Start is not supported on the current platform.", true)]
-               public static Process Start(string fileName, string arguments, string username, SecureString password, string domain)
+               public static Process Start(string fileName, string arguments, string userName, SecureString password, string domain)
                {
                        throw new PlatformNotSupportedException ("Process.Start is not supported on the current platform.");
                }