OSX: proc_pidinfo returns 0 for other user's processes

前端 未结 2 843
轻奢々
轻奢々 2021-02-10 02:28

I need to get some information (PID, UID, GID, process name) about running processes on Mac OSX. I tried proc_pidinfo. For my own processes it works fine. However,

2条回答
  •  爱一瞬间的悲伤
    2021-02-10 02:50

    You can use :

    int proc_listpids(uint32_t type, uint32_t typeinfo, void *buffer, int buffersize);
    

    combinated to proc_pidinfo to get many others pids informations like :

    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    void procpid(pid_t pid)
    { struct proc_bsdinfo proc;
      int st = proc_pidinfo(pid, PROC_PIDTBSDINFO, 0, &proc, PROC_PIDTBSDINFO_SIZE);
      printf("name: %s\n", proc.pbi_name);
    }
    
    void pidlist(void)
    { int bufsize = proc_listpids(PROC_ALL_PIDS, 0, NULL, 0);
      pid_t pids[2 * bufsize / sizeof(pid_t)];
      bufsize = proc_listpids(PROC_ALL_PIDS, 0, pids, sizeof(pids));
      size_t num_pids = bufsize / sizeof(pid_t);
      int i = 0;
      while (i < num_pids)
      { printf("pid[%d]::%d\n", i, pids[i]);
        procpid(pids[i]);
        printf("\n-------------------------------\n\n");
        i += 1; }}
    
    int main(void)
    { pidlist();
      return(42); }
    

提交回复
热议问题