ps: seconds_since_boot: uint64_t -> ulong
[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:     "\n        -p      Show pids"
28
29 #include "libbb.h"
30
31 #define PROC_BASE "/proc"
32
33 #define OPT_PID  (1 << 0)
34
35 struct child;
36
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;
46
47 /* For flags above */
48 //#define PFLAG_THREAD  0x01
49
50 typedef struct child {
51         PROC *child;
52         struct child *next;
53 } CHILD;
54
55 #define empty_2  "  "
56 #define branch_2 "|-"
57 #define vert_2   "| "
58 #define last_2   "`-"
59 #define single_3 "---"
60 #define first_3  "-+-"
61
62 struct globals {
63         /* 0-based. IOW: the number of chars we printed on current line */
64         unsigned cur_x;
65         unsigned output_width;
66
67         /* The buffers will be dynamically increased in size as needed */
68         unsigned capacity;
69         unsigned *width;
70         uint8_t *more;
71
72         PROC *list;
73
74         smallint dumped; /* used by dump_by_user */
75 };
76 #define G (*ptr_to_globals)
77 #define INIT_G() do { \
78         SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
79 } while (0)
80
81
82 /*
83  * Allocates additional buffer space for width and more as needed.
84  * The first call will allocate the first buffer.
85  *
86  * bufindex  the index that will be used after the call to this function.
87  */
88 static void ensure_buffer_capacity(int bufindex)
89 {
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]));
94         }
95 }
96
97 /* NB: this function is never called with "bad" chars
98  * (control chars or chars >= 0x7f)
99  */
100 static void out_char(char c)
101 {
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);
108 }
109
110 /* NB: this function is never called with "bad" chars
111  * (control chars or chars >= 0x7f)
112  */
113 static void out_string(const char *str)
114 {
115         while (*str)
116                 out_char(*str++);
117 }
118
119 static void out_newline(void)
120 {
121         putchar('\n');
122         G.cur_x = 0;
123 }
124
125 static PROC *find_proc(pid_t pid)
126 {
127         PROC *walk;
128
129         for (walk = G.list; walk; walk = walk->next)
130                 if (walk->pid == pid)
131                         break;
132
133         return walk;
134 }
135
136 static PROC *new_proc(const char *comm, pid_t pid, uid_t uid)
137 {
138         PROC *new = xzalloc(sizeof(*new));
139
140         strcpy(new->comm, comm);
141         new->pid = pid;
142         new->uid = uid;
143         new->next = G.list;
144
145         G.list = new;
146         return G.list;
147 }
148
149 static void add_child(PROC *parent, PROC *child)
150 {
151         CHILD *new, **walk;
152         int cmp;
153
154         new = xmalloc(sizeof(*new));
155
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;
163         }
164         new->next = *walk;
165         *walk = new;
166 }
167
168 static void add_proc(const char *comm, pid_t pid, pid_t ppid,
169                         uid_t uid /*, char isthread*/)
170 {
171         PROC *this, *parent;
172
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;
179         }
180
181         if (pid == ppid)
182                 ppid = 0;
183 //      if (isthread)
184 //              this->flags |= PFLAG_THREAD;
185
186         parent = find_proc(ppid);
187         if (!parent)
188                 parent = new_proc("?", ppid, 0);
189
190         add_child(parent, this);
191         this->parent = parent;
192 }
193
194 static int tree_equal(const PROC *a, const PROC *b)
195 {
196         const CHILD *walk_a, *walk_b;
197
198         if (strcmp(a->comm, b->comm) != 0)
199                 return 0;
200         if ((option_mask32 /*& OPT_PID*/) && a->pid != b->pid)
201                 return 0;
202
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
206         ) {
207                 if (!tree_equal(walk_a->child, walk_b->child))
208                         return 0;
209         }
210
211         return !(walk_a || walk_b);
212 }
213
214 static int out_args(const char *mystr)
215 {
216         const char *here;
217         int strcount = 0;
218         char tmpstr[5];
219
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;
231                 }
232         }
233
234         return strcount;
235 }
236
237 static void
238 dump_tree(PROC *current, int level, int rep, int leaf, int last, int closing)
239 {
240         CHILD *walk, *next, **scan;
241         int lvl, i, add, offset, count, comm_len, first;
242         char tmp[sizeof(int)*3 + 4];
243
244         if (!current)
245                 return;
246
247         if (!leaf) {
248                 for (lvl = 0; lvl < level; lvl++) {
249                         i = G.width[lvl] + 1;
250                         while (--i >= 0)
251                                 out_char(' ');
252
253                         if (lvl == level - 1) {
254                                 if (last) {
255                                         out_string(last_2);
256                                 } else {
257                                         out_string(branch_2);
258                                 }
259                         } else {
260                                 if (G.more[lvl + 1]) {
261                                         out_string(vert_2);
262                                 } else {
263                                         out_string(empty_2);
264                                 }
265                         }
266                 }
267         }
268
269         add = 0;
270         if (rep > 1) {
271                 add += sprintf(tmp, "%d*[", rep);
272                 out_string(tmp);
273         }
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);
278         }
279         offset = G.cur_x;
280
281         if (!current->children) {
282                 while (closing--)
283                         out_char(']');
284                 out_newline();
285         }
286         ensure_buffer_capacity(level);
287         G.more[level] = !last;
288
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;
295         }
296
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;
310                         }
311                 }
312                 if (first) {
313                         out_string(next ? first_3 : single_3);
314                         first = 0;
315                 }
316
317                 dump_tree(walk->child, level + 1, count + 1,
318                                 walk == current->children, !next,
319                                 closing + (count ? 1 : 0));
320         }
321 }
322
323 static void dump_by_user(PROC *current, uid_t uid)
324 {
325         const CHILD *walk;
326
327         if (!current)
328                 return;
329
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;
336         }
337         for (walk = current->children; walk; walk = walk->next)
338                 dump_by_user(walk->child, uid);
339 }
340
341 #if ENABLE_FEATURE_SHOW_THREADS
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 #endif
349
350 static void mread_proc(void)
351 {
352         procps_status_t *p = NULL;
353         pid_t parent = 0;
354         int flags = PSSCAN_COMM | PSSCAN_PID | PSSCAN_PPID | PSSCAN_UIDGID | PSSCAN_TASKS;
355
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
362                 {
363                         add_proc(p->comm, p->pid, p->ppid, p->uid/*, 0*/);
364                         parent = p->pid;
365                 }
366         }
367 }
368
369 int pstree_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
370 int pstree_main(int argc UNUSED_PARAM, char **argv)
371 {
372         pid_t pid = 1;
373         long uid = 0;
374
375         INIT_G();
376
377         get_terminal_width_height(0, &G.output_width, NULL);
378
379         opt_complementary = "?1";
380         getopt32(argv, "p");
381         argv += optind;
382
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]);
388                 }
389         }
390
391         mread_proc();
392
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");
399                 }
400         }
401
402         if (ENABLE_FEATURE_CLEAN_UP) {
403                 free(G.width);
404                 free(G.more);
405         }
406         return 0;
407 }