1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
| #include <unistd.h> #include <dirent.h> #include <stdio.h> #include <string.h> #include <errno.h> #include <stdlib.h> #include <sys/types.h> #include <unistd.h> #include <limits.h>
int get_pid_max() { FILE *pid_max = fopen("/proc/sys/kernel/pid_max", "r"); int ret = -1; fscanf(pid_max, "%d", &ret); fclose(pid_max); if(ret == -1) { fprintf(stderr, "Error: fail to read /proc/sys/kernel/pid_max\n"); exit(1); } return ret; }
struct process { char *name; char *cmd; pid_t parent; pid_t pid; struct process *children; struct process *sibling; };
char *append(char * str, const char *cat) { int len = strlen(str) + strlen(cat) + 1; char *ret = malloc(len*sizeof(char)); ret[0] = 0; strcat(ret, str); strcat(ret, cat); return ret; }
int max(int a, int b) { return a>b?a:b; }
void prettyPrint(struct process *root, char *preffix, int last) { const char * tab = last ? "└" : "├"; printf("%s│•Name=%s\n", preffix, root->name); printf("%s│ pid=%d\n", preffix, root->pid); printf("%s│ cmd=%s\n", preffix, root->cmd); printf("%s│ ppid=%d\n", preffix, root->parent); printf("%s%s", preffix, tab); struct process *move = root->children; if(move != NULL) { printf("┬"); } else { printf("─"); } int suff = max(max(strlen(root->name)+5, strlen(root->cmd)+4), 10); while(suff) {printf("─"); suff--;} printf("\n"); preffix = append(preffix, (!last ? "│" : " ")); while(move != NULL && move->sibling != NULL) { prettyPrint(move, preffix, 0); move = move->sibling; }
if(move != NULL) prettyPrint(move, preffix, 1); }
int main(int argc, char **argv) { #ifndef DEBUG freopen("/dev/null", "w", stderr); #endif long lim_filename = pathconf("/proc",_PC_NAME_MAX); long lim_argmax = sysconf(_SC_ARG_MAX);
int pid_max = get_pid_max(); struct process **pidlist = (struct process **)malloc(pid_max * sizeof(struct process *)); pid_t *pids = (pid_t *)malloc(pid_max * sizeof(pid_t)); int pidscount = 0; pids[pidscount++] = 0; memset(pidlist, 0, pid_max * sizeof(struct process *)); pidlist[0] = malloc(sizeof(struct process)); memcpy(pidlist[0], &(struct process) { .name=NULL, .cmd=NULL, .parent=-1, .pid=0, .children=NULL, .sibling=NULL }, sizeof(struct process));
char *filename = malloc((14 + lim_filename+1)*sizeof(char)); DIR *proc = opendir("/proc"); struct dirent *proc_rent = NULL; while((proc_rent = readdir(proc)) != NULL) { const char *spid = proc_rent->d_name; char *end; errno = 0; pid_t pid = strtol(spid, &end, 10); if(end == spid || *end != '\0' || errno != 0) { fprintf(stderr, "INFO: %s/%s is not a pid\n", "/proc", spid); continue; } fprintf(stderr, "INFO: current pid = %d\n", pid); pidlist[pid] = malloc(sizeof(struct process)); memcpy(pidlist[pid], &(struct process) { .name=malloc((lim_filename+1)*sizeof(char)), .cmd=malloc((lim_argmax+1)*sizeof(char)), .parent=-1, .pid=pid, .children=NULL, .sibling=NULL }, sizeof(struct process)); pidlist[pid]->name[0] = 0; pidlist[pid]->cmd[0] = 0; sprintf(filename, "/proc/%s/status", spid); FILE *status = fopen(filename, "r"); if(status == NULL) { fprintf(stderr, "ERROR: fail to open %s\n", filename); exit(4); } uid_t realUid = -1; char buffer[1024] = {0}; fscanf(status, "%*s %s\n", pidlist[pid]->name); fscanf(status, "%*[^\n]\n"); fscanf(status, "%*[^\n]\n"); fscanf(status, "%*[^\n]\n"); fscanf(status, "%*[^\n]\n"); fscanf(status, "%*[^\n]\n"); fscanf(status, "%*s %d\n", &pidlist[pid]->parent); if(strcmp(pidlist[pid]->name, "") == 0 || pidlist[pid]->parent == -1) { fprintf(stderr, "ERROR: fail to read name or ppid in %s/%s/status\n", "/proc", spid); exit(2); } sprintf(filename, "/proc/%s/cmdline", spid); FILE *cmdline = fopen(filename, "r"); if(cmdline == NULL) { fprintf(stderr, "ERROR: fail to open %s\n", filename); exit(4); } fscanf(cmdline, "%s", pidlist[pid]->cmd); fclose(cmdline); fclose(status); pids[pidscount++] = pid;
} for(int i = 1; i < pidscount; i++) { pid_t pid = pids[i]; pid_t ppid = pidlist[pid]->parent; pidlist[pid]->sibling = pidlist[ppid]->children; pidlist[ppid]->children = pidlist[pid]; } closedir(proc); prettyPrint(pidlist[1], memset(malloc(sizeof(char)), 0, sizeof(char)), 1);
for(int i = 0; i < pidscount; i++) { free(pidlist[pids[i]]->name); free(pidlist[pids[i]]->cmd); free(pidlist[pids[i]]); } free(filename); free(pids); free(pidlist); return 0; }
|