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).
21 #include <sys/resource.h>
22 #include <sys/signal.h>
37 static int err_compar(const void *p1, const void *p2);
39 struct errs /* structure for a system-call error */
41 int errnum; /* value of errno (that is, the actual error) */
42 char *arg; /* argument that caused the error */
45 static char *err_string(void);
46 static int str_adderr(char *str, int len, int err);
47 static int str_addarg(char *str, int len, char *arg, bool first);
50 * show_help() - display the help screen; invoked in response to
54 const struct command all_commands[] =
56 {'C', "toggle the displaying of weighted CPU percentage", false, CMD_wcputog},
57 {'d', "change number of displays to show", false, CMD_displays},
58 {'e', "list errors generated by last \"kill\" or \"renice\" command", false, CMD_errors},
59 {'H', "toggle the displaying of threads", false, CMD_thrtog},
60 {'h', "show this help text", true, CMD_help},
61 {'?', NULL, true, CMD_help},
62 {'/', "filter on command name (+ selects all commands)", false, CMD_grep},
63 {'i', "toggle the displaying of idle processes", false, CMD_idletog},
64 {'I', NULL, false, CMD_idletog},
65 {'j', "toggle the displaying of jail ID", false, CMD_jidtog},
66 {'J', "display processes for only one jail (+ selects all jails)", false, CMD_jail},
67 {'k', "kill processes; send a signal to a list of processes", false, CMD_kill},
68 {'q', "quit" , true, CMD_quit},
69 {'m', "toggle the display between 'cpu' and 'io' modes", false, CMD_viewtog},
70 {'n', "change number of processes to display", false, CMD_number},
71 {'#', NULL, false, CMD_number},
72 {'o', "specify the sort order", false, CMD_order},
73 {'p', "display one process (+ selects all processes)", false, CMD_pid},
74 {'P', "toggle the displaying of per-CPU statistics", false, CMD_pcputog},
75 {'r', "renice a process", false, CMD_renice},
76 {'s', "change number of seconds to delay between updates", false, CMD_delay},
77 {'S', "toggle the displaying of system processes", false, CMD_viewsys},
78 {'a', "toggle the displaying of process titles", false, CMD_showargs},
79 {'T', "toggle the displaying of thread IDs", false, CMD_toggletid},
80 {'t', "toggle the display of this process", false, CMD_selftog},
81 {'u', "display processes for only one user (+ selects all users)", false, CMD_user},
82 {'w', "toggle the display of swap use for each process", false, CMD_swaptog},
83 {'z', "toggle the displaying of the system idle process", false, CMD_kidletog},
84 {' ', "update the display", false, CMD_update},
85 {0, NULL, true, CMD_NONE}
91 const struct command *curcmd, *nextcmd;
93 _Static_assert(sizeof(keys) >= sizeof("a or b"), "keys right size");
95 printf("Top version FreeBSD, %s\n", copyright);
96 curcmd = all_commands;
97 while (curcmd->c != 0) {
98 if (overstrike && !curcmd->available_to_dumb) {
102 if (curcmd->desc == NULL) {
103 /* we already printed this */
107 nextcmd = curcmd + 1;
108 if (nextcmd->desc == NULL && nextcmd->c != '\0') {
109 sprintf(keys, "%c or %c", curcmd->c, nextcmd->c);
110 } else if (curcmd->c == ' '){
111 /* special case space rather than introducing a "display string" to
113 sprintf(keys, "SPC");
115 sprintf(keys, "%c", curcmd->c);
117 printf("%s\t- %s\n", keys, curcmd->desc);
123 Other commands are also available, but this terminal is not\n\
124 sophisticated enough to handle those commands gracefully.\n", stdout);
129 * Utility routines that help with some of the commands.
133 next_field(char *str)
135 if ((str = strchr(str, ' ')) == NULL)
140 while (*++str == ' ') /* loop */;
142 /* if there is nothing left of the string, return NULL */
143 /* This fix is dedicated to Greg Earle */
144 return(*str == '\0' ? NULL : str);
148 scanint(char *str, int *intp)
153 /* if there is nothing left of the string, flag it as an error */
154 /* This fix is dedicated to Greg Earle */
160 while ((ch = *str++) != '\0')
164 val = val * 10 + (ch - '0');
166 else if (isspace(ch))
180 * Some of the commands make system calls that could generate errors.
181 * These errors are collected up in an array of structures for later
182 * contemplation and display. Such routines return a string containing an
183 * error message, or NULL if no errors occurred. The next few routines are
184 * for manipulating and displaying these errors. We need an upper limit on
185 * the number of errors, so we arbitrarily choose 20.
190 static struct errs errs[ERRMAX];
192 static char err_toomany[] = " too many errors occurred";
193 static char err_listem[] =
194 " Many errors occurred. Press `e' to display the list of errors.";
196 /* These macros get used to reset and log the errors */
197 #define ERR_RESET errcnt = 0
198 #define ERROR(p, e) if (errcnt >= ERRMAX) \
200 return(err_toomany); \
204 errs[errcnt].arg = (p); \
205 errs[errcnt++].errnum = (e); \
209 * err_string() - return an appropriate error string. This is what the
210 * command will return for displaying. If no errors were logged, then
211 * return NULL. The maximum length of the error string is defined by
224 int stringlen; /* characters still available in "string" */
225 static char string[STRMAX];
227 /* if there are no errors, return NULL */
233 /* sort the errors */
234 qsort((char *)errs, errcnt, sizeof(struct errs), err_compar);
236 /* need a space at the front of the error string */
239 stringlen = STRMAX - 2;
241 /* loop thru the sorted list, building an error string */
244 errp = &(errs[cnt++]);
245 if (errp->errnum != currerr)
249 if ((stringlen = str_adderr(string, stringlen, currerr)) < 2)
253 strcat(string, "; "); /* we know there's more */
255 currerr = errp->errnum;
258 if ((stringlen = str_addarg(string, stringlen, errp->arg, first)) ==0)
265 /* add final message */
266 stringlen = str_adderr(string, stringlen, currerr);
268 /* return the error string */
269 return(stringlen == 0 ? err_listem : string);
273 * str_adderr(str, len, err) - add an explanation of error "err" to
278 str_adderr(char *str, int len, int err)
283 msg = err == 0 ? "Not a number" : strerror(err);
284 msglen = strlen(msg) + 2;
291 return(len - msglen);
295 * str_addarg(str, len, arg, first) - add the string argument "arg" to
296 * the string "str". This is the first in the group when "first"
297 * is set (indicating that a comma should NOT be added to the front).
301 str_addarg(char str[], int len, char arg[], bool first)
305 arglen = strlen(arg);
319 return(len - arglen);
323 * err_compar(p1, p2) - comparison routine used by "qsort"
324 * for sorting errors.
328 err_compar(const void *p1, const void *p2)
331 const struct errs * const g1 = (const struct errs * const)p1;
332 const struct errs * const g2 = (const struct errs * const)p2;
336 if ((result = g1->errnum - g2->errnum) == 0)
338 return(strcmp(g1->arg, g2->arg));
344 * error_count() - return the number of errors currently logged.
354 * show_errors() - display on stdout the current log of errors.
361 struct errs *errp = errs;
363 printf("%d error%s:\n\n", errcnt, errcnt == 1 ? "" : "s");
364 while (cnt++ < errcnt)
366 printf("%5s: %s\n", errp->arg,
367 errp->errnum == 0 ? "Not a number" : strerror(errp->errnum));
372 static const char no_proc_specified[] = " no processes specified";
373 static const char invalid_signal_number[] = " invalid_signal_number";
374 static const char bad_signal_name[] = " bad signal name";
375 static const char bad_pri_value[] = " bad priority value";
378 signame_to_signum(const char * sig)
382 if (strncasecmp(sig, "SIG", 3) == 0)
384 for (n = 1; n < sys_nsig; n++) {
385 if (!strcasecmp(sys_signame[n], sig))
392 * kill_procs(str) - send signals to processes, much like the "kill"
393 * command does; invoked in response to 'k'.
397 kill_procs(char *str)
400 int signum = SIGTERM; /* default */
403 /* reset error array */
406 /* skip over leading white space */
407 while (isspace(*str)) str++;
411 /* explicit signal specified */
412 if ((nptr = next_field(str)) == NULL)
414 return(no_proc_specified);
419 scanint(str + 1, &signum);
420 if (signum <= 0 || signum >= NSIG)
422 return(invalid_signal_number);
427 signum = signame_to_signum(str + 1);
429 /* was it ever found */
432 return(bad_signal_name);
435 /* put the new pointer in place */
439 /* loop thru the string, killing processes */
442 if (scanint(str, &procnum) == -1)
448 /* go in for the kill */
449 if (kill(procnum, signum) == -1)
451 /* chalk up an error */
455 } while ((str = next_field(str)) != NULL);
457 /* return appropriate error string */
458 return(err_string());
462 * renice_procs(str) - change the "nice" of processes, much like the
463 * "renice" command does; invoked in response to 'r'.
467 renice_procs(char *str)
475 /* allow for negative priority values */
476 if ((negate = (*str == '-')) != 0)
478 /* move past the minus sign */
482 /* use procnum as a temporary holding place and get the number */
483 procnum = scanint(str, &prio);
485 /* negate if necessary */
491 /* check for validity */
492 if (procnum == -1 || prio < PRIO_MIN || prio > PRIO_MAX)
494 return(bad_pri_value);
497 /* move to the first process number */
498 if ((str = next_field(str)) == NULL)
500 return(no_proc_specified);
503 /* loop thru the process numbers, renicing each one */
506 if (scanint(str, &procnum) == -1)
511 if (setpriority(PRIO_PROCESS, procnum, prio) == -1)
515 } while ((str = next_field(str)) != NULL);
517 /* return appropriate error string */
518 return(err_string());