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