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