]> CyberLeo.Net >> Repos - FreeBSD/releng/8.2.git/blob - contrib/top/commands.c
Copy stable/8 to releng/8.2 in preparation for FreeBSD-8.2 release.
[FreeBSD/releng/8.2.git] / contrib / top / commands.c
1 /*
2  *  Top users/processes display for Unix
3  *  Version 3
4  *
5  *  This program may be freely redistributed,
6  *  but this entire comment MUST remain intact.
7  *
8  *  Copyright (c) 1984, 1989, William LeFebvre, Rice University
9  *  Copyright (c) 1989, 1990, 1992, William LeFebvre, Northwestern University
10  *
11  * $FreeBSD$
12  */
13
14 /*
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).
19  */
20
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>
27
28 #include "sigdesc.h"            /* generated automatically */
29 #include "top.h"
30 #include "boolean.h"
31 #include "utils.h"
32
33 extern int  errno;
34
35 extern char *copyright;
36
37 /* imported from screen.c */
38 extern int overstrike;
39
40 int err_compar();
41 char *err_string();
42
43 /*
44  *  show_help() - display the help screen; invoked in response to
45  *              either 'h' or '?'.
46  */
47
48 show_help()
49
50 {
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);
60
61     /* not all commands are availalbe with overstrike terminals */
62     if (overstrike)
63     {
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);
67     }
68     else
69     {
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, jid)\n",
84             stdout);
85         else
86                 fputs("\
87 o       - specify sort order (vcsw, ivcsw, read, write, fault, total, jid)\n",
88             stdout);
89 #endif
90         fputs("\
91 r       - renice a process\n\
92 s       - change number of seconds to delay between updates\n\
93 S       - toggle the displaying of system processes\n\
94 a       - toggle the displaying of process titles\n\
95 t       - toggle the display of this process\n\
96 u       - display processes for only one user (+ selects all users)\n\
97 \n\
98 \n", stdout);
99     }
100 }
101
102 /*
103  *  Utility routines that help with some of the commands.
104  */
105
106 char *next_field(str)
107
108 register char *str;
109
110 {
111     if ((str = strchr(str, ' ')) == NULL)
112     {
113         return(NULL);
114     }
115     *str = '\0';
116     while (*++str == ' ') /* loop */;
117
118     /* if there is nothing left of the string, return NULL */
119     /* This fix is dedicated to Greg Earle */
120     return(*str == '\0' ? NULL : str);
121 }
122
123 scanint(str, intp)
124
125 char *str;
126 int  *intp;
127
128 {
129     register int val = 0;
130     register char ch;
131
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     {
136         return(-1);
137     }
138
139     while ((ch = *str++) != '\0')
140     {
141         if (isdigit(ch))
142         {
143             val = val * 10 + (ch - '0');
144         }
145         else if (isspace(ch))
146         {
147             break;
148         }
149         else
150         {
151             return(-1);
152         }
153     }
154     *intp = val;
155     return(0);
156 }
157
158 /*
159  *  Some of the commands make system calls that could generate errors.
160  *  These errors are collected up in an array of structures for later
161  *  contemplation and display.  Such routines return a string containing an
162  *  error message, or NULL if no errors occurred.  The next few routines are
163  *  for manipulating and displaying these errors.  We need an upper limit on
164  *  the number of errors, so we arbitrarily choose 20.
165  */
166
167 #define ERRMAX 20
168
169 struct errs             /* structure for a system-call error */
170 {
171     int  errnum;        /* value of errno (that is, the actual error) */
172     char *arg;          /* argument that caused the error */
173 };
174
175 static struct errs errs[ERRMAX];
176 static int errcnt;
177 static char *err_toomany = " too many errors occurred";
178 static char *err_listem = 
179         " Many errors occurred.  Press `e' to display the list of errors.";
180
181 /* These macros get used to reset and log the errors */
182 #define ERR_RESET   errcnt = 0
183 #define ERROR(p, e) if (errcnt >= ERRMAX) \
184                     { \
185                         return(err_toomany); \
186                     } \
187                     else \
188                     { \
189                         errs[errcnt].arg = (p); \
190                         errs[errcnt++].errnum = (e); \
191                     }
192
193 /*
194  *  err_string() - return an appropriate error string.  This is what the
195  *      command will return for displaying.  If no errors were logged, then
196  *      return NULL.  The maximum length of the error string is defined by
197  *      "STRMAX".
198  */
199
200 #define STRMAX 80
201
202 char *err_string()
203
204 {
205     register struct errs *errp;
206     register int  cnt = 0;
207     register int  first = Yes;
208     register int  currerr = -1;
209     int stringlen;              /* characters still available in "string" */
210     static char string[STRMAX];
211
212     /* if there are no errors, return NULL */
213     if (errcnt == 0)
214     {
215         return(NULL);
216     }
217
218     /* sort the errors */
219     qsort((char *)errs, errcnt, sizeof(struct errs), err_compar);
220
221     /* need a space at the front of the error string */
222     string[0] = ' ';
223     string[1] = '\0';
224     stringlen = STRMAX - 2;
225
226     /* loop thru the sorted list, building an error string */
227     while (cnt < errcnt)
228     {
229         errp = &(errs[cnt++]);
230         if (errp->errnum != currerr)
231         {
232             if (currerr != -1)
233             {
234                 if ((stringlen = str_adderr(string, stringlen, currerr)) < 2)
235                 {
236                     return(err_listem);
237                 }
238                 (void) strcat(string, "; ");      /* we know there's more */
239             }
240             currerr = errp->errnum;
241             first = Yes;
242         }
243         if ((stringlen = str_addarg(string, stringlen, errp->arg, first)) ==0)
244         {
245             return(err_listem);
246         }
247         first = No;
248     }
249
250     /* add final message */
251     stringlen = str_adderr(string, stringlen, currerr);
252
253     /* return the error string */
254     return(stringlen == 0 ? err_listem : string);
255 }
256
257 /*
258  *  str_adderr(str, len, err) - add an explanation of error "err" to
259  *      the string "str".
260  */
261
262 str_adderr(str, len, err)
263
264 char *str;
265 int len;
266 int err;
267
268 {
269     register char *msg;
270     register int  msglen;
271
272     msg = err == 0 ? "Not a number" : errmsg(err);
273     msglen = strlen(msg) + 2;
274     if (len <= msglen)
275     {
276         return(0);
277     }
278     (void) strcat(str, ": ");
279     (void) strcat(str, msg);
280     return(len - msglen);
281 }
282
283 /*
284  *  str_addarg(str, len, arg, first) - add the string argument "arg" to
285  *      the string "str".  This is the first in the group when "first"
286  *      is set (indicating that a comma should NOT be added to the front).
287  */
288
289 str_addarg(str, len, arg, first)
290
291 char *str;
292 int  len;
293 char *arg;
294 int  first;
295
296 {
297     register int arglen;
298
299     arglen = strlen(arg);
300     if (!first)
301     {
302         arglen += 2;
303     }
304     if (len <= arglen)
305     {
306         return(0);
307     }
308     if (!first)
309     {
310         (void) strcat(str, ", ");
311     }
312     (void) strcat(str, arg);
313     return(len - arglen);
314 }
315
316 /*
317  *  err_compar(p1, p2) - comparison routine used by "qsort"
318  *      for sorting errors.
319  */
320
321 err_compar(p1, p2)
322
323 register struct errs *p1, *p2;
324
325 {
326     register int result;
327
328     if ((result = p1->errnum - p2->errnum) == 0)
329     {
330         return(strcmp(p1->arg, p2->arg));
331     }
332     return(result);
333 }
334
335 /*
336  *  error_count() - return the number of errors currently logged.
337  */
338
339 error_count()
340
341 {
342     return(errcnt);
343 }
344
345 /*
346  *  show_errors() - display on stdout the current log of errors.
347  */
348
349 show_errors()
350
351 {
352     register int cnt = 0;
353     register struct errs *errp = errs;
354
355     printf("%d error%s:\n\n", errcnt, errcnt == 1 ? "" : "s");
356     while (cnt++ < errcnt)
357     {
358         printf("%5s: %s\n", errp->arg,
359             errp->errnum == 0 ? "Not a number" : errmsg(errp->errnum));
360         errp++;
361     }
362 }
363
364 /*
365  *  kill_procs(str) - send signals to processes, much like the "kill"
366  *              command does; invoked in response to 'k'.
367  */
368
369 char *kill_procs(str)
370
371 char *str;
372
373 {
374     register char *nptr;
375     int signum = SIGTERM;       /* default */
376     int procnum;
377     struct sigdesc *sigp;
378     int uid;
379
380     /* reset error array */
381     ERR_RESET;
382
383     /* remember our uid */
384     uid = getuid();
385
386     /* skip over leading white space */
387     while (isspace(*str)) str++;
388
389     if (str[0] == '-')
390     {
391         /* explicit signal specified */
392         if ((nptr = next_field(str)) == NULL)
393         {
394             return(" kill: no processes specified");
395         }
396
397         if (isdigit(str[1]))
398         {
399             (void) scanint(str + 1, &signum);
400             if (signum <= 0 || signum >= NSIG)
401             {
402                 return(" invalid signal number");
403             }
404         }
405         else 
406         {
407             /* translate the name into a number */
408             for (sigp = sigdesc; sigp->name != NULL; sigp++)
409             {
410                 if (strcmp(sigp->name, str + 1) == 0)
411                 {
412                     signum = sigp->number;
413                     break;
414                 }
415             }
416
417             /* was it ever found */
418             if (sigp->name == NULL)
419             {
420                 return(" bad signal name");
421             }
422         }
423         /* put the new pointer in place */
424         str = nptr;
425     }
426
427     /* loop thru the string, killing processes */
428     do
429     {
430         if (scanint(str, &procnum) == -1)
431         {
432             ERROR(str, 0);
433         }
434         else
435         {
436             /* check process owner if we're not root */
437             if (uid && (uid != proc_owner(procnum)))
438             {
439                 ERROR(str, EACCES);
440             }
441             /* go in for the kill */
442             else if (kill(procnum, signum) == -1)
443             {
444                 /* chalk up an error */
445                 ERROR(str, errno);
446             }
447         }
448     } while ((str = next_field(str)) != NULL);
449
450     /* return appropriate error string */
451     return(err_string());
452 }
453
454 /*
455  *  renice_procs(str) - change the "nice" of processes, much like the
456  *              "renice" command does; invoked in response to 'r'.
457  */
458
459 char *renice_procs(str)
460
461 char *str;
462
463 {
464     register char negate;
465     int prio;
466     int procnum;
467     int uid;
468
469     ERR_RESET;
470     uid = getuid();
471
472     /* allow for negative priority values */
473     if ((negate = (*str == '-')) != 0)
474     {
475         /* move past the minus sign */
476         str++;
477     }
478
479     /* use procnum as a temporary holding place and get the number */
480     procnum = scanint(str, &prio);
481
482     /* negate if necessary */
483     if (negate)
484     {
485         prio = -prio;
486     }
487
488 #if defined(PRIO_MIN) && defined(PRIO_MAX)
489     /* check for validity */
490     if (procnum == -1 || prio < PRIO_MIN || prio > PRIO_MAX)
491     {
492         return(" bad priority value");
493     }
494 #endif
495
496     /* move to the first process number */
497     if ((str = next_field(str)) == NULL)
498     {
499         return(" no processes specified");
500     }
501
502     /* loop thru the process numbers, renicing each one */
503     do
504     {
505         if (scanint(str, &procnum) == -1)
506         {
507             ERROR(str, 0);
508         }
509
510         /* check process owner if we're not root */
511         else if (uid && (uid != proc_owner(procnum)))
512         {
513             ERROR(str, EACCES);
514         }
515         else if (setpriority(PRIO_PROCESS, procnum, prio) == -1)
516         {
517             ERROR(str, errno);
518         }
519     } while ((str = next_field(str)) != NULL);
520
521     /* return appropriate error string */
522     return(err_string());
523 }
524