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 k - kill processes; send a signal to a list of processes\n\
78 m - toggle the display between 'cpu' and 'io' modes\n\
79 n or # - change number of processes to display\n", stdout);
81 if (displaymode == DISP_CPU)
83 o - specify sort order (pri, size, res, cpu, time, threads)\n", stdout);
86 o - specify sort order (vcsw, ivcsw, read, write, fault, total)\n", stdout);
89 r - renice a process\n\
90 s - change number of seconds to delay between updates\n\
91 S - toggle the displaying of system processes\n\
92 a - toggle the displaying of process titles\n\
93 t - toggle the display of this process\n\
94 u - display processes for only one user (+ selects all users)\n\
101 * Utility routines that help with some of the commands.
104 char *next_field(str)
109 if ((str = strchr(str, ' ')) == NULL)
114 while (*++str == ' ') /* loop */;
116 /* if there is nothing left of the string, return NULL */
117 /* This fix is dedicated to Greg Earle */
118 return(*str == '\0' ? NULL : str);
127 register int val = 0;
130 /* if there is nothing left of the string, flag it as an error */
131 /* This fix is dedicated to Greg Earle */
137 while ((ch = *str++) != '\0')
141 val = val * 10 + (ch - '0');
143 else if (isspace(ch))
157 * Some of the commands make system calls that could generate errors.
158 * These errors are collected up in an array of structures for later
159 * contemplation and display. Such routines return a string containing an
160 * error message, or NULL if no errors occurred. The next few routines are
161 * for manipulating and displaying these errors. We need an upper limit on
162 * the number of errors, so we arbitrarily choose 20.
167 struct errs /* structure for a system-call error */
169 int errnum; /* value of errno (that is, the actual error) */
170 char *arg; /* argument that caused the error */
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
203 register struct errs *errp;
204 register int cnt = 0;
205 register int first = Yes;
206 register int currerr = -1;
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 (void) 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
260 str_adderr(str, len, err)
270 msg = err == 0 ? "Not a number" : errmsg(err);
271 msglen = strlen(msg) + 2;
276 (void) strcat(str, ": ");
277 (void) strcat(str, msg);
278 return(len - msglen);
282 * str_addarg(str, len, arg, first) - add the string argument "arg" to
283 * the string "str". This is the first in the group when "first"
284 * is set (indicating that a comma should NOT be added to the front).
287 str_addarg(str, len, arg, first)
297 arglen = strlen(arg);
308 (void) strcat(str, ", ");
310 (void) strcat(str, arg);
311 return(len - arglen);
315 * err_compar(p1, p2) - comparison routine used by "qsort"
316 * for sorting errors.
321 register struct errs *p1, *p2;
326 if ((result = p1->errnum - p2->errnum) == 0)
328 return(strcmp(p1->arg, p2->arg));
334 * error_count() - return the number of errors currently logged.
344 * show_errors() - display on stdout the current log of errors.
350 register int cnt = 0;
351 register struct errs *errp = errs;
353 printf("%d error%s:\n\n", errcnt, errcnt == 1 ? "" : "s");
354 while (cnt++ < errcnt)
356 printf("%5s: %s\n", errp->arg,
357 errp->errnum == 0 ? "Not a number" : errmsg(errp->errnum));
363 * kill_procs(str) - send signals to processes, much like the "kill"
364 * command does; invoked in response to 'k'.
367 char *kill_procs(str)
373 int signum = SIGTERM; /* default */
375 struct sigdesc *sigp;
378 /* reset error array */
381 /* remember our uid */
384 /* skip over leading white space */
385 while (isspace(*str)) str++;
389 /* explicit signal specified */
390 if ((nptr = next_field(str)) == NULL)
392 return(" kill: no processes specified");
397 (void) scanint(str + 1, &signum);
398 if (signum <= 0 || signum >= NSIG)
400 return(" invalid signal number");
405 /* translate the name into a number */
406 for (sigp = sigdesc; sigp->name != NULL; sigp++)
408 if (strcmp(sigp->name, str + 1) == 0)
410 signum = sigp->number;
415 /* was it ever found */
416 if (sigp->name == NULL)
418 return(" bad signal name");
421 /* put the new pointer in place */
425 /* loop thru the string, killing processes */
428 if (scanint(str, &procnum) == -1)
434 /* check process owner if we're not root */
435 if (uid && (uid != proc_owner(procnum)))
439 /* go in for the kill */
440 else if (kill(procnum, signum) == -1)
442 /* chalk up an error */
446 } while ((str = next_field(str)) != NULL);
448 /* return appropriate error string */
449 return(err_string());
453 * renice_procs(str) - change the "nice" of processes, much like the
454 * "renice" command does; invoked in response to 'r'.
457 char *renice_procs(str)
462 register char negate;
470 /* allow for negative priority values */
471 if ((negate = (*str == '-')) != 0)
473 /* move past the minus sign */
477 /* use procnum as a temporary holding place and get the number */
478 procnum = scanint(str, &prio);
480 /* negate if necessary */
486 #if defined(PRIO_MIN) && defined(PRIO_MAX)
487 /* check for validity */
488 if (procnum == -1 || prio < PRIO_MIN || prio > PRIO_MAX)
490 return(" bad priority value");
494 /* move to the first process number */
495 if ((str = next_field(str)) == NULL)
497 return(" no processes specified");
500 /* loop thru the process numbers, renicing each one */
503 if (scanint(str, &procnum) == -1)
508 /* check process owner if we're not root */
509 else if (uid && (uid != proc_owner(procnum)))
513 else if (setpriority(PRIO_PROCESS, procnum, prio) == -1)
517 } while ((str = next_field(str)) != NULL);
519 /* return appropriate error string */
520 return(err_string());