1 |
|
|
/* $OpenBSD: dmesg.c,v 1.29 2017/09/01 07:31:45 tom Exp $ */ |
2 |
|
|
/* $NetBSD: dmesg.c,v 1.8 1995/03/18 14:54:49 cgd Exp $ */ |
3 |
|
|
|
4 |
|
|
/*- |
5 |
|
|
* Copyright (c) 1991, 1993 |
6 |
|
|
* The Regents of the University of California. All rights reserved. |
7 |
|
|
* |
8 |
|
|
* Redistribution and use in source and binary forms, with or without |
9 |
|
|
* modification, are permitted provided that the following conditions |
10 |
|
|
* are met: |
11 |
|
|
* 1. Redistributions of source code must retain the above copyright |
12 |
|
|
* notice, this list of conditions and the following disclaimer. |
13 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright |
14 |
|
|
* notice, this list of conditions and the following disclaimer in the |
15 |
|
|
* documentation and/or other materials provided with the distribution. |
16 |
|
|
* 3. Neither the name of the University nor the names of its contributors |
17 |
|
|
* may be used to endorse or promote products derived from this software |
18 |
|
|
* without specific prior written permission. |
19 |
|
|
* |
20 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
21 |
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
22 |
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
23 |
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
24 |
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
25 |
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
26 |
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
27 |
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
28 |
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
29 |
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
30 |
|
|
* SUCH DAMAGE. |
31 |
|
|
*/ |
32 |
|
|
|
33 |
|
|
#include <sys/types.h> |
34 |
|
|
#include <sys/msgbuf.h> |
35 |
|
|
#include <sys/sysctl.h> |
36 |
|
|
|
37 |
|
|
#include <err.h> |
38 |
|
|
#include <fcntl.h> |
39 |
|
|
#include <kvm.h> |
40 |
|
|
#include <limits.h> |
41 |
|
|
#include <nlist.h> |
42 |
|
|
#include <stdio.h> |
43 |
|
|
#include <stdlib.h> |
44 |
|
|
#include <string.h> |
45 |
|
|
#include <time.h> |
46 |
|
|
#include <unistd.h> |
47 |
|
|
#include <vis.h> |
48 |
|
|
|
49 |
|
|
#ifndef NOKVM |
50 |
|
|
struct nlist nl[] = { |
51 |
|
|
#define X_MSGBUF 0 |
52 |
|
|
{ "_msgbufp" }, |
53 |
|
|
{ NULL }, |
54 |
|
|
}; |
55 |
|
|
#endif |
56 |
|
|
|
57 |
|
|
void usage(void); |
58 |
|
|
|
59 |
|
|
#define KREAD(addr, var) \ |
60 |
|
|
kvm_read(kd, addr, &var, sizeof(var)) != sizeof(var) |
61 |
|
|
|
62 |
|
|
int |
63 |
|
|
main(int argc, char *argv[]) |
64 |
|
|
{ |
65 |
|
|
int ch, newl, skip, i; |
66 |
|
|
char *p; |
67 |
|
4 |
struct msgbuf cur; |
68 |
|
|
char *memf, *nlistf, *bufdata = NULL; |
69 |
|
|
char *allocated = NULL; |
70 |
|
|
int startupmsgs = 0; |
71 |
|
2 |
char buf[5]; |
72 |
|
|
|
73 |
|
|
memf = nlistf = NULL; |
74 |
✗✓ |
4 |
while ((ch = getopt(argc, argv, "sM:N:")) != -1) |
75 |
|
|
switch(ch) { |
76 |
|
|
case 's': |
77 |
|
|
startupmsgs = 1; |
78 |
|
|
break; |
79 |
|
|
case 'M': |
80 |
|
|
memf = optarg; |
81 |
|
|
break; |
82 |
|
|
case 'N': |
83 |
|
|
nlistf = optarg; |
84 |
|
|
break; |
85 |
|
|
case '?': |
86 |
|
|
default: |
87 |
|
|
usage(); |
88 |
|
|
} |
89 |
|
2 |
argc -= optind; |
90 |
|
2 |
argv += optind; |
91 |
|
|
|
92 |
✓✗ |
2 |
if (memf == NULL && nlistf == NULL) { |
93 |
|
2 |
int mib[2], msgbufsize; |
94 |
|
2 |
size_t len; |
95 |
|
|
|
96 |
|
2 |
mib[0] = CTL_KERN; |
97 |
|
2 |
mib[1] = startupmsgs ? KERN_CONSBUFSIZE : KERN_MSGBUFSIZE; |
98 |
|
2 |
len = sizeof(msgbufsize); |
99 |
✗✓ |
2 |
if (sysctl(mib, 2, &msgbufsize, &len, NULL, 0)) |
100 |
|
|
err(1, "sysctl: %s", startupmsgs ? "KERN_CONSBUFSIZE" : |
101 |
|
|
"KERN_MSGBUFSIZE"); |
102 |
|
|
|
103 |
|
2 |
msgbufsize += sizeof(struct msgbuf) - 1; |
104 |
|
2 |
allocated = bufdata = calloc(1, msgbufsize); |
105 |
✗✓ |
2 |
if (bufdata == NULL) |
106 |
|
|
errx(1, "couldn't allocate space for buffer data"); |
107 |
|
|
|
108 |
|
2 |
mib[1] = startupmsgs ? KERN_CONSBUF : KERN_MSGBUF; |
109 |
|
2 |
len = msgbufsize; |
110 |
✗✓ |
2 |
if (sysctl(mib, 2, bufdata, &len, NULL, 0)) |
111 |
|
|
err(1, "sysctl: %s", |
112 |
|
|
startupmsgs ? "KERN_CONSBUF" : "KERN_MSGBUF"); |
113 |
|
|
|
114 |
✗✓ |
2 |
if (pledge("stdio flock rpath cpath wpath", NULL) == -1) |
115 |
|
|
err(1, "pledge"); |
116 |
|
|
|
117 |
|
2 |
memcpy(&cur, bufdata, sizeof(cur)); |
118 |
|
2 |
bufdata = ((struct msgbuf *)bufdata)->msg_bufc; |
119 |
|
2 |
} else { |
120 |
|
|
#ifndef NOKVM |
121 |
|
|
struct msgbuf *bufp; |
122 |
|
|
kvm_t *kd; |
123 |
|
|
|
124 |
|
|
/* Read in kernel message buffer, do sanity checks. */ |
125 |
|
|
if ((kd = kvm_open(nlistf, memf, NULL, O_RDONLY, |
126 |
|
|
"dmesg")) == NULL) |
127 |
|
|
return (1); |
128 |
|
|
|
129 |
|
|
if (pledge("stdio flock rpath cpath wpath", NULL) == -1) |
130 |
|
|
err(1, "pledge"); |
131 |
|
|
|
132 |
|
|
if (kvm_nlist(kd, nl) == -1) |
133 |
|
|
errx(1, "kvm_nlist: %s", kvm_geterr(kd)); |
134 |
|
|
if (nl[X_MSGBUF].n_type == 0) |
135 |
|
|
errx(1, "%s: msgbufp not found", |
136 |
|
|
nlistf ? nlistf : "namelist"); |
137 |
|
|
if (KREAD(nl[X_MSGBUF].n_value, bufp)) |
138 |
|
|
errx(1, "kvm_read: %s: (0x%lx)", kvm_geterr(kd), |
139 |
|
|
nl[X_MSGBUF].n_value); |
140 |
|
|
if (KREAD((long)bufp, cur)) |
141 |
|
|
errx(1, "kvm_read: %s (%0lx)", kvm_geterr(kd), |
142 |
|
|
(unsigned long)bufp); |
143 |
|
|
if (cur.msg_magic != MSG_MAGIC) |
144 |
|
|
errx(1, "magic number incorrect"); |
145 |
|
|
allocated = bufdata = malloc(cur.msg_bufs); |
146 |
|
|
if (bufdata == NULL) |
147 |
|
|
errx(1, "couldn't allocate space for buffer data"); |
148 |
|
|
if (kvm_read(kd, (long)&bufp->msg_bufc, bufdata, |
149 |
|
|
cur.msg_bufs) != cur.msg_bufs) |
150 |
|
|
errx(1, "kvm_read: %s", kvm_geterr(kd)); |
151 |
|
|
kvm_close(kd); |
152 |
|
|
#endif |
153 |
|
|
} |
154 |
|
|
|
155 |
✗✓ |
2 |
if (cur.msg_bufx >= cur.msg_bufs) |
156 |
|
|
cur.msg_bufx = 0; |
157 |
|
|
/* |
158 |
|
|
* The message buffer is circular; start at the read pointer, and |
159 |
|
|
* go to the write pointer - 1. |
160 |
|
|
*/ |
161 |
✓✓ |
261956 |
for (newl = skip = i = 0, p = bufdata + cur.msg_bufx; |
162 |
|
261954 |
i < cur.msg_bufs; i++, p++) { |
163 |
✓✓ |
130976 |
if (p == bufdata + cur.msg_bufs) |
164 |
|
2 |
p = bufdata; |
165 |
|
130976 |
ch = *p; |
166 |
|
|
/* Skip "\n<.*>" syslog sequences. */ |
167 |
✓✓ |
130976 |
if (skip) { |
168 |
✓✓ |
252 |
if (ch == '>') |
169 |
|
126 |
newl = skip = 0; |
170 |
|
|
continue; |
171 |
|
|
} |
172 |
✓✓ |
130724 |
if (newl && ch == '<') { |
173 |
|
|
skip = 1; |
174 |
|
126 |
continue; |
175 |
|
|
} |
176 |
✓✓ |
130598 |
if (ch == '\0') |
177 |
|
|
continue; |
178 |
|
12102 |
newl = ch == '\n'; |
179 |
|
12102 |
vis(buf, ch, 0, 0); |
180 |
✓✗ |
12102 |
if (buf[1] == 0) |
181 |
✓✗ |
24204 |
putchar(buf[0]); |
182 |
|
|
else |
183 |
|
|
printf("%s", buf); |
184 |
|
|
} |
185 |
✗✓ |
2 |
if (!newl) |
186 |
|
|
putchar('\n'); |
187 |
|
2 |
free(allocated); |
188 |
|
2 |
return (0); |
189 |
|
2 |
} |
190 |
|
|
|
191 |
|
|
void |
192 |
|
|
usage(void) |
193 |
|
|
{ |
194 |
|
|
extern char *__progname; |
195 |
|
|
|
196 |
|
|
fprintf(stderr, "usage: %s [-s] [-M core] [-N system]\n", __progname); |
197 |
|
|
exit(1); |
198 |
|
|
} |