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