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 d - change number of displays to show\n\
72 e - list errors generated by last \"kill\" or \"renice\" command\n\
73 i or I - toggle the displaying of idle processes\n\
74 H - toggle the displaying of threads\n\
75 k - kill processes; send a signal to a list of processes\n\
76 m - toggle the display between 'cpu' and 'io' modes\n\
77 n or # - change number of processes to display\n", stdout);
79 if (displaymode == DISP_CPU)
81 o - specify sort order (pri, size, res, cpu, time)\n", stdout);
84 o - specify sort order (vcsw, ivcsw, read, write, fault, total)\n", stdout);
87 r - renice a process\n\
88 s - change number of seconds to delay between updates\n\
89 S - toggle the displaying of system processes\n\
90 t - toggle the display of this process\n\
91 u - display processes for only one user (+ selects all users)\n\
98 * Utility routines that help with some of the commands.
101 char *next_field(str)
106 if ((str = strchr(str, ' ')) == NULL)
111 while (*++str == ' ') /* loop */;
113 /* if there is nothing left of the string, return NULL */
114 /* This fix is dedicated to Greg Earle */
115 return(*str == '\0' ? NULL : str);
124 register int val = 0;
127 /* if there is nothing left of the string, flag it as an error */
128 /* This fix is dedicated to Greg Earle */
134 while ((ch = *str++) != '\0')
138 val = val * 10 + (ch - '0');
140 else if (isspace(ch))
154 * Some of the commands make system calls that could generate errors.
155 * These errors are collected up in an array of structures for later
156 * contemplation and display. Such routines return a string containing an
157 * error message, or NULL if no errors occurred. The next few routines are
158 * for manipulating and displaying these errors. We need an upper limit on
159 * the number of errors, so we arbitrarily choose 20.
164 struct errs /* structure for a system-call error */
166 int errnum; /* value of errno (that is, the actual error) */
167 char *arg; /* argument that caused the error */
170 static struct errs errs[ERRMAX];
172 static char *err_toomany = " too many errors occurred";
173 static char *err_listem =
174 " Many errors occurred. Press `e' to display the list of errors.";
176 /* These macros get used to reset and log the errors */
177 #define ERR_RESET errcnt = 0
178 #define ERROR(p, e) if (errcnt >= ERRMAX) \
180 return(err_toomany); \
184 errs[errcnt].arg = (p); \
185 errs[errcnt++].errnum = (e); \
189 * err_string() - return an appropriate error string. This is what the
190 * command will return for displaying. If no errors were logged, then
191 * return NULL. The maximum length of the error string is defined by
200 register struct errs *errp;
201 register int cnt = 0;
202 register int first = Yes;
203 register int currerr = -1;
204 int stringlen; /* characters still available in "string" */
205 static char string[STRMAX];
207 /* if there are no errors, return NULL */
213 /* sort the errors */
214 qsort((char *)errs, errcnt, sizeof(struct errs), err_compar);
216 /* need a space at the front of the error string */
219 stringlen = STRMAX - 2;
221 /* loop thru the sorted list, building an error string */
224 errp = &(errs[cnt++]);
225 if (errp->errnum != currerr)
229 if ((stringlen = str_adderr(string, stringlen, currerr)) < 2)
233 (void) strcat(string, "; "); /* we know there's more */
235 currerr = errp->errnum;
238 if ((stringlen = str_addarg(string, stringlen, errp->arg, first)) ==0)
245 /* add final message */
246 stringlen = str_adderr(string, stringlen, currerr);
248 /* return the error string */
249 return(stringlen == 0 ? err_listem : string);
253 * str_adderr(str, len, err) - add an explanation of error "err" to
257 str_adderr(str, len, err)
267 msg = err == 0 ? "Not a number" : errmsg(err);
268 msglen = strlen(msg) + 2;
273 (void) strcat(str, ": ");
274 (void) strcat(str, msg);
275 return(len - msglen);
279 * str_addarg(str, len, arg, first) - add the string argument "arg" to
280 * the string "str". This is the first in the group when "first"
281 * is set (indicating that a comma should NOT be added to the front).
284 str_addarg(str, len, arg, first)
294 arglen = strlen(arg);
305 (void) strcat(str, ", ");
307 (void) strcat(str, arg);
308 return(len - arglen);
312 * err_compar(p1, p2) - comparison routine used by "qsort"
313 * for sorting errors.
318 register struct errs *p1, *p2;
323 if ((result = p1->errnum - p2->errnum) == 0)
325 return(strcmp(p1->arg, p2->arg));
331 * error_count() - return the number of errors currently logged.
341 * show_errors() - display on stdout the current log of errors.
347 register int cnt = 0;
348 register struct errs *errp = errs;
350 printf("%d error%s:\n\n", errcnt, errcnt == 1 ? "" : "s");
351 while (cnt++ < errcnt)
353 printf("%5s: %s\n", errp->arg,
354 errp->errnum == 0 ? "Not a number" : errmsg(errp->errnum));
360 * kill_procs(str) - send signals to processes, much like the "kill"
361 * command does; invoked in response to 'k'.
364 char *kill_procs(str)
370 int signum = SIGTERM; /* default */
372 struct sigdesc *sigp;
375 /* reset error array */
378 /* remember our uid */
381 /* skip over leading white space */
382 while (isspace(*str)) str++;
386 /* explicit signal specified */
387 if ((nptr = next_field(str)) == NULL)
389 return(" kill: no processes specified");
394 (void) scanint(str + 1, &signum);
395 if (signum <= 0 || signum >= NSIG)
397 return(" invalid signal number");
402 /* translate the name into a number */
403 for (sigp = sigdesc; sigp->name != NULL; sigp++)
405 if (strcmp(sigp->name, str + 1) == 0)
407 signum = sigp->number;
412 /* was it ever found */
413 if (sigp->name == NULL)
415 return(" bad signal name");
418 /* put the new pointer in place */
422 /* loop thru the string, killing processes */
425 if (scanint(str, &procnum) == -1)
431 /* check process owner if we're not root */
432 if (uid && (uid != proc_owner(procnum)))
436 /* go in for the kill */
437 else if (kill(procnum, signum) == -1)
439 /* chalk up an error */
443 } while ((str = next_field(str)) != NULL);
445 /* return appropriate error string */
446 return(err_string());
450 * renice_procs(str) - change the "nice" of processes, much like the
451 * "renice" command does; invoked in response to 'r'.
454 char *renice_procs(str)
459 register char negate;
467 /* allow for negative priority values */
468 if ((negate = (*str == '-')) != 0)
470 /* move past the minus sign */
474 /* use procnum as a temporary holding place and get the number */
475 procnum = scanint(str, &prio);
477 /* negate if necessary */
483 #if defined(PRIO_MIN) && defined(PRIO_MAX)
484 /* check for validity */
485 if (procnum == -1 || prio < PRIO_MIN || prio > PRIO_MAX)
487 return(" bad priority value");
491 /* move to the first process number */
492 if ((str = next_field(str)) == NULL)
494 return(" no processes specified");
497 /* loop thru the process numbers, renicing each one */
500 if (scanint(str, &procnum) == -1)
505 /* check process owner if we're not root */
506 else if (uid && (uid != proc_owner(procnum)))
510 else if (setpriority(PRIO_PROCESS, procnum, prio) == -1)
514 } while ((str = next_field(str)) != NULL);
516 /* return appropriate error string */
517 return(err_string());