1 /* $OpenBSD: dmesg.c,v 1.33 2022/12/04 23:50:46 cheloha 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 <stddef.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.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
main(int argc,char * argv[])63 main(int argc, char *argv[])
64 {
65 int ch, newl, skip, i;
66 char *p;
67 struct msgbuf cur;
68 char *memf, *nlistf, *bufdata = NULL;
69 char *allocated = NULL;
70 int startupmsgs = 0;
71 char buf[5];
72
73 memf = nlistf = NULL;
74 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 default:
86 usage();
87 }
88 argc -= optind;
89 argv += optind;
90
91 if (argc != 0)
92 usage();
93
94 if (memf == NULL && nlistf == NULL) {
95 int mib[2], msgbufsize;
96 size_t len;
97
98 mib[0] = CTL_KERN;
99 mib[1] = startupmsgs ? KERN_CONSBUFSIZE : KERN_MSGBUFSIZE;
100 len = sizeof(msgbufsize);
101 if (sysctl(mib, 2, &msgbufsize, &len, NULL, 0))
102 err(1, "sysctl: %s", startupmsgs ? "KERN_CONSBUFSIZE" :
103 "KERN_MSGBUFSIZE");
104
105 msgbufsize += offsetof(struct msgbuf, msg_bufc);
106 allocated = bufdata = calloc(1, msgbufsize);
107 if (bufdata == NULL)
108 errx(1, "couldn't allocate space for buffer data");
109
110 mib[1] = startupmsgs ? KERN_CONSBUF : KERN_MSGBUF;
111 len = msgbufsize;
112 if (sysctl(mib, 2, bufdata, &len, NULL, 0))
113 err(1, "sysctl: %s",
114 startupmsgs ? "KERN_CONSBUF" : "KERN_MSGBUF");
115
116 if (pledge("stdio", NULL) == -1)
117 err(1, "pledge");
118
119 memcpy(&cur, bufdata, sizeof(cur));
120 bufdata = ((struct msgbuf *)bufdata)->msg_bufc;
121 } else {
122 #ifndef NOKVM
123 struct msgbuf *bufp;
124 kvm_t *kd;
125
126 /* Read in kernel message buffer, do sanity checks. */
127 if ((kd = kvm_open(nlistf, memf, NULL, O_RDONLY,
128 "dmesg")) == NULL)
129 return (1);
130
131 if (pledge("stdio", NULL) == -1)
132 err(1, "pledge");
133
134 if (kvm_nlist(kd, nl) == -1)
135 errx(1, "kvm_nlist: %s", kvm_geterr(kd));
136 if (nl[X_MSGBUF].n_type == 0)
137 errx(1, "%s: msgbufp not found",
138 nlistf ? nlistf : "namelist");
139 if (KREAD(nl[X_MSGBUF].n_value, bufp))
140 errx(1, "kvm_read: %s: (0x%lx)", kvm_geterr(kd),
141 nl[X_MSGBUF].n_value);
142 if (KREAD((long)bufp, cur))
143 errx(1, "kvm_read: %s (%0lx)", kvm_geterr(kd),
144 (unsigned long)bufp);
145 if (cur.msg_magic != MSG_MAGIC)
146 errx(1, "magic number incorrect");
147 allocated = bufdata = malloc(cur.msg_bufs);
148 if (bufdata == NULL)
149 errx(1, "couldn't allocate space for buffer data");
150 if (kvm_read(kd, (long)&bufp->msg_bufc, bufdata,
151 cur.msg_bufs) != cur.msg_bufs)
152 errx(1, "kvm_read: %s", kvm_geterr(kd));
153 kvm_close(kd);
154 #endif
155 }
156
157 if (cur.msg_bufx >= cur.msg_bufs)
158 cur.msg_bufx = 0;
159 /*
160 * The message buffer is circular; start at the read pointer, and
161 * go to the write pointer - 1.
162 */
163 for (newl = skip = i = 0, p = bufdata + cur.msg_bufx;
164 i < cur.msg_bufs; i++, p++) {
165 if (p == bufdata + cur.msg_bufs)
166 p = bufdata;
167 ch = *p;
168 /* Skip "\n<.*>" syslog sequences. */
169 if (skip) {
170 if (ch == '>')
171 newl = skip = 0;
172 continue;
173 }
174 if (newl && ch == '<') {
175 skip = 1;
176 continue;
177 }
178 if (ch == '\0')
179 continue;
180 newl = ch == '\n';
181 vis(buf, ch, 0, 0);
182 if (buf[1] == 0)
183 putchar(buf[0]);
184 else
185 printf("%s", buf);
186 }
187 if (!newl)
188 putchar('\n');
189 free(allocated);
190 return (0);
191 }
192
193 void
usage(void)194 usage(void)
195 {
196 extern char *__progname;
197
198 fprintf(stderr, "usage: %s [-s] [-M core] [-N system]\n", __progname);
199 exit(1);
200 }
201