-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpstree.c
1185 lines (1076 loc) · 34.3 KB
/
pstree.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* This is pstree written by Fred Hucht (c) 1992-2022 *
* E-Mail: fred AT thp.uni-due.de *
* Feel free to copy and redistribute in terms of the *
* GNU public license. *
*/
static char *WhatString[]= {
"@(#)pstree $Revision: 2.40 $ by Fred Hucht (C) 1992-2022",
"@(#)E-Mail: fred AT thp.uni-due.de"
};
#define MAXLINE 8192
#if defined(_AIX) || defined(___AIX) /* AIX >= 3.1 */
/* Under AIX, we directly read the process table from the kernel */
# ifndef _AIX50
/* problems with getprocs() under AIX 5L
* workaround contributed by Chris Benesch <chris AT fdbs.com> */
# define USE_GetProcessesDirect
# endif /*_AIX50*/
# define HAS_TERMDEF
extern char *termdef(int, char);
# define _ALL_SOURCE
# include <procinfo.h>
# define USE_GETPROCS
# ifdef USE_GETPROCS
# define IFNEW(a,b) a
# define ProcInfo procsinfo
# ifndef _AIX61
/* workaround contributed by Michael Staats <michael.staats AT gmx.de> */
extern getprocs(struct procsinfo *, int, struct fdsinfo *, int, pid_t *, int);
# endif /*_AIX61*/
# else /*USE_GETPROCS*/
# define IFNEW(a,b) b
# define ProcInfo procinfo
extern getproc(struct procinfo *, int, int);
extern getuser(struct procinfo *, int, void *, int);
# endif /*USE_GETPROCS*/
# ifndef _AIX61
/* workaround contributed by Michael Staats <michael.staats AT gmx.de> */
extern getargs(struct ProcInfo *, int, char *, int);
# endif /*_AIX61*/
/*# define PSCMD "ps -ekf"
# define PSFORMAT "%s %ld %ld %*20c %*s %[^\n]"*/
# define HAS_PGID
# define UID2USER
# define PSCMD "ps -eko uid,pid,ppid,pgid,thcount,args"
# define PSFORMAT "%ld %ld %ld %ld %ld %[^\n]"
# define PSVARS &P[i].uid, &P[i].pid, &P[i].ppid, &P[i].pgid, &P[i].thcount, P[i].cmd
# define PSVARSN 6
/************************************************************************/
#elif defined(__linux) || (defined __alpha && defined(_SYSTYPE_BSD) || defined (Tru64))
/* TRU64 contributed by Frank Parkin <fparki AT acxiom.co.uk>
*/
# ifdef __linux
# define USE_GetProcessesDirect
# include <glob.h>
# include <sys/stat.h>
# endif
# define UID2USER
# define HAS_PGID
# define PSCMD "ps -eo uid,pid,ppid,pgid,args"
# define PSFORMAT "%ld %ld %ld %ld %[^\n]"
# define PSVARS &P[i].uid, &P[i].pid, &P[i].ppid, &P[i].pgid, P[i].cmd
# define PSVARSN 5
/************************************************************************/
#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__APPLE__) || defined(__DragonFly__)
/* NetBSD contributed by Gary D. Duzan <gary AT wheel.tiac.net>
* FreeBSD contributed by Randall Hopper <rhh AT ct.picker.com>
* Darwin / Mac OS X patch by Yuji Yamano <yyamano AT kt.rim.or.jp>
* wide output format fix for NetBSD by Jeff Brown <jabrown AT caida.org>
* (Net|Open|Free)BSD & Darwin merged by Ralf Meyer <ralf AT thp.Uni-Duisburg.DE>
* DragonFlyBSD contributed by Krzysztof Piecuch <piecuch AT kpiecuch.pl>
*/
# define HAS_PGID
# define PSCMD "ps -axwwo user,pid,ppid,pgid,command"
# define PSFORMAT "%s %ld %ld %ld %[^\n]"
# define PSVARS P[i].name, &P[i].pid, &P[i].ppid, &P[i].pgid, P[i].cmd
# define PSVARSN 5
# ifdef __DragonFly__
# define PARENT_CYCLE_PID_MINUS_ONE
# else
# define ZOMBIES_HAVE_PID_0
# endif
/************************************************************************/
#elif defined(sun) && (!defined(__SVR4)) /* Solaris 1.x */
/* contributed by L. Mark Larsen <mlarsen AT ptdcs2.intel.com> */
/* new cpp criteria by Pierre Belanger <belanger AT risq.qc.ca> */
# define solaris1x
# define UID2USER
# ifdef mc68000
/* contributed by Paul Kern <pkern AT utcc.utoronto.ca> */
# define PSCMD "ps laxw"
# define PSFORMAT "%*7c%ld %ld %ld %*d %*d %*d %*x %*d %*d %*x %*14c %[^\n]"
# define uid_t int
# define NEED_STRSTR
# else
# define PSCMD "ps jaxw"
# define PSFORMAT "%ld %ld %*d %*d %*s %*d %*s %ld %*s %[^\n]"
# define PSVARS &P[i].ppid, &P[i].pid, &P[i].uid, P[i].cmd
# define PSVARSN 4
# endif
/************************************************************************/
#elif defined(sun) && (defined(__SVR4)) /* Solaris 2.x */
/* contributed by Pierre Belanger <belanger AT risq.qc.ca> */
# define solaris2x
# define PSCMD "ps -ef"
# define PSFORMAT "%s %ld %ld %*d %*s %*s %*s %[^\n]"
/************************************************************************/
#elif defined(bsdi)
/* contributed by Dean Gaudet <dgaudet AT hotwired.com> */
# define UID2USER
# define PSCMD "ps laxw"
# define PSFORMAT "%ld %ld %ld %*d %*d %*d %*d %*d %*s %*s %*s %*s %[^\n]"
/************************************************************************/
#elif defined(_BSD) /* Untested */
# define UID2USER
# define PSCMD "ps laxw"
# define PSFORMAT "%*d %*c %ld %ld %ld %*d %*d %*d %*x %*d %d %*15c %*s %[^\n]"
/************************************************************************/
#elif defined(__convex) /* ConvexOS */
# define UID2USER
# define PSCMD "ps laxw"
# define PSFORMAT "%*s %ld %ld %ld %*d %*g %*d %*d %*21c %*s %[^\n]"
/************************************************************************/
#else /* HP-UX, A/UX etc. */
# define PSCMD "ps -ef"
# define PSFORMAT "%s %ld %ld %*20c %*s %[^\n]"
#endif
/*********************** end of configurable part ***********************/
#ifndef PSVARS /* Set default */
# ifdef UID2USER
# define PSVARS &P[i].uid, &P[i].pid, &P[i].ppid, P[i].cmd
# else
# define PSVARS P[i].name, &P[i].pid, &P[i].ppid, P[i].cmd
# endif
# define PSVARSN 4
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h> /* For str...() */
#ifdef NEED_SNPRINTF
#include <stdarg.h>
int snprintf(char *, int, char *, ...);
#endif
#include <unistd.h> /* For getopt() */
#include <pwd.h> /* For getpwnam() */
#include <sys/ioctl.h> /* For TIOCGSIZE/TIOCGWINSZ */
/* #include <termios.h> */
#ifdef DEBUG
# include <errno.h>
#endif
#ifdef NEED_STRSTR
static char *strstr(char *, char *);
#endif
#ifndef TRUE
#define TRUE 1
#define FALSE 0
#endif
struct TreeChars {
char *s2, /* SS String between header and pid */
*p, /* PP dito, when parent of printed childs */
*pgl, /* G Process group leader */
*npgl, /* N No process group leader */
*barc, /* C bar for line with child */
*bar, /* B bar for line without child */
*barl, /* L bar for last child */
*sg, /* Start graphics (alt char set) */
*eg, /* End graphics (alt char set) */
*init; /* Init string sent at the beginning */
};
/* Example:
* |-+- 01111 ... CPPN 01111 ...
* | \-+= 01112 ... B LPPG 01112 ...
* | |--= 01113 ... B CSSG 01113 ...
* | \--= 01114 ... B LSSG 01114 ...
* \--- 01115 ... LSSN 01115 ...
*/
enum { G_ASCII = 0, G_PC850 = 1, G_VT100 = 2, G_UTF8 = 3, G_LAST };
/* VT sequences contributed by Randall Hopper <rhh AT ct.picker.com> */
/* UTF8 sequences contributed by Mark-Andre Hopf <mhopf AT mark13.org> */
static struct TreeChars TreeChars[] = {
/* SS PP G N C B L sg eg init */
{ "--", "-+", "=", "-", "|", "|", "\\", "", "", "" }, /*Ascii*/
{ "\304\304", "\304\302", "\372", "\304", "\303", "\263", "\300", "", "", "" }, /*Pc850*/
{ "qq", "qw", "`", "q", "t", "x", "m", "\016", "\017", "\033(B\033)0" }, /*Vt100*/
{ "\342\224\200\342\224\200",
/**/ "\342\224\200\342\224\254",
/**/ "=",
/**/ "\342\224\200",
/**/ "\342\224\234",
/**/ "\342\224\202",
/**/ "\342\224\224",
/**/ "", "", "" } /*UTF8*/
}, *C;
static int MyPid, NProc, Columns, RootPid;
static short showall = TRUE, soption = FALSE, Uoption = FALSE;
static char *name = "", *str = NULL, *Progname;
static long ipid = -1;
static char *input = NULL;
static int atLdepth=0; /* LOPTION - track how deep in the print chain we are */
static int maxLdepth=100; /* LOPTION - will be changed by -l n option */
static int compress = FALSE;
#ifdef DEBUG
static int debug = FALSE;
#endif
struct Proc {
long uid, pid, ppid, pgid;
char name[32], cmd[MAXLINE];
int print;
long parent, child, sister;
unsigned long thcount;
} *P;
#ifdef UID2USER
static void uid2user(uid_t uid, char *name, int len) {
#define NUMUN 128
static struct un_ {
uid_t uid;
char name[32];
} un[NUMUN];
static short n = 0;
short i;
char uid_name[32];
char *found;
#ifdef DEBUG
if (name == NULL) {
for (i = 0; i < n; i++)
fprintf(stderr, "uid = %3d, name = %s\n", un[i].uid, un[i].name);
return;
}
#endif
for (i = n - 1; i >= 0 && un[i].uid != uid; i--);
if (i >= 0) { /* found locally */
found = un[i].name;
} else {
struct passwd *pw = getpwuid(uid);
if (pw) {
found = pw->pw_name;
} else {
/* fix by Stan Sieler & Philippe Torche */
snprintf(uid_name, sizeof(uid_name), "#%d", uid);
found = uid_name;
}
if (n < NUMUN) {
un[n].uid = uid;
strncpy(un[n].name, found, 9);
un[n].name[8] = '\0';
n++;
}
}
strncpy(name, found, len);
name[len-1] = '\0';
}
#endif
#if defined(_AIX) || defined(___AIX) /* AIX 3.x / 4.x */
static int GetProcessesDirect(void) {
int i, nproc, maxnproc = 1024;
struct ProcInfo *proc;
int idx;
#ifndef USE_GETPROCS
struct userinfo user;
#endif
do {
proc = malloc(maxnproc * sizeof(struct ProcInfo));
if (proc == NULL) {
fprintf(stderr, "Problems with malloc.\n");
exit(1);
}
/* Get process table */
idx = 0;
nproc = IFNEW(getprocs(proc, sizeof(struct procsinfo), NULL, 0,
&idx, maxnproc),
getproc(proc, maxnproc, sizeof(struct procinfo))
);
#ifdef DEBUG
idx = errno; /* Don't ask... */
if (debug)
fprintf(stderr,
"nproc = %d maxnproc = %d" IFNEW(" idx = %d ","") "\n",
nproc, maxnproc, idx);
errno = idx;
#endif
#ifdef USE_GETPROCS
if (nproc == -1) {
perror("getprocs");
exit(1);
} else if (nproc == maxnproc) {
nproc = -1;
}
#endif
if (nproc == -1) {
free(proc);
maxnproc *= 2;
}
} while (nproc == -1);
P = malloc((nproc+1) * sizeof(struct Proc));
if (P == NULL) {
fprintf(stderr, "Problems with malloc.\n");
exit(1);
}
for (i = 0; i < nproc; i++) {
#ifndef USE_GETPROCS
getuser(&proc[i],sizeof(struct procinfo),
&user, sizeof(struct userinfo));
#endif
P[i].uid = proc[i].pi_uid;
P[i].pid = proc[i].pi_pid;
P[i].ppid = proc[i].pi_ppid;
P[i].pgid = proc[i].pi_pgrp;
P[i].thcount = IFNEW(proc[i].pi_thcount, 1);
uid2user(P[i].uid, P[i].name, sizeof(P[i].name));
if (IFNEW(proc[i].pi_state,proc[i].pi_stat) == SZOMB) {
strcpy(P[i].cmd, "<defunct>");
} else {
char *c = P[i].cmd;
int ci = 0;
getargs(&proc[i], sizeof(struct procinfo), c, MAXLINE - 2);
c[MAXLINE-2] = c[MAXLINE-1] = '\0';
/* Collect args. Stop when we encounter two '\0' */
while (c[ci] != '\0' && (ci += strlen(&c[ci])) < MAXLINE - 2)
c[ci++] = ' ';
/* Drop trailing blanks */
ci = strlen(c);
while (ci > 0 && c[ci-1] == ' ') ci--;
c[ci] = '\0';
/* Replace some unprintables with '?' */
for (ci = 0; c[ci] != '\0'; ci++)
if (c[ci] == '\n' || c[ci] == '\t') c[ci] = '?';
/* Insert [ui_comm] when getargs returns nothing */
if (c[0] == '\0') {
int l = strlen(IFNEW(proc[i].pi_comm,user.ui_comm));
c[0] = '[';
strcpy(c+1, IFNEW(proc[i].pi_comm,user.ui_comm));
c[l+1] = ']';
c[l+2] = '\0';
}
}
#ifdef DEBUG
if (debug)
fprintf(stderr,
"%d: uid=%5ld, name=%8s, pid=%5ld, ppid=%5ld, pgid=%5ld, tsize=%7u, dvm=%4u, "
"thcount=%2d, cmd[%d]='%s'\n",
i, P[i].uid, P[i].name, P[i].pid, P[i].ppid, P[i].pgid,
IFNEW(proc[i].pi_tsize,user.ui_tsize),
IFNEW(proc[i].pi_dvm,user.ui_dvm),
proc[i].pi_thcount,
strlen(P[i].cmd),P[i].cmd);
#endif
P[i].parent = P[i].child = P[i].sister = -1;
P[i].print = FALSE;
}
free(proc);
return nproc;
}
#endif /* _AIX */
#ifdef __linux
static int GetProcessesDirect(void) {
glob_t globbuf;
unsigned int i, j;
glob("/proc/[0-9]*", GLOB_NOSORT, NULL, &globbuf);
P = calloc(globbuf.gl_pathc, sizeof(struct Proc));
if (P == NULL) {
fprintf(stderr, "Problems with malloc.\n");
exit(1);
}
for (i = j = 0; i < globbuf.gl_pathc; i++) {
char *pdir, name[32];
int c;
FILE *tn;
int k = 0;
pdir = globbuf.gl_pathv[globbuf.gl_pathc - i - 1];
/* if processes change their UID this change is only reflected in the owner of pdir.
* fixed since version 2.36 */
{
struct stat st;
if (stat(pdir, &st) != 0) { /* get uid */
continue; /* process vanished since glob() */
}
P[j].uid = st.st_uid;
uid2user(P[j].uid, P[j].name, sizeof(P[j].name));
}
snprintf(name, sizeof(name), "%s%s",
globbuf.gl_pathv[globbuf.gl_pathc - i - 1], "/stat");
tn = fopen(name, "r");
if (tn == NULL) continue; /* process vanished since glob() */
fscanf(tn, "%ld %s %*c %ld %ld",
&P[j].pid, P[j].cmd, &P[j].ppid, &P[j].pgid);
fclose(tn);
P[j].thcount = 1;
snprintf(name, sizeof(name), "%s%s",
globbuf.gl_pathv[globbuf.gl_pathc - i - 1], "/cmdline");
tn = fopen(name, "r");
if (tn == NULL) continue; /* process vanished since glob() */
while (k < MAXLINE - 1 && EOF != (c = fgetc(tn))) {
P[j].cmd[k++] = c == '\0' ? ' ' : c;
}
if (k > 0) P[j].cmd[k] = '\0';
fclose(tn);
#ifdef DEBUG
if (debug) fprintf(stderr,
"uid=%5ld, name=%8s, pid=%5ld, ppid=%5ld, pgid=%5ld, thcount=%ld, cmd='%s'\n",
P[j].uid, P[j].name, P[j].pid, P[j].ppid, P[j].pgid, P[j].thcount, P[j].cmd);
#endif
P[j].parent = P[j].child = P[j].sister = -1;
P[j].print = FALSE;
j++;
}
globfree(&globbuf);
return j;
}
#endif /* __linux */
static int GetProcesses(void) {
FILE *tn;
int i = 0;
char line[MAXLINE], command[] = PSCMD;
/* file read code contributed by Paul Kern <pkern AT utcc.utoronto.ca> */
if (input != NULL) {
if (strcmp(input, "-") == 0)
tn = stdin;
else if (NULL == (tn = fopen(input,"r"))) {
perror(input);
exit(1);
}
} else {
#ifdef DEBUG
if (debug) fprintf(stderr, "calling '%s'\n", command);
#endif
if (NULL == (tn = (FILE*)popen(command,"r"))) {
perror("Problems with pipe");
exit(1);
}
}
#ifdef DEBUG
if (debug) fprintf(stderr, "popen:errno = %d\n", errno);
#endif
if (NULL == fgets(line, MAXLINE, tn)) { /* Throw away header line */
fprintf(stderr, "No input.\n");
exit(1);
}
#ifdef DEBUG
if (debug) fputs(line, stderr);
#endif
P = malloc(sizeof(struct Proc));
if (P == NULL) {
fprintf(stderr, "Problems with malloc.\n");
exit(1);
}
while (NULL != fgets(line, MAXLINE, tn)) {
int len, num;
len = strlen(line);
#ifdef DEBUG
if (debug) {
fprintf(stderr, "len=%3d ", len);
fputs(line, stderr);
}
#endif
if (len == MAXLINE - 1) { /* line too long, drop remaining stuff */
char tmp[MAXLINE];
while (MAXLINE - 1 == strlen(fgets(tmp, MAXLINE, tn)));
}
P = realloc(P, (i+1) * sizeof(struct Proc));
if (P == NULL) {
fprintf(stderr, "Problems with realloc.\n");
exit(1);
}
memset(&P[i], 0, sizeof(*P));
#ifdef solaris1x
{ /* SunOS allows columns to run together. With the -j option, the CPU
* time used can run into the numeric user id, so make sure there is
* space between these two columns. Also, the order of the desired
* items is different. (L. Mark Larsen <mlarsen AT ptdcs2.intel.com>)
*/
char buf1[45], buf2[MAXLINE];
buf1[44] = '\0';
sscanf(line, "%44c%[^\n]", buf1, buf2);
snprintf(line, sizeof(line), "%s %s", buf1, buf2);
}
#endif
num = sscanf(line, PSFORMAT, PSVARS);
if (num != PSVARSN) {
#ifdef DEBUG
if (debug) fprintf(stderr, "dropped line, num=%d != %d\n", num, PSVARSN);
#endif
continue;
}
#ifdef UID2USER /* get username */
uid2user(P[i].uid, P[i].name, sizeof(P[i].name));
#endif
#ifdef DEBUG
if (debug) fprintf(stderr,
"uid=%5ld, name=%8s, pid=%5ld, ppid=%5ld, pgid=%5ld, thcount=%ld, cmd='%s'\n",
P[i].uid, P[i].name, P[i].pid, P[i].ppid, P[i].pgid, P[i].thcount, P[i].cmd);
#endif
P[i].parent = P[i].child = P[i].sister = -1;
P[i].print = FALSE;
i++;
}
if (input != NULL)
fclose(tn);
else
pclose(tn);
return i;
}
static int GetRootPid(void) {
int me;
for (me = 0; me < NProc; me++) {
if (P[me].pid == 1) return P[me].pid;
}
/* PID == 1 not found, so we'll take process with PPID == 0
* Fix for TRU64 TruCluster with uniq PIDs
* reported by Frank Parkin <fparki AT acxiom.co.uk>
* re-reported by Eric van Doorn <Eric.van.Doorn AT isc.politie.nl>,
* because fix was not published by me :-/ */
for (me = 0; me < NProc; me++) {
if (P[me].ppid == 0) return P[me].pid;
}
/* OK, still nothing found. Maybe it is FreeBSD and won't show foreign
* processes. So we also accept PPID == 1 */
for (me = 0; me < NProc; me++) {
if (P[me].ppid == 1) return P[me].pid;
}
/* Still nothing. Maybe it is something like Solaris Zone. We'll take
* the process with PID == PPID */
for (me = 0; me < NProc; me++) {
if (P[me].pid == P[me].ppid) return P[me].pid;
}
/* Should not happen */
fprintf(stderr,
"%s: No process found with PID == 1 || PPID == 0 || PPID == 1\n"
" || PID == PPID, contact author.\n",
Progname);
exit(1);
}
#ifdef ZOMBIES_HAVE_PID_0
void FixZombies(void) {
int me, num = 0;
for (me = 0; me < NProc; me++) {
if (P[me].pid == 0) num++;
}
if (num > 1) for (me = 0; me < NProc; me++) {
if (P[me].pid == 0 && P[me].ppid != 0 && P[me].ppid != -1) {
P[me].pid = -1;
#ifdef DEBUG
if (debug) fprintf(stderr,
"fixed zombie %s with ppid %ld\n",
P[me].cmd, (long)P[me].ppid);
#endif
}
}
}
#endif
#ifdef PARENT_CYCLE_PID_MINUS_ONE
void FixParentCycle(void) {
int me, num = 0;
for (me = 0; me < NProc; me++) {
if (P[me].pid == -1) num++;
}
if (num > 1) for (me = 0; me < NProc; me++) {
if (P[me].pid == -1) {
P[me].pid -= num;
--num;
#ifdef DEBUG
if (debug) fprintf(stderr,
"changed process %s pid from -1 to %ld\n",
P[me].cmd, (long)P[me].pid);
#endif
}
}
}
#endif
int get_pid_index(long pid) {
int me;
for (me = NProc - 1;me >= 0 && P[me].pid != pid; me--); /* Search process */
return me;
}
#define EXIST(idx) ((idx) != -1)
static void MakeTree(void) {
/* Build the process hierarchy. Every process marks itself as first child
* of it's parent or as sister of first child of it's parent */
int me;
for (me = 0; me < NProc; me++) {
int parent;
parent = get_pid_index(P[me].ppid);
if (parent != me && parent != -1) { /* valid process, not me */
P[me].parent = parent;
if (P[parent].child == -1) /* first child */
P[parent].child = me;
else {
int sister;
for (sister = P[parent].child; EXIST(P[sister].sister); sister = P[sister].sister);
P[sister].sister = me;
}
}
}
}
static void MarkChildren(int me) {
int child;
P[me].print = TRUE;
for (child = P[me].child; EXIST(child); child = P[child].sister)
MarkChildren(child);
}
static void MarkProcs(void) {
int me;
for (me = 0; me < NProc; me++) {
if (showall) {
P[me].print = TRUE;
} else {
int parent;
if (0 == strcmp(P[me].name, name) /* for -u */
|| (Uoption &&
0 != strcmp(P[me].name, "root")) /* for -U */
|| P[me].pid == ipid /* for -p */
|| (soption
&& NULL != strstr(P[me].cmd, str)
&& P[me].pid != MyPid) /* for -s */
) {
/* Mark parents */
for (parent = P[me].parent; EXIST(parent); parent = P[parent].parent) {
P[parent].print = TRUE;
}
/* Mark children */
MarkChildren(me);
}
}
#if 0 /* experimental thread compression */
{
int parent = P[me].parent;
int ancestor; /* oldest parent with same cmd */
if (0 == strcmp(P[me].cmd, P[parent].cmd)) {
P[me].print = FALSE;
for (parent = P[me].parent;
EXIST(parent) && (0 == strcmp(P[me].cmd, P[parent].cmd));
parent = P[parent].parent) {
ancestor = parent;
}
fprintf(stderr, "%d: %d\n",
P[me].pid,
P[ancestor].pid);
P[ancestor].thcount++;
}
}
#endif
}
}
static void DropProcs(void) {
int me;
for (me = 0; me < NProc; me++) if (P[me].print) {
int child, sister;
/* Drop children that won't print */
for (child = P[me].child;
EXIST(child) && !P[child].print; child = P[child].sister);
P[me].child = child;
/* Drop sisters that won't print */
for (sister = P[me].sister;
EXIST(sister) && !P[sister].print; sister = P[sister].sister);
P[me].sister = sister;
}
}
static void PrintTree(int idx, const char *head) {
char nhead[MAXLINE], out[4 * MAXLINE], thread[16] = {'\0'};
int child;
if (head[0] == '\0' && !P[idx].print) return;
/*if (!P[idx].print) return;*/
if (P[idx].thcount > 1) snprintf(thread, sizeof(thread), "[%ld]", P[idx].thcount);
if(atLdepth == maxLdepth) return; /* LOPTION */
++atLdepth; /* LOPTION */
snprintf(out, sizeof(out),
"%s%s%s%s%s%s %05ld %s %s%s" /*" (ch=%d, si=%d, pr=%d)"*/,
C->sg,
head,
head[0] == '\0' ? "" : EXIST(P[idx].sister) ? C->barc : C->barl,
EXIST(P[idx].child) ? C->p : C->s2,
P[idx].pid == P[idx].pgid ? C->pgl : C->npgl,
C->eg,
P[idx].pid, P[idx].name,
thread,
P[idx].cmd
/*,P[idx].child,P[idx].sister,P[idx].print*/);
out[Columns-1] = '\0';
puts(out);
/* Process children */
snprintf(nhead, sizeof(nhead), "%s%s ", head,
head[0] == '\0' ? "" : EXIST(P[idx].sister) ? C->bar : " ");
/*
if ( compress ) {
int c1, c2, flag = 0;
for ( c1 = P[idx].child; EXIST(c1); c1 = P[c1].sister ) {
for ( c2 = P[c1].sister; EXIST(c2); c2 = P[c2].sister ) {
if ( 0 == strcmp(P[c1].cmd, P[c2].cmd) ) {
flag = 1;
printf("%d:%d ", c1, c2);
P[c1].pid = -1;
P[c2].print = FALSE;
}
}
}
if ( flag ) printf("\n");
}
*/
for (child = P[idx].child; EXIST(child); child = P[child].sister) {
PrintTree(child, nhead);
}
--atLdepth; /* LOPTION */
}
static void Usage(void) {
fprintf(stderr,
"%s\n"
"%s\n\n"
"Usage: %s "
#ifdef DEBUG
"[-d] "
#endif
"[-f file] [-g n] [-l n] [-u user] [-U] [-s string] [-p pid] [-w] [pid ...]\n"
/*" -a align output\n"*/
#ifdef DEBUG
" -d print debugging info to stderr\n"
#endif
" -f file read input from <file> (- is stdin) instead of running\n"
" \"%s\"\n"
" -g n use graphics chars for tree. n=1: IBM-850, n=2: VT100, n=3: UTF-8\n"
" -l n print tree to n level deep\n"
" -u user show only branches containing processes of <user>\n"
" -U don't show branches containing only root processes\n"
" -s string show only branches containing process with <string> in commandline\n"
" -p pid show only branches containing process <pid>\n"
" -w wide output, not truncated to window width\n"
" pid ... process ids to start from, default is 1 (probably init)\n"
, WhatString[0] + 4, WhatString[1] + 4, Progname, PSCMD);
#ifdef HAS_PGID
fprintf(stderr, "\n%sProcess group leaders are marked with '%s%s%s'.\n",
C->init, C->sg, C->pgl, C->eg);
#endif
exit(1);
}
int main(int argc, char **argv) {
extern int optind;
extern char *optarg;
int ch;
long pid;
int graph = G_ASCII, wide = FALSE;
C = &TreeChars[graph];
Progname = strrchr(argv[0],'/');
Progname = (NULL == Progname) ? argv[0] : Progname + 1;
while ((ch = getopt(argc, argv, "cdf:g:hl:p:s:u:Uw?")) != EOF)
switch(ch) {
/*case 'a':
align = TRUE;
break;*/
case 'c':
compress = TRUE;
break;
#ifdef DEBUG
case 'd':
debug = TRUE;
break;
#endif
case 'f':
input = optarg;
break;
case 'g':
graph = atoi(optarg);
if (graph < 0 || graph >= G_LAST) {
fprintf(stderr, "%s: Invalid graph parameter.\n",
Progname);
exit(1);
}
C = &TreeChars[graph];
break;
case 'l': /* LOPTION */
maxLdepth = atoi(optarg); /* LOPTION */
if(maxLdepth < 1) maxLdepth = 1; /* LOPTION */
break; /* LOPTION */
case 'p':
showall = FALSE;
ipid = atoi(optarg);
break;
case 's':
showall = FALSE;
soption = TRUE;
str = optarg;
break;
case 'u':
showall = FALSE;
name = optarg;
if (
#ifdef solaris2x
(int)
#endif
NULL == getpwnam(name)) {
fprintf(stderr, "%s: User '%s' does not exist.\n",
Progname, name);
exit(1);
}
break;
case 'U':
showall = FALSE;
Uoption = TRUE;
break;
case 'w':
wide = TRUE;
break;
case 'h':
case '?':
default :
Usage();
break;
}
#ifdef USE_GetProcessesDirect
NProc = input == NULL ? GetProcessesDirect() : GetProcesses();
#else
NProc = GetProcesses();
#endif
#ifdef ZOMBIES_HAVE_PID_0
FixZombies();
#endif
#ifdef PARENT_CYCLE_PID_MINUS_ONE
FixParentCycle();
#endif
if (NProc == 0) {
fprintf(stderr, "%s: No processes read.\n", Progname);
exit(1);
}
#ifdef DEBUG
if (debug) fprintf(stderr, "NProc = %d processes found.\n", NProc);
#endif
RootPid = GetRootPid();
#ifdef DEBUG
if (debug) fprintf(stderr, "RootPid = %d.\n", RootPid);
#endif
#if defined(UID2USER) && defined(DEBUG)
if (debug) uid2user(0,NULL,0);
#endif
MyPid = getpid();
if (wide)
Columns = MAXLINE - 1;
else {
#if defined (HAS_TERMDEF)
Columns = atoi((char*)termdef(fileno(stdout),'c'));
#elif defined(TIOCGWINSZ)
struct winsize winsize;
if ( ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1 )
Columns = winsize.ws_col;
#elif defined(TIOCGSIZE)
struct ttysize ttysize;
if ( ioctl(fileno(stdout), TIOCGSIZE, &ttysize) != -1 )
Columns = ttysize.ts_cols;
#else
char *env = getenv("COLUMNS");
Columns = env ? atoi(env) : 80;
#endif
}
if (Columns == 0) Columns = MAXLINE - 1;
printf("%s", C->init);
Columns += strlen(C->sg) + strlen(C->eg); /* Don't count hidden chars */
if (Columns >= MAXLINE) Columns = MAXLINE - 1;
#ifdef DEBUG
if (debug) fprintf(stderr, "Columns = %d\n", Columns);
#endif
MakeTree();
MarkProcs();
DropProcs();
if (argc == optind) { /* No pids */
PrintTree(get_pid_index(RootPid), "");
} else while (optind < argc) {
int idx;
pid = (long)atoi(argv[optind]);
idx = get_pid_index(pid);
if (idx > -1) PrintTree(idx, "");
optind++;
}
free(P);
return 0;
}
#ifdef NEED_STRSTR
/* Contributed by Paul Kern <pkern AT utcc.utoronto.ca> */
static char * strstr(s1, s2)
register char *s1, *s2;
{
register int n1, n2;
if (n2 = strlen(s2))
for (n1 = strlen(s1); n1 >= n2; s1++, n1--)
if (strncmp(s1, s2, n2) == 0)
return s1;
return NULL;
}
#endif /* NEED_STRSTR */
#ifdef NEED_SNPRINTF
int snprintf (char *s, int namesiz, char *format, ...) {
/* Original portable version by Michael E. White.
This version of Stan Sieler (sieler AT allegro.com) */
int chars_needed; /* not including trailing null */
char bigbuf [1024] = {'\0'}; /* note: 1024 is a guess, and may not be large enough! */
va_list ap; /* some systems allow "va_list ap = NULL;", others *do not* (like MACH) */
va_start (ap, format);
chars_needed = vsprintf (bigbuf, format, ap); /* note: chars_needed does not include trailing null */
va_end (ap);
/* 0 is documented as "don't write anything" ... while not specifically spelled out
(e.g., does it also mean "don't internally call vsprintf"?), one can imply that it simply means
"don't write to the output buffer 's'. (Otherwise, if we didn't call vsprintf, we wouldn't