mass removal of underscores from _BB_DIR_foo and _BB_SUID_foo
[platform/upstream/busybox.git] / procps / pstree.c
1 /*
2  * pstree.c - display process tree
3  *
4  * Copyright (C) 1993-2002 Werner Almesberger
5  * Copyright (C) 2002-2009 Craig Small
6  * Copyright (C) 2010 Lauri Kasanen
7  *
8  * Based on pstree (PSmisc) 22.13.
9  *
10  * Licensed under GPLv2, see file LICENSE in this source tree.
11  */
12
13 //config:config PSTREE
14 //config:       bool "pstree"
15 //config:       default y
16 //config:       help
17 //config:         Display a tree of processes.
18
19 //applet:IF_PSTREE(APPLET(pstree, BB_DIR_USR_BIN, BB_SUID_DROP))
20
21 //kbuild:lib-$(CONFIG_PSTREE) += pstree.o
22
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:     "\nOptions:"
28 //usage:     "\n        -p      Show pids"
29
30 #include "libbb.h"
31
32 #define PROC_BASE "/proc"
33
34 #define OPT_PID  (1 << 0)
35
36 struct child;
37
38 typedef struct proc {
39         char comm[COMM_LEN + 1];
40 //      char flags; - unused, delete?
41         pid_t pid;
42         uid_t uid;
43         struct child *children;
44         struct proc *parent;
45         struct proc *next;
46 } PROC;
47
48 /* For flags above */
49 //#define PFLAG_THREAD  0x01
50
51 typedef struct child {
52         PROC *child;
53         struct child *next;
54 } CHILD;
55
56 #define empty_2  "  "
57 #define branch_2 "|-"
58 #define vert_2   "| "
59 #define last_2   "`-"
60 #define single_3 "---"
61 #define first_3  "-+-"
62
63 struct globals {
64         /* 0-based. IOW: the number of chars we printed on current line */
65         unsigned cur_x;
66         unsigned output_width;
67
68         /* The buffers will be dynamically increased in size as needed */
69         unsigned capacity;
70         unsigned *width;
71         uint8_t *more;
72
73         PROC *list;
74
75         smallint dumped; /* used by dump_by_user */
76 };
77 #define G (*ptr_to_globals)
78 #define INIT_G() do { \
79         SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
80 } while (0)
81
82
83 /*
84  * Allocates additional buffer space for width and more as needed.
85  * The first call will allocate the first buffer.
86  *
87  * bufindex  the index that will be used after the call to this function.
88  */
89 static void ensure_buffer_capacity(int bufindex)
90 {
91         if (bufindex >= G.capacity) {
92                 G.capacity += 0x100;
93                 G.width = xrealloc(G.width, G.capacity * sizeof(G.width[0]));
94                 G.more = xrealloc(G.more, G.capacity * sizeof(G.more[0]));
95         }
96 }
97
98 /* NB: this function is never called with "bad" chars
99  * (control chars or chars >= 0x7f)
100  */
101 static void out_char(char c)
102 {
103         G.cur_x++;
104         if (G.cur_x > G.output_width)
105                 return;
106         if (G.cur_x == G.output_width)
107                 c = '+';
108         putchar(c);
109 }
110
111 /* NB: this function is never called with "bad" chars
112  * (control chars or chars >= 0x7f)
113  */
114 static void out_string(const char *str)
115 {
116         while (*str)
117                 out_char(*str++);
118 }
119
120 static void out_newline(void)
121 {
122         putchar('\n');
123         G.cur_x = 0;
124 }
125
126 static PROC *find_proc(pid_t pid)
127 {
128         PROC *walk;
129
130         for (walk = G.list; walk; walk = walk->next)
131                 if (walk->pid == pid)
132                         break;
133
134         return walk;
135 }
136
137 static PROC *new_proc(const char *comm, pid_t pid, uid_t uid)
138 {
139         PROC *new = xzalloc(sizeof(*new));
140
141         strcpy(new->comm, comm);
142         new->pid = pid;
143         new->uid = uid;
144         new->next = G.list;
145
146         G.list = new;
147         return G.list;
148 }
149
150 static void add_child(PROC *parent, PROC *child)
151 {
152         CHILD *new, **walk;
153         int cmp;
154
155         new = xmalloc(sizeof(*new));
156
157         new->child = child;
158         for (walk = &parent->children; *walk; walk = &(*walk)->next) {
159                 cmp = strcmp((*walk)->child->comm, child->comm);
160                 if (cmp > 0)
161                         break;
162                 if (cmp == 0 && (*walk)->child->uid > child->uid)
163                         break;
164         }
165         new->next = *walk;
166         *walk = new;
167 }
168
169 static void add_proc(const char *comm, pid_t pid, pid_t ppid,
170                         uid_t uid /*, char isthread*/)
171 {
172         PROC *this, *parent;
173
174         this = find_proc(pid);
175         if (!this)
176                 this = new_proc(comm, pid, uid);
177         else {
178                 strcpy(this->comm, comm);
179                 this->uid = uid;
180         }
181
182         if (pid == ppid)
183                 ppid = 0;
184 //      if (isthread)
185 //              this->flags |= PFLAG_THREAD;
186
187         parent = find_proc(ppid);
188         if (!parent)
189                 parent = new_proc("?", ppid, 0);
190
191         add_child(parent, this);
192         this->parent = parent;
193 }
194
195 static int tree_equal(const PROC *a, const PROC *b)
196 {
197         const CHILD *walk_a, *walk_b;
198
199         if (strcmp(a->comm, b->comm) != 0)
200                 return 0;
201         if ((option_mask32 /*& OPT_PID*/) && a->pid != b->pid)
202                 return 0;
203
204         for (walk_a = a->children, walk_b = b->children;
205           walk_a && walk_b;
206           walk_a = walk_a->next, walk_b = walk_b->next
207         ) {
208                 if (!tree_equal(walk_a->child, walk_b->child))
209                         return 0;
210         }
211
212         return !(walk_a || walk_b);
213 }
214
215 static int out_args(const char *mystr)
216 {
217         const char *here;
218         int strcount = 0;
219         char tmpstr[5];
220
221         for (here = mystr; *here; here++) {
222                 if (*here == '\\') {
223                         out_string("\\\\");
224                         strcount += 2;
225                 } else if (*here >= ' ' && *here < 0x7f) {
226                         out_char(*here);
227                         strcount++;
228                 } else {
229                         sprintf(tmpstr, "\\%03o", (unsigned char) *here);
230                         out_string(tmpstr);
231                         strcount += 4;
232                 }
233         }
234
235         return strcount;
236 }
237
238 static void
239 dump_tree(PROC *current, int level, int rep, int leaf, int last, int closing)
240 {
241         CHILD *walk, *next, **scan;
242         int lvl, i, add, offset, count, comm_len, first;
243         char tmp[sizeof(int)*3 + 4];
244
245         if (!current)
246                 return;
247
248         if (!leaf) {
249                 for (lvl = 0; lvl < level; lvl++) {
250                         i = G.width[lvl] + 1;
251                         while (--i >= 0)
252                                 out_char(' ');
253
254                         if (lvl == level - 1) {
255                                 if (last) {
256                                         out_string(last_2);
257                                 } else {
258                                         out_string(branch_2);
259                                 }
260                         } else {
261                                 if (G.more[lvl + 1]) {
262                                         out_string(vert_2);
263                                 } else {
264                                         out_string(empty_2);
265                                 }
266                         }
267                 }
268         }
269
270         add = 0;
271         if (rep > 1) {
272                 add += sprintf(tmp, "%d*[", rep);
273                 out_string(tmp);
274         }
275         comm_len = out_args(current->comm);
276         if (option_mask32 /*& OPT_PID*/) {
277                 comm_len += sprintf(tmp, "(%d)", (int)current->pid);
278                 out_string(tmp);
279         }
280         offset = G.cur_x;
281
282         if (!current->children) {
283                 while (closing--)
284                         out_char(']');
285                 out_newline();
286         }
287         ensure_buffer_capacity(level);
288         G.more[level] = !last;
289
290         G.width[level] = comm_len + G.cur_x - offset + add;
291         if (G.cur_x >= G.output_width) {
292                 //out_string(first_3); - why? it won't print anything
293                 //out_char('+');
294                 out_newline();
295                 return;
296         }
297
298         first = 1;
299         for (walk = current->children; walk; walk = next) {
300                 count = 0;
301                 next = walk->next;
302                 scan = &walk->next;
303                 while (*scan) {
304                         if (!tree_equal(walk->child, (*scan)->child))
305                                 scan = &(*scan)->next;
306                         else {
307                                 if (next == *scan)
308                                         next = (*scan)->next;
309                                 count++;
310                                 *scan = (*scan)->next;
311                         }
312                 }
313                 if (first) {
314                         out_string(next ? first_3 : single_3);
315                         first = 0;
316                 }
317
318                 dump_tree(walk->child, level + 1, count + 1,
319                                 walk == current->children, !next,
320                                 closing + (count ? 1 : 0));
321         }
322 }
323
324 static void dump_by_user(PROC *current, uid_t uid)
325 {
326         const CHILD *walk;
327
328         if (!current)
329                 return;
330
331         if (current->uid == uid) {
332                 if (G.dumped)
333                         putchar('\n');
334                 dump_tree(current, 0, 1, 1, 1, 0);
335                 G.dumped = 1;
336                 return;
337         }
338         for (walk = current->children; walk; walk = walk->next)
339                 dump_by_user(walk->child, uid);
340 }
341
342 static void handle_thread(const char *comm, pid_t pid, pid_t ppid, uid_t uid)
343 {
344         char threadname[COMM_LEN + 2];
345         sprintf(threadname, "{%.*s}", COMM_LEN - 2, comm);
346         add_proc(threadname, pid, ppid, uid/*, 1*/);
347 }
348
349 static void mread_proc(void)
350 {
351         procps_status_t *p = NULL;
352         pid_t parent = 0;
353         int flags = PSSCAN_COMM | PSSCAN_PID | PSSCAN_PPID | PSSCAN_UIDGID | PSSCAN_TASKS;
354
355         while ((p = procps_scan(p, flags)) != NULL) {
356 #if ENABLE_FEATURE_SHOW_THREADS
357                 if (p->pid != p->main_thread_pid)
358                         handle_thread(p->comm, p->pid, parent, p->uid);
359                 else
360 #endif
361                 {
362                         add_proc(p->comm, p->pid, p->ppid, p->uid/*, 0*/);
363                         parent = p->pid;
364                 }
365         }
366 }
367
368 int pstree_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
369 int pstree_main(int argc UNUSED_PARAM, char **argv)
370 {
371         pid_t pid = 1;
372         long uid = 0;
373
374         INIT_G();
375
376         get_terminal_width_height(0, &G.output_width, NULL);
377
378         opt_complementary = "?1";
379         getopt32(argv, "p");
380         argv += optind;
381
382         if (argv[0]) {
383                 if (argv[0][0] >= '0' && argv[0][0] <= '9') {
384                         pid = xatoi(argv[0]);
385                 } else {
386                         uid = xuname2uid(argv[0]);
387                 }
388         }
389
390         mread_proc();
391
392         if (!uid)
393                 dump_tree(find_proc(pid), 0, 1, 1, 1, 0);
394         else {
395                 dump_by_user(find_proc(1), uid);
396                 if (!G.dumped) {
397                         bb_error_msg_and_die("no processes found");
398                 }
399         }
400
401         if (ENABLE_FEATURE_CLEAN_UP) {
402                 free(G.width);
403                 free(G.more);
404         }
405         return 0;
406 }