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