void shutdown_process (const char *name) { struct kinfo_proc *mylist = NULL; size_t mycount = 0; pid_t current_pid = getpid(); GetBSDProcessList (&mylist, &mycount); for (size_t k = 0; k < mycount; k++) { kinfo_proc *proc = &mylist[k]; if (strcmp (proc->kp_proc.p_comm, name) == 0 && proc->kp_proc.p_pid != current_pid && proc->kp_eproc.e_pcred.p_ruid == getuid()) { kill (proc->kp_proc.p_pid, SIGKILL); } } free (mylist); }
int count_process(const char *process_name) { int count = 0; struct kinfo_proc *mylist = NULL; size_t mycount = 0; GetBSDProcessList (&mylist, &mycount); for (size_t k = 0; k < mycount; k++) { kinfo_proc *proc = &mylist[k]; if (strcmp (proc->kp_proc.p_comm, process_name) == 0 && proc->kp_eproc.e_pcred.p_ruid == getuid()){ count++; } } free (mylist); return count; }
static char *get_my_exe(void) { #ifdef OS_OSX size_t cnt; struct kinfo_proc *procs = 0; int err; char *ret = 0; if ( (err=GetBSDProcessList(&procs, &cnt )) ) { fprintf(stderr, "GetBSDProcessList: err %d\n", err); return 0; } if (procs) { int i; pid_t mypid = getpid(); for (i = 0; i < (int)cnt; ++i) { if (procs[i].kp_proc.p_pid == mypid) { ret = strdup(procs[i].kp_proc.p_comm); } } } free(procs); return ret; #else char buf[PATH_MAX+1], myexe[PATH_MAX+1]; char *ret = 0; int count = 0; CHKPROC; buf[PATH_MAX] = 0; /* build the /proc/self/exe string */ snprintf(buf, PATH_MAX, "%s/%s/%s", PROCPATH, "self", "exe"); count = readlink(buf, myexe, PATH_MAX); /* this should never happen.. */ if (count < 0) { perror("readlink"); return 0; } myexe[count] = 0; ret = (char *)calloc(count+1, sizeof(const char *)); if (!ret) { perror("calloc"); return 0; } strncpy(ret, myexe, count); return ret; #endif }
static int getBSDProcessPid (const char *name, int except_pid) { int pid = 0; struct kinfo_proc *mylist = NULL; size_t mycount = 0; GetBSDProcessList (&mylist, &mycount); for (size_t k = 0; k < mycount; k++) { kinfo_proc *proc = &mylist[k]; if (proc->kp_proc.p_pid != except_pid && strcmp (proc->kp_proc.p_comm, name) == 0 && proc->kp_eproc.e_pcred.p_ruid == getuid()){ pid = proc->kp_proc.p_pid; break; } } free (mylist); return pid; }
bool IsInBSDProcessList(const char name[]) { assert(name != NULL); kinfo_proc *result = NULL; size_t count = 0; bool ret = 0; if (GetBSDProcessList(&result, &count) == 0) { for (size_t i = 0; i < count; i++) { char *p_comm = result[i].kp_proc.p_comm; // pid_t p_pid = result[i].kp_proc.p_pid; // printf("%d %s (%ld/%ld)\n", p_pid, p_comm, i, count); if (strcmp(p_comm, name) == 0) { ret = 1; break; } } free(result); } return ret; }
pid_t grab_parent_of_pid(pid_t pid) { #ifdef OS_OSX size_t cnt; struct kinfo_proc *procs = 0; int err; pid_t ret = 0; if ( (err=GetBSDProcessList(&procs, &cnt)) ) { fprintf(stderr, "GetBSDProcessList: err %d\n", err); return 0; } if (procs) { int i; for (i = 0; i < (int)cnt; ++i) { if (procs[i].kp_proc.p_pid == pid) { ret = procs[i].kp_eproc.e_ppid; break; } } } free(procs); return ret; #else char statusfile[PATH_MAX+1]; FILE *f; int ret = 0, s; snprintf(statusfile, PATH_MAX, "%s/%d/status", PROCPATH, pid); statusfile[PATH_MAX] = 0; f = fopen(statusfile, "r"); if (!f) { /* silently ignore dead procs.. */ return ret; } s = fscanf(f, "%*s %*s %*s %*s %*s %*s %*s PPid: %d", &ret); if ( s != 1 ) ret = 0; fclose(f); return (pid_t)ret; #endif }
int XBMCHelper::GetProcessPid(const char* strProgram) { kinfo_proc* mylist = 0; size_t mycount = 0; int ret = -1; GetBSDProcessList(&mylist, &mycount); for (size_t k = 0; k < mycount && ret == -1; k++) { kinfo_proc *proc = NULL; proc = &mylist[k]; // Process names are at most sixteen characters long. if (strncmp(proc->kp_proc.p_comm, strProgram, 16) == 0) { ret = proc->kp_proc.p_pid; } } free (mylist); return ret; }
/* returns NULL on major error, or a malloc'd pointer to a zero-terminated pid_t array (which may itself be of length 0) on success */ pid_t *pids_of_exe(const char *exe) { #ifdef OS_OSX size_t cnt, num = 0; struct kinfo_proc *procs = 0; int err; pid_t *ret = 0; if ( (err=GetBSDProcessList(&procs, &cnt)) ) { fprintf(stderr, "GetBSDProcessList: err %d\n", err); return 0; } if (procs) { int i; for (i = 0; i < (int)cnt; ++i) { if (!strcmp(procs[i].kp_proc.p_comm, exe)) { ++num; } } } if (num) { int i,j; ret = malloc(sizeof(*ret)*(num+1)); ret[num] = 0; for (i = 0, j = 0; i < (int)cnt; ++i) { if (!strcmp(procs[i].kp_proc.p_comm,exe)) { ret[j++] = procs[i].kp_proc.p_pid; } } } free(procs); return ret; #else struct dirent **dirs; pid_t *ret, *pcur; int n; CHKPROC; n = scandir(PROCPATH, &dirs, select_numeric_dir, alphasort); /* couldn't scan /proc? hmm.. should never happen */ if (n < 0) { perror("scandir"); return 0; } ret = pcur = (pid_t *)calloc(n+1, sizeof(pid_t)); while(n--) { char buf[PATH_MAX+1], link[PATH_MAX+1]; int sz; snprintf(buf, PATH_MAX, "%s/%s/exe", PROCPATH, dirs[n]->d_name); sz = readlink(buf, link, PATH_MAX); /* sz < 0 ... the process either no longer exists, or is not owned by us so try and check /proc/PID/maps instead.. the first map seems to always be the executable! (this is a hack!) */ if (sz < 0) { char mapfile[PATH_MAX+1], *protect_from_overflow; FILE *f; sz = 0; snprintf(mapfile, PATH_MAX, "%s/%s/maps", PROCPATH, dirs[n]->d_name); mapfile[PATH_MAX] = 0; f = fopen(mapfile, "r"); if (!f) continue; /* ok we give up.. forget this /proc/PID entry! */ /* parse /proc/PID/maps.. sixth column is mapping name */ if (fscanf(f, "%*s %*s %*s %*s %*s %as", &protect_from_overflow) == 1) { strncpy(link, protect_from_overflow, PATH_MAX); sz = PATH_MAX; /* so code below don't break... */ free(protect_from_overflow); } fclose(f); } link[sz] = 0; /* add null.. damned readlink() */ /* At this point, link is either /proc/PID/exe's destination OR the first mapping entry in /proc/PID/maps! If link matches the requested executable, append PID to the pid_t return list... */ if (!strncmp(link, exe, PATH_MAX)) { *pcur = strtol(dirs[n]->d_name, 0, 10); /* we know it's numeric.. */ pcur++; } } *pcur = 0; free(dirs); return ret; #endif }
static int FindProcessByName(const char *processName, pid_t *pid) // Find the process that best matches processName and return // its PID. It first tries to find an exact match; if that fails // it tries to find a substring match; if that fails it checks // whether processName is a number and returns that as the PID. // // On entry, processName must not be NULL, and it must not be the // empty string. pid must not be NULL. // On success, *pid will be the process ID of the found process. // On error, *pid is undefined. { int err; int foundCount; kinfo_proc * processList; size_t processCount; size_t processIndex; assert(processName != NULL); assert(processName[0] != 0); // needed for strstr to behave assert(pid != NULL); processList = NULL; foundCount = 0; // Get the list of all processes. err = GetBSDProcessList(&processList, &processCount); if (err == 0) { // Search for an exact match. for (processIndex = 0; processIndex < processCount; processIndex++) { if ( strcmp(processList[processIndex].kp_proc.p_comm, processName) == 0 ) { *pid = processList[processIndex].kp_proc.p_pid; foundCount = 1; break; } } // If that failed, search for a substring match. if (foundCount == 0) { for (processIndex = 0; processIndex < processCount; processIndex++) { if ( strstr(processList[processIndex].kp_proc.p_comm, processName) != NULL ) { *pid = processList[processIndex].kp_proc.p_pid; foundCount += 1; } } } // If we found more than 1, that's ambiguous and we error out. if (foundCount > 1) { fprintf(stderr, "%s: '%s' does not denote a unique process.\n", gProgramName, processName); err = EINVAL; } } // If still not found, try processName as a PID. if ( (err == 0) && (foundCount == 0) ) { char * firstInvalid; *pid = (pid_t) strtol(processName, &firstInvalid, 10); if ( (processName[0] == 0) || (*firstInvalid != 0) ) { err = EINVAL; } } free(processList); return err; }
int main(int argv,char *args[]){ while(1){ int r=getinput(); if(r==R_CODE_EXIT) break; else if(r==R_CODE_INFO) printf("-help info:\n"HELP); else if(r==R_CODE_PS||r==R_CODE_AT){ GetBSDProcessList(); int i = 0; for (i = 0; i < gprocCount; i++) { kinfo_proc *pro = (gprocList + i); if(r==R_CODE_PS){ printf("%d pid:%d name:%s user_stack:%p\n", i, pro->kp_proc.p_pid, pro->kp_proc.p_comm, pro->kp_proc.user_stack); }else{ pid_t targetpid = pro->kp_proc.p_pid; int num=-1; MioGetArg2Num(1,&num); if(num==targetpid){ kern_return_t kr=task_for_pid(current_task(), targetpid, >ask); if(kr==KERN_SUCCESS){ printf("[attach proccess %s %d]\n",pro->kp_proc.p_comm,num); gproc=pro; }else{ printf("task_for_pid fail %d pid:%d\n",kr,num); gproc=NULL; } break; } } } }else if(r==R_CODE_SUS){ kern_return_t kr = task_suspend(gtask); if(kr==KERN_SUCCESS){ printf("[suspend]\n"); }else{ printf("task_suspend fail %d\n",kr); } }else if(r==R_CODE_RES){ kern_return_t kr = task_resume(gtask); if(kr==KERN_SUCCESS){ printf("[resume]\n"); }else{ printf("task_resume fail %d\n",kr); } }else if(r==R_CODE_SSI){ int num=-1; if(MioGetArg2Num(1,&num)!=0){ printf("arg error"); continue; } findmemoryspace(); int i=0; int index=0; for(i=0;i<gspace_count;i++){ space *target_space=gspaces+i; vm_address_t target_add=target_space->address; vm_address_t end_add=target_space->address+target_space->size; printf("start search %d from %p to %p of %dK space.\n",num,target_add,end_add,target_space->size/1024); do{ int *buf; uint32_t sz; kern_return_t kr=vm_read(gtask,target_add,sizeof(int),&buf,&sz); if(kr!=KERN_SUCCESS){ printf("error %d\n",kr); } if((*buf)==num){ if(index<MAX_ADDS){ printf("find the var at %p=%lu\n",target_add,target_add); gadds[index]=target_add; index++; }else{ printf("gadds over flow\n"); } } target_add=target_add+sizeof(int); }while(target_add<end_add); printf("there are %d vars\n",index); gadds[index]=0; } //end of start search int }else if(r==R_CODE_CSI){ int num=-1; if(MioGetArg2Num(1,&num)!=0){ printf("arg error"); continue; } char *add=NULL; int index=0; while((add=gadds[index])!=0){ int *buf; uint32_t sz; kern_return_t kr=vm_read(gtask,add,sizeof(int),&buf,&sz); if(kr!=KERN_SUCCESS){ printf("error %d\n",kr); break; } if((*buf)==num){ printf("still find the var at %p=%lu\n",add,add); int t=0; char *tadd=NULL; while(1){ tadd=gadds[t]; if(tadd=-1){ gadds[t]=add; break; }else{ continue; } } index++; }else{ gadds[index]=0; index++; } } gadds[index]=0; }else if(r==R_CODE_MOD){ char *add=-1; if(MioGetArg2Long(1,&add)!=0){ printf("address arg error"); continue; } int num=-1; if(MioGetArg2Num(2,&num)!=0){ printf("change to arg error"); continue; } printf("mod %p to %d\n",add,num); kern_return_t kr=vm_write(gtask,add,(vm_offset_t)&num,sizeof(int)); if(kr==KERN_SUCCESS){ printf("OK!\n"); }else{ printf("vm_write fail %d\n",kr); } } } return 0; }