turns printfs back on
[freebsd-src/fkvm-freebsd.git] / contrib / top / commands.c
blob83f966d5e21b9cc98fad3ab2b43c98e8ac863730
1 /*
2 * Top users/processes display for Unix
3 * Version 3
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
11 * $FreeBSD$
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 "os.h"
22 #include <ctype.h>
23 #include <signal.h>
24 #include <errno.h>
25 #include <sys/time.h>
26 #include <sys/resource.h>
28 #include "sigdesc.h" /* generated automatically */
29 #include "top.h"
30 #include "boolean.h"
31 #include "utils.h"
33 extern int errno;
35 extern char *copyright;
37 /* imported from screen.c */
38 extern int overstrike;
40 int err_compar();
41 char *err_string();
44 * show_help() - display the help screen; invoked in response to
45 * either 'h' or '?'.
48 show_help()
51 printf("Top version %s, %s\n", version_string(), copyright);
52 fputs("\n\n\
53 A top users display for Unix\n\
54 \n\
55 These single-character commands are available:\n\
56 \n\
57 ^L - redraw screen\n\
58 q - quit\n\
59 h or ? - help; show this text\n", stdout);
61 /* not all commands are availalbe with overstrike terminals */
62 if (overstrike)
64 fputs("\n\
65 Other commands are also available, but this terminal is not\n\
66 sophisticated enough to handle those commands gracefully.\n\n", stdout);
68 else
70 fputs("\
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);
80 #ifdef ORDER
81 if (displaymode == DISP_CPU)
82 fputs("\
83 o - specify sort order (pri, size, res, cpu, time, threads)\n", stdout);
84 else
85 fputs("\
86 o - specify sort order (vcsw, ivcsw, read, write, fault, total)\n", stdout);
87 #endif
88 fputs("\
89 r - renice a process\n\
90 s - change number of seconds to delay between updates\n\
91 S - toggle the displaying of system processes\n\
92 a - toggle the displaying of process titles\n\
93 t - toggle the display of this process\n\
94 u - display processes for only one user (+ selects all users)\n\
95 \n\
96 \n", stdout);
101 * Utility routines that help with some of the commands.
104 char *next_field(str)
106 register char *str;
109 if ((str = strchr(str, ' ')) == NULL)
111 return(NULL);
113 *str = '\0';
114 while (*++str == ' ') /* loop */;
116 /* if there is nothing left of the string, return NULL */
117 /* This fix is dedicated to Greg Earle */
118 return(*str == '\0' ? NULL : str);
121 scanint(str, intp)
123 char *str;
124 int *intp;
127 register int val = 0;
128 register char ch;
130 /* if there is nothing left of the string, flag it as an error */
131 /* This fix is dedicated to Greg Earle */
132 if (*str == '\0')
134 return(-1);
137 while ((ch = *str++) != '\0')
139 if (isdigit(ch))
141 val = val * 10 + (ch - '0');
143 else if (isspace(ch))
145 break;
147 else
149 return(-1);
152 *intp = val;
153 return(0);
157 * Some of the commands make system calls that could generate errors.
158 * These errors are collected up in an array of structures for later
159 * contemplation and display. Such routines return a string containing an
160 * error message, or NULL if no errors occurred. The next few routines are
161 * for manipulating and displaying these errors. We need an upper limit on
162 * the number of errors, so we arbitrarily choose 20.
165 #define ERRMAX 20
167 struct errs /* structure for a system-call error */
169 int errnum; /* value of errno (that is, the actual error) */
170 char *arg; /* argument that caused the error */
173 static struct errs errs[ERRMAX];
174 static int errcnt;
175 static char *err_toomany = " too many errors occurred";
176 static char *err_listem =
177 " Many errors occurred. Press `e' to display the list of errors.";
179 /* These macros get used to reset and log the errors */
180 #define ERR_RESET errcnt = 0
181 #define ERROR(p, e) if (errcnt >= ERRMAX) \
183 return(err_toomany); \
185 else \
187 errs[errcnt].arg = (p); \
188 errs[errcnt++].errnum = (e); \
192 * err_string() - return an appropriate error string. This is what the
193 * command will return for displaying. If no errors were logged, then
194 * return NULL. The maximum length of the error string is defined by
195 * "STRMAX".
198 #define STRMAX 80
200 char *err_string()
203 register struct errs *errp;
204 register int cnt = 0;
205 register int first = Yes;
206 register int currerr = -1;
207 int stringlen; /* characters still available in "string" */
208 static char string[STRMAX];
210 /* if there are no errors, return NULL */
211 if (errcnt == 0)
213 return(NULL);
216 /* sort the errors */
217 qsort((char *)errs, errcnt, sizeof(struct errs), err_compar);
219 /* need a space at the front of the error string */
220 string[0] = ' ';
221 string[1] = '\0';
222 stringlen = STRMAX - 2;
224 /* loop thru the sorted list, building an error string */
225 while (cnt < errcnt)
227 errp = &(errs[cnt++]);
228 if (errp->errnum != currerr)
230 if (currerr != -1)
232 if ((stringlen = str_adderr(string, stringlen, currerr)) < 2)
234 return(err_listem);
236 (void) strcat(string, "; "); /* we know there's more */
238 currerr = errp->errnum;
239 first = Yes;
241 if ((stringlen = str_addarg(string, stringlen, errp->arg, first)) ==0)
243 return(err_listem);
245 first = No;
248 /* add final message */
249 stringlen = str_adderr(string, stringlen, currerr);
251 /* return the error string */
252 return(stringlen == 0 ? err_listem : string);
256 * str_adderr(str, len, err) - add an explanation of error "err" to
257 * the string "str".
260 str_adderr(str, len, err)
262 char *str;
263 int len;
264 int err;
267 register char *msg;
268 register int msglen;
270 msg = err == 0 ? "Not a number" : errmsg(err);
271 msglen = strlen(msg) + 2;
272 if (len <= msglen)
274 return(0);
276 (void) strcat(str, ": ");
277 (void) strcat(str, msg);
278 return(len - msglen);
282 * str_addarg(str, len, arg, first) - add the string argument "arg" to
283 * the string "str". This is the first in the group when "first"
284 * is set (indicating that a comma should NOT be added to the front).
287 str_addarg(str, len, arg, first)
289 char *str;
290 int len;
291 char *arg;
292 int first;
295 register int arglen;
297 arglen = strlen(arg);
298 if (!first)
300 arglen += 2;
302 if (len <= arglen)
304 return(0);
306 if (!first)
308 (void) strcat(str, ", ");
310 (void) strcat(str, arg);
311 return(len - arglen);
315 * err_compar(p1, p2) - comparison routine used by "qsort"
316 * for sorting errors.
319 err_compar(p1, p2)
321 register struct errs *p1, *p2;
324 register int result;
326 if ((result = p1->errnum - p2->errnum) == 0)
328 return(strcmp(p1->arg, p2->arg));
330 return(result);
334 * error_count() - return the number of errors currently logged.
337 error_count()
340 return(errcnt);
344 * show_errors() - display on stdout the current log of errors.
347 show_errors()
350 register int cnt = 0;
351 register struct errs *errp = errs;
353 printf("%d error%s:\n\n", errcnt, errcnt == 1 ? "" : "s");
354 while (cnt++ < errcnt)
356 printf("%5s: %s\n", errp->arg,
357 errp->errnum == 0 ? "Not a number" : errmsg(errp->errnum));
358 errp++;
363 * kill_procs(str) - send signals to processes, much like the "kill"
364 * command does; invoked in response to 'k'.
367 char *kill_procs(str)
369 char *str;
372 register char *nptr;
373 int signum = SIGTERM; /* default */
374 int procnum;
375 struct sigdesc *sigp;
376 int uid;
378 /* reset error array */
379 ERR_RESET;
381 /* remember our uid */
382 uid = getuid();
384 /* skip over leading white space */
385 while (isspace(*str)) str++;
387 if (str[0] == '-')
389 /* explicit signal specified */
390 if ((nptr = next_field(str)) == NULL)
392 return(" kill: no processes specified");
395 if (isdigit(str[1]))
397 (void) scanint(str + 1, &signum);
398 if (signum <= 0 || signum >= NSIG)
400 return(" invalid signal number");
403 else
405 /* translate the name into a number */
406 for (sigp = sigdesc; sigp->name != NULL; sigp++)
408 if (strcmp(sigp->name, str + 1) == 0)
410 signum = sigp->number;
411 break;
415 /* was it ever found */
416 if (sigp->name == NULL)
418 return(" bad signal name");
421 /* put the new pointer in place */
422 str = nptr;
425 /* loop thru the string, killing processes */
428 if (scanint(str, &procnum) == -1)
430 ERROR(str, 0);
432 else
434 /* check process owner if we're not root */
435 if (uid && (uid != proc_owner(procnum)))
437 ERROR(str, EACCES);
439 /* go in for the kill */
440 else if (kill(procnum, signum) == -1)
442 /* chalk up an error */
443 ERROR(str, errno);
446 } while ((str = next_field(str)) != NULL);
448 /* return appropriate error string */
449 return(err_string());
453 * renice_procs(str) - change the "nice" of processes, much like the
454 * "renice" command does; invoked in response to 'r'.
457 char *renice_procs(str)
459 char *str;
462 register char negate;
463 int prio;
464 int procnum;
465 int uid;
467 ERR_RESET;
468 uid = getuid();
470 /* allow for negative priority values */
471 if ((negate = (*str == '-')) != 0)
473 /* move past the minus sign */
474 str++;
477 /* use procnum as a temporary holding place and get the number */
478 procnum = scanint(str, &prio);
480 /* negate if necessary */
481 if (negate)
483 prio = -prio;
486 #if defined(PRIO_MIN) && defined(PRIO_MAX)
487 /* check for validity */
488 if (procnum == -1 || prio < PRIO_MIN || prio > PRIO_MAX)
490 return(" bad priority value");
492 #endif
494 /* move to the first process number */
495 if ((str = next_field(str)) == NULL)
497 return(" no processes specified");
500 /* loop thru the process numbers, renicing each one */
503 if (scanint(str, &procnum) == -1)
505 ERROR(str, 0);
508 /* check process owner if we're not root */
509 else if (uid && (uid != proc_owner(procnum)))
511 ERROR(str, EACCES);
513 else if (setpriority(PRIO_PROCESS, procnum, prio) == -1)
515 ERROR(str, errno);
517 } while ((str = next_field(str)) != NULL);
519 /* return appropriate error string */
520 return(err_string());