int main(int argc, char const *argv[]) { int target_pid = atoi(argv[1]); int counter = 1; DIR *proc = opendir("/proc"); struct dirent *entry; // For every directory in proc while (entry = readdir(proc)) { if (entry->d_type == DT_DIR) { long int pid = strtol(entry->d_name, NULL, 10); if (pid != 0) { int ppid = get_ppid(pid); if (ppid == target_pid ) counter++; } } } printf("%d\n", counter); return 0; }
//Function to generate 1 byte selector id for end point selection //The End Point Selection is an optional protocol before SGX EPID Provisioning // to get the server address and expired date(of the server) for SGX EPID Provisioning // a one byte selector id is required for each machine which never changes for any machine // First byte of PPID is used currently as the selector id static pve_status_t gen_es_selector_id(uint8_t *selector_id) { ppid_t ppid; memset(&ppid, 0, sizeof(ppid)); pve_status_t ret = get_ppid(&ppid); if(ret != PVEC_SUCCESS) return ret; *selector_id = ppid.ppid[0]; (void)memset_s(&ppid, sizeof(ppid), 0, sizeof(ppid));//clear the PPID in stack return PVEC_SUCCESS; }
int main(){ int i; printf("simplon (%i): empieza\n", get_pid()); if (crear_proceso("simplon")<0) printf("Error creando fill simplon\n"); for (i=0; i<TOT_ITER; i++){ printf("Soy simplon (%i), mi padres es %i \n", get_pid(), get_ppid()); } printf("simplon (%i): termina\n", get_pid()); return 0; }
int main(){ int i; printf("simplon: empieza\n"); //fijar_prio(11); if (crear_proceso("simplon")<0) printf("Error creando simplon\n"); printf("Soy simplon, mi padre es %i", get_ppid()); for (i=0; i<TOT_ITER; i++){ if(i == TOT_ITER/2){ printf("simplon: MITAD\n"); //dormir(2); } } printf("simplon: termina\n"); return 0; }
Process *get_process(pid_t pid) { /* TODO: Add test for invalid pid. Right now, we get a lot of errors and some * structure.*/ Process *retval = (Process *)calloc(1, sizeof(Process)); unsigned int *uids = NULL; unsigned int *gids = NULL; retval->pid = pid; retval->ppid = get_ppid(pid); retval->name = get_procname(pid); retval->exe = get_exe(pid); retval->cmdline = get_cmdline(pid); retval->create_time = get_create_time(pid); uids = get_ids(pid, "Uid:"); if (uids) { retval->uid = uids[0]; retval->euid = uids[1]; retval->suid = uids[2]; retval->username = get_username(retval->uid); /* Uses real uid and not euid */ } else { retval->uid = retval->euid = retval->suid = 0; retval->username = NULL; } gids = get_ids(pid, "Gid:"); if (uids) { retval->gid = gids[0]; retval->egid = gids[1]; retval->sgid = gids[2]; } else { retval->uid = retval->euid = retval->suid = 0; } retval->terminal = get_terminal(pid); free(uids); free(gids); return retval; }
static int pid2exe(pid_t pid, char *buf, size_t len) { pid_t ppid; FILE *f; char path[PATH_MAX]; char *p, *q; int st = -1; if (buf && len > 0) { ppid = get_ppid(pid); snprintf(path, sizeof(path), "/proc/%u/cmdline", ppid); if ((f = fopen(path, "r"))) { if (fgets(buf, (int)len-1, f)) { if ((p = strchr(buf, ' '))) *p = '\0'; else if ((p = strchr(buf, '\n'))) *p = '\0'; else p = buf + strlen(buf); if ((q = strrchr(buf, '/'))) memmove(buf, q+1, (size_t)(p-q)); st = 0; } fclose(f); } } if (st < 0) pa_log("pid2exe(%u) failed", pid); else pa_log_debug("pid2exe(%u) => exe %s", pid, buf); return st; }
Process * get_process(unsigned pid) { Process *retval = calloc(1, sizeof(Process)); unsigned int *uids = NULL; unsigned int *gids = NULL; retval->pid = pid; retval->ppid = get_ppid(pid); retval->name = get_procname(pid); retval->exe = get_exe(pid); retval->cmdline = get_cmdline(pid); retval->create_time = get_create_time(pid); uids = get_ids(pid, "Uid:"); if (uids) { retval->uid = uids[0]; retval->euid = uids[1]; retval->suid = uids[2]; retval->username = get_username(retval->uid); /* Uses real uid and not euid */ } else { retval->uid = retval->euid = retval->suid = 0; retval->username = NULL; } gids = get_ids(pid, "Gid:"); if (uids) { retval->gid = gids[0]; retval->egid = gids[1]; retval->sgid = gids[2]; } else { retval->uid = retval->euid = retval->suid = 0; } retval->terminal = get_terminal(pid); if (uids) free(uids); if (gids) free(gids); return retval; }
XBT_ATTRIB_DEPRECATED_v323("Please use Actor::get_ppid()") aid_t getPpid() { return get_ppid(); }
int ccstree_main(int argc, char *argv[]) { const char *policy_file = proc_policy_process_status; static _Bool show_all = false; int i; for (i = 1; i < argc; i++) { char *ptr = argv[i]; char *cp = strchr(ptr, ':'); if (cp) { *cp++ = '\0'; if (network_mode) goto usage; network_ip = inet_addr(ptr); network_port = htons(atoi(cp)); network_mode = true; if (!check_remote_host()) return 1; } else if (!strcmp(ptr, "-a")) { show_all = true; } else { usage: fprintf(stderr, "Usage: %s " "[-a] [remote_ip:remote_port]\n", argv[0]); return 0; } } if (network_mode) { FILE *fp = open_write(show_all ? "proc:all_process_status" : "proc:process_status"); if (!fp) { fprintf(stderr, "Can't connect.\n"); return 1; } get(); while (freadline(fp)) { unsigned int pid = 0; unsigned int ppid = 0; int profile = -1; char *name; char *domain; sscanf(shared_buffer, "PID=%u PPID=%u", &pid, &ppid); name = strstr(shared_buffer, "NAME="); if (name) name = strdup(name + 5); if (!name) name = "<UNKNOWN>"; if (!freadline(fp)) break; sscanf(shared_buffer, "%u %u", &pid, &profile); domain = strchr(shared_buffer, '<'); if (domain) domain = strdup(domain); if (!domain) domain = "<UNKNOWN>"; task_list = realloc(task_list, (task_list_len + 1) * sizeof(struct task_entry)); if (!task_list) out_of_memory(); task_list[task_list_len].pid = pid; task_list[task_list_len].ppid = ppid; task_list[task_list_len].profile = profile; task_list[task_list_len].name = name; task_list[task_list_len].domain = domain; task_list[task_list_len].done = false; task_list_len++; } put(); fclose(fp); } else { struct dirent **namelist; int i; int n; int status_fd; if (access(proc_policy_dir, F_OK)) { fprintf(stderr, "You can't use this command " "for this kernel.\n"); return 1; } status_fd = open(policy_file, O_RDWR); if (status_fd == EOF) { fprintf(stderr, "Can't open %s\n", policy_file); return 1; } n = scandir("/proc/", &namelist, 0, 0); for (i = 0; i < n; i++) { char *name; char *domain; int profile = -1; unsigned int pid = 0; char buffer[128]; char test[16]; if (sscanf(namelist[i]->d_name, "%u", &pid) != 1) goto skip; memset(buffer, 0, sizeof(buffer)); snprintf(buffer, sizeof(buffer) - 1, "/proc/%u/exe", pid); if (!show_all && readlink(buffer, test, sizeof(test)) <= 0) goto skip; name = get_name(pid); if (!name) name = "<UNKNOWN>"; snprintf(buffer, sizeof(buffer) - 1, "%u\n", pid); write(status_fd, buffer, strlen(buffer)); get(); memset(shared_buffer, 0, sizeof(shared_buffer)); read(status_fd, shared_buffer, sizeof(shared_buffer) - 1); sscanf(shared_buffer, "%u %u", &pid, &profile); domain = strchr(shared_buffer, '<'); if (domain) domain = strdup(domain); if (!domain) domain = "<UNKNOWN>"; put(); task_list = realloc(task_list, (task_list_len + 1) * sizeof(struct task_entry)); if (!task_list) out_of_memory(); task_list[task_list_len].pid = pid; task_list[task_list_len].ppid = get_ppid(pid); task_list[task_list_len].profile = profile; task_list[task_list_len].name = name; task_list[task_list_len].domain = domain; task_list[task_list_len].done = false; task_list_len++; skip: free((void *) namelist[i]); } if (n >= 0) free((void *) namelist); close(status_fd); } dump(1, 0); dump_unprocessed(); return 0; }