2 * Top users/processes display for Unix
5 * This program may be freely redistributed,
6 * but this entire comment MUST remain intact.
8 * Copyright (c) 1984, 1989, William LeFebvre, Rice University
9 * Copyright (c) 1989, 1990, 1992, William LeFebvre, Northwestern University
15 * This file contains the routines that implement some of the interactive
16 * mode commands. Note that some of the commands are implemented in-line
17 * in "main". This is necessary because they change the global state of
18 * "top" (i.e.: changing the number of processes to display).
24 #include <sys/resource.h>
32 #include "sigdesc.h" /* generated automatically */
40 extern char *copyright;
42 /* imported from screen.c */
43 extern int overstrike;
47 static int str_adderr(char *str, int len, int err);
48 static int str_addarg(char *str, int len, char *arg, int first);
51 * show_help() - display the help screen; invoked in response to
59 printf("Top version %s, %s\n", version_string(), copyright);
61 A top users display for Unix\n\
63 These single-character commands are available:\n\
67 h or ? - help; show this text\n", stdout);
69 /* not all commands are availalbe with overstrike terminals */
73 Other commands are also available, but this terminal is not\n\
74 sophisticated enough to handle those commands gracefully.\n\n", stdout);
79 C - toggle the displaying of weighted CPU percentage\n\
80 d - change number of displays to show\n\
81 e - list errors generated by last \"kill\" or \"renice\" command\n\
82 H - toggle the displaying of threads\n\
83 i or I - toggle the displaying of idle processes\n\
84 j - toggle the displaying of jail ID\n\
85 J - display processes for only one jail (+ selects all jails)\n\
86 k - kill processes; send a signal to a list of processes\n\
87 m - toggle the display between 'cpu' and 'io' modes\n\
88 n or # - change number of processes to display\n", stdout);
90 if (displaymode == DISP_CPU)
92 o - specify sort order (pri, size, res, cpu, time, threads, jid, pid)\n",
96 o - specify sort order (vcsw, ivcsw, read, write, fault, total, jid, pid)\n",
100 P - toggle the displaying of per-CPU statistics\n\
101 r - renice a process\n\
102 s - change number of seconds to delay between updates\n\
103 S - toggle the displaying of system processes\n\
104 a - toggle the displaying of process titles\n\
105 t - toggle the display of this process\n\
106 u - display processes for only one user (+ selects all users)\n\
107 w - toggle the display of swap use for each process\n\
108 z - toggle the displaying of the system idle process\n\
115 * Utility routines that help with some of the commands.
118 char *next_field(str)
123 if ((str = strchr(str, ' ')) == NULL)
128 while (*++str == ' ') /* loop */;
130 /* if there is nothing left of the string, return NULL */
131 /* This fix is dedicated to Greg Earle */
132 return(*str == '\0' ? NULL : str);
142 register int val = 0;
145 /* if there is nothing left of the string, flag it as an error */
146 /* This fix is dedicated to Greg Earle */
152 while ((ch = *str++) != '\0')
156 val = val * 10 + (ch - '0');
158 else if (isspace(ch))
172 * Some of the commands make system calls that could generate errors.
173 * These errors are collected up in an array of structures for later
174 * contemplation and display. Such routines return a string containing an
175 * error message, or NULL if no errors occurred. The next few routines are
176 * for manipulating and displaying these errors. We need an upper limit on
177 * the number of errors, so we arbitrarily choose 20.
182 struct errs /* structure for a system-call error */
184 int errnum; /* value of errno (that is, the actual error) */
185 char *arg; /* argument that caused the error */
188 static struct errs errs[ERRMAX];
190 static char *err_toomany = " too many errors occurred";
191 static char *err_listem =
192 " Many errors occurred. Press `e' to display the list of errors.";
194 /* These macros get used to reset and log the errors */
195 #define ERR_RESET errcnt = 0
196 #define ERROR(p, e) if (errcnt >= ERRMAX) \
198 return(err_toomany); \
202 errs[errcnt].arg = (p); \
203 errs[errcnt++].errnum = (e); \
207 * err_string() - return an appropriate error string. This is what the
208 * command will return for displaying. If no errors were logged, then
209 * return NULL. The maximum length of the error string is defined by
218 register struct errs *errp;
219 register int cnt = 0;
220 register int first = Yes;
221 register int currerr = -1;
222 int stringlen; /* characters still available in "string" */
223 static char string[STRMAX];
225 /* if there are no errors, return NULL */
231 /* sort the errors */
232 qsort((char *)errs, errcnt, sizeof(struct errs), err_compar);
234 /* need a space at the front of the error string */
237 stringlen = STRMAX - 2;
239 /* loop thru the sorted list, building an error string */
242 errp = &(errs[cnt++]);
243 if (errp->errnum != currerr)
247 if ((stringlen = str_adderr(string, stringlen, currerr)) < 2)
251 (void) strcat(string, "; "); /* we know there's more */
253 currerr = errp->errnum;
256 if ((stringlen = str_addarg(string, stringlen, errp->arg, first)) ==0)
263 /* add final message */
264 stringlen = str_adderr(string, stringlen, currerr);
266 /* return the error string */
267 return(stringlen == 0 ? err_listem : string);
271 * str_adderr(str, len, err) - add an explanation of error "err" to
276 str_adderr(str, len, err)
286 msg = err == 0 ? "Not a number" : errmsg(err);
287 msglen = strlen(msg) + 2;
292 (void) strcat(str, ": ");
293 (void) strcat(str, msg);
294 return(len - msglen);
298 * str_addarg(str, len, arg, first) - add the string argument "arg" to
299 * the string "str". This is the first in the group when "first"
300 * is set (indicating that a comma should NOT be added to the front).
304 str_addarg(str, len, arg, first)
314 arglen = strlen(arg);
325 (void) strcat(str, ", ");
327 (void) strcat(str, arg);
328 return(len - arglen);
332 * err_compar(p1, p2) - comparison routine used by "qsort"
333 * for sorting errors.
339 register struct errs *p1, *p2;
344 if ((result = p1->errnum - p2->errnum) == 0)
346 return(strcmp(p1->arg, p2->arg));
352 * error_count() - return the number of errors currently logged.
363 * show_errors() - display on stdout the current log of errors.
370 register int cnt = 0;
371 register struct errs *errp = errs;
373 printf("%d error%s:\n\n", errcnt, errcnt == 1 ? "" : "s");
374 while (cnt++ < errcnt)
376 printf("%5s: %s\n", errp->arg,
377 errp->errnum == 0 ? "Not a number" : errmsg(errp->errnum));
383 * kill_procs(str) - send signals to processes, much like the "kill"
384 * command does; invoked in response to 'k'.
387 char *kill_procs(str)
393 int signum = SIGTERM; /* default */
395 struct sigdesc *sigp;
398 /* reset error array */
401 /* remember our uid */
404 /* skip over leading white space */
405 while (isspace(*str)) str++;
409 /* explicit signal specified */
410 if ((nptr = next_field(str)) == NULL)
412 return(" kill: no processes specified");
417 (void) scanint(str + 1, &signum);
418 if (signum <= 0 || signum >= NSIG)
420 return(" invalid signal number");
425 /* translate the name into a number */
426 for (sigp = sigdesc; sigp->name != NULL; sigp++)
428 if (strcmp(sigp->name, str + 1) == 0)
430 signum = sigp->number;
435 /* was it ever found */
436 if (sigp->name == NULL)
438 return(" bad signal name");
441 /* put the new pointer in place */
445 /* loop thru the string, killing processes */
448 if (scanint(str, &procnum) == -1)
454 /* check process owner if we're not root */
455 if (uid && (uid != proc_owner(procnum)))
459 /* go in for the kill */
460 else if (kill(procnum, signum) == -1)
462 /* chalk up an error */
466 } while ((str = next_field(str)) != NULL);
468 /* return appropriate error string */
469 return(err_string());
473 * renice_procs(str) - change the "nice" of processes, much like the
474 * "renice" command does; invoked in response to 'r'.
477 char *renice_procs(str)
482 register char negate;
490 /* allow for negative priority values */
491 if ((negate = (*str == '-')) != 0)
493 /* move past the minus sign */
497 /* use procnum as a temporary holding place and get the number */
498 procnum = scanint(str, &prio);
500 /* negate if necessary */
506 #if defined(PRIO_MIN) && defined(PRIO_MAX)
507 /* check for validity */
508 if (procnum == -1 || prio < PRIO_MIN || prio > PRIO_MAX)
510 return(" bad priority value");
514 /* move to the first process number */
515 if ((str = next_field(str)) == NULL)
517 return(" no processes specified");
520 /* loop thru the process numbers, renicing each one */
523 if (scanint(str, &procnum) == -1)
528 /* check process owner if we're not root */
529 else if (uid && (uid != proc_owner(procnum)))
533 else if (setpriority(PRIO_PROCESS, procnum, prio) == -1)
537 } while ((str = next_field(str)) != NULL);
539 /* return appropriate error string */
540 return(err_string());