Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 26 Nov 2012 02:06:29 +0200
From:      Nikos Vassiliadis <nvass@gmx.com>
To:        freebsd-hackers@freebsd.org
Subject:   "not" user display in top
Message-ID:  <50B2B285.2020008@gmx.com>

next in thread | raw e-mail | index | archive | help
This is a multi-part message in MIME format.
--------------020603090909080604080608
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

Hi,

  The attached patch adds 'not' functionality to the interactive u command
in top so one can select all processes not owned by a user. This happens
when the username is prefixed with a minus. Example display for -root:

> CPU:  0.0% user,  0.0% nice,  0.1% system,  0.0% interrupt, 99.9% idle
> Mem: 16M Active, 108M Inact, 150M Wired, 3760K Cache, 112M Buf, 1210M Free
> Swap: 288M Total, 288M Free
>
>   PID USERNAME  THR PRI NICE   SIZE    RES STATE   C   TIME   WCPU COMMAND
>   794 smmsp       1  20    0 12812K  4284K pause   1   0:00  0.00% sendmail
>   566 _dhcp       1  39    0 10136K  1848K select  0   0:00  0.00% dhclient
>

Thanks, Nikos


--------------020603090909080604080608
Content-Type: text/plain; charset=windows-1252;
 name="top.diff.txt"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
 filename="top.diff.txt"

Index: contrib/top/machine.h
===================================================================
--- contrib/top/machine.h	(revision 243514)
+++ contrib/top/machine.h	(working copy)
@@ -65,6 +65,7 @@
     int system;		/* show system processes */
     int thread;		/* show threads */
     int uid;		/* only this uid (unless uid == -1) */
+    int buid;		/* all but this uid (unless buid == -1) */
     int wcpu;		/* show weighted cpu */
     int jail;		/* show jail ID */
     int kidle;		/* show per-CPU idle threads */
Index: contrib/top/top.X
===================================================================
--- contrib/top/top.X	(revision 243514)
+++ contrib/top/top.X	(working copy)
@@ -286,8 +286,10 @@
 .TP
 .B u
 Display only processes owned by a specific username (prompt for username).
-If the username specified is simply \*(lq+\*(rq, then processes belonging
-to all users will be displayed.
+If the username specified is simply \*(lq+\*(rq or \*(lq-\*(rq, then processes
+belonging to all users will be displayed.
+If the username is prefixed by a \*(lq-\*(rq, then only processes not owned
+by the username will be displayed.
 .TP
 .B o
 Change the order in which the display is sorted.  This command is not
Index: contrib/top/top.c
===================================================================
--- contrib/top/top.c	(revision 243514)
+++ contrib/top/top.c	(working copy)
@@ -259,6 +259,7 @@
     ps.self    = -1;
     ps.system  = No;
     ps.uid     = -1;
+    ps.buid    = -1;
     ps.thread  = No;
     ps.wcpu    = 1;
     ps.jail    = No;
@@ -997,20 +998,32 @@
 				    "Username to show: ");
 				if (readline(tempbuf2, sizeof(tempbuf2), No) > 0)
 				{
-				    if (tempbuf2[0] == '+' &&
+				    if ((tempbuf2[0] == '+' || tempbuf2[0] == '-') &&
 					tempbuf2[1] == '\0')
 				    {
 					ps.uid = -1;
+					ps.buid = -1;
 				    }
+				    else if (tempbuf2[0] == '-')
+				    {
+					if ((i = userid(tempbuf2 + 1)) == -1)
+					{
+					    new_message(MT_standout,
+						" %s: unknown user", tempbuf2 + 1);
+					    no_command = Yes;
+					} else {
+					    ps.uid = -1;
+					    ps.buid = i;
+					}
+				    }
 				    else if ((i = userid(tempbuf2)) == -1)
 				    {
 					new_message(MT_standout,
 					    " %s: unknown user", tempbuf2);
 					no_command = Yes;
-				    }
-				    else
-				    {
+				    } else {
 					ps.uid = i;
+					ps.buid = -1;
 				    }
 				    putchar('\r');
 				}
Index: usr.bin/top/machine.c
===================================================================
--- usr.bin/top/machine.c	(revision 243514)
+++ usr.bin/top/machine.c	(working copy)
@@ -671,6 +671,7 @@
 	int show_self;
 	int show_system;
 	int show_uid;
+	int show_buid;
 	int show_command;
 	int show_kidle;
 
@@ -713,6 +714,7 @@
 	show_self = sel->self == -1;
 	show_system = sel->system;
 	show_uid = sel->uid != -1;
+	show_buid = sel->buid != -1;
 	show_command = sel->command != NULL;
 	show_kidle = sel->kidle;
 
@@ -768,6 +770,10 @@
 			/* skip proc. that don't belong to the selected UID */
 			continue;
 
+		if (show_buid && pp->ki_ruid == (uid_t)sel->buid)
+			/* skip proc. that belong to the selected UID */
+			continue;
+
 		*prefp++ = pp;
 		active_procs++;
 	}

--------------020603090909080604080608--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?50B2B285.2020008>