|
|
1.1 root 1: /*
2: * Suck up system messages
3: * dmesg
4: * print current buffer
5: * dmesg -
6: * print and update incremental history
7: */
8:
9: #include <stdio.h>
10: #include <libc.h>
11: #include <sys/param.h>
12: #include <nlist.h>
13: #include <signal.h>
14: #include <sys/msgbuf.h>
15:
16: long msgptr;
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: int timeout();
35:
36: signal(SIGALRM, timeout);
37: alarm(60);
38: if (argc>1 && argv[1][0] == '-') {
39: sflg++;
40: switch (argv[1][1]) {
41: default: /* ugh */
42: wflg++;
43: break;
44:
45: case 'i':
46: break;
47: }
48: argc--;
49: argv++;
50: }
51: if (sflg)
52: openbuf();
53: sflg = 0;
54: timeout("can't read namelist\n");
55: nlist(argc>2? argv[2]:"/unix", nl);
56: if (nl[0].n_type==0)
57: done("No namelist\n");
58: if ((mem = open((argc>1? argv[1]: "/dev/kmem"), 0)) < 0)
59: done("No /dev/kmem\n");
60: lseek(mem, (long)nl[0].n_value, 0);
61: read(mem, &msgptr, sizeof(msgptr));
62: lseek(mem, msgptr, 0);
63: read(mem, &msgbuf, sizeof (msgbuf));
64: if (msgbuf.msg_magic != MSG_MAGIC)
65: done("Magic number wrong (namelist mismatch?)\n");
66: if (bufdiff(&omesg, &msgbuf) == 0)
67: exit(0);
68: prdiff(&omesg, &msgbuf);
69: done((char *)NULL);
70: }
71:
72: bufdiff(om, nm)
73: register struct msgbuf *om, *nm;
74: {
75:
76: if (om->msg_bufx != nm->msg_bufx)
77: return (1);
78: return (memcmp(om->msg_bufc, nm->msg_bufc, MSG_BSIZE));
79: }
80:
81: prdiff(om, nm)
82: register struct msgbuf *om, *nm;
83: {
84: register int ox, nx;
85: register int c;
86:
87: if ((ox = om->msg_bufx) < 0 || ox >= MSG_BSIZE)
88: ox = 0;
89: if ((nx = nm->msg_bufx) < 0 || nx >= MSG_BSIZE)
90: nx = 0;
91: pdate();
92: /*
93: * did we lose something?
94: */
95: if (ox < nx) {
96: if (memcmp(&om->msg_bufc[0], &nm->msg_bufc[0], ox)
97: || memcmp(&om->msg_bufc[nx], &nm->msg_bufc[nx], MSG_BSIZE - nx)) {
98: ox = nx;
99: printf("...\n");
100: }
101: } else if (ox > nx) {
102: if (memcmp(&om->msg_bufc[ox], &nm->msg_bufc[ox], nx - ox)) {
103: ox = nx;
104: printf("...\n");
105: }
106: }
107: /*
108: * else ox == nx; something might be lost, but assume not
109: * now print the new part: from ox to nx-1
110: */
111: do {
112: c = nm->msg_bufc[ox++];
113: if ((c & 0200) == 0 && c != 0) /* sanity */
114: putchar(c);
115: if (ox >= MSG_BSIZE)
116: ox = 0;
117: } while (ox != nx);
118: }
119:
120: done(s)
121: char *s;
122: {
123:
124: if (s && s!=(char *)omesg.msg_magic && sflg==0) {
125: pdate();
126: printf(s);
127: }
128: if (wflg) {
129: wflg = 0;
130: writebuf();
131: }
132: exit(s!=NULL);
133: }
134:
135: openbuf()
136: {
137: int f;
138:
139: timeout("can't read buffer file\n");
140: if ((f = open(BUFFER, 0)) < 0)
141: return;
142: if (read(f, (char *)&omesg, sizeof(omesg)) != sizeof(omesg)
143: || omesg.msg_magic != MSG_MAGIC)
144: memset((char *)&omesg, sizeof(omesg), 0);
145: close(f);
146: }
147:
148: writebuf()
149: {
150: int f;
151:
152: timeout("can't write buffer file\n");
153: if ((f = open(BUFFER, 1)) < 0)
154: done("can't open buffer\n");
155: if (write(f, (char *)&msgbuf, sizeof(msgbuf)) != sizeof(msgbuf))
156: done("error writing buffer\n");
157: close(f);
158: }
159:
160: pdate()
161: {
162: extern char *ctime();
163: static firstime;
164: time_t tbuf;
165:
166: if (firstime==0) {
167: firstime++;
168: time(&tbuf);
169: printf("\n%.12s\n", ctime(&tbuf)+4);
170: }
171: }
172:
173: char *terr;
174:
175: timeout(s)
176: char *s;
177: {
178: extern ttrap();
179:
180: terr = s;
181: signal(SIGALRM, ttrap);
182: alarm(60);
183: }
184:
185: ttrap()
186: {
187: char buf[100];
188:
189: sprintf(buf, "timeout: %s", terr);
190: done(buf);
191: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.