19b50d902SRodney W. Grimes /*- 29b50d902SRodney W. Grimes * Copyright (c) 1980, 1991, 1993, 1994 39b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved. 49b50d902SRodney W. Grimes * 59b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 69b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions 79b50d902SRodney W. Grimes * are met: 89b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 99b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 109b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 119b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 129b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution. 139b50d902SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 149b50d902SRodney W. Grimes * must display the following acknowledgement: 159b50d902SRodney W. Grimes * This product includes software developed by the University of 169b50d902SRodney W. Grimes * California, Berkeley and its contributors. 179b50d902SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 189b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 199b50d902SRodney W. Grimes * without specific prior written permission. 209b50d902SRodney W. Grimes * 219b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 229b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 239b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 249b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 259b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 269b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 279b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 289b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 299b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 309b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 319b50d902SRodney W. Grimes * SUCH DAMAGE. 329b50d902SRodney W. Grimes */ 339b50d902SRodney W. Grimes 349b50d902SRodney W. Grimes #ifndef lint 3590389da9SPhilippe Charnier static const char copyright[] = 369b50d902SRodney W. Grimes "@(#) Copyright (c) 1980, 1991, 1993, 1994\n\ 379b50d902SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 389b50d902SRodney W. Grimes #endif /* not lint */ 399b50d902SRodney W. Grimes 409b50d902SRodney W. Grimes #ifndef lint 4190389da9SPhilippe Charnier #if 0 429b50d902SRodney W. Grimes static char sccsid[] = "@(#)w.c 8.4 (Berkeley) 4/16/94"; 4390389da9SPhilippe Charnier #endif 4490389da9SPhilippe Charnier static const char rcsid[] = 459587fac0SAndrey A. Chernov "$Id: w.c,v 1.33 1999/03/31 21:01:39 brian Exp $"; 469b50d902SRodney W. Grimes #endif /* not lint */ 479b50d902SRodney W. Grimes 489b50d902SRodney W. Grimes /* 499b50d902SRodney W. Grimes * w - print system status (who and what) 509b50d902SRodney W. Grimes * 519b50d902SRodney W. Grimes * This program is similar to the systat command on Tenex/Tops 10/20 529b50d902SRodney W. Grimes * 539b50d902SRodney W. Grimes */ 549b50d902SRodney W. Grimes #include <sys/param.h> 559b50d902SRodney W. Grimes #include <sys/time.h> 569b50d902SRodney W. Grimes #include <sys/stat.h> 579b50d902SRodney W. Grimes #include <sys/sysctl.h> 589b50d902SRodney W. Grimes #include <sys/proc.h> 599b50d902SRodney W. Grimes #include <sys/user.h> 609b50d902SRodney W. Grimes #include <sys/ioctl.h> 619b50d902SRodney W. Grimes #include <sys/socket.h> 629b50d902SRodney W. Grimes #include <sys/tty.h> 639b50d902SRodney W. Grimes 649b50d902SRodney W. Grimes #include <machine/cpu.h> 659b50d902SRodney W. Grimes #include <netinet/in.h> 669b50d902SRodney W. Grimes #include <arpa/inet.h> 679b50d902SRodney W. Grimes 689b50d902SRodney W. Grimes #include <ctype.h> 699b50d902SRodney W. Grimes #include <err.h> 709b50d902SRodney W. Grimes #include <errno.h> 719b50d902SRodney W. Grimes #include <fcntl.h> 729b50d902SRodney W. Grimes #include <kvm.h> 739b50d902SRodney W. Grimes #include <netdb.h> 749b50d902SRodney W. Grimes #include <nlist.h> 759b50d902SRodney W. Grimes #include <paths.h> 769b50d902SRodney W. Grimes #include <stdio.h> 779b50d902SRodney W. Grimes #include <stdlib.h> 789b50d902SRodney W. Grimes #include <string.h> 799b50d902SRodney W. Grimes #include <unistd.h> 809b50d902SRodney W. Grimes #include <utmp.h> 819b50d902SRodney W. Grimes #include <vis.h> 82baf72ec1SAndrey A. Chernov #include <locale.h> 839b50d902SRodney W. Grimes 846367cd09SPeter Wemm #include <arpa/nameser.h> 856367cd09SPeter Wemm #include <resolv.h> 866367cd09SPeter Wemm 879b50d902SRodney W. Grimes #include "extern.h" 889b50d902SRodney W. Grimes 899b50d902SRodney W. Grimes struct timeval boottime; 909b50d902SRodney W. Grimes struct utmp utmp; 919b50d902SRodney W. Grimes struct winsize ws; 929b50d902SRodney W. Grimes kvm_t *kd; 939b50d902SRodney W. Grimes time_t now; /* the current time of day */ 949b50d902SRodney W. Grimes time_t uptime; /* time of last reboot & elapsed time since */ 959b50d902SRodney W. Grimes int ttywidth; /* width of tty */ 969b50d902SRodney W. Grimes int argwidth; /* width of tty */ 979b50d902SRodney W. Grimes int header = 1; /* true if -h flag: don't print heading */ 989b50d902SRodney W. Grimes int nflag; /* true if -n flag: don't convert addrs */ 991bb32253SAndrey A. Chernov int dflag; /* true if -d flag: output debug info */ 10069b41093SPeter Wemm int sortidle; /* sort by idle time */ 1019b50d902SRodney W. Grimes char *sel_user; /* login of particular user selected */ 1029b50d902SRodney W. Grimes char domain[MAXHOSTNAMELEN]; 1039b50d902SRodney W. Grimes 1049b50d902SRodney W. Grimes /* 1059b50d902SRodney W. Grimes * One of these per active utmp entry. 1069b50d902SRodney W. Grimes */ 1079b50d902SRodney W. Grimes struct entry { 1089b50d902SRodney W. Grimes struct entry *next; 1099b50d902SRodney W. Grimes struct utmp utmp; 1109b50d902SRodney W. Grimes dev_t tdev; /* dev_t of terminal */ 1119b50d902SRodney W. Grimes time_t idle; /* idle time of terminal in seconds */ 1129b50d902SRodney W. Grimes struct kinfo_proc *kp; /* `most interesting' proc */ 1139b50d902SRodney W. Grimes char *args; /* arg list of interesting process */ 1141bb32253SAndrey A. Chernov struct kinfo_proc *dkp; /* debug option proc list */ 1159b50d902SRodney W. Grimes } *ep, *ehead = NULL, **nextp = &ehead; 1169b50d902SRodney W. Grimes 1179b50d902SRodney W. Grimes static void pr_header __P((time_t *, int)); 1189b50d902SRodney W. Grimes static struct stat 1190cadb9caSBrian Somers *ttystat __P((char *, int)); 1209b50d902SRodney W. Grimes static void usage __P((int)); 121a4f33f43SMatthew Dillon static int this_is_uptime __P((const char *s)); 1229b50d902SRodney W. Grimes 1239b50d902SRodney W. Grimes char *fmt_argv __P((char **, char *, int)); /* ../../bin/ps/fmt.c */ 1249b50d902SRodney W. Grimes 1259b50d902SRodney W. Grimes int 1269b50d902SRodney W. Grimes main(argc, argv) 1279b50d902SRodney W. Grimes int argc; 1289b50d902SRodney W. Grimes char **argv; 1299b50d902SRodney W. Grimes { 1309b50d902SRodney W. Grimes struct kinfo_proc *kp; 1311bb32253SAndrey A. Chernov struct kinfo_proc *dkp; 1329b50d902SRodney W. Grimes struct hostent *hp; 1339b50d902SRodney W. Grimes struct stat *stp; 1349b50d902SRodney W. Grimes FILE *ut; 1359b50d902SRodney W. Grimes u_long l; 13670498d5aSDaniel O'Callaghan int ch, i, nentries, nusers, wcmd, longidle; 137fd67e2c5SWarner Losh char *memf, *nlistf, *p, *x; 1389b50d902SRodney W. Grimes char buf[MAXHOSTNAMELEN], errbuf[256]; 1399b50d902SRodney W. Grimes 140d1b2ad1aSAndrey A. Chernov (void) setlocale(LC_ALL, ""); 141baf72ec1SAndrey A. Chernov 1429b50d902SRodney W. Grimes /* Are we w(1) or uptime(1)? */ 143a4f33f43SMatthew Dillon if (this_is_uptime(argv[0]) == 0) { 1449b50d902SRodney W. Grimes wcmd = 0; 1459b50d902SRodney W. Grimes p = ""; 1469b50d902SRodney W. Grimes } else { 1479b50d902SRodney W. Grimes wcmd = 1; 1481bb32253SAndrey A. Chernov p = "dhiflM:N:nsuw"; 1499b50d902SRodney W. Grimes } 1509b50d902SRodney W. Grimes 1519b50d902SRodney W. Grimes memf = nlistf = NULL; 1521c8af878SWarner Losh while ((ch = getopt(argc, argv, p)) != -1) 1539b50d902SRodney W. Grimes switch (ch) { 1541bb32253SAndrey A. Chernov case 'd': 1551bb32253SAndrey A. Chernov dflag = 1; 1561bb32253SAndrey A. Chernov break; 1579b50d902SRodney W. Grimes case 'h': 1589b50d902SRodney W. Grimes header = 0; 1599b50d902SRodney W. Grimes break; 1609b50d902SRodney W. Grimes case 'i': 1619b50d902SRodney W. Grimes sortidle = 1; 1629b50d902SRodney W. Grimes break; 1639b50d902SRodney W. Grimes case 'M': 1649b50d902SRodney W. Grimes header = 0; 1659b50d902SRodney W. Grimes memf = optarg; 1669b50d902SRodney W. Grimes break; 1679b50d902SRodney W. Grimes case 'N': 1689b50d902SRodney W. Grimes nlistf = optarg; 1699b50d902SRodney W. Grimes break; 1709b50d902SRodney W. Grimes case 'n': 1719b50d902SRodney W. Grimes nflag = 1; 1729b50d902SRodney W. Grimes break; 1739b50d902SRodney W. Grimes case 'f': case 'l': case 's': case 'u': case 'w': 1749b50d902SRodney W. Grimes warnx("[-flsuw] no longer supported"); 1759b50d902SRodney W. Grimes /* FALLTHROUGH */ 1769b50d902SRodney W. Grimes case '?': 1779b50d902SRodney W. Grimes default: 1789b50d902SRodney W. Grimes usage(wcmd); 1799b50d902SRodney W. Grimes } 1809b50d902SRodney W. Grimes argc -= optind; 1819b50d902SRodney W. Grimes argv += optind; 1829b50d902SRodney W. Grimes 1836367cd09SPeter Wemm if (!(_res.options & RES_INIT)) 1846367cd09SPeter Wemm res_init(); 1856367cd09SPeter Wemm _res.retrans = 2; /* resolver timeout to 2 seconds per try */ 1866367cd09SPeter Wemm _res.retry = 1; /* only try once.. */ 1876367cd09SPeter Wemm 18866e5b18fSPaul Traina /* 18966e5b18fSPaul Traina * Discard setgid privileges if not the running kernel so that bad 19066e5b18fSPaul Traina * guys can't print interesting stuff from kernel memory. 19166e5b18fSPaul Traina */ 19266e5b18fSPaul Traina if (nlistf != NULL || memf != NULL) 19366e5b18fSPaul Traina setgid(getgid()); 19466e5b18fSPaul Traina 1959b50d902SRodney W. Grimes if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf)) == NULL) 1969b50d902SRodney W. Grimes errx(1, "%s", errbuf); 1979b50d902SRodney W. Grimes 1989b50d902SRodney W. Grimes (void)time(&now); 1999b50d902SRodney W. Grimes if ((ut = fopen(_PATH_UTMP, "r")) == NULL) 2009b50d902SRodney W. Grimes err(1, "%s", _PATH_UTMP); 2019b50d902SRodney W. Grimes 2029b50d902SRodney W. Grimes if (*argv) 2039b50d902SRodney W. Grimes sel_user = *argv; 2049b50d902SRodney W. Grimes 2059b50d902SRodney W. Grimes for (nusers = 0; fread(&utmp, sizeof(utmp), 1, ut);) { 2069b50d902SRodney W. Grimes if (utmp.ut_name[0] == '\0') 2079b50d902SRodney W. Grimes continue; 2080cadb9caSBrian Somers if (!(stp = ttystat(utmp.ut_line, UT_LINESIZE))) 2090cadb9caSBrian Somers continue; /* corrupted record */ 2109b50d902SRodney W. Grimes ++nusers; 2119b50d902SRodney W. Grimes if (wcmd == 0 || (sel_user && 2129b50d902SRodney W. Grimes strncmp(utmp.ut_name, sel_user, UT_NAMESIZE) != 0)) 2139b50d902SRodney W. Grimes continue; 2149b50d902SRodney W. Grimes if ((ep = calloc(1, sizeof(struct entry))) == NULL) 21590389da9SPhilippe Charnier errx(1, "calloc"); 2169b50d902SRodney W. Grimes *nextp = ep; 2179b50d902SRodney W. Grimes nextp = &(ep->next); 2189b50d902SRodney W. Grimes memmove(&(ep->utmp), &utmp, sizeof(struct utmp)); 2199b50d902SRodney W. Grimes ep->tdev = stp->st_rdev; 2209b50d902SRodney W. Grimes #ifdef CPU_CONSDEV 2219b50d902SRodney W. Grimes /* 2229b50d902SRodney W. Grimes * If this is the console device, attempt to ascertain 2239b50d902SRodney W. Grimes * the true console device dev_t. 2249b50d902SRodney W. Grimes */ 2259b50d902SRodney W. Grimes if (ep->tdev == 0) { 2269b50d902SRodney W. Grimes int mib[2]; 2279b50d902SRodney W. Grimes size_t size; 2289b50d902SRodney W. Grimes 2299b50d902SRodney W. Grimes mib[0] = CTL_MACHDEP; 2309b50d902SRodney W. Grimes mib[1] = CPU_CONSDEV; 2319b50d902SRodney W. Grimes size = sizeof(dev_t); 2329b50d902SRodney W. Grimes (void) sysctl(mib, 2, &ep->tdev, &size, NULL, 0); 2339b50d902SRodney W. Grimes } 2349b50d902SRodney W. Grimes #endif 2359b50d902SRodney W. Grimes if ((ep->idle = now - stp->st_atime) < 0) 2369b50d902SRodney W. Grimes ep->idle = 0; 2379b50d902SRodney W. Grimes } 2389b50d902SRodney W. Grimes (void)fclose(ut); 2399b50d902SRodney W. Grimes 2409b50d902SRodney W. Grimes if (header || wcmd == 0) { 2419b50d902SRodney W. Grimes pr_header(&now, nusers); 2429b50d902SRodney W. Grimes if (wcmd == 0) 2439b50d902SRodney W. Grimes exit (0); 2449b50d902SRodney W. Grimes 245a88d7a82SJoseph Koshy #define HEADER_USER "USER" 246a88d7a82SJoseph Koshy #define HEADER_TTY "TTY" 247a88d7a82SJoseph Koshy #define HEADER_FROM "FROM" 248a88d7a82SJoseph Koshy #define HEADER_LOGIN_IDLE "LOGIN@ IDLE " 249a88d7a82SJoseph Koshy #define HEADER_WHAT "WHAT\n" 250a88d7a82SJoseph Koshy #define WUSED (UT_NAMESIZE + UT_LINESIZE + UT_HOSTSIZE + \ 251a88d7a82SJoseph Koshy sizeof(HEADER_LOGIN_IDLE) + 3) /* header width incl. spaces */ 252a88d7a82SJoseph Koshy (void)printf("%-*.*s %-*.*s %-*.*s %s", 253a88d7a82SJoseph Koshy UT_NAMESIZE, UT_NAMESIZE, HEADER_USER, 254a88d7a82SJoseph Koshy UT_LINESIZE, UT_LINESIZE, HEADER_TTY, 255a88d7a82SJoseph Koshy UT_HOSTSIZE, UT_HOSTSIZE, HEADER_FROM, 256a88d7a82SJoseph Koshy HEADER_LOGIN_IDLE HEADER_WHAT); 2570812a2b4SPeter Wemm } 2589b50d902SRodney W. Grimes 2599b50d902SRodney W. Grimes if ((kp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nentries)) == NULL) 2609b50d902SRodney W. Grimes err(1, "%s", kvm_geterr(kd)); 2619b50d902SRodney W. Grimes for (i = 0; i < nentries; i++, kp++) { 2620cadb9caSBrian Somers struct proc *pr = &kp->kp_proc; 2639b50d902SRodney W. Grimes struct eproc *e; 2649b50d902SRodney W. Grimes 2650cadb9caSBrian Somers if (pr->p_stat == SIDL || pr->p_stat == SZOMB) 2669b50d902SRodney W. Grimes continue; 2679b50d902SRodney W. Grimes e = &kp->kp_eproc; 2689b50d902SRodney W. Grimes for (ep = ehead; ep != NULL; ep = ep->next) { 2691bb32253SAndrey A. Chernov if (ep->tdev == e->e_tdev) { 2709b50d902SRodney W. Grimes /* 2711bb32253SAndrey A. Chernov * proc is associated with this terminal 2721bb32253SAndrey A. Chernov */ 2731bb32253SAndrey A. Chernov if (ep->kp == NULL && e->e_pgid == e->e_tpgid) { 2741bb32253SAndrey A. Chernov /* 2751bb32253SAndrey A. Chernov * Proc is 'most interesting' 2769b50d902SRodney W. Grimes */ 2770cadb9caSBrian Somers if (proc_compare(&ep->kp->kp_proc, pr)) 2789b50d902SRodney W. Grimes ep->kp = kp; 2791bb32253SAndrey A. Chernov } 2801bb32253SAndrey A. Chernov /* 2811bb32253SAndrey A. Chernov * Proc debug option info; add to debug 2821bb32253SAndrey A. Chernov * list using kinfo_proc kp_eproc.e_spare 2831bb32253SAndrey A. Chernov * as next pointer; ptr to ptr avoids the 2841bb32253SAndrey A. Chernov * ptr = long assumption. 2851bb32253SAndrey A. Chernov */ 2861bb32253SAndrey A. Chernov dkp = ep->dkp; 2871bb32253SAndrey A. Chernov ep->dkp = kp; 2881bb32253SAndrey A. Chernov *((struct kinfo_proc **)(&kp->kp_eproc.e_spare[0])) = dkp; 2899b50d902SRodney W. Grimes } 2909b50d902SRodney W. Grimes } 2919b50d902SRodney W. Grimes } 2929b50d902SRodney W. Grimes if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 && 2939b50d902SRodney W. Grimes ioctl(STDERR_FILENO, TIOCGWINSZ, &ws) == -1 && 2949b50d902SRodney W. Grimes ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1) || ws.ws_col == 0) 2959b50d902SRodney W. Grimes ttywidth = 79; 2969b50d902SRodney W. Grimes else 2979b50d902SRodney W. Grimes ttywidth = ws.ws_col - 1; 2989b50d902SRodney W. Grimes argwidth = ttywidth - WUSED; 2999b50d902SRodney W. Grimes if (argwidth < 4) 3009b50d902SRodney W. Grimes argwidth = 8; 3019b50d902SRodney W. Grimes for (ep = ehead; ep != NULL; ep = ep->next) { 3029b50d902SRodney W. Grimes if (ep->kp == NULL) { 3039b50d902SRodney W. Grimes ep->args = "-"; 3049b50d902SRodney W. Grimes continue; 3059b50d902SRodney W. Grimes } 3069b50d902SRodney W. Grimes ep->args = fmt_argv(kvm_getargv(kd, ep->kp, argwidth), 3079b50d902SRodney W. Grimes ep->kp->kp_proc.p_comm, MAXCOMLEN); 3089b50d902SRodney W. Grimes if (ep->args == NULL) 3099b50d902SRodney W. Grimes err(1, NULL); 3109b50d902SRodney W. Grimes } 3119b50d902SRodney W. Grimes /* sort by idle time */ 3129b50d902SRodney W. Grimes if (sortidle && ehead != NULL) { 3139b50d902SRodney W. Grimes struct entry *from = ehead, *save; 3149b50d902SRodney W. Grimes 3159b50d902SRodney W. Grimes ehead = NULL; 3169b50d902SRodney W. Grimes while (from != NULL) { 3179b50d902SRodney W. Grimes for (nextp = &ehead; 3189b50d902SRodney W. Grimes (*nextp) && from->idle >= (*nextp)->idle; 3199b50d902SRodney W. Grimes nextp = &(*nextp)->next) 3209b50d902SRodney W. Grimes continue; 3219b50d902SRodney W. Grimes save = from; 3229b50d902SRodney W. Grimes from = from->next; 3239b50d902SRodney W. Grimes save->next = *nextp; 3249b50d902SRodney W. Grimes *nextp = save; 3259b50d902SRodney W. Grimes } 3269b50d902SRodney W. Grimes } 3279b50d902SRodney W. Grimes 3289b50d902SRodney W. Grimes if (!nflag) 3299b50d902SRodney W. Grimes if (gethostname(domain, sizeof(domain) - 1) < 0 || 3309b50d902SRodney W. Grimes (p = strchr(domain, '.')) == 0) 3319b50d902SRodney W. Grimes domain[0] = '\0'; 3329b50d902SRodney W. Grimes else { 3339b50d902SRodney W. Grimes domain[sizeof(domain) - 1] = '\0'; 3349b50d902SRodney W. Grimes memmove(domain, p, strlen(p) + 1); 3359b50d902SRodney W. Grimes } 3369b50d902SRodney W. Grimes 3379b50d902SRodney W. Grimes for (ep = ehead; ep != NULL; ep = ep->next) { 3389587fac0SAndrey A. Chernov char host_buf[UT_HOSTSIZE + 1]; 3399587fac0SAndrey A. Chernov 3409587fac0SAndrey A. Chernov host_buf[UT_HOSTSIZE] = '\0'; 3419587fac0SAndrey A. Chernov strncpy(host_buf, ep->utmp.ut_host, UT_HOSTSIZE); 3429587fac0SAndrey A. Chernov p = *host_buf ? host_buf : "-"; 3439b50d902SRodney W. Grimes if ((x = strchr(p, ':')) != NULL) 3449b50d902SRodney W. Grimes *x++ = '\0'; 3459b50d902SRodney W. Grimes if (!nflag && isdigit(*p) && 3469b50d902SRodney W. Grimes (long)(l = inet_addr(p)) != -1 && 3479b50d902SRodney W. Grimes (hp = gethostbyaddr((char *)&l, sizeof(l), AF_INET))) { 3489b50d902SRodney W. Grimes if (domain[0] != '\0') { 3499b50d902SRodney W. Grimes p = hp->h_name; 3509b50d902SRodney W. Grimes p += strlen(hp->h_name); 3519b50d902SRodney W. Grimes p -= strlen(domain); 3529b50d902SRodney W. Grimes if (p > hp->h_name && strcmp(p, domain) == 0) 3539b50d902SRodney W. Grimes *p = '\0'; 3549b50d902SRodney W. Grimes } 3559b50d902SRodney W. Grimes p = hp->h_name; 3569b50d902SRodney W. Grimes } 35769b41093SPeter Wemm if (nflag && *p && strcmp(p, "-") && 35869b41093SPeter Wemm inet_addr(p) == INADDR_NONE) { 35966b12529SAndrey A. Chernov hp = gethostbyname(p); 36066b12529SAndrey A. Chernov 36166b12529SAndrey A. Chernov if (hp != NULL) { 36266b12529SAndrey A. Chernov struct in_addr in; 36366b12529SAndrey A. Chernov 36466b12529SAndrey A. Chernov memmove(&in, hp->h_addr, sizeof(in)); 36566b12529SAndrey A. Chernov p = inet_ntoa(in); 366c54481c1SAndrey A. Chernov } 36766b12529SAndrey A. Chernov } 3689b50d902SRodney W. Grimes if (x) { 3699587fac0SAndrey A. Chernov (void)snprintf(buf, sizeof(buf), "%s:%s", p, x); 3709b50d902SRodney W. Grimes p = buf; 3719b50d902SRodney W. Grimes } 3721bb32253SAndrey A. Chernov if (dflag) { 3731bb32253SAndrey A. Chernov for (dkp = ep->dkp; dkp != NULL; dkp = *((struct kinfo_proc **)(&dkp->kp_eproc.e_spare[0]))) { 3740cadb9caSBrian Somers char *ptr; 3750cadb9caSBrian Somers ptr = fmt_argv(kvm_getargv(kd, dkp, argwidth), 3761bb32253SAndrey A. Chernov dkp->kp_proc.p_comm, MAXCOMLEN); 3770cadb9caSBrian Somers if (ptr == NULL) 3780cadb9caSBrian Somers ptr = "-"; 3790cadb9caSBrian Somers (void)printf( "\t\t%-9d %s\n", dkp->kp_proc.p_pid, ptr); 3801bb32253SAndrey A. Chernov } 3811bb32253SAndrey A. Chernov } 3823d65e4d1SPoul-Henning Kamp (void)printf("%-*.*s %-*.*s %-*.*s ", 3839b50d902SRodney W. Grimes UT_NAMESIZE, UT_NAMESIZE, ep->utmp.ut_name, 3843d65e4d1SPoul-Henning Kamp UT_LINESIZE, UT_LINESIZE, 385255318a8SAndrey A. Chernov strncmp(ep->utmp.ut_line, "tty", 3) && 386255318a8SAndrey A. Chernov strncmp(ep->utmp.ut_line, "cua", 3) ? 3879b50d902SRodney W. Grimes ep->utmp.ut_line : ep->utmp.ut_line + 3, 3889b50d902SRodney W. Grimes UT_HOSTSIZE, UT_HOSTSIZE, *p ? p : "-"); 3899b50d902SRodney W. Grimes pr_attime(&ep->utmp.ut_time, &now); 39070498d5aSDaniel O'Callaghan longidle = pr_idle(ep->idle); 39169b41093SPeter Wemm (void)printf("%.*s\n", argwidth - longidle, ep->args); 3929b50d902SRodney W. Grimes } 3939b50d902SRodney W. Grimes exit(0); 3949b50d902SRodney W. Grimes } 3959b50d902SRodney W. Grimes 3969b50d902SRodney W. Grimes static void 3979b50d902SRodney W. Grimes pr_header(nowp, nusers) 3989b50d902SRodney W. Grimes time_t *nowp; 3999b50d902SRodney W. Grimes int nusers; 4009b50d902SRodney W. Grimes { 4019b50d902SRodney W. Grimes double avenrun[3]; 4029b50d902SRodney W. Grimes time_t uptime; 4039d8c91b7SAndrey A. Chernov int days, hrs, i, mins, secs; 4049b50d902SRodney W. Grimes int mib[2]; 4059b50d902SRodney W. Grimes size_t size; 4069b50d902SRodney W. Grimes char buf[256]; 4079b50d902SRodney W. Grimes 4089b50d902SRodney W. Grimes /* 4099b50d902SRodney W. Grimes * Print time of day. 4109b50d902SRodney W. Grimes * 4119b50d902SRodney W. Grimes * SCCS forces the string manipulation below, as it replaces 4129b50d902SRodney W. Grimes * %, M, and % in a character string with the file name. 4139b50d902SRodney W. Grimes */ 4146d7977a7SKazutaka YOKOTA (void)strftime(buf, sizeof(buf) - 1, 4159b50d902SRodney W. Grimes __CONCAT("%l:%","M%p"), localtime(nowp)); 4166d7977a7SKazutaka YOKOTA buf[sizeof(buf) - 1] = '\0'; 4179b50d902SRodney W. Grimes (void)printf("%s ", buf); 4189b50d902SRodney W. Grimes 4199b50d902SRodney W. Grimes /* 4209b50d902SRodney W. Grimes * Print how long system has been up. 4219b50d902SRodney W. Grimes * (Found by looking getting "boottime" from the kernel) 4229b50d902SRodney W. Grimes */ 4239b50d902SRodney W. Grimes mib[0] = CTL_KERN; 4249b50d902SRodney W. Grimes mib[1] = KERN_BOOTTIME; 4259b50d902SRodney W. Grimes size = sizeof(boottime); 4269b50d902SRodney W. Grimes if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 && 4279b50d902SRodney W. Grimes boottime.tv_sec != 0) { 4289b50d902SRodney W. Grimes uptime = now - boottime.tv_sec; 4299b50d902SRodney W. Grimes uptime += 30; 430656dcd43SGarrett Wollman days = uptime / 86400; 431656dcd43SGarrett Wollman uptime %= 86400; 432656dcd43SGarrett Wollman hrs = uptime / 3600; 433656dcd43SGarrett Wollman uptime %= 3600; 434656dcd43SGarrett Wollman mins = uptime / 60; 4359d8c91b7SAndrey A. Chernov secs = uptime % 60; 4369b50d902SRodney W. Grimes (void)printf(" up"); 4379b50d902SRodney W. Grimes if (days > 0) 4389b50d902SRodney W. Grimes (void)printf(" %d day%s,", days, days > 1 ? "s" : ""); 4399b50d902SRodney W. Grimes if (hrs > 0 && mins > 0) 4409b50d902SRodney W. Grimes (void)printf(" %2d:%02d,", hrs, mins); 4419d8c91b7SAndrey A. Chernov else if (hrs > 0) 4429b50d902SRodney W. Grimes (void)printf(" %d hr%s,", 4439b50d902SRodney W. Grimes hrs, hrs > 1 ? "s" : ""); 4449d8c91b7SAndrey A. Chernov else if (mins > 0) 4459b50d902SRodney W. Grimes (void)printf(" %d min%s,", 4469b50d902SRodney W. Grimes mins, mins > 1 ? "s" : ""); 4479d8c91b7SAndrey A. Chernov else 4489d8c91b7SAndrey A. Chernov (void)printf(" %d sec%s,", 4499d8c91b7SAndrey A. Chernov secs, secs > 1 ? "s" : ""); 4509b50d902SRodney W. Grimes } 4519b50d902SRodney W. Grimes 4529b50d902SRodney W. Grimes /* Print number of users logged in to system */ 4531b0c06d9SScott Mace (void)printf(" %d user%s", nusers, nusers == 1 ? "" : "s"); 4549b50d902SRodney W. Grimes 4559b50d902SRodney W. Grimes /* 4569b50d902SRodney W. Grimes * Print 1, 5, and 15 minute load averages. 4579b50d902SRodney W. Grimes */ 4589b50d902SRodney W. Grimes if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) == -1) 4599b50d902SRodney W. Grimes (void)printf(", no load average information available\n"); 4609b50d902SRodney W. Grimes else { 4619b50d902SRodney W. Grimes (void)printf(", load averages:"); 4629b50d902SRodney W. Grimes for (i = 0; i < (sizeof(avenrun) / sizeof(avenrun[0])); i++) { 4639b50d902SRodney W. Grimes if (i > 0) 4649b50d902SRodney W. Grimes (void)printf(","); 4659b50d902SRodney W. Grimes (void)printf(" %.2f", avenrun[i]); 4669b50d902SRodney W. Grimes } 4679b50d902SRodney W. Grimes (void)printf("\n"); 4689b50d902SRodney W. Grimes } 4699b50d902SRodney W. Grimes } 4709b50d902SRodney W. Grimes 4719b50d902SRodney W. Grimes static struct stat * 4720cadb9caSBrian Somers ttystat(line, sz) 4739b50d902SRodney W. Grimes char *line; 4740cadb9caSBrian Somers int sz; 4759b50d902SRodney W. Grimes { 4769b50d902SRodney W. Grimes static struct stat sb; 4779b50d902SRodney W. Grimes char ttybuf[MAXPATHLEN]; 4789b50d902SRodney W. Grimes 4790cadb9caSBrian Somers (void)snprintf(ttybuf, sizeof(ttybuf), "%s%.*s", _PATH_DEV, sz, line); 480bc53c0a6SJoseph Koshy if (stat(ttybuf, &sb)) { 481bc53c0a6SJoseph Koshy warn("%s", ttybuf); 482bc53c0a6SJoseph Koshy return NULL; 483bc53c0a6SJoseph Koshy } 4849b50d902SRodney W. Grimes return (&sb); 4859b50d902SRodney W. Grimes } 4869b50d902SRodney W. Grimes 4879b50d902SRodney W. Grimes static void 4889b50d902SRodney W. Grimes usage(wcmd) 4899b50d902SRodney W. Grimes int wcmd; 4909b50d902SRodney W. Grimes { 4919b50d902SRodney W. Grimes if (wcmd) 4929b50d902SRodney W. Grimes (void)fprintf(stderr, 4937860b557SAndrey A. Chernov "usage: w [-dhin] [-M core] [-N system] [user]\n"); 4949b50d902SRodney W. Grimes else 49590389da9SPhilippe Charnier (void)fprintf(stderr, 49690389da9SPhilippe Charnier "usage: uptime\n"); 4979b50d902SRodney W. Grimes exit (1); 4989b50d902SRodney W. Grimes } 499a4f33f43SMatthew Dillon 500a4f33f43SMatthew Dillon static int 501a4f33f43SMatthew Dillon this_is_uptime(s) 502a4f33f43SMatthew Dillon const char *s; 503a4f33f43SMatthew Dillon { 504a4f33f43SMatthew Dillon const char *u; 505a4f33f43SMatthew Dillon 506a4f33f43SMatthew Dillon if ((u = strrchr(s, '/')) != NULL) 507a4f33f43SMatthew Dillon ++u; 508a4f33f43SMatthew Dillon else 509a4f33f43SMatthew Dillon u = s; 510a4f33f43SMatthew Dillon if (strcmp(u, "uptime") == 0) 511a4f33f43SMatthew Dillon return(0); 512a4f33f43SMatthew Dillon return(-1); 513a4f33f43SMatthew Dillon } 514a4f33f43SMatthew Dillon 515