Skip to content
Discussion options

You must be logged in to vote

The following approach isn't restricted by SIP, yet you might need root privileges.

- (pid_t)findProcessByName:(NSString *)processName {
    if (!processName || processName.length == 0) return -1;

    const char *name = [processName UTF8String];
    pid_t *pids = NULL;
    int bufCount = 4096;
    int num = 0;

    for (;;) {
        pids = (pid_t *)realloc(pids, bufCount * sizeof(pid_t));
        if (!pids) return -1;

        num = proc_listallpids(pids, bufCount * sizeof(pid_t));
        if (num < 0) {
            free(pids);
            return -1;
        }
        if (num >= bufCount) {
            bufCount *= 2;
            continue;
        }
        break;
    }

    pid_t found …

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by andrd3v
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants