Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 28 Nov 2012 15:59:55 GMT
From:      Nikos Vassiliadis <nvass@gmx.com>
To:        freebsd-gnats-submit@FreeBSD.org
Subject:   bin/173973: [patch] 'not function' for user display for top(1)
Message-ID:  <201211281559.qASFxtFB057005@red.freebsd.org>
Resent-Message-ID: <201211281610.qASGA0gr047405@freefall.freebsd.org>

next in thread | raw e-mail | index | archive | help

>Number:         173973
>Category:       bin
>Synopsis:       [patch] 'not function' for user display for top(1)
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Wed Nov 28 16:10:00 UTC 2012
>Closed-Date:
>Last-Modified:
>Originator:     Nikos Vassiliadis
>Release:        FreeBSD 10.0-CURRENT
>Organization:
>Environment:
FreeBSD lab.local 10.0-CURRENT FreeBSD 10.0-CURRENT #3 r243503: Sun Nov 25 11:44:20 EET 2012     root@lab.local:/usr/obj/usr/src/sys/RCTLLAB  i386
>Description:
It's sometimes useful to be able to ignore the processes of a specific user in top(1). This is might be especially true for system processes owned by root.
 
Regular top(1) display:
last pid:  3242;  load averages:  0.05,  0.04,  0.00    up 0+05:43:03  17:53:11
24 processes:  1 running, 23 sleeping
CPU:  0.1% user,  0.0% nice,  0.0% system,  0.1% interrupt, 99.7% idle
Mem: 16M Active, 128M Inact, 151M Wired, 112M Buf, 1193M Free
Swap: 288M Total, 288M Free

  PID USERNAME  THR PRI NICE   SIZE    RES STATE   C   TIME   WCPU COMMAND
  798 root        1  20    0 10060K  1792K nanslp  2   0:05  0.00% cron
  861 root        1  20    0 17292K  5652K select  1   0:02  0.00% sshd
  791 root        1  20    0 12812K  4272K select  1   0:01  0.00% sendmail
  864 root        1  20    0 11772K  3648K pause   1   0:00  0.00% csh
 3169 root        1  20    0 17292K  5556K select  1   0:00  0.00% sshd
  854 root        1  52    0 10020K  1632K ttyin   0   0:00  0.00% getty
  856 root        1  52    0 10020K  1632K ttyin   2   0:00  0.00% getty
  690 root        1  20    0 10032K  1708K select  2   0:00  0.00% syslogd
 3172 root        1  32    0 11772K  3372K pause   1   0:00  0.00% csh
 3187 root        1  20    0 10112K  1820K select  2   0:00  0.00% inetd
  147 root        1  52    0  9912K  1448K pause   0   0:00  0.00% adjkerntz
  794 smmsp       1  20    0 12812K  4284K pause   2   0:00  0.00% sendmail
 3242 root        1  20    0 11168K  2264K CPU1    2   0:00  0.00% top
  788 root        1  20    0 14528K  4980K select  2   0:00  0.00% sshd
  858 root        1  52    0 10020K  1632K ttyin   2   0:00  0.00% getty
[... the list of processes goes on...]

>How-To-Repeat:
n/a

>Fix:
The attached patch expands the 'u' function in the interactive display so the user can select processes owned by all users but one. The minus character is used to select the excluded user.

Example display for all processes not owned by root:
last pid:  3242;  load averages:  0.02,  0.03,  0.00    up 0+05:43:55  17:54:03
24 processes:  1 running, 23 sleeping
CPU:  0.0% user,  0.0% nice,  0.0% system,  0.0% interrupt,  100% idle
Mem: 16M Active, 128M Inact, 151M Wired, 112M Buf, 1193M 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   2   0:00  0.00% sendmail
  566 _dhcp       1  20    0 10136K  1848K select  2   0:00  0.00% dhclient

Top's display now is short, it fits in only a few rows.


Patch attached with submission follows:

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


>Release-Note:
>Audit-Trail:
>Unformatted:



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