xref: /original-bsd/sbin/dmesg/dmesg.c (revision 53787e02)
1 /*
2  * Copyright (c) 1980 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  */
6 
7 #ifndef lint
8 static char sccsid[] = "@(#)dmesg.c	5.1 (Berkeley) 06/05/85";
9 #endif not lint
10 
11 /*
12  *	Suck up system messages
13  *	dmesg
14  *		print current buffer
15  *	dmesg -
16  *		print and update incremental history
17  */
18 
19 #include <stdio.h>
20 #include <sys/param.h>
21 #include <nlist.h>
22 #include <signal.h>
23 #include <sys/vm.h>
24 #include <sys/msgbuf.h>
25 
26 struct	msgbuf msgbuf;
27 char	*msgbufp;
28 int	sflg;
29 int	of	= -1;
30 
31 struct	msgbuf omesg;
32 struct	nlist nl[2] = {
33 	{ "_msgbuf" },
34 	{ "" }
35 };
36 
37 main(argc, argv)
38 char **argv;
39 {
40 	int mem;
41 	register char *mp, *omp, *mstart;
42 	int timeout();
43 	int samef;
44 
45 	signal(SIGALRM, timeout);
46 	alarm(30);
47 	if (argc>1 && argv[1][0] == '-') {
48 		sflg++;
49 		argc--;
50 		argv++;
51 	}
52 	if (sflg) {
53 		of = open("/usr/adm/msgbuf", 2);
54 		if (of < 0)
55 			done("Can't open /usr/adm/msgbuf\n");
56 		read(of, (char *)&omesg, sizeof(omesg));
57 		lseek(of, 0L, 0);
58 	}
59 	sflg = 0;
60 	nlist(argc>2? argv[2]:"/vmunix", nl);
61 	if (nl[0].n_type==0)
62 		done("Can't get kernel namelist\n");
63 	if ((mem = open((argc>1? argv[1]: "/dev/kmem"), 0)) < 0)
64 		done("Can't read kernel memory\n");
65 	lseek(mem, (long)nl[0].n_value, 0);
66 	read(mem, &msgbuf, sizeof (msgbuf));
67 	if (msgbuf.msg_magic != MSG_MAGIC)
68 		done("Magic number wrong (namelist mismatch?)\n");
69 	mstart = &msgbuf.msg_bufc[omesg.msg_bufx];
70 	omp = &omesg.msg_bufc[msgbuf.msg_bufx];
71 	mp = msgbufp = &msgbuf.msg_bufc[msgbuf.msg_bufx];
72 	samef = 1;
73 	do {
74 		if (*mp++ != *omp++) {
75 			mstart = msgbufp;
76 			samef = 0;
77 			pdate();
78 			printf("...\n");
79 			break;
80 		}
81 		if (mp == &msgbuf.msg_bufc[MSG_BSIZE])
82 			mp = msgbuf.msg_bufc;
83 		if (omp == &omesg.msg_bufc[MSG_BSIZE])
84 			omp = omesg.msg_bufc;
85 	} while (mp != mstart);
86 	if (samef && omesg.msg_bufx == msgbuf.msg_bufx)
87 		exit(0);
88 	mp = mstart;
89 	do {
90 		pdate();
91 		if (*mp && (*mp & 0200) == 0)
92 			putchar(*mp);
93 		mp++;
94 		if (mp == &msgbuf.msg_bufc[MSG_BSIZE])
95 			mp = msgbuf.msg_bufc;
96 	} while (mp != msgbufp);
97 	done((char *)NULL);
98 }
99 
100 done(s)
101 char *s;
102 {
103 	register char *p, *q;
104 
105 	if (s && s!=(char *)omesg.msg_magic && sflg==0) {
106 		pdate();
107 		printf(s);
108 	}
109 	write(of, (char *)&msgbuf, sizeof(msgbuf));
110 	exit(s!=NULL);
111 }
112 
113 pdate()
114 {
115 	extern char *ctime();
116 	static firstime;
117 	time_t tbuf;
118 
119 	if (firstime==0) {
120 		firstime++;
121 		time(&tbuf);
122 		printf("\n%.12s\n", ctime(&tbuf)+4);
123 	}
124 }
125 
126 timeout()
127 {
128 	done("Buffer file screwed up\n");
129 }
130