Bug 51561

Summary: Getting process name of process running under higher privilege user throws exception
Product: [Mono] Runtime Reporter: Alexander Köplinger [MSFT] <alkpli>
Component: io-layerAssignee: Ludovic Henry <ludovic>
Status: RESOLVED ANSWERED    
Severity: enhancement CC: kyle.white, mono-bugs+mono, mono-bugs+runtime
Priority: ---    
Version: master   
Target Milestone: Future Cycle (TBD)   
Hardware: PC   
OS: Mac OS   
Tags: Is this bug a regression?: ---
Last known good build:

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