1 /* $OpenBSD: print.c,v 1.82 2022/02/15 23:16:00 rob Exp $ */ 2 /* $NetBSD: print.c,v 1.27 1995/09/29 21:58:12 cgd Exp $ */ 3 4 /*- 5 * Copyright (c) 1990, 1993, 1994 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #include <sys/param.h> /* PZERO NODEV */ 34 #include <sys/types.h> 35 #include <sys/signal.h> 36 #include <sys/proc.h> 37 #include <sys/stat.h> 38 39 #include <sys/sysctl.h> 40 #define PLEDGENAMES 41 #include <sys/pledge.h> 42 43 #include <err.h> 44 #include <grp.h> 45 #include <kvm.h> 46 #include <math.h> 47 #include <nlist.h> 48 #include <stddef.h> 49 #include <stdio.h> 50 #include <stdlib.h> 51 #include <string.h> 52 #include <unistd.h> 53 #include <limits.h> 54 #include <pwd.h> 55 56 #include "ps.h" 57 58 extern kvm_t *kd; 59 extern int needenv, needcomm, neednlist, commandonly; 60 61 int mbswprint(const char *, int, int); /* utf8.c */ 62 63 static char *cmdpart(char *); 64 65 #define min(a,b) ((a) < (b) ? (a) : (b)) 66 67 static char * 68 cmdpart(char *arg0) 69 { 70 char *cp; 71 72 return ((cp = strrchr(arg0, '/')) != NULL ? cp + 1 : arg0); 73 } 74 75 void 76 printheader(void) 77 { 78 VAR *v; 79 struct varent *vent; 80 81 if (!needheader) 82 return; 83 for (vent = vhead; vent; vent = vent->next) { 84 v = vent->var; 85 if (v->flag & LJUST) { 86 if (vent->next == NULL) /* last one */ 87 (void)printf("%s", v->header); 88 else 89 (void)printf("%-*s", v->width, v->header); 90 } else 91 (void)printf("%*s", v->width, v->header); 92 if (vent->next != NULL) 93 (void)putchar(' '); 94 } 95 (void)putchar('\n'); 96 } 97 98 void 99 command(const struct kinfo_proc *kp, VARENT *ve) 100 { 101 VAR *v; 102 int left, wantspace = 0; 103 char **p; 104 105 /* 106 * Determine the available number of display columns. 107 * Always decrement and check after writing. 108 * No check is needed before mbswprint() 109 * and after writing the last data, though. 110 */ 111 112 v = ve->var; 113 if (ve->next != NULL || termwidth != UNLIMITED) { 114 if (ve->next == NULL) { 115 left = termwidth - (totwidth - v->width); 116 if (left < 1) /* already wrapped, just use std width */ 117 left = v->width; 118 } else 119 left = v->width; 120 } else 121 left = INT_MAX; 122 123 if (needenv && kd != NULL) { 124 char **envp = kvm_getenvv(kd, kp, termwidth); 125 if ((p = envp) != NULL) { 126 while (*p) { 127 if (wantspace) { 128 putchar(' '); 129 left--; 130 } 131 left -= mbswprint(*p, left, 0); 132 if (left == 0) 133 return; 134 p++; 135 wantspace = 1; 136 } 137 } 138 } 139 140 if (needcomm) { 141 if (!commandonly) { 142 char **argv = NULL; 143 144 if (kd != NULL) { 145 argv = kvm_getargv(kd, kp, termwidth); 146 if ((p = argv) != NULL) { 147 while (*p) { 148 if (wantspace) { 149 putchar(' '); 150 left--; 151 } 152 left -= mbswprint(*p, left, 0); 153 if (left == 0) 154 return; 155 p++; 156 wantspace = 1; 157 } 158 } 159 } 160 if (argv == NULL || argv[0] == NULL || 161 strcmp(cmdpart(argv[0]), kp->p_comm)) { 162 if (wantspace) { 163 putchar(' '); 164 if (--left == 0) 165 return; 166 } 167 putchar('('); 168 left--; 169 left -= mbswprint(kp->p_comm, left, 0); 170 if (left == 0) 171 return; 172 putchar(')'); 173 left--; 174 } 175 } else { 176 if (wantspace) { 177 putchar(' '); 178 left--; 179 } 180 left -= mbswprint(kp->p_comm, left, 0); 181 } 182 } 183 if (ve->next != NULL) 184 while (left-- > 0) 185 putchar(' '); 186 } 187 188 void 189 ucomm(const struct kinfo_proc *kp, VARENT *ve) 190 { 191 mbswprint(kp->p_comm, ve->var->width, ve->next != NULL); 192 } 193 194 void 195 curwd(const struct kinfo_proc *kp, VARENT *ve) 196 { 197 int name[] = { CTL_KERN, KERN_PROC_CWD, kp->p_pid }; 198 char path[PATH_MAX]; 199 size_t pathlen = sizeof path; 200 201 if (!kvm_sysctl_only || sysctl(name, 3, path, &pathlen, NULL, 0) != 0) 202 *path = '\0'; 203 204 mbswprint(path, ve->var->width, ve->next != NULL); 205 } 206 207 void 208 logname(const struct kinfo_proc *kp, VARENT *ve) 209 { 210 VAR *v; 211 212 v = ve->var; 213 if (kp->p_login[0]) { 214 int n = min(v->width, LOGIN_NAME_MAX); 215 mbswprint(kp->p_login, n, ve->next != NULL); 216 if (ve->next != NULL) 217 while (n++ < v->width) 218 putchar(' '); 219 } else 220 (void)printf("%-*s", v->width, "-"); 221 } 222 223 #define pgtok(a) (((unsigned long long)(a)*getpagesize())/1024) 224 225 void 226 printstate(const struct kinfo_proc *kp, VARENT *ve) 227 { 228 int flag; 229 char *cp, state = '\0'; 230 VAR *v; 231 char buf[16]; 232 233 v = ve->var; 234 flag = kp->p_flag; 235 cp = buf; 236 237 switch (kp->p_stat) { 238 239 case SSTOP: 240 *cp = 'T'; 241 break; 242 243 case SSLEEP: 244 if (flag & P_SINTR) /* interruptible (long) */ 245 *cp = kp->p_slptime >= maxslp ? 'I' : 'S'; 246 else 247 *cp = 'D'; 248 break; 249 250 case SRUN: 251 case SIDL: 252 case SONPROC: 253 state = *cp = 'R'; 254 break; 255 256 case SDEAD: 257 *cp = 'Z'; 258 break; 259 260 default: 261 *cp = '?'; 262 } 263 cp++; 264 265 if (kp->p_nice < NZERO) 266 *cp++ = '<'; 267 else if (kp->p_nice > NZERO) 268 *cp++ = 'N'; 269 if (kp->p_psflags & PS_TRACED) 270 *cp++ = 'X'; 271 if ((kp->p_psflags & (PS_EXITING | PS_ZOMBIE)) == PS_EXITING) 272 *cp++ = 'E'; 273 if (kp->p_psflags & PS_ISPWAIT) 274 *cp++ = 'V'; 275 if (flag & P_SYSTEM) 276 *cp++ = 'K'; 277 if ((flag & P_SYSTEM) == 0 && 278 kp->p_rlim_rss_cur / 1024 < pgtok(kp->p_vm_rssize)) 279 *cp++ = '>'; 280 if (kp->p_eflag & EPROC_SLEADER) 281 *cp++ = 's'; 282 if ((kp->p_psflags & PS_CONTROLT) && kp->p__pgid == kp->p_tpgid) 283 *cp++ = '+'; 284 if (kp->p_psflags & PS_PLEDGE) 285 *cp++ = 'p'; 286 if (kp->p_eflag & EPROC_UNVEIL) { 287 if (kp->p_eflag & EPROC_LKUNVEIL) 288 *cp++ = 'U'; 289 else 290 *cp++ = 'u'; 291 } 292 if (kp->p_psflags & PS_CHROOT) 293 *cp++ = 'c'; 294 *cp = '\0'; 295 296 if (state == 'R' && kp->p_cpuid != KI_NOCPU) { 297 char pbuf[16]; 298 299 snprintf(pbuf, sizeof pbuf, "/%llu", kp->p_cpuid); 300 *++cp = '\0'; 301 strlcat(buf, pbuf, sizeof buf); 302 cp = buf + strlen(buf); 303 } 304 305 (void)printf("%-*s", v->width, buf); 306 } 307 308 void 309 printpledge(const struct kinfo_proc *kp, VARENT *ve) 310 { 311 int i; 312 VAR *v; 313 char buf[1024]; 314 315 v = ve->var; 316 buf[0] = '\0'; 317 318 for (i = 0; pledgenames[i].bits != 0; i++) { 319 if (pledgenames[i].bits & kp->p_pledge) { 320 if (*buf != '\0') 321 strlcat(buf, ",", sizeof buf); 322 strlcat(buf, pledgenames[i].name, sizeof buf); 323 } 324 } 325 326 (void)printf("%-*s", v->width, buf); 327 } 328 329 void 330 pri(const struct kinfo_proc *kp, VARENT *ve) 331 { 332 VAR *v; 333 334 v = ve->var; 335 (void)printf("%*d", v->width, kp->p_priority - PZERO); 336 } 337 338 void 339 pnice(const struct kinfo_proc *kp, VARENT *ve) 340 { 341 VAR *v; 342 v = ve->var; 343 (void)printf("%*d", v->width, kp->p_nice - NZERO); 344 } 345 346 void 347 euname(const struct kinfo_proc *kp, VARENT *ve) 348 { 349 mbswprint(user_from_uid(kp->p_uid, 0), ve->var->width, 350 ve->next != NULL); 351 } 352 353 void 354 runame(const struct kinfo_proc *kp, VARENT *ve) 355 { 356 mbswprint(user_from_uid(kp->p_ruid, 0), ve->var->width, 357 ve->next != NULL); 358 } 359 360 void 361 gname(const struct kinfo_proc *kp, VARENT *ve) 362 { 363 mbswprint(group_from_gid(kp->p_gid, 0), ve->var->width, 364 ve->next != NULL); 365 } 366 367 void 368 rgname(const struct kinfo_proc *kp, VARENT *ve) 369 { 370 mbswprint(group_from_gid(kp->p_rgid, 0), ve->var->width, 371 ve->next != NULL); 372 } 373 374 void 375 supgid(const struct kinfo_proc *kp, VARENT *ve) 376 { 377 char buf[1024]; 378 char *p = buf; 379 ssize_t size = sizeof(buf); 380 int i, len; 381 382 for (i = 0; i < kp->p_ngroups; i++) { 383 len = snprintf(p, size, "%s%u", 384 p == buf ? "" : ",", 385 kp->p_groups[i]); 386 if (len < 0 || len >= size) 387 break; 388 p += len; 389 size -= len; 390 } 391 392 (void)printf("%-*s", ve->var->width, buf); 393 } 394 395 void 396 supgrp(const struct kinfo_proc *kp, VARENT *ve) 397 { 398 char buf[1024]; 399 char *p = buf; 400 ssize_t size = sizeof(buf); 401 int i, len; 402 403 for (i = 0; i < kp->p_ngroups; i++) { 404 len = snprintf(p, size, "%s%s", 405 p == buf ? "" : ",", 406 group_from_gid(kp->p_groups[i], 0)); 407 if (len < 0 || len >= size) 408 break; 409 p += len; 410 size -= len; 411 } 412 413 (void)printf("%-*s", ve->var->width, buf); 414 } 415 416 void 417 tdev(const struct kinfo_proc *kp, VARENT *ve) 418 { 419 VAR *v; 420 dev_t dev; 421 422 v = ve->var; 423 dev = kp->p_tdev; 424 if (dev == NODEV) 425 (void)printf("%*s", v->width, "??"); 426 else { 427 char buff[10+1+10+1]; 428 429 (void)snprintf(buff, sizeof(buff), 430 "%u/%u", major(dev), minor(dev)); 431 (void)printf("%*s", v->width, buff); 432 } 433 } 434 435 void 436 tname(const struct kinfo_proc *kp, VARENT *ve) 437 { 438 VAR *v; 439 dev_t dev; 440 char *ttname; 441 442 v = ve->var; 443 dev = kp->p_tdev; 444 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL) 445 (void)printf("%-*s", v->width, "??"); 446 else { 447 if (strncmp(ttname, "tty", 3) == 0) 448 ttname += 3; 449 (void)printf("%*.*s%c", v->width-1, v->width-1, ttname, 450 kp->p_eflag & EPROC_CTTY ? ' ' : '-'); 451 } 452 } 453 454 void 455 longtname(const struct kinfo_proc *kp, VARENT *ve) 456 { 457 VAR *v; 458 dev_t dev; 459 char *ttname; 460 461 v = ve->var; 462 dev = kp->p_tdev; 463 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL) 464 (void)printf("%-*s", v->width, "??"); 465 else 466 (void)printf("%-*s", v->width, ttname); 467 } 468 469 void 470 started(const struct kinfo_proc *kp, VARENT *ve) 471 { 472 VAR *v; 473 static time_t now; 474 time_t startt; 475 struct tm *tp; 476 char buf[100]; 477 478 v = ve->var; 479 if (!kp->p_uvalid) { 480 (void)printf("%-*s", v->width, "-"); 481 return; 482 } 483 484 #define SECSPERHOUR (60 * 60) 485 #define SECSPERDAY (24 * 60 * 60) 486 487 startt = kp->p_ustart_sec; 488 tp = localtime(&startt); 489 if (!now) 490 (void)time(&now); 491 if (now - kp->p_ustart_sec < 12 * SECSPERHOUR) { 492 (void)strftime(buf, sizeof(buf) - 1, "%l:%M%p", tp); 493 } else if (now - kp->p_ustart_sec < 7 * SECSPERDAY) { 494 (void)strftime(buf, sizeof(buf) - 1, "%a%I%p", tp); 495 } else 496 (void)strftime(buf, sizeof(buf) - 1, "%e%b%y", tp); 497 (void)printf("%-*s", v->width, buf); 498 } 499 500 void 501 lstarted(const struct kinfo_proc *kp, VARENT *ve) 502 { 503 VAR *v; 504 time_t startt; 505 char buf[100]; 506 507 v = ve->var; 508 if (!kp->p_uvalid) { 509 (void)printf("%-*s", v->width, "-"); 510 return; 511 } 512 startt = kp->p_ustart_sec; 513 (void)strftime(buf, sizeof(buf) -1, "%c", 514 localtime(&startt)); 515 (void)printf("%-*s", v->width, buf); 516 } 517 518 void elapsed(const struct kinfo_proc *kp, VARENT *ve) 519 { 520 VAR *v; 521 static time_t now; 522 time_t secs; 523 char buf[64]; 524 long days, hours, minutes, seconds; 525 526 v = ve->var; 527 if (!kp->p_uvalid) { 528 (void)printf("%*s", v->width, "-"); 529 return; 530 } 531 532 if (!now) 533 (void)time(&now); 534 secs = now - kp->p_ustart_sec; 535 536 if (secs < 0) { 537 (void)printf("%*s", v->width, "-"); 538 return; 539 } 540 541 days = secs / SECSPERDAY; 542 secs %= SECSPERDAY; 543 544 hours = secs / SECSPERHOUR; 545 secs %= SECSPERHOUR; 546 547 minutes = secs / 60; 548 seconds = secs % 60; 549 550 if (days > 0) 551 (void)snprintf(buf, sizeof(buf), "%ld-%02ld:%02ld:%02ld", 552 days, hours, minutes, seconds); 553 else if (hours > 0) 554 (void)snprintf(buf, sizeof(buf), "%02ld:%02ld:%02ld", 555 hours, minutes, seconds); 556 else 557 (void)snprintf(buf, sizeof(buf), "%02ld:%02ld", 558 minutes, seconds); 559 (void)printf("%*s", v->width, buf); 560 } 561 562 void 563 wchan(const struct kinfo_proc *kp, VARENT *ve) 564 { 565 VAR *v; 566 567 v = ve->var; 568 if (kp->p_wmesg[0]) { 569 (void)printf("%-*s", (int)v->width, kp->p_wmesg); 570 } else 571 (void)printf("%-*s", v->width, "-"); 572 } 573 574 void 575 vsize(const struct kinfo_proc *kp, VARENT *ve) 576 { 577 VAR *v; 578 579 v = ve->var; 580 (void)printf("%*llu", v->width, 581 pgtok(kp->p_vm_dsize + kp->p_vm_ssize + kp->p_vm_tsize)); 582 } 583 584 void 585 rssize(const struct kinfo_proc *kp, VARENT *ve) 586 { 587 VAR *v; 588 589 v = ve->var; 590 /* XXX don't have info about shared */ 591 (void)printf("%*llu", v->width, (kp->p_flag & P_SYSTEM) ? 0 : 592 pgtok(kp->p_vm_rssize)); 593 } 594 595 void 596 p_rssize(const struct kinfo_proc *kp, VARENT *ve) 597 { 598 VAR *v; 599 600 v = ve->var; 601 (void)printf("%*llu", v->width, (kp->p_flag & P_SYSTEM) ? 0 : 602 pgtok(kp->p_vm_rssize)); 603 } 604 605 void 606 cputime(const struct kinfo_proc *kp, VARENT *ve) 607 { 608 VAR *v; 609 long secs; 610 long psecs; /* "parts" of a second. first micro, then centi */ 611 char obuff[128]; 612 613 v = ve->var; 614 if (kp->p_stat == SDEAD || !kp->p_uvalid) { 615 secs = 0; 616 psecs = 0; 617 } else { 618 /* 619 * This counts time spent handling interrupts. XXX 620 */ 621 secs = kp->p_rtime_sec; 622 psecs = kp->p_rtime_usec; 623 if (sumrusage) { 624 secs += kp->p_uctime_sec; 625 psecs += kp->p_uctime_usec; 626 } 627 /* 628 * round and scale to 100's 629 */ 630 psecs = (psecs + 5000) / 10000; 631 secs += psecs / 100; 632 psecs = psecs % 100; 633 } 634 (void)snprintf(obuff, sizeof(obuff), 635 "%3ld:%02ld.%02ld", secs/60, secs%60, psecs); 636 (void)printf("%*s", v->width, obuff); 637 } 638 639 double 640 getpcpu(const struct kinfo_proc *kp) 641 { 642 if (fscale == 0) 643 return (0.0); 644 645 #define fxtofl(fixpt) ((double)(fixpt) / fscale) 646 647 return (100.0 * fxtofl(kp->p_pctcpu)); 648 } 649 650 void 651 pcpu(const struct kinfo_proc *kp, VARENT *ve) 652 { 653 VAR *v; 654 655 v = ve->var; 656 (void)printf("%*.1f", v->width, getpcpu(kp)); 657 } 658 659 double 660 getpmem(const struct kinfo_proc *kp) 661 { 662 double fracmem; 663 664 if (mempages == 0) 665 return (0.0); 666 667 if (kp->p_flag & P_SYSTEM) 668 return (0.0); 669 /* XXX don't have info about shared */ 670 fracmem = ((float)kp->p_vm_rssize)/mempages; 671 return (100.0 * fracmem); 672 } 673 674 void 675 pmem(const struct kinfo_proc *kp, VARENT *ve) 676 { 677 VAR *v; 678 679 v = ve->var; 680 (void)printf("%*.1f", v->width, getpmem(kp)); 681 } 682 683 void 684 pagein(const struct kinfo_proc *kp, VARENT *ve) 685 { 686 VAR *v; 687 688 v = ve->var; 689 (void)printf("%*llu", v->width, 690 kp->p_uvalid ? kp->p_uru_majflt : 0); 691 } 692 693 void 694 maxrss(const struct kinfo_proc *kp, VARENT *ve) 695 { 696 VAR *v; 697 698 v = ve->var; 699 (void)printf("%*llu", v->width, kp->p_rlim_rss_cur / 1024); 700 } 701 702 void 703 tsize(const struct kinfo_proc *kp, VARENT *ve) 704 { 705 VAR *v; 706 707 v = ve->var; 708 (void)printf("%*llu", v->width, pgtok(kp->p_vm_tsize)); 709 } 710 711 void 712 dsize(const struct kinfo_proc *kp, VARENT *ve) 713 { 714 VAR *v; 715 716 v = ve->var; 717 (void)printf("%*llu", v->width, pgtok(kp->p_vm_dsize)); 718 } 719 720 void 721 ssize(const struct kinfo_proc *kp, VARENT *ve) 722 { 723 VAR *v; 724 725 v = ve->var; 726 (void)printf("%*llu", v->width, pgtok(kp->p_vm_ssize)); 727 } 728 729 /* 730 * Generic output routines. Print fields from various prototype 731 * structures. 732 */ 733 static void 734 printval(char *bp, VAR *v) 735 { 736 char ofmt[32]; 737 738 snprintf(ofmt, sizeof(ofmt), "%%%s*%s", (v->flag & LJUST) ? "-" : "", 739 v->fmt); 740 741 /* 742 * Note that the "INF127" check is nonsensical for types 743 * that are or can be signed. 744 */ 745 #define GET(type) (*(type *)bp) 746 #define CHK_INF127(n) (((n) > 127) && (v->flag & INF127) ? 127 : (n)) 747 748 switch (v->type) { 749 case INT8: 750 (void)printf(ofmt, v->width, GET(int8_t)); 751 break; 752 case UINT8: 753 (void)printf(ofmt, v->width, CHK_INF127(GET(u_int8_t))); 754 break; 755 case INT16: 756 (void)printf(ofmt, v->width, GET(int16_t)); 757 break; 758 case UINT16: 759 (void)printf(ofmt, v->width, CHK_INF127(GET(u_int16_t))); 760 break; 761 case INT32: 762 (void)printf(ofmt, v->width, GET(int32_t)); 763 break; 764 case UINT32: 765 (void)printf(ofmt, v->width, CHK_INF127(GET(u_int32_t))); 766 break; 767 case INT64: 768 (void)printf(ofmt, v->width, GET(int64_t)); 769 break; 770 case UINT64: 771 (void)printf(ofmt, v->width, CHK_INF127(GET(u_int64_t))); 772 break; 773 default: 774 errx(1, "unknown type %d", v->type); 775 } 776 #undef GET 777 #undef CHK_INF127 778 } 779 780 void 781 pvar(const struct kinfo_proc *kp, VARENT *ve) 782 { 783 VAR *v; 784 785 v = ve->var; 786 if ((v->flag & USER) && !kp->p_uvalid) 787 (void)printf("%*s", v->width, "-"); 788 else 789 printval((char *)kp + v->off, v); 790 } 791