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, jid)\n",
87 o - specify sort order (vcsw, ivcsw, read, write, fault, total, jid)\n",
91 P - toggle the displaying of per-CPU statistics\n\
92 r - renice a process\n\
93 s - change number of seconds to delay between updates\n\
94 S - toggle the displaying of system processes\n\
95 a - toggle the displaying of process titles\n\
96 t - toggle the display of this process\n\
97 u - display processes for only one user (+ selects all users)\n\
98 z - toggle the displaying of the system idle process\n\
105 * Utility routines that help with some of the commands.
108 char *next_field(str)
113 if ((str = strchr(str, ' ')) == NULL)
118 while (*++str == ' ') /* loop */;
120 /* if there is nothing left of the string, return NULL */
121 /* This fix is dedicated to Greg Earle */
122 return(*str == '\0' ? NULL : str);
131 register int val = 0;
134 /* if there is nothing left of the string, flag it as an error */
135 /* This fix is dedicated to Greg Earle */
141 while ((ch = *str++) != '\0')
145 val = val * 10 + (ch - '0');
147 else if (isspace(ch))
161 * Some of the commands make system calls that could generate errors.
162 * These errors are collected up in an array of structures for later
163 * contemplation and display. Such routines return a string containing an
164 * error message, or NULL if no errors occurred. The next few routines are
165 * for manipulating and displaying these errors. We need an upper limit on
166 * the number of errors, so we arbitrarily choose 20.
171 struct errs /* structure for a system-call error */
173 int errnum; /* value of errno (that is, the actual error) */
174 char *arg; /* argument that caused the error */
177 static struct errs errs[ERRMAX];
179 static char *err_toomany = " too many errors occurred";
180 static char *err_listem =
181 " Many errors occurred. Press `e' to display the list of errors.";
183 /* These macros get used to reset and log the errors */
184 #define ERR_RESET errcnt = 0
185 #define ERROR(p, e) if (errcnt >= ERRMAX) \
187 return(err_toomany); \
191 errs[errcnt].arg = (p); \
192 errs[errcnt++].errnum = (e); \
196 * err_string() - return an appropriate error string. This is what the
197 * command will return for displaying. If no errors were logged, then
198 * return NULL. The maximum length of the error string is defined by
207 register struct errs *errp;
208 register int cnt = 0;
209 register int first = Yes;
210 register int currerr = -1;
211 int stringlen; /* characters still available in "string" */
212 static char string[STRMAX];
214 /* if there are no errors, return NULL */
220 /* sort the errors */
221 qsort((char *)errs, errcnt, sizeof(struct errs), err_compar);
223 /* need a space at the front of the error string */
226 stringlen = STRMAX - 2;
228 /* loop thru the sorted list, building an error string */
231 errp = &(errs[cnt++]);
232 if (errp->errnum != currerr)
236 if ((stringlen = str_adderr(string, stringlen, currerr)) < 2)
240 (void) strcat(string, "; "); /* we know there's more */
242 currerr = errp->errnum;
245 if ((stringlen = str_addarg(string, stringlen, errp->arg, first)) ==0)
252 /* add final message */
253 stringlen = str_adderr(string, stringlen, currerr);
255 /* return the error string */
256 return(stringlen == 0 ? err_listem : string);
260 * str_adderr(str, len, err) - add an explanation of error "err" to
264 str_adderr(str, len, err)
274 msg = err == 0 ? "Not a number" : errmsg(err);
275 msglen = strlen(msg) + 2;
280 (void) strcat(str, ": ");
281 (void) strcat(str, msg);
282 return(len - msglen);
286 * str_addarg(str, len, arg, first) - add the string argument "arg" to
287 * the string "str". This is the first in the group when "first"
288 * is set (indicating that a comma should NOT be added to the front).
291 str_addarg(str, len, arg, first)
301 arglen = strlen(arg);
312 (void) strcat(str, ", ");
314 (void) strcat(str, arg);
315 return(len - arglen);
319 * err_compar(p1, p2) - comparison routine used by "qsort"
320 * for sorting errors.
325 register struct errs *p1, *p2;
330 if ((result = p1->errnum - p2->errnum) == 0)
332 return(strcmp(p1->arg, p2->arg));
338 * error_count() - return the number of errors currently logged.
348 * show_errors() - display on stdout the current log of errors.
354 register int cnt = 0;
355 register struct errs *errp = errs;
357 printf("%d error%s:\n\n", errcnt, errcnt == 1 ? "" : "s");
358 while (cnt++ < errcnt)
360 printf("%5s: %s\n", errp->arg,
361 errp->errnum == 0 ? "Not a number" : errmsg(errp->errnum));
367 * kill_procs(str) - send signals to processes, much like the "kill"
368 * command does; invoked in response to 'k'.
371 char *kill_procs(str)
377 int signum = SIGTERM; /* default */
379 struct sigdesc *sigp;
382 /* reset error array */
385 /* remember our uid */
388 /* skip over leading white space */
389 while (isspace(*str)) str++;
393 /* explicit signal specified */
394 if ((nptr = next_field(str)) == NULL)
396 return(" kill: no processes specified");
401 (void) scanint(str + 1, &signum);
402 if (signum <= 0 || signum >= NSIG)
404 return(" invalid signal number");
409 /* translate the name into a number */
410 for (sigp = sigdesc; sigp->name != NULL; sigp++)
412 if (strcmp(sigp->name, str + 1) == 0)
414 signum = sigp->number;
419 /* was it ever found */
420 if (sigp->name == NULL)
422 return(" bad signal name");
425 /* put the new pointer in place */
429 /* loop thru the string, killing processes */
432 if (scanint(str, &procnum) == -1)
438 /* check process owner if we're not root */
439 if (uid && (uid != proc_owner(procnum)))
443 /* go in for the kill */
444 else if (kill(procnum, signum) == -1)
446 /* chalk up an error */
450 } while ((str = next_field(str)) != NULL);
452 /* return appropriate error string */
453 return(err_string());
457 * renice_procs(str) - change the "nice" of processes, much like the
458 * "renice" command does; invoked in response to 'r'.
461 char *renice_procs(str)
466 register char negate;
474 /* allow for negative priority values */
475 if ((negate = (*str == '-')) != 0)
477 /* move past the minus sign */
481 /* use procnum as a temporary holding place and get the number */
482 procnum = scanint(str, &prio);
484 /* negate if necessary */
490 #if defined(PRIO_MIN) && defined(PRIO_MAX)
491 /* check for validity */
492 if (procnum == -1 || prio < PRIO_MIN || prio > PRIO_MAX)
494 return(" bad priority value");
498 /* move to the first process number */
499 if ((str = next_field(str)) == NULL)
501 return(" no processes specified");
504 /* loop thru the process numbers, renicing each one */
507 if (scanint(str, &procnum) == -1)
512 /* check process owner if we're not root */
513 else if (uid && (uid != proc_owner(procnum)))
517 else if (setpriority(PRIO_PROCESS, procnum, prio) == -1)
521 } while ((str = next_field(str)) != NULL);
523 /* return appropriate error string */
524 return(err_string());