From ecf25101086810b83d9d4d30ee532c3e2b56ec9b Mon Sep 17 00:00:00 2001 From: Ludovic Henry Date: Thu, 9 Feb 2017 14:57:59 -0500 Subject: [PATCH 1/1] [process] Improve error message for inaccessible process (#4354) Fixes bug https://bugzilla.xamarin.com/show_bug.cgi?id=51561 --- mcs/class/System/System.Diagnostics/Process.cs | 4 ++-- .../System/Test/System.Diagnostics/ProcessTest.cs | 10 ++++++++++ mono/metadata/w32process-unix-osx.c | 7 ++++++- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/mcs/class/System/System.Diagnostics/Process.cs b/mcs/class/System/System.Diagnostics/Process.cs index 8423659fdc7..9b3fdf9dc5e 100644 --- a/mcs/class/System/System.Diagnostics/Process.cs +++ b/mcs/class/System/System.Diagnostics/Process.cs @@ -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 */ diff --git a/mcs/class/System/Test/System.Diagnostics/ProcessTest.cs b/mcs/class/System/Test/System.Diagnostics/ProcessTest.cs index ac07fdc8bab..dcf83695f06 100644 --- a/mcs/class/System/Test/System.Diagnostics/ProcessTest.cs +++ b/mcs/class/System/Test/System.Diagnostics/ProcessTest.cs @@ -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; + } } } diff --git a/mono/metadata/w32process-unix-osx.c b/mono/metadata/w32process-unix-osx.c index eb3d5b70b08..d6696cab584 100644 --- a/mono/metadata/w32process-unix-osx.c +++ b/mono/metadata/w32process-unix-osx.c @@ -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) { -- 2.25.1