| 1 | static char *sccsid = "@(#)dmesg.c 4.1 (Berkeley) 82/03/01"; |
| 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 of = -1; |
| 21 | |
| 22 | struct msgbuf omesg; |
| 23 | struct nlist nl[2] = { |
| 24 | { "_msgbuf" }, |
| 25 | { 0 } |
| 26 | }; |
| 27 | |
| 28 | main(argc, argv) |
| 29 | char **argv; |
| 30 | { |
| 31 | int mem; |
| 32 | register char *mp, *omp, *mstart; |
| 33 | int timeout(); |
| 34 | int samef; |
| 35 | |
| 36 | signal(SIGALRM, timeout); |
| 37 | alarm(30); |
| 38 | if (argc>1 && argv[1][0] == '-') { |
| 39 | sflg++; |
| 40 | argc--; |
| 41 | argv++; |
| 42 | } |
| 43 | if (sflg) { |
| 44 | of = open("/usr/adm/msgbuf", 2); |
| 45 | read(of, (char *)&omesg, sizeof(omesg)); |
| 46 | lseek(of, 0L, 0); |
| 47 | } |
| 48 | sflg = 0; |
| 49 | nlist(argc>2? argv[2]:"/vmunix", nl); |
| 50 | if (nl[0].n_type==0) |
| 51 | done("No namelist\n"); |
| 52 | if ((mem = open((argc>1? argv[1]: "/dev/kmem"), 0)) < 0) |
| 53 | done("No mem\n"); |
| 54 | lseek(mem, (long)nl[0].n_value, 0); |
| 55 | read(mem, &msgbuf, sizeof (msgbuf)); |
| 56 | if (msgbuf.msg_magic != MSG_MAGIC) |
| 57 | done("Magic number wrong (namelist mismatch?)\n"); |
| 58 | mstart = &msgbuf.msg_bufc[omesg.msg_bufx]; |
| 59 | omp = &omesg.msg_bufc[msgbuf.msg_bufx]; |
| 60 | mp = msgbufp = &msgbuf.msg_bufc[msgbuf.msg_bufx]; |
| 61 | samef = 1; |
| 62 | do { |
| 63 | if (*mp++ != *omp++) { |
| 64 | mstart = msgbufp; |
| 65 | samef = 0; |
| 66 | pdate(); |
| 67 | printf("...\n"); |
| 68 | break; |
| 69 | } |
| 70 | if (mp == &msgbuf.msg_bufc[MSG_BSIZE]) |
| 71 | mp = msgbuf.msg_bufc; |
| 72 | if (omp == &omesg.msg_bufc[MSG_BSIZE]) |
| 73 | omp = omesg.msg_bufc; |
| 74 | } while (mp != mstart); |
| 75 | if (samef && omesg.msg_bufx == msgbuf.msg_bufx) |
| 76 | exit(0); |
| 77 | mp = mstart; |
| 78 | do { |
| 79 | pdate(); |
| 80 | if (*mp && (*mp & 0200) == 0) |
| 81 | putchar(*mp); |
| 82 | mp++; |
| 83 | if (mp == &msgbuf.msg_bufc[MSG_BSIZE]) |
| 84 | mp = msgbuf.msg_bufc; |
| 85 | } while (mp != msgbufp); |
| 86 | done((char *)NULL); |
| 87 | } |
| 88 | |
| 89 | done(s) |
| 90 | char *s; |
| 91 | { |
| 92 | register char *p, *q; |
| 93 | |
| 94 | if (s && s!=(char *)omesg.msg_magic && sflg==0) { |
| 95 | pdate(); |
| 96 | printf(s); |
| 97 | } |
| 98 | write(of, (char *)&msgbuf, sizeof(msgbuf)); |
| 99 | exit(s!=NULL); |
| 100 | } |
| 101 | |
| 102 | pdate() |
| 103 | { |
| 104 | extern char *ctime(); |
| 105 | static firstime; |
| 106 | time_t tbuf; |
| 107 | |
| 108 | if (firstime==0) { |
| 109 | firstime++; |
| 110 | time(&tbuf); |
| 111 | printf("\n%.12s\n", ctime(&tbuf)+4); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | timeout() |
| 116 | { |
| 117 | done("Buffer file screwed up\n"); |
| 118 | } |