pstree.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /* This file is part of ToaruOS and is released under the terms
  2. * of the NCSA / University of Illinois License - see LICENSE.md
  3. * Copyright (C) 2014 Kevin Lange
  4. */
  5. /*
  6. * pstree
  7. *
  8. * Prints running processes as a tree of
  9. */
  10. #include <sys/stat.h>
  11. #include <fcntl.h>
  12. #include <stdint.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <syscall.h>
  17. #include <unistd.h>
  18. #include <dirent.h>
  19. #include <toaru/list.h>
  20. #include <toaru/tree.h>
  21. typedef struct process {
  22. int pid;
  23. int ppid;
  24. int tgid;
  25. char name[100];
  26. char path[200];
  27. } p_t;
  28. #define LINE_LEN 4096
  29. p_t * build_entry(struct dirent * dent) {
  30. char tmp[256], buf[4096];
  31. FILE * f;
  32. int read = 1;
  33. char line[LINE_LEN];
  34. int pid, uid;
  35. sprintf(tmp, "/proc/%s/status", dent->d_name);
  36. f = fopen(tmp, "r");
  37. p_t * proc = malloc(sizeof(p_t));
  38. while (fgets(line, LINE_LEN, f) != NULL) {
  39. char * n = strstr(line,"\n");
  40. if (n) { *n = '\0'; }
  41. char * tab = strstr(line,"\t");
  42. if (tab) {
  43. *tab = '\0';
  44. tab++;
  45. }
  46. if (strstr(line, "Pid:") == line) {
  47. proc->pid = atoi(tab);
  48. } else if (strstr(line, "PPid:") == line) {
  49. proc->ppid = atoi(tab);
  50. } else if (strstr(line, "Tgid:") == line) {
  51. proc->tgid = atoi(tab);
  52. } else if (strstr(line, "Name:") == line) {
  53. strcpy(proc->name, tab);
  54. } else if (strstr(line, "Path:") == line) {
  55. strcpy(proc->path, tab);
  56. }
  57. }
  58. if (strstr(proc->name,"python") == proc->name) {
  59. char * name = proc->path + strlen(proc->path) - 1;
  60. while (1) {
  61. if (*name == '/') {
  62. name++;
  63. break;
  64. }
  65. if (name == proc->name) break;
  66. name--;
  67. }
  68. memcpy(proc->name, name, strlen(name)+1);
  69. }
  70. if (proc->tgid != proc->pid) {
  71. char tmp[100] = {0};
  72. sprintf(tmp, "{%s}", proc->name);
  73. memcpy(proc->name, tmp, strlen(tmp)+1);
  74. }
  75. fclose(f);
  76. return proc;
  77. }
  78. uint8_t find_pid(void * proc_v, void * pid_v) {
  79. p_t * p = proc_v;
  80. pid_t i = (pid_t)pid_v;
  81. return (uint8_t)(p->pid == i);
  82. }
  83. void print_process_tree_node(tree_node_t * node, size_t depth, int indented, int more, char lines[]) {
  84. p_t * proc = node->value;
  85. for (int i = 0; i < strlen(proc->name)+3; ++i) {
  86. lines[depth+i] = 0;
  87. }
  88. if (!indented && depth) {
  89. if (more) {
  90. printf("─┬─");
  91. lines[depth+1] = 1;
  92. } else {
  93. printf("───");
  94. }
  95. depth += 3;
  96. } else if (depth) {
  97. for (int i = 0; i < depth; ++i) {
  98. if (lines[i]) {
  99. printf("│");
  100. } else {
  101. printf(" ");
  102. }
  103. }
  104. if (more) {
  105. printf(" ├─");
  106. lines[depth+1] = 1;
  107. } else {
  108. printf(" └─");
  109. }
  110. depth += 3;
  111. }
  112. printf(proc->name);
  113. if (!node->children->length) {
  114. printf("\n");
  115. } else {
  116. depth += strlen(proc->name);
  117. int t = 0;
  118. foreach(child, node->children) {
  119. /* Recursively print the children */
  120. print_process_tree_node(child->value, depth, !!(t++), ((t+1)!=node->children->length), lines);
  121. }
  122. }
  123. for (int i = 0; i < strlen(proc->name)+3; ++i) {
  124. lines[depth+i] = 0;
  125. }
  126. }
  127. int main (int argc, char * argv[]) {
  128. /* Open the directory */
  129. DIR * dirp = opendir("/proc");
  130. /* Read the entries in the directory */
  131. tree_t * procs = tree_create();
  132. struct dirent * ent = readdir(dirp);
  133. while (ent != NULL) {
  134. if (ent->d_name[0] >= '0' && ent->d_name[0] <= '9') {
  135. p_t * proc = build_entry(ent);
  136. if (proc->ppid == 0 && proc->pid == 1) {
  137. tree_set_root(procs, proc);
  138. } else {
  139. tree_node_t * parent = tree_find(procs,(void *)proc->ppid,find_pid);
  140. if (parent) {
  141. tree_node_insert_child(procs, parent, proc);
  142. }
  143. }
  144. }
  145. ent = readdir(dirp);
  146. }
  147. closedir(dirp);
  148. char lines[500] = {0};
  149. print_process_tree_node(procs->root, 0, 0, 0, lines);
  150. return 0;
  151. }
  152. /*
  153. * vim: tabstop=4
  154. * vim: shiftwidth=4
  155. * vim: noexpandtab
  156. */