"not" user display in top

Nikos Vassiliadis nvass at gmx.com
Mon Nov 26 00:06:38 UTC 2012


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

-------------- next part --------------
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++;
 	}


More information about the freebsd-hackers mailing list