Annotation of src/usr.bin/w/w.c, Revision 1.20
1.20 ! kstailey 1: /* $OpenBSD: w.c,v 1.19 1997/07/25 05:04:07 mickey 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.
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: static char sccsid[] = "@(#)w.c 8.4 (Berkeley) 4/16/94";
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:
99: /*
100: * One of these per active utmp entry.
101: */
102: struct entry {
103: struct entry *next;
104: struct utmp utmp;
105: dev_t tdev; /* dev_t of terminal */
106: time_t idle; /* idle time of terminal in seconds */
107: struct kinfo_proc *kp; /* `most interesting' proc */
108: } *ep, *ehead = NULL, **nextp = &ehead;
109:
110: static void pr_args __P((struct kinfo_proc *));
111: static void pr_header __P((time_t *, int));
112: static struct stat
113: *ttystat __P((char *));
114: static void usage __P((int));
115:
116: int
117: main(argc, argv)
118: int argc;
119: char **argv;
120: {
121: extern char *__progname;
122: struct kinfo_proc *kp;
123: struct hostent *hp;
124: struct stat *stp;
125: FILE *ut;
126: u_long l;
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;
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.3 deraadt 174: /*
175: * Discard setgid privileges if not the running kernel so that bad
176: * guys can't print interesting stuff from kernel memory.
177: */
1.11 tholo 178: if (nlistf != NULL || memf != NULL) {
179: setegid(getgid());
1.3 deraadt 180: setgid(getgid());
1.11 tholo 181: }
1.3 deraadt 182:
1.1 deraadt 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)
1.2 deraadt 239: errx(1, "%s", kvm_geterr(kd));
1.1 deraadt 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) {
1.7 downsj 248: /* ftp is a special case. */
249: if (strncmp(ep->utmp.ut_line, "ftp", 3) == 0) {
250: pid_t fp = (pid_t)strtol(&ep->utmp.ut_line[3],
251: NULL, 10);
252: if (p->p_pid == fp) {
253: ep->kp = kp;
254:
255: break;
256: }
257: } else if (ep->tdev == e->e_tdev
258: && e->e_pgid == e->e_tpgid) {
1.1 deraadt 259: /*
260: * Proc is in foreground of this terminal
261: */
262: if (proc_compare(&ep->kp->kp_proc, p))
263: ep->kp = kp;
264: break;
265: }
266: }
267: }
268: if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 &&
269: ioctl(STDERR_FILENO, TIOCGWINSZ, &ws) == -1 &&
270: ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1) || ws.ws_col == 0)
271: ttywidth = 79;
272: else
273: ttywidth = ws.ws_col - 1;
274: argwidth = ttywidth - WUSED;
275: if (argwidth < 4)
276: argwidth = 8;
277: /* sort by idle time */
278: if (sortidle && ehead != NULL) {
279: struct entry *from = ehead, *save;
280:
281: ehead = NULL;
282: while (from != NULL) {
283: for (nextp = &ehead;
284: (*nextp) && from->idle >= (*nextp)->idle;
285: nextp = &(*nextp)->next)
286: continue;
287: save = from;
288: from = from->next;
289: save->next = *nextp;
290: *nextp = save;
291: }
292: }
293:
294: if (!nflag)
295: if (gethostname(domain, sizeof(domain) - 1) < 0 ||
296: (p = strchr(domain, '.')) == 0)
297: domain[0] = '\0';
298: else {
299: domain[sizeof(domain) - 1] = '\0';
300: memmove(domain, p, strlen(p) + 1);
301: }
302:
303: for (ep = ehead; ep != NULL; ep = ep->next) {
304: p = *ep->utmp.ut_host ? ep->utmp.ut_host : "-";
1.16 millert 305: for (x = NULL, i = 0; p[i] != '\0' && i < UT_HOSTSIZE; i++)
306: if (p[i] == ':') {
307: x = &p[i];
308: *x++ = '\0';
309: break;
310: }
1.1 deraadt 311: if (!nflag && isdigit(*p) &&
312: (long)(l = inet_addr(p)) != -1 &&
313: (hp = gethostbyaddr((char *)&l, sizeof(l), AF_INET))) {
314: if (domain[0] != '\0') {
315: p = hp->h_name;
316: p += strlen(hp->h_name);
317: p -= strlen(domain);
318: if (p > hp->h_name && strcmp(p, domain) == 0)
319: *p = '\0';
320: }
321: p = hp->h_name;
322: }
323: if (x) {
324: (void)snprintf(buf, sizeof(buf), "%s:%.*s", p,
325: ep->utmp.ut_host + UT_HOSTSIZE - x, x);
326: p = buf;
327: }
328: (void)printf("%-*.*s %-2.2s %-*.*s ",
329: UT_NAMESIZE, UT_NAMESIZE, ep->utmp.ut_name,
330: strncmp(ep->utmp.ut_line, "tty", 3) ?
331: ep->utmp.ut_line : ep->utmp.ut_line + 3,
332: UT_HOSTSIZE, UT_HOSTSIZE, *p ? p : "-");
333: pr_attime(&ep->utmp.ut_time, &now);
334: pr_idle(ep->idle);
335: pr_args(ep->kp);
336: printf("\n");
337: }
338: exit(0);
339: }
340:
341: static void
342: pr_args(kp)
343: struct kinfo_proc *kp;
344: {
1.7 downsj 345: char **argv, *str;
1.1 deraadt 346: int left;
347:
348: if (kp == 0)
349: goto nothing;
350: left = argwidth;
1.9 deraadt 351: argv = kvm_getargv(kd, kp, argwidth+60); /*+60 for ftpd snip */
1.1 deraadt 352: if (argv == 0)
353: goto nothing;
1.7 downsj 354:
1.1 deraadt 355: while (*argv) {
1.7 downsj 356: /* ftp is a special case... */
357: if (strncmp(*argv, "ftpd:", 5) == 0) {
358: str = strrchr(*argv, ':');
359: if (str != (char *)NULL) {
1.8 downsj 360: if ((str[0] == ':') && isspace(str[1]))
1.7 downsj 361: str += 2;
362: fmt_puts(str, &left);
363: } else
364: fmt_puts(*argv, &left);
365: } else
366: fmt_puts(*argv, &left);
1.1 deraadt 367: argv++;
368: fmt_putc(' ', &left);
369: }
370: return;
371: nothing:
372: putchar('-');
373: }
374:
375: static void
376: pr_header(nowp, nusers)
377: time_t *nowp;
378: int nusers;
379: {
380: double avenrun[3];
381: time_t uptime;
382: int days, hrs, i, mins;
383: int mib[2];
384: size_t size;
385: char buf[256];
386:
387: /*
388: * Print time of day.
389: *
390: * SCCS forces the string manipulation below, as it replaces
391: * %, M, and % in a character string with the file name.
392: */
1.14 deraadt 393: (void)strftime(buf, sizeof(buf) - 1,
1.1 deraadt 394: __CONCAT("%l:%","M%p"), localtime(nowp));
1.14 deraadt 395: buf[sizeof buf -1] = '\0';
1.1 deraadt 396: (void)printf("%s ", buf);
397:
398: /*
399: * Print how long system has been up.
400: * (Found by looking getting "boottime" from the kernel)
401: */
402: mib[0] = CTL_KERN;
403: mib[1] = KERN_BOOTTIME;
404: size = sizeof(boottime);
1.17 deraadt 405: if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1) {
1.1 deraadt 406: uptime = now - boottime.tv_sec;
1.19 mickey 407: if (uptime > 59) {
1.17 deraadt 408: uptime += 30;
409: days = uptime / SECSPERDAY;
410: uptime %= SECSPERDAY;
411: hrs = uptime / SECSPERHOUR;
412: uptime %= SECSPERHOUR;
413: mins = uptime / SECSPERMIN;
414: (void)printf(" up");
415: if (days > 0)
416: (void)printf(" %d day%s,", days,
417: days > 1 ? "s" : "");
418: if (hrs > 0 && mins > 0)
419: (void)printf(" %2d:%02d,", hrs, mins);
420: else {
421: if (hrs > 0)
422: (void)printf(" %d hr%s,",
423: hrs, hrs > 1 ? "s" : "");
424: if (mins > 0 || (days == 0 && hrs == 0))
425: (void)printf(" %d min%s,",
426: mins, mins != 1 ? "s" : "");
427: }
428: } else
1.18 deraadt 429: printf(" %d secs,", uptime);
1.1 deraadt 430: }
431:
432: /* Print number of users logged in to system */
433: (void)printf(" %d user%s", nusers, nusers != 1 ? "s" : "");
434:
435: /*
436: * Print 1, 5, and 15 minute load averages.
437: */
438: if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) == -1)
439: (void)printf(", no load average information available\n");
440: else {
441: (void)printf(", load averages:");
442: for (i = 0; i < (sizeof(avenrun) / sizeof(avenrun[0])); i++) {
443: if (i > 0)
444: (void)printf(",");
445: (void)printf(" %.2f", avenrun[i]);
446: }
447: (void)printf("\n");
448: }
449: }
450:
451: static struct stat *
452: ttystat(line)
453: char *line;
454: {
455: static struct stat sb;
456: char ttybuf[MAXPATHLEN];
457:
458: (void)snprintf(ttybuf, sizeof(ttybuf), "%s/%s", _PATH_DEV, line);
459: if (stat(ttybuf, &sb))
460: return (NULL);
461: return (&sb);
462: }
463:
464: static void
465: usage(wcmd)
466: int wcmd;
467: {
468: if (wcmd)
469: (void)fprintf(stderr,
1.13 angelos 470: "usage: w: [-hia] [-M core] [-N system] [user]\n");
1.1 deraadt 471: else
472: (void)fprintf(stderr, "uptime\n");
473: exit (1);
474: }