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