[process] Improve error message for inaccessible process (#4354)
[mono.git] / mcs / class / System / System.Diagnostics / Process.cs
index fcdd0c072d87604ab4827eb15eb29944c8988565..9b3fdf9dc5e26a631378240e3aba9405896f8dbd 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 */
@@ -485,6 +485,34 @@ namespace System.Diagnostics
                        return (new Process (new SafeProcessHandle (proc, false), 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)]
                private extern static int[] GetProcesses_internal();
 
@@ -747,6 +775,8 @@ namespace System.Diagnostics
                        SetProcessHandle (new SafeProcessHandle (procInfo.process_handle, true));
                        SetProcessId (procInfo.pid);
                        
+#pragma warning disable 618
+
                        if (startInfo.RedirectStandardInput) {
                                MonoIO.Close (stdin_read, out error);
 
@@ -775,6 +805,7 @@ namespace System.Diagnostics
 
                                standardError = new StreamReader (new FileStream (stderr_read, FileAccess.Read, true, 8192), stderrEncoding, true);
                        }
+#pragma warning restore
 
                        return true;
                }