static task_t pid_to_task(int pid) { task_t task = 0; static task_t old_task = 0; static int old_pid = -1; kern_return_t kr; if (old_task != 0 && old_pid == pid) { return old_task; } else if (old_task != 0 && old_pid != pid) { //we changed the process pid so deallocate a ref from the old_task //since we are going to get a new task kr = mach_port_deallocate (mach_task_self (), old_task); if (kr != KERN_SUCCESS) { eprintf ("pid_to_task: fail to deallocate port\n"); return 0; } } int err = task_for_pid (mach_task_self (), (pid_t)pid, &task); if ((err != KERN_SUCCESS) || !MACH_PORT_VALID (task)) { task = task_for_pid_workaround (pid); if (task == MACH_PORT_NULL) { task = task_for_pid_ios9pangu (pid); if (task != MACH_PORT_NULL) { //eprintf ("Failed to get task %d for pid %d.\n", (int)task, (int)pid); //eprintf ("Missing priviledges? 0x%x: %s\n", err, MACH_ERROR_STRING (err)); return -1; } } } old_task = task; old_pid = pid; return task; }
static task_t pid_to_task(RIODesc *fd, int pid) { task_t task = 0; static task_t old_task = 0; static int old_pid = -1; kern_return_t kr; RIODescData *iodd = fd? (RIODescData *)fd->data: NULL; RIOMach *riom = NULL; if (iodd) { riom = iodd->data; if (riom && riom->task) { old_task = riom->task; riom->task = 0; old_pid = iodd->pid; } } if (old_task != 0) { if (old_pid == pid) { return old_task; } //we changed the process pid so deallocate a ref from the old_task //since we are going to get a new task kr = mach_port_deallocate (mach_task_self (), old_task); if (kr != KERN_SUCCESS) { eprintf ("pid_to_task: fail to deallocate port\n"); return 0; } } int err = task_for_pid (mach_task_self (), (pid_t)pid, &task); if ((err != KERN_SUCCESS) || !MACH_PORT_VALID (task)) { task = task_for_pid_workaround (pid); if (task == MACH_PORT_NULL) { task = task_for_pid_ios9pangu (pid); if (task != MACH_PORT_NULL) { //eprintf ("Failed to get task %d for pid %d.\n", (int)task, (int)pid); //eprintf ("Missing priviledges? 0x%x: %s\n", err, MACH_ERROR_STRING (err)); return -1; } } } old_task = task; old_pid = pid; return task; }