summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCraig Small <csmall@users.sourceforge.net>2012-05-20 09:59:01 +1000
committerCraig Small <csmall@users.sourceforge.net>2012-05-20 09:59:01 +1000
commit372eb51fdc8d36e373cc1feacf416a9abcee4421 (patch)
treeb068ffe7233cb426c5c09afc620f63367fe67af2
parentf046053911fb2cafa36453114f5dce740fb86326 (diff)
pstree displays PGIDs
Patch based upon a patch submitted by Alan Grow. The -g flag shows program group IDs, much like the PIDs display. Bug-Sourceforge: http://sourceforge.net/tracker/?func=detail&aid=3471056&group_id=15273&atid=315273
-rw-r--r--ChangeLog1
-rw-r--r--doc/pstree.17
-rw-r--r--src/pstree.c46
3 files changed, 36 insertions, 18 deletions
diff --git a/ChangeLog b/ChangeLog
index 1ac7dec..d454282 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -13,6 +13,7 @@ Changes in 22.17 (unreleased)
* No TEXTRELS in src/lists built as PIE on x86
* Fake pstree root for kernels with hidepid turned on
* More fixes for Cygwin SF Patch #3511920
+ * pstree can show PGIDs SF Patch #3471056
Changes in 22.16
================
diff --git a/doc/pstree.1 b/doc/pstree.1
index b30717a..9cfbb97 100644
--- a/doc/pstree.1
+++ b/doc/pstree.1
@@ -1,4 +1,4 @@
-.TH PSTREE 1 2011-02-22 "Linux" "User Commands"
+.TH PSTREE 1 2012-05-20 "Linux" "User Commands"
.SH NAME
pstree \- display a tree of processes
.SH SYNOPSIS
@@ -7,6 +7,7 @@ pstree \- display a tree of processes
.RB [ \-a | \-\-arguments ]
.RB [ \-c | \-\-compact ]
.RB [ \-h | \-\-highlight\-all | \-H \fIpid\fB | \-\-highlight\-pid\ \fIpid\fB ]
+.RB [ \-g ] \-\-show\-pgids ]
.RB [ \-l | \-\-long ]
.RB [ \-n | \-\-numeric\-sort ]
.RB [ \-p | \-\-show\-pids ]
@@ -75,6 +76,10 @@ nor any of its ancestors are in the subtree being shown.
Like \fB\-h\fP, but highlight the specified process instead. Unlike with
\fB\-h\fP, \fBpstree\fP fails when using \fB\-H\fP if highlighting is not
available.
+.IP \fB\-g\fP
+Show PGIDs. Process Group IDs are shown as decimal numbers in parentheses
+after each process name. \fB\-p\fP implicitly disables compaction. If both
+PIDs and PGIDs are displayed then PIDs are shown first.
.IP \fB\-l\fP
Display long lines. By default, lines are truncated to the display width or
132 if output is sent to a non-tty or if the display width is unknown.
diff --git a/src/pstree.c b/src/pstree.c
index 6d821a4..dd7045b 100644
--- a/src/pstree.c
+++ b/src/pstree.c
@@ -2,7 +2,7 @@
* pstree.c - display process tree
*
* Copyright (C) 1993-2002 Werner Almesberger
- * Copyright (C) 2002-2009 Craig Small
+ * Copyright (C) 2002-2012 Craig Small
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -73,6 +73,7 @@ typedef struct _proc {
char **argv; /* only used : argv[0] is 1st arg; undef if argc < 1 */
int argc; /* with -a : number of arguments, -1 if swapped */
pid_t pid;
+ pid_t pgid;
uid_t uid;
#ifdef WITH_SELINUX
security_context_t scontext;
@@ -122,7 +123,7 @@ static int capacity = 0;
static int *width = NULL;
static int *more = NULL;
-static int print_args = 0, compact = 1, user_change = 0, pids = 0,
+static int print_args = 0, compact = 1, user_change = 0, pids = 0, pgids = 0,
show_parents = 0, by_pid = 0, trunc = 1, wait_end = 0;
#ifdef WITH_SELINUX
static int show_scontext = 0;
@@ -336,11 +337,11 @@ static void set_args(PROC * this, const char *args, int size)
#ifdef WITH_SELINUX
static void
-add_proc(const char *comm, pid_t pid, pid_t ppid, uid_t uid,
+add_proc(const char *comm, pid_t pid, pid_t ppid, pid_t pgid, uid_t uid,
const char *args, int size, char isthread, security_context_t scontext)
#else /*WITH_SELINUX */
static void
-add_proc(const char *comm, pid_t pid, pid_t ppid, uid_t uid,
+add_proc(const char *comm, pid_t pid, pid_t ppid, pid_t pgid, uid_t uid,
const char *args, int size, char isthread)
#endif /*WITH_SELINUX */
{
@@ -361,6 +362,7 @@ add_proc(const char *comm, pid_t pid, pid_t ppid, uid_t uid,
set_args(this, args, size);
if (pid == ppid)
ppid = 0;
+ this->pgid = pgid;
if (isthread)
this->flags |= PFLAG_THREAD;
if (!(parent = find_proc(ppid))) {
@@ -466,6 +468,10 @@ dump_tree(PROC * current, int level, int rep, int leaf, int last,
out_char(info++ ? ',' : '(');
(void) out_int(current->pid);
}
+ if (pgids) {
+ out_char(info++ ? ',' : '(');
+ (void) out_int(current->pgid);
+ }
if (user_change && prev_uid != current->uid) {
out_char(info++ ? ',' : '(');
if ((pw = getpwuid(current->uid)))
@@ -629,7 +635,7 @@ static void read_proc(void)
size_t buffer_size;
char readbuf[BUFSIZ + 1];
char *tmpptr;
- pid_t pid, ppid;
+ pid_t pid, ppid, pgid;
int fd, size;
int empty;
#ifdef WITH_SELINUX
@@ -684,7 +690,7 @@ static void read_proc(void)
/* We now have readbuf with pid and cmd, and tmpptr+2
* with the rest */
/*printf("tmpptr: %s\n", tmpptr+2); */
- if (sscanf(tmpptr + 2, "%*c %d", &ppid) == 1) {
+ if (sscanf(tmpptr + 2, "%*c %d %d", &ppid, &pgid) == 2) {
DIR *taskdir;
struct dirent *dt;
char *taskpath;
@@ -706,17 +712,17 @@ static void read_proc(void)
if (thread != pid) {
#ifdef WITH_SELINUX
if (print_args)
- add_proc(threadname, thread, pid, st.st_uid,
+ add_proc(threadname, thread, pid, pgid, st.st_uid,
threadname, strlen (threadname) + 1, 1,scontext);
else
- add_proc(threadname, thread, pid, st.st_uid,
+ add_proc(threadname, thread, pid, pgid, st.st_uid,
NULL, 0, 1, scontext);
#else /*WITH_SELINUX */
if (print_args)
- add_proc(threadname, thread, pid, st.st_uid,
+ add_proc(threadname, thread, pid, pgid, st.st_uid,
threadname, strlen (threadname) + 1, 1);
else
- add_proc(threadname, thread, pid, st.st_uid,
+ add_proc(threadname, thread, pid, pgid, st.st_uid,
NULL, 0, 1);
#endif /*WITH_SELINUX */
}
@@ -728,9 +734,9 @@ static void read_proc(void)
free(taskpath);
if (!print_args)
#ifdef WITH_SELINUX
- add_proc(comm, pid, ppid, st.st_uid, NULL, 0, 0, scontext);
+ add_proc(comm, pid, ppid, pgid, st.st_uid, NULL, 0, 0, scontext);
#else /*WITH_SELINUX */
- add_proc(comm, pid, ppid, st.st_uid, NULL, 0, 0);
+ add_proc(comm, pid, ppid, pgid, st.st_uid, NULL, 0, 0);
#endif /*WITH_SELINUX */
else {
sprintf(path, "%s/%d/cmdline", PROC_BASE, pid);
@@ -749,10 +755,10 @@ static void read_proc(void)
if (size)
buffer[size++] = 0;
#ifdef WITH_SELINUX
- add_proc(comm, pid, ppid, st.st_uid,
+ add_proc(comm, pid, ppid, pgid, st.st_uid,
buffer, size, 0, scontext);
#else /*WITH_SELINUX */
- add_proc(comm, pid, ppid, st.st_uid,
+ add_proc(comm, pid, ppid, pgid, st.st_uid,
buffer, size, 0);
#endif /*WITH_SELINUX */
}
@@ -805,7 +811,7 @@ static void usage(void)
{
fprintf(stderr,
_
- ("Usage: pstree [ -a ] [ -c ] [ -h | -H PID ] [ -l ] [ -n ] [ -p ] [ -u ]\n"
+ ("Usage: pstree [ -a ] [ -c ] [ -h | -H PID ] [ -l ] [ -n ] [ -p ] [ -g ] [ -u ]\n"
" [ -A | -G | -U ] [ PID | USER ]\n"
" pstree -V\n" "Display a tree of processes.\n\n"
" -a, --arguments show command line arguments\n"
@@ -814,6 +820,7 @@ static void usage(void)
" -h, --highlight-all highlight current process and its ancestors\n"
" -H PID,\n"
" --highlight-pid=PID highlight this process and its ancestors\n"
+ " -g, --show-pgids show process group ids; implies -c\n"
" -G, --vt100 use VT100 line drawing characters\n"
" -l, --long don't truncate long lines\n"
" -n, --numeric-sort sort output by PID\n"
@@ -865,6 +872,7 @@ int main(int argc, char **argv)
{"long", 0, NULL, 'l'},
{"numeric-sort", 0, NULL, 'n'},
{"show-pids", 0, NULL, 'p'},
+ {"show-pgids", 0, NULL, 'g'},
{"show-parents", 0, NULL, 's'},
{"uid-changes", 0, NULL, 'u'},
{"unicode", 0, NULL, 'U'},
@@ -918,11 +926,11 @@ int main(int argc, char **argv)
#ifdef WITH_SELINUX
while ((c =
- getopt_long(argc, argv, "aAcGhH:nplsuUVZ", options,
+ getopt_long(argc, argv, "aAcGhH:npglsuUVZ", options,
NULL)) != -1)
#else /*WITH_SELINUX */
while ((c =
- getopt_long(argc, argv, "aAcGhH:nplsuUV", options, NULL)) != -1)
+ getopt_long(argc, argv, "aAcGhH:npglsuUV", options, NULL)) != -1)
#endif /*WITH_SELINUX */
switch (c) {
case 'a':
@@ -968,6 +976,10 @@ int main(int argc, char **argv)
pids = 1;
compact = 0;
break;
+ case 'g':
+ pgids = 1;
+ compact = 0;
+ break;
case 's':
show_parents = 1;
break;