Annotation of cci/usr/src/bin/ps.c, revision 1.1.1.1

1.1       root        1: #ifndef lint
                      2: static char *sccsid = "@(#)ps.c        4.26 (Berkeley) 9/25/83";
                      3: #endif
                      4: 
                      5: /*
                      6:  * ps
                      7:  */
                      8: #include <stdio.h>
                      9: #include <ctype.h>
                     10: #include <nlist.h>
                     11: #include <pwd.h>
                     12: #include <sys/param.h>
                     13: #include <sys/tty.h>
                     14: #include <sys/dir.h>
                     15: #include <sys/user.h>
                     16: #include <sys/proc.h>
                     17: #include <machine/pte.h>
                     18: #include <sys/vm.h>
                     19: #include <sys/text.h>
                     20: #include <sys/stat.h>
                     21: #include <sys/mbuf.h>
                     22: #include <math.h>
                     23: 
                     24: struct nlist nl[] = {
                     25:        { "_proc" },
                     26: #define        X_PROC          0
                     27:        { "_Usrptmap" },
                     28: #define        X_USRPTMA       1
                     29:        { "_usrpt" },
                     30: #define        X_USRPT         2
                     31:        { "_text" },
                     32: #define        X_TEXT          3
                     33:        { "_nswap" },
                     34: #define        X_NSWAP         4
                     35:        { "_maxslp" },
                     36: #define        X_MAXSLP        5
                     37:        { "_ccpu" },
                     38: #define        X_CCPU          6
                     39:        { "_ecmx" },
                     40: #define        X_ECMX          7
                     41:        { "_nproc" },
                     42: #define        X_NPROC         8
                     43:        { "_ntext" },
                     44: #define        X_NTEXT         9
                     45:        { "_dmmin" },
                     46: #define        X_DMMIN         10
                     47:        { "_dmmax" },
                     48: #define        X_DMMAX         11
                     49:        { "_Sysmap" },
                     50: #define        X_SYSMAP        12
                     51:        { "_Syssize" },
                     52: #define        X_SYSSIZE       13
                     53:        { "" },
                     54: };
                     55: 
                     56: struct savcom {
                     57:        union {
                     58:                struct  lsav *lp;
                     59:                float   u_pctcpu;
                     60:                struct  vsav *vp;
                     61:                int     s_ssiz;
                     62:        } s_un;
                     63:        struct  asav *ap;
                     64: } *savcom;
                     65: 
                     66: struct asav {
                     67:        char    *a_cmdp;
                     68:        int     a_flag;
                     69:        short   a_stat, a_uid, a_pid, a_nice, a_pri, a_slptime, a_time;
                     70:        size_t  a_size, a_rss, a_tsiz, a_txtrss;
                     71:        short   a_xccount;
                     72:        char    a_tty[MAXNAMLEN+1];
                     73:        dev_t   a_ttyd;
                     74:        time_t  a_cpu;
                     75:        size_t  a_maxrss;
                     76: };
                     77: 
                     78: char   *lhdr;
                     79: struct lsav {
                     80:        short   l_ppid;
                     81:        char    l_cpu;
                     82:        int     l_addr;
                     83:        caddr_t l_wchan;
                     84: };
                     85: 
                     86: char   *uhdr;
                     87: char   *shdr;
                     88: 
                     89: char   *vhdr;
                     90: struct vsav {
                     91:        u_int   v_majflt;
                     92:        size_t  v_swrss, v_txtswrss;
                     93:        float   v_pctcpu;
                     94: };
                     95: 
                     96: struct proc proc[8];           /* 8 = a few, for less syscalls */
                     97: struct proc *mproc;
                     98: struct text *text;
                     99: 
                    100: union {
                    101:        struct  user user;
                    102:        char    upages[UPAGES][NBPG];
                    103: } user;
                    104: #define u      user.user
                    105: 
                    106: int    chkpid;
                    107: int    aflg, cflg, eflg, gflg, kflg, lflg, sflg,
                    108:        uflg, vflg, xflg;
                    109: char   *tptr;
                    110: char   *gettty(), *getcmd(), *getname(), *savestr(), *alloc(), *state();
                    111: char   *rindex(), *calloc(), *sbrk(), *strcpy(), *strcat(), *strncat();
                    112: char   *index(), *ttyname(), mytty[16];
                    113: long   lseek();
                    114: double pcpu(), pmem();
                    115: int    pscomp();
                    116: int    nswap, maxslp;
                    117: struct text *atext;
                    118: double ccpu;
                    119: int    ecmx;
                    120: struct pte *Usrptma, *usrpt;
                    121: int    nproc, ntext;
                    122: int    dmmin, dmmax;
                    123: 
                    124: struct ttys {
                    125:        char    name[MAXNAMLEN+1];
                    126:        dev_t   ttyd;
                    127:        struct  ttys *next;
                    128:        struct  ttys *cand;
                    129: } *allttys, *cand[16];
                    130: 
                    131: int    npr;
                    132: 
                    133: int    cmdstart;
                    134: int    twidth;
                    135: char   *kmemf, *memf, *swapf, *nlistf;
                    136: int    kmem, mem, swap = -1;
                    137: int    rawcpu, sumcpu;
                    138: 
                    139: int    pcbpf;
                    140: int    argaddr;
                    141: extern char _sobuf[];
                    142: 
                    143: #define        pgtok(a)        ((a)/(1024/NBPG))
                    144: 
                    145: main(argc, argv)
                    146:        char **argv;
                    147: {
                    148:        register int i, j;
                    149:        register char *ap;
                    150:        int uid;
                    151:        off_t procp;
                    152: 
                    153:        twidth = 80;
                    154:        argc--, argv++;
                    155:        if (argc > 0) {
                    156:                ap = argv[0];
                    157:                while (*ap) switch (*ap++) {
                    158: 
                    159:                case 'C':
                    160:                        rawcpu++;
                    161:                        break;
                    162:                case 'S':
                    163:                        sumcpu++;
                    164:                        break;
                    165:                case 'a':
                    166:                        aflg++;
                    167:                        break;
                    168:                case 'c':
                    169:                        cflg = !cflg;
                    170:                        break;
                    171:                case 'e':
                    172:                        eflg++;
                    173:                        break;
                    174:                case 'g':
                    175:                        gflg++;
                    176:                        break;
                    177:                case 'k':
                    178:                        kflg++;
                    179:                        break;
                    180:                case 'l':
                    181:                        lflg++;
                    182:                        break;
                    183:                case 's':
                    184:                        sflg++;
                    185:                        break;
                    186:                case 't':
                    187:                        if (*ap)
                    188:                                tptr = ap;
                    189:                        else if ((tptr = ttyname(2)) != 0) {
                    190:                                strcpy(mytty, tptr);
                    191:                                if ((tptr = index(mytty,'y')) != 0)
                    192:                                        tptr++;
                    193:                        }
                    194:                        aflg++;
                    195:                        gflg++;
                    196:                        if (tptr && *tptr == '?')
                    197:                                xflg++;
                    198:                        while (*ap)
                    199:                                ap++;
                    200:                        break;
                    201:                case 'u': 
                    202:                        uflg++;
                    203:                        break;
                    204:                case 'v':
                    205:                        cflg = 1;
                    206:                        vflg++;
                    207:                        break;
                    208:                case 'w':
                    209:                        if (twidth == 80)
                    210:                                twidth = 132;
                    211:                        else
                    212:                                twidth = BUFSIZ;
                    213:                        break;
                    214:                case 'x':
                    215:                        xflg++;
                    216:                        break;
                    217:                default:
                    218:                        if (!isdigit(ap[-1]))
                    219:                                break;
                    220:                        chkpid = atoi(--ap);
                    221:                        *ap = 0;
                    222:                        aflg++;
                    223:                        xflg++;
                    224:                        break;
                    225:                }
                    226:        }
                    227:        openfiles(argc, argv);
                    228:        getkvars(argc, argv);
                    229:        if (chdir("/dev") < 0) {
                    230:                perror("/dev");
                    231:                exit(1);
                    232:        }
                    233:        getdev();
                    234:        uid = getuid();
                    235:        printhdr();
                    236:        procp = getw(nl[X_PROC].n_value);
                    237:        nproc = getw(nl[X_NPROC].n_value);
                    238:        savcom = (struct savcom *)calloc(nproc, sizeof (*savcom));
                    239:        for (i=0; i<nproc; i += 8) {
                    240:                klseek(kmem, (long)procp, 0);
                    241:                j = nproc - i;
                    242:                if (j > 8)
                    243:                        j = 8;
                    244:                j *= sizeof (struct proc);
                    245:                if (read(kmem, (char *)proc, j) != j) {
                    246:                        cantread("proc table", kmemf);
                    247:                        exit(1);
                    248:                }
                    249:                procp += j;
                    250:                for (j = j / sizeof (struct proc) - 1; j >= 0; j--) {
                    251:                        mproc = &proc[j];
                    252:                        if (mproc->p_stat == 0 ||
                    253:                            mproc->p_pgrp == 0 && xflg == 0)
                    254:                                continue;
                    255:                        if (tptr == 0 && gflg == 0 && xflg == 0 &&
                    256:                            mproc->p_ppid == 1)
                    257:                                continue;
                    258:                        if (uid != mproc->p_uid && aflg==0 ||
                    259:                            chkpid != 0 && chkpid != mproc->p_pid)
                    260:                                continue;
                    261:                        if (vflg && gflg == 0 && xflg == 0) {
                    262:                                if (mproc->p_stat == SZOMB ||
                    263:                                    mproc->p_flag&SWEXIT)
                    264:                                        continue;
                    265:                                if (mproc->p_slptime > MAXSLP &&
                    266:                                    (mproc->p_stat == SSLEEP ||
                    267:                                     mproc->p_stat == SSTOP))
                    268:                                continue;
                    269:                        }
                    270:                        save();
                    271:                }
                    272:        }
                    273:        qsort(savcom, npr, sizeof(savcom[0]), pscomp);
                    274:        for (i=0; i<npr; i++) {
                    275:                register struct savcom *sp = &savcom[i];
                    276:                if (lflg)
                    277:                        lpr(sp);
                    278:                else if (vflg)
                    279:                        vpr(sp);
                    280:                else if (uflg)
                    281:                        upr(sp);
                    282:                else
                    283:                        spr(sp);
                    284:                if (sp->ap->a_flag & SWEXIT)
                    285:                        printf(" <exiting>");
                    286:                else if (sp->ap->a_stat == SZOMB)
                    287:                        printf(" <defunct>");
                    288:                else if (sp->ap->a_pid == 0)
                    289:                        printf(" swapper");
                    290:                else if (sp->ap->a_pid == 2)
                    291:                        printf(" pagedaemon");
                    292:                else if (sp->ap->a_pid == 3 && sp->ap->a_flag & SSYS)
                    293:                        printf(" ip input");
                    294:                else
                    295:                        printf(" %.*s", twidth - cmdstart - 2, sp->ap->a_cmdp);
                    296:                printf("\n");
                    297:        }
                    298:        exit(npr == 0);
                    299: }
                    300: 
                    301: getw(loc)
                    302:        unsigned long loc;
                    303: {
                    304:        long word;
                    305: 
                    306:        klseek(kmem, (long)loc, 0);
                    307:        if (read(kmem, (char *)&word, sizeof (word)) != sizeof (word))
                    308:                printf("error reading kmem at %x\n", loc);
                    309:        return (word);
                    310: }
                    311: 
                    312: klseek(fd, loc, off)
                    313:        int fd;
                    314:        long loc;
                    315:        int off;
                    316: {
                    317: 
                    318:        long v;
                    319:        long addr;
                    320:        struct pte pte;
                    321: 
                    322:        if (kflg && (loc & 0xc0000000) != 0) {
                    323:                loc &= ~0xc0000000;
                    324:                v = btop(loc);
                    325:                if(v >= nl[X_SYSSIZE].n_value) {
                    326:                        printf("address botch %x\n", loc);
                    327:                        return;
                    328:                }
                    329:                addr = (long)((struct pte *)nl[X_SYSMAP].n_value + v);
                    330:                lseek(fd, addr & ~0xc0000000, 0);
                    331:                if(read(fd, (char *)&pte, sizeof(pte)) != sizeof(pte)) {
                    332:                        printf("Error reading kmem for pte at %x\n", addr);
                    333:                        return;
                    334:                }
                    335:                if (pte.pg_v == 0 && (pte.pg_fod || pte.pg_pfnum == 0)) {
                    336:                        printf("pte bad for %x\n", addr);
                    337:                        return;
                    338:                }
                    339:                loc = (long)ptob(pte.pg_pfnum) + (loc & PGOFSET);
                    340:        }
                    341:        (void) lseek(fd, (long)loc, off);
                    342: }
                    343: 
                    344: openfiles(argc, argv)
                    345:        char **argv;
                    346: {
                    347: 
                    348:        kmemf = "/dev/kmem";
                    349:        if (kflg)
                    350:                kmemf = argc > 2 ? argv[2] : "/vmcore";
                    351:        kmem = open(kmemf, 0);
                    352:        if (kmem < 0) {
                    353:                perror(kmemf);
                    354:                exit(1);
                    355:        }
                    356:        if (kflg)  {
                    357:                mem = kmem;
                    358:                memf = kmemf;
                    359:        } else {
                    360:                memf = "/dev/mem";
                    361:                mem = open(memf, 0);
                    362:                if (mem < 0) {
                    363:                        perror(memf);
                    364:                        exit(1);
                    365:                }
                    366:        }
                    367:        if (kflg == 0 || argc > 3) {
                    368:                swapf = argc>3 ? argv[3]: "/dev/drum";
                    369:                swap = open(swapf, 0);
                    370:                if (swap < 0) {
                    371:                        perror(swapf);
                    372:                        exit(1);
                    373:                }
                    374:        }
                    375: }
                    376: 
                    377: getkvars(argc, argv)
                    378:        char **argv;
                    379: {
                    380:        register struct nlist *nlp;
                    381: 
                    382:        nlistf = argc > 1 ? argv[1] : "/vmunix";
                    383:        nlist(nlistf, nl);
                    384:        if (nl[0].n_type == 0) {
                    385:                fprintf(stderr, "%s: No namelist\n", nlistf);
                    386:                exit(1);
                    387:        }
                    388:        if (kflg)
                    389:                for (nlp = nl; nlp < &nl[sizeof (nl)/sizeof (nl[0])]; nlp++)
                    390:                        nlp->n_value = nlp->n_value;
                    391:        usrpt = (struct pte *)nl[X_USRPT].n_value;
                    392:        Usrptma = (struct pte *)nl[X_USRPTMA].n_value;
                    393:        klseek(kmem, (long)nl[X_NSWAP].n_value, 0);
                    394:        if (read(kmem, (char *)&nswap, sizeof (nswap)) != sizeof (nswap)) {
                    395:                cantread("nswap", kmemf);
                    396:                exit(1);
                    397:        }
                    398:        klseek(kmem, (long)nl[X_MAXSLP].n_value, 0);
                    399:        if (read(kmem, (char *)&maxslp, sizeof (maxslp)) != sizeof (maxslp)) {
                    400:                cantread("maxslp", kmemf);
                    401:                exit(1);
                    402:        }
                    403:        klseek(kmem, (long)nl[X_CCPU].n_value, 0);
                    404:        if (read(kmem, (char *)&ccpu, sizeof (ccpu)) != sizeof (ccpu)) {
                    405:                cantread("ccpu", kmemf);
                    406:                exit(1);
                    407:        }
                    408:        klseek(kmem, (long)nl[X_ECMX].n_value, 0);
                    409:        if (read(kmem, (char *)&ecmx, sizeof (ecmx)) != sizeof (ecmx)) {
                    410:                cantread("ecmx", kmemf);
                    411:                exit(1);
                    412:        }
                    413:        if (uflg || vflg) {
                    414:                ntext = getw(nl[X_NTEXT].n_value);
                    415:                text = (struct text *)alloc(ntext * sizeof (struct text));
                    416:                if (text == 0) {
                    417:                        fprintf(stderr, "no room for text table\n");
                    418:                        exit(1);
                    419:                }
                    420:                atext = (struct text *)getw(nl[X_TEXT].n_value);
                    421:                klseek(kmem, (long)atext, 0);
                    422:                if (read(kmem, (char *)text, ntext * sizeof (struct text))
                    423:                    != ntext * sizeof (struct text)) {
                    424:                        cantread("text table", kmemf);
                    425:                        exit(1);
                    426:                }
                    427:        }
                    428:        dmmin = getw(nl[X_DMMIN].n_value);
                    429:        dmmax = getw(nl[X_DMMAX].n_value);
                    430: }
                    431: 
                    432: printhdr()
                    433: {
                    434:        char *hdr;
                    435: 
                    436:        if (sflg+lflg+vflg+uflg > 1) {
                    437:                fprintf(stderr, "ps: specify only one of s,l,v and u\n");
                    438:                exit(1);
                    439:        }
                    440:        hdr = lflg ? lhdr : 
                    441:                        (vflg ? vhdr : 
                    442:                                (uflg ? uhdr : shdr));
                    443:        if (lflg+vflg+uflg+sflg == 0)
                    444:                hdr += strlen("SSIZ ");
                    445:        cmdstart = strlen(hdr);
                    446:        printf("%s COMMAND\n", hdr);
                    447:        (void) fflush(stdout);
                    448: }
                    449: 
                    450: cantread(what, fromwhat)
                    451:        char *what, *fromwhat;
                    452: {
                    453: 
                    454:        fprintf(stderr, "ps: error reading %s from %s\n", what, fromwhat);
                    455: }
                    456: 
                    457: struct direct *dbuf;
                    458: int    dialbase;
                    459: 
                    460: getdev()
                    461: {
                    462:        register DIR *df;
                    463: 
                    464:        dialbase = -1;
                    465:        if ((df = opendir(".")) == NULL) {
                    466:                fprintf(stderr, "Can't open . in /dev\n");
                    467:                exit(1);
                    468:        }
                    469:        while ((dbuf = readdir(df)) != NULL) 
                    470:                maybetty();
                    471:        closedir(df);
                    472: }
                    473: 
                    474: /*
                    475:  * Attempt to avoid stats by guessing minor device
                    476:  * numbers from tty names.  Console is known,
                    477:  * know that r(hp|up|mt) are unlikely as are different mem's,
                    478:  * floppy, null, tty, etc.
                    479:  */
                    480: maybetty()
                    481: {
                    482:        register char *cp = dbuf->d_name;
                    483:        register struct ttys *dp;
                    484:        int x;
                    485:        struct stat stb;
                    486: 
                    487:        switch (cp[0]) {
                    488: 
                    489:        case 'c':
                    490:                if (!strcmp(cp, "console")) {
                    491:                        x = 0;
                    492:                        goto donecand;
                    493:                }
                    494:                /* cu[la]? are possible!?! don't rule them out */
                    495:                break;
                    496: 
                    497:        case 'd':
                    498:                if (!strcmp(cp, "drum"))
                    499:                        return;
                    500:                break;
                    501: 
                    502:        case 'f':
                    503:                if (!strcmp(cp, "floppy"))
                    504:                        return;
                    505:                break;
                    506: 
                    507:        case 'k':
                    508:                cp++;
                    509:                if (*cp == 'U')
                    510:                        cp++;
                    511:                goto trymem;
                    512: 
                    513:        case 'r':
                    514:                cp++;
                    515:                if (*cp == 'r' || *cp == 'u' || *cp == 'h')
                    516:                        cp++;
                    517: #define is(a,b) cp[0] == 'a' && cp[1] == 'b'
                    518:                if (is(r,p) || is(u,p) || is(r,k) || is(r,m) || is(m,t)) {
                    519:                        cp += 2;
                    520:                        if (isdigit(*cp) && cp[2] == 0)
                    521:                                return;
                    522:                }
                    523:                break;
                    524: 
                    525:        case 'm':
                    526: trymem:
                    527:                if (cp[0] == 'm' && cp[1] == 'e' && cp[2] == 'm' && cp[3] == 0)
                    528:                        return;
                    529:                if (cp[0] == 'm' && cp[1] == 't')
                    530:                        return;
                    531:                break;
                    532: 
                    533:        case 'n':
                    534:                if (!strcmp(cp, "null"))
                    535:                        return;
                    536:                break;
                    537: 
                    538:        case 'v':
                    539:                if ((cp[1] == 'a' || cp[1] == 'p') && isdigit(cp[2]) &&
                    540:                    cp[3] == 0)
                    541:                        return;
                    542:                break;
                    543:        }
                    544:        cp = dbuf->d_name + dbuf->d_namlen - 1;
                    545:        x = 0;
                    546:        if (cp[-1] == 'd') {
                    547:                if (dialbase == -1) {
                    548:                        if (stat("ttyd0", &stb) == 0)
                    549:                                dialbase = stb.st_rdev & 017;
                    550:                        else
                    551:                                dialbase = -2;
                    552:                }
                    553:                if (dialbase == -2)
                    554:                        x = 0;
                    555:                else
                    556:                        x = 11;
                    557:        }
                    558:        if (cp > dbuf->d_name && isdigit(cp[-1]) && isdigit(*cp))
                    559:                x += 10 * (cp[-1] - ' ') + cp[0] - '0';
                    560:        else if (*cp >= 'a' && *cp <= 'f')
                    561:                x += 10 + *cp - 'a';
                    562:        else if (isdigit(*cp))
                    563:                x += *cp - '0';
                    564:        else
                    565:                x = -1;
                    566: donecand:
                    567:        dp = (struct ttys *)alloc(sizeof (struct ttys));
                    568:        (void) strcpy(dp->name, dbuf->d_name);
                    569:        dp->next = allttys;
                    570:        dp->ttyd = -1;
                    571:        allttys = dp;
                    572:        if (x == -1)
                    573:                return;
                    574:        x &= 017;
                    575:        dp->cand = cand[x];
                    576:        cand[x] = dp;
                    577: }
                    578: 
                    579: char *
                    580: gettty()
                    581: {
                    582:        register char *p;
                    583:        register struct ttys *dp;
                    584:        struct stat stb;
                    585:        int x;
                    586: 
                    587:        if (u.u_ttyp == 0)
                    588:                return("?");
                    589:        x = u.u_ttyd & 017;
                    590:        for (dp = cand[x]; dp; dp = dp->cand) {
                    591:                if (dp->ttyd == -1) {
                    592:                        if (stat(dp->name, &stb) == 0 &&
                    593:                           (stb.st_mode&S_IFMT)==S_IFCHR)
                    594:                                dp->ttyd = stb.st_rdev;
                    595:                        else
                    596:                                dp->ttyd = -2;
                    597:                }
                    598:                if (dp->ttyd == u.u_ttyd)
                    599:                        goto found;
                    600:        }
                    601:        /* ick */
                    602:        for (dp = allttys; dp; dp = dp->next) {
                    603:                if (dp->ttyd == -1) {
                    604:                        if (stat(dp->name, &stb) == 0 &&
                    605:                           (stb.st_mode&S_IFMT)==S_IFCHR)
                    606:                                dp->ttyd = stb.st_rdev;
                    607:                        else
                    608:                                dp->ttyd = -2;
                    609:                }
                    610:                if (dp->ttyd == u.u_ttyd)
                    611:                        goto found;
                    612:        }
                    613:        return ("?");
                    614: found:
                    615:        p = dp->name;
                    616:        if (p[0]=='t' && p[1]=='t' && p[2]=='y')
                    617:                p += 3;
                    618:        return (p);
                    619: }
                    620: 
                    621: save()
                    622: {
                    623:        register struct savcom *sp;
                    624:        register struct asav *ap;
                    625:        register char *cp;
                    626:        register struct text *xp;
                    627:        char *ttyp, *cmdp;
                    628: 
                    629:        if (mproc->p_stat != SZOMB && getu() == 0)
                    630:                return;
                    631:        ttyp = gettty();
                    632:        if (xflg == 0 && ttyp[0] == '?' || tptr && strncmp(tptr, ttyp, 2))
                    633:                return;
                    634:        sp = &savcom[npr];
                    635:        cmdp = getcmd();
                    636:        if (cmdp == 0)
                    637:                return;
                    638:        sp->ap = ap = (struct asav *)alloc(sizeof (struct asav));
                    639:        sp->ap->a_cmdp = cmdp;
                    640: #define e(a,b) ap->a = mproc->b
                    641:        e(a_flag, p_flag); e(a_stat, p_stat); e(a_nice, p_nice);
                    642:        e(a_uid, p_uid); e(a_pid, p_pid); e(a_pri, p_pri);
                    643:        e(a_slptime, p_slptime); e(a_time, p_time);
                    644:        ap->a_tty[0] = ttyp[0];
                    645:        ap->a_tty[1] = ttyp[1] ? ttyp[1] : ' ';
                    646:        if (ap->a_stat == SZOMB) {
                    647:                ap->a_cpu = 0;
                    648:        } else {
                    649:                ap->a_size = mproc->p_dsize + mproc->p_ssize;
                    650:                e(a_rss, p_rssize); 
                    651:                ap->a_ttyd = u.u_ttyd;
                    652:                ap->a_cpu = u.u_ru.ru_utime.tv_sec + u.u_ru.ru_stime.tv_sec;
                    653:                if (sumcpu)
                    654:                        ap->a_cpu += u.u_cru.ru_utime.tv_sec + u.u_cru.ru_stime.tv_sec;
                    655:                if (mproc->p_textp && text) {
                    656:                        xp = &text[mproc->p_textp - atext];
                    657:                        ap->a_tsiz = xp->x_size;
                    658:                        ap->a_txtrss = xp->x_rssize;
                    659:                        ap->a_xccount = xp->x_ccount;
                    660:                }
                    661:        }
                    662: #undef e
                    663:        ap->a_maxrss = mproc->p_maxrss;
                    664:        if (lflg) {
                    665:                register struct lsav *lp;
                    666: 
                    667:                sp->s_un.lp = lp = (struct lsav *)alloc(sizeof (struct lsav));
                    668: #define e(a,b) lp->a = mproc->b
                    669:                e(l_ppid, p_ppid); e(l_cpu, p_cpu);
                    670:                if (ap->a_stat != SZOMB)
                    671:                        e(l_wchan, p_wchan);
                    672: #undef e
                    673:                lp->l_addr = pcbpf;
                    674:        } else if (vflg) {
                    675:                register struct vsav *vp;
                    676: 
                    677:                sp->s_un.vp = vp = (struct vsav *)alloc(sizeof (struct vsav));
                    678: #define e(a,b) vp->a = mproc->b
                    679:                if (ap->a_stat != SZOMB) {
                    680:                        e(v_swrss, p_swrss);
                    681:                        vp->v_majflt = u.u_ru.ru_majflt;
                    682:                        if (mproc->p_textp)
                    683:                                vp->v_txtswrss = xp->x_swrss;
                    684:                }
                    685:                vp->v_pctcpu = pcpu();
                    686: #undef e
                    687:        } else if (uflg)
                    688:                sp->s_un.u_pctcpu = pcpu();
                    689:        else if (sflg) {
                    690:                if (ap->a_stat != SZOMB) {
                    691:                        for (cp = (char *)u.u_stack;
                    692:                            cp < &user.upages[UPAGES][0]; )
                    693:                                if (*cp++)
                    694:                                        break;
                    695:                        sp->s_un.s_ssiz = (&user.upages[UPAGES][0] - cp);
                    696:                }
                    697:        }
                    698: 
                    699:        npr++;
                    700: }
                    701: 
                    702: double
                    703: pmem(ap)
                    704:        register struct asav *ap;
                    705: {
                    706:        double fracmem;
                    707:        int szptudot;
                    708: 
                    709:        if ((ap->a_flag&SLOAD) == 0)
                    710:                fracmem = 0.0;
                    711:        else {
                    712:                szptudot = UPAGES + clrnd(ctopt(ap->a_size+ap->a_tsiz));
                    713:                fracmem = ((float)ap->a_rss+szptudot)/CLSIZE/ecmx;
                    714:                if (ap->a_xccount)
                    715:                        fracmem += ((float)ap->a_txtrss)/CLSIZE/
                    716:                            ap->a_xccount/ecmx;
                    717:        }
                    718:        return (100.0 * fracmem);
                    719: }
                    720: 
                    721: double
                    722: pcpu()
                    723: {
                    724:        time_t time;
                    725: 
                    726:        time = mproc->p_time;
                    727:        if (time == 0 || (mproc->p_flag&SLOAD) == 0)
                    728:                return (0.0);
                    729:        if (rawcpu)
                    730:                return (100.0 * mproc->p_pctcpu);
                    731:        return (100.0 * mproc->p_pctcpu / (1.0 - exp(time * log(ccpu))));
                    732: }
                    733: 
                    734: getu()
                    735: {
                    736:        struct pte *pteaddr, apte;
                    737:        struct pte arguutl[UPAGES+CLSIZE];
                    738:        register int i;
                    739:        int ncl, size;
                    740: 
                    741:        size = sflg ? ctob(UPAGES) : sizeof (struct user);
                    742:        if ((mproc->p_flag & SLOAD) == 0) {
                    743:                if (swap < 0)
                    744:                        return (0);
                    745:                (void) lseek(swap, (long)dtob(mproc->p_swaddr), 0);
                    746:                if (read(swap, (char *)&user.user, size) != size) {
                    747:                        fprintf(stderr, "ps: cant read u for pid %d from %s\n",
                    748:                            mproc->p_pid, swapf);
                    749:                        return (0);
                    750:                }
                    751:                pcbpf = 0;
                    752:                argaddr = 0;
                    753:                return (1);
                    754:        }
                    755:        if (kflg)
                    756:                mproc->p_p0br = (struct pte *)mproc->p_p0br;
                    757:        pteaddr = &Usrptma[btokmx(mproc->p_p0br) + mproc->p_szpt - 1];
                    758:        klseek(kmem, (long)pteaddr, 0);
                    759:        if (read(kmem, (char *)&apte, sizeof(apte)) != sizeof(apte)) {
                    760:                printf("ps: cant read indir pte to get u for pid %d from %s\n",
                    761:                    mproc->p_pid, swapf);
                    762:                return (0);
                    763:        }
                    764:        klseek(mem,
                    765:            (long)ctob(apte.pg_pfnum+1) - (UPAGES+CLSIZE) * sizeof (struct pte),
                    766:                0);
                    767:        if (read(mem, (char *)arguutl, sizeof(arguutl)) != sizeof(arguutl)) {
                    768:                printf("ps: cant read page table for u of pid %d from %s\n",
                    769:                    mproc->p_pid, kmemf);
                    770:                return (0);
                    771:        }
                    772:        if (arguutl[0].pg_fod == 0 && arguutl[0].pg_pfnum)
                    773:                argaddr = ctob(arguutl[0].pg_pfnum);
                    774:        else
                    775:                argaddr = 0;
                    776:        pcbpf = arguutl[CLSIZE].pg_pfnum;
                    777:        ncl = (size + NBPG*CLSIZE - 1) / (NBPG*CLSIZE);
                    778:        while (--ncl >= 0) {
                    779:                i = ncl * CLSIZE;
                    780:                klseek(mem, (long)ctob(arguutl[CLSIZE+i].pg_pfnum), 0);
                    781:                if (read(mem, user.upages[i], CLSIZE*NBPG) != CLSIZE*NBPG) {
                    782:                        printf("ps: cant read page %d of u of pid %d from %s\n",
                    783:                            arguutl[CLSIZE+i].pg_pfnum, mproc->p_pid, memf);
                    784:                        return(0);
                    785:                }
                    786:        }
                    787:        return (1);
                    788: }
                    789: 
                    790: char *
                    791: getcmd()
                    792: {
                    793:        char cmdbuf[CLSIZE*NBPG];
                    794:        union {
                    795:                char    argc[CLSIZE*NBPG];
                    796:                int     argi[CLSIZE*NBPG/sizeof (int)];
                    797:        } argspac;
                    798:        register char *cp;
                    799:        register int *ip;
                    800:        char c;
                    801:        int nbad;
                    802:        struct dblock db;
                    803:        char *file;
                    804: 
                    805:        if (mproc->p_stat == SZOMB || mproc->p_flag&(SSYS|SWEXIT))
                    806:                return ("");
                    807:        if (cflg) {
                    808:                (void) strncpy(cmdbuf, u.u_comm, sizeof (u.u_comm));
                    809:                return (savestr(cmdbuf));
                    810:        }
                    811:        if ((mproc->p_flag & SLOAD) == 0 || argaddr == 0) {
                    812:                if (swap < 0)
                    813:                        goto retucomm;
                    814:                vstodb(0, CLSIZE, &u.u_smap, &db, 1);
                    815:                (void) lseek(swap, (long)dtob(db.db_base), 0);
                    816:                if (read(swap, (char *)&argspac, sizeof(argspac))
                    817:                    != sizeof(argspac))
                    818:                        goto bad;
                    819:                file = swapf;
                    820:        } else {
                    821:                klseek(mem, (long)argaddr, 0);
                    822:                if (read(mem, (char *)&argspac, sizeof (argspac))
                    823:                    != sizeof (argspac))
                    824:                        goto bad;
                    825:                file = memf;
                    826:        }
                    827:        ip = &argspac.argi[CLSIZE*NBPG/sizeof (int)];
                    828:        ip -= 2;                /* last arg word and .long 0 */
                    829:        while (*--ip)
                    830:                if (ip == argspac.argi)
                    831:                        goto retucomm;
                    832:        *(char *)ip = ' ';
                    833:        ip++;
                    834:        nbad = 0;
                    835:        for (cp = (char *)ip; cp < &argspac.argc[CLSIZE*NBPG]; cp++) {
                    836:                c = *cp & 0177;
                    837:                if (c == 0)
                    838:                        *cp = ' ';
                    839:                else if (c < ' ' || c > 0176) {
                    840:                        if (++nbad >= 5*(eflg+1)) {
                    841:                                *cp++ = ' ';
                    842:                                break;
                    843:                        }
                    844:                        *cp = '?';
                    845:                } else if (eflg == 0 && c == '=') {
                    846:                        while (*--cp != ' ')
                    847:                                if (cp <= (char *)ip)
                    848:                                        break;
                    849:                        break;
                    850:                }
                    851:        }
                    852:        *cp = 0;
                    853:        while (*--cp == ' ')
                    854:                *cp = 0;
                    855:        cp = (char *)ip;
                    856:        (void) strncpy(cmdbuf, cp, &argspac.argc[CLSIZE*NBPG] - cp);
                    857:        if (cp[0] == '-' || cp[0] == '?' || cp[0] <= ' ') {
                    858:                (void) strcat(cmdbuf, " (");
                    859:                (void) strncat(cmdbuf, u.u_comm, sizeof(u.u_comm));
                    860:                (void) strcat(cmdbuf, ")");
                    861:        }
                    862: /*
                    863:        if (xflg == 0 && gflg == 0 && tptr == 0 && cp[0] == '-')
                    864:                return (0);
                    865: */
                    866:        return (savestr(cmdbuf));
                    867: 
                    868: bad:
                    869:        fprintf(stderr, "ps: error locating command name for pid %d from %s\n",
                    870:            mproc->p_pid, file);
                    871: retucomm:
                    872:        (void) strcpy(cmdbuf, " (");
                    873:        (void) strncat(cmdbuf, u.u_comm, sizeof (u.u_comm));
                    874:        (void) strcat(cmdbuf, ")");
                    875:        return (savestr(cmdbuf));
                    876: }
                    877: 
                    878: char   *lhdr =
                    879: "      F UID   PID  PPID CP PRI NI ADDR  SZ  RSS WCHAN STAT TT  TIME";
                    880: lpr(sp)
                    881:        struct savcom *sp;
                    882: {
                    883:        register struct asav *ap = sp->ap;
                    884:        register struct lsav *lp = sp->s_un.lp;
                    885: 
                    886:        printf("%7x%4d%6u%6u%3d%4d%3d%5x%4d%5d",
                    887:            ap->a_flag, ap->a_uid,
                    888:            ap->a_pid, lp->l_ppid, lp->l_cpu&0377, ap->a_pri-PZERO,
                    889:            ap->a_nice-NZERO, lp->l_addr, pgtok(ap->a_size), pgtok(ap->a_rss));
                    890:        printf(lp->l_wchan ? " %5x" : "      ", (int)lp->l_wchan&0xfffff);
                    891:        printf(" %4.4s ", state(ap));
                    892:        ptty(ap->a_tty);
                    893:        ptime(ap);
                    894: }
                    895: 
                    896: ptty(tp)
                    897:        char *tp;
                    898: {
                    899: 
                    900:        printf("%-2.2s", tp);
                    901: }
                    902: 
                    903: ptime(ap)
                    904:        struct asav *ap;
                    905: {
                    906: 
                    907:        printf("%3ld:%02ld", ap->a_cpu / 60, ap->a_cpu % 60);
                    908: }
                    909: 
                    910: char   *uhdr =
                    911: "USER       PID %CPU %MEM   SZ  RSS TT STAT  TIME";
                    912: upr(sp)
                    913:        struct savcom *sp;
                    914: {
                    915:        register struct asav *ap = sp->ap;
                    916:        int vmsize, rmsize;
                    917: 
                    918:        vmsize = pgtok((ap->a_size + ap->a_tsiz));
                    919:        rmsize = pgtok(ap->a_rss);
                    920:        if (ap->a_xccount)
                    921:                rmsize += pgtok(ap->a_txtrss/ap->a_xccount);
                    922:        printf("%-8.8s %5d%5.1f%5.1f%5d%5d",
                    923:            getname(ap->a_uid), ap->a_pid, sp->s_un.u_pctcpu, pmem(ap),
                    924:            vmsize, rmsize);
                    925:        putchar(' ');
                    926:        ptty(ap->a_tty);
                    927:        printf(" %4.4s", state(ap));
                    928:        ptime(ap);
                    929: }
                    930: 
                    931: char *vhdr =
                    932: " SIZE  PID TT STAT  TIME SL RE PAGEIN SIZE  RSS  LIM TSIZ TRS %CPU %MEM"+5;
                    933: vpr(sp)
                    934:        struct savcom *sp;
                    935: {
                    936:        register struct vsav *vp = sp->s_un.vp;
                    937:        register struct asav *ap = sp->ap;
                    938: 
                    939:        printf("%5u ", ap->a_pid);
                    940:        ptty(ap->a_tty);
                    941:        printf(" %4.4s", state(ap));
                    942:        ptime(ap);
                    943:        printf("%3d%3d%7d%5d%5d",
                    944:           ap->a_slptime > 99 ? 99 : ap-> a_slptime,
                    945:           ap->a_time > 99 ? 99 : ap->a_time, vp->v_majflt,
                    946:           pgtok(ap->a_size), pgtok(ap->a_rss));
                    947:        if (ap->a_maxrss == (RLIM_INFINITY/NBPG))
                    948:                printf("   xx");
                    949:        else
                    950:                printf("%5d", pgtok(ap->a_maxrss));
                    951:        printf("%5d%4d%5.1f%5.1f",
                    952:           pgtok(ap->a_tsiz), pgtok(ap->a_txtrss), vp->v_pctcpu, pmem(ap));
                    953: }
                    954: 
                    955: char   *shdr =
                    956: "SSIZ   PID TT STAT  TIME";
                    957: spr(sp)
                    958:        struct savcom *sp;
                    959: {
                    960:        register struct asav *ap = sp->ap;
                    961: 
                    962:        if (sflg)
                    963:                printf("%4d ", sp->s_un.s_ssiz);
                    964:        printf("%5u", ap->a_pid);
                    965:        putchar(' ');
                    966:        ptty(ap->a_tty);
                    967:        printf(" %4.4s", state(ap));
                    968:        ptime(ap);
                    969: }
                    970: 
                    971: char *
                    972: state(ap)
                    973:        register struct asav *ap;
                    974: {
                    975:        char stat, load, nice, anom;
                    976:        static char res[5];
                    977: 
                    978:        switch (ap->a_stat) {
                    979: 
                    980:        case SSTOP:
                    981:                stat = 'T';
                    982:                break;
                    983: 
                    984:        case SSLEEP:
                    985:                if (ap->a_pri >= PZERO)
                    986:                        if (ap->a_slptime >= MAXSLP)
                    987:                                stat = 'I';
                    988:                        else
                    989:                                stat = 'S';
                    990:                else if (ap->a_flag & SPAGE)
                    991:                        stat = 'P';
                    992:                else
                    993:                        stat = 'D';
                    994:                break;
                    995: 
                    996:        case SWAIT:
                    997:        case SRUN:
                    998:        case SIDL:
                    999:                stat = 'R';
                   1000:                break;
                   1001: 
                   1002:        case SZOMB:
                   1003:                stat = 'Z';
                   1004:                break;
                   1005: 
                   1006:        default:
                   1007:                stat = '?';
                   1008:        }
                   1009:        load = ap->a_flag & SLOAD ? (ap->a_rss>ap->a_maxrss ? '>' : ' ') : 'W';
                   1010:        if (ap->a_nice < NZERO)
                   1011:                nice = '<';
                   1012:        else if (ap->a_nice > NZERO)
                   1013:                nice = 'N';
                   1014:        else
                   1015:                nice = ' ';
                   1016:        anom = (ap->a_flag&SUANOM) ? 'A' : ((ap->a_flag&SSEQL) ? 'S' : ' ');
                   1017:        res[0] = stat; res[1] = load; res[2] = nice; res[3] = anom;
                   1018:        return (res);
                   1019: }
                   1020: 
                   1021: /*
                   1022:  * Given a base/size pair in virtual swap area,
                   1023:  * return a physical base/size pair which is the
                   1024:  * (largest) initial, physically contiguous block.
                   1025:  */
                   1026: vstodb(vsbase, vssize, dmp, dbp, rev)
                   1027:        register int vsbase;
                   1028:        int vssize;
                   1029:        struct dmap *dmp;
                   1030:        register struct dblock *dbp;
                   1031: {
                   1032:        register int blk = dmmin;
                   1033:        register swblk_t *ip = dmp->dm_map;
                   1034: 
                   1035:        vsbase = ctod(vsbase);
                   1036:        vssize = ctod(vssize);
                   1037:        if (vsbase < 0 || vsbase + vssize > dmp->dm_size)
                   1038:                panic("vstodb");
                   1039:        while (vsbase >= blk) {
                   1040:                vsbase -= blk;
                   1041:                if (blk < dmmax)
                   1042:                        blk *= 2;
                   1043:                ip++;
                   1044:        }
                   1045:        if (*ip <= 0 || *ip + blk > nswap)
                   1046:                panic("vstodb *ip");
                   1047:        dbp->db_size = min(vssize, blk - vsbase);
                   1048:        dbp->db_base = *ip + (rev ? blk - (vsbase + dbp->db_size) : vsbase);
                   1049: }
                   1050: 
                   1051: /*ARGSUSED*/
                   1052: panic(cp)
                   1053:        char *cp;
                   1054: {
                   1055: 
                   1056: #ifdef DEBUG
                   1057:        printf("%s\n", cp);
                   1058: #endif
                   1059: }
                   1060: 
                   1061: min(a, b)
                   1062: {
                   1063: 
                   1064:        return (a < b ? a : b);
                   1065: }
                   1066: 
                   1067: pscomp(s1, s2)
                   1068:        struct savcom *s1, *s2;
                   1069: {
                   1070:        register int i;
                   1071: 
                   1072:        if (uflg)
                   1073:                return (s2->s_un.u_pctcpu > s1->s_un.u_pctcpu ? 1 : -1);
                   1074:        if (vflg)
                   1075:                return (vsize(s2) - vsize(s1));
                   1076:        i = s1->ap->a_ttyd - s2->ap->a_ttyd;
                   1077:        if (i == 0)
                   1078:                i = s1->ap->a_pid - s2->ap->a_pid;
                   1079:        return (i);
                   1080: }
                   1081: 
                   1082: vsize(sp)
                   1083:        struct savcom *sp;
                   1084: {
                   1085:        register struct asav *ap = sp->ap;
                   1086:        register struct vsav *vp = sp->s_un.vp;
                   1087:        
                   1088:        if (ap->a_flag & SLOAD)
                   1089:                return (ap->a_rss +
                   1090:                    ap->a_txtrss / (ap->a_xccount ? ap->a_xccount : 1));
                   1091:        return (vp->v_swrss + (ap->a_xccount ? 0 : vp->v_txtswrss));
                   1092: }
                   1093: 
                   1094: #define        NMAX    8       /* sizeof loginname (should be sizeof (utmp.ut_name)) */
                   1095: #define NUID   2048    /* must not be a multiple of 5 */
                   1096: 
                   1097: struct nametable {
                   1098:        char    nt_name[NMAX+1];
                   1099:        int     nt_uid;
                   1100: } nametable[NUID];
                   1101: 
                   1102: struct nametable *
                   1103: findslot(uid)
                   1104: unsigned short uid;
                   1105: {
                   1106:        register struct nametable       *n, *start;
                   1107: 
                   1108:        /*
                   1109:         * find the uid or an empty slot.
                   1110:         * return NULL if neither found.
                   1111:         */
                   1112: 
                   1113:        n = start = nametable + (uid % (NUID - 20));
                   1114:        while (n->nt_name[0] && n->nt_uid != uid) {
                   1115:                if ((n += 5) >= &nametable[NUID])
                   1116:                        n -= NUID;
                   1117:                if (n == start)
                   1118:                        return((struct nametable *)NULL);
                   1119:        }
                   1120:        return(n);
                   1121: }
                   1122: 
                   1123: char *
                   1124: getname(uid)
                   1125: {
                   1126:        register struct passwd          *pw;
                   1127:        static                          init = 0;
                   1128:        struct passwd                   *getpwent();
                   1129:        register struct nametable       *n;
                   1130: 
                   1131:        /*
                   1132:         * find uid in hashed table; add it if not found.
                   1133:         * return pointer to name.
                   1134:         */
                   1135: 
                   1136:        if ((n = findslot(uid)) == NULL)
                   1137:                return((char *)NULL);
                   1138: 
                   1139:        if (n->nt_name[0])      /* occupied? */
                   1140:                return(n->nt_name);
                   1141: 
                   1142:        switch (init) {
                   1143:                case 0:
                   1144:                        setpwent();
                   1145:                        init = 1;
                   1146:                        /* intentional fall-thru */
                   1147:                case 1:
                   1148:                        while (pw = getpwent()) {
                   1149:                                if (pw->pw_uid < 0)
                   1150:                                        continue;
                   1151:                                if ((n = findslot(pw->pw_uid)) == NULL) {
                   1152:                                        endpwent();
                   1153:                                        init = 2;
                   1154:                                        return((char *)NULL);
                   1155:                                }
                   1156:                                if (n->nt_name[0])
                   1157:                                        continue;       /* duplicate, not uid */
                   1158:                                strncpy(n->nt_name, pw->pw_name, NMAX);
                   1159:                                n->nt_uid = pw->pw_uid;
                   1160:                                if (pw->pw_uid == uid)
                   1161:                                        return (n->nt_name);
                   1162:                        }
                   1163:                        endpwent();
                   1164:                        init = 2;
                   1165:                        /* intentional fall-thru */
                   1166:                case 2:
                   1167:                        return ((char *)NULL);
                   1168:        }
                   1169: }
                   1170: 
                   1171: char   *freebase;
                   1172: int    nleft;
                   1173: 
                   1174: char *
                   1175: alloc(size)
                   1176:        int size;
                   1177: {
                   1178:        register char *cp;
                   1179:        register int i;
                   1180: 
                   1181: #ifdef sun
                   1182:        size = (size+1)&~1;
                   1183: #endif
                   1184:        if (size > nleft) {
                   1185:                freebase = (char *)sbrk((int)(i = size > 2048 ? size : 2048));
                   1186:                if (freebase == (char *)-1) {
                   1187:                        fprintf(stderr, "ps: ran out of memory\n");
                   1188:                        exit(1);
                   1189:                }
                   1190:                nleft = i - size;
                   1191:        } else
                   1192:                nleft -= size;
                   1193:        cp = freebase;
                   1194:        for (i = size; --i >= 0; )
                   1195:                *cp++ = 0;
                   1196:        freebase = cp;
                   1197:        return (cp - size);
                   1198: }
                   1199: 
                   1200: char *
                   1201: savestr(cp)
                   1202:        char *cp;
                   1203: {
                   1204:        register int len;
                   1205:        register char *dp;
                   1206: 
                   1207:        len = strlen(cp);
                   1208:        dp = (char *)alloc((len+1+3) & ~3);
                   1209:        (void) strcpy(dp, cp);
                   1210:        return (dp);
                   1211: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.