19b50d902SRodney W. Grimes /*- 28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 38a16b7a1SPedro F. Giffuni * 49b50d902SRodney W. Grimes * Copyright (c) 1980, 1991, 1993, 1994 59b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved. 69b50d902SRodney W. Grimes * 79b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 89b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions 99b50d902SRodney W. Grimes * are met: 109b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 119b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 129b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 139b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 149b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution. 15fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 169b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 179b50d902SRodney W. Grimes * without specific prior written permission. 189b50d902SRodney W. Grimes * 199b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 209b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 219b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 229b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 239b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 249b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 259b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 269b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 279b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 289b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 299b50d902SRodney W. Grimes * SUCH DAMAGE. 309b50d902SRodney W. Grimes */ 319b50d902SRodney W. Grimes 328b56c58bSMark Murray #include <sys/cdefs.h> 338b56c58bSMark Murray 348b56c58bSMark Murray __FBSDID("$FreeBSD$"); 358b56c58bSMark Murray 369b50d902SRodney W. Grimes #ifndef lint 3790389da9SPhilippe Charnier static const char copyright[] = 389b50d902SRodney W. Grimes "@(#) Copyright (c) 1980, 1991, 1993, 1994\n\ 399b50d902SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 408b56c58bSMark Murray #endif 419b50d902SRodney W. Grimes 429b50d902SRodney W. Grimes #ifndef lint 438b56c58bSMark Murray static const char sccsid[] = "@(#)w.c 8.4 (Berkeley) 4/16/94"; 4490389da9SPhilippe Charnier #endif 459b50d902SRodney W. Grimes 469b50d902SRodney W. Grimes /* 479b50d902SRodney W. Grimes * w - print system status (who and what) 489b50d902SRodney W. Grimes * 499b50d902SRodney W. Grimes * This program is similar to the systat command on Tenex/Tops 10/20 509b50d902SRodney W. Grimes * 519b50d902SRodney W. Grimes */ 529b50d902SRodney W. Grimes #include <sys/param.h> 539b50d902SRodney W. Grimes #include <sys/time.h> 549b50d902SRodney W. Grimes #include <sys/stat.h> 559b50d902SRodney W. Grimes #include <sys/sysctl.h> 569b50d902SRodney W. Grimes #include <sys/proc.h> 579b50d902SRodney W. Grimes #include <sys/user.h> 589b50d902SRodney W. Grimes #include <sys/ioctl.h> 59630a02daSAllan Jude #include <sys/sbuf.h> 609b50d902SRodney W. Grimes #include <sys/socket.h> 619b50d902SRodney W. Grimes #include <sys/tty.h> 62630a02daSAllan Jude #include <sys/types.h> 639b50d902SRodney W. Grimes 64a29cc9a3SAndriy Gapon #include <machine/cpu.h> 659b50d902SRodney W. Grimes #include <netinet/in.h> 669b50d902SRodney W. Grimes #include <arpa/inet.h> 678b56c58bSMark Murray #include <arpa/nameser.h> 689b50d902SRodney W. Grimes 69821df508SXin LI #include <ctype.h> 709b50d902SRodney W. Grimes #include <err.h> 719b50d902SRodney W. Grimes #include <errno.h> 729b50d902SRodney W. Grimes #include <fcntl.h> 739b50d902SRodney W. Grimes #include <kvm.h> 7400034645SAndrey A. Chernov #include <langinfo.h> 75920aa23dSEitan Adler #include <libgen.h> 765b718312SBrian Somers #include <libutil.h> 77af943f55SThomas Moestl #include <limits.h> 7800034645SAndrey A. Chernov #include <locale.h> 799b50d902SRodney W. Grimes #include <netdb.h> 809b50d902SRodney W. Grimes #include <nlist.h> 819b50d902SRodney W. Grimes #include <paths.h> 828b56c58bSMark Murray #include <resolv.h> 839b50d902SRodney W. Grimes #include <stdio.h> 849b50d902SRodney W. Grimes #include <stdlib.h> 859b50d902SRodney W. Grimes #include <string.h> 86821df508SXin LI #include <timeconv.h> 879b50d902SRodney W. Grimes #include <unistd.h> 881131779fSEd Schouten #include <utmpx.h> 89821df508SXin LI #include <vis.h> 9076c0abf1SMarcel Moolenaar #include <libxo/xo.h> 919b50d902SRodney W. Grimes 929b50d902SRodney W. Grimes #include "extern.h" 939b50d902SRodney W. Grimes 94d11cba9cSEd Schouten static struct utmpx *utmp; 95d11cba9cSEd Schouten static struct winsize ws; 96d11cba9cSEd Schouten static kvm_t *kd; 97d11cba9cSEd Schouten static time_t now; /* the current time of day */ 98*04f6b9cbSDag-Erling Smørgrav static size_t ttywidth; /* width of tty */ 99*04f6b9cbSDag-Erling Smørgrav static size_t fromwidth = 0; /* max width of "from" field */ 100*04f6b9cbSDag-Erling Smørgrav static size_t argwidth; /* width of arguments */ 101d11cba9cSEd Schouten static int header = 1; /* true if -h flag: don't print heading */ 102d11cba9cSEd Schouten static int nflag; /* true if -n flag: don't convert addrs */ 103d11cba9cSEd Schouten static int dflag; /* true if -d flag: output debug info */ 104d11cba9cSEd Schouten static int sortidle; /* sort by idle time */ 10500034645SAndrey A. Chernov int use_ampm; /* use AM/PM time */ 106d11cba9cSEd Schouten static int use_comma; /* use comma as floats separator */ 107d11cba9cSEd Schouten static char **sel_users; /* login array of particular users selected */ 1089b50d902SRodney W. Grimes 1099b50d902SRodney W. Grimes /* 1109b50d902SRodney W. Grimes * One of these per active utmp entry. 1119b50d902SRodney W. Grimes */ 112d11cba9cSEd Schouten static struct entry { 1139b50d902SRodney W. Grimes struct entry *next; 114dc65bcacSEd Schouten struct utmpx utmp; 1159b50d902SRodney W. Grimes dev_t tdev; /* dev_t of terminal */ 1169b50d902SRodney W. Grimes time_t idle; /* idle time of terminal in seconds */ 1179b50d902SRodney W. Grimes struct kinfo_proc *kp; /* `most interesting' proc */ 1189b50d902SRodney W. Grimes char *args; /* arg list of interesting process */ 1191bb32253SAndrey A. Chernov struct kinfo_proc *dkp; /* debug option proc list */ 120ec9801deSMike Karels char *from; /* "from": name or addr */ 121ae335351SMike Karels char *save_from; /* original "from": name or addr */ 1229b50d902SRodney W. Grimes } *ep, *ehead = NULL, **nextp = &ehead; 1239b50d902SRodney W. Grimes 12402f56376SDavid Malone #define debugproc(p) *(&((struct kinfo_proc *)p)->ki_udata) 125ac3cd520SBrian Feldman 126dc65bcacSEd Schouten #define W_DISPUSERSIZE 10 127dc65bcacSEd Schouten #define W_DISPLINESIZE 8 128ec9801deSMike Karels #define W_MAXHOSTSIZE 40 1295b718312SBrian Somers 1303f330d7dSWarner Losh static void pr_header(time_t *, int); 131dc65bcacSEd Schouten static struct stat *ttystat(char *); 1323f330d7dSWarner Losh static void usage(int); 1339b50d902SRodney W. Grimes 1349f0b6e5eSJohn Baldwin char *fmt_argv(char **, char *, char *, size_t); /* ../../bin/ps/fmt.c */ 1359b50d902SRodney W. Grimes 1369b50d902SRodney W. Grimes int 137e8e649ccSJuli Mallett main(int argc, char *argv[]) 1389b50d902SRodney W. Grimes { 1399b50d902SRodney W. Grimes struct kinfo_proc *kp; 1401bb32253SAndrey A. Chernov struct kinfo_proc *dkp; 1419b50d902SRodney W. Grimes struct stat *stp; 1423bebe991SBrian Somers time_t touched; 143*04f6b9cbSDag-Erling Smørgrav size_t width; 144ab9cee11SXin LI int ch, i, nentries, nusers, wcmd, longidle, longattime; 14576c0abf1SMarcel Moolenaar const char *memf, *nlistf, *p, *save_p; 1468b56c58bSMark Murray char *x_suffix; 147*04f6b9cbSDag-Erling Smørgrav char errbuf[_POSIX2_LINE_MAX]; 148*04f6b9cbSDag-Erling Smørgrav char buf[MAXHOSTNAMELEN], fn[MAXHOSTNAMELEN]; 1495b718312SBrian Somers char *dot; 1509b50d902SRodney W. Grimes 151d1b2ad1aSAndrey A. Chernov (void)setlocale(LC_ALL, ""); 15200034645SAndrey A. Chernov use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0'); 1532742fc8eSAndrey A. Chernov use_comma = (*nl_langinfo(RADIXCHAR) != ','); 154baf72ec1SAndrey A. Chernov 15576c0abf1SMarcel Moolenaar argc = xo_parse_args(argc, argv); 15676c0abf1SMarcel Moolenaar if (argc < 0) 15776c0abf1SMarcel Moolenaar exit(1); 15876c0abf1SMarcel Moolenaar 1599b50d902SRodney W. Grimes /* Are we w(1) or uptime(1)? */ 160920aa23dSEitan Adler if (strcmp(basename(argv[0]), "uptime") == 0) { 1619b50d902SRodney W. Grimes wcmd = 0; 1629b50d902SRodney W. Grimes p = ""; 1639b50d902SRodney W. Grimes } else { 1649b50d902SRodney W. Grimes wcmd = 1; 1651bb32253SAndrey A. Chernov p = "dhiflM:N:nsuw"; 1669b50d902SRodney W. Grimes } 1679b50d902SRodney W. Grimes 168c08dcaf1SRebecca Cran memf = _PATH_DEVNULL; 169c08dcaf1SRebecca Cran nlistf = NULL; 1701c8af878SWarner Losh while ((ch = getopt(argc, argv, p)) != -1) 1719b50d902SRodney W. Grimes switch (ch) { 1721bb32253SAndrey A. Chernov case 'd': 1731bb32253SAndrey A. Chernov dflag = 1; 1741bb32253SAndrey A. Chernov break; 1759b50d902SRodney W. Grimes case 'h': 1769b50d902SRodney W. Grimes header = 0; 1779b50d902SRodney W. Grimes break; 1789b50d902SRodney W. Grimes case 'i': 1799b50d902SRodney W. Grimes sortidle = 1; 1809b50d902SRodney W. Grimes break; 1819b50d902SRodney W. Grimes case 'M': 1829b50d902SRodney W. Grimes header = 0; 1839b50d902SRodney W. Grimes memf = optarg; 1849b50d902SRodney W. Grimes break; 1859b50d902SRodney W. Grimes case 'N': 1869b50d902SRodney W. Grimes nlistf = optarg; 1879b50d902SRodney W. Grimes break; 1889b50d902SRodney W. Grimes case 'n': 1892279a9a4SHajimu UMEMOTO nflag += 1; 1909b50d902SRodney W. Grimes break; 1919b50d902SRodney W. Grimes case 'f': case 'l': case 's': case 'u': case 'w': 192*04f6b9cbSDag-Erling Smørgrav warnx("-%c no longer supported", ch); 1939b50d902SRodney W. Grimes /* FALLTHROUGH */ 1949b50d902SRodney W. Grimes case '?': 1959b50d902SRodney W. Grimes default: 1969b50d902SRodney W. Grimes usage(wcmd); 1979b50d902SRodney W. Grimes } 1989b50d902SRodney W. Grimes argc -= optind; 1999b50d902SRodney W. Grimes argv += optind; 2009b50d902SRodney W. Grimes 2016367cd09SPeter Wemm if (!(_res.options & RES_INIT)) 2026367cd09SPeter Wemm res_init(); 2036367cd09SPeter Wemm _res.retrans = 2; /* resolver timeout to 2 seconds per try */ 2046367cd09SPeter Wemm _res.retry = 1; /* only try once.. */ 2056367cd09SPeter Wemm 2069b50d902SRodney W. Grimes if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf)) == NULL) 2079b50d902SRodney W. Grimes errx(1, "%s", errbuf); 2089b50d902SRodney W. Grimes 2099b50d902SRodney W. Grimes (void)time(&now); 2109b50d902SRodney W. Grimes 2119b50d902SRodney W. Grimes if (*argv) 212ac3cd520SBrian Feldman sel_users = argv; 2139b50d902SRodney W. Grimes 214dc65bcacSEd Schouten setutxent(); 215dc65bcacSEd Schouten for (nusers = 0; (utmp = getutxent()) != NULL;) { 216ec9801deSMike Karels struct addrinfo hints, *res; 217ec9801deSMike Karels struct sockaddr_storage ss; 218ec9801deSMike Karels struct sockaddr *sa = (struct sockaddr *)&ss; 219ec9801deSMike Karels struct sockaddr_in *lsin = (struct sockaddr_in *)&ss; 220ec9801deSMike Karels struct sockaddr_in6 *lsin6 = (struct sockaddr_in6 *)&ss; 221ec9801deSMike Karels int isaddr; 222ec9801deSMike Karels 223dc65bcacSEd Schouten if (utmp->ut_type != USER_PROCESS) 2249b50d902SRodney W. Grimes continue; 225dc65bcacSEd Schouten if (!(stp = ttystat(utmp->ut_line))) 2260cadb9caSBrian Somers continue; /* corrupted record */ 2279b50d902SRodney W. Grimes ++nusers; 228ac3cd520SBrian Feldman if (wcmd == 0) 2299b50d902SRodney W. Grimes continue; 230ac3cd520SBrian Feldman if (sel_users) { 231ac3cd520SBrian Feldman int usermatch; 232ac3cd520SBrian Feldman char **user; 233ac3cd520SBrian Feldman 234ac3cd520SBrian Feldman usermatch = 0; 235ac3cd520SBrian Feldman for (user = sel_users; !usermatch && *user; user++) 236dc65bcacSEd Schouten if (!strcmp(utmp->ut_user, *user)) 237ac3cd520SBrian Feldman usermatch = 1; 238ac3cd520SBrian Feldman if (!usermatch) 239ac3cd520SBrian Feldman continue; 240ac3cd520SBrian Feldman } 2419b50d902SRodney W. Grimes if ((ep = calloc(1, sizeof(struct entry))) == NULL) 24290389da9SPhilippe Charnier errx(1, "calloc"); 2439b50d902SRodney W. Grimes *nextp = ep; 244ac3cd520SBrian Feldman nextp = &ep->next; 245dc65bcacSEd Schouten memmove(&ep->utmp, utmp, sizeof *utmp); 2469b50d902SRodney W. Grimes ep->tdev = stp->st_rdev; 2479b50d902SRodney W. Grimes /* 2489b50d902SRodney W. Grimes * If this is the console device, attempt to ascertain 2499b50d902SRodney W. Grimes * the true console device dev_t. 2509b50d902SRodney W. Grimes */ 2519b50d902SRodney W. Grimes if (ep->tdev == 0) { 2529b50d902SRodney W. Grimes size_t size; 2539b50d902SRodney W. Grimes 2549b50d902SRodney W. Grimes size = sizeof(dev_t); 255bec1fa86SPoul-Henning Kamp (void)sysctlbyname("machdep.consdev", &ep->tdev, &size, NULL, 0); 2569b50d902SRodney W. Grimes } 2573bebe991SBrian Somers touched = stp->st_atime; 258dc65bcacSEd Schouten if (touched < ep->utmp.ut_tv.tv_sec) { 2593bebe991SBrian Somers /* tty untouched since before login */ 260dc65bcacSEd Schouten touched = ep->utmp.ut_tv.tv_sec; 2613bebe991SBrian Somers } 2623bebe991SBrian Somers if ((ep->idle = now - touched) < 0) 2639b50d902SRodney W. Grimes ep->idle = 0; 264ec9801deSMike Karels 265ec9801deSMike Karels save_p = p = *ep->utmp.ut_host ? ep->utmp.ut_host : "-"; 266ec9801deSMike Karels if ((x_suffix = strrchr(p, ':')) != NULL) { 267ec9801deSMike Karels if ((dot = strchr(x_suffix, '.')) != NULL && 268ec9801deSMike Karels strchr(dot+1, '.') == NULL) 269ec9801deSMike Karels *x_suffix++ = '\0'; 270ec9801deSMike Karels else 271ec9801deSMike Karels x_suffix = NULL; 272ec9801deSMike Karels } 273ec9801deSMike Karels 274ec9801deSMike Karels isaddr = 0; 275ec9801deSMike Karels memset(&ss, '\0', sizeof(ss)); 276ec9801deSMike Karels if (inet_pton(AF_INET6, p, &lsin6->sin6_addr) == 1) { 277ec9801deSMike Karels lsin6->sin6_len = sizeof(*lsin6); 278ec9801deSMike Karels lsin6->sin6_family = AF_INET6; 279ec9801deSMike Karels isaddr = 1; 280ec9801deSMike Karels } else if (inet_pton(AF_INET, p, &lsin->sin_addr) == 1) { 281ec9801deSMike Karels lsin->sin_len = sizeof(*lsin); 282ec9801deSMike Karels lsin->sin_family = AF_INET; 283ec9801deSMike Karels isaddr = 1; 284ec9801deSMike Karels } 285ec9801deSMike Karels if (nflag == 0) { 286ec9801deSMike Karels /* Attempt to change an IP address into a name */ 287ec9801deSMike Karels if (isaddr && realhostname_sa(fn, sizeof(fn), sa, 288ec9801deSMike Karels sa->sa_len) == HOSTNAME_FOUND) 289ec9801deSMike Karels p = fn; 290ec9801deSMike Karels } else if (!isaddr && nflag > 1) { 291ec9801deSMike Karels /* 292ec9801deSMike Karels * If a host has only one A/AAAA RR, change a 293ec9801deSMike Karels * name into an IP address 294ec9801deSMike Karels */ 295ec9801deSMike Karels memset(&hints, 0, sizeof(hints)); 296ec9801deSMike Karels hints.ai_flags = AI_PASSIVE; 297ec9801deSMike Karels hints.ai_family = AF_UNSPEC; 298ec9801deSMike Karels hints.ai_socktype = SOCK_STREAM; 299ec9801deSMike Karels if (getaddrinfo(p, NULL, &hints, &res) == 0) { 300ec9801deSMike Karels if (res->ai_next == NULL && 301ec9801deSMike Karels getnameinfo(res->ai_addr, res->ai_addrlen, 302ec9801deSMike Karels fn, sizeof(fn), NULL, 0, 303ec9801deSMike Karels NI_NUMERICHOST) == 0) 304ec9801deSMike Karels p = fn; 305ec9801deSMike Karels freeaddrinfo(res); 306ec9801deSMike Karels } 307ec9801deSMike Karels } 308ec9801deSMike Karels 309ec9801deSMike Karels if (x_suffix) { 310ec9801deSMike Karels (void)snprintf(buf, sizeof(buf), "%s:%s", p, x_suffix); 311ec9801deSMike Karels p = buf; 312ec9801deSMike Karels } 313ec9801deSMike Karels ep->from = strdup(p); 314*04f6b9cbSDag-Erling Smørgrav if ((width = strlen(p)) > fromwidth) 315*04f6b9cbSDag-Erling Smørgrav fromwidth = width; 316ae335351SMike Karels if (save_p != p) 317ae335351SMike Karels ep->save_from = strdup(save_p); 3189b50d902SRodney W. Grimes } 319dc65bcacSEd Schouten endutxent(); 3209b50d902SRodney W. Grimes 321ec9801deSMike Karels #define HEADER_USER "USER" 322ec9801deSMike Karels #define HEADER_TTY "TTY" 323ec9801deSMike Karels #define HEADER_FROM "FROM" 324ec9801deSMike Karels #define HEADER_LOGIN_IDLE "LOGIN@ IDLE " 325ec9801deSMike Karels #define HEADER_WHAT "WHAT\n" 326ec9801deSMike Karels #define WUSED (W_DISPUSERSIZE + W_DISPLINESIZE + fromwidth + \ 327ec9801deSMike Karels sizeof(HEADER_LOGIN_IDLE) + 3) /* header width incl. spaces */ 328ec9801deSMike Karels 329*04f6b9cbSDag-Erling Smørgrav if (sizeof(HEADER_FROM) > fromwidth) 330ec9801deSMike Karels fromwidth = sizeof(HEADER_FROM); 331ec9801deSMike Karels fromwidth++; 332ec9801deSMike Karels if (fromwidth > W_MAXHOSTSIZE) 333ec9801deSMike Karels fromwidth = W_MAXHOSTSIZE; 334ec9801deSMike Karels 33576c0abf1SMarcel Moolenaar xo_open_container("uptime-information"); 33676c0abf1SMarcel Moolenaar 3379b50d902SRodney W. Grimes if (header || wcmd == 0) { 3389b50d902SRodney W. Grimes pr_header(&now, nusers); 339c59c7f97SPoul-Henning Kamp if (wcmd == 0) { 34076c0abf1SMarcel Moolenaar xo_close_container("uptime-information"); 341441ee032SMichael Gmelin xo_finish(); 342c59c7f97SPoul-Henning Kamp (void)kvm_close(kd); 3439b50d902SRodney W. Grimes exit(0); 344c59c7f97SPoul-Henning Kamp } 3459b50d902SRodney W. Grimes 34676c0abf1SMarcel Moolenaar xo_emit("{T:/%-*.*s} {T:/%-*.*s} {T:/%-*.*s} {T:/%s}", 347dc65bcacSEd Schouten W_DISPUSERSIZE, W_DISPUSERSIZE, HEADER_USER, 348dc65bcacSEd Schouten W_DISPLINESIZE, W_DISPLINESIZE, HEADER_TTY, 349ec9801deSMike Karels fromwidth, fromwidth, HEADER_FROM, 350a88d7a82SJoseph Koshy HEADER_LOGIN_IDLE HEADER_WHAT); 3510812a2b4SPeter Wemm } 3529b50d902SRodney W. Grimes 3539b50d902SRodney W. Grimes if ((kp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nentries)) == NULL) 3549b50d902SRodney W. Grimes err(1, "%s", kvm_geterr(kd)); 3559b50d902SRodney W. Grimes for (i = 0; i < nentries; i++, kp++) { 3561131779fSEd Schouten if (kp->ki_stat == SIDL || kp->ki_stat == SZOMB || 3571131779fSEd Schouten kp->ki_tdev == NODEV) 3589b50d902SRodney W. Grimes continue; 3599b50d902SRodney W. Grimes for (ep = ehead; ep != NULL; ep = ep->next) { 3601f7d2501SKirk McKusick if (ep->tdev == kp->ki_tdev) { 3619b50d902SRodney W. Grimes /* 3621bb32253SAndrey A. Chernov * proc is associated with this terminal 3631bb32253SAndrey A. Chernov */ 3641f7d2501SKirk McKusick if (ep->kp == NULL && kp->ki_pgid == kp->ki_tpgid) { 3651bb32253SAndrey A. Chernov /* 3661bb32253SAndrey A. Chernov * Proc is 'most interesting' 3679b50d902SRodney W. Grimes */ 3681f7d2501SKirk McKusick if (proc_compare(ep->kp, kp)) 3699b50d902SRodney W. Grimes ep->kp = kp; 3701bb32253SAndrey A. Chernov } 3711bb32253SAndrey A. Chernov /* 3721bb32253SAndrey A. Chernov * Proc debug option info; add to debug 3731f7d2501SKirk McKusick * list using kinfo_proc ki_spare[0] 3741bb32253SAndrey A. Chernov * as next pointer; ptr to ptr avoids the 3751bb32253SAndrey A. Chernov * ptr = long assumption. 3761bb32253SAndrey A. Chernov */ 3771bb32253SAndrey A. Chernov dkp = ep->dkp; 3781bb32253SAndrey A. Chernov ep->dkp = kp; 379ac3cd520SBrian Feldman debugproc(kp) = dkp; 3809b50d902SRodney W. Grimes } 3819b50d902SRodney W. Grimes } 3829b50d902SRodney W. Grimes } 3839b50d902SRodney W. Grimes if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 && 3849b50d902SRodney W. Grimes ioctl(STDERR_FILENO, TIOCGWINSZ, &ws) == -1 && 3859b50d902SRodney W. Grimes ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1) || ws.ws_col == 0) 3869b50d902SRodney W. Grimes ttywidth = 79; 3879b50d902SRodney W. Grimes else 3889b50d902SRodney W. Grimes ttywidth = ws.ws_col - 1; 3899b50d902SRodney W. Grimes argwidth = ttywidth - WUSED; 3909b50d902SRodney W. Grimes if (argwidth < 4) 3919b50d902SRodney W. Grimes argwidth = 8; 39230762584SKristof Provost /* Don't truncate if we're outputting json or XML. */ 39330762584SKristof Provost if (xo_get_style(NULL) != XO_STYLE_TEXT) 39430762584SKristof Provost argwidth = ARG_MAX; 3959b50d902SRodney W. Grimes for (ep = ehead; ep != NULL; ep = ep->next) { 3969b50d902SRodney W. Grimes if (ep->kp == NULL) { 3978b56c58bSMark Murray ep->args = strdup("-"); 3989b50d902SRodney W. Grimes continue; 3999b50d902SRodney W. Grimes } 4009b50d902SRodney W. Grimes ep->args = fmt_argv(kvm_getargv(kd, ep->kp, argwidth), 4019f0b6e5eSJohn Baldwin ep->kp->ki_comm, NULL, MAXCOMLEN); 4029b50d902SRodney W. Grimes if (ep->args == NULL) 4039b50d902SRodney W. Grimes err(1, NULL); 4049b50d902SRodney W. Grimes } 4059b50d902SRodney W. Grimes /* sort by idle time */ 4069b50d902SRodney W. Grimes if (sortidle && ehead != NULL) { 407ac3cd520SBrian Feldman struct entry *from, *save; 4089b50d902SRodney W. Grimes 409ac3cd520SBrian Feldman from = ehead; 4109b50d902SRodney W. Grimes ehead = NULL; 4119b50d902SRodney W. Grimes while (from != NULL) { 4129b50d902SRodney W. Grimes for (nextp = &ehead; 4139b50d902SRodney W. Grimes (*nextp) && from->idle >= (*nextp)->idle; 4149b50d902SRodney W. Grimes nextp = &(*nextp)->next) 4159b50d902SRodney W. Grimes continue; 4169b50d902SRodney W. Grimes save = from; 4179b50d902SRodney W. Grimes from = from->next; 4189b50d902SRodney W. Grimes save->next = *nextp; 4199b50d902SRodney W. Grimes *nextp = save; 4209b50d902SRodney W. Grimes } 4219b50d902SRodney W. Grimes } 4229b50d902SRodney W. Grimes 42376c0abf1SMarcel Moolenaar xo_open_container("user-table"); 42476c0abf1SMarcel Moolenaar xo_open_list("user-entry"); 42576c0abf1SMarcel Moolenaar 4269b50d902SRodney W. Grimes for (ep = ehead; ep != NULL; ep = ep->next) { 427c76b41b7SPeter Wemm time_t t; 4289587fac0SAndrey A. Chernov 42976c0abf1SMarcel Moolenaar xo_open_instance("user-entry"); 43076c0abf1SMarcel Moolenaar 4311bb32253SAndrey A. Chernov if (dflag) { 43276c0abf1SMarcel Moolenaar xo_open_container("process-table"); 43376c0abf1SMarcel Moolenaar xo_open_list("process-entry"); 43476c0abf1SMarcel Moolenaar 435ac3cd520SBrian Feldman for (dkp = ep->dkp; dkp != NULL; dkp = debugproc(dkp)) { 4368b56c58bSMark Murray const char *ptr; 437ac3cd520SBrian Feldman 4380cadb9caSBrian Somers ptr = fmt_argv(kvm_getargv(kd, dkp, argwidth), 4399f0b6e5eSJohn Baldwin dkp->ki_comm, NULL, MAXCOMLEN); 4400cadb9caSBrian Somers if (ptr == NULL) 4410cadb9caSBrian Somers ptr = "-"; 44276c0abf1SMarcel Moolenaar xo_open_instance("process-entry"); 4432d3725d6SYuri Pankov xo_emit("\t\t{:process-id/%-9d/%d} " 4442d3725d6SYuri Pankov "{:command/%hs}\n", dkp->ki_pid, ptr); 44576c0abf1SMarcel Moolenaar xo_close_instance("process-entry"); 4461bb32253SAndrey A. Chernov } 44776c0abf1SMarcel Moolenaar xo_close_list("process-entry"); 44876c0abf1SMarcel Moolenaar xo_close_container("process-table"); 4491bb32253SAndrey A. Chernov } 45076c0abf1SMarcel Moolenaar xo_emit("{:user/%-*.*s/%@**@s} {:tty/%-*.*s/%@**@s} ", 451dc65bcacSEd Schouten W_DISPUSERSIZE, W_DISPUSERSIZE, ep->utmp.ut_user, 452dc65bcacSEd Schouten W_DISPLINESIZE, W_DISPLINESIZE, 4531131779fSEd Schouten *ep->utmp.ut_line ? 4541131779fSEd Schouten (strncmp(ep->utmp.ut_line, "tty", 3) && 455255318a8SAndrey A. Chernov strncmp(ep->utmp.ut_line, "cua", 3) ? 45676c0abf1SMarcel Moolenaar ep->utmp.ut_line : ep->utmp.ut_line + 3) : "-"); 45776c0abf1SMarcel Moolenaar 458ae335351SMike Karels if (ep->save_from) 459ae335351SMike Karels xo_attr("address", "%s", ep->save_from); 46076c0abf1SMarcel Moolenaar xo_emit("{:from/%-*.*s/%@**@s} ", 461*04f6b9cbSDag-Erling Smørgrav (int)fromwidth, (int)fromwidth, ep->from); 462dc65bcacSEd Schouten t = ep->utmp.ut_tv.tv_sec; 46334903a55SHajimu UMEMOTO longattime = pr_attime(&t, &now); 46470498d5aSDaniel O'Callaghan longidle = pr_idle(ep->idle); 4652d3725d6SYuri Pankov xo_emit("{:command/%.*hs/%@*@hs}\n", 466*04f6b9cbSDag-Erling Smørgrav (int)argwidth - longidle - longattime, 46734903a55SHajimu UMEMOTO ep->args); 46876c0abf1SMarcel Moolenaar 46976c0abf1SMarcel Moolenaar xo_close_instance("user-entry"); 4709b50d902SRodney W. Grimes } 47176c0abf1SMarcel Moolenaar 47276c0abf1SMarcel Moolenaar xo_close_list("user-entry"); 47376c0abf1SMarcel Moolenaar xo_close_container("user-table"); 47476c0abf1SMarcel Moolenaar xo_close_container("uptime-information"); 47576c0abf1SMarcel Moolenaar xo_finish(); 47676c0abf1SMarcel Moolenaar 477c59c7f97SPoul-Henning Kamp (void)kvm_close(kd); 4789b50d902SRodney W. Grimes exit(0); 4799b50d902SRodney W. Grimes } 4809b50d902SRodney W. Grimes 4819b50d902SRodney W. Grimes static void 482e8e649ccSJuli Mallett pr_header(time_t *nowp, int nusers) 4839b50d902SRodney W. Grimes { 4849b50d902SRodney W. Grimes double avenrun[3]; 485716ced0bSDavid E. O'Brien time_t uptime; 486a21cbcb8SAndre Oppermann struct timespec tp; 4879d8c91b7SAndrey A. Chernov int days, hrs, i, mins, secs; 4889b50d902SRodney W. Grimes char buf[256]; 489630a02daSAllan Jude struct sbuf *upbuf; 4909b50d902SRodney W. Grimes 491630a02daSAllan Jude upbuf = sbuf_new_auto(); 4929b50d902SRodney W. Grimes /* 4939b50d902SRodney W. Grimes * Print time of day. 4949b50d902SRodney W. Grimes */ 49569fe77ceSPhilippe Charnier if (strftime(buf, sizeof(buf), 49669fe77ceSPhilippe Charnier use_ampm ? "%l:%M%p" : "%k:%M", localtime(nowp)) != 0) 49776c0abf1SMarcel Moolenaar xo_emit("{:time-of-day/%s} ", buf); 4989b50d902SRodney W. Grimes /* 4999b50d902SRodney W. Grimes * Print how long system has been up. 5009b50d902SRodney W. Grimes */ 501b6eec535SEd Maste if (clock_gettime(CLOCK_UPTIME, &tp) != -1) { 502a21cbcb8SAndre Oppermann uptime = tp.tv_sec; 503716ced0bSDavid E. O'Brien if (uptime > 60) 504716ced0bSDavid E. O'Brien uptime += 30; 505716ced0bSDavid E. O'Brien days = uptime / 86400; 506716ced0bSDavid E. O'Brien uptime %= 86400; 507716ced0bSDavid E. O'Brien hrs = uptime / 3600; 508716ced0bSDavid E. O'Brien uptime %= 3600; 509716ced0bSDavid E. O'Brien mins = uptime / 60; 510716ced0bSDavid E. O'Brien secs = uptime % 60; 51176c0abf1SMarcel Moolenaar xo_emit(" up"); 512630a02daSAllan Jude xo_emit("{e:uptime/%lu}", (unsigned long)tp.tv_sec); 513*04f6b9cbSDag-Erling Smørgrav xo_emit("{e:days/%d}{e:hours/%d}{e:minutes/%d}{e:seconds/%d}", 514*04f6b9cbSDag-Erling Smørgrav days, hrs, mins, secs); 515630a02daSAllan Jude 5169b50d902SRodney W. Grimes if (days > 0) 517630a02daSAllan Jude sbuf_printf(upbuf, " %d day%s,", 51876c0abf1SMarcel Moolenaar days, days > 1 ? "s" : ""); 5199b50d902SRodney W. Grimes if (hrs > 0 && mins > 0) 520630a02daSAllan Jude sbuf_printf(upbuf, " %2d:%02d,", hrs, mins); 5219d8c91b7SAndrey A. Chernov else if (hrs > 0) 522630a02daSAllan Jude sbuf_printf(upbuf, " %d hr%s,", 52376c0abf1SMarcel Moolenaar hrs, hrs > 1 ? "s" : ""); 5249d8c91b7SAndrey A. Chernov else if (mins > 0) 525630a02daSAllan Jude sbuf_printf(upbuf, " %d min%s,", 52676c0abf1SMarcel Moolenaar mins, mins > 1 ? "s" : ""); 5279d8c91b7SAndrey A. Chernov else 528630a02daSAllan Jude sbuf_printf(upbuf, " %d sec%s,", 52976c0abf1SMarcel Moolenaar secs, secs > 1 ? "s" : ""); 530630a02daSAllan Jude if (sbuf_finish(upbuf) != 0) 531630a02daSAllan Jude xo_err(1, "Could not generate output"); 532630a02daSAllan Jude xo_emit("{:uptime-human/%s}", sbuf_data(upbuf)); 533630a02daSAllan Jude sbuf_delete(upbuf); 5349b50d902SRodney W. Grimes } 5359b50d902SRodney W. Grimes 5369b50d902SRodney W. Grimes /* Print number of users logged in to system */ 537ba86ca7cSAlexander Motin xo_emit(" {:users/%d} {Np:user,users}", nusers); 5389b50d902SRodney W. Grimes 5399b50d902SRodney W. Grimes /* 5409b50d902SRodney W. Grimes * Print 1, 5, and 15 minute load averages. 5419b50d902SRodney W. Grimes */ 542807bd0d9SMarcelo Araujo if (getloadavg(avenrun, nitems(avenrun)) == -1) 54376c0abf1SMarcel Moolenaar xo_emit(", no load average information available\n"); 5449b50d902SRodney W. Grimes else { 54576c0abf1SMarcel Moolenaar static const char *format[] = { 54676c0abf1SMarcel Moolenaar " {:load-average-1/%.2f}", 54776c0abf1SMarcel Moolenaar " {:load-average-5/%.2f}", 54876c0abf1SMarcel Moolenaar " {:load-average-15/%.2f}", 54976c0abf1SMarcel Moolenaar }; 55076c0abf1SMarcel Moolenaar xo_emit(", load averages:"); 551807bd0d9SMarcelo Araujo for (i = 0; i < (int)(nitems(avenrun)); i++) { 5522742fc8eSAndrey A. Chernov if (use_comma && i > 0) 55376c0abf1SMarcel Moolenaar xo_emit(","); 55476c0abf1SMarcel Moolenaar xo_emit(format[i], avenrun[i]); 5552742fc8eSAndrey A. Chernov } 55676c0abf1SMarcel Moolenaar xo_emit("\n"); 5579b50d902SRodney W. Grimes } 5589b50d902SRodney W. Grimes } 5599b50d902SRodney W. Grimes 5609b50d902SRodney W. Grimes static struct stat * 561dc65bcacSEd Schouten ttystat(char *line) 5629b50d902SRodney W. Grimes { 5639b50d902SRodney W. Grimes static struct stat sb; 5649b50d902SRodney W. Grimes char ttybuf[MAXPATHLEN]; 5659b50d902SRodney W. Grimes 566dc65bcacSEd Schouten (void)snprintf(ttybuf, sizeof(ttybuf), "%s%s", _PATH_DEV, line); 567*04f6b9cbSDag-Erling Smørgrav if (stat(ttybuf, &sb) == 0 && S_ISCHR(sb.st_mode)) 5689b50d902SRodney W. Grimes return (&sb); 569d0d0355eSSean Chittenden return (NULL); 5709b50d902SRodney W. Grimes } 5719b50d902SRodney W. Grimes 5729b50d902SRodney W. Grimes static void 573e8e649ccSJuli Mallett usage(int wcmd) 5749b50d902SRodney W. Grimes { 5759b50d902SRodney W. Grimes if (wcmd) 57676c0abf1SMarcel Moolenaar xo_error("usage: w [-dhin] [-M core] [-N system] [user ...]\n"); 5779b50d902SRodney W. Grimes else 57876c0abf1SMarcel Moolenaar xo_error("usage: uptime\n"); 57910b92369SMark Johnston xo_finish(); 5809b50d902SRodney W. Grimes exit(1); 5819b50d902SRodney W. Grimes } 582