5

I have used NSApplication and NSWorkspace to get a list of running applications.

But it is only giving me the applications that are active from the administrator and not the root processes that run in the background.

I want to get a list of all running processes and keep updating that list as soon as a new process spawns.

I prefer not to use NSTask and parsing the output.

Is there a fix for this?

NSArray * runningapps = [[NSWorkspace sharedWorkspace] runningApplications];
Bharath Suresh
  • 483
  • 2
  • 18
  • I think following one is useful. [Programmatically check if a process is running on Mac](https://stackoverflow.com/questions/2518160/programmatically-check-if-a-process-is-running-on-mac) – MyCometG3 Jun 11 '18 at 23:05
  • What are you trying to do? Perhaps you don't need what you are asking for. Are you trying to check whether a process you spawned is running? – jvarela Jun 12 '18 at 00:20
  • 1
    I want to know if any process spawns in the background. I was able to do it for applications but the root processes are not being listed – Bharath Suresh Jun 12 '18 at 04:56

3 Answers3

1

To have access to the list of root processes you need to do stuff very similar to what the ps command does. If you want to go at it, go study the source code of this tool:

https://opensource.apple.com/source/adv_cmds/adv_cmds-172/ps/

However, as you can see, this is not easy. Thus, if you don't want to reinvent the wheel, I'd just parse the output of ps command with grep or you will need to write your own code to do what you want.

jvarela
  • 3,744
  • 1
  • 22
  • 43
  • After all, there is no need to be root to list all the processes. I just managed to build and run ps from Apple's source code in the lldb and it works without being root. – jvarela Jun 13 '18 at 05:28
1

Refer to the website given below : https://github.com/objective-see/ProcInfo

Great Person
  • 62
  • 10
1

The way I achieve this is simply by iterating through every process identifier (to a certain extent). (This does require higher permissions though like sudo to get info on root processes)

#include <sys/proc_info.h>
#define SHOW_ZOMBIES 0

extern int proc_pidinfo(int pid, int flavor, uint64_t arg, user_addr_t buffer, uint32_t buffersize);

void iterateProcesses() {

    pid_t maxPID = 100000;
    for (pid_t cPID = 0; cPID < maxPID+1; cPID++) {
        if (!(getpgid(cPID)>=0)) {continue;} // Skips if PID is invalid

        // Now we can get some information 

        struct proc_taskallinfo info;
        proc_pidinfo(cPID, PROC_PIDTASKALLINFO, SHOW_ZOMBIES, (user_addr_t)&info, sizeof(struct proc_taskallinfo));

        char *proc_name = (char *)info.pbsd.pbi_name;
        printf("Found a Process Called %s With a PID of %d\n",proc_name,cPID);

        // Now if we want to do some more stuff with it we can get its task.

        task_t task;
        kern_return_t ret = task_for_pid(current_task(),cPID,&task);
        if (ret != KERN_SUCCESS) {continue;} // Can't access task. skip

        // Now we have the task. We can do some stuff with it...
        task_suspend(task);
    }
}

Anyways I hope this helps someone. ;)

YeaTheMans
  • 1,005
  • 8
  • 19
  • This was really useful and was pretty much exactly what I was looking for. Thank you! However, I found that this code leaked memory on each iteration. I solved the memory leak by replacing the relevant line of code with this: proc_pidinfo(cPID, PROC_PIDTASKALLINFO, (uint64_t)0, &info, sizeof(info)); – William Gustafson Feb 03 '21 at 21:28
  • ahh ye my bad, thats good that you got some use outta it :P – YeaTheMans Feb 20 '21 at 10:02