user429620
user429620

Reputation:

objective-c get list of processes, their paths AND arguments

Getting the list of processes and their path is quite easy;

    int numberOfProcesses = proc_listpids(PROC_ALL_PIDS, 0, NULL, 0);
pid_t pids[1024];
bzero(pids, 1024);
proc_listpids(PROC_ALL_PIDS, 0, pids, sizeof(pids));
for (int i = 0; i < numberOfProcesses; ++i) {
    if (pids[i] == 0) { continue; }
    char pathBuffer[PROC_PIDPATHINFO_MAXSIZE];
    bzero(pathBuffer, PROC_PIDPATHINFO_MAXSIZE);
    proc_pidpath(pids[i], pathBuffer, sizeof(pathBuffer));

    char arguments[KERN_PROCARGS2];

    if (strlen(pathBuffer) > 0) {
        printf("path: %s\n", pathBuffer);
    }
}

However, I would also like to get any arguments that were used to launch these processes. I can't seem to find how to do this. Any pointers?

Upvotes: 1

Views: 1603

Answers (2)

user4821390
user4821390

Reputation:

./build64.sh # Build cmdline app for 64-bit Intel Mac
# Enumerate all processes running and print the argvs
./xproc --pid-enum | xargs -L1 ./xproc --cmd-from-pid 

Calling the functions directly will be faster than running a new task.

Source code can be built for Windows, MacOS, Linux, and FreeBSD.

Feel free to borrow any portion of code you may need from it:

https://github.com/time-killer-games/xproc

Upvotes: 0

CRD
CRD

Reputation: 53000

A pointer? The ps command lists them and its source is available as part of Apple's open source: ps folder.

Upvotes: 1

Related Questions