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

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