[BACK]Return to w.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / w

Annotation of src/usr.bin/w/w.c, Revision 1.7

1.7     ! downsj      1: /*     $OpenBSD: w.c,v 1.6 1996/08/12 02:28:43 deraadt 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++;
                    135:        if (*p == 'u') {
                    136:                wcmd = 0;
                    137:                p = "";
                    138:        } else {
                    139:                wcmd = 1;
1.6       deraadt   140:                p = "hiflM:N:asuw";
1.1       deraadt   141:        }
                    142:
                    143:        memf = nlistf = NULL;
                    144:        while ((ch = getopt(argc, argv, p)) != EOF)
                    145:                switch (ch) {
                    146:                case 'h':
                    147:                        header = 0;
                    148:                        break;
                    149:                case 'i':
                    150:                        sortidle = 1;
                    151:                        break;
                    152:                case 'M':
                    153:                        header = 0;
                    154:                        memf = optarg;
                    155:                        break;
                    156:                case 'N':
                    157:                        nlistf = optarg;
                    158:                        break;
1.6       deraadt   159:                case 'a':
                    160:                        nflag = 0;
1.1       deraadt   161:                        break;
                    162:                case 'f': case 'l': case 's': case 'u': case 'w':
                    163:                        warnx("[-flsuw] no longer supported");
                    164:                        /* FALLTHROUGH */
                    165:                case '?':
                    166:                default:
                    167:                        usage(wcmd);
                    168:                }
                    169:        argc -= optind;
                    170:        argv += optind;
                    171:
1.3       deraadt   172:        /*
                    173:         * Discard setgid privileges if not the running kernel so that bad
                    174:         * guys can't print interesting stuff from kernel memory.
                    175:         */
                    176:        if (nlistf != NULL || memf != NULL)
                    177:                setgid(getgid());
                    178:
1.1       deraadt   179:        if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf)) == NULL)
                    180:                errx(1, "%s", errbuf);
                    181:
                    182:        (void)time(&now);
                    183:        if ((ut = fopen(_PATH_UTMP, "r")) == NULL)
                    184:                err(1, "%s", _PATH_UTMP);
                    185:
                    186:        if (*argv)
                    187:                sel_user = *argv;
                    188:
                    189:        for (nusers = 0; fread(&utmp, sizeof(utmp), 1, ut);) {
                    190:                if (utmp.ut_name[0] == '\0')
                    191:                        continue;
                    192:                ++nusers;
                    193:                if (wcmd == 0 || (sel_user &&
                    194:                    strncmp(utmp.ut_name, sel_user, UT_NAMESIZE) != 0))
                    195:                        continue;
                    196:                if ((ep = calloc(1, sizeof(struct entry))) == NULL)
                    197:                        err(1, NULL);
                    198:                *nextp = ep;
                    199:                nextp = &(ep->next);
                    200:                memmove(&(ep->utmp), &utmp, sizeof(struct utmp));
                    201:                if (!(stp = ttystat(ep->utmp.ut_line)))
                    202:                        continue;
                    203:                ep->tdev = stp->st_rdev;
                    204: #ifdef CPU_CONSDEV
                    205:                /*
                    206:                 * If this is the console device, attempt to ascertain
                    207:                 * the true console device dev_t.
                    208:                 */
                    209:                if (ep->tdev == 0) {
                    210:                        int mib[2];
                    211:                        size_t size;
                    212:
                    213:                        mib[0] = CTL_MACHDEP;
                    214:                        mib[1] = CPU_CONSDEV;
                    215:                        size = sizeof(dev_t);
                    216:                        (void) sysctl(mib, 2, &ep->tdev, &size, NULL, 0);
                    217:                }
                    218: #endif
                    219:                if ((ep->idle = now - stp->st_atime) < 0)
                    220:                        ep->idle = 0;
                    221:        }
                    222:        (void)fclose(ut);
                    223:
                    224:        if (header || wcmd == 0) {
                    225:                pr_header(&now, nusers);
                    226:                if (wcmd == 0)
                    227:                        exit (0);
                    228:        }
                    229:
                    230: #define HEADER "USER    TTY FROM              LOGIN@  IDLE WHAT\n"
                    231: #define WUSED  (sizeof (HEADER) - sizeof ("WHAT\n"))
                    232:        (void)printf(HEADER);
                    233:
                    234:        if ((kp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nentries)) == NULL)
1.2       deraadt   235:                errx(1, "%s", kvm_geterr(kd));
1.1       deraadt   236:        for (i = 0; i < nentries; i++, kp++) {
                    237:                struct proc *p = &kp->kp_proc;
                    238:                struct eproc *e;
                    239:
                    240:                if (p->p_stat == SIDL || p->p_stat == SZOMB)
                    241:                        continue;
                    242:                e = &kp->kp_eproc;
                    243:                for (ep = ehead; ep != NULL; ep = ep->next) {
1.7     ! downsj    244:                        /* ftp is a special case. */
        !           245:                        if (strncmp(ep->utmp.ut_line, "ftp", 3) == 0) {
        !           246:                                pid_t fp = (pid_t)strtol(&ep->utmp.ut_line[3],
        !           247:                                                         NULL, 10);
        !           248:                                if (p->p_pid == fp) {
        !           249:                                        ep->kp = kp;
        !           250:
        !           251:                                        break;
        !           252:                                }
        !           253:                        } else if (ep->tdev == e->e_tdev
        !           254:                                   && e->e_pgid == e->e_tpgid) {
1.1       deraadt   255:                                /*
                    256:                                 * Proc is in foreground of this terminal
                    257:                                 */
                    258:                                if (proc_compare(&ep->kp->kp_proc, p))
                    259:                                        ep->kp = kp;
                    260:                                break;
                    261:                        }
                    262:                }
                    263:        }
                    264:        if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 &&
                    265:             ioctl(STDERR_FILENO, TIOCGWINSZ, &ws) == -1 &&
                    266:             ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1) || ws.ws_col == 0)
                    267:               ttywidth = 79;
                    268:         else
                    269:               ttywidth = ws.ws_col - 1;
                    270:        argwidth = ttywidth - WUSED;
                    271:        if (argwidth < 4)
                    272:                argwidth = 8;
                    273:        /* sort by idle time */
                    274:        if (sortidle && ehead != NULL) {
                    275:                struct entry *from = ehead, *save;
                    276:
                    277:                ehead = NULL;
                    278:                while (from != NULL) {
                    279:                        for (nextp = &ehead;
                    280:                            (*nextp) && from->idle >= (*nextp)->idle;
                    281:                            nextp = &(*nextp)->next)
                    282:                                continue;
                    283:                        save = from;
                    284:                        from = from->next;
                    285:                        save->next = *nextp;
                    286:                        *nextp = save;
                    287:                }
                    288:        }
                    289:
                    290:        if (!nflag)
                    291:                if (gethostname(domain, sizeof(domain) - 1) < 0 ||
                    292:                    (p = strchr(domain, '.')) == 0)
                    293:                        domain[0] = '\0';
                    294:                else {
                    295:                        domain[sizeof(domain) - 1] = '\0';
                    296:                        memmove(domain, p, strlen(p) + 1);
                    297:                }
                    298:
                    299:        for (ep = ehead; ep != NULL; ep = ep->next) {
                    300:                p = *ep->utmp.ut_host ? ep->utmp.ut_host : "-";
                    301:                if ((x = strchr(p, ':')) != NULL)
                    302:                        *x++ = '\0';
                    303:                if (!nflag && isdigit(*p) &&
                    304:                    (long)(l = inet_addr(p)) != -1 &&
                    305:                    (hp = gethostbyaddr((char *)&l, sizeof(l), AF_INET))) {
                    306:                        if (domain[0] != '\0') {
                    307:                                p = hp->h_name;
                    308:                                p += strlen(hp->h_name);
                    309:                                p -= strlen(domain);
                    310:                                if (p > hp->h_name && strcmp(p, domain) == 0)
                    311:                                        *p = '\0';
                    312:                        }
                    313:                        p = hp->h_name;
                    314:                }
                    315:                if (x) {
                    316:                        (void)snprintf(buf, sizeof(buf), "%s:%.*s", p,
                    317:                            ep->utmp.ut_host + UT_HOSTSIZE - x, x);
                    318:                        p = buf;
                    319:                }
                    320:                (void)printf("%-*.*s %-2.2s %-*.*s ",
                    321:                    UT_NAMESIZE, UT_NAMESIZE, ep->utmp.ut_name,
                    322:                    strncmp(ep->utmp.ut_line, "tty", 3) ?
                    323:                    ep->utmp.ut_line : ep->utmp.ut_line + 3,
                    324:                    UT_HOSTSIZE, UT_HOSTSIZE, *p ? p : "-");
                    325:                pr_attime(&ep->utmp.ut_time, &now);
                    326:                pr_idle(ep->idle);
                    327:                pr_args(ep->kp);
                    328:                printf("\n");
                    329:        }
                    330:        exit(0);
                    331: }
                    332:
                    333: static void
                    334: pr_args(kp)
                    335:        struct kinfo_proc *kp;
                    336: {
1.7     ! downsj    337:        char **argv, *str;
1.1       deraadt   338:        int left;
                    339:
                    340:        if (kp == 0)
                    341:                goto nothing;
                    342:        left = argwidth;
                    343:        argv = kvm_getargv(kd, kp, argwidth);
                    344:        if (argv == 0)
                    345:                goto nothing;
1.7     ! downsj    346:
1.1       deraadt   347:        while (*argv) {
1.7     ! downsj    348:                /* ftp is a special case... */
        !           349:                if (strncmp(*argv, "ftpd:", 5) == 0) {
        !           350:                        str = strrchr(*argv, ':');
        !           351:                        if (str != (char *)NULL) {
        !           352:                                if (strlen(str) > 2)
        !           353:                                        str += 2;
        !           354:                                fmt_puts(str, &left);
        !           355:                        } else
        !           356:                                fmt_puts(*argv, &left);
        !           357:                } else
        !           358:                        fmt_puts(*argv, &left);
1.1       deraadt   359:                argv++;
                    360:                fmt_putc(' ', &left);
                    361:        }
                    362:        return;
                    363: nothing:
                    364:        putchar('-');
                    365: }
                    366:
                    367: static void
                    368: pr_header(nowp, nusers)
                    369:        time_t *nowp;
                    370:        int nusers;
                    371: {
                    372:        double avenrun[3];
                    373:        time_t uptime;
                    374:        int days, hrs, i, mins;
                    375:        int mib[2];
                    376:        size_t size;
                    377:        char buf[256];
                    378:
                    379:        /*
                    380:         * Print time of day.
                    381:         *
                    382:         * SCCS forces the string manipulation below, as it replaces
                    383:         * %, M, and % in a character string with the file name.
                    384:         */
                    385:        (void)strftime(buf, sizeof(buf),
                    386:            __CONCAT("%l:%","M%p"), localtime(nowp));
                    387:        (void)printf("%s ", buf);
                    388:
                    389:        /*
                    390:         * Print how long system has been up.
                    391:         * (Found by looking getting "boottime" from the kernel)
                    392:         */
                    393:        mib[0] = CTL_KERN;
                    394:        mib[1] = KERN_BOOTTIME;
                    395:        size = sizeof(boottime);
                    396:        if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 &&
                    397:            boottime.tv_sec != 0) {
                    398:                uptime = now - boottime.tv_sec;
                    399:                uptime += 30;
                    400:                days = uptime / SECSPERDAY;
                    401:                uptime %= SECSPERDAY;
                    402:                hrs = uptime / SECSPERHOUR;
                    403:                uptime %= SECSPERHOUR;
                    404:                mins = uptime / SECSPERMIN;
                    405:                (void)printf(" up");
                    406:                if (days > 0)
                    407:                        (void)printf(" %d day%s,", days, days > 1 ? "s" : "");
                    408:                if (hrs > 0 && mins > 0)
                    409:                        (void)printf(" %2d:%02d,", hrs, mins);
                    410:                else {
                    411:                        if (hrs > 0)
                    412:                                (void)printf(" %d hr%s,",
                    413:                                    hrs, hrs > 1 ? "s" : "");
                    414:                        if (mins > 0)
                    415:                                (void)printf(" %d min%s,",
                    416:                                    mins, mins > 1 ? "s" : "");
                    417:                }
                    418:        }
                    419:
                    420:        /* Print number of users logged in to system */
                    421:        (void)printf(" %d user%s", nusers, nusers != 1 ? "s" : "");
                    422:
                    423:        /*
                    424:         * Print 1, 5, and 15 minute load averages.
                    425:         */
                    426:        if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) == -1)
                    427:                (void)printf(", no load average information available\n");
                    428:        else {
                    429:                (void)printf(", load averages:");
                    430:                for (i = 0; i < (sizeof(avenrun) / sizeof(avenrun[0])); i++) {
                    431:                        if (i > 0)
                    432:                                (void)printf(",");
                    433:                        (void)printf(" %.2f", avenrun[i]);
                    434:                }
                    435:                (void)printf("\n");
                    436:        }
                    437: }
                    438:
                    439: static struct stat *
                    440: ttystat(line)
                    441:        char *line;
                    442: {
                    443:        static struct stat sb;
                    444:        char ttybuf[MAXPATHLEN];
                    445:
                    446:        (void)snprintf(ttybuf, sizeof(ttybuf), "%s/%s", _PATH_DEV, line);
                    447:        if (stat(ttybuf, &sb))
                    448:                return (NULL);
                    449:        return (&sb);
                    450: }
                    451:
                    452: static void
                    453: usage(wcmd)
                    454:        int wcmd;
                    455: {
                    456:        if (wcmd)
                    457:                (void)fprintf(stderr,
                    458:                    "usage: w: [-hin] [-M core] [-N system] [user]\n");
                    459:        else
                    460:                (void)fprintf(stderr, "uptime\n");
                    461:        exit (1);
                    462: }