Commit | Line | Data |
---|---|---|
76797561 DF |
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 | |
fa060fb0 | 8 | static char sccsid[] = "@(#)dmesg.c 5.7 (Berkeley) %G%"; |
76797561 DF |
9 | #endif not lint |
10 | ||
34a7981f BJ |
11 | /* |
12 | * Suck up system messages | |
13 | * dmesg | |
14 | * print current buffer | |
15 | * dmesg - | |
16 | * print and update incremental history | |
17 | */ | |
18 | ||
34a7981f | 19 | #include <sys/param.h> |
a5d34734 | 20 | #include <sys/signal.h> |
3b294534 | 21 | #include <sys/file.h> |
34a7981f BJ |
22 | #include <sys/vm.h> |
23 | #include <sys/msgbuf.h> | |
a5d34734 KB |
24 | #include <nlist.h> |
25 | #include <stdio.h> | |
26 | #include "pathnames.h" | |
34a7981f BJ |
27 | |
28 | struct msgbuf msgbuf; | |
29 | char *msgbufp; | |
30 | int sflg; | |
31 | int of = -1; | |
32 | ||
33 | struct msgbuf omesg; | |
34 | struct nlist nl[2] = { | |
35 | { "_msgbuf" }, | |
c10ca4d6 | 36 | { "" } |
34a7981f BJ |
37 | }; |
38 | ||
39 | main(argc, argv) | |
40 | char **argv; | |
41 | { | |
42 | int mem; | |
43 | register char *mp, *omp, *mstart; | |
0c7abeeb | 44 | int samef, sawnl, ignore = 0; |
34a7981f | 45 | |
34a7981f BJ |
46 | if (argc>1 && argv[1][0] == '-') { |
47 | sflg++; | |
48 | argc--; | |
49 | argv++; | |
50 | } | |
51 | if (sflg) { | |
a5d34734 | 52 | of = open(_PATH_MSGBUF, O_RDWR | O_CREAT, 0644); |
c118b033 | 53 | if (of < 0) |
a5d34734 | 54 | done("Can't open msgbuf file\n"); |
34a7981f BJ |
55 | read(of, (char *)&omesg, sizeof(omesg)); |
56 | lseek(of, 0L, 0); | |
57 | } | |
58 | sflg = 0; | |
fa060fb0 | 59 | nlist(argc>2? argv[2]:_PATH_UNIX, nl); |
34a7981f | 60 | if (nl[0].n_type==0) |
c118b033 | 61 | done("Can't get kernel namelist\n"); |
a5d34734 | 62 | if ((mem = open((argc>1? argv[1]: _PATH_KMEM), 0)) < 0) |
c118b033 | 63 | done("Can't read kernel memory\n"); |
34a7981f BJ |
64 | lseek(mem, (long)nl[0].n_value, 0); |
65 | read(mem, &msgbuf, sizeof (msgbuf)); | |
66 | if (msgbuf.msg_magic != MSG_MAGIC) | |
67 | done("Magic number wrong (namelist mismatch?)\n"); | |
ba25830c MK |
68 | if (msgbuf.msg_bufx >= MSG_BSIZE) |
69 | msgbuf.msg_bufx = 0; | |
43a11e0e MK |
70 | if (omesg.msg_bufx >= MSG_BSIZE) |
71 | omesg.msg_bufx = 0; | |
34a7981f BJ |
72 | mstart = &msgbuf.msg_bufc[omesg.msg_bufx]; |
73 | omp = &omesg.msg_bufc[msgbuf.msg_bufx]; | |
74 | mp = msgbufp = &msgbuf.msg_bufc[msgbuf.msg_bufx]; | |
75 | samef = 1; | |
76 | do { | |
77 | if (*mp++ != *omp++) { | |
78 | mstart = msgbufp; | |
79 | samef = 0; | |
80 | pdate(); | |
81 | printf("...\n"); | |
82 | break; | |
83 | } | |
ba25830c | 84 | if (mp >= &msgbuf.msg_bufc[MSG_BSIZE]) |
34a7981f | 85 | mp = msgbuf.msg_bufc; |
ba25830c | 86 | if (omp >= &omesg.msg_bufc[MSG_BSIZE]) |
34a7981f BJ |
87 | omp = omesg.msg_bufc; |
88 | } while (mp != mstart); | |
89 | if (samef && omesg.msg_bufx == msgbuf.msg_bufx) | |
90 | exit(0); | |
91 | mp = mstart; | |
3b294534 MK |
92 | pdate(); |
93 | sawnl = 1; | |
34a7981f | 94 | do { |
3b294534 MK |
95 | if (sawnl && *mp == '<') |
96 | ignore = 1; | |
97 | if (*mp && (*mp & 0200) == 0 && !ignore) | |
34a7981f | 98 | putchar(*mp); |
3b294534 MK |
99 | if (ignore && *mp == '>') |
100 | ignore = 0; | |
101 | sawnl = (*mp == '\n'); | |
34a7981f | 102 | mp++; |
ba25830c | 103 | if (mp >= &msgbuf.msg_bufc[MSG_BSIZE]) |
34a7981f BJ |
104 | mp = msgbuf.msg_bufc; |
105 | } while (mp != msgbufp); | |
106 | done((char *)NULL); | |
107 | } | |
108 | ||
109 | done(s) | |
110 | char *s; | |
111 | { | |
3b294534 | 112 | if (s) { |
34a7981f BJ |
113 | pdate(); |
114 | printf(s); | |
3b294534 MK |
115 | } else if (of != -1) |
116 | write(of, (char *)&msgbuf, sizeof(msgbuf)); | |
34a7981f BJ |
117 | exit(s!=NULL); |
118 | } | |
119 | ||
120 | pdate() | |
121 | { | |
122 | extern char *ctime(); | |
123 | static firstime; | |
124 | time_t tbuf; | |
125 | ||
126 | if (firstime==0) { | |
127 | firstime++; | |
128 | time(&tbuf); | |
129 | printf("\n%.12s\n", ctime(&tbuf)+4); | |
130 | } | |
131 | } |