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