Skip to content

Diagnostics.Process on OSX should try harder to provide ProcessName #30909

Closed

Description

@JamesWTruher

It seems like the ProcessName should be more than an empty string. On OSX this is pretty egregious as the first couple 100 processes don't have a name. Providing the path to the executable could be a very reasonable fallback. (or perhaps pbi_name has this already?)

I know that proc id 0 is the exception to this rule, but still ProcessName should be useful.

Activity

  1. danmoseley commented on Sep 23, 2019

    @danmoseley
    Contributor

    Thanks for the report. Any interest in proposing a PR?

  2. wfurt commented on Oct 1, 2019

    @wfurt
    Member

    I did quick check and the output is lame as @JamesWTruher mentioned. It seems like we are failing on

                Interop.libproc.proc_taskallinfo? info = Interop.libproc.GetProcessInfoById(pid);
                if (info.HasValue)

    So the problem seems to live deeper in Interop/libproc.

    cc: @aik-jahoda

  3. the-exodus commented on Nov 15, 2019

    @the-exodus

    I did quick check and the output is lame as @JamesWTruher mentioned. It seems like we are failing on

                Interop.libproc.proc_taskallinfo? info = Interop.libproc.GetProcessInfoById(pid);
                if (info.HasValue)

    So the problem seems to live deeper in Interop/libproc.

    cc: @aik-jahoda

    It seems like you've generated the interop for libproc from really old headers. You need to sudo to get all the task info, and the proc_bsdshortinfo struct (which is what you can fill as low-priv) doesn't seem to be generated.

  4. aik-jahoda commented on Nov 15, 2019

    @aik-jahoda
    Contributor

    @the-exodus I'm not sure what you mean by we are generating interop from old headers. Can you please explain it more? There is no related native code in corefx there is pinvoke only. Are you referring to managed constants and structs definition?

  5. aik-jahoda commented on Nov 15, 2019

    @aik-jahoda
    Contributor

    I tested native proc_pidinfo and proc_name. Both behave the same on the system pids. It requires sudo to be able to obtain the process info.

    The proc_name uses proc_pidinfo under the hood and the app should have admin privileges to be able to obtain system process information.

    So far the current behavior looks good for me as it respects the OS. The fallback to the path should be implemented on a different layer as a path is not the name.

  6. the-exodus commented on Nov 15, 2019

    @the-exodus

    @the-exodus I'm not sure what you mean by we are generating interop from old headers. Can you please explain it more? There is no related native code in corefx there is pinvoke only. Are you referring to managed constants and structs definition?

    Sorry, I totally failed at English there. Anyway, I assumed you had generated the type definitions etc from the header files. The proc_bsdshortinfo type was added a few years ago, and since I didn't see it in the corefx-code I assumed you had been using older headers.

    The bsdshortinfo doesn't have name, stats, etc, as you can't query that info for other user's processes. It has everything you CAN get though, which is better than the nullref you get after getting access denied :)

  7. aik-jahoda commented on Nov 18, 2019

    @aik-jahoda
    Contributor

    @the-exodus the bsdshortinfo works fine. Thank you for pointing to this API.

  8. transferred this issue fromdotnet/corefxon Feb 1, 2020
  9. added this to the 5.0 milestone on Feb 1, 2020
  10. added
    untriaged
    on Feb 23, 2020
  11. removed
    untriaged
    on Jul 6, 2020
  12. removed their assignment
    on Jul 6, 2020
  13. danmoseley commented on Jul 8, 2020

    @danmoseley
    Contributor

    @aik-jahoda sounds like you have a sketch of a fix?

  14. aik-jahoda commented on Jul 8, 2020

    @aik-jahoda
    Contributor

    I did a c example which demonstrate the functionality. We need to implement C# interop on our side.

  15. danmoseley commented on Jul 8, 2020

    @danmoseley
    Contributor

    Perhaps you could paste that in here and maybe a member of the community would be interested in doing it.

  16. modified the milestones: 5.0.0, Future on Jul 9, 2020
  17. aik-jahoda commented on Jul 9, 2020

    @aik-jahoda
    Contributor

    Attached is example c program:

    procinfo.zip

    There is several way how to obtain proc name:

    1. in method name it calls proc_name which return truncated proc name (31char+null terminating). For PID<100 return empty name unless it runs as sudo
    2. in method taskallinfo it calls proc_pidinfo with PROC_PIDTASKALLINFO arg which return truncated proc name (15char+null terminating). For PID<100 return invalid structure unless it runs as sudo
    3. in method bsdshortinfo it calls proc_pidinfo with PROC_PIDT_SHORTBSDINFO arg which return truncated proc name (15char+null terminating). For PID<100 works without sudo
    4. in method bsdinfo it calls proc_pidinfo with PROC_PIDTBSDINFO arg which return truncated proc name (15char+null terminating). For PID<100 return invalid structure unless it runs as sudo
    5. in method pathPart it returns process executable. It works for all cases and without truncating
  18. danmoseley commented on Jul 9, 2020

    @danmoseley
    Contributor

    Pasting here so it's searchable

    #include <iostream>
    
    #include <cstdlib>
    #include "libproc.h"
    
    
    void name(int pid ) {
        char pr_name[255];
        pr_name[0] = '\0';
    
        proc_name(pid, pr_name, 255);
    
        std::cout << "Name: " << pr_name << std::endl;
    }
    
    void taskallinfo(int pid ){
           proc_taskallinfo info;
           int size = sizeof(proc_taskallinfo);
    
           intptr_t ret = proc_pidinfo(pid, PROC_PIDTASKALLINFO, 0, &info, size);
    
           if (ret != size) {
               std::cout << "Invalid size\n" << "ret: " << ret << "\n" << "size: " << size << std::endl;
           }
           else {
                  std::cout << "Info: " << (char *) info.pbsd.pbi_comm << std::endl;
           }
    }
    
    void bsdinfo(int pid) {
        proc_bsdinfo info;
        int size = sizeof(info);
    
        intptr_t ret = proc_pidinfo(pid, PROC_PIDTBSDINFO, 0, &info, size);
    
        if (ret != size) {
            std::cout << "Invalid size\n" << "ret: " << ret << "\n" << "size: " << size << std::endl;
        }
        else {
               std::cout << "Info: " << (char *) info.pbi_comm << std::endl;
        }
    }
    
    void bsdshortinfo(int pid) {
        proc_bsdshortinfo info;
        int size = sizeof(info);
    
        intptr_t ret = proc_pidinfo(pid, PROC_PIDT_SHORTBSDINFO, 0, &info, size);
    
        if (ret != size) {
            std::cout << "Invalid size\n" << "ret: " << ret << "\n" << "size: " << size << std::endl;
        }
        else {
               std::cout << "Info: " << (char *) info.pbsi_comm << std::endl;
        }
    }
    
    void pathPart(int pid) {
        char pathBuffer [PROC_PIDPATHINFO_MAXSIZE];
        proc_pidpath(pid, pathBuffer, sizeof(pathBuffer));
    
        char nameBuffer[256];
    
        size_t position = strlen(pathBuffer);
        while(position >= 0 && pathBuffer[position] != '/')
        {
            position--;
        }
    
        strcpy(nameBuffer, pathBuffer + position + 1);
    
        std::cout << "name:" << nameBuffer << "\n\n";
    }
    
    int main(int argc, const char * argv[]) {
    
        if (argc <2 ) {
            return 1;
        }
    
        int pid = atoi(argv[1]);
    
        name(pid);
        taskallinfo(pid);
        bsdshortinfo(pid);
        bsdinfo(pid);
        pathPart(pid);
    
        return 0;
    }
  19. dotnet-policy-service commented on Jan 8, 2025

    @dotnet-policy-service
    Contributor

    Due to lack of recent activity, this issue has been marked as a candidate for backlog cleanup. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will undo this process.

    This process is part of our issue cleanup automation.

  20. added
    backlog-cleanup-candidate
    on Jan 8, 2025
  21. dotnet-policy-service commented on Jan 23, 2025

    @dotnet-policy-service
    Contributor

    This issue will now be closed since it had been marked no-recent-activity but received no further activity in the past 14 days. It is still possible to reopen or comment on the issue, but please note that the issue will be locked if it remains inactive for another 30 days.

  22. removed this from the Future milestone on Jan 23, 2025
  23. locked and limited conversation to collaborators on Feb 22, 2025
  24. removed
    backlog-cleanup-candidate
    on Feb 22, 2025
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

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions