views:

158

answers:

2
A: 
lewiguez
+2  A: 

The only complete list of running processes is provided by 2 above, asking the kernel. Getting the actual name of the process is not straight forward. In a nutshell, you look up the pid in any other source you can find until you get a match.

For some processes, the following will work:

ProcessSerialNumber         psn;
CFStringRef             name = NULL;
status = GetProcessForPID( inPID , &psn );
if ( noErr == status ) CopyProcessName( &psn , &name );

For some processes, you can look up the pid in the results of [[NSWorkspace sharedWorkspace] launchedApplications] by NSApplicationProcessIdentifier. Available with 10.2 and later. Most, but maybe not all, items in this list will be the same as CopyProcessName above.

For some processes, you can look up the process arguments and get the full path from the first argument. Similar to getting the original list, but using KERN_PROCARGS or KERN_PROCARGS2 as the second mib value. This is what ps is doing.

For some processes, you are stuck with the 16 character p_comm.

drawnonward
+1 this looks quite promising! Getting the `ProcessSerialNumber` didn't work, but I'll try the `KERN_PROCARGS` approach and report back.
Dave DeLong
`KERN_PROCARGS` and `KERN_PROCARGS2` work if my app is also running as `root`. Unfortunately, it isn't. :(
Dave DeLong
Yes, ps is setuid root. I think there is a special group, maybe procmod, that can also access those. There used to be a whole undocumented CPS suite for this stuff, but it appears to be deprecated. Looks like Apple has made this a security issue.
drawnonward