Annotation of src/usr.bin/w/w.c, Revision 1.45
1.45 ! jaredy 1: /* $OpenBSD: w.c,v 1.44 2005/04/11 07:04:47 deraadt Exp $ */
1.4 deraadt 2:
1.1 deraadt 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.
1.39 millert 15: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 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: #ifndef lint
33: static char copyright[] =
34: "@(#) Copyright (c) 1980, 1991, 1993, 1994\n\
35: The Regents of the University of California. All rights reserved.\n";
36: #endif /* not lint */
37:
38: #ifndef lint
1.23 millert 39: #if 0
1.1 deraadt 40: static char sccsid[] = "@(#)w.c 8.4 (Berkeley) 4/16/94";
1.23 millert 41: #else
1.45 ! jaredy 42: static char *rcsid = "$OpenBSD: w.c,v 1.44 2005/04/11 07:04:47 deraadt Exp $";
1.23 millert 43: #endif
1.1 deraadt 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/time.h>
54: #include <sys/stat.h>
55: #include <sys/sysctl.h>
56: #include <sys/proc.h>
57: #include <sys/user.h>
58: #include <sys/ioctl.h>
59: #include <sys/socket.h>
60: #include <sys/tty.h>
61:
62: #include <machine/cpu.h>
63: #include <netinet/in.h>
64: #include <arpa/inet.h>
65:
66: #include <ctype.h>
67: #include <err.h>
68: #include <errno.h>
69: #include <fcntl.h>
70: #include <kvm.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 <tzfile.h>
78: #include <unistd.h>
1.5 deraadt 79: #include <limits.h>
1.1 deraadt 80: #include <utmp.h>
81: #include <vis.h>
82:
83: #include "extern.h"
84:
85: struct timeval boottime;
86: struct utmp utmp;
87: struct winsize ws;
88: kvm_t *kd;
89: time_t now; /* the current time of day */
90: int ttywidth; /* width of tty */
91: int argwidth; /* width of tty */
92: int header = 1; /* true if -h flag: don't print heading */
1.6 deraadt 93: int nflag = 1; /* true if -n flag: don't convert addrs */
1.1 deraadt 94: int sortidle; /* sort bu idle time */
95: char *sel_user; /* login of particular user selected */
96: char domain[MAXHOSTNAMELEN];
97:
1.32 deraadt 98: #define NAME_WIDTH 8
99: #define HOST_WIDTH 16
100:
1.1 deraadt 101: /*
102: * One of these per active utmp entry.
103: */
104: struct entry {
105: struct entry *next;
106: struct utmp utmp;
1.41 millert 107: dev_t tdev; /* dev_t of terminal */
108: time_t idle; /* idle time of terminal in seconds */
109: struct kinfo_proc2 *kp; /* `most interesting' proc */
1.1 deraadt 110: } *ep, *ehead = NULL, **nextp = &ehead;
111:
1.41 millert 112: static void pr_args(struct kinfo_proc2 *);
1.35 millert 113: static void pr_header(time_t *, int);
1.1 deraadt 114: static struct stat
1.35 millert 115: *ttystat(char *);
116: static void usage(int);
1.1 deraadt 117:
118: int
1.38 deraadt 119: main(int argc, char *argv[])
1.1 deraadt 120: {
121: extern char *__progname;
1.41 millert 122: struct kinfo_proc2 *kp;
1.1 deraadt 123: struct hostent *hp;
124: struct stat *stp;
125: FILE *ut;
1.27 deraadt 126: struct in_addr addr;
1.1 deraadt 127: int ch, i, nentries, nusers, wcmd;
128: char *memf, *nlistf, *p, *x;
1.5 deraadt 129: char buf[MAXHOSTNAMELEN], errbuf[_POSIX2_LINE_MAX];
1.1 deraadt 130:
131: /* Are we w(1) or uptime(1)? */
132: p = __progname;
133: if (*p == '-')
134: p++;
1.19 mickey 135: if (p[0] == 'w' && p[1] == '\0') {
136: wcmd = 1;
137: p = "hiflM:N:asuw";
138: } else if (!strcmp(p, "uptime")) {
1.1 deraadt 139: wcmd = 0;
1.45 ! jaredy 140: p = "";
1.19 mickey 141: } else
1.20 kstailey 142: errx(1,
143: "this program should be invoked only as \"w\" or \"uptime\"");
1.1 deraadt 144:
145: memf = nlistf = NULL;
1.12 millert 146: while ((ch = getopt(argc, argv, p)) != -1)
1.1 deraadt 147: switch (ch) {
148: case 'h':
149: header = 0;
150: break;
151: case 'i':
152: sortidle = 1;
153: break;
154: case 'M':
155: header = 0;
156: memf = optarg;
157: break;
158: case 'N':
159: nlistf = optarg;
160: break;
1.6 deraadt 161: case 'a':
162: nflag = 0;
1.1 deraadt 163: break;
164: case 'f': case 'l': case 's': case 'u': case 'w':
165: warnx("[-flsuw] no longer supported");
166: /* FALLTHROUGH */
167: case '?':
168: default:
169: usage(wcmd);
170: }
171: argc -= optind;
172: argv += optind;
173:
1.37 angelos 174: if (nlistf == NULL && memf == NULL) {
1.38 deraadt 175: if ((kd = kvm_openfiles(nlistf, memf, NULL, KVM_NO_FILES,
176: errbuf)) == NULL)
1.37 angelos 177: errx(1, "%s", errbuf);
178: } else {
179: if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf)) == NULL)
180: errx(1, "%s", errbuf);
1.11 tholo 181: }
1.1 deraadt 182:
183: (void)time(&now);
184: if ((ut = fopen(_PATH_UTMP, "r")) == NULL)
185: err(1, "%s", _PATH_UTMP);
186:
187: if (*argv)
188: sel_user = *argv;
189:
190: for (nusers = 0; fread(&utmp, sizeof(utmp), 1, ut);) {
191: if (utmp.ut_name[0] == '\0')
192: continue;
193: ++nusers;
194: if (wcmd == 0 || (sel_user &&
195: strncmp(utmp.ut_name, sel_user, UT_NAMESIZE) != 0))
196: continue;
1.33 smart 197: if ((ep = calloc(1, sizeof(*ep))) == NULL)
1.1 deraadt 198: err(1, NULL);
199: *nextp = ep;
200: nextp = &(ep->next);
1.33 smart 201: memcpy(&(ep->utmp), &utmp, sizeof(utmp));
1.1 deraadt 202: if (!(stp = ttystat(ep->utmp.ut_line)))
203: continue;
204: ep->tdev = stp->st_rdev;
205: #ifdef CPU_CONSDEV
206: /*
207: * If this is the console device, attempt to ascertain
208: * the true console device dev_t.
209: */
210: if (ep->tdev == 0) {
211: int mib[2];
212: size_t size;
213:
214: mib[0] = CTL_MACHDEP;
215: mib[1] = CPU_CONSDEV;
216: size = sizeof(dev_t);
217: (void) sysctl(mib, 2, &ep->tdev, &size, NULL, 0);
218: }
219: #endif
220: if ((ep->idle = now - stp->st_atime) < 0)
221: ep->idle = 0;
222: }
223: (void)fclose(ut);
224:
225: if (header || wcmd == 0) {
226: pr_header(&now, nusers);
227: if (wcmd == 0)
228: exit (0);
229: }
230:
1.30 deraadt 231: #define HEADER "USER TTY FROM LOGIN@ IDLE WHAT"
1.33 smart 232: #define WUSED (sizeof(HEADER) - sizeof("WHAT"))
1.30 deraadt 233: (void)puts(HEADER);
1.1 deraadt 234:
1.41 millert 235: kp = kvm_getproc2(kd, KERN_PROC_ALL, 0, sizeof(*kp), &nentries);
236: if (kp == NULL)
1.2 deraadt 237: errx(1, "%s", kvm_geterr(kd));
1.1 deraadt 238: for (i = 0; i < nentries; i++, kp++) {
1.41 millert 239: if (kp->p_stat == SIDL || kp->p_stat == SZOMB)
1.1 deraadt 240: continue;
241: for (ep = ehead; ep != NULL; ep = ep->next) {
1.7 downsj 242: /* ftp is a special case. */
243: if (strncmp(ep->utmp.ut_line, "ftp", 3) == 0) {
1.26 millert 244: char pidstr[UT_LINESIZE-2];
245: pid_t fp;
246:
247: (void)strncpy(pidstr, &ep->utmp.ut_line[3],
248: sizeof(pidstr) - 1);
249: pidstr[sizeof(pidstr) - 1] = '\0';
250: fp = (pid_t)strtol(pidstr, NULL, 10);
1.41 millert 251: if (kp->p_pid == fp) {
1.7 downsj 252: ep->kp = kp;
253: break;
254: }
1.41 millert 255: } else if (ep->tdev == kp->p_tdev &&
256: kp->p__pgid == kp->p_tpgid) {
1.1 deraadt 257: /*
258: * Proc is in foreground of this terminal
259: */
1.41 millert 260: if (proc_compare(ep->kp, kp))
1.1 deraadt 261: ep->kp = kp;
262: break;
263: }
264: }
265: }
266: if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 &&
1.44 deraadt 267: ioctl(STDERR_FILENO, TIOCGWINSZ, &ws) == -1 &&
268: ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1) || ws.ws_col == 0)
269: ttywidth = 79;
270: else
271: ttywidth = ws.ws_col - 1;
1.1 deraadt 272: argwidth = ttywidth - WUSED;
273: if (argwidth < 4)
274: argwidth = 8;
275: /* sort by idle time */
276: if (sortidle && ehead != NULL) {
277: struct entry *from = ehead, *save;
1.44 deraadt 278:
1.1 deraadt 279: ehead = NULL;
280: while (from != NULL) {
281: for (nextp = &ehead;
282: (*nextp) && from->idle >= (*nextp)->idle;
283: nextp = &(*nextp)->next)
284: continue;
285: save = from;
286: from = from->next;
287: save->next = *nextp;
288: *nextp = save;
289: }
290: }
1.44 deraadt 291:
1.34 deraadt 292: if (!nflag) {
1.36 mpech 293: if (gethostname(domain, sizeof(domain)) < 0 ||
1.1 deraadt 294: (p = strchr(domain, '.')) == 0)
295: domain[0] = '\0';
296: else {
297: domain[sizeof(domain) - 1] = '\0';
298: memmove(domain, p, strlen(p) + 1);
299: }
1.34 deraadt 300: }
1.1 deraadt 301:
302: for (ep = ehead; ep != NULL; ep = ep->next) {
303: p = *ep->utmp.ut_host ? ep->utmp.ut_host : "-";
1.16 millert 304: for (x = NULL, i = 0; p[i] != '\0' && i < UT_HOSTSIZE; i++)
305: if (p[i] == ':') {
306: x = &p[i];
307: *x++ = '\0';
308: break;
309: }
1.27 deraadt 310: if (!nflag && inet_aton(p, &addr) &&
311: (hp = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET))) {
1.1 deraadt 312: if (domain[0] != '\0') {
313: p = hp->h_name;
314: p += strlen(hp->h_name);
315: p -= strlen(domain);
1.31 deraadt 316: if (p > hp->h_name &&
317: strcasecmp(p, domain) == 0)
1.1 deraadt 318: *p = '\0';
319: }
320: p = hp->h_name;
321: }
322: if (x) {
323: (void)snprintf(buf, sizeof(buf), "%s:%.*s", p,
1.34 deraadt 324: (int)(ep->utmp.ut_host + UT_HOSTSIZE - x), x);
1.1 deraadt 325: p = buf;
326: }
1.22 deraadt 327: (void)printf("%-*.*s %-2.2s %-*.*s ",
1.32 deraadt 328: NAME_WIDTH, UT_NAMESIZE, ep->utmp.ut_name,
1.1 deraadt 329: strncmp(ep->utmp.ut_line, "tty", 3) ?
330: ep->utmp.ut_line : ep->utmp.ut_line + 3,
1.32 deraadt 331: HOST_WIDTH, HOST_WIDTH, *p ? p : "-");
1.1 deraadt 332: pr_attime(&ep->utmp.ut_time, &now);
333: pr_idle(ep->idle);
334: pr_args(ep->kp);
335: printf("\n");
336: }
337: exit(0);
338: }
339:
340: static void
1.41 millert 341: pr_args(struct kinfo_proc2 *kp)
1.1 deraadt 342: {
1.7 downsj 343: char **argv, *str;
1.1 deraadt 344: int left;
345:
1.23 millert 346: if (kp == NULL)
1.41 millert 347: goto nothing; /* XXX - can this happen? */
1.1 deraadt 348: left = argwidth;
1.41 millert 349: argv = kvm_getargv2(kd, kp, argwidth+60); /* +60 for ftpd snip */
1.23 millert 350: if (argv == NULL)
1.1 deraadt 351: goto nothing;
1.7 downsj 352:
1.23 millert 353: if (*argv == NULL || **argv == '\0') {
354: /* Process has zeroed argv[0], display executable name. */
355: fmt_putc('(', &left);
1.41 millert 356: fmt_puts(kp->p_comm, &left);
1.23 millert 357: fmt_putc(')', &left);
358: }
1.1 deraadt 359: while (*argv) {
1.7 downsj 360: /* ftp is a special case... */
361: if (strncmp(*argv, "ftpd:", 5) == 0) {
362: str = strrchr(*argv, ':');
363: if (str != (char *)NULL) {
1.8 downsj 364: if ((str[0] == ':') && isspace(str[1]))
1.7 downsj 365: str += 2;
366: fmt_puts(str, &left);
367: } else
368: fmt_puts(*argv, &left);
369: } else
370: fmt_puts(*argv, &left);
1.1 deraadt 371: argv++;
372: fmt_putc(' ', &left);
373: }
374: return;
375: nothing:
376: putchar('-');
377: }
378:
379: static void
1.38 deraadt 380: pr_header(time_t *nowp, int nusers)
1.1 deraadt 381: {
382: double avenrun[3];
383: time_t uptime;
384: int days, hrs, i, mins;
385: int mib[2];
386: size_t size;
387: char buf[256];
388:
389: /*
390: * Print time of day.
391: */
1.40 millert 392: (void)strftime(buf, sizeof(buf) - 1, "%l:%M%p", localtime(nowp));
1.33 smart 393: buf[sizeof(buf) - 1] = '\0';
1.1 deraadt 394: (void)printf("%s ", buf);
395:
396: /*
397: * Print how long system has been up.
398: * (Found by looking getting "boottime" from the kernel)
399: */
400: mib[0] = CTL_KERN;
401: mib[1] = KERN_BOOTTIME;
402: size = sizeof(boottime);
1.17 deraadt 403: if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1) {
1.1 deraadt 404: uptime = now - boottime.tv_sec;
1.19 mickey 405: if (uptime > 59) {
1.17 deraadt 406: uptime += 30;
407: days = uptime / SECSPERDAY;
408: uptime %= SECSPERDAY;
409: hrs = uptime / SECSPERHOUR;
410: uptime %= SECSPERHOUR;
411: mins = uptime / SECSPERMIN;
412: (void)printf(" up");
413: if (days > 0)
414: (void)printf(" %d day%s,", days,
415: days > 1 ? "s" : "");
416: if (hrs > 0 && mins > 0)
417: (void)printf(" %2d:%02d,", hrs, mins);
418: else {
419: if (hrs > 0)
420: (void)printf(" %d hr%s,",
421: hrs, hrs > 1 ? "s" : "");
422: if (mins > 0 || (days == 0 && hrs == 0))
423: (void)printf(" %d min%s,",
424: mins, mins != 1 ? "s" : "");
425: }
426: } else
1.18 deraadt 427: printf(" %d secs,", uptime);
1.1 deraadt 428: }
429:
430: /* Print number of users logged in to system */
431: (void)printf(" %d user%s", nusers, nusers != 1 ? "s" : "");
432:
433: /*
434: * Print 1, 5, and 15 minute load averages.
435: */
436: if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) == -1)
437: (void)printf(", no load average information available\n");
438: else {
439: (void)printf(", load averages:");
440: for (i = 0; i < (sizeof(avenrun) / sizeof(avenrun[0])); i++) {
441: if (i > 0)
442: (void)printf(",");
443: (void)printf(" %.2f", avenrun[i]);
444: }
445: (void)printf("\n");
446: }
447: }
448:
449: static struct stat *
1.38 deraadt 450: ttystat(char *line)
1.1 deraadt 451: {
452: static struct stat sb;
1.26 millert 453: char ttybuf[sizeof(_PATH_DEV) + UT_LINESIZE];
1.1 deraadt 454:
1.26 millert 455: /* Note, line may not be NUL-terminated */
1.33 smart 456: (void)strlcpy(ttybuf, _PATH_DEV, sizeof(ttybuf));
457: (void)strncat(ttybuf, line, sizeof(ttybuf) - 1 - strlen(ttybuf));
1.1 deraadt 458: if (stat(ttybuf, &sb))
459: return (NULL);
460: return (&sb);
461: }
462:
463: static void
1.38 deraadt 464: usage(int wcmd)
1.1 deraadt 465: {
466: if (wcmd)
467: (void)fprintf(stderr,
1.42 jmc 468: "usage: w [-ahi] [-M core] [-N system] [user]\n");
1.1 deraadt 469: else
1.33 smart 470: (void)fprintf(stderr,
1.45 ! jaredy 471: "usage: uptime\n");
1.1 deraadt 472: exit (1);
473: }