1 /*- 2 * Copyright (c) 1990, 1993, 1994 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #if 0 31 #ifndef lint 32 static char sccsid[] = "@(#)print.c 8.6 (Berkeley) 4/16/94"; 33 #endif /* not lint */ 34 #endif 35 36 #include <sys/cdefs.h> 37 __FBSDID("$FreeBSD$"); 38 39 #include <sys/param.h> 40 #include <sys/time.h> 41 #include <sys/resource.h> 42 #include <sys/proc.h> 43 #include <sys/stat.h> 44 45 #include <sys/mac.h> 46 #include <sys/user.h> 47 #include <sys/sysctl.h> 48 #include <sys/vmmeter.h> 49 50 #include <err.h> 51 #include <grp.h> 52 #include <langinfo.h> 53 #include <locale.h> 54 #include <math.h> 55 #include <nlist.h> 56 #include <pwd.h> 57 #include <stddef.h> 58 #include <stdint.h> 59 #include <stdio.h> 60 #include <stdlib.h> 61 #include <string.h> 62 #include <unistd.h> 63 #include <vis.h> 64 65 #include "ps.h" 66 67 #define ps_pgtok(a) (((a) * getpagesize()) / 1024) 68 69 void 70 printheader(void) 71 { 72 VAR *v; 73 struct varent *vent; 74 75 STAILQ_FOREACH(vent, &varlist, next_ve) 76 if (*vent->header != '\0') 77 break; 78 if (!vent) 79 return; 80 81 STAILQ_FOREACH(vent, &varlist, next_ve) { 82 v = vent->var; 83 if (v->flag & LJUST) { 84 if (STAILQ_NEXT(vent, next_ve) == NULL) /* last one */ 85 (void)printf("%s", vent->header); 86 else 87 (void)printf("%-*s", v->width, vent->header); 88 } else 89 (void)printf("%*s", v->width, vent->header); 90 if (STAILQ_NEXT(vent, next_ve) != NULL) 91 (void)putchar(' '); 92 } 93 (void)putchar('\n'); 94 } 95 96 void 97 arguments(KINFO *k, VARENT *ve) 98 { 99 VAR *v; 100 int left; 101 char *cp, *vis_args; 102 103 v = ve->var; 104 if ((vis_args = malloc(strlen(k->ki_args) * 4 + 1)) == NULL) 105 errx(1, "malloc failed"); 106 strvis(vis_args, k->ki_args, VIS_TAB | VIS_NL | VIS_NOSLASH); 107 if (STAILQ_NEXT(ve, next_ve) == NULL) { 108 /* last field */ 109 if (termwidth == UNLIMITED) { 110 (void)printf("%s", vis_args); 111 } else { 112 left = termwidth - (totwidth - v->width); 113 if (left < 1) /* already wrapped, just use std width */ 114 left = v->width; 115 for (cp = vis_args; --left >= 0 && *cp != '\0';) 116 (void)putchar(*cp++); 117 } 118 } else { 119 (void)printf("%-*.*s", v->width, v->width, vis_args); 120 } 121 free(vis_args); 122 } 123 124 void 125 command(KINFO *k, VARENT *ve) 126 { 127 VAR *v; 128 int left; 129 char *cp, *vis_env, *vis_args; 130 131 v = ve->var; 132 if (cflag) { 133 /* If it is the last field, then don't pad */ 134 if (STAILQ_NEXT(ve, next_ve) == NULL) { 135 if (k->ki_d.prefix) 136 (void)printf("%s", k->ki_d.prefix); 137 (void)printf("%s", k->ki_p->ki_comm); 138 if (showthreads && k->ki_p->ki_numthreads > 1) 139 (void)printf("/%s", k->ki_p->ki_ocomm); 140 } else 141 (void)printf("%-*s", v->width, k->ki_p->ki_comm); 142 return; 143 } 144 if ((vis_args = malloc(strlen(k->ki_args) * 4 + 1)) == NULL) 145 errx(1, "malloc failed"); 146 strvis(vis_args, k->ki_args, VIS_TAB | VIS_NL | VIS_NOSLASH); 147 148 if (STAILQ_NEXT(ve, next_ve) == NULL) { 149 /* last field */ 150 151 if (k->ki_env) { 152 if ((vis_env = malloc(strlen(k->ki_env) * 4 + 1)) 153 == NULL) 154 errx(1, "malloc failed"); 155 strvis(vis_env, k->ki_env, 156 VIS_TAB | VIS_NL | VIS_NOSLASH); 157 } else 158 vis_env = NULL; 159 160 if (termwidth == UNLIMITED) { 161 if (k->ki_d.prefix) 162 (void)printf("%s", k->ki_d.prefix); 163 if (vis_env) 164 (void)printf("%s ", vis_env); 165 (void)printf("%s", vis_args); 166 } else { 167 left = termwidth - (totwidth - v->width); 168 if (left < 1) /* already wrapped, just use std width */ 169 left = v->width; 170 if ((cp = k->ki_d.prefix) != NULL) 171 while (--left >= 0 && *cp) 172 (void)putchar(*cp++); 173 if ((cp = vis_env) != NULL) { 174 while (--left >= 0 && *cp) 175 (void)putchar(*cp++); 176 if (--left >= 0) 177 putchar(' '); 178 } 179 for (cp = vis_args; --left >= 0 && *cp != '\0';) 180 (void)putchar(*cp++); 181 } 182 if (vis_env != NULL) 183 free(vis_env); 184 } else 185 /* ki_d.prefix & ki_env aren't shown for interim fields */ 186 (void)printf("%-*.*s", v->width, v->width, vis_args); 187 free(vis_args); 188 } 189 190 void 191 ucomm(KINFO *k, VARENT *ve) 192 { 193 char tmpbuff[COMMLEN + OCOMMLEN + 2]; 194 VAR *v; 195 196 v = ve->var; 197 if (STAILQ_NEXT(ve, next_ve) == NULL) { /* last field, don't pad */ 198 if (k->ki_d.prefix) 199 (void)printf("%s", k->ki_d.prefix); 200 (void)printf("%s", k->ki_p->ki_comm); 201 if (showthreads && k->ki_p->ki_numthreads > 1) 202 printf("/%s", k->ki_p->ki_ocomm); 203 } else { 204 bzero(tmpbuff, sizeof(tmpbuff)); 205 if (showthreads && k->ki_p->ki_numthreads > 1) 206 sprintf(tmpbuff, "%s/%s", k->ki_p->ki_comm, 207 k->ki_p->ki_ocomm); 208 else 209 sprintf(tmpbuff, "%s", k->ki_p->ki_comm); 210 (void)printf("%-*s", v->width, tmpbuff); 211 } 212 } 213 214 void 215 tdnam(KINFO *k, VARENT *ve) 216 { 217 VAR *v; 218 219 v = ve->var; 220 if (showthreads && k->ki_p->ki_numthreads > 1) 221 (void)printf("%-*s", v->width, k->ki_p->ki_ocomm); 222 else 223 (void)printf("%-*s", v->width, " "); 224 } 225 226 void 227 logname(KINFO *k, VARENT *ve) 228 { 229 VAR *v; 230 char *s; 231 232 v = ve->var; 233 (void)printf("%-*s", v->width, (s = k->ki_p->ki_login, *s) ? s : "-"); 234 } 235 236 void 237 state(KINFO *k, VARENT *ve) 238 { 239 int flag, tdflags; 240 char *cp; 241 VAR *v; 242 char buf[16]; 243 244 v = ve->var; 245 flag = k->ki_p->ki_flag; 246 tdflags = k->ki_p->ki_tdflags; /* XXXKSE */ 247 cp = buf; 248 249 switch (k->ki_p->ki_stat) { 250 251 case SSTOP: 252 *cp = 'T'; 253 break; 254 255 case SSLEEP: 256 if (tdflags & TDF_SINTR) /* interruptable (long) */ 257 *cp = k->ki_p->ki_slptime >= MAXSLP ? 'I' : 'S'; 258 else 259 *cp = 'D'; 260 break; 261 262 case SRUN: 263 case SIDL: 264 *cp = 'R'; 265 break; 266 267 case SWAIT: 268 *cp = 'W'; 269 break; 270 271 case SLOCK: 272 *cp = 'L'; 273 break; 274 275 case SZOMB: 276 *cp = 'Z'; 277 break; 278 279 default: 280 *cp = '?'; 281 } 282 cp++; 283 if (!(flag & P_INMEM)) 284 *cp++ = 'W'; 285 if (k->ki_p->ki_nice < NZERO) 286 *cp++ = '<'; 287 else if (k->ki_p->ki_nice > NZERO) 288 *cp++ = 'N'; 289 if (flag & P_TRACED) 290 *cp++ = 'X'; 291 if (flag & P_WEXIT && k->ki_p->ki_stat != SZOMB) 292 *cp++ = 'E'; 293 if (flag & P_PPWAIT) 294 *cp++ = 'V'; 295 if ((flag & P_SYSTEM) || k->ki_p->ki_lock > 0) 296 *cp++ = 'L'; 297 if (k->ki_p->ki_kiflag & KI_SLEADER) 298 *cp++ = 's'; 299 if ((flag & P_CONTROLT) && k->ki_p->ki_pgid == k->ki_p->ki_tpgid) 300 *cp++ = '+'; 301 if (flag & P_JAILED) 302 *cp++ = 'J'; 303 *cp = '\0'; 304 (void)printf("%-*s", v->width, buf); 305 } 306 307 #define scalepri(x) ((x) - PZERO) 308 309 void 310 pri(KINFO *k, VARENT *ve) 311 { 312 VAR *v; 313 314 v = ve->var; 315 (void)printf("%*d", v->width, scalepri(k->ki_p->ki_pri.pri_level)); 316 } 317 318 void 319 upr(KINFO *k, VARENT *ve) 320 { 321 VAR *v; 322 323 v = ve->var; 324 (void)printf("%*d", v->width, scalepri(k->ki_p->ki_pri.pri_user)); 325 } 326 #undef scalepri 327 328 void 329 uname(KINFO *k, VARENT *ve) 330 { 331 VAR *v; 332 333 v = ve->var; 334 (void)printf("%-*s", v->width, user_from_uid(k->ki_p->ki_uid, 0)); 335 } 336 337 int 338 s_uname(KINFO *k) 339 { 340 return (strlen(user_from_uid(k->ki_p->ki_uid, 0))); 341 } 342 343 void 344 rgroupname(KINFO *k, VARENT *ve) 345 { 346 VAR *v; 347 348 v = ve->var; 349 (void)printf("%-*s", v->width, group_from_gid(k->ki_p->ki_rgid, 0)); 350 } 351 352 int 353 s_rgroupname(KINFO *k) 354 { 355 return (strlen(group_from_gid(k->ki_p->ki_rgid, 0))); 356 } 357 358 void 359 runame(KINFO *k, VARENT *ve) 360 { 361 VAR *v; 362 363 v = ve->var; 364 (void)printf("%-*s", v->width, user_from_uid(k->ki_p->ki_ruid, 0)); 365 } 366 367 int 368 s_runame(KINFO *k) 369 { 370 return (strlen(user_from_uid(k->ki_p->ki_ruid, 0))); 371 } 372 373 374 void 375 tdev(KINFO *k, VARENT *ve) 376 { 377 VAR *v; 378 dev_t dev; 379 char buff[16]; 380 381 v = ve->var; 382 dev = k->ki_p->ki_tdev; 383 if (dev == NODEV) 384 (void)printf("%*s", v->width, "??"); 385 else { 386 (void)snprintf(buff, sizeof(buff), 387 "%d/%d", major(dev), minor(dev)); 388 (void)printf("%*s", v->width, buff); 389 } 390 } 391 392 void 393 tname(KINFO *k, VARENT *ve) 394 { 395 VAR *v; 396 dev_t dev; 397 char *ttname; 398 399 v = ve->var; 400 dev = k->ki_p->ki_tdev; 401 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL) 402 (void)printf("%*s ", v->width - 1, "??"); 403 else { 404 if (strncmp(ttname, "tty", 3) == 0 || 405 strncmp(ttname, "cua", 3) == 0) 406 ttname += 3; 407 if (strncmp(ttname, "pts/", 4) == 0) 408 ttname += 4; 409 (void)printf("%*.*s%c", v->width - 1, v->width - 1, ttname, 410 k->ki_p->ki_kiflag & KI_CTTY ? ' ' : '-'); 411 } 412 } 413 414 void 415 longtname(KINFO *k, VARENT *ve) 416 { 417 VAR *v; 418 dev_t dev; 419 char *ttname; 420 421 v = ve->var; 422 dev = k->ki_p->ki_tdev; 423 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL) 424 (void)printf("%-*s", v->width, "??"); 425 else 426 (void)printf("%-*s", v->width, ttname); 427 } 428 429 void 430 started(KINFO *k, VARENT *ve) 431 { 432 VAR *v; 433 time_t then; 434 struct tm *tp; 435 static int use_ampm = -1; 436 char buf[100]; 437 438 v = ve->var; 439 if (!k->ki_valid) { 440 (void)printf("%-*s", v->width, "-"); 441 return; 442 } 443 if (use_ampm < 0) 444 use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0'); 445 then = k->ki_p->ki_start.tv_sec; 446 tp = localtime(&then); 447 if (now - k->ki_p->ki_start.tv_sec < 24 * 3600) { 448 (void)strftime(buf, sizeof(buf), 449 use_ampm ? "%l:%M%p" : "%k:%M ", tp); 450 } else if (now - k->ki_p->ki_start.tv_sec < 7 * 86400) { 451 (void)strftime(buf, sizeof(buf), 452 use_ampm ? "%a%I%p" : "%a%H ", tp); 453 } else 454 (void)strftime(buf, sizeof(buf), "%e%b%y", tp); 455 (void)printf("%-*s", v->width, buf); 456 } 457 458 void 459 lstarted(KINFO *k, VARENT *ve) 460 { 461 VAR *v; 462 time_t then; 463 char buf[100]; 464 465 v = ve->var; 466 if (!k->ki_valid) { 467 (void)printf("%-*s", v->width, "-"); 468 return; 469 } 470 then = k->ki_p->ki_start.tv_sec; 471 (void)strftime(buf, sizeof(buf), "%c", localtime(&then)); 472 (void)printf("%-*s", v->width, buf); 473 } 474 475 void 476 lockname(KINFO *k, VARENT *ve) 477 { 478 VAR *v; 479 480 v = ve->var; 481 if (k->ki_p->ki_kiflag & KI_LOCKBLOCK) { 482 if (k->ki_p->ki_lockname[0] != 0) 483 (void)printf("%-*.*s", v->width, v->width, 484 k->ki_p->ki_lockname); 485 else 486 (void)printf("%-*s", v->width, "???"); 487 } else 488 (void)printf("%-*s", v->width, "-"); 489 } 490 491 void 492 wchan(KINFO *k, VARENT *ve) 493 { 494 VAR *v; 495 496 v = ve->var; 497 if (k->ki_p->ki_wchan) { 498 if (k->ki_p->ki_wmesg[0] != 0) 499 (void)printf("%-*.*s", v->width, v->width, 500 k->ki_p->ki_wmesg); 501 else 502 (void)printf("%-*lx", v->width, 503 (long)k->ki_p->ki_wchan); 504 } else 505 (void)printf("%-*s", v->width, "-"); 506 } 507 508 void 509 nwchan(KINFO *k, VARENT *ve) 510 { 511 VAR *v; 512 513 v = ve->var; 514 if (k->ki_p->ki_wchan) { 515 (void)printf("%0*lx", v->width, 516 (long)k->ki_p->ki_wchan); 517 } else 518 (void)printf("%-*s", v->width, "-"); 519 } 520 521 void 522 mwchan(KINFO *k, VARENT *ve) 523 { 524 VAR *v; 525 526 v = ve->var; 527 if (k->ki_p->ki_wchan) { 528 if (k->ki_p->ki_wmesg[0] != 0) 529 (void)printf("%-*.*s", v->width, v->width, 530 k->ki_p->ki_wmesg); 531 else 532 (void)printf("%-*lx", v->width, 533 (long)k->ki_p->ki_wchan); 534 } else if (k->ki_p->ki_kiflag & KI_LOCKBLOCK) { 535 if (k->ki_p->ki_lockname[0]) { 536 (void)printf("%-*.*s", v->width, v->width, 537 k->ki_p->ki_lockname); 538 } else 539 (void)printf("%-*s", v->width, "???"); 540 } else 541 (void)printf("%-*s", v->width, "-"); 542 } 543 544 void 545 vsize(KINFO *k, VARENT *ve) 546 { 547 VAR *v; 548 549 v = ve->var; 550 (void)printf("%*lu", v->width, (u_long)(k->ki_p->ki_size / 1024)); 551 } 552 553 void 554 cputime(KINFO *k, VARENT *ve) 555 { 556 VAR *v; 557 long secs; 558 long psecs; /* "parts" of a second. first micro, then centi */ 559 char obuff[128]; 560 static char decimal_point; 561 562 if (decimal_point == '\0') 563 decimal_point = localeconv()->decimal_point[0]; 564 v = ve->var; 565 if (!k->ki_valid) { 566 secs = 0; 567 psecs = 0; 568 } else { 569 /* 570 * This counts time spent handling interrupts. We could 571 * fix this, but it is not 100% trivial (and interrupt 572 * time fractions only work on the sparc anyway). XXX 573 */ 574 secs = k->ki_p->ki_runtime / 1000000; 575 psecs = k->ki_p->ki_runtime % 1000000; 576 if (sumrusage) { 577 secs += k->ki_p->ki_childtime.tv_sec; 578 psecs += k->ki_p->ki_childtime.tv_usec; 579 } 580 /* 581 * round and scale to 100's 582 */ 583 psecs = (psecs + 5000) / 10000; 584 secs += psecs / 100; 585 psecs = psecs % 100; 586 } 587 (void)snprintf(obuff, sizeof(obuff), "%3ld:%02ld%c%02ld", 588 secs / 60, secs % 60, decimal_point, psecs); 589 (void)printf("%*s", v->width, obuff); 590 } 591 592 void 593 elapsed(KINFO *k, VARENT *ve) 594 { 595 VAR *v; 596 time_t val; 597 int days, hours, mins, secs; 598 char obuff[128]; 599 600 v = ve->var; 601 if (!k->ki_valid) { 602 (void)printf("%-*s", v->width, "-"); 603 return; 604 } 605 val = now - k->ki_p->ki_start.tv_sec; 606 days = val / (24 * 60 * 60); 607 val %= 24 * 60 * 60; 608 hours = val / (60 * 60); 609 val %= 60 * 60; 610 mins = val / 60; 611 secs = val % 60; 612 if (days != 0) 613 (void)snprintf(obuff, sizeof(obuff), "%3d-%02d:%02d:%02d", 614 days, hours, mins, secs); 615 else if (hours != 0) 616 (void)snprintf(obuff, sizeof(obuff), "%02d:%02d:%02d", 617 hours, mins, secs); 618 else 619 (void)snprintf(obuff, sizeof(obuff), "%02d:%02d", mins, secs); 620 (void)printf("%*s", v->width, obuff); 621 } 622 623 void 624 elapseds(KINFO *k, VARENT *ve) 625 { 626 VAR *v; 627 time_t val; 628 629 v = ve->var; 630 if (!k->ki_valid) { 631 (void)printf("%-*s", v->width, "-"); 632 return; 633 } 634 val = now - k->ki_p->ki_start.tv_sec; 635 (void)printf("%*jd", v->width, (intmax_t)val); 636 } 637 638 double 639 getpcpu(const KINFO *k) 640 { 641 static int failure; 642 643 if (!nlistread) 644 failure = donlist(); 645 if (failure) 646 return (0.0); 647 648 #define fxtofl(fixpt) ((double)(fixpt) / fscale) 649 650 /* XXX - I don't like this */ 651 if (k->ki_p->ki_swtime == 0 || (k->ki_p->ki_flag & P_INMEM) == 0) 652 return (0.0); 653 if (rawcpu) 654 return (100.0 * fxtofl(k->ki_p->ki_pctcpu)); 655 return (100.0 * fxtofl(k->ki_p->ki_pctcpu) / 656 (1.0 - exp(k->ki_p->ki_swtime * log(fxtofl(ccpu))))); 657 } 658 659 void 660 pcpu(KINFO *k, VARENT *ve) 661 { 662 VAR *v; 663 664 v = ve->var; 665 (void)printf("%*.1f", v->width, getpcpu(k)); 666 } 667 668 static double 669 getpmem(KINFO *k) 670 { 671 static int failure; 672 double fracmem; 673 674 if (!nlistread) 675 failure = donlist(); 676 if (failure) 677 return (0.0); 678 679 if ((k->ki_p->ki_flag & P_INMEM) == 0) 680 return (0.0); 681 /* XXX want pmap ptpages, segtab, etc. (per architecture) */ 682 /* XXX don't have info about shared */ 683 fracmem = ((float)k->ki_p->ki_rssize) / mempages; 684 return (100.0 * fracmem); 685 } 686 687 void 688 pmem(KINFO *k, VARENT *ve) 689 { 690 VAR *v; 691 692 v = ve->var; 693 (void)printf("%*.1f", v->width, getpmem(k)); 694 } 695 696 void 697 pagein(KINFO *k, VARENT *ve) 698 { 699 VAR *v; 700 701 v = ve->var; 702 (void)printf("%*ld", v->width, 703 k->ki_valid ? k->ki_p->ki_rusage.ru_majflt : 0); 704 } 705 706 /* ARGSUSED */ 707 void 708 maxrss(KINFO *k __unused, VARENT *ve) 709 { 710 VAR *v; 711 712 v = ve->var; 713 /* XXX not yet */ 714 (void)printf("%*s", v->width, "-"); 715 } 716 717 void 718 priorityr(KINFO *k, VARENT *ve) 719 { 720 VAR *v; 721 struct priority *lpri; 722 char str[8]; 723 unsigned class, level; 724 725 v = ve->var; 726 lpri = &k->ki_p->ki_pri; 727 class = lpri->pri_class; 728 level = lpri->pri_level; 729 switch (class) { 730 case PRI_ITHD: 731 snprintf(str, sizeof(str), "intr:%u", level); 732 break; 733 case PRI_REALTIME: 734 snprintf(str, sizeof(str), "real:%u", level); 735 break; 736 case PRI_TIMESHARE: 737 strncpy(str, "normal", sizeof(str)); 738 break; 739 case PRI_IDLE: 740 snprintf(str, sizeof(str), "idle:%u", level); 741 break; 742 default: 743 snprintf(str, sizeof(str), "%u:%u", class, level); 744 break; 745 } 746 str[sizeof(str) - 1] = '\0'; 747 (void)printf("%*s", v->width, str); 748 } 749 750 /* 751 * Generic output routines. Print fields from various prototype 752 * structures. 753 */ 754 static void 755 printval(void *bp, VAR *v) 756 { 757 static char ofmt[32] = "%"; 758 const char *fcp; 759 char *cp; 760 761 cp = ofmt + 1; 762 fcp = v->fmt; 763 if (v->flag & LJUST) 764 *cp++ = '-'; 765 *cp++ = '*'; 766 while ((*cp++ = *fcp++)); 767 768 #define CHKINF127(n) (((n) > 127) && (v->flag & INF127) ? 127 : (n)) 769 770 switch (v->type) { 771 case CHAR: 772 (void)printf(ofmt, v->width, *(char *)bp); 773 break; 774 case UCHAR: 775 (void)printf(ofmt, v->width, *(u_char *)bp); 776 break; 777 case SHORT: 778 (void)printf(ofmt, v->width, *(short *)bp); 779 break; 780 case USHORT: 781 (void)printf(ofmt, v->width, *(u_short *)bp); 782 break; 783 case INT: 784 (void)printf(ofmt, v->width, *(int *)bp); 785 break; 786 case UINT: 787 (void)printf(ofmt, v->width, CHKINF127(*(u_int *)bp)); 788 break; 789 case LONG: 790 (void)printf(ofmt, v->width, *(long *)bp); 791 break; 792 case ULONG: 793 (void)printf(ofmt, v->width, *(u_long *)bp); 794 break; 795 case KPTR: 796 (void)printf(ofmt, v->width, *(u_long *)bp); 797 break; 798 case PGTOK: 799 (void)printf(ofmt, v->width, ps_pgtok(*(u_long *)bp)); 800 break; 801 default: 802 errx(1, "unknown type %d", v->type); 803 } 804 } 805 806 void 807 kvar(KINFO *k, VARENT *ve) 808 { 809 VAR *v; 810 811 v = ve->var; 812 printval((char *)((char *)k->ki_p + v->off), v); 813 } 814 815 void 816 rvar(KINFO *k, VARENT *ve) 817 { 818 VAR *v; 819 820 v = ve->var; 821 if (k->ki_valid) 822 printval((char *)((char *)(&k->ki_p->ki_rusage) + v->off), v); 823 else 824 (void)printf("%*s", v->width, "-"); 825 } 826 827 void 828 emulname(KINFO *k, VARENT *ve) 829 { 830 VAR *v; 831 832 v = ve->var; 833 printf("%-*s", v->width, *k->ki_p->ki_emul ? k->ki_p->ki_emul : "-"); 834 } 835 836 void 837 label(KINFO *k, VARENT *ve) 838 { 839 char *string; 840 VAR *v; 841 mac_t proclabel; 842 int error; 843 844 v = ve->var; 845 string = NULL; 846 if (mac_prepare_process_label(&proclabel) == -1) { 847 warn("mac_prepare_process_label"); 848 goto out; 849 } 850 error = mac_get_pid(k->ki_p->ki_pid, proclabel); 851 if (error == 0) { 852 if (mac_to_text(proclabel, &string) == -1) 853 string = NULL; 854 } 855 mac_free(proclabel); 856 out: 857 if (string != NULL) { 858 (void)printf("%-*s", v->width, string); 859 free(string); 860 } else 861 (void)printf("%-*s", v->width, " -"); 862 return; 863 } 864 865 int 866 s_comm(KINFO *k) 867 { 868 char tmpbuff[COMMLEN + OCOMMLEN + 2]; 869 870 bzero(tmpbuff, sizeof(tmpbuff)); 871 if (showthreads && k->ki_p->ki_numthreads > 1) 872 sprintf(tmpbuff, "%s/%s", k->ki_p->ki_comm, 873 k->ki_p->ki_ocomm); 874 else 875 sprintf(tmpbuff, "%s", k->ki_p->ki_comm); 876 return (strlen(tmpbuff)); 877 } 878 879 int 880 s_label(KINFO *k) 881 { 882 char *string = NULL; 883 mac_t proclabel; 884 int error, size = 0; 885 886 if (mac_prepare_process_label(&proclabel) == -1) { 887 warn("mac_prepare_process_label"); 888 return (0); 889 } 890 error = mac_get_pid(k->ki_p->ki_pid, proclabel); 891 if (error == 0 && mac_to_text(proclabel, &string) == 0) { 892 size = strlen(string); 893 free(string); 894 } 895 mac_free(proclabel); 896 return (size); 897 } 898