diff options
author | Otto Moerbeek <otto@cvs.openbsd.org> | 2004-10-07 06:26:13 +0000 |
---|---|---|
committer | Otto Moerbeek <otto@cvs.openbsd.org> | 2004-10-07 06:26:13 +0000 |
commit | 2bccf344d8a3f9f34752e51ac50df53503c76f94 (patch) | |
tree | 0a07b2d089a5058b9680ac02f8bc303ab79cd6d9 | |
parent | 21dda97f12ab9e6f16723ca5550c186a3b677b32 (diff) |
introduce -p option and p command to only show a single process.
From Patrick Latifi. ok deraadt@ millert@
-rw-r--r-- | usr.bin/top/commands.c | 5 | ||||
-rw-r--r-- | usr.bin/top/machine.c | 10 | ||||
-rw-r--r-- | usr.bin/top/machine.h | 5 | ||||
-rw-r--r-- | usr.bin/top/top.1 | 11 | ||||
-rw-r--r-- | usr.bin/top/top.c | 61 | ||||
-rw-r--r-- | usr.bin/top/utils.c | 21 | ||||
-rw-r--r-- | usr.bin/top/utils.h | 3 |
7 files changed, 100 insertions, 16 deletions
diff --git a/usr.bin/top/commands.c b/usr.bin/top/commands.c index 99a5ec27bd7..a4a6f58b10b 100644 --- a/usr.bin/top/commands.c +++ b/usr.bin/top/commands.c @@ -1,4 +1,4 @@ -/* $OpenBSD: commands.c,v 1.15 2004/06/27 00:48:22 marc Exp $ */ +/* $OpenBSD: commands.c,v 1.16 2004/10/07 06:26:12 otto Exp $ */ /* * Top users/processes display for Unix @@ -92,7 +92,8 @@ show_help(void) "k - kill processes; send a signal to a list of processes\n" "n or # - change number of processes to display\n", stdout); fputs( - "o - specify sort order (size, res, cpu, time)\n", + "o - specify sort order (size, res, cpu, time)\n" + "p - display one process (+ selects all processes)\n", stdout); fputs( "r - renice a process\n" diff --git a/usr.bin/top/machine.c b/usr.bin/top/machine.c index 62c4cf9b2b5..49aa0ebbcc8 100644 --- a/usr.bin/top/machine.c +++ b/usr.bin/top/machine.c @@ -1,4 +1,4 @@ -/* $OpenBSD: machine.c,v 1.43 2004/06/13 18:49:02 otto Exp $ */ +/* $OpenBSD: machine.c,v 1.44 2004/10/07 06:26:12 otto Exp $ */ /*- * Copyright (c) 1994 Thorsten Lockert <tholo@sigmasoft.com> @@ -264,7 +264,7 @@ get_system_info(struct system_info *si) static struct handle handle; -static struct kinfo_proc2 * +struct kinfo_proc2 * getprocs(int op, int arg, int *cnt) { size_t size; @@ -307,7 +307,7 @@ caddr_t get_process_info(struct system_info *si, struct process_select *sel, int (*compare) (const void *, const void *)) { - int show_idle, show_system, show_uid; + int show_idle, show_system, show_uid, show_pid; int total_procs, active_procs, i; struct kinfo_proc2 **prefp, *pp; @@ -329,6 +329,7 @@ get_process_info(struct system_info *si, struct process_select *sel, show_idle = sel->idle; show_system = sel->system; show_uid = sel->uid != (uid_t)-1; + show_pid = sel->pid != (pid_t)-1; /* count up process states and get pointers to interesting procs */ total_procs = 0; @@ -349,7 +350,8 @@ get_process_info(struct system_info *si, struct process_select *sel, if (pp->p_stat != SZOMB && (show_idle || pp->p_pctcpu != 0 || pp->p_stat == SRUN) && - (!show_uid || pp->p_ruid == sel->uid)) { + (!show_uid || pp->p_ruid == sel->uid) && + (!show_pid || pp->p_pid == sel->pid)) { *prefp++ = pp; active_procs++; } diff --git a/usr.bin/top/machine.h b/usr.bin/top/machine.h index ff4cd478d7e..5a7adc8097c 100644 --- a/usr.bin/top/machine.h +++ b/usr.bin/top/machine.h @@ -1,4 +1,4 @@ -/* $OpenBSD: machine.h,v 1.11 2004/06/13 18:49:02 otto Exp $ */ +/* $OpenBSD: machine.h,v 1.12 2004/10/07 06:26:12 otto Exp $ */ /* * Top users/processes display for Unix @@ -73,6 +73,7 @@ struct process_select { int idle; /* show idle processes */ int system; /* show system processes */ uid_t uid; /* only this uid (unless uid == -1) */ + pid_t pid; /* only this pid (unless pid == -1) */ char *command;/* only this command (unless == NULL) */ }; @@ -88,3 +89,5 @@ get_process_info(struct system_info *, struct process_select *, int (*) (const void *, const void *)); extern char *format_next_process(caddr_t, char *(*)(uid_t)); extern uid_t proc_owner(pid_t); + +extern struct kinfo_proc2 *getprocs(int, int, int *); diff --git a/usr.bin/top/top.1 b/usr.bin/top/top.1 index 4857c5d647d..d0c415b2afa 100644 --- a/usr.bin/top/top.1 +++ b/usr.bin/top/top.1 @@ -1,4 +1,4 @@ -.\" $OpenBSD: top.1,v 1.24 2004/05/10 20:26:08 millert Exp $ +.\" $OpenBSD: top.1,v 1.25 2004/10/07 06:26:12 otto Exp $ .\" .\" Copyright (c) 1997, Jason Downs. All rights reserved. .\" @@ -34,6 +34,7 @@ .Op Fl biInqSu .Op Fl d Ar count .Op Fl o Ar field +.Op Fl p Ar pid .Op Fl s Ar time .Op Fl U Ar username .Op Ar number @@ -127,6 +128,9 @@ version of top supports .Ar time , and .Ar pri . +.It Fl p Ar pid +Show only the process +.Ar pid . .It Fl q Renice .Nm @@ -271,6 +275,11 @@ Change the sorting order of the processes Values are the same as for the .Fl o flag, as detailed above. +.It p +Display a specific process (prompt for pid). +If the pid specified is simply +.Dq + , +then processes belonging to all users will be displayed. .It r Change the priority (the .Em nice ) diff --git a/usr.bin/top/top.c b/usr.bin/top/top.c index 8c62a129883..b53d0e1cda5 100644 --- a/usr.bin/top/top.c +++ b/usr.bin/top/top.c @@ -1,4 +1,4 @@ -/* $OpenBSD: top.c,v 1.34 2004/09/14 22:55:48 deraadt Exp $ */ +/* $OpenBSD: top.c,v 1.35 2004/10/07 06:26:12 otto Exp $ */ /* * Top users/processes display for Unix @@ -40,6 +40,7 @@ const char copyright[] = "Copyright (c) 1984 through 1996, William LeFebvre"; #include <string.h> #include <poll.h> #include <stdlib.h> +#include <limits.h> #include <unistd.h> /* includes specific to top */ @@ -94,6 +95,7 @@ double delay = Default_DELAY; char *order_name = NULL; int topn = Default_TOPN; int no_command = Yes; +int old_system = No; #if Default_TOPN == Infinity char topn_specified = No; @@ -121,6 +123,7 @@ char topn_specified = No; #define CMD_user 14 #define CMD_system 15 #define CMD_order 16 +#define CMD_pid 17 static void usage(void) @@ -128,7 +131,7 @@ usage(void) extern char *__progname; fprintf(stderr, - "usage: %s [-biInqSu] [-d count] [-o field] [-s time] [-U username] [number]\n", + "usage: %s [-biInqSu] [-d count] [-o field] [-p pid] [-s time] [-U username] [number]\n", __progname); } @@ -138,7 +141,7 @@ parseargs(int ac, char **av) char *endp; int i; - while ((i = getopt(ac, av, "SIbinqus:d:U:o:")) != -1) { + while ((i = getopt(ac, av, "SIbinqus:d:p:U:o:")) != -1) { switch (i) { case 'u': /* toggle uid/username display */ do_unames = !do_unames; @@ -151,8 +154,23 @@ parseargs(int ac, char **av) } break; + case 'p': { /* display only process id */ + unsigned long long num; + const char *errstr; + + num = strtonum(optarg, 0, INT_MAX, &errstr); + if (errstr != NULL || !find_pid(num)) { + fprintf(stderr, "%s: unknown pid\n", optarg); + exit(1); + } + ps.pid = (pid_t)num; + ps.system = Yes; + break; + } + case 'S': /* show system processes */ - ps.system = !ps.system; + ps.system = Yes; + old_system = Yes; break; case 'I': /* show idle processes */ @@ -249,6 +267,7 @@ main(int argc, char *argv[]) ps.idle = Yes; ps.system = No; ps.uid = (uid_t)-1; + ps.pid = (pid_t)-1; ps.command = NULL; /* get preset options from the environment */ @@ -492,7 +511,7 @@ rundisplay(void) int change, i; struct pollfd pfd[1]; uid_t uid; - static char command_chars[] = "\f qh?en#sdkriIuSo"; + static char command_chars[] = "\f qh?en#sdkriIuSop"; /* * assume valid command unless told @@ -772,6 +791,7 @@ rundisplay(void) case CMD_system: ps.system = !ps.system; + old_system = ps.system; new_message(MT_standout | MT_delayed, " %sisplaying system processes.", ps.system ? "D" : "Not d"); @@ -796,6 +816,37 @@ rundisplay(void) clear_message(); break; + case CMD_pid: + new_message(MT_standout, "Process id to show: "); + if (readline(tempbuf2, sizeof(tempbuf2), No) > 0) { + if (tempbuf2[0] == '+' && + tempbuf2[1] == '\0') { + ps.pid = (pid_t)-1; + ps.system = old_system; + } else { + unsigned long long num; + const char *errstr; + + num = strtonum(tempbuf2, 0, INT_MAX, + &errstr); + if (errstr != NULL || !find_pid(num)) { + new_message(MT_standout, + " %s: unknown pid", + tempbuf2); + no_command = Yes; + } else { + if (ps.system == No) + old_system = No; + ps.pid = (pid_t)num; + ps.system = Yes; + } + } + if (putchar('\r') == EOF) + exit(1); + } else + clear_message(); + break; + default: new_message(MT_standout, " BAD CASE IN SWITCH!"); if (putchar('\r') == EOF) diff --git a/usr.bin/top/utils.c b/usr.bin/top/utils.c index 7a6ec4f2e28..adae8630f56 100644 --- a/usr.bin/top/utils.c +++ b/usr.bin/top/utils.c @@ -1,4 +1,4 @@ -/* $OpenBSD: utils.c,v 1.13 2003/07/07 21:36:52 deraadt Exp $ */ +/* $OpenBSD: utils.c,v 1.14 2004/10/07 06:26:12 otto Exp $ */ /* * Top users/processes display for Unix @@ -32,13 +32,15 @@ * This file contains various handy utilities used by top. */ -#include <sys/types.h> +#include <sys/param.h> +#include <sys/sysctl.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #include "top.h" +#include "machine.h" #include "utils.h" int @@ -328,3 +330,18 @@ format_k(int amt) snprintf(ret, sizeof(retarray[0]), "%d%c", amt, tag); return (ret); } + +int +find_pid(pid_t pid) +{ + struct kinfo_proc2 *pbase, *cur; + int i, nproc; + + if ((pbase = getprocs(KERN_PROC_KTHREAD, 0, &nproc)) == NULL) + quit(23); + + for (i = 0, cur = pbase; i < nproc; i++, cur++) + if (cur->p_pid == pid) + return 1; + return 0; +} diff --git a/usr.bin/top/utils.h b/usr.bin/top/utils.h index 5c411f47267..4d0261da47d 100644 --- a/usr.bin/top/utils.h +++ b/usr.bin/top/utils.h @@ -1,4 +1,4 @@ -/* $OpenBSD: utils.h,v 1.6 2003/06/19 22:40:45 millert Exp $ */ +/* $OpenBSD: utils.h,v 1.7 2004/10/07 06:26:12 otto Exp $ */ /* * Top users/processes display for Unix @@ -39,3 +39,4 @@ char **argparse(char *, int *); int percentages(int, int *, long *, long *, long *); char *format_time(time_t); char *format_k(int); +int find_pid(pid_t); |