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, for processes owned by other users, 0 is returned. There's no documentation for this function, but according to information found on Internet, it's supposed to return number of bytes written into provided buffer. Calling this function on other user's processes returns 0, which means that no information was provided.
Example:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <libproc.h>
int main(int argc, char *argv[])
{
pid_t pid;
struct proc_bsdinfo proc;
if (argc == 2)
pid = atoi(argv[1]);
else
pid = getpid();
int st = proc_pidinfo(pid, PROC_PIDTBSDINFO, 0,
&proc, PROC_PIDTBSDINFO_SIZE);
if (st != PROC_PIDTBSDINFO_SIZE) {
fprintf(stderr, "Cannot get process info");
return 1;
}
printf(" pid: %d\n", (int)proc.pbi_pid);
printf("ppid: %d\n", (int)proc.pbi_ppid);
printf("comm: %s\n", proc.pbi_comm);
printf("name: %s\n", proc.pbi_name);
printf(" uid: %d\n", (int)proc.pbi_uid);
printf(" gid: %d\n", (int)proc.pbi_gid);
return 0;
}
Running this program yields:
$ ./pidinfo
pid: 30519
ppid: 8434
comm: pidinfo
name: pidinfo
uid: 501
gid: 20
$ ./pidinfo 1
Cannot get process info
$ sudo ./pidinfo 1
pid: 1
ppid: 0
comm: launchd
name: launchd
uid: 0
gid: 0
That's strange, because I can get all this information from ps(1)
. But then I checked that both ps
and top
on OSX are SUID binaries, which would be in line with proc_pidinfo
behavior:
$ ls -l `which ps` `which top`
-rwsr-xr-x 1 root wheel 51008 5 maj 08:06 /bin/ps
-r-sr-xr-x 1 root wheel 87952 5 maj 08:05 /usr/bin/top
But then, Activity Monitor works without SUID.
So, my question is, why proc_pidinfo
provides information only about my own processes? Can I make it give me information about other processes? If not, how can I get this information without parsing ps(1)
output?