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