GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: sbin/dmesg/dmesg.c Lines: 42 85 49.4 %
Date: 2017-11-07 Branches: 22 58 37.9 %

Line Branch Exec Source
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
74
	struct msgbuf cur;
68
	char *memf, *nlistf, *bufdata = NULL;
69
	char *allocated = NULL;
70
	int startupmsgs = 0;
71
37
	char buf[5];
72
73
	memf = nlistf = NULL;
74
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
		case '?':
86
		default:
87
			usage();
88
		}
89
37
	argc -= optind;
90
37
	argv += optind;
91
92
37
	if (memf == NULL && nlistf == NULL) {
93
37
		int mib[2], msgbufsize;
94
37
		size_t len;
95
96
37
		mib[0] = CTL_KERN;
97
37
		mib[1] = startupmsgs ? KERN_CONSBUFSIZE : KERN_MSGBUFSIZE;
98
37
		len = sizeof(msgbufsize);
99
37
		if (sysctl(mib, 2, &msgbufsize, &len, NULL, 0))
100
			err(1, "sysctl: %s", startupmsgs ? "KERN_CONSBUFSIZE" :
101
			    "KERN_MSGBUFSIZE");
102
103
37
		msgbufsize += sizeof(struct msgbuf) - 1;
104
37
		allocated = bufdata = calloc(1, msgbufsize);
105
37
		if (bufdata == NULL)
106
			errx(1, "couldn't allocate space for buffer data");
107
108
37
		mib[1] = startupmsgs ? KERN_CONSBUF : KERN_MSGBUF;
109
37
		len = msgbufsize;
110
37
		if (sysctl(mib, 2, bufdata, &len, NULL, 0))
111
			err(1, "sysctl: %s",
112
			    startupmsgs ? "KERN_CONSBUF" : "KERN_MSGBUF");
113
114
37
		if (pledge("stdio flock rpath cpath wpath", NULL) == -1)
115
			err(1, "pledge");
116
117
37
		memcpy(&cur, bufdata, sizeof(cur));
118
37
		bufdata = ((struct msgbuf *)bufdata)->msg_bufc;
119
37
	} 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
37
	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
4846186
	for (newl = skip = i = 0, p = bufdata + cur.msg_bufx;
162
4846149
	    i < cur.msg_bufs; i++, p++) {
163
2423056
		if (p == bufdata + cur.msg_bufs)
164
37
			p = bufdata;
165
2423056
		ch = *p;
166
		/* Skip "\n<.*>" syslog sequences. */
167
2423056
		if (skip) {
168
47184
			if (ch == '>')
169
23592
				newl = skip = 0;
170
			continue;
171
		}
172
2375872
		if (newl && ch == '<') {
173
			skip = 1;
174
23592
			continue;
175
		}
176
2352280
		if (ch == '\0')
177
			continue;
178
934703
		newl = ch == '\n';
179
934703
		vis(buf, ch, 0, 0);
180
934703
		if (buf[1] == 0)
181
1869406
			putchar(buf[0]);
182
		else
183
			printf("%s", buf);
184
	}
185
37
	if (!newl)
186
		putchar('\n');
187
37
	free(allocated);
188
37
	return (0);
189
37
}
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
}