1 /* $NetBSD: w.c,v 1.25 1997/07/07 20:30:08 phil 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. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #ifndef lint 37 static char copyright[] = 38 "@(#) Copyright (c) 1980, 1991, 1993, 1994\n\ 39 The Regents of the University of California. All rights reserved.\n"; 40 #endif /* not lint */ 41 42 #ifndef lint 43 #if 0 44 static char sccsid[] = "@(#)w.c 8.6 (Berkeley) 6/30/94"; 45 #else 46 static char rcsid[] = "$NetBSD: w.c,v 1.25 1997/07/07 20:30:08 phil Exp $"; 47 #endif 48 #endif /* not lint */ 49 50 /* 51 * w - print system status (who and what) 52 * 53 * This program is similar to the systat command on Tenex/Tops 10/20 54 * 55 */ 56 #include <sys/param.h> 57 #include <sys/time.h> 58 #include <sys/stat.h> 59 #include <sys/sysctl.h> 60 #include <sys/proc.h> 61 #include <sys/user.h> 62 #include <sys/ioctl.h> 63 #include <sys/socket.h> 64 #include <sys/tty.h> 65 66 #include <machine/cpu.h> 67 #include <netinet/in.h> 68 #include <arpa/inet.h> 69 70 #include <ctype.h> 71 #include <err.h> 72 #include <errno.h> 73 #include <fcntl.h> 74 #include <kvm.h> 75 #include <limits.h> 76 #include <netdb.h> 77 #include <nlist.h> 78 #include <paths.h> 79 #include <stdio.h> 80 #include <stdlib.h> 81 #include <string.h> 82 #include <tzfile.h> 83 #include <unistd.h> 84 #include <utmp.h> 85 #include <vis.h> 86 87 #include "extern.h" 88 89 struct timeval boottime; 90 struct utmp utmp; 91 struct winsize ws; 92 kvm_t *kd; 93 time_t now; /* the current time of day */ 94 time_t uptime; /* time of last reboot & elapsed time since */ 95 int ttywidth; /* width of tty */ 96 int argwidth; /* width of tty */ 97 int header = 1; /* true if -h flag: don't print heading */ 98 int nflag; /* true if -n flag: don't convert addrs */ 99 int sortidle; /* sort bu idle time */ 100 char *sel_user; /* login of particular user selected */ 101 char domain[MAXHOSTNAMELEN]; 102 103 /* 104 * One of these per active utmp entry. 105 */ 106 struct entry { 107 struct entry *next; 108 struct utmp utmp; 109 dev_t tdev; /* dev_t of terminal */ 110 time_t idle; /* idle time of terminal in seconds */ 111 struct kinfo_proc *kp; /* `most interesting' proc */ 112 } *ep, *ehead = NULL, **nextp = &ehead; 113 114 static void pr_args __P((struct kinfo_proc *)); 115 static void pr_header __P((time_t *, int)); 116 static struct stat 117 *ttystat __P((char *)); 118 static void usage __P((int)); 119 120 int 121 main(argc, argv) 122 int argc; 123 char **argv; 124 { 125 extern char *__progname; 126 struct kinfo_proc *kp; 127 struct hostent *hp; 128 struct stat *stp; 129 FILE *ut; 130 struct in_addr l; 131 int ch, i, nentries, nusers, wcmd; 132 char *memf, *nlistf, *p, *x; 133 char buf[MAXHOSTNAMELEN], errbuf[_POSIX2_LINE_MAX]; 134 135 /* Are we w(1) or uptime(1)? */ 136 p = __progname; 137 if (*p == '-') 138 p++; 139 if (*p == 'u') { 140 wcmd = 0; 141 p = ""; 142 } else { 143 wcmd = 1; 144 p = "hiflM:N:nsuw"; 145 } 146 147 memf = nlistf = NULL; 148 while ((ch = getopt(argc, argv, p)) != EOF) 149 switch (ch) { 150 case 'h': 151 header = 0; 152 break; 153 case 'i': 154 sortidle = 1; 155 break; 156 case 'M': 157 header = 0; 158 memf = optarg; 159 break; 160 case 'N': 161 nlistf = optarg; 162 break; 163 case 'n': 164 nflag = 1; 165 break; 166 case 'f': case 'l': case 's': case 'u': case 'w': 167 warnx("[-flsuw] no longer supported"); 168 /* FALLTHROUGH */ 169 case '?': 170 default: 171 usage(wcmd); 172 } 173 argc -= optind; 174 argv += optind; 175 176 /* 177 * Discard setgid privelidges if not the running kernel so that 178 * bad guys can't print interesting stuff from kernel memory. 179 */ 180 if (nlistf != NULL || memf != NULL) 181 setgid(getgid()); 182 183 if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf)) == NULL) 184 errx(1, "%s", errbuf); 185 186 (void)time(&now); 187 if ((ut = fopen(_PATH_UTMP, "r")) == NULL) 188 err(1, "%s", _PATH_UTMP); 189 190 if (*argv) 191 sel_user = *argv; 192 193 for (nusers = 0; fread(&utmp, sizeof(utmp), 1, ut);) { 194 if (utmp.ut_name[0] == '\0') 195 continue; 196 ++nusers; 197 if (wcmd == 0 || (sel_user && 198 strncmp(utmp.ut_name, sel_user, UT_NAMESIZE) != 0)) 199 continue; 200 if ((ep = calloc(1, sizeof(struct entry))) == NULL) 201 err(1, NULL); 202 *nextp = ep; 203 nextp = &(ep->next); 204 memmove(&(ep->utmp), &utmp, sizeof(struct utmp)); 205 if (!(stp = ttystat(ep->utmp.ut_line))) 206 continue; 207 ep->tdev = stp->st_rdev; 208 #ifdef CPU_CONSDEV 209 /* 210 * If this is the console device, attempt to ascertain 211 * the true console device dev_t. 212 */ 213 if (ep->tdev == 0) { 214 int mib[2]; 215 size_t size; 216 217 mib[0] = CTL_MACHDEP; 218 mib[1] = CPU_CONSDEV; 219 size = sizeof(dev_t); 220 (void) sysctl(mib, 2, &ep->tdev, &size, NULL, 0); 221 } 222 #endif 223 if ((ep->idle = now - stp->st_atime) < 0) 224 ep->idle = 0; 225 } 226 (void)fclose(ut); 227 228 if (header || wcmd == 0) { 229 pr_header(&now, nusers); 230 if (wcmd == 0) 231 exit (0); 232 } 233 234 #define HEADER "USER TTY FROM LOGIN@ IDLE WHAT\n" 235 #define WUSED (sizeof (HEADER) - sizeof ("WHAT\n")) 236 (void)printf(HEADER); 237 238 if ((kp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nentries)) == NULL) 239 errx(1, "%s", kvm_geterr(kd)); 240 for (i = 0; i < nentries; i++, kp++) { 241 struct proc *p = &kp->kp_proc; 242 struct eproc *e; 243 244 if (p->p_stat == SIDL || p->p_stat == SZOMB) 245 continue; 246 e = &kp->kp_eproc; 247 for (ep = ehead; ep != NULL; ep = ep->next) { 248 if (ep->tdev == e->e_tdev && e->e_pgid == e->e_tpgid) { 249 /* 250 * Proc is in foreground of this terminal 251 */ 252 if (proc_compare(&ep->kp->kp_proc, p)) 253 ep->kp = kp; 254 break; 255 } 256 } 257 } 258 if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 && 259 ioctl(STDERR_FILENO, TIOCGWINSZ, &ws) == -1 && 260 ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1) || ws.ws_col == 0) 261 ttywidth = 79; 262 else 263 ttywidth = ws.ws_col - 1; 264 argwidth = ttywidth - WUSED; 265 if (argwidth < 4) 266 argwidth = 8; 267 /* sort by idle time */ 268 if (sortidle && ehead != NULL) { 269 struct entry *from = ehead, *save; 270 271 ehead = NULL; 272 while (from != NULL) { 273 for (nextp = &ehead; 274 (*nextp) && from->idle >= (*nextp)->idle; 275 nextp = &(*nextp)->next) 276 continue; 277 save = from; 278 from = from->next; 279 save->next = *nextp; 280 *nextp = save; 281 } 282 } 283 284 if (!nflag) 285 if (gethostname(domain, sizeof(domain) - 1) < 0 || 286 (p = strchr(domain, '.')) == 0) 287 domain[0] = '\0'; 288 else { 289 domain[sizeof(domain) - 1] = '\0'; 290 memmove(domain, p, strlen(p) + 1); 291 } 292 293 for (ep = ehead; ep != NULL; ep = ep->next) { 294 p = *ep->utmp.ut_host ? ep->utmp.ut_host : "-"; 295 for (x = p; x < p + UT_HOSTSIZE; x++) 296 if (*x == '\0' || *x == ':') 297 break; 298 if (x == p + UT_HOSTSIZE || *x != ':') 299 x = NULL; 300 else 301 *x++ = '\0'; 302 303 if (!nflag && inet_aton(p, &l) && 304 (hp = gethostbyaddr((char *)&l, sizeof(l), AF_INET))) { 305 if (domain[0] != '\0') { 306 p = hp->h_name; 307 p += strlen(hp->h_name); 308 p -= strlen(domain); 309 if (p > hp->h_name && strcmp(p, domain) == 0) 310 *p = '\0'; 311 } 312 p = hp->h_name; 313 } 314 if (x) { 315 (void)snprintf(buf, sizeof(buf), "%s:%.*s", p, 316 ep->utmp.ut_host + UT_HOSTSIZE - x, x); 317 p = buf; 318 } 319 (void)printf("%-*.*s %-2.2s %-*.*s ", 320 UT_NAMESIZE, UT_NAMESIZE, ep->utmp.ut_name, 321 strncmp(ep->utmp.ut_line, "tty", 3) ? 322 ep->utmp.ut_line : ep->utmp.ut_line + 3, 323 UT_HOSTSIZE, UT_HOSTSIZE, *p ? p : "-"); 324 pr_attime(&ep->utmp.ut_time, &now); 325 pr_idle(ep->idle); 326 pr_args(ep->kp); 327 printf("\n"); 328 } 329 exit(0); 330 } 331 332 static void 333 pr_args(kp) 334 struct kinfo_proc *kp; 335 { 336 char **argv; 337 int left; 338 339 if (kp == 0) 340 goto nothing; 341 left = argwidth; 342 argv = kvm_getargv(kd, kp, argwidth); 343 if (argv == 0) 344 goto nothing; 345 while (*argv) { 346 fmt_puts(*argv, &left); 347 argv++; 348 fmt_putc(' ', &left); 349 } 350 return; 351 nothing: 352 putchar('-'); 353 } 354 355 static void 356 pr_header(nowp, nusers) 357 time_t *nowp; 358 int nusers; 359 { 360 double avenrun[3]; 361 time_t uptime; 362 int days, hrs, i, mins; 363 int mib[2]; 364 size_t size; 365 char buf[256]; 366 367 /* 368 * Print time of day. 369 * 370 * SCCS forces the string manipulation below, as it replaces 371 * %, M, and % in a character string with the file name. 372 */ 373 (void)strftime(buf, sizeof(buf), 374 __CONCAT("%l:%","M%p"), localtime(nowp)); 375 buf[sizeof(buf) - 1] = '\0'; 376 (void)printf("%s ", buf); 377 378 /* 379 * Print how long system has been up. 380 * (Found by looking getting "boottime" from the kernel) 381 */ 382 mib[0] = CTL_KERN; 383 mib[1] = KERN_BOOTTIME; 384 size = sizeof(boottime); 385 if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 && 386 boottime.tv_sec != 0) { 387 uptime = now - boottime.tv_sec; 388 uptime += 30; 389 if (uptime > SECSPERMIN) { 390 days = uptime / SECSPERDAY; 391 uptime %= SECSPERDAY; 392 hrs = uptime / SECSPERHOUR; 393 uptime %= SECSPERHOUR; 394 mins = uptime / SECSPERMIN; 395 (void)printf(" up"); 396 if (days > 0) 397 (void)printf(" %d day%s,", days, 398 days > 1 ? "s" : ""); 399 if (hrs > 0 && mins > 0) 400 (void)printf(" %2d:%02d,", hrs, mins); 401 else { 402 if (hrs > 0) 403 (void)printf(" %d hr%s,", 404 hrs, hrs > 1 ? "s" : ""); 405 if (mins > 0) 406 (void)printf(" %d min%s,", 407 mins, mins > 1 ? "s" : ""); 408 } 409 } 410 } 411 412 /* Print number of users logged in to system */ 413 (void)printf(" %d user%s", nusers, nusers != 1 ? "s" : ""); 414 415 /* 416 * Print 1, 5, and 15 minute load averages. 417 */ 418 if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) == -1) 419 (void)printf(", no load average information available\n"); 420 else { 421 (void)printf(", load averages:"); 422 for (i = 0; i < (sizeof(avenrun) / sizeof(avenrun[0])); i++) { 423 if (i > 0) 424 (void)printf(","); 425 (void)printf(" %.2f", avenrun[i]); 426 } 427 (void)printf("\n"); 428 } 429 } 430 431 static struct stat * 432 ttystat(line) 433 char *line; 434 { 435 static struct stat sb; 436 char ttybuf[MAXPATHLEN]; 437 438 (void)snprintf(ttybuf, sizeof(ttybuf), "%s/%s", _PATH_DEV, line); 439 if (stat(ttybuf, &sb)) 440 return (NULL); 441 return (&sb); 442 } 443 444 static void 445 usage(wcmd) 446 int wcmd; 447 { 448 if (wcmd) 449 (void)fprintf(stderr, 450 "usage: w: [-hin] [-M core] [-N system] [user]\n"); 451 else 452 (void)fprintf(stderr, "uptime\n"); 453 exit (1); 454 } 455