2 * Top users/processes display for Unix
4 * This program may be freely redistributed,
5 * but this entire comment MUST remain intact.
7 * Copyright (c) 1984, 1989, William LeFebvre, Rice University
8 * Copyright (c) 1989, 1990, 1992, William LeFebvre, Northwestern University
14 * This file contains the routines that implement some of the interactive
15 * mode commands. Note that some of the commands are implemented in-line
16 * in "main". This is necessary because they change the global state of
17 * "top" (i.e.: changing the number of processes to display).
20 #include <sys/resource.h>
32 #include "sigdesc.h" /* generated automatically */
36 static int err_compar(const void *p1, const void *p2);
38 struct errs /* structure for a system-call error */
40 int errnum; /* value of errno (that is, the actual error) */
41 char *arg; /* argument that caused the error */
44 static char *err_string(void);
45 static int str_adderr(char *str, int len, int err);
46 static int str_addarg(char *str, int len, char *arg, int first);
49 * show_help() - display the help screen; invoked in response to
56 printf("Top version FreeBSD, %s\n", copyright);
58 A top users display for Unix\n\
60 These single-character commands are available:\n\
64 h or ? - help; show this text\n", stdout);
66 /* not all commands are availalbe with overstrike terminals */
70 Other commands are also available, but this terminal is not\n\
71 sophisticated enough to handle those commands gracefully.\n\n", stdout);
76 C - toggle the displaying of weighted CPU percentage\n\
77 d - change number of displays to show\n\
78 e - list errors generated by last \"kill\" or \"renice\" command\n\
79 H - toggle the displaying of threads\n\
80 i or I - toggle the displaying of idle processes\n\
81 j - toggle the displaying of jail ID\n\
82 J - display processes for only one jail (+ selects all jails)\n\
83 k - kill processes; send a signal to a list of processes\n\
84 m - toggle the display between 'cpu' and 'io' modes\n\
85 n or # - change number of processes to display\n", stdout);
86 if (displaymode == DISP_CPU)
88 o - specify sort order (pri, size, res, cpu, time, threads, jid, pid)\n",
92 o - specify sort order (vcsw, ivcsw, read, write, fault, total, jid, pid)\n",
95 P - toggle the displaying of per-CPU statistics\n\
96 r - renice a process\n\
97 s - change number of seconds to delay between updates\n\
98 S - toggle the displaying of system processes\n\
99 a - toggle the displaying of process titles\n\
100 t - toggle the display of this process\n\
101 u - display processes for only one user (+ selects all users)\n\
102 w - toggle the display of swap use for each process\n\
103 z - toggle the displaying of the system idle process\n\
110 * Utility routines that help with some of the commands.
114 next_field(char *str)
116 if ((str = strchr(str, ' ')) == NULL)
121 while (*++str == ' ') /* loop */;
123 /* if there is nothing left of the string, return NULL */
124 /* This fix is dedicated to Greg Earle */
125 return(*str == '\0' ? NULL : str);
129 scanint(char *str, int *intp)
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 static struct errs errs[ERRMAX];
173 static char err_toomany[] = " too many errors occurred";
174 static char err_listem[] =
175 " Many errors occurred. Press `e' to display the list of errors.";
177 /* These macros get used to reset and log the errors */
178 #define ERR_RESET errcnt = 0
179 #define ERROR(p, e) if (errcnt >= ERRMAX) \
181 return(err_toomany); \
185 errs[errcnt].arg = (p); \
186 errs[errcnt++].errnum = (e); \
190 * err_string() - return an appropriate error string. This is what the
191 * command will return for displaying. If no errors were logged, then
192 * return NULL. The maximum length of the error string is defined by
198 char *err_string(void)
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 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
258 str_adderr(char *str, int len, int err)
263 msg = err == 0 ? "Not a number" : strerror(err);
264 msglen = strlen(msg) + 2;
271 return(len - msglen);
275 * str_addarg(str, len, arg, first) - add the string argument "arg" to
276 * the string "str". This is the first in the group when "first"
277 * is set (indicating that a comma should NOT be added to the front).
281 str_addarg(char str[], int len, char arg[], int first)
285 arglen = strlen(arg);
299 return(len - arglen);
303 * err_compar(p1, p2) - comparison routine used by "qsort"
304 * for sorting errors.
308 err_compar(const void *p1, const void *p2)
311 const struct errs * const g1 = (const struct errs * const)p1;
312 const struct errs * const g2 = (const struct errs * const)p2;
316 if ((result = g1->errnum - g2->errnum) == 0)
318 return(strcmp(g1->arg, g2->arg));
324 * error_count() - return the number of errors currently logged.
334 * show_errors() - display on stdout the current log of errors.
341 struct errs *errp = errs;
343 printf("%d error%s:\n\n", errcnt, errcnt == 1 ? "" : "s");
344 while (cnt++ < errcnt)
346 printf("%5s: %s\n", errp->arg,
347 errp->errnum == 0 ? "Not a number" : strerror(errp->errnum));
352 static char no_proc_specified[] = " no processes specified";
353 static char invalid_signal_number[] = " invalid_signal_number";
354 static char bad_signal_name[] = " bad signal name";
355 static char bad_pri_value[] = " bad priority value";
358 * kill_procs(str) - send signals to processes, much like the "kill"
359 * command does; invoked in response to 'k'.
363 kill_procs(char *str)
366 int signum = SIGTERM; /* default */
368 struct sigdesc *sigp;
371 /* reset error array */
374 /* remember our uid */
377 /* skip over leading white space */
378 while (isspace(*str)) str++;
382 /* explicit signal specified */
383 if ((nptr = next_field(str)) == NULL)
385 return(no_proc_specified);
390 scanint(str + 1, &signum);
391 if (signum <= 0 || signum >= NSIG)
393 return(invalid_signal_number);
398 /* translate the name into a number */
399 for (sigp = sigdesc; sigp->name != NULL; sigp++)
401 if (strcmp(sigp->name, str + 1) == 0)
403 signum = sigp->number;
408 /* was it ever found */
409 if (sigp->name == NULL)
411 return(bad_signal_name);
414 /* put the new pointer in place */
418 /* loop thru the string, killing processes */
421 if (scanint(str, &procnum) == -1)
427 /* check process owner if we're not root */
428 if (uid && (uid != proc_owner(procnum)))
432 /* go in for the kill */
433 else if (kill(procnum, signum) == -1)
435 /* chalk up an error */
439 } while ((str = next_field(str)) != NULL);
441 /* return appropriate error string */
442 return(err_string());
446 * renice_procs(str) - change the "nice" of processes, much like the
447 * "renice" command does; invoked in response to 'r'.
451 renice_procs(char *str)
461 /* allow for negative priority values */
462 if ((negate = (*str == '-')) != 0)
464 /* move past the minus sign */
468 /* use procnum as a temporary holding place and get the number */
469 procnum = scanint(str, &prio);
471 /* negate if necessary */
477 /* check for validity */
478 if (procnum == -1 || prio < PRIO_MIN || prio > PRIO_MAX)
480 return(bad_pri_value);
483 /* move to the first process number */
484 if ((str = next_field(str)) == NULL)
486 return(no_proc_specified);
489 /* loop thru the process numbers, renicing each one */
492 if (scanint(str, &procnum) == -1)
497 /* check process owner if we're not root */
498 else if (uid && (uid != proc_owner(procnum)))
502 else if (setpriority(PRIO_PROCESS, procnum, prio) == -1)
506 } while ((str = next_field(str)) != NULL);
508 /* return appropriate error string */
509 return(err_string());