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.
12 //config:config PSTREE
13 //config: bool "pstree (9.4 kb)"
16 //config: Display a tree of processes.
18 //applet:IF_PSTREE(APPLET_NOEXEC(pstree, pstree, BB_DIR_USR_BIN, BB_SUID_DROP, pstree))
20 //kbuild:lib-$(CONFIG_PSTREE) += pstree.o
22 //usage:#define pstree_trivial_usage
23 //usage: "[-p] [PID|USER]"
24 //usage:#define pstree_full_usage "\n\n"
25 //usage: "Display process tree, optionally start from USER or PID\n"
26 //usage: "\n -p Show pids"
30 #define PROC_BASE "/proc"
32 #define OPT_PID (1 << 0)
36 #if ENABLE_FEATURE_SHOW_THREADS
37 /* For threads, we add {...} around the comm, so we need two extra bytes */
38 # define COMM_DISP_LEN (COMM_LEN + 2)
40 # define COMM_DISP_LEN COMM_LEN
44 char comm[COMM_DISP_LEN + 1];
45 // char flags; - unused, delete?
48 struct child *children;
54 //#define PFLAG_THREAD 0x01
56 typedef struct child {
65 #define single_3 "---"
69 /* 0-based. IOW: the number of chars we printed on current line */
71 unsigned output_width;
73 /* The buffers will be dynamically increased in size as needed */
80 smallint dumped; /* used by dump_by_user */
82 #define G (*ptr_to_globals)
83 #define INIT_G() do { \
84 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
89 * Allocates additional buffer space for width and more as needed.
90 * The first call will allocate the first buffer.
92 * bufindex the index that will be used after the call to this function.
94 static void ensure_buffer_capacity(int bufindex)
96 if (bufindex >= G.capacity) {
98 G.width = xrealloc(G.width, G.capacity * sizeof(G.width[0]));
99 G.more = xrealloc(G.more, G.capacity * sizeof(G.more[0]));
103 /* NB: this function is never called with "bad" chars
104 * (control chars or chars >= 0x7f)
106 static void out_char(char c)
109 if (G.cur_x > G.output_width)
111 if (G.cur_x == G.output_width)
116 /* NB: this function is never called with "bad" chars
117 * (control chars or chars >= 0x7f)
119 static void out_string(const char *str)
125 static void out_newline(void)
131 static PROC *find_proc(pid_t pid)
135 for (walk = G.list; walk; walk = walk->next)
136 if (walk->pid == pid)
142 static PROC *new_proc(const char *comm, pid_t pid, uid_t uid)
144 PROC *new = xzalloc(sizeof(*new));
146 strcpy(new->comm, comm);
155 static void add_child(PROC *parent, PROC *child)
160 new = xmalloc(sizeof(*new));
163 for (walk = &parent->children; *walk; walk = &(*walk)->next) {
164 cmp = strcmp((*walk)->child->comm, child->comm);
167 if (cmp == 0 && (*walk)->child->uid > child->uid)
174 static void add_proc(const char *comm, pid_t pid, pid_t ppid,
175 uid_t uid /*, char isthread*/)
179 this = find_proc(pid);
181 this = new_proc(comm, pid, uid);
183 strcpy(this->comm, comm);
190 // this->flags |= PFLAG_THREAD;
192 parent = find_proc(ppid);
194 parent = new_proc("?", ppid, 0);
196 add_child(parent, this);
197 this->parent = parent;
200 static int tree_equal(const PROC *a, const PROC *b)
202 const CHILD *walk_a, *walk_b;
204 if (strcmp(a->comm, b->comm) != 0)
206 if ((option_mask32 /*& OPT_PID*/) && a->pid != b->pid)
209 for (walk_a = a->children, walk_b = b->children;
211 walk_a = walk_a->next, walk_b = walk_b->next
213 if (!tree_equal(walk_a->child, walk_b->child))
217 return !(walk_a || walk_b);
220 static int out_args(const char *mystr)
226 for (here = mystr; *here; here++) {
230 } else if (*here >= ' ' && *here < 0x7f) {
234 sprintf(tmpstr, "\\%03o", (unsigned char) *here);
244 dump_tree(PROC *current, int level, int rep, int leaf, int last, int closing)
246 CHILD *walk, *next, **scan;
247 int lvl, i, add, offset, count, comm_len, first;
248 char tmp[sizeof(int)*3 + 4];
254 for (lvl = 0; lvl < level; lvl++) {
255 i = G.width[lvl] + 1;
259 if (lvl == level - 1) {
263 out_string(branch_2);
266 if (G.more[lvl + 1]) {
277 add += sprintf(tmp, "%d*[", rep);
280 comm_len = out_args(current->comm);
281 if (option_mask32 /*& OPT_PID*/) {
282 comm_len += sprintf(tmp, "(%d)", (int)current->pid);
287 if (!current->children) {
292 ensure_buffer_capacity(level);
293 G.more[level] = !last;
295 G.width[level] = comm_len + G.cur_x - offset + add;
296 if (G.cur_x >= G.output_width) {
297 //out_string(first_3); - why? it won't print anything
304 for (walk = current->children; walk; walk = next) {
309 if (!tree_equal(walk->child, (*scan)->child))
310 scan = &(*scan)->next;
313 next = (*scan)->next;
315 *scan = (*scan)->next;
319 out_string(next ? first_3 : single_3);
323 dump_tree(walk->child, level + 1, count + 1,
324 walk == current->children, !next,
325 closing + (count ? 1 : 0));
329 static void dump_by_user(PROC *current, uid_t uid)
336 if (current->uid == uid) {
339 dump_tree(current, 0, 1, 1, 1, 0);
343 for (walk = current->children; walk; walk = walk->next)
344 dump_by_user(walk->child, uid);
347 #if ENABLE_FEATURE_SHOW_THREADS
348 static void handle_thread(const char *comm, pid_t pid, pid_t ppid, uid_t uid)
350 char threadname[COMM_DISP_LEN + 1];
351 sprintf(threadname, "{%.*s}", (int)sizeof(threadname) - 3, comm);
352 add_proc(threadname, pid, ppid, uid/*, 1*/);
356 static void mread_proc(void)
358 procps_status_t *p = NULL;
359 #if ENABLE_FEATURE_SHOW_THREADS
362 int flags = PSSCAN_COMM | PSSCAN_PID | PSSCAN_PPID | PSSCAN_UIDGID | PSSCAN_TASKS;
364 while ((p = procps_scan(p, flags)) != NULL) {
365 #if ENABLE_FEATURE_SHOW_THREADS
366 if (p->pid != p->main_thread_pid)
367 handle_thread(p->comm, p->pid, parent, p->uid);
371 add_proc(p->comm, p->pid, p->ppid, p->uid/*, 0*/);
372 #if ENABLE_FEATURE_SHOW_THREADS
379 int pstree_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
380 int pstree_main(int argc UNUSED_PARAM, char **argv)
387 G.output_width = get_terminal_width(0);
389 opt_complementary = "?1";
394 if (argv[0][0] >= '0' && argv[0][0] <= '9') {
395 pid = xatoi(argv[0]);
397 uid = xuname2uid(argv[0]);
404 dump_tree(find_proc(pid), 0, 1, 1, 1, 0);
406 dump_by_user(find_proc(1), uid);
408 bb_error_msg_and_die("no processes found");
412 if (ENABLE_FEATURE_CLEAN_UP) {