Annotation of src/usr.bin/top/commands.c, Revision 1.17
1.17 ! markus 1: /* $OpenBSD: commands.c,v 1.16 2004/10/07 06:26:12 otto Exp $ */
1.1 downsj 2:
3: /*
4: * Top users/processes display for Unix
5: * Version 3
6: *
1.6 deraadt 7: * Copyright (c) 1984, 1989, William LeFebvre, Rice University
8: * Copyright (c) 1989, 1990, 1992, William LeFebvre, Northwestern University
1.1 downsj 9: *
1.6 deraadt 10: * Redistribution and use in source and binary forms, with or without
11: * modification, are permitted provided that the following conditions
12: * are met:
13: * 1. Redistributions of source code must retain the above copyright
14: * notice, this list of conditions and the following disclaimer.
15: * 2. Redistributions in binary form must reproduce the above copyright
16: * notice, this list of conditions and the following disclaimer in the
17: * documentation and/or other materials provided with the distribution.
18: *
19: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22: * IN NO EVENT SHALL THE AUTHOR OR HIS EMPLOYER BE LIABLE FOR ANY DIRECT, INDIRECT,
23: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.1 downsj 29: */
30:
31: /*
32: * This file contains the routines that implement some of the interactive
33: * mode commands. Note that some of the commands are implemented in-line
34: * in "main". This is necessary because they change the global state of
35: * "top" (i.e.: changing the number of processes to display).
36: */
37:
1.2 downsj 38: #include <sys/types.h>
39: #include <stdio.h>
1.1 downsj 40: #include <ctype.h>
1.2 downsj 41: #include <errno.h>
42: #include <stdlib.h>
43: #include <string.h>
1.1 downsj 44: #include <signal.h>
1.2 downsj 45: #include <unistd.h>
1.1 downsj 46: #include <sys/time.h>
47: #include <sys/resource.h>
48:
1.2 downsj 49: #include "top.h"
50:
1.1 downsj 51: #include "sigdesc.h" /* generated automatically */
52: #include "boolean.h"
53: #include "utils.h"
1.2 downsj 54: #include "machine.h"
1.1 downsj 55:
1.9 deraadt 56: static char *next_field(char *);
57: static int scanint(char *, int *);
58: static char *err_string(void);
59: static size_t str_adderr(char *, size_t, int);
60: static size_t str_addarg(char *, size_t, char *, int);
61: static int err_compar(const void *, const void *);
1.1 downsj 62:
63: /*
64: * show_help() - display the help screen; invoked in response to
65: * either 'h' or '?'.
66: */
1.2 downsj 67: void
1.8 pvalchev 68: show_help(void)
1.1 downsj 69: {
1.9 deraadt 70: printf("Top version %s, %s\n", version_string(), copyright);
71: fputs("\n\n"
72: "A top users display for Unix\n"
73: "\n"
74: "These single-character commands are available:\n"
75: "\n"
76: "^L - redraw screen\n"
1.13 jmc 77: "h or ? - help; show this text\n"
78: "q - quit\n", stdout);
1.9 deraadt 79:
80: /* not all commands are available with overstrike terminals */
81: if (overstrike) {
82: fputs("\n"
83: "Other commands are also available, but this terminal is not\n"
84: "sophisticated enough to handle those commands gracefully.\n\n",
85: stdout);
86: } else {
87: fputs(
88: "d - change number of displays to show\n"
89: "e - list errors generated by last \"kill\" or \"renice\" command\n"
90: "i - toggle the displaying of idle processes\n"
91: "I - same as 'i'\n"
92: "k - kill processes; send a signal to a list of processes\n"
93: "n or # - change number of processes to display\n", stdout);
94: fputs(
1.16 otto 95: "o - specify sort order (size, res, cpu, time)\n"
96: "p - display one process (+ selects all processes)\n",
1.9 deraadt 97: stdout);
98: fputs(
99: "r - renice a process\n"
100: "s - change number of seconds to delay between updates\n"
1.13 jmc 101: "S - toggle the display of system processes\n"
1.9 deraadt 102: "u - display processes for only one user (+ selects all users)\n"
1.17 ! markus 103: "C - toggle the display of the command line arguments\n"
1.9 deraadt 104: "\n\n", stdout);
105: }
1.1 downsj 106: }
107:
108: /*
109: * Utility routines that help with some of the commands.
110: */
1.8 pvalchev 111: static char *
112: next_field(char *str)
1.1 downsj 113: {
1.9 deraadt 114: if ((str = strchr(str, ' ')) == NULL)
115: return (NULL);
116:
117: *str = '\0';
118: while (*++str == ' ') /* loop */
119: ;
120:
121: /* if there is nothing left of the string, return NULL */
122: /* This fix is dedicated to Greg Earle */
123: return (*str == '\0' ? NULL : str);
1.1 downsj 124: }
125:
1.14 deraadt 126: static int
1.9 deraadt 127: scanint(char *str, int *intp)
1.1 downsj 128: {
1.9 deraadt 129: int val = 0;
130: char ch;
1.1 downsj 131:
1.9 deraadt 132: /* if there is nothing left of the string, flag it as an error */
133: /* This fix is dedicated to Greg Earle */
134: if (*str == '\0')
135: return (-1);
136:
137: while ((ch = *str++) != '\0') {
138: if (isdigit(ch))
139: val = val * 10 + (ch - '0');
140: else if (isspace(ch))
141: break;
142: else
143: return (-1);
1.1 downsj 144: }
1.9 deraadt 145: *intp = val;
146: return (0);
1.1 downsj 147: }
148:
149: /*
150: * Some of the commands make system calls that could generate errors.
151: * These errors are collected up in an array of structures for later
152: * contemplation and display. Such routines return a string containing an
153: * error message, or NULL if no errors occurred. The next few routines are
154: * for manipulating and displaying these errors. We need an upper limit on
155: * the number of errors, so we arbitrarily choose 20.
156: */
157:
158: #define ERRMAX 20
159:
1.9 deraadt 160: struct errs { /* structure for a system-call error */
1.15 marc 161: int err; /* value of errno (that is, the actual error) */
1.9 deraadt 162: char *arg; /* argument that caused the error */
1.1 downsj 163: };
164:
165: static struct errs errs[ERRMAX];
1.9 deraadt 166: static int errcnt;
167: static char *err_toomany = " too many errors occurred";
168: static char *err_listem =
1.1 downsj 169: " Many errors occurred. Press `e' to display the list of errors.";
170:
171: /* These macros get used to reset and log the errors */
172: #define ERR_RESET errcnt = 0
1.9 deraadt 173: #define ERROR(p, e) \
174: if (errcnt >= ERRMAX) { \
175: return(err_toomany); \
176: } else { \
177: errs[errcnt].arg = (p); \
1.15 marc 178: errs[errcnt++].err = (e); \
1.9 deraadt 179: }
180:
181: #define STRMAX 80
1.1 downsj 182:
183: /*
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".
188: */
1.8 pvalchev 189: static char *
190: err_string(void)
1.1 downsj 191: {
1.9 deraadt 192: int cnt = 0, first = Yes, currerr = -1;
193: static char string[STRMAX];
194: struct errs *errp;
195:
196: /* if there are no errors, return NULL */
197: if (errcnt == 0)
198: return (NULL);
199:
200: /* sort the errors */
1.12 deraadt 201: qsort(errs, errcnt, sizeof(struct errs), err_compar);
1.9 deraadt 202:
203: /* need a space at the front of the error string */
204: string[0] = ' ';
205: string[1] = '\0';
206:
207: /* loop thru the sorted list, building an error string */
208: while (cnt < errcnt) {
209: errp = &(errs[cnt++]);
1.15 marc 210: if (errp->err != currerr) {
1.9 deraadt 211: if (currerr != -1) {
212: if (str_adderr(string, sizeof string, currerr) >
213: sizeof string - 2)
214: return (err_listem);
215:
216: /* we know there's more */
217: (void) strlcat(string, "; ", sizeof string);
218: }
1.15 marc 219: currerr = errp->err;
1.9 deraadt 220: first = Yes;
1.1 downsj 221: }
1.9 deraadt 222: if (str_addarg(string, sizeof string, errp->arg, first) >=
223: sizeof string)
224: return (err_listem);
225:
226: first = No;
1.1 downsj 227: }
228:
1.9 deraadt 229: /* add final message */
230: if (str_adderr(string, sizeof string, currerr) >= sizeof string)
231: return (err_listem);
1.1 downsj 232:
1.9 deraadt 233: /* return the error string */
234: return (string);
1.1 downsj 235: }
236:
237: /*
238: * str_adderr(str, len, err) - add an explanation of error "err" to
239: * the string "str".
240: */
1.8 pvalchev 241: static size_t
242: str_adderr(char *str, size_t len, int err)
1.1 downsj 243: {
1.9 deraadt 244: size_t msglen;
245: char *msg;
1.1 downsj 246:
1.9 deraadt 247: msg = err == 0 ? "Not a number" : strerror(err);
1.8 pvalchev 248:
1.9 deraadt 249: if ((msglen = strlcat(str, ": ", len)) >= len)
250: return (msglen);
1.8 pvalchev 251:
1.9 deraadt 252: return (strlcat(str, msg, len));
1.1 downsj 253: }
254:
255: /*
256: * str_addarg(str, len, arg, first) - add the string argument "arg" to
257: * the string "str". This is the first in the group when "first"
258: * is set (indicating that a comma should NOT be added to the front).
259: */
1.8 pvalchev 260: static size_t
261: str_addarg(char *str, size_t len, char *arg, int first)
1.1 downsj 262: {
1.9 deraadt 263: size_t msglen;
1.1 downsj 264:
1.9 deraadt 265: if (!first) {
266: if ((msglen = strlcat(str, ", ", len)) >= len)
267: return (msglen);
268: }
269: return (strlcat(str, arg, len));
1.1 downsj 270: }
271:
272: /*
273: * err_compar(p1, p2) - comparison routine used by "qsort"
274: * for sorting errors.
275: */
1.8 pvalchev 276: static int
277: err_compar(const void *e1, const void *e2)
1.1 downsj 278: {
1.9 deraadt 279: const struct errs *p1 = (struct errs *) e1;
280: const struct errs *p2 = (struct errs *) e2;
281: int result;
282:
1.15 marc 283: if ((result = p1->err - p2->err) == 0)
1.9 deraadt 284: return (strcmp(p1->arg, p2->arg));
285: return (result);
1.1 downsj 286: }
287:
288: /*
289: * error_count() - return the number of errors currently logged.
290: */
1.8 pvalchev 291: int
292: error_count(void)
1.1 downsj 293: {
1.9 deraadt 294: return (errcnt);
1.1 downsj 295: }
296:
297: /*
298: * show_errors() - display on stdout the current log of errors.
299: */
1.8 pvalchev 300: void
301: show_errors(void)
1.1 downsj 302: {
1.9 deraadt 303: struct errs *errp = errs;
304: int cnt = 0;
1.1 downsj 305:
1.9 deraadt 306: printf("%d error%s:\n\n", errcnt, errcnt == 1 ? "" : "s");
307: while (cnt++ < errcnt) {
308: printf("%5s: %s\n", errp->arg,
1.15 marc 309: errp->err == 0 ? "Not a number" : strerror(errp->err));
1.9 deraadt 310: errp++;
311: }
1.1 downsj 312: }
313:
314: /*
315: * kill_procs(str) - send signals to processes, much like the "kill"
316: * command does; invoked in response to 'k'.
317: */
1.8 pvalchev 318: char *
319: kill_procs(char *str)
1.1 downsj 320: {
1.10 millert 321: int signum = SIGTERM, procnum;
1.9 deraadt 322: struct sigdesc *sigp;
1.11 jfb 323: uid_t uid, puid;
1.9 deraadt 324: char *nptr;
325:
326: /* reset error array */
327: ERR_RESET;
328:
329: /* remember our uid */
330: uid = getuid();
331:
332: /* skip over leading white space */
333: while (isspace(*str))
334: str++;
335:
336: if (str[0] == '-') {
337: /* explicit signal specified */
338: if ((nptr = next_field(str)) == NULL)
339: return (" kill: no processes specified");
340:
341: if (isdigit(str[1])) {
342: (void) scanint(str + 1, &signum);
343: if (signum <= 0 || signum >= NSIG)
344: return (" invalid signal number");
345: } else {
346: /* translate the name into a number */
347: for (sigp = sigdesc; sigp->name != NULL; sigp++) {
348: if (strcmp(sigp->name, str + 1) == 0) {
349: signum = sigp->number;
350: break;
351: }
352: }
353:
354: /* was it ever found */
355: if (sigp->name == NULL)
356: return (" bad signal name");
357: }
358: /* put the new pointer in place */
359: str = nptr;
1.1 downsj 360: }
1.9 deraadt 361: /* loop thru the string, killing processes */
362: do {
363: if (scanint(str, &procnum) == -1) {
364: ERROR(str, 0);
365: } else {
366: /* check process owner if we're not root */
1.11 jfb 367: puid = proc_owner(procnum);
368: if (puid == (uid_t)(-1)) {
369: ERROR(str, ESRCH);
370: } else if (uid && (uid != puid)) {
1.9 deraadt 371: ERROR(str, EACCES);
372: } else if (kill(procnum, signum) == -1) {
373: ERROR(str, errno);
374: }
1.1 downsj 375: }
1.9 deraadt 376: } while ((str = next_field(str)) != NULL);
1.1 downsj 377:
1.9 deraadt 378: /* return appropriate error string */
379: return (err_string());
1.1 downsj 380: }
381:
382: /*
383: * renice_procs(str) - change the "nice" of processes, much like the
384: * "renice" command does; invoked in response to 'r'.
385: */
1.8 pvalchev 386: char *
387: renice_procs(char *str)
1.1 downsj 388: {
1.10 millert 389: uid_t uid;
1.9 deraadt 390: char negate;
1.10 millert 391: int prio, procnum;
1.9 deraadt 392:
393: ERR_RESET;
394: uid = getuid();
395:
396: /* allow for negative priority values */
397: if ((negate = (*str == '-')) != 0) {
398: /* move past the minus sign */
399: str++;
400: }
401: /* use procnum as a temporary holding place and get the number */
402: procnum = scanint(str, &prio);
403:
404: /* negate if necessary */
405: if (negate)
406: prio = -prio;
1.1 downsj 407:
408: #if defined(PRIO_MIN) && defined(PRIO_MAX)
1.9 deraadt 409: /* check for validity */
410: if (procnum == -1 || prio < PRIO_MIN || prio > PRIO_MAX)
411: return (" bad priority value");
1.1 downsj 412: #endif
413:
1.9 deraadt 414: /* move to the first process number */
415: if ((str = next_field(str)) == NULL)
416: return (" no processes specified");
417:
418: /* loop thru the process numbers, renicing each one */
419: do {
420: if (scanint(str, &procnum) == -1) {
421: ERROR(str, 0);
422: }
423: /* check process owner if we're not root */
424: else if (uid && (uid != proc_owner(procnum))) {
425: ERROR(str, EACCES);
426: } else if (setpriority(PRIO_PROCESS, procnum, prio) == -1) {
427: ERROR(str, errno);
428: }
429: } while ((str = next_field(str)) != NULL);
1.1 downsj 430:
1.9 deraadt 431: /* return appropriate error string */
432: return (err_string());
1.1 downsj 433: }