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).
24 #include <sys/resource.h>
32 #include "sigdesc.h" /* generated automatically */
40 extern char *copyright;
42 /* imported from screen.c */
43 extern int overstrike;
47 static int str_adderr(char *str, int len, int err);
48 static int str_addarg(char *str, int len, char *arg, int first);
51 * show_help() - display the help screen; invoked in response to
59 printf("Top version %s, %s\n", version_string(), copyright);
61 A top users display for Unix\n\
63 These single-character commands are available:\n\
67 h or ? - help; show this text\n", stdout);
69 /* not all commands are availalbe with overstrike terminals */
73 Other commands are also available, but this terminal is not\n\
74 sophisticated enough to handle those commands gracefully.\n\n", stdout);
79 C - toggle the displaying of weighted CPU percentage\n\
80 d - change number of displays to show\n\
81 e - list errors generated by last \"kill\" or \"renice\" command\n\
82 H - toggle the displaying of threads\n\
83 i or I - toggle the displaying of idle processes\n\
84 j - toggle the displaying of jail ID\n\
85 J - display processes for only one jail (+ selects all jails)\n\
86 k - kill processes; send a signal to a list of processes\n\
87 m - toggle the display between 'cpu' and 'io' modes\n\
88 n or # - change number of processes to display\n", stdout);
90 if (displaymode == DISP_CPU)
92 o - specify sort order (pri, size, res, cpu, time, threads, jid, pid)\n",
96 o - specify sort order (vcsw, ivcsw, read, write, fault, total, jid, pid)\n",
100 P - toggle the displaying of per-CPU statistics\n\
101 r - renice a process\n\
102 s - change number of seconds to delay between updates\n\
103 S - toggle the displaying of system processes\n\
104 a - toggle the displaying of process titles\n\
105 t - toggle the display of this process\n\
106 u - display processes for only one user (+ selects all users)\n\
107 z - toggle the displaying of the system idle process\n\
114 * Utility routines that help with some of the commands.
117 char *next_field(str)
122 if ((str = strchr(str, ' ')) == NULL)
127 while (*++str == ' ') /* loop */;
129 /* if there is nothing left of the string, return NULL */
130 /* This fix is dedicated to Greg Earle */
131 return(*str == '\0' ? NULL : str);
141 register int val = 0;
144 /* if there is nothing left of the string, flag it as an error */
145 /* This fix is dedicated to Greg Earle */
151 while ((ch = *str++) != '\0')
155 val = val * 10 + (ch - '0');
157 else if (isspace(ch))
171 * Some of the commands make system calls that could generate errors.
172 * These errors are collected up in an array of structures for later
173 * contemplation and display. Such routines return a string containing an
174 * error message, or NULL if no errors occurred. The next few routines are
175 * for manipulating and displaying these errors. We need an upper limit on
176 * the number of errors, so we arbitrarily choose 20.
181 struct errs /* structure for a system-call error */
183 int errnum; /* value of errno (that is, the actual error) */
184 char *arg; /* argument that caused the error */
187 static struct errs errs[ERRMAX];
189 static char *err_toomany = " too many errors occurred";
190 static char *err_listem =
191 " Many errors occurred. Press `e' to display the list of errors.";
193 /* These macros get used to reset and log the errors */
194 #define ERR_RESET errcnt = 0
195 #define ERROR(p, e) if (errcnt >= ERRMAX) \
197 return(err_toomany); \
201 errs[errcnt].arg = (p); \
202 errs[errcnt++].errnum = (e); \
206 * err_string() - return an appropriate error string. This is what the
207 * command will return for displaying. If no errors were logged, then
208 * return NULL. The maximum length of the error string is defined by
217 register struct errs *errp;
218 register int cnt = 0;
219 register int first = Yes;
220 register int currerr = -1;
221 int stringlen; /* characters still available in "string" */
222 static char string[STRMAX];
224 /* if there are no errors, return NULL */
230 /* sort the errors */
231 qsort((char *)errs, errcnt, sizeof(struct errs), err_compar);
233 /* need a space at the front of the error string */
236 stringlen = STRMAX - 2;
238 /* loop thru the sorted list, building an error string */
241 errp = &(errs[cnt++]);
242 if (errp->errnum != currerr)
246 if ((stringlen = str_adderr(string, stringlen, currerr)) < 2)
250 (void) strcat(string, "; "); /* we know there's more */
252 currerr = errp->errnum;
255 if ((stringlen = str_addarg(string, stringlen, errp->arg, first)) ==0)
262 /* add final message */
263 stringlen = str_adderr(string, stringlen, currerr);
265 /* return the error string */
266 return(stringlen == 0 ? err_listem : string);
270 * str_adderr(str, len, err) - add an explanation of error "err" to
275 str_adderr(str, len, err)
285 msg = err == 0 ? "Not a number" : errmsg(err);
286 msglen = strlen(msg) + 2;
291 (void) strcat(str, ": ");
292 (void) strcat(str, msg);
293 return(len - msglen);
297 * str_addarg(str, len, arg, first) - add the string argument "arg" to
298 * the string "str". This is the first in the group when "first"
299 * is set (indicating that a comma should NOT be added to the front).
303 str_addarg(str, len, arg, first)
313 arglen = strlen(arg);
324 (void) strcat(str, ", ");
326 (void) strcat(str, arg);
327 return(len - arglen);
331 * err_compar(p1, p2) - comparison routine used by "qsort"
332 * for sorting errors.
338 register struct errs *p1, *p2;
343 if ((result = p1->errnum - p2->errnum) == 0)
345 return(strcmp(p1->arg, p2->arg));
351 * error_count() - return the number of errors currently logged.
362 * show_errors() - display on stdout the current log of errors.
369 register int cnt = 0;
370 register struct errs *errp = errs;
372 printf("%d error%s:\n\n", errcnt, errcnt == 1 ? "" : "s");
373 while (cnt++ < errcnt)
375 printf("%5s: %s\n", errp->arg,
376 errp->errnum == 0 ? "Not a number" : errmsg(errp->errnum));
382 * kill_procs(str) - send signals to processes, much like the "kill"
383 * command does; invoked in response to 'k'.
386 char *kill_procs(str)
392 int signum = SIGTERM; /* default */
394 struct sigdesc *sigp;
397 /* reset error array */
400 /* remember our uid */
403 /* skip over leading white space */
404 while (isspace(*str)) str++;
408 /* explicit signal specified */
409 if ((nptr = next_field(str)) == NULL)
411 return(" kill: no processes specified");
416 (void) scanint(str + 1, &signum);
417 if (signum <= 0 || signum >= NSIG)
419 return(" invalid signal number");
424 /* translate the name into a number */
425 for (sigp = sigdesc; sigp->name != NULL; sigp++)
427 if (strcmp(sigp->name, str + 1) == 0)
429 signum = sigp->number;
434 /* was it ever found */
435 if (sigp->name == NULL)
437 return(" bad signal name");
440 /* put the new pointer in place */
444 /* loop thru the string, killing processes */
447 if (scanint(str, &procnum) == -1)
453 /* check process owner if we're not root */
454 if (uid && (uid != proc_owner(procnum)))
458 /* go in for the kill */
459 else if (kill(procnum, signum) == -1)
461 /* chalk up an error */
465 } while ((str = next_field(str)) != NULL);
467 /* return appropriate error string */
468 return(err_string());
472 * renice_procs(str) - change the "nice" of processes, much like the
473 * "renice" command does; invoked in response to 'r'.
476 char *renice_procs(str)
481 register char negate;
489 /* allow for negative priority values */
490 if ((negate = (*str == '-')) != 0)
492 /* move past the minus sign */
496 /* use procnum as a temporary holding place and get the number */
497 procnum = scanint(str, &prio);
499 /* negate if necessary */
505 #if defined(PRIO_MIN) && defined(PRIO_MAX)
506 /* check for validity */
507 if (procnum == -1 || prio < PRIO_MIN || prio > PRIO_MAX)
509 return(" bad priority value");
513 /* move to the first process number */
514 if ((str = next_field(str)) == NULL)
516 return(" no processes specified");
519 /* loop thru the process numbers, renicing each one */
522 if (scanint(str, &procnum) == -1)
527 /* check process owner if we're not root */
528 else if (uid && (uid != proc_owner(procnum)))
532 else if (setpriority(PRIO_PROCESS, procnum, prio) == -1)
536 } while ((str = next_field(str)) != NULL);
538 /* return appropriate error string */
539 return(err_string());