2 * Top users/processes display for Unix
4 * This program may be freely redistributed,
5 * but this entire comment MUST remain intact.
7 * Copyright (c) 1984, 1989, William LeFebvre, Rice University
8 * Copyright (c) 1989, 1990, 1992, William LeFebvre, Northwestern University
14 * This file contains the routines that implement some of the interactive
15 * mode commands. Note that some of the commands are implemented in-line
16 * in "main". This is necessary because they change the global state of
17 * "top" (i.e.: changing the number of processes to display).
20 #include <sys/resource.h>
21 #include <sys/signal.h>
33 #include "sigdesc.h" /* generated automatically */
37 static int err_compar(const void *p1, const void *p2);
39 struct errs /* structure for a system-call error */
41 int errnum; /* value of errno (that is, the actual error) */
42 char *arg; /* argument that caused the error */
45 static char *err_string(void);
46 static int str_adderr(char *str, int len, int err);
47 static int str_addarg(char *str, int len, char *arg, bool first);
50 * show_help() - display the help screen; invoked in response to
57 printf("Top version FreeBSD, %s\n", copyright);
59 A top users display for Unix\n\
61 These single-character commands are available:\n\
65 h or ? - help; show this text\n", stdout);
67 /* not all commands are availalbe with overstrike terminals */
71 Other commands are also available, but this terminal is not\n\
72 sophisticated enough to handle those commands gracefully.\n\n", stdout);
77 C - toggle the displaying of weighted CPU percentage\n\
78 d - change number of displays to show\n\
79 e - list errors generated by last \"kill\" or \"renice\" command\n\
80 H - toggle the displaying of threads\n\
81 i or I - toggle the displaying of idle processes\n\
82 j - toggle the displaying of jail ID\n\
83 J - display processes for only one jail (+ selects all jails)\n\
84 k - kill processes; send a signal to a list of processes\n\
85 m - toggle the display between 'cpu' and 'io' modes\n\
86 n or # - change number of processes to display\n", stdout);
87 if (displaymode == DISP_CPU)
89 o - specify sort order (pri, size, res, cpu, time, threads, jid, pid)\n",
93 o - specify sort order (vcsw, ivcsw, read, write, fault, total, jid, pid)\n",
96 p - display one process (+ selects all processes)\n\
97 P - toggle the displaying of per-CPU statistics\n\
98 r - renice a process\n\
99 s - change number of seconds to delay between updates\n\
100 S - toggle the displaying of system processes\n\
101 a - toggle the displaying of process titles\n\
102 t - toggle the display of this process\n\
103 u - display processes for only one user (+ selects all users)\n\
104 w - toggle the display of swap use for each process\n\
105 z - toggle the displaying of the system idle process\n\
112 * Utility routines that help with some of the commands.
116 next_field(char *str)
118 if ((str = strchr(str, ' ')) == NULL)
123 while (*++str == ' ') /* loop */;
125 /* if there is nothing left of the string, return NULL */
126 /* This fix is dedicated to Greg Earle */
127 return(*str == '\0' ? NULL : str);
131 scanint(char *str, int *intp)
136 /* if there is nothing left of the string, flag it as an error */
137 /* This fix is dedicated to Greg Earle */
143 while ((ch = *str++) != '\0')
147 val = val * 10 + (ch - '0');
149 else if (isspace(ch))
163 * Some of the commands make system calls that could generate errors.
164 * These errors are collected up in an array of structures for later
165 * contemplation and display. Such routines return a string containing an
166 * error message, or NULL if no errors occurred. The next few routines are
167 * for manipulating and displaying these errors. We need an upper limit on
168 * the number of errors, so we arbitrarily choose 20.
173 static struct errs errs[ERRMAX];
175 static char err_toomany[] = " too many errors occurred";
176 static char err_listem[] =
177 " Many errors occurred. Press `e' to display the list of errors.";
179 /* These macros get used to reset and log the errors */
180 #define ERR_RESET errcnt = 0
181 #define ERROR(p, e) if (errcnt >= ERRMAX) \
183 return(err_toomany); \
187 errs[errcnt].arg = (p); \
188 errs[errcnt++].errnum = (e); \
192 * err_string() - return an appropriate error string. This is what the
193 * command will return for displaying. If no errors were logged, then
194 * return NULL. The maximum length of the error string is defined by
200 char *err_string(void)
206 int stringlen; /* characters still available in "string" */
207 static char string[STRMAX];
209 /* if there are no errors, return NULL */
215 /* sort the errors */
216 qsort((char *)errs, errcnt, sizeof(struct errs), err_compar);
218 /* need a space at the front of the error string */
221 stringlen = STRMAX - 2;
223 /* loop thru the sorted list, building an error string */
226 errp = &(errs[cnt++]);
227 if (errp->errnum != currerr)
231 if ((stringlen = str_adderr(string, stringlen, currerr)) < 2)
235 strcat(string, "; "); /* we know there's more */
237 currerr = errp->errnum;
240 if ((stringlen = str_addarg(string, stringlen, errp->arg, first)) ==0)
247 /* add final message */
248 stringlen = str_adderr(string, stringlen, currerr);
250 /* return the error string */
251 return(stringlen == 0 ? err_listem : string);
255 * str_adderr(str, len, err) - add an explanation of error "err" to
260 str_adderr(char *str, int len, int err)
265 msg = err == 0 ? "Not a number" : strerror(err);
266 msglen = strlen(msg) + 2;
273 return(len - msglen);
277 * str_addarg(str, len, arg, first) - add the string argument "arg" to
278 * the string "str". This is the first in the group when "first"
279 * is set (indicating that a comma should NOT be added to the front).
283 str_addarg(char str[], int len, char arg[], bool first)
287 arglen = strlen(arg);
301 return(len - arglen);
305 * err_compar(p1, p2) - comparison routine used by "qsort"
306 * for sorting errors.
310 err_compar(const void *p1, const void *p2)
313 const struct errs * const g1 = (const struct errs * const)p1;
314 const struct errs * const g2 = (const struct errs * const)p2;
318 if ((result = g1->errnum - g2->errnum) == 0)
320 return(strcmp(g1->arg, g2->arg));
326 * error_count() - return the number of errors currently logged.
336 * show_errors() - display on stdout the current log of errors.
343 struct errs *errp = errs;
345 printf("%d error%s:\n\n", errcnt, errcnt == 1 ? "" : "s");
346 while (cnt++ < errcnt)
348 printf("%5s: %s\n", errp->arg,
349 errp->errnum == 0 ? "Not a number" : strerror(errp->errnum));
354 static char no_proc_specified[] = " no processes specified";
355 static char invalid_signal_number[] = " invalid_signal_number";
356 static char bad_signal_name[] = " bad signal name";
357 static char bad_pri_value[] = " bad priority value";
360 * kill_procs(str) - send signals to processes, much like the "kill"
361 * command does; invoked in response to 'k'.
365 kill_procs(char *str)
368 int signum = SIGTERM; /* default */
370 struct sigdesc *sigp;
373 /* reset error array */
376 /* remember our uid */
379 /* skip over leading white space */
380 while (isspace(*str)) str++;
384 /* explicit signal specified */
385 if ((nptr = next_field(str)) == NULL)
387 return(no_proc_specified);
392 scanint(str + 1, &signum);
393 if (signum <= 0 || signum >= NSIG)
395 return(invalid_signal_number);
400 /* translate the name into a number */
401 for (sigp = sigdesc; sigp->name != NULL; sigp++)
403 if (strcmp(sigp->name, str + 1) == 0)
405 signum = sigp->number;
410 /* was it ever found */
411 if (sigp->name == NULL)
413 return(bad_signal_name);
416 /* put the new pointer in place */
420 /* loop thru the string, killing processes */
423 if (scanint(str, &procnum) == -1)
429 /* check process owner if we're not root */
430 if (uid && (uid != proc_owner(procnum)))
434 /* go in for the kill */
435 else if (kill(procnum, signum) == -1)
437 /* chalk up an error */
441 } while ((str = next_field(str)) != NULL);
443 /* return appropriate error string */
444 return(err_string());
448 * renice_procs(str) - change the "nice" of processes, much like the
449 * "renice" command does; invoked in response to 'r'.
453 renice_procs(char *str)
463 /* allow for negative priority values */
464 if ((negate = (*str == '-')) != 0)
466 /* move past the minus sign */
470 /* use procnum as a temporary holding place and get the number */
471 procnum = scanint(str, &prio);
473 /* negate if necessary */
479 /* check for validity */
480 if (procnum == -1 || prio < PRIO_MIN || prio > PRIO_MAX)
482 return(bad_pri_value);
485 /* move to the first process number */
486 if ((str = next_field(str)) == NULL)
488 return(no_proc_specified);
491 /* loop thru the process numbers, renicing each one */
494 if (scanint(str, &procnum) == -1)
499 /* check process owner if we're not root */
500 else if (uid && (uid != proc_owner(procnum)))
504 else if (setpriority(PRIO_PROCESS, procnum, prio) == -1)
508 } while ((str = next_field(str)) != NULL);
510 /* return appropriate error string */
511 return(err_string());