Bug 51561 - Getting process name of process running under higher privilege user throws exception
Summary: Getting process name of process running under higher privilege user throws ex...
Status: RESOLVED ANSWERED
Alias: None
Product: Runtime
Classification: Mono
Component: io-layer ()
Version: master
Hardware: PC Mac OS
: --- enhancement
Target Milestone: Future Cycle (TBD)
Assignee: Ludovic Henry
URL:
Depends on:
Blocks:
 
Reported: 2017-01-16 18:46 UTC by Alexander Köplinger [MSFT]
Modified: 2017-02-09 00:03 UTC (History)
3 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED ANSWERED

Description Alexander Köplinger [MSFT] 2017-01-16 18:46:41 UTC
Repro code:

> using System;
> using System.Diagnostics;
> 
> class Program
> {
>         public static void Main (string[] args)
>         {
>                 var proc = Process.GetProcessById (1);
>                 Console.WriteLine ("PID: " + proc.Id);
>                 Console.WriteLine ("HasExited: " + proc.HasExited);
>                 Console.WriteLine ("Name: " + proc.ProcessName);
>         }
> }

Process 1 runs under root, if I run this app under my normal user I get the following exception:

>$ mono --debug test.exe
> PID: 1
> HasExited: False
> 
> Unhandled Exception:
> System.InvalidOperationException: Process has exited, so the requested information is not available.
>   at System.Diagnostics.Process.get_ProcessName () [0x0002d] in /Users/alexander/dev/mono/mcs/class/System/System.Diagnostics/Process.cs:336
>   at (wrapper remoting-invoke-with-check) System.Diagnostics.Process:get_ProcessName ()
>   at Program.Main (System.String[] args) [0x0003b] in <9c838a66cdc44c81b38c974edcc9c029>:0
> [ERROR] FATAL UNHANDLED EXCEPTION: System.InvalidOperationException: Process has exited, so the requested information is not available.
>   at System.Diagnostics.Process.get_ProcessName () [0x0002d] in /Users/alexander/dev/mono/mcs/class/System/System.Diagnostics/Process.cs:336
>   at (wrapper remoting-invoke-with-check) System.Diagnostics.Process:get_ProcessName ()
>   at Program.Main (System.String[] args) [0x0003b] in <9c838a66cdc44c81b38c974edcc9c029>:0

Note that it says the process has exited, which is clearly wrong.

It works fine with sudo:

> $ sudo mono test.exe
> Password:
> PID: 1
> HasExited: False
> Name: launchd

Customer report in the mono Gitter that prompted this: https://gitter.im/mono/mono?at=587cf057074f7be763da0dc9
Reproduced using mono master/70ce36925.
Comment 1 Ludovic Henry 2017-02-08 23:00:39 UTC
This fails because `proc_name` on OSX returns `Operation not permitted`. The exception message is wrong, but because there is not equivalent error on Win32, that's pretty much as close as you will get. That could be a feature enhancement though.
Comment 2 Alexander Köplinger [MSFT] 2017-02-08 23:43:56 UTC
Ok, thanks, makes sense. Let's update the error message so it's less confusing, e.g. to "Process has exited or is inaccessible, so the requested information is not available."
Comment 3 Ludovic Henry 2017-02-09 00:03:53 UTC
This is fixed with https://github.com/mono/mono/pull/4354