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 r - renice a process\n\
92 s - change number of seconds to delay between updates\n\
93 S - toggle the displaying of system processes\n\
94 a - toggle the displaying of process titles\n\
95 t - toggle the display of this process\n\
96 u - display processes for only one user (+ selects all users)\n\
103 * Utility routines that help with some of the commands.
106 char *next_field(str)
111 if ((str = strchr(str, ' ')) == NULL)
116 while (*++str == ' ') /* loop */;
118 /* if there is nothing left of the string, return NULL */
119 /* This fix is dedicated to Greg Earle */
120 return(*str == '\0' ? NULL : str);
129 register int val = 0;
132 /* if there is nothing left of the string, flag it as an error */
133 /* This fix is dedicated to Greg Earle */
139 while ((ch = *str++) != '\0')
143 val = val * 10 + (ch - '0');
145 else if (isspace(ch))
159 * Some of the commands make system calls that could generate errors.
160 * These errors are collected up in an array of structures for later
161 * contemplation and display. Such routines return a string containing an
162 * error message, or NULL if no errors occurred. The next few routines are
163 * for manipulating and displaying these errors. We need an upper limit on
164 * the number of errors, so we arbitrarily choose 20.
169 struct errs /* structure for a system-call error */
171 int errnum; /* value of errno (that is, the actual error) */
172 char *arg; /* argument that caused the error */
175 static struct errs errs[ERRMAX];
177 static char *err_toomany = " too many errors occurred";
178 static char *err_listem =
179 " Many errors occurred. Press `e' to display the list of errors.";
181 /* These macros get used to reset and log the errors */
182 #define ERR_RESET errcnt = 0
183 #define ERROR(p, e) if (errcnt >= ERRMAX) \
185 return(err_toomany); \
189 errs[errcnt].arg = (p); \
190 errs[errcnt++].errnum = (e); \
194 * err_string() - return an appropriate error string. This is what the
195 * command will return for displaying. If no errors were logged, then
196 * return NULL. The maximum length of the error string is defined by
205 register struct errs *errp;
206 register int cnt = 0;
207 register int first = Yes;
208 register int currerr = -1;
209 int stringlen; /* characters still available in "string" */
210 static char string[STRMAX];
212 /* if there are no errors, return NULL */
218 /* sort the errors */
219 qsort((char *)errs, errcnt, sizeof(struct errs), err_compar);
221 /* need a space at the front of the error string */
224 stringlen = STRMAX - 2;
226 /* loop thru the sorted list, building an error string */
229 errp = &(errs[cnt++]);
230 if (errp->errnum != currerr)
234 if ((stringlen = str_adderr(string, stringlen, currerr)) < 2)
238 (void) strcat(string, "; "); /* we know there's more */
240 currerr = errp->errnum;
243 if ((stringlen = str_addarg(string, stringlen, errp->arg, first)) ==0)
250 /* add final message */
251 stringlen = str_adderr(string, stringlen, currerr);
253 /* return the error string */
254 return(stringlen == 0 ? err_listem : string);
258 * str_adderr(str, len, err) - add an explanation of error "err" to
262 str_adderr(str, len, err)
272 msg = err == 0 ? "Not a number" : errmsg(err);
273 msglen = strlen(msg) + 2;
278 (void) strcat(str, ": ");
279 (void) strcat(str, msg);
280 return(len - msglen);
284 * str_addarg(str, len, arg, first) - add the string argument "arg" to
285 * the string "str". This is the first in the group when "first"
286 * is set (indicating that a comma should NOT be added to the front).
289 str_addarg(str, len, arg, first)
299 arglen = strlen(arg);
310 (void) strcat(str, ", ");
312 (void) strcat(str, arg);
313 return(len - arglen);
317 * err_compar(p1, p2) - comparison routine used by "qsort"
318 * for sorting errors.
323 register struct errs *p1, *p2;
328 if ((result = p1->errnum - p2->errnum) == 0)
330 return(strcmp(p1->arg, p2->arg));
336 * error_count() - return the number of errors currently logged.
346 * show_errors() - display on stdout the current log of errors.
352 register int cnt = 0;
353 register struct errs *errp = errs;
355 printf("%d error%s:\n\n", errcnt, errcnt == 1 ? "" : "s");
356 while (cnt++ < errcnt)
358 printf("%5s: %s\n", errp->arg,
359 errp->errnum == 0 ? "Not a number" : errmsg(errp->errnum));
365 * kill_procs(str) - send signals to processes, much like the "kill"
366 * command does; invoked in response to 'k'.
369 char *kill_procs(str)
375 int signum = SIGTERM; /* default */
377 struct sigdesc *sigp;
380 /* reset error array */
383 /* remember our uid */
386 /* skip over leading white space */
387 while (isspace(*str)) str++;
391 /* explicit signal specified */
392 if ((nptr = next_field(str)) == NULL)
394 return(" kill: no processes specified");
399 (void) scanint(str + 1, &signum);
400 if (signum <= 0 || signum >= NSIG)
402 return(" invalid signal number");
407 /* translate the name into a number */
408 for (sigp = sigdesc; sigp->name != NULL; sigp++)
410 if (strcmp(sigp->name, str + 1) == 0)
412 signum = sigp->number;
417 /* was it ever found */
418 if (sigp->name == NULL)
420 return(" bad signal name");
423 /* put the new pointer in place */
427 /* loop thru the string, killing processes */
430 if (scanint(str, &procnum) == -1)
436 /* check process owner if we're not root */
437 if (uid && (uid != proc_owner(procnum)))
441 /* go in for the kill */
442 else if (kill(procnum, signum) == -1)
444 /* chalk up an error */
448 } while ((str = next_field(str)) != NULL);
450 /* return appropriate error string */
451 return(err_string());
455 * renice_procs(str) - change the "nice" of processes, much like the
456 * "renice" command does; invoked in response to 'r'.
459 char *renice_procs(str)
464 register char negate;
472 /* allow for negative priority values */
473 if ((negate = (*str == '-')) != 0)
475 /* move past the minus sign */
479 /* use procnum as a temporary holding place and get the number */
480 procnum = scanint(str, &prio);
482 /* negate if necessary */
488 #if defined(PRIO_MIN) && defined(PRIO_MAX)
489 /* check for validity */
490 if (procnum == -1 || prio < PRIO_MIN || prio > PRIO_MAX)
492 return(" bad priority value");
496 /* move to the first process number */
497 if ((str = next_field(str)) == NULL)
499 return(" no processes specified");
502 /* loop thru the process numbers, renicing each one */
505 if (scanint(str, &procnum) == -1)
510 /* check process owner if we're not root */
511 else if (uid && (uid != proc_owner(procnum)))
515 else if (setpriority(PRIO_PROCESS, procnum, prio) == -1)
519 } while ((str = next_field(str)) != NULL);
521 /* return appropriate error string */
522 return(err_string());