Skip to content

System.Diagnostics.Process.GetProcesses() returns truncated 15 char string on macOS #52860

Closed

Description

@SimeonIksanov

When using Process.GetProcesses() on macOS in dotnet core it returns process names as strings max 15 chars. It truncates the process names to be 15 char long.

Configuration

dotnet --info
.NET SDK (reflecting any global.json):
Version: 5.0.203
Commit: 383637d63f

Runtime Environment:
OS Name: Mac OS X
OS Version: 10.15
OS Platform: Darwin
RID: osx.10.15-x64
Base Path: /usr/local/share/dotnet/sdk/5.0.203/

Host (useful for support):
Version: 5.0.6
Commit: 478b2f8

Activity

  1. ghost added
    untriaged
    on May 17, 2021
  2. added
    os-macos
    on May 19, 2021
  3. wfurt commented on May 19, 2021

    @wfurt
    Member

    I think that comes from MAXCOMLEN limit on kinfo_proc. I think we would need to find different way than

    info.ProcessName = Marshal.PtrToStringAnsi((IntPtr)kinfo->ki_comm)!;
  4. added this to the Future milestone on Jul 13, 2021
  5. removed
    untriaged
    on Jul 13, 2021
  6. added
    help wanted
    on Jun 23, 2022
  7. Genbox commented on Jul 6, 2022

    @Genbox
    Contributor

    To whomever grabs this. The sysctrl interface in macOS does not support giving longer paths. You need the libproc interface instead. Luckily, the interop is already implemented in .NET Runtime.

    That means, to fix this issue, it is a simple matter of rewriting the line here, to use the libproc interop.

  8. Genbox commented on Jul 6, 2022

    @Genbox
    Contributor

    For anyone with this problem, a workaround is to call Process.GetCurrentProcess().MainModule.FileName instead. Something like this:

    string? fileName = currentProcess.MainModule?.FileName;
    
    if (string.IsNullOrEmpty(fileName))
        throw new InvalidOperationException("Unable to obtain process name");
    
    char[] split = { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar };
    
    int idx = fileName.LastIndexOfAny(split);
    string processName = fileName.Substring(idx + 1);
  9. sonodima commented on Dec 22, 2023

    @sonodima
    Contributor

    Is there anything blocking the transition to using the main module's FileName property in the implementation of Process.Name and Process.GetProcessesByName?

    Given that even the ps command extracts the long name from the main module's path I believe it's the right thing to do here as well.

  10. danmoseley commented on Dec 22, 2023

    @danmoseley
    Contributor

    @sonodima I think this issue is available if you want to give it a try.

  11. wfurt commented on Dec 22, 2023

    @wfurt
    Member

    That would work only for current process right @sonodima? (e.g. it would not work for all processes in the system)
    Even that would be good improvement.

  12. am11 commented on Dec 22, 2023

    @am11
    Member

    For anyone with this problem, a workaround is to call Process.GetCurrentProcess().MainModule.FileName instead. Something like this:

    string? fileName = currentProcess.MainModule?.FileName;
    
    if (string.IsNullOrEmpty(fileName))
        throw new InvalidOperationException("Unable to obtain process name");
    
    char[] split = { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar };
    
    int idx = fileName.LastIndexOfAny(split);
    string processName = fileName.Substring(idx + 1);

    This produces the same result: Path.GetFileName(Environment.ProcessPath) (.NET 6+).

  13. sonodima commented on Dec 22, 2023

    @sonodima
    Contributor

    That would work only for current process right @sonodima? (e.g. it would not work for all processes in the system) Even that would be good improvement.

    This should work on all the processes. I wrote a similar implementation some time ago in another project's process abstraction, and using the filename was still better behaviour than using the truncated name.

    Moreover, in these tests using the path appears to work well even on system processes which would otherwise require sudo permissions.

    In theory for GetProcessesByName we would need to modify the CreateProcessInfo function in ProcessManager.OSX.cs and use Path.GetFileName(GetProcPath(pid)) instead of temp.pbsd.pbi_comm

  14. ghost added
    in-pr
    on Jan 25, 2024
  15. ghost removed
    in-pr
    on Jan 29, 2024
  16. locked and limited conversation to collaborators on Feb 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Diagnostics.Processhelp wantedos-macos

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions