Changes for kernel and Busybox
[tomato.git] / release / src / router / busybox / procps / pstree.c
blob8ba30795dc58a0eda26945029cfc87da597964f5
1 /*
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"
15 //config: default y
16 //config: help
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"
27 //usage: "\n -p Show pids"
29 #include "libbb.h"
31 #define PROC_BASE "/proc"
33 #define OPT_PID (1 << 0)
35 struct child;
37 typedef struct proc {
38 char comm[COMM_LEN + 1];
39 // char flags; - unused, delete?
40 pid_t pid;
41 uid_t uid;
42 struct child *children;
43 struct proc *parent;
44 struct proc *next;
45 } PROC;
47 /* For flags above */
48 //#define PFLAG_THREAD 0x01
50 typedef struct child {
51 PROC *child;
52 struct child *next;
53 } CHILD;
55 #define empty_2 " "
56 #define branch_2 "|-"
57 #define vert_2 "| "
58 #define last_2 "`-"
59 #define single_3 "---"
60 #define first_3 "-+-"
62 struct globals {
63 /* 0-based. IOW: the number of chars we printed on current line */
64 unsigned cur_x;
65 unsigned output_width;
67 /* The buffers will be dynamically increased in size as needed */
68 unsigned capacity;
69 unsigned *width;
70 uint8_t *more;
72 PROC *list;
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))); \
79 } while (0)
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 to this function.
88 static void ensure_buffer_capacity(int bufindex)
90 if (bufindex >= G.capacity) {
91 G.capacity += 0x100;
92 G.width = xrealloc(G.width, G.capacity * sizeof(G.width[0]));
93 G.more = xrealloc(G.more, G.capacity * sizeof(G.more[0]));
97 /* NB: this function is never called with "bad" chars
98 * (control chars or chars >= 0x7f)
100 static void out_char(char c)
102 G.cur_x++;
103 if (G.cur_x > G.output_width)
104 return;
105 if (G.cur_x == G.output_width)
106 c = '+';
107 putchar(c);
110 /* NB: this function is never called with "bad" chars
111 * (control chars or chars >= 0x7f)
113 static void out_string(const char *str)
115 while (*str)
116 out_char(*str++);
119 static void out_newline(void)
121 putchar('\n');
122 G.cur_x = 0;
125 static PROC *find_proc(pid_t pid)
127 PROC *walk;
129 for (walk = G.list; walk; walk = walk->next)
130 if (walk->pid == pid)
131 break;
133 return walk;
136 static PROC *new_proc(const char *comm, pid_t pid, uid_t uid)
138 PROC *new = xzalloc(sizeof(*new));
140 strcpy(new->comm, comm);
141 new->pid = pid;
142 new->uid = uid;
143 new->next = G.list;
145 G.list = new;
146 return G.list;
149 static void add_child(PROC *parent, PROC *child)
151 CHILD *new, **walk;
152 int cmp;
154 new = xmalloc(sizeof(*new));
156 new->child = child;
157 for (walk = &parent->children; *walk; walk = &(*walk)->next) {
158 cmp = strcmp((*walk)->child->comm, child->comm);
159 if (cmp > 0)
160 break;
161 if (cmp == 0 && (*walk)->child->uid > child->uid)
162 break;
164 new->next = *walk;
165 *walk = new;
168 static void add_proc(const char *comm, pid_t pid, pid_t ppid,
169 uid_t uid /*, char isthread*/)
171 PROC *this, *parent;
173 this = find_proc(pid);
174 if (!this)
175 this = new_proc(comm, pid, uid);
176 else {
177 strcpy(this->comm, comm);
178 this->uid = uid;
181 if (pid == ppid)
182 ppid = 0;
183 // if (isthread)
184 // this->flags |= PFLAG_THREAD;
186 parent = find_proc(ppid);
187 if (!parent)
188 parent = new_proc("?", ppid, 0);
190 add_child(parent, this);
191 this->parent = parent;
194 static int tree_equal(const PROC *a, const PROC *b)
196 const CHILD *walk_a, *walk_b;
198 if (strcmp(a->comm, b->comm) != 0)
199 return 0;
200 if ((option_mask32 /*& OPT_PID*/) && a->pid != b->pid)
201 return 0;
203 for (walk_a = a->children, walk_b = b->children;
204 walk_a && walk_b;
205 walk_a = walk_a->next, walk_b = walk_b->next
207 if (!tree_equal(walk_a->child, walk_b->child))
208 return 0;
211 return !(walk_a || walk_b);
214 static int out_args(const char *mystr)
216 const char *here;
217 int strcount = 0;
218 char tmpstr[5];
220 for (here = mystr; *here; here++) {
221 if (*here == '\\') {
222 out_string("\\\\");
223 strcount += 2;
224 } else if (*here >= ' ' && *here < 0x7f) {
225 out_char(*here);
226 strcount++;
227 } else {
228 sprintf(tmpstr, "\\%03o", (unsigned char) *here);
229 out_string(tmpstr);
230 strcount += 4;
234 return strcount;
237 static void
238 dump_tree(PROC *current, int level, int rep, int leaf, int last, int closing)
240 CHILD *walk, *next, **scan;
241 int lvl, i, add, offset, count, comm_len, first;
242 char tmp[sizeof(int)*3 + 4];
244 if (!current)
245 return;
247 if (!leaf) {
248 for (lvl = 0; lvl < level; lvl++) {
249 i = G.width[lvl] + 1;
250 while (--i >= 0)
251 out_char(' ');
253 if (lvl == level - 1) {
254 if (last) {
255 out_string(last_2);
256 } else {
257 out_string(branch_2);
259 } else {
260 if (G.more[lvl + 1]) {
261 out_string(vert_2);
262 } else {
263 out_string(empty_2);
269 add = 0;
270 if (rep > 1) {
271 add += sprintf(tmp, "%d*[", rep);
272 out_string(tmp);
274 comm_len = out_args(current->comm);
275 if (option_mask32 /*& OPT_PID*/) {
276 comm_len += sprintf(tmp, "(%d)", (int)current->pid);
277 out_string(tmp);
279 offset = G.cur_x;
281 if (!current->children) {
282 while (closing--)
283 out_char(']');
284 out_newline();
286 ensure_buffer_capacity(level);
287 G.more[level] = !last;
289 G.width[level] = comm_len + G.cur_x - offset + add;
290 if (G.cur_x >= G.output_width) {
291 //out_string(first_3); - why? it won't print anything
292 //out_char('+');
293 out_newline();
294 return;
297 first = 1;
298 for (walk = current->children; walk; walk = next) {
299 count = 0;
300 next = walk->next;
301 scan = &walk->next;
302 while (*scan) {
303 if (!tree_equal(walk->child, (*scan)->child))
304 scan = &(*scan)->next;
305 else {
306 if (next == *scan)
307 next = (*scan)->next;
308 count++;
309 *scan = (*scan)->next;
312 if (first) {
313 out_string(next ? first_3 : single_3);
314 first = 0;
317 dump_tree(walk->child, level + 1, count + 1,
318 walk == current->children, !next,
319 closing + (count ? 1 : 0));
323 static void dump_by_user(PROC *current, uid_t uid)
325 const CHILD *walk;
327 if (!current)
328 return;
330 if (current->uid == uid) {
331 if (G.dumped)
332 putchar('\n');
333 dump_tree(current, 0, 1, 1, 1, 0);
334 G.dumped = 1;
335 return;
337 for (walk = current->children; walk; walk = walk->next)
338 dump_by_user(walk->child, uid);
341 #if ENABLE_FEATURE_SHOW_THREADS
342 static void handle_thread(const char *comm, pid_t pid, pid_t ppid, uid_t uid)
344 char threadname[COMM_LEN + 2];
345 sprintf(threadname, "{%.*s}", COMM_LEN - 2, comm);
346 add_proc(threadname, pid, ppid, uid/*, 1*/);
348 #endif
350 static void mread_proc(void)
352 procps_status_t *p = NULL;
353 pid_t parent = 0;
354 int flags = PSSCAN_COMM | PSSCAN_PID | PSSCAN_PPID | PSSCAN_UIDGID | PSSCAN_TASKS;
356 while ((p = procps_scan(p, flags)) != NULL) {
357 #if ENABLE_FEATURE_SHOW_THREADS
358 if (p->pid != p->main_thread_pid)
359 handle_thread(p->comm, p->pid, parent, p->uid);
360 else
361 #endif
363 add_proc(p->comm, p->pid, p->ppid, p->uid/*, 0*/);
364 parent = p->pid;
369 int pstree_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
370 int pstree_main(int argc UNUSED_PARAM, char **argv)
372 pid_t pid = 1;
373 long uid = 0;
375 INIT_G();
377 get_terminal_width_height(0, &G.output_width, NULL);
379 opt_complementary = "?1";
380 getopt32(argv, "p");
381 argv += optind;
383 if (argv[0]) {
384 if (argv[0][0] >= '0' && argv[0][0] <= '9') {
385 pid = xatoi(argv[0]);
386 } else {
387 uid = xuname2uid(argv[0]);
391 mread_proc();
393 if (!uid)
394 dump_tree(find_proc(pid), 0, 1, 1, 1, 0);
395 else {
396 dump_by_user(find_proc(1), uid);
397 if (!G.dumped) {
398 bb_error_msg_and_die("no processes found");
402 if (ENABLE_FEATURE_CLEAN_UP) {
403 free(G.width);
404 free(G.more);
406 return 0;