1 /* $NetBSD: w.c,v 1.82 2014/12/22 15:24:14 dennis 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.82 2014/12/22 15:24:14 dennis 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 timeval 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 #ifdef SUPPORT_UTMPX 209 setutxent(); 210 #endif 211 #ifdef SUPPORT_UTMP 212 setutent(); 213 #endif 214 215 if (*argv) 216 sel_user = *argv; 217 218 nusers = 0; 219 #ifdef SUPPORT_UTMPX 220 while ((utx = getutxent()) != NULL) { 221 if (utx->ut_type != USER_PROCESS) 222 continue; 223 ++nusers; 224 225 #ifndef SUPPORT_UTMP 226 if (wcmd == 0) 227 continue; 228 #endif /* !SUPPORT_UTMP */ 229 230 if (sel_user && 231 strncmp(utx->ut_name, sel_user, sizeof(utx->ut_name)) != 0) 232 continue; 233 if ((ep = calloc(1, sizeof(struct entry))) == NULL) 234 err(1, NULL); 235 (void)memcpy(ep->line, utx->ut_line, sizeof(utx->ut_line)); 236 ep->line[sizeof(utx->ut_line)] = '\0'; 237 *nextp = ep; 238 nextp = &(ep->next); 239 240 if (wcmd == 0) 241 continue; 242 243 (void)memcpy(ep->name, utx->ut_name, sizeof(utx->ut_name)); 244 ep->name[sizeof(utx->ut_name)] = '\0'; 245 if (!nflag || getnameinfo((struct sockaddr *)&utx->ut_ss, 246 utx->ut_ss.ss_len, ep->host, sizeof(ep->host), NULL, 0, 247 NI_NUMERICHOST) != 0) { 248 (void)memcpy(ep->host, utx->ut_host, 249 sizeof(utx->ut_host)); 250 ep->host[sizeof(utx->ut_host)] = '\0'; 251 } 252 fixhost(ep); 253 ep->type[0] = 'x'; 254 ep->tv = utx->ut_tv; 255 ep->pid = utx->ut_pid; 256 process(ep); 257 } 258 #endif 259 260 #ifdef SUPPORT_UTMP 261 while ((ut = getutent()) != NULL) { 262 if (ut->ut_name[0] == '\0') 263 continue; 264 265 if (sel_user && 266 strncmp(ut->ut_name, sel_user, sizeof(ut->ut_name)) != 0) 267 continue; 268 269 /* Don't process entries that we have utmpx for */ 270 for (ep = ehead; ep != NULL; ep = ep->next) { 271 if (strncmp(ep->line, ut->ut_line, 272 sizeof(ut->ut_line)) == 0) 273 break; 274 } 275 if (ep != NULL) 276 continue; 277 278 ++nusers; 279 280 if (wcmd == 0) 281 continue; 282 283 if ((ep = calloc(1, sizeof(struct entry))) == NULL) 284 err(1, NULL); 285 (void)memcpy(ep->name, ut->ut_name, sizeof(ut->ut_name)); 286 (void)memcpy(ep->line, ut->ut_line, sizeof(ut->ut_line)); 287 (void)memcpy(ep->host, ut->ut_host, sizeof(ut->ut_host)); 288 ep->name[sizeof(ut->ut_name)] = '\0'; 289 ep->line[sizeof(ut->ut_line)] = '\0'; 290 ep->host[sizeof(ut->ut_host)] = '\0'; 291 fixhost(ep); 292 ep->tv.tv_sec = ut->ut_time; 293 *nextp = ep; 294 nextp = &(ep->next); 295 process(ep); 296 } 297 #endif 298 299 #ifdef SUPPORT_UTMPX 300 endutxent(); 301 #endif 302 #ifdef SUPPORT_UTMP 303 endutent(); 304 #endif 305 306 if (header || wcmd == 0) { 307 pr_header(&now, nusers); 308 if (wcmd == 0) 309 exit (0); 310 } 311 312 if ((kp = kvm_getproc2(kd, KERN_PROC_ALL, 0, 313 sizeof(struct kinfo_proc2), &nentries)) == NULL) 314 errx(1, "%s", kvm_geterr(kd)); 315 316 /* Include trailing space because TTY header starts one column early. */ 317 for (i = 0; i < nentries; i++, kp++) { 318 319 for (ep = ehead; ep != NULL; ep = ep->next) { 320 if (ep->tdev != 0 && ep->tdev == kp->p_tdev && 321 kp->p__pgid == kp->p_tpgid) { 322 /* 323 * Proc is in foreground of this 324 * terminal 325 */ 326 if (proc_compare_wrapper(ep->tp, kp)) 327 ep->tp = kp; 328 break; 329 } 330 if (ep->pid != 0 && ep->pid == kp->p_pid) { 331 ep->pp = kp; 332 break; 333 } 334 } 335 } 336 337 if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 && 338 ioctl(STDERR_FILENO, TIOCGWINSZ, &ws) == -1 && 339 ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1) || ws.ws_col == 0) 340 ttywidth = 79; 341 else 342 ttywidth = ws.ws_col - 1; 343 344 if (!wflag && maxhost > (ttywidth / 3)) 345 maxhost = ttywidth / 3; 346 347 argwidth = printf("%-*s TTY %-*s %*s IDLE WHAT\n", 348 maxname, "USER", maxhost, "FROM", 349 7 /* "dddhhXm" */, "LOGIN@"); 350 argwidth -= sizeof("WHAT\n") - 1 /* NUL */; 351 argwidth = ttywidth - argwidth; 352 if (argwidth < 4) 353 argwidth = 8; 354 if (wflag) 355 argwidth = -1; 356 357 /* sort by idle time */ 358 if (sortidle && ehead != NULL) { 359 struct entry *from = ehead, *save; 360 361 ehead = NULL; 362 while (from != NULL) { 363 for (nextp = &ehead; 364 (*nextp) && from->idle >= (*nextp)->idle; 365 nextp = &(*nextp)->next) 366 continue; 367 save = from; 368 from = from->next; 369 save->next = *nextp; 370 *nextp = save; 371 } 372 } 373 #if defined(SUPPORT_UTMP) && defined(SUPPORT_UTMPX) 374 else if (ehead != NULL) { 375 struct entry *from = ehead, *save; 376 377 ehead = NULL; 378 while (from != NULL) { 379 for (nextp = &ehead; 380 (*nextp) && strcmp(from->line, (*nextp)->line) > 0; 381 nextp = &(*nextp)->next) 382 continue; 383 save = from; 384 from = from->next; 385 save->next = *nextp; 386 *nextp = save; 387 } 388 } 389 #endif 390 391 if (!nflag) { 392 int rv; 393 char *p; 394 395 rv = gethostname(domain, sizeof(domain)); 396 domain[sizeof(domain) - 1] = '\0'; 397 if (rv < 0 || (p = strchr(domain, '.')) == 0) 398 domain[0] = '\0'; 399 else 400 memmove(domain, p, strlen(p) + 1); 401 } 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 #if !defined(__minix) 418 usrnp = (kp == NULL) ? ep->name : kp->p_login; 419 #else 420 usrnp = ep->name; /* TODO: implement getlogin/setlogin */ 421 #endif /* !defined(__minix) */ 422 (void)printf("%-*s %-7.7s %-*.*s ", 423 maxname, usrnp, ep->line, 424 maxhost, maxhost, ep->host); 425 then = (time_t)ep->tv.tv_sec; 426 pr_attime(&then, &now); 427 pr_idle(ep->idle); 428 pr_args(kp); 429 (void)printf("\n"); 430 } 431 exit(0); 432 } 433 434 static void 435 pr_args(struct kinfo_proc2 *kp) 436 { 437 char **argv; 438 int left; 439 440 if (kp == 0) 441 goto nothing; 442 left = argwidth; 443 argv = kvm_getargv2(kd, kp, (argwidth < 0) ? 0 : argwidth); 444 if (argv == 0) { 445 fmt_putc('(', &left); 446 fmt_puts((char *)kp->p_comm, &left); 447 fmt_putc(')', &left); 448 return; 449 } 450 while (*argv) { 451 fmt_puts(*argv, &left); 452 argv++; 453 fmt_putc(' ', &left); 454 } 455 return; 456 nothing: 457 putchar('-'); 458 } 459 460 static void 461 pr_header(time_t *nowp, int nusers) 462 { 463 double avenrun[3]; 464 time_t uptime; 465 int days, hrs, mins; 466 int mib[2]; 467 size_t size, i; 468 char buf[256]; 469 470 /* 471 * Print time of day. 472 * 473 * SCCS forces the string manipulation below, as it replaces 474 * %, M, and % in a character string with the file name. 475 */ 476 (void)strftime(buf, sizeof(buf), "%l:%" "M%p", localtime(nowp)); 477 buf[sizeof(buf) - 1] = '\0'; 478 (void)printf("%s ", buf); 479 480 /* 481 * Print how long system has been up. 482 * (Found by looking getting "boottime" from the kernel) 483 */ 484 mib[0] = CTL_KERN; 485 mib[1] = KERN_BOOTTIME; 486 size = sizeof(boottime); 487 if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 && 488 boottime.tv_sec != 0) { 489 uptime = now - boottime.tv_sec; 490 uptime += 30; 491 if (uptime > SECSPERMIN) { 492 days = uptime / SECSPERDAY; 493 uptime %= SECSPERDAY; 494 hrs = uptime / SECSPERHOUR; 495 uptime %= SECSPERHOUR; 496 mins = uptime / SECSPERMIN; 497 (void)printf(" up"); 498 if (days > 0) 499 (void)printf(" %d day%s,", days, 500 days > 1 ? "s" : ""); 501 if (hrs > 0 && mins > 0) 502 (void)printf(" %2d:%02d,", hrs, mins); 503 else { 504 if (hrs > 0) 505 (void)printf(" %d hr%s,", 506 hrs, hrs > 1 ? "s" : ""); 507 if (mins > 0) 508 (void)printf(" %d min%s,", 509 mins, mins > 1 ? "s" : ""); 510 } 511 } 512 } 513 514 /* Print number of users logged in to system */ 515 (void)printf(" %d user%s", nusers, nusers != 1 ? "s" : ""); 516 517 /* 518 * Print 1, 5, and 15 minute load averages. 519 */ 520 if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) == -1) 521 (void)printf(", no load average information available\n"); 522 else { 523 (void)printf(", load averages:"); 524 for (i = 0; i < (sizeof(avenrun) / sizeof(avenrun[0])); i++) { 525 if (i > 0) 526 (void)printf(","); 527 (void)printf(" %.2f", avenrun[i]); 528 } 529 (void)printf("\n"); 530 } 531 } 532 533 #if defined(SUPPORT_UTMP) || defined(SUPPORT_UTMPX) 534 static int 535 ttystat(const char *line, struct stat *st) 536 { 537 char ttybuf[MAXPATHLEN]; 538 539 (void)snprintf(ttybuf, sizeof(ttybuf), "%s%s", _PATH_DEV, line); 540 return stat(ttybuf, st); 541 } 542 543 static void 544 process(struct entry *ep) 545 { 546 struct stat st; 547 time_t touched; 548 int max; 549 550 if ((max = strlen(ep->name)) > maxname) 551 maxname = max; 552 if ((max = strlen(ep->line)) > maxline) 553 maxline = max; 554 if ((max = strlen(ep->host)) > maxhost) 555 maxhost = max; 556 557 ep->tdev = 0; 558 ep->idle = (time_t)-1; 559 560 #ifdef SUPPORT_UTMP 561 /* 562 * Hack to recognize and correctly parse 563 * ut entry made by ftpd. The "tty" used 564 * by ftpd is not a real tty, just identifier in 565 * form ftpPID. Pid parsed from the "tty name" 566 * is used later to match corresponding process. 567 * NB: This is only used for utmp entries. For utmpx, 568 * we already have the pid. 569 */ 570 if (ep->pid == 0 && strncmp(ep->line, "ftp", 3) == 0) { 571 ep->pid = strtol(ep->line + 3, NULL, 10); 572 return; 573 } 574 #endif 575 if (ttystat(ep->line, &st) == -1) 576 return; 577 578 ep->tdev = st.st_rdev; 579 /* 580 * If this is the console device, attempt to ascertain 581 * the true console device dev_t. 582 */ 583 if (ep->tdev == 0) { 584 int mib[2]; 585 size_t size; 586 587 mib[0] = CTL_KERN; 588 mib[1] = KERN_CONSDEV; 589 size = sizeof(dev_t); 590 (void) sysctl(mib, 2, &ep->tdev, &size, NULL, 0); 591 } 592 593 touched = st.st_atime; 594 if (touched < ep->tv.tv_sec) { 595 /* tty untouched since before login */ 596 touched = ep->tv.tv_sec; 597 } 598 if ((ep->idle = now - touched) < 0) 599 ep->idle = 0; 600 } 601 #endif 602 603 static int 604 proc_compare_wrapper(const struct kinfo_proc2 *p1, 605 const struct kinfo_proc2 *p2) 606 { 607 struct kinfo_lwp *l1, *l2; 608 int cnt; 609 610 if (p1 == NULL) 611 return 1; 612 613 l1 = kvm_getlwps(kd, p1->p_pid, 0, sizeof(*l1), &cnt); 614 if (l1 == NULL || cnt == 0) 615 return 1; 616 617 l2 = kvm_getlwps(kd, p2->p_pid, 0, sizeof(*l1), &cnt); 618 if (l2 == NULL || cnt == 0) 619 return 0; 620 621 return proc_compare(p1, l1, p2, l2); 622 } 623 624 static void 625 fixhost(struct entry *ep) 626 { 627 char host_buf[sizeof(ep->host)]; 628 char *p, *x; 629 struct hostent *hp; 630 struct in_addr l; 631 632 strlcpy(host_buf, *ep->host ? ep->host : "-", sizeof(host_buf)); 633 p = host_buf; 634 635 /* 636 * XXX: Historical behavior, ':' in hostname means X display number, 637 * IPv6 not handled. 638 */ 639 for (x = p; x < &host_buf[sizeof(host_buf)]; x++) 640 if (*x == '\0' || *x == ':') 641 break; 642 if (x == p + sizeof(host_buf) || *x != ':') 643 x = NULL; 644 else 645 *x++ = '\0'; 646 647 if (!nflag && inet_aton(p, &l) && 648 (hp = gethostbyaddr((char *)&l, sizeof(l), AF_INET))) { 649 if (domain[0] != '\0') { 650 p = hp->h_name; 651 p += strlen(hp->h_name); 652 p -= strlen(domain); 653 if (p > hp->h_name && 654 strcasecmp(p, domain) == 0) 655 *p = '\0'; 656 } 657 p = hp->h_name; 658 } 659 660 if (x) 661 (void)snprintf(ep->host, sizeof(ep->host), "%s:%s", p, x); 662 else 663 664 strlcpy(ep->host, p, sizeof(ep->host)); 665 } 666 667 static void 668 usage(int wcmd) 669 { 670 671 if (wcmd) 672 (void)fprintf(stderr, 673 "Usage: %s [-hinw] [-M core] [-N system] [user]\n", 674 getprogname()); 675 else 676 (void)fprintf(stderr, "Usage: %s\n", getprogname()); 677 exit(1); 678 } 679