[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.5

1.5     ! deraadt     1: /*     $OpenBSD: w.c,v 1.4 1996/06/26 05:42:45 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 */
                     94: int            nflag;          /* true if -n flag: don't convert addrs */
                     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;
                    140:                p = "hiflM:N:nsuw";
                    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;
                    159:                case 'n':
                    160:                        nflag = 1;
                    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) {
                    244:                        if (ep->tdev == e->e_tdev && e->e_pgid == e->e_tpgid) {
                    245:                                /*
                    246:                                 * Proc is in foreground of this terminal
                    247:                                 */
                    248:                                if (proc_compare(&ep->kp->kp_proc, p))
                    249:                                        ep->kp = kp;
                    250:                                break;
                    251:                        }
                    252:                }
                    253:        }
                    254:        if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 &&
                    255:             ioctl(STDERR_FILENO, TIOCGWINSZ, &ws) == -1 &&
                    256:             ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1) || ws.ws_col == 0)
                    257:               ttywidth = 79;
                    258:         else
                    259:               ttywidth = ws.ws_col - 1;
                    260:        argwidth = ttywidth - WUSED;
                    261:        if (argwidth < 4)
                    262:                argwidth = 8;
                    263:        /* sort by idle time */
                    264:        if (sortidle && ehead != NULL) {
                    265:                struct entry *from = ehead, *save;
                    266:
                    267:                ehead = NULL;
                    268:                while (from != NULL) {
                    269:                        for (nextp = &ehead;
                    270:                            (*nextp) && from->idle >= (*nextp)->idle;
                    271:                            nextp = &(*nextp)->next)
                    272:                                continue;
                    273:                        save = from;
                    274:                        from = from->next;
                    275:                        save->next = *nextp;
                    276:                        *nextp = save;
                    277:                }
                    278:        }
                    279:
                    280:        if (!nflag)
                    281:                if (gethostname(domain, sizeof(domain) - 1) < 0 ||
                    282:                    (p = strchr(domain, '.')) == 0)
                    283:                        domain[0] = '\0';
                    284:                else {
                    285:                        domain[sizeof(domain) - 1] = '\0';
                    286:                        memmove(domain, p, strlen(p) + 1);
                    287:                }
                    288:
                    289:        for (ep = ehead; ep != NULL; ep = ep->next) {
                    290:                p = *ep->utmp.ut_host ? ep->utmp.ut_host : "-";
                    291:                if ((x = strchr(p, ':')) != NULL)
                    292:                        *x++ = '\0';
                    293:                if (!nflag && isdigit(*p) &&
                    294:                    (long)(l = inet_addr(p)) != -1 &&
                    295:                    (hp = gethostbyaddr((char *)&l, sizeof(l), AF_INET))) {
                    296:                        if (domain[0] != '\0') {
                    297:                                p = hp->h_name;
                    298:                                p += strlen(hp->h_name);
                    299:                                p -= strlen(domain);
                    300:                                if (p > hp->h_name && strcmp(p, domain) == 0)
                    301:                                        *p = '\0';
                    302:                        }
                    303:                        p = hp->h_name;
                    304:                }
                    305:                if (x) {
                    306:                        (void)snprintf(buf, sizeof(buf), "%s:%.*s", p,
                    307:                            ep->utmp.ut_host + UT_HOSTSIZE - x, x);
                    308:                        p = buf;
                    309:                }
                    310:                (void)printf("%-*.*s %-2.2s %-*.*s ",
                    311:                    UT_NAMESIZE, UT_NAMESIZE, ep->utmp.ut_name,
                    312:                    strncmp(ep->utmp.ut_line, "tty", 3) ?
                    313:                    ep->utmp.ut_line : ep->utmp.ut_line + 3,
                    314:                    UT_HOSTSIZE, UT_HOSTSIZE, *p ? p : "-");
                    315:                pr_attime(&ep->utmp.ut_time, &now);
                    316:                pr_idle(ep->idle);
                    317:                pr_args(ep->kp);
                    318:                printf("\n");
                    319:        }
                    320:        exit(0);
                    321: }
                    322:
                    323: static void
                    324: pr_args(kp)
                    325:        struct kinfo_proc *kp;
                    326: {
                    327:        char **argv;
                    328:        int left;
                    329:
                    330:        if (kp == 0)
                    331:                goto nothing;
                    332:        left = argwidth;
                    333:        argv = kvm_getargv(kd, kp, argwidth);
                    334:        if (argv == 0)
                    335:                goto nothing;
                    336:        while (*argv) {
                    337:                fmt_puts(*argv, &left);
                    338:                argv++;
                    339:                fmt_putc(' ', &left);
                    340:        }
                    341:        return;
                    342: nothing:
                    343:        putchar('-');
                    344: }
                    345:
                    346: static void
                    347: pr_header(nowp, nusers)
                    348:        time_t *nowp;
                    349:        int nusers;
                    350: {
                    351:        double avenrun[3];
                    352:        time_t uptime;
                    353:        int days, hrs, i, mins;
                    354:        int mib[2];
                    355:        size_t size;
                    356:        char buf[256];
                    357:
                    358:        /*
                    359:         * Print time of day.
                    360:         *
                    361:         * SCCS forces the string manipulation below, as it replaces
                    362:         * %, M, and % in a character string with the file name.
                    363:         */
                    364:        (void)strftime(buf, sizeof(buf),
                    365:            __CONCAT("%l:%","M%p"), localtime(nowp));
                    366:        (void)printf("%s ", buf);
                    367:
                    368:        /*
                    369:         * Print how long system has been up.
                    370:         * (Found by looking getting "boottime" from the kernel)
                    371:         */
                    372:        mib[0] = CTL_KERN;
                    373:        mib[1] = KERN_BOOTTIME;
                    374:        size = sizeof(boottime);
                    375:        if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 &&
                    376:            boottime.tv_sec != 0) {
                    377:                uptime = now - boottime.tv_sec;
                    378:                uptime += 30;
                    379:                days = uptime / SECSPERDAY;
                    380:                uptime %= SECSPERDAY;
                    381:                hrs = uptime / SECSPERHOUR;
                    382:                uptime %= SECSPERHOUR;
                    383:                mins = uptime / SECSPERMIN;
                    384:                (void)printf(" up");
                    385:                if (days > 0)
                    386:                        (void)printf(" %d day%s,", days, days > 1 ? "s" : "");
                    387:                if (hrs > 0 && mins > 0)
                    388:                        (void)printf(" %2d:%02d,", hrs, mins);
                    389:                else {
                    390:                        if (hrs > 0)
                    391:                                (void)printf(" %d hr%s,",
                    392:                                    hrs, hrs > 1 ? "s" : "");
                    393:                        if (mins > 0)
                    394:                                (void)printf(" %d min%s,",
                    395:                                    mins, mins > 1 ? "s" : "");
                    396:                }
                    397:        }
                    398:
                    399:        /* Print number of users logged in to system */
                    400:        (void)printf(" %d user%s", nusers, nusers != 1 ? "s" : "");
                    401:
                    402:        /*
                    403:         * Print 1, 5, and 15 minute load averages.
                    404:         */
                    405:        if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) == -1)
                    406:                (void)printf(", no load average information available\n");
                    407:        else {
                    408:                (void)printf(", load averages:");
                    409:                for (i = 0; i < (sizeof(avenrun) / sizeof(avenrun[0])); i++) {
                    410:                        if (i > 0)
                    411:                                (void)printf(",");
                    412:                        (void)printf(" %.2f", avenrun[i]);
                    413:                }
                    414:                (void)printf("\n");
                    415:        }
                    416: }
                    417:
                    418: static struct stat *
                    419: ttystat(line)
                    420:        char *line;
                    421: {
                    422:        static struct stat sb;
                    423:        char ttybuf[MAXPATHLEN];
                    424:
                    425:        (void)snprintf(ttybuf, sizeof(ttybuf), "%s/%s", _PATH_DEV, line);
                    426:        if (stat(ttybuf, &sb))
                    427:                return (NULL);
                    428:        return (&sb);
                    429: }
                    430:
                    431: static void
                    432: usage(wcmd)
                    433:        int wcmd;
                    434: {
                    435:        if (wcmd)
                    436:                (void)fprintf(stderr,
                    437:                    "usage: w: [-hin] [-M core] [-N system] [user]\n");
                    438:        else
                    439:                (void)fprintf(stderr, "uptime\n");
                    440:        exit (1);
                    441: }