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

unix.superglobalmegacorp.com

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