summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoredd <edd@openbsd.org>2016-05-11 08:11:27 +0000
committeredd <edd@openbsd.org>2016-05-11 08:11:27 +0000
commitbeb9a05bc3e8506e0a13ee55d20bc36bab53288d (patch)
tree02efdeee11df0f7c475f872d8775a3e34a8fde19
parentDont reuse "mult" local variable. Use separate variable for hardware (diff)
downloadwireguard-openbsd-beb9a05bc3e8506e0a13ee55d20bc36bab53288d.tar.xz
wireguard-openbsd-beb9a05bc3e8506e0a13ee55d20bc36bab53288d.zip
Allow top(1) to filter process args if they are being displayed.
OK tedu@, deraadt@, and with input from Michal Mazurek. Thanks
-rw-r--r--usr.bin/top/machine.c85
-rw-r--r--usr.bin/top/top.16
2 files changed, 69 insertions, 22 deletions
diff --git a/usr.bin/top/machine.c b/usr.bin/top/machine.c
index 9c6aaea20af..72056253d51 100644
--- a/usr.bin/top/machine.c
+++ b/usr.bin/top/machine.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: machine.c,v 1.85 2015/08/20 22:32:42 deraadt Exp $ */
+/* $OpenBSD: machine.c,v 1.86 2016/05/11 08:11:27 edd Exp $ */
/*-
* Copyright (c) 1994 Thorsten Lockert <tholo@sigmasoft.com>
@@ -57,6 +57,8 @@
static int swapmode(int *, int *);
static char *state_abbr(struct kinfo_proc *);
static char *format_comm(struct kinfo_proc *);
+static int cmd_matches(struct kinfo_proc *, char *);
+static char **get_proc_args(struct kinfo_proc *);
/* get_process_info passes back a handle. This is what it looks like: */
@@ -360,6 +362,60 @@ getprocs(int op, int arg, int *cnt)
return (procbase);
}
+static char **
+get_proc_args(struct kinfo_proc *kp)
+{
+ static char **s;
+ size_t siz = 100;
+ int mib[4];
+
+ for (;; siz *= 2) {
+ if ((s = realloc(s, siz)) == NULL)
+ err(1, NULL);
+ mib[0] = CTL_KERN;
+ mib[1] = KERN_PROC_ARGS;
+ mib[2] = kp->p_pid;
+ mib[3] = KERN_PROC_ARGV;
+ if (sysctl(mib, 4, s, &siz, NULL, 0) == 0)
+ break;
+ if (errno != ENOMEM)
+ return NULL;
+ }
+ return s;
+}
+
+static int
+cmd_matches(struct kinfo_proc *proc, char *term)
+{
+ extern int show_args;
+ char **args = NULL;
+
+ if (!term) {
+ /* No command filter set */
+ return 1;
+ } else {
+ /* Filter set, process name needs to contain term */
+ if (strstr(proc->p_comm, term))
+ return 1;
+ /* If showing arguments, search those as well */
+ if (show_args) {
+ args = get_proc_args(proc);
+
+ if (args == NULL) {
+ /* Failed to get args, so can't search them */
+ return 0;
+ }
+
+ while (*args != NULL) {
+ if (strstr(*args, term))
+ return 1;
+ args++;
+ }
+ }
+ }
+ return 0;
+}
+
caddr_t
get_process_info(struct system_info *si, struct process_select *sel,
int (*compare) (const void *, const void *))
@@ -421,8 +477,7 @@ get_process_info(struct system_info *si, struct process_select *sel,
(!hide_uid || pp->p_ruid != sel->huid) &&
(!show_uid || pp->p_ruid == sel->uid) &&
(!show_pid || pp->p_pid == sel->pid) &&
- (!show_cmd || strstr(pp->p_comm,
- sel->command))) {
+ (!show_cmd || cmd_matches(pp, sel->command))) {
*prefp++ = pp;
active_procs++;
}
@@ -462,27 +517,17 @@ state_abbr(struct kinfo_proc *pp)
static char *
format_comm(struct kinfo_proc *kp)
{
- static char **s, buf[MAX_COLS];
- size_t siz = 100;
- char **p;
- int mib[4];
- extern int show_args;
+ static char buf[MAX_COLS];
+ char **p, **s;
+ extern int show_args;
if (!show_args)
return (kp->p_comm);
- for (;; siz *= 2) {
- if ((s = realloc(s, siz)) == NULL)
- err(1, NULL);
- mib[0] = CTL_KERN;
- mib[1] = KERN_PROC_ARGS;
- mib[2] = kp->p_pid;
- mib[3] = KERN_PROC_ARGV;
- if (sysctl(mib, 4, s, &siz, NULL, 0) == 0)
- break;
- if (errno != ENOMEM)
- return (kp->p_comm);
- }
+ s = get_proc_args(kp);
+ if (s == NULL)
+ return kp->p_comm;
+
buf[0] = '\0';
for (p = s; *p != NULL; p++) {
if (p != s)
diff --git a/usr.bin/top/top.1 b/usr.bin/top/top.1
index c13a98b171d..47c36ac4184 100644
--- a/usr.bin/top/top.1
+++ b/usr.bin/top/top.1
@@ -1,4 +1,4 @@
-.\" $OpenBSD: top.1,v 1.66 2015/05/06 07:53:29 mpi Exp $
+.\" $OpenBSD: top.1,v 1.67 2016/05/11 08:11:27 edd Exp $
.\"
.\" Copyright (c) 1997, Jason Downs. All rights reserved.
.\"
@@ -23,7 +23,7 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
-.Dd $Mdocdate: May 6 2015 $
+.Dd $Mdocdate: May 11 2016 $
.Dt TOP 1
.Os
.Sh NAME
@@ -108,6 +108,7 @@ The default is 1 for dumb terminals.
Display only processes that contain
.Ar string
in their command name.
+If displaying of arguments is enabled, the arguments are searched too.
.It Fl H
Show process threads in the display.
Normally, only the main process is shown.
@@ -306,6 +307,7 @@ command.
Display only processes that contain
.Ar string
in their command name.
+If displaying of arguments is enabled, the arguments are searched too.
.Sq g+
shows all processes.
.It H