2 * pstree.c - display process tree
4 * Copyright (C) 1993-2002 Werner Almesberger
5 * Copyright (C) 2002-2009 Craig Small
6 * Copyright (C) 2010 Lauri Kasanen
8 * Based on pstree (PSmisc) 22.13.
10 * Licensed under GPLv2, see file LICENSE in this source tree.
13 //config:config PSTREE
14 //config: bool "pstree"
17 //config: Display a tree of processes.
19 //applet:IF_PSTREE(APPLET(pstree, _BB_DIR_USR_BIN, _BB_SUID_DROP))
21 //kbuild:lib-$(CONFIG_PSTREE) += pstree.o
23 //usage:#define pstree_trivial_usage
24 //usage: "[-p] [PID|USER]"
25 //usage:#define pstree_full_usage "\n\n"
26 //usage: "Display process tree, optionally start from USER or PID\n"
28 //usage: "\n -p Show pids"
32 #define PROC_BASE "/proc"
34 #define OPT_PID (1 << 0)
39 char comm[COMM_LEN + 1];
40 // char flags; - unused, delete?
43 struct child *children;
49 //#define PFLAG_THREAD 0x01
51 typedef struct child {
60 #define single_3 "---"
66 /* The buffers will be dynamically increased in size as needed */
71 // Disabled, since code is broken anyway and needs fixing
72 // unsigned output_width;
74 smallint dumped; /* used by dump_by_user */
76 #define G (*ptr_to_globals)
77 #define INIT_G() do { \
78 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
83 * Allocates additional buffer space for width and more as needed.
84 * The first call will allocate the first buffer.
86 * bufindex the index that will be used after the call
89 static void ensure_buffer_capacity(int bufindex)
91 if (bufindex >= G.capacity) {
93 G.width = xrealloc(G.width, G.capacity * sizeof(G.width[0]));
94 G.more = xrealloc(G.more, G.capacity * sizeof(G.more[0]));
98 #if ENABLE_FEATURE_CLEAN_UP
99 static void maybe_free_buffers(void)
105 # define maybe_free_buffers() ((void)0)
108 /* NB: this function is never called with "bad" chars
109 * (control chars or chars >= 0x7f)
111 static void out_char(char c)
114 // if (G.cur_x <= G.output_width)
116 // else if (G.cur_x == G.output_width - 1)
120 /* NB: this function is never called with "bad" chars
121 * (control chars or chars >= 0x7f)
123 static void out_string(const char *str)
129 static void out_newline(void)
135 static PROC *find_proc(pid_t pid)
139 for (walk = G.list; walk; walk = walk->next)
140 if (walk->pid == pid)
146 static PROC *new_proc(const char *comm, pid_t pid, uid_t uid)
148 PROC *new = xzalloc(sizeof(*new));
150 strcpy(new->comm, comm);
159 static void add_child(PROC *parent, PROC *child)
164 new = xmalloc(sizeof(*new));
167 for (walk = &parent->children; *walk; walk = &(*walk)->next) {
168 cmp = strcmp((*walk)->child->comm, child->comm);
171 if (cmp == 0 && (*walk)->child->uid > child->uid)
178 static void add_proc(const char *comm, pid_t pid, pid_t ppid,
179 uid_t uid /*, char isthread*/)
183 this = find_proc(pid);
185 this = new_proc(comm, pid, uid);
187 strcpy(this->comm, comm);
194 // this->flags |= PFLAG_THREAD;
196 parent = find_proc(ppid);
198 parent = new_proc("?", ppid, 0);
200 add_child(parent, this);
201 this->parent = parent;
204 static int tree_equal(const PROC *a, const PROC *b)
206 const CHILD *walk_a, *walk_b;
208 if (strcmp(a->comm, b->comm) != 0)
210 if ((option_mask32 /*& OPT_PID*/) && a->pid != b->pid)
213 for (walk_a = a->children, walk_b = b->children;
215 walk_a = walk_a->next, walk_b = walk_b->next
217 if (!tree_equal(walk_a->child, walk_b->child))
221 return !(walk_a || walk_b);
224 static int out_args(const char *mystr)
230 for (here = mystr; *here; here++) {
234 } else if (*here >= ' ' && *here < 0x7f) {
238 sprintf(tmpstr, "\\%03o", (unsigned char) *here);
248 dump_tree(PROC *current, int level, int rep, int leaf, int last, int closing)
250 CHILD *walk, *next, **scan;
251 int lvl, i, add, offset, count, comm_len, first;
257 for (lvl = 0; lvl < level; lvl++) {
258 i = G.width[lvl] + 1;
262 if (lvl == level - 1) {
266 out_string(branch_2);
269 if (G.more[lvl + 1]) {
281 add = printf("%d", rep) + 2;
284 comm_len = out_args(current->comm);
285 if (option_mask32 /*& OPT_PID*/) {
287 comm_len += printf("%d", (int)current->pid) + 2;
292 if (!current->children) {
297 ensure_buffer_capacity(level);
298 G.more[level] = !last;
300 G.width[level] = comm_len + G.cur_x - offset + add;
301 // if (G.cur_x >= G.output_width) {
302 // out_string(first_3);
309 for (walk = current->children; walk; walk = next) {
314 if (!tree_equal(walk->child, (*scan)->child))
315 scan = &(*scan)->next;
318 next = (*scan)->next;
320 *scan = (*scan)->next;
324 out_string(next ? first_3 : single_3);
328 dump_tree(walk->child, level + 1, count + 1,
329 walk == current->children, !next,
330 closing + (count ? 1 : 0));
334 static void dump_by_user(PROC *current, uid_t uid)
341 if (current->uid == uid) {
344 dump_tree(current, 0, 1, 1, 1, 0);
348 for (walk = current->children; walk; walk = walk->next)
349 dump_by_user(walk->child, uid);
352 static void handle_thread(const char *comm, pid_t pid, pid_t ppid, uid_t uid)
354 char threadname[COMM_LEN + 2];
355 sprintf(threadname, "{%.*s}", COMM_LEN - 2, comm);
356 add_proc(threadname, pid, ppid, uid/*, 1*/);
359 static void mread_proc(void)
361 procps_status_t *p = NULL;
363 int flags = PSSCAN_COMM | PSSCAN_PID | PSSCAN_PPID | PSSCAN_UIDGID | PSSCAN_TASKS;
365 while ((p = procps_scan(p, flags)) != NULL) {
366 #if ENABLE_FEATURE_SHOW_THREADS
367 if (p->pid != p->main_thread_pid)
368 handle_thread(p->comm, p->pid, parent, p->uid);
372 add_proc(p->comm, p->pid, p->ppid, p->uid/*, 0*/);
378 int pstree_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
379 int pstree_main(int argc UNUSED_PARAM, char **argv)
387 // get_terminal_width_height(1, &G.output_width, NULL);
395 if (argv[0][0] >= '0' && argv[0][0] <= '9') {
396 pid = xatoi(argv[0]);
398 uid = xuname2uid(argv[0]);
405 dump_tree(find_proc(pid), 0, 1, 1, 1, 0);
407 dump_by_user(find_proc(1), uid);
409 bb_error_msg_and_die("no processes found");
413 maybe_free_buffers();