Annotation of researchv8dc/cmd/dmesg/dmesg.c, revision 1.1

1.1     ! root        1: static char *sccsid = "@(#)dmesg.c     4.3 (Berkeley) 2/28/81";
        !             2: /*
        !             3:  *     Suck up system messages
        !             4:  *     dmesg
        !             5:  *             print current buffer
        !             6:  *     dmesg -
        !             7:  *             print and update incremental history
        !             8:  */
        !             9: 
        !            10: #include <stdio.h>
        !            11: #include <sys/param.h>
        !            12: #include <nlist.h>
        !            13: #include <signal.h>
        !            14: #include <sys/vm.h>
        !            15: #include <sys/msgbuf.h>
        !            16: 
        !            17: struct msgbuf msgbuf;
        !            18: char   *msgbufp;
        !            19: int    sflg;
        !            20: int    wflg;
        !            21: 
        !            22: struct msgbuf omesg;
        !            23: struct nlist nl[2] = {
        !            24:        { "_msgbuf" },
        !            25:        { 0 }
        !            26: };
        !            27: 
        !            28: #define        BUFFER  "/usr/adm/msgbuf"
        !            29: 
        !            30: main(argc, argv)
        !            31: char **argv;
        !            32: {
        !            33:        int mem;
        !            34:        register char *mp, *omp, *mstart;
        !            35:        int timeout();
        !            36:        int samef;
        !            37: 
        !            38:        signal(SIGALRM, timeout);
        !            39:        alarm(60);
        !            40:        if (argc>1 && argv[1][0] == '-') {
        !            41:                sflg++;
        !            42:                switch (argv[1][1]) {
        !            43:                default:                /* ugh */
        !            44:                        wflg++;
        !            45:                        break;
        !            46: 
        !            47:                case 'i':
        !            48:                        break;
        !            49:                }
        !            50:                argc--;
        !            51:                argv++;
        !            52:        }
        !            53:        if (sflg)
        !            54:                openbuf();
        !            55:        sflg = 0;
        !            56:        timeout("can't read namelist\n");
        !            57:        nlist(argc>2? argv[2]:"/unix", nl);
        !            58:        if (nl[0].n_type==0)
        !            59:                done("No namelist\n");
        !            60:        if ((mem = open((argc>1? argv[1]: "/dev/kmem"), 0)) < 0)
        !            61:                done("No /dev/kmem\n");
        !            62:        lseek(mem, (long)nl[0].n_value, 0);
        !            63:        read(mem, &msgbuf, sizeof (msgbuf));
        !            64:        if (msgbuf.msg_magic != MSG_MAGIC)
        !            65:                done("Magic number wrong (namelist mismatch?)\n");
        !            66:        mstart = &msgbuf.msg_bufc[omesg.msg_bufx];
        !            67:        omp = &omesg.msg_bufc[msgbuf.msg_bufx];
        !            68:        mp = msgbufp = &msgbuf.msg_bufc[msgbuf.msg_bufx];
        !            69:        samef = 1;
        !            70:        do {
        !            71:                if (*mp++ != *omp++) {
        !            72:                        mstart = msgbufp;
        !            73:                        samef = 0;
        !            74:                        pdate();
        !            75:                        printf("...\n");
        !            76:                        break;
        !            77:                }
        !            78:                if (mp == &msgbuf.msg_bufc[MSG_BSIZE])
        !            79:                        mp = msgbuf.msg_bufc;
        !            80:                if (omp == &omesg.msg_bufc[MSG_BSIZE])
        !            81:                        omp = omesg.msg_bufc;
        !            82:        } while (mp != mstart);
        !            83:        if (samef && omesg.msg_bufx == msgbuf.msg_bufx)
        !            84:                exit(0);
        !            85:        mp = mstart;
        !            86:        do {
        !            87:                pdate();
        !            88:                if (*mp && (*mp & 0200) == 0)
        !            89:                        putchar(*mp);
        !            90:                mp++;
        !            91:                if (mp == &msgbuf.msg_bufc[MSG_BSIZE])
        !            92:                        mp = msgbuf.msg_bufc;
        !            93:        } while (mp != msgbufp);
        !            94:        done((char *)NULL);
        !            95: }
        !            96: 
        !            97: done(s)
        !            98: char *s;
        !            99: {
        !           100:        register char *p, *q;
        !           101: 
        !           102:        if (s && s!=(char *)omesg.msg_magic && sflg==0) {
        !           103:                pdate();
        !           104:                printf(s);
        !           105:        }
        !           106:        if (wflg)
        !           107:                writebuf();
        !           108:        exit(s!=NULL);
        !           109: }
        !           110: 
        !           111: openbuf()
        !           112: {
        !           113:        int f;
        !           114: 
        !           115:        timeout("can't read buffer file\n");
        !           116:        if ((f = open(BUFFER, 0)) < 0)
        !           117:                return;
        !           118:        if (read(f, (char *)&omesg, sizeof(omesg)) != sizeof(omesg)
        !           119:        ||  omesg.msg_magic != MSG_MAGIC)
        !           120:                zero((char *)&omesg, sizeof(omesg));
        !           121:        close(f);
        !           122: }
        !           123: 
        !           124: writebuf()
        !           125: {
        !           126:        int f;
        !           127: 
        !           128:        timeout("can't write buffer file\n");
        !           129:        if ((f = open(BUFFER, 1)) < 0)
        !           130:                done("can't open buffer\n");
        !           131:        if (write(f, (char *)&msgbuf, sizeof(msgbuf)) != sizeof(msgbuf))
        !           132:                done("error writing buffer\n");
        !           133:        close(f);
        !           134: }
        !           135: 
        !           136: pdate()
        !           137: {
        !           138:        extern char *ctime();
        !           139:        static firstime;
        !           140:        time_t tbuf;
        !           141: 
        !           142:        if (firstime==0) {
        !           143:                firstime++;
        !           144:                time(&tbuf);
        !           145:                printf("\n%.12s\n", ctime(&tbuf)+4);
        !           146:        }
        !           147: }
        !           148: 
        !           149: zero(b, n)
        !           150: register char *b;
        !           151: register int n;
        !           152: {
        !           153: 
        !           154:        while (--n >= 0)
        !           155:                *b++ = 0;
        !           156: }
        !           157: 
        !           158: char *terr;
        !           159: 
        !           160: timeout(s)
        !           161: char *s;
        !           162: {
        !           163:        extern ttrap();
        !           164: 
        !           165:        terr = s;
        !           166:        signal(SIGALRM, ttrap);
        !           167:        alarm(60);
        !           168: }
        !           169: 
        !           170: ttrap()
        !           171: {
        !           172:        char buf[100];
        !           173: 
        !           174:        sprintf(buf, "timeout: %s", terr);
        !           175:        done(buf);
        !           176: }

unix.superglobalmegacorp.com

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