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