[process] Improve error message for inaccessible process (#4354)
authorLudovic Henry <ludovic@xamarin.com>
Thu, 9 Feb 2017 19:57:59 +0000 (14:57 -0500)
committerGitHub <noreply@github.com>
Thu, 9 Feb 2017 19:57:59 +0000 (14:57 -0500)
Fixes bug https://bugzilla.xamarin.com/show_bug.cgi?id=51561

mcs/class/System/System.Diagnostics/Process.cs
mcs/class/System/Test/System.Diagnostics/ProcessTest.cs
mono/metadata/w32process-unix-osx.c

index 8423659fdc7cd86f9fd552b6656e3a49396cc0f1..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 */
index ac07fdc8bab5cfb132beb28b8ede2f3a76dd832b..dcf83695f06517fa3d91dd10a1519d2bf7f5cf91 100644 (file)
@@ -1120,5 +1120,15 @@ namespace MonoTests.System.Diagnostics
                        // This should return Process[0] or a Process[] with all the "foo" programs running
                        Process.GetProcessesByName ("foo");
                }
+
+               [Test]
+               [ExpectedException(typeof(InvalidOperationException))]
+               public void HigherPrivilegeProcessName ()
+               {
+                       if (!RunningOnUnix)
+                               Assert.Ignore ("accessing pid 1, only available on unix");
+
+                       string v = Process.GetProcessById (1).ProcessName;
+               }
        }
 }
index eb3d5b70b082e8f447a0dfe252f98d5b06665c86..d6696cab584277ab946a3ba8f37f837807c431e5 100644 (file)
@@ -54,10 +54,15 @@ mono_w32process_get_name (pid_t pid)
        g_free (pi);
 #else
        gchar buf[256];
+       gint res;
 
        /* No proc name on OSX < 10.5 nor ppc nor iOS */
        memset (buf, '\0', sizeof(buf));
-       proc_name (pid, buf, sizeof(buf));
+       res = proc_name (pid, buf, sizeof(buf));
+       if (res == 0) {
+               mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: proc_name failed, error (%d) \"%s\"", __func__, errno, g_strerror (errno));
+               return NULL;
+       }
 
        // Fixes proc_name triming values to 15 characters #32539
        if (strlen (buf) >= MAXCOMLEN - 1) {