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 available 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 displaying of thread IDs\n\
103 t - toggle the display of this process\n\
104 u - display processes for only one user (+ selects all users)\n\
105 w - toggle the display of swap use for each process\n\
106 z - toggle the displaying of the system idle process\n\
113 * Utility routines that help with some of the commands.
117 next_field(char *str)
119 if ((str = strchr(str, ' ')) == NULL)
124 while (*++str == ' ') /* loop */;
126 /* if there is nothing left of the string, return NULL */
127 /* This fix is dedicated to Greg Earle */
128 return(*str == '\0' ? NULL : str);
132 scanint(char *str, int *intp)
137 /* if there is nothing left of the string, flag it as an error */
138 /* This fix is dedicated to Greg Earle */
144 while ((ch = *str++) != '\0')
148 val = val * 10 + (ch - '0');
150 else if (isspace(ch))
164 * Some of the commands make system calls that could generate errors.
165 * These errors are collected up in an array of structures for later
166 * contemplation and display. Such routines return a string containing an
167 * error message, or NULL if no errors occurred. The next few routines are
168 * for manipulating and displaying these errors. We need an upper limit on
169 * the number of errors, so we arbitrarily choose 20.
174 static struct errs errs[ERRMAX];
176 static char err_toomany[] = " too many errors occurred";
177 static char err_listem[] =
178 " Many errors occurred. Press `e' to display the list of errors.";
180 /* These macros get used to reset and log the errors */
181 #define ERR_RESET errcnt = 0
182 #define ERROR(p, e) if (errcnt >= ERRMAX) \
184 return(err_toomany); \
188 errs[errcnt].arg = (p); \
189 errs[errcnt++].errnum = (e); \
193 * err_string() - return an appropriate error string. This is what the
194 * command will return for displaying. If no errors were logged, then
195 * return NULL. The maximum length of the error string is defined by
201 char *err_string(void)
207 int stringlen; /* characters still available in "string" */
208 static char string[STRMAX];
210 /* if there are no errors, return NULL */
216 /* sort the errors */
217 qsort((char *)errs, errcnt, sizeof(struct errs), err_compar);
219 /* need a space at the front of the error string */
222 stringlen = STRMAX - 2;
224 /* loop thru the sorted list, building an error string */
227 errp = &(errs[cnt++]);
228 if (errp->errnum != currerr)
232 if ((stringlen = str_adderr(string, stringlen, currerr)) < 2)
236 strcat(string, "; "); /* we know there's more */
238 currerr = errp->errnum;
241 if ((stringlen = str_addarg(string, stringlen, errp->arg, first)) ==0)
248 /* add final message */
249 stringlen = str_adderr(string, stringlen, currerr);
251 /* return the error string */
252 return(stringlen == 0 ? err_listem : string);
256 * str_adderr(str, len, err) - add an explanation of error "err" to
261 str_adderr(char *str, int len, int err)
266 msg = err == 0 ? "Not a number" : strerror(err);
267 msglen = strlen(msg) + 2;
274 return(len - msglen);
278 * str_addarg(str, len, arg, first) - add the string argument "arg" to
279 * the string "str". This is the first in the group when "first"
280 * is set (indicating that a comma should NOT be added to the front).
284 str_addarg(char str[], int len, char arg[], bool first)
288 arglen = strlen(arg);
302 return(len - arglen);
306 * err_compar(p1, p2) - comparison routine used by "qsort"
307 * for sorting errors.
311 err_compar(const void *p1, const void *p2)
314 const struct errs * const g1 = (const struct errs * const)p1;
315 const struct errs * const g2 = (const struct errs * const)p2;
319 if ((result = g1->errnum - g2->errnum) == 0)
321 return(strcmp(g1->arg, g2->arg));
327 * error_count() - return the number of errors currently logged.
337 * show_errors() - display on stdout the current log of errors.
344 struct errs *errp = errs;
346 printf("%d error%s:\n\n", errcnt, errcnt == 1 ? "" : "s");
347 while (cnt++ < errcnt)
349 printf("%5s: %s\n", errp->arg,
350 errp->errnum == 0 ? "Not a number" : strerror(errp->errnum));
355 static char no_proc_specified[] = " no processes specified";
356 static char invalid_signal_number[] = " invalid_signal_number";
357 static char bad_signal_name[] = " bad signal name";
358 static char bad_pri_value[] = " bad priority value";
361 * kill_procs(str) - send signals to processes, much like the "kill"
362 * command does; invoked in response to 'k'.
366 kill_procs(char *str)
369 int signum = SIGTERM; /* default */
371 struct sigdesc *sigp;
374 /* reset error array */
377 /* remember our uid */
380 /* skip over leading white space */
381 while (isspace(*str)) str++;
385 /* explicit signal specified */
386 if ((nptr = next_field(str)) == NULL)
388 return(no_proc_specified);
393 scanint(str + 1, &signum);
394 if (signum <= 0 || signum >= NSIG)
396 return(invalid_signal_number);
401 /* translate the name into a number */
402 for (sigp = sigdesc; sigp->name != NULL; sigp++)
404 if (strcmp(sigp->name, str + 1) == 0)
406 signum = sigp->number;
411 /* was it ever found */
412 if (sigp->name == NULL)
414 return(bad_signal_name);
417 /* put the new pointer in place */
421 /* loop thru the string, killing processes */
424 if (scanint(str, &procnum) == -1)
430 /* check process owner if we're not root */
431 if (uid && (uid != proc_owner(procnum)))
435 /* go in for the kill */
436 else if (kill(procnum, signum) == -1)
438 /* chalk up an error */
442 } while ((str = next_field(str)) != NULL);
444 /* return appropriate error string */
445 return(err_string());
449 * renice_procs(str) - change the "nice" of processes, much like the
450 * "renice" command does; invoked in response to 'r'.
454 renice_procs(char *str)
464 /* allow for negative priority values */
465 if ((negate = (*str == '-')) != 0)
467 /* move past the minus sign */
471 /* use procnum as a temporary holding place and get the number */
472 procnum = scanint(str, &prio);
474 /* negate if necessary */
480 /* check for validity */
481 if (procnum == -1 || prio < PRIO_MIN || prio > PRIO_MAX)
483 return(bad_pri_value);
486 /* move to the first process number */
487 if ((str = next_field(str)) == NULL)
489 return(no_proc_specified);
492 /* loop thru the process numbers, renicing each one */
495 if (scanint(str, &procnum) == -1)
500 /* check process owner if we're not root */
501 else if (uid && (uid != proc_owner(procnum)))
505 else if (setpriority(PRIO_PROCESS, procnum, prio) == -1)
509 } while ((str = next_field(str)) != NULL);
511 /* return appropriate error string */
512 return(err_string());