Adapt to OpenPAM Hydrangea.
[dragonfly.git] / contrib / top / commands.c
blob48d7c8165dff4ec3379256ddf2bf503aa75a6ae7
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: src/contrib/top/commands.c,v 1.4.6.1 2002/08/11 17:09:25 dwmalone Exp $
12 * $DragonFly: src/contrib/top/commands.c,v 1.3 2006/02/15 12:54:36 corecode Exp $
16 * This file contains the routines that implement some of the interactive
17 * mode commands. Note that some of the commands are implemented in-line
18 * in "main". This is necessary because they change the global state of
19 * "top" (i.e.: changing the number of processes to display).
22 #include "os.h"
23 #include <ctype.h>
24 #include <signal.h>
25 #include <errno.h>
26 #include <sys/time.h>
27 #include <sys/resource.h>
29 #include "sigdesc.h" /* generated automatically */
30 #include "top.h"
31 #include "boolean.h"
32 #include "utils.h"
34 extern char *copyright;
36 /* imported from screen.c */
37 extern int overstrike;
39 int err_compar();
40 char *err_string();
43 * show_help() - display the help screen; invoked in response to
44 * either 'h' or '?'.
47 show_help()
50 printf("Top version %s, %s\n", version_string(), copyright);
51 fputs("\n\n\
52 A top users display for Unix\n\
53 \n\
54 These single-character commands are available:\n\
55 \n\
56 ^L - redraw screen\n\
57 q - quit\n\
58 h or ? - help; show this text\n", stdout);
60 /* not all commands are availalbe with overstrike terminals */
61 if (overstrike)
63 fputs("\n\
64 Other commands are also available, but this terminal is not\n\
65 sophisticated enough to handle those commands gracefully.\n\n", stdout);
67 else
69 fputs("\
70 d - change number of displays to show\n\
71 e - list errors generated by last \"kill\" or \"renice\" command\n\
72 i - toggle the displaying of idle processes\n\
73 I - same as 'i'\n\
74 O - only display threads\n\
75 T - toggle the displaying of threads\n\
76 S - toggle the displaying of system processes\n\
77 k - kill processes; send a signal to a list of processes\n\
78 n or # - change number of processes to display\n", stdout);
79 #ifdef ORDER
80 fputs("\
81 o - specify sort order (pri, size, res, cpu, time, thr)\n", stdout);
82 #endif
83 fputs("\
84 r - renice a process\n\
85 s - change number of seconds to delay between updates\n\
86 u - display processes for only one user (+ selects all users)\n\
87 \n\
88 \n", stdout);
93 * Utility routines that help with some of the commands.
96 char *next_field(str)
98 register char *str;
101 if ((str = strchr(str, ' ')) == NULL)
103 return(NULL);
105 *str = '\0';
106 while (*++str == ' ') /* loop */;
108 /* if there is nothing left of the string, return NULL */
109 /* This fix is dedicated to Greg Earle */
110 return(*str == '\0' ? NULL : str);
113 scanint(str, intp)
115 char *str;
116 int *intp;
119 register int val = 0;
120 register char ch;
122 /* if there is nothing left of the string, flag it as an error */
123 /* This fix is dedicated to Greg Earle */
124 if (*str == '\0')
126 return(-1);
129 while ((ch = *str++) != '\0')
131 if (isdigit(ch))
133 val = val * 10 + (ch - '0');
135 else if (isspace(ch))
137 break;
139 else
141 return(-1);
144 *intp = val;
145 return(0);
149 * Some of the commands make system calls that could generate errors.
150 * These errors are collected up in an array of structures for later
151 * contemplation and display. Such routines return a string containing an
152 * error message, or NULL if no errors occurred. The next few routines are
153 * for manipulating and displaying these errors. We need an upper limit on
154 * the number of errors, so we arbitrarily choose 20.
157 #define ERRMAX 20
159 struct errs /* structure for a system-call error */
161 int errnum; /* value of errno (that is, the actual error) */
162 char *arg; /* argument that caused the error */
165 static struct errs errs[ERRMAX];
166 static int errcnt;
167 static char *err_toomany = " too many errors occurred";
168 static char *err_listem =
169 " Many errors occurred. Press `e' to display the list of errors.";
171 /* These macros get used to reset and log the errors */
172 #define ERR_RESET errcnt = 0
173 #define ERROR(p, e) if (errcnt >= ERRMAX) \
175 return(err_toomany); \
177 else \
179 errs[errcnt].arg = (p); \
180 errs[errcnt++].errnum = (e); \
184 * err_string() - return an appropriate error string. This is what the
185 * command will return for displaying. If no errors were logged, then
186 * return NULL. The maximum length of the error string is defined by
187 * "STRMAX".
190 #define STRMAX 80
192 char *err_string()
195 register struct errs *errp;
196 register int cnt = 0;
197 register int first = Yes;
198 register int currerr = -1;
199 int stringlen; /* characters still available in "string" */
200 static char string[STRMAX];
202 /* if there are no errors, return NULL */
203 if (errcnt == 0)
205 return(NULL);
208 /* sort the errors */
209 qsort((char *)errs, errcnt, sizeof(struct errs), err_compar);
211 /* need a space at the front of the error string */
212 string[0] = ' ';
213 string[1] = '\0';
214 stringlen = STRMAX - 2;
216 /* loop thru the sorted list, building an error string */
217 while (cnt < errcnt)
219 errp = &(errs[cnt++]);
220 if (errp->errnum != currerr)
222 if (currerr != -1)
224 if ((stringlen = str_adderr(string, stringlen, currerr)) < 2)
226 return(err_listem);
228 (void) strcat(string, "; "); /* we know there's more */
230 currerr = errp->errnum;
231 first = Yes;
233 if ((stringlen = str_addarg(string, stringlen, errp->arg, first)) ==0)
235 return(err_listem);
237 first = No;
240 /* add final message */
241 stringlen = str_adderr(string, stringlen, currerr);
243 /* return the error string */
244 return(stringlen == 0 ? err_listem : string);
248 * str_adderr(str, len, err) - add an explanation of error "err" to
249 * the string "str".
252 str_adderr(str, len, err)
254 char *str;
255 int len;
256 int err;
259 register char *msg;
260 register int msglen;
262 msg = err == 0 ? "Not a number" : errmsg(err);
263 msglen = strlen(msg) + 2;
264 if (len <= msglen)
266 return(0);
268 (void) strcat(str, ": ");
269 (void) strcat(str, msg);
270 return(len - msglen);
274 * str_addarg(str, len, arg, first) - add the string argument "arg" to
275 * the string "str". This is the first in the group when "first"
276 * is set (indicating that a comma should NOT be added to the front).
279 str_addarg(str, len, arg, first)
281 char *str;
282 int len;
283 char *arg;
284 int first;
287 register int arglen;
289 arglen = strlen(arg);
290 if (!first)
292 arglen += 2;
294 if (len <= arglen)
296 return(0);
298 if (!first)
300 (void) strcat(str, ", ");
302 (void) strcat(str, arg);
303 return(len - arglen);
307 * err_compar(p1, p2) - comparison routine used by "qsort"
308 * for sorting errors.
311 err_compar(p1, p2)
313 register struct errs *p1, *p2;
316 register int result;
318 if ((result = p1->errnum - p2->errnum) == 0)
320 return(strcmp(p1->arg, p2->arg));
322 return(result);
326 * error_count() - return the number of errors currently logged.
329 error_count()
332 return(errcnt);
336 * show_errors() - display on stdout the current log of errors.
339 show_errors()
342 register int cnt = 0;
343 register struct errs *errp = errs;
345 printf("%d error%s:\n\n", errcnt, errcnt == 1 ? "" : "s");
346 while (cnt++ < errcnt)
348 printf("%5s: %s\n", errp->arg,
349 errp->errnum == 0 ? "Not a number" : errmsg(errp->errnum));
350 errp++;
355 * kill_procs(str) - send signals to processes, much like the "kill"
356 * command does; invoked in response to 'k'.
359 char *kill_procs(str)
361 char *str;
364 register char *nptr;
365 int signum = SIGTERM; /* default */
366 int procnum;
367 struct sigdesc *sigp;
368 int uid;
370 /* reset error array */
371 ERR_RESET;
373 /* remember our uid */
374 uid = getuid();
376 /* skip over leading white space */
377 while (isspace(*str)) str++;
379 if (str[0] == '-')
381 /* explicit signal specified */
382 if ((nptr = next_field(str)) == NULL)
384 return(" kill: no processes specified");
387 if (isdigit(str[1]))
389 (void) scanint(str + 1, &signum);
390 if (signum <= 0 || signum >= NSIG)
392 return(" invalid signal number");
395 else
397 /* translate the name into a number */
398 for (sigp = sigdesc; sigp->name != NULL; sigp++)
400 if (strcmp(sigp->name, str + 1) == 0)
402 signum = sigp->number;
403 break;
407 /* was it ever found */
408 if (sigp->name == NULL)
410 return(" bad signal name");
413 /* put the new pointer in place */
414 str = nptr;
417 /* loop thru the string, killing processes */
420 if (scanint(str, &procnum) == -1)
422 ERROR(str, 0);
424 else
426 /* check process owner if we're not root */
427 if (uid && (uid != proc_owner(procnum)))
429 ERROR(str, EACCES);
431 /* go in for the kill */
432 else if (kill(procnum, signum) == -1)
434 /* chalk up an error */
435 ERROR(str, errno);
438 } while ((str = next_field(str)) != NULL);
440 /* return appropriate error string */
441 return(err_string());
445 * renice_procs(str) - change the "nice" of processes, much like the
446 * "renice" command does; invoked in response to 'r'.
449 char *renice_procs(str)
451 char *str;
454 register char negate;
455 int prio;
456 int procnum;
457 int uid;
459 ERR_RESET;
460 uid = getuid();
462 /* allow for negative priority values */
463 if ((negate = (*str == '-')) != 0)
465 /* move past the minus sign */
466 str++;
469 /* use procnum as a temporary holding place and get the number */
470 procnum = scanint(str, &prio);
472 /* negate if necessary */
473 if (negate)
475 prio = -prio;
478 #if defined(PRIO_MIN) && defined(PRIO_MAX)
479 /* check for validity */
480 if (procnum == -1 || prio < PRIO_MIN || prio > PRIO_MAX)
482 return(" bad priority value");
484 #endif
486 /* move to the first process number */
487 if ((str = next_field(str)) == NULL)
489 return(" no processes specified");
492 /* loop thru the process numbers, renicing each one */
495 if (scanint(str, &procnum) == -1)
497 ERROR(str, 0);
500 /* check process owner if we're not root */
501 else if (uid && (uid != proc_owner(procnum)))
503 ERROR(str, EACCES);
505 else if (setpriority(PRIO_PROCESS, procnum, prio) == -1)
507 ERROR(str, errno);
509 } while ((str = next_field(str)) != NULL);
511 /* return appropriate error string */
512 return(err_string());