1 /* $NetBSD: w.c,v 1.91 2021/04/17 06:14:15 maya Exp $ */ 2 3 /*- 4 * Copyright (c) 1980, 1991, 1993, 1994 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 #ifndef lint 34 __COPYRIGHT("@(#) Copyright (c) 1980, 1991, 1993, 1994\ 35 The Regents of the University of California. All rights reserved."); 36 #endif /* not lint */ 37 38 #ifndef lint 39 #if 0 40 static char sccsid[] = "@(#)w.c 8.6 (Berkeley) 6/30/94"; 41 #else 42 __RCSID("$NetBSD: w.c,v 1.91 2021/04/17 06:14:15 maya Exp $"); 43 #endif 44 #endif /* not lint */ 45 46 /* 47 * w - print system status (who and what) 48 * 49 * This program is similar to the systat command on Tenex/Tops 10/20 50 * 51 */ 52 #include <sys/param.h> 53 #include <sys/types.h> 54 #include <sys/time.h> 55 #include <sys/stat.h> 56 #include <sys/sysctl.h> 57 #include <sys/proc.h> 58 #include <sys/ioctl.h> 59 #include <sys/socket.h> 60 61 #include <netinet/in.h> 62 #include <arpa/inet.h> 63 64 #include <ctype.h> 65 #include <err.h> 66 #include <errno.h> 67 #include <fcntl.h> 68 #include <kvm.h> 69 #include <limits.h> 70 #include <netdb.h> 71 #include <nlist.h> 72 #include <paths.h> 73 #include <stdio.h> 74 #include <stdlib.h> 75 #include <string.h> 76 #include <time.h> 77 #include <tzfile.h> 78 #include <unistd.h> 79 #ifdef SUPPORT_UTMP 80 #include <utmp.h> 81 #endif 82 #ifdef SUPPORT_UTMPX 83 #include <utmpx.h> 84 #endif 85 #include <vis.h> 86 87 #include "extern.h" 88 89 struct timespec boottime; 90 struct winsize ws; 91 kvm_t *kd; 92 time_t now; /* the current time of day */ 93 int ttywidth; /* width of tty */ 94 int argwidth; /* width of tty left to print process args */ 95 int header = 1; /* true if -h flag: don't print heading */ 96 int nflag; /* true if -n flag: don't convert addrs */ 97 int wflag; /* true if -w flag: wide printout */ 98 int sortidle; /* sort bu idle time */ 99 char *sel_user; /* login of particular user selected */ 100 char domain[MAXHOSTNAMELEN + 1]; 101 int maxname = 8, maxline = 3, maxhost = 16; 102 103 /* 104 * One of these per active utmp entry. 105 */ 106 struct entry { 107 struct entry *next; 108 char name[UTX_USERSIZE + 1]; 109 char line[UTX_LINESIZE + 1]; 110 char host[UTX_HOSTSIZE + 1]; 111 char type[2]; 112 struct timeval tv; 113 dev_t tdev; /* dev_t of terminal */ 114 time_t idle; /* idle time of terminal in seconds */ 115 struct kinfo_proc2 *tp; /* `most interesting' tty proc */ 116 struct kinfo_proc2 *pp; /* pid proc */ 117 pid_t pid; /* pid or ~0 if not known */ 118 } *ehead = NULL, **nextp = &ehead; 119 120 static void pr_args(struct kinfo_proc2 *); 121 static void pr_header(time_t *, int); 122 static int proc_compare_wrapper(const struct kinfo_proc2 *, 123 const struct kinfo_proc2 *); 124 #if defined(SUPPORT_UTMP) || defined(SUPPORT_UTMPX) 125 static int ttystat(const char *, struct stat *); 126 static void process(struct entry *); 127 #endif 128 static void fixhost(struct entry *ep); 129 __dead static void usage(int); 130 131 int 132 main(int argc, char **argv) 133 { 134 struct kinfo_proc2 *kp; 135 struct entry *ep; 136 int ch, i, nentries, nusers, wcmd, curtain, use_sysctl; 137 char *memf, *nlistf, *usrnp; 138 const char *options; 139 time_t then; 140 size_t len; 141 #ifdef SUPPORT_UTMP 142 struct utmp *ut; 143 #endif 144 #ifdef SUPPORT_UTMPX 145 struct utmpx *utx; 146 #endif 147 const char *progname; 148 char errbuf[_POSIX2_LINE_MAX]; 149 150 setprogname(argv[0]); 151 152 /* Are we w(1) or uptime(1)? */ 153 progname = getprogname(); 154 if (*progname == '-') 155 progname++; 156 if (*progname == 'u') { 157 wcmd = 0; 158 options = ""; 159 } else { 160 wcmd = 1; 161 options = "hiM:N:nw"; 162 } 163 164 memf = nlistf = NULL; 165 while ((ch = getopt(argc, argv, options)) != -1) 166 switch (ch) { 167 case 'h': 168 header = 0; 169 break; 170 case 'i': 171 sortidle = 1; 172 break; 173 case 'M': 174 header = 0; 175 memf = optarg; 176 break; 177 case 'N': 178 nlistf = optarg; 179 break; 180 case 'n': 181 nflag = 1; 182 break; 183 case 'w': 184 wflag = 1; 185 break; 186 case '?': 187 default: 188 usage(wcmd); 189 } 190 argc -= optind; 191 argv += optind; 192 193 use_sysctl = (memf == NULL && nlistf == NULL); 194 195 if ((kd = kvm_openfiles(nlistf, memf, NULL, 196 memf == NULL ? KVM_NO_FILES : O_RDONLY, errbuf)) == NULL) 197 errx(1, "%s", errbuf); 198 199 (void)time(&now); 200 201 if (use_sysctl) { 202 len = sizeof(curtain); 203 if (sysctlbyname("security.curtain", &curtain, &len, 204 NULL, 0) == -1) 205 curtain = 0; 206 } 207 208 if (!nflag) { 209 int rv; 210 char *p; 211 212 rv = gethostname(domain, sizeof(domain)); 213 domain[sizeof(domain) - 1] = '\0'; 214 if (rv < 0 || (p = strchr(domain, '.')) == 0) 215 domain[0] = '\0'; 216 else 217 memmove(domain, p, strlen(p) + 1); 218 } 219 220 #ifdef SUPPORT_UTMPX 221 setutxent(); 222 #endif 223 #ifdef SUPPORT_UTMP 224 setutent(); 225 #endif 226 227 if (*argv) 228 sel_user = *argv; 229 230 nusers = 0; 231 #ifdef SUPPORT_UTMPX 232 while ((utx = getutxent()) != NULL) { 233 if (utx->ut_type != USER_PROCESS) 234 continue; 235 ++nusers; 236 237 #ifndef SUPPORT_UTMP 238 if (wcmd == 0) 239 continue; 240 #endif /* !SUPPORT_UTMP */ 241 242 if (sel_user && 243 strncmp(utx->ut_name, sel_user, sizeof(utx->ut_name)) != 0) 244 continue; 245 if ((ep = calloc(1, sizeof(struct entry))) == NULL) 246 err(1, NULL); 247 (void)memcpy(ep->line, utx->ut_line, sizeof(utx->ut_line)); 248 ep->line[sizeof(utx->ut_line)] = '\0'; 249 *nextp = ep; 250 nextp = &(ep->next); 251 252 if (wcmd == 0) 253 continue; 254 255 (void)memcpy(ep->name, utx->ut_name, sizeof(utx->ut_name)); 256 ep->name[sizeof(utx->ut_name)] = '\0'; 257 if (!nflag || getnameinfo((struct sockaddr *)&utx->ut_ss, 258 utx->ut_ss.ss_len, ep->host, sizeof(ep->host), NULL, 0, 259 NI_NUMERICHOST) != 0) { 260 (void)memcpy(ep->host, utx->ut_host, 261 sizeof(utx->ut_host)); 262 ep->host[sizeof(utx->ut_host)] = '\0'; 263 } 264 fixhost(ep); 265 ep->type[0] = 'x'; 266 ep->tv = utx->ut_tv; 267 ep->pid = utx->ut_pid; 268 process(ep); 269 } 270 #endif 271 272 #ifdef SUPPORT_UTMP 273 while ((ut = getutent()) != NULL) { 274 if (ut->ut_name[0] == '\0') 275 continue; 276 277 if (sel_user && 278 strncmp(ut->ut_name, sel_user, sizeof(ut->ut_name)) != 0) 279 continue; 280 281 /* Don't process entries that we have utmpx for */ 282 for (ep = ehead; ep != NULL; ep = ep->next) { 283 if (strncmp(ep->line, ut->ut_line, 284 sizeof(ut->ut_line)) == 0) 285 break; 286 } 287 if (ep != NULL) 288 continue; 289 290 ++nusers; 291 292 if (wcmd == 0) 293 continue; 294 295 if ((ep = calloc(1, sizeof(struct entry))) == NULL) 296 err(1, NULL); 297 (void)memcpy(ep->name, ut->ut_name, sizeof(ut->ut_name)); 298 (void)memcpy(ep->line, ut->ut_line, sizeof(ut->ut_line)); 299 (void)memcpy(ep->host, ut->ut_host, sizeof(ut->ut_host)); 300 ep->name[sizeof(ut->ut_name)] = '\0'; 301 ep->line[sizeof(ut->ut_line)] = '\0'; 302 ep->host[sizeof(ut->ut_host)] = '\0'; 303 fixhost(ep); 304 ep->tv.tv_sec = ut->ut_time; 305 *nextp = ep; 306 nextp = &(ep->next); 307 process(ep); 308 } 309 #endif 310 311 #ifdef SUPPORT_UTMPX 312 endutxent(); 313 #endif 314 #ifdef SUPPORT_UTMP 315 endutent(); 316 #endif 317 318 if (header || wcmd == 0) { 319 pr_header(&now, nusers); 320 if (wcmd == 0) 321 exit (0); 322 } 323 324 if ((kp = kvm_getproc2(kd, KERN_PROC_ALL, 0, 325 sizeof(struct kinfo_proc2), &nentries)) == NULL) 326 errx(1, "%s", kvm_geterr(kd)); 327 328 /* Include trailing space because TTY header starts one column early. */ 329 for (i = 0; i < nentries; i++, kp++) { 330 331 for (ep = ehead; ep != NULL; ep = ep->next) { 332 if (ep->tdev != 0 && ep->tdev == kp->p_tdev && 333 kp->p__pgid == kp->p_tpgid) { 334 /* 335 * Proc is in foreground of this 336 * terminal 337 */ 338 if (proc_compare_wrapper(ep->tp, kp)) 339 ep->tp = kp; 340 break; 341 } 342 if (ep->pid != 0 && ep->pid == kp->p_pid) { 343 ep->pp = kp; 344 break; 345 } 346 } 347 } 348 349 if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 && 350 ioctl(STDERR_FILENO, TIOCGWINSZ, &ws) == -1 && 351 ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1) || ws.ws_col == 0) 352 ttywidth = 79; 353 else 354 ttywidth = ws.ws_col - 1; 355 356 if (!wflag && maxhost > (ttywidth / 3)) 357 maxhost = ttywidth / 3; 358 359 argwidth = printf("%-*s TTY %-*s %*s IDLE WHAT\n", 360 maxname, "USER", maxhost, "FROM", 361 7 /* "dddhhXm" */, "LOGIN@"); 362 argwidth -= sizeof("WHAT\n") - 1 /* NUL */; 363 argwidth = ttywidth - argwidth; 364 if (argwidth < 4) 365 argwidth = 8; 366 if (wflag) 367 argwidth = -1; 368 369 /* sort by idle time */ 370 if (sortidle && ehead != NULL) { 371 struct entry *from = ehead, *save; 372 373 ehead = NULL; 374 while (from != NULL) { 375 for (nextp = &ehead; 376 (*nextp) && from->idle >= (*nextp)->idle; 377 nextp = &(*nextp)->next) 378 continue; 379 save = from; 380 from = from->next; 381 save->next = *nextp; 382 *nextp = save; 383 } 384 } 385 #if defined(SUPPORT_UTMP) && defined(SUPPORT_UTMPX) 386 else if (ehead != NULL) { 387 struct entry *from = ehead, *save; 388 389 ehead = NULL; 390 while (from != NULL) { 391 for (nextp = &ehead; 392 (*nextp) && strcmp(from->line, (*nextp)->line) > 0; 393 nextp = &(*nextp)->next) 394 continue; 395 save = from; 396 from = from->next; 397 save->next = *nextp; 398 *nextp = save; 399 } 400 } 401 #endif 402 403 for (ep = ehead; ep != NULL; ep = ep->next) { 404 if (ep->tp != NULL) 405 kp = ep->tp; 406 else if (ep->pp != NULL) 407 kp = ep->pp; 408 else if (ep->pid != 0) { 409 if (curtain) 410 kp = NULL; 411 else { 412 warnx("Stale utmp%s entry: %s %s %s", 413 ep->type, ep->name, ep->line, ep->host); 414 continue; 415 } 416 } 417 usrnp = (kp == NULL) ? ep->name : kp->p_login; 418 (void)printf("%-*s %-7.7s %-*.*s ", 419 maxname, usrnp, ep->line, 420 maxhost, maxhost, ep->host); 421 then = (time_t)ep->tv.tv_sec; 422 pr_attime(&then, &now); 423 pr_idle(ep->idle); 424 pr_args(kp); 425 (void)printf("\n"); 426 } 427 exit(0); 428 } 429 430 static void 431 pr_args(struct kinfo_proc2 *kp) 432 { 433 char **argv; 434 int left; 435 436 if (kp == 0) 437 goto nothing; 438 left = argwidth; 439 argv = kvm_getargv2(kd, kp, (argwidth < 0) ? 0 : argwidth); 440 if (argv == 0) { 441 fmt_putc('(', &left); 442 fmt_puts((char *)kp->p_comm, &left); 443 fmt_putc(')', &left); 444 return; 445 } 446 while (*argv) { 447 fmt_puts(*argv, &left); 448 argv++; 449 fmt_putc(' ', &left); 450 } 451 return; 452 nothing: 453 putchar('-'); 454 } 455 456 static void 457 pr_header(time_t *nowp, int nusers) 458 { 459 double avenrun[3]; 460 time_t uptime; 461 int days, hrs, mins; 462 int mib[2]; 463 size_t size, i; 464 char buf[256]; 465 466 /* 467 * Print time of day. 468 */ 469 (void)strftime(buf, sizeof(buf), "%l:%M%p", localtime(nowp)); 470 buf[sizeof(buf) - 1] = '\0'; 471 (void)printf("%s ", buf); 472 473 /* 474 * Print how long system has been up. 475 * (Found by looking getting "boottime" from the kernel) 476 */ 477 mib[0] = CTL_KERN; 478 mib[1] = KERN_BOOTTIME; 479 size = sizeof(boottime); 480 if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 && 481 boottime.tv_sec != 0) { 482 uptime = now - boottime.tv_sec; 483 uptime += 30; 484 if (uptime > SECSPERMIN) { 485 days = uptime / SECSPERDAY; 486 uptime %= SECSPERDAY; 487 hrs = uptime / SECSPERHOUR; 488 uptime %= SECSPERHOUR; 489 mins = uptime / SECSPERMIN; 490 (void)printf(" up"); 491 if (days > 0) 492 (void)printf(" %d day%s,", days, 493 days > 1 ? "s" : ""); 494 if (hrs > 0 && mins > 0) 495 (void)printf(" %2d:%02d,", hrs, mins); 496 else { 497 if (hrs > 0) 498 (void)printf(" %d hr%s,", 499 hrs, hrs > 1 ? "s" : ""); 500 if (mins > 0) 501 (void)printf(" %d min%s,", 502 mins, mins > 1 ? "s" : ""); 503 } 504 } 505 } 506 507 /* Print number of users logged in to system */ 508 (void)printf(" %d user%s", nusers, nusers != 1 ? "s" : ""); 509 510 /* 511 * Print 1, 5, and 15 minute load averages. 512 */ 513 if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) == -1) 514 (void)printf(", no load average information available\n"); 515 else { 516 (void)printf(", load averages:"); 517 for (i = 0; i < (sizeof(avenrun) / sizeof(avenrun[0])); i++) { 518 if (i > 0) 519 (void)printf(","); 520 (void)printf(" %.2f", avenrun[i]); 521 } 522 (void)printf("\n"); 523 } 524 } 525 526 #if defined(SUPPORT_UTMP) || defined(SUPPORT_UTMPX) 527 static int 528 ttystat(const char *line, struct stat *st) 529 { 530 char ttybuf[MAXPATHLEN]; 531 532 (void)snprintf(ttybuf, sizeof(ttybuf), "%s%s", _PATH_DEV, line); 533 return stat(ttybuf, st); 534 } 535 536 static void 537 process(struct entry *ep) 538 { 539 struct stat st; 540 time_t touched; 541 int max; 542 543 if ((max = strlen(ep->name)) > maxname) 544 maxname = max; 545 if ((max = strlen(ep->line)) > maxline) 546 maxline = max; 547 if ((max = strlen(ep->host)) > maxhost) 548 maxhost = max; 549 550 ep->tdev = 0; 551 ep->idle = (time_t)-1; 552 553 #ifdef SUPPORT_UTMP 554 /* 555 * Hack to recognize and correctly parse 556 * ut entry made by ftpd. The "tty" used 557 * by ftpd is not a real tty, just identifier in 558 * form ftpPID. Pid parsed from the "tty name" 559 * is used later to match corresponding process. 560 * NB: This is only used for utmp entries. For utmpx, 561 * we already have the pid. 562 */ 563 if (ep->pid == 0 && strncmp(ep->line, "ftp", 3) == 0) { 564 ep->pid = strtol(ep->line + 3, NULL, 10); 565 return; 566 } 567 #endif 568 if (ttystat(ep->line, &st) == -1) 569 return; 570 571 ep->tdev = st.st_rdev; 572 /* 573 * If this is the console device, attempt to ascertain 574 * the true console device dev_t. 575 */ 576 if (ep->tdev == 0) { 577 int mib[2]; 578 size_t size; 579 580 mib[0] = CTL_KERN; 581 mib[1] = KERN_CONSDEV; 582 size = sizeof(dev_t); 583 (void) sysctl(mib, 2, &ep->tdev, &size, NULL, 0); 584 } 585 586 touched = st.st_atime; 587 if (touched < ep->tv.tv_sec) { 588 /* tty untouched since before login */ 589 touched = ep->tv.tv_sec; 590 } 591 if ((ep->idle = now - touched) < 0) 592 ep->idle = 0; 593 } 594 #endif 595 596 static int 597 proc_compare_wrapper(const struct kinfo_proc2 *p1, 598 const struct kinfo_proc2 *p2) 599 { 600 struct kinfo_lwp *l1, *l2; 601 int cnt; 602 603 if (p1 == NULL) 604 return 1; 605 606 l1 = kvm_getlwps(kd, p1->p_pid, 0, sizeof(*l1), &cnt); 607 if (l1 == NULL || cnt == 0) 608 return 1; 609 610 l2 = kvm_getlwps(kd, p2->p_pid, 0, sizeof(*l1), &cnt); 611 if (l2 == NULL || cnt == 0) 612 return 0; 613 614 return proc_compare(p1, l1, p2, l2); 615 } 616 617 static void 618 fixhost(struct entry *ep) 619 { 620 char host_buf[sizeof(ep->host)]; 621 char *b, *m, *p, *r, *x; 622 struct hostent *hp; 623 union { 624 struct in_addr l4; 625 struct in6_addr l6; 626 } l; 627 628 strlcpy(host_buf, *ep->host ? ep->host : "-", sizeof(host_buf)); 629 p = host_buf; 630 631 /* 632 * One ':' in hostname means X display number, more is IPv6. 633 */ 634 for (x = p; x < &host_buf[sizeof(host_buf)]; x++) 635 if (*x == '\0' || *x == ':') 636 break; 637 if (x == p + sizeof(host_buf) || *x != ':') 638 m = x = NULL; 639 else { 640 for (m = x + 1; m < &host_buf[sizeof(host_buf)]; m++) 641 if (*m == '\0' || *m == ':') 642 break; 643 if (m == p + sizeof(host_buf) || *m != ':') { 644 *x++ = '\0'; 645 m = NULL; 646 } else 647 x = NULL; 648 } 649 650 /* 651 * Leading '[' indicates an IP address inside brackets. 652 */ 653 b = NULL; 654 if (!nflag && (*p == '[')) { 655 for (b = p++; b < &host_buf[sizeof(host_buf)]; b++) 656 if (*b == '\0' || *b == ']') 657 break; 658 if (b < &host_buf[sizeof(host_buf)] && *b == ']') { 659 *b = '\0'; 660 for (x = b + 1; x < &host_buf[sizeof(host_buf)]; x++) 661 if (*x == '\0' || *x == ':') 662 break; 663 if (x < &host_buf[sizeof(host_buf)] && *x == ':') 664 *x++ = '\0'; 665 } else 666 b = NULL; 667 } 668 669 int af = m ? AF_INET6 : AF_INET; 670 size_t alen = m ? sizeof(l.l6) : sizeof(l.l4); 671 if (!nflag && inet_pton(af, p, &l) && 672 (hp = gethostbyaddr((char *)&l, alen, af))) 673 r = hp->h_name; 674 else { 675 if (b) 676 *b = ']'; 677 r = host_buf; 678 } 679 680 if (domain[0] != '\0') { 681 p = r; 682 p += strlen(r); 683 p -= strlen(domain); 684 if (p > r && 685 strcasecmp(p, domain) == 0) 686 *p = '\0'; 687 } 688 689 if (x) 690 (void)snprintf(ep->host, sizeof(ep->host), "%s:%s", r, x); 691 else 692 strlcpy(ep->host, r, sizeof(ep->host)); 693 } 694 695 static void 696 usage(int wcmd) 697 { 698 699 if (wcmd) 700 (void)fprintf(stderr, 701 "Usage: %s [-hinw] [-M core] [-N system] [user]\n", 702 getprogname()); 703 else 704 (void)fprintf(stderr, "Usage: %s\n", getprogname()); 705 exit(1); 706 } 707