blob: 16649cfaa46ed64521bc86b6265f1f2c8be63b55 [file] [log] [blame]
Lauri Kasanen6578f2c2010-12-05 05:22:29 +01001/*
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
Denys Vlasenkob9f2d9f2011-01-18 13:58:01 +010019//applet:IF_PSTREE(APPLET(pstree, BB_DIR_USR_BIN, BB_SUID_DROP))
Lauri Kasanen6578f2c2010-12-05 05:22:29 +010020
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
36struct child;
37
38typedef 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
51typedef 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
63struct globals {
Denys Vlasenko2161bd72010-12-05 19:36:58 +010064 /* 0-based. IOW: the number of chars we printed on current line */
Lauri Kasanene48e6f82010-12-05 15:53:55 +010065 unsigned cur_x;
66 unsigned output_width;
Lauri Kasanen6578f2c2010-12-05 05:22:29 +010067
68 /* The buffers will be dynamically increased in size as needed */
69 unsigned capacity;
Lauri Kasanene48e6f82010-12-05 15:53:55 +010070 unsigned *width;
71 uint8_t *more;
Lauri Kasanen6578f2c2010-12-05 05:22:29 +010072
Lauri Kasanene48e6f82010-12-05 15:53:55 +010073 PROC *list;
74
Lauri Kasanen6578f2c2010-12-05 05:22:29 +010075 smallint dumped; /* used by dump_by_user */
76};
77#define G (*ptr_to_globals)
78#define INIT_G() do { \
Denys Vlasenkob7c9fb22011-02-03 00:05:48 +010079 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
Lauri Kasanen6578f2c2010-12-05 05:22:29 +010080} 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 *
Lauri Kasanene48e6f82010-12-05 15:53:55 +010087 * bufindex the index that will be used after the call to this function.
Lauri Kasanen6578f2c2010-12-05 05:22:29 +010088 */
89static 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
Lauri Kasanen6578f2c2010-12-05 05:22:29 +010098/* NB: this function is never called with "bad" chars
99 * (control chars or chars >= 0x7f)
100 */
101static void out_char(char c)
102{
103 G.cur_x++;
Denys Vlasenkoc32e6262010-12-05 16:05:03 +0100104 if (G.cur_x > G.output_width)
105 return;
Lauri Kasanene48e6f82010-12-05 15:53:55 +0100106 if (G.cur_x == G.output_width)
107 c = '+';
Denys Vlasenkoc32e6262010-12-05 16:05:03 +0100108 putchar(c);
Lauri Kasanen6578f2c2010-12-05 05:22:29 +0100109}
110
111/* NB: this function is never called with "bad" chars
112 * (control chars or chars >= 0x7f)
113 */
114static void out_string(const char *str)
115{
116 while (*str)
117 out_char(*str++);
118}
119
120static void out_newline(void)
121{
122 putchar('\n');
Lauri Kasanene48e6f82010-12-05 15:53:55 +0100123 G.cur_x = 0;
Lauri Kasanen6578f2c2010-12-05 05:22:29 +0100124}
125
126static 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
137static 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
150static 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
169static 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
195static 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
215static 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
238static void
239dump_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;
Lauri Kasanene48e6f82010-12-05 15:53:55 +0100243 char tmp[sizeof(int)*3 + 4];
Lauri Kasanen6578f2c2010-12-05 05:22:29 +0100244
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
Lauri Kasanene48e6f82010-12-05 15:53:55 +0100270 add = 0;
271 if (rep > 1) {
272 add += sprintf(tmp, "%d*[", rep);
273 out_string(tmp);
Lauri Kasanen6578f2c2010-12-05 05:22:29 +0100274 }
275 comm_len = out_args(current->comm);
276 if (option_mask32 /*& OPT_PID*/) {
Lauri Kasanene48e6f82010-12-05 15:53:55 +0100277 comm_len += sprintf(tmp, "(%d)", (int)current->pid);
278 out_string(tmp);
Lauri Kasanen6578f2c2010-12-05 05:22:29 +0100279 }
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;
Lauri Kasanene48e6f82010-12-05 15:53:55 +0100291 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 }
Lauri Kasanen6578f2c2010-12-05 05:22:29 +0100297
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
324static 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
Denys Vlasenkoa9e5c432011-03-27 16:15:02 +0200342#if ENABLE_FEATURE_SHOW_THREADS
Lauri Kasanen6578f2c2010-12-05 05:22:29 +0100343static void handle_thread(const char *comm, pid_t pid, pid_t ppid, uid_t uid)
344{
345 char threadname[COMM_LEN + 2];
346 sprintf(threadname, "{%.*s}", COMM_LEN - 2, comm);
347 add_proc(threadname, pid, ppid, uid/*, 1*/);
348}
Denys Vlasenkoa9e5c432011-03-27 16:15:02 +0200349#endif
Lauri Kasanen6578f2c2010-12-05 05:22:29 +0100350
351static void mread_proc(void)
352{
353 procps_status_t *p = NULL;
354 pid_t parent = 0;
355 int flags = PSSCAN_COMM | PSSCAN_PID | PSSCAN_PPID | PSSCAN_UIDGID | PSSCAN_TASKS;
356
357 while ((p = procps_scan(p, flags)) != NULL) {
358#if ENABLE_FEATURE_SHOW_THREADS
359 if (p->pid != p->main_thread_pid)
360 handle_thread(p->comm, p->pid, parent, p->uid);
361 else
362#endif
363 {
364 add_proc(p->comm, p->pid, p->ppid, p->uid/*, 0*/);
365 parent = p->pid;
366 }
367 }
368}
369
370int pstree_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
371int pstree_main(int argc UNUSED_PARAM, char **argv)
372{
373 pid_t pid = 1;
374 long uid = 0;
375
376 INIT_G();
Lauri Kasanen6578f2c2010-12-05 05:22:29 +0100377
Denys Vlasenko86aa8032010-12-06 12:54:24 +0100378 get_terminal_width_height(0, &G.output_width, NULL);
Lauri Kasanen6578f2c2010-12-05 05:22:29 +0100379
Denys Vlasenko2161bd72010-12-05 19:36:58 +0100380 opt_complementary = "?1";
Lauri Kasanen6578f2c2010-12-05 05:22:29 +0100381 getopt32(argv, "p");
382 argv += optind;
383
384 if (argv[0]) {
Lauri Kasanen6578f2c2010-12-05 05:22:29 +0100385 if (argv[0][0] >= '0' && argv[0][0] <= '9') {
386 pid = xatoi(argv[0]);
387 } else {
388 uid = xuname2uid(argv[0]);
389 }
390 }
391
392 mread_proc();
393
394 if (!uid)
395 dump_tree(find_proc(pid), 0, 1, 1, 1, 0);
396 else {
397 dump_by_user(find_proc(1), uid);
398 if (!G.dumped) {
399 bb_error_msg_and_die("no processes found");
400 }
401 }
402
Denys Vlasenko2161bd72010-12-05 19:36:58 +0100403 if (ENABLE_FEATURE_CLEAN_UP) {
404 free(G.width);
405 free(G.more);
406 }
Lauri Kasanen6578f2c2010-12-05 05:22:29 +0100407 return 0;
408}