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).
26 #include <sys/resource.h>
28 #include "sigdesc.h" /* generated automatically */
35 extern char *copyright;
37 /* imported from screen.c */
38 extern int overstrike;
44 * show_help() - display the help screen; invoked in response to
51 printf("Top version %s, %s\n", version_string(), copyright);
53 A top users display for Unix\n\
55 These single-character commands are available:\n\
59 h or ? - help; show this text\n", stdout);
61 /* not all commands are availalbe with overstrike terminals */
65 Other commands are also available, but this terminal is not\n\
66 sophisticated enough to handle those commands gracefully.\n\n", stdout);
71 C - toggle the displaying of weighted CPU percentage\n\
72 d - change number of displays to show\n\
73 e - list errors generated by last \"kill\" or \"renice\" command\n\
74 H - toggle the displaying of threads\n\
75 i or I - toggle the displaying of idle processes\n\
76 j - toggle the displaying of jail ID\n\
77 J - display processes for only one jail (+ selects all jails)\n\
78 k - kill processes; send a signal to a list of processes\n\
79 m - toggle the display between 'cpu' and 'io' modes\n\
80 n or # - change number of processes to display\n", stdout);
82 if (displaymode == DISP_CPU)
84 o - specify sort order (pri, size, res, cpu, time, threads, jid, pid)\n",
88 o - specify sort order (vcsw, ivcsw, read, write, fault, total, jid, pid)\n",
92 P - toggle the displaying of per-CPU statistics\n\
93 r - renice a process\n\
94 s - change number of seconds to delay between updates\n\
95 S - toggle the displaying of system processes\n\
96 a - toggle the displaying of process titles\n\
97 t - toggle the display of this process\n\
98 u - display processes for only one user (+ selects all users)\n\
99 z - toggle the displaying of the system idle process\n\
106 * Utility routines that help with some of the commands.
109 char *next_field(str)
114 if ((str = strchr(str, ' ')) == NULL)
119 while (*++str == ' ') /* loop */;
121 /* if there is nothing left of the string, return NULL */
122 /* This fix is dedicated to Greg Earle */
123 return(*str == '\0' ? NULL : str);
132 register int val = 0;
135 /* if there is nothing left of the string, flag it as an error */
136 /* This fix is dedicated to Greg Earle */
142 while ((ch = *str++) != '\0')
146 val = val * 10 + (ch - '0');
148 else if (isspace(ch))
162 * Some of the commands make system calls that could generate errors.
163 * These errors are collected up in an array of structures for later
164 * contemplation and display. Such routines return a string containing an
165 * error message, or NULL if no errors occurred. The next few routines are
166 * for manipulating and displaying these errors. We need an upper limit on
167 * the number of errors, so we arbitrarily choose 20.
172 struct errs /* structure for a system-call error */
174 int errnum; /* value of errno (that is, the actual error) */
175 char *arg; /* argument that caused the error */
178 static struct errs errs[ERRMAX];
180 static char *err_toomany = " too many errors occurred";
181 static char *err_listem =
182 " Many errors occurred. Press `e' to display the list of errors.";
184 /* These macros get used to reset and log the errors */
185 #define ERR_RESET errcnt = 0
186 #define ERROR(p, e) if (errcnt >= ERRMAX) \
188 return(err_toomany); \
192 errs[errcnt].arg = (p); \
193 errs[errcnt++].errnum = (e); \
197 * err_string() - return an appropriate error string. This is what the
198 * command will return for displaying. If no errors were logged, then
199 * return NULL. The maximum length of the error string is defined by
208 register struct errs *errp;
209 register int cnt = 0;
210 register int first = Yes;
211 register int currerr = -1;
212 int stringlen; /* characters still available in "string" */
213 static char string[STRMAX];
215 /* if there are no errors, return NULL */
221 /* sort the errors */
222 qsort((char *)errs, errcnt, sizeof(struct errs), err_compar);
224 /* need a space at the front of the error string */
227 stringlen = STRMAX - 2;
229 /* loop thru the sorted list, building an error string */
232 errp = &(errs[cnt++]);
233 if (errp->errnum != currerr)
237 if ((stringlen = str_adderr(string, stringlen, currerr)) < 2)
241 (void) strcat(string, "; "); /* we know there's more */
243 currerr = errp->errnum;
246 if ((stringlen = str_addarg(string, stringlen, errp->arg, first)) ==0)
253 /* add final message */
254 stringlen = str_adderr(string, stringlen, currerr);
256 /* return the error string */
257 return(stringlen == 0 ? err_listem : string);
261 * str_adderr(str, len, err) - add an explanation of error "err" to
265 str_adderr(str, len, err)
275 msg = err == 0 ? "Not a number" : errmsg(err);
276 msglen = strlen(msg) + 2;
281 (void) strcat(str, ": ");
282 (void) strcat(str, msg);
283 return(len - msglen);
287 * str_addarg(str, len, arg, first) - add the string argument "arg" to
288 * the string "str". This is the first in the group when "first"
289 * is set (indicating that a comma should NOT be added to the front).
292 str_addarg(str, len, arg, first)
302 arglen = strlen(arg);
313 (void) strcat(str, ", ");
315 (void) strcat(str, arg);
316 return(len - arglen);
320 * err_compar(p1, p2) - comparison routine used by "qsort"
321 * for sorting errors.
326 register struct errs *p1, *p2;
331 if ((result = p1->errnum - p2->errnum) == 0)
333 return(strcmp(p1->arg, p2->arg));
339 * error_count() - return the number of errors currently logged.
349 * show_errors() - display on stdout the current log of errors.
355 register int cnt = 0;
356 register struct errs *errp = errs;
358 printf("%d error%s:\n\n", errcnt, errcnt == 1 ? "" : "s");
359 while (cnt++ < errcnt)
361 printf("%5s: %s\n", errp->arg,
362 errp->errnum == 0 ? "Not a number" : errmsg(errp->errnum));
368 * kill_procs(str) - send signals to processes, much like the "kill"
369 * command does; invoked in response to 'k'.
372 char *kill_procs(str)
378 int signum = SIGTERM; /* default */
380 struct sigdesc *sigp;
383 /* reset error array */
386 /* remember our uid */
389 /* skip over leading white space */
390 while (isspace(*str)) str++;
394 /* explicit signal specified */
395 if ((nptr = next_field(str)) == NULL)
397 return(" kill: no processes specified");
402 (void) scanint(str + 1, &signum);
403 if (signum <= 0 || signum >= NSIG)
405 return(" invalid signal number");
410 /* translate the name into a number */
411 for (sigp = sigdesc; sigp->name != NULL; sigp++)
413 if (strcmp(sigp->name, str + 1) == 0)
415 signum = sigp->number;
420 /* was it ever found */
421 if (sigp->name == NULL)
423 return(" bad signal name");
426 /* put the new pointer in place */
430 /* loop thru the string, killing processes */
433 if (scanint(str, &procnum) == -1)
439 /* check process owner if we're not root */
440 if (uid && (uid != proc_owner(procnum)))
444 /* go in for the kill */
445 else if (kill(procnum, signum) == -1)
447 /* chalk up an error */
451 } while ((str = next_field(str)) != NULL);
453 /* return appropriate error string */
454 return(err_string());
458 * renice_procs(str) - change the "nice" of processes, much like the
459 * "renice" command does; invoked in response to 'r'.
462 char *renice_procs(str)
467 register char negate;
475 /* allow for negative priority values */
476 if ((negate = (*str == '-')) != 0)
478 /* move past the minus sign */
482 /* use procnum as a temporary holding place and get the number */
483 procnum = scanint(str, &prio);
485 /* negate if necessary */
491 #if defined(PRIO_MIN) && defined(PRIO_MAX)
492 /* check for validity */
493 if (procnum == -1 || prio < PRIO_MIN || prio > PRIO_MAX)
495 return(" bad priority value");
499 /* move to the first process number */
500 if ((str = next_field(str)) == NULL)
502 return(" no processes specified");
505 /* loop thru the process numbers, renicing each one */
508 if (scanint(str, &procnum) == -1)
513 /* check process owner if we're not root */
514 else if (uid && (uid != proc_owner(procnum)))
518 else if (setpriority(PRIO_PROCESS, procnum, prio) == -1)
522 } while ((str = next_field(str)) != NULL);
524 /* return appropriate error string */
525 return(err_string());