]>
Commit | Line | Data |
---|---|---|
1815bff5 A |
1 | /* |
2 | * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
c3a08f59 A |
6 | * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved. |
7 | * | |
8 | * This file contains Original Code and/or Modifications of Original Code | |
9 | * as defined in and that are subject to the Apple Public Source License | |
10 | * Version 2.0 (the 'License'). You may not use this file except in | |
11 | * compliance with the License. Please obtain a copy of the License at | |
12 | * http://www.opensource.apple.com/apsl/ and read it before using this | |
13 | * file. | |
1815bff5 A |
14 | * |
15 | * The Original Code and all software distributed under the License are | |
16 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
17 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
18 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
c3a08f59 A |
19 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
20 | * Please see the License for the specific language governing rights and | |
21 | * limitations under the License. | |
1815bff5 A |
22 | * |
23 | * @APPLE_LICENSE_HEADER_END@ | |
24 | */ | |
25 | /*- | |
26 | * Copyright (c) 1991, 1993 | |
27 | * The Regents of the University of California. All rights reserved. | |
28 | * | |
29 | * Redistribution and use in source and binary forms, with or without | |
30 | * modification, are permitted provided that the following conditions | |
31 | * are met: | |
32 | * 1. Redistributions of source code must retain the above copyright | |
33 | * notice, this list of conditions and the following disclaimer. | |
34 | * 2. Redistributions in binary form must reproduce the above copyright | |
35 | * notice, this list of conditions and the following disclaimer in the | |
36 | * documentation and/or other materials provided with the distribution. | |
37 | * 3. All advertising materials mentioning features or use of this software | |
38 | * must display the following acknowledgement: | |
39 | * This product includes software developed by the University of | |
40 | * California, Berkeley and its contributors. | |
41 | * 4. Neither the name of the University nor the names of its contributors | |
42 | * may be used to endorse or promote products derived from this software | |
43 | * without specific prior written permission. | |
44 | * | |
45 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
46 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
47 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
48 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
49 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
50 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
51 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
52 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
53 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
54 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
55 | * SUCH DAMAGE. | |
56 | */ | |
57 | ||
58 | #include <sys/cdefs.h> | |
59 | #include <sys/msgbuf.h> | |
60 | ||
61 | #include <fcntl.h> | |
62 | #include <kvm.h> | |
63 | #include <limits.h> | |
64 | #include <nlist.h> | |
65 | #include <stdio.h> | |
66 | #include <stdlib.h> | |
67 | #include <time.h> | |
68 | #include <unistd.h> | |
69 | #include <vis.h> | |
70 | ||
71 | struct nlist nl[] = { | |
72 | #define X_MSGBUF 0 | |
73 | { "_msgbufp" }, | |
74 | { NULL }, | |
75 | }; | |
76 | ||
77 | void usage __P((void)); | |
78 | ||
79 | #define KREAD(addr, var) \ | |
80 | kvm_read(kd, addr, &var, sizeof(var)) != sizeof(var) | |
81 | ||
82 | int | |
83 | main(argc, argv) | |
84 | int argc; | |
85 | char *argv[]; | |
86 | { | |
87 | register int ch, newl, skip; | |
88 | register char *p, *ep; | |
89 | struct msgbuf *bufp, cur; | |
90 | char *memf, *nlistf; | |
91 | kvm_t *kd; | |
92 | char buf[5]; | |
93 | ||
94 | memf = nlistf = NULL; | |
95 | while ((ch = getopt(argc, argv, "M:N:")) != EOF) | |
96 | switch(ch) { | |
97 | case 'M': | |
98 | memf = optarg; | |
99 | break; | |
100 | case 'N': | |
101 | nlistf = optarg; | |
102 | break; | |
103 | case '?': | |
104 | default: | |
105 | usage(); | |
106 | } | |
107 | argc -= optind; | |
108 | argv += optind; | |
109 | ||
110 | /* | |
111 | * Discard setgid privileges if not the running kernel so that bad | |
112 | * guys can't print interesting stuff from kernel memory. | |
113 | */ | |
114 | if (memf != NULL || nlistf != NULL) | |
115 | setgid(getgid()); | |
116 | ||
117 | /* Read in kernel message buffer, do sanity checks. */ | |
118 | if ((kd = kvm_open(nlistf, memf, NULL, O_RDONLY, "dmesg")) == NULL) | |
119 | exit (1); | |
120 | if (kvm_nlist(kd, nl) == -1) | |
121 | errx(1, "kvm_nlist: %s", kvm_geterr(kd)); | |
122 | if (nl[X_MSGBUF].n_type == 0) | |
123 | errx(1, "%s: msgbufp not found", nlistf ? nlistf : "namelist"); | |
124 | if (KREAD(nl[X_MSGBUF].n_value, bufp) || KREAD((long)bufp, cur)) | |
125 | errx(1, "kvm_read: %s", kvm_geterr(kd)); | |
126 | kvm_close(kd); | |
127 | if (cur.msg_magic != MSG_MAGIC) | |
128 | errx(1, "magic number incorrect"); | |
129 | if (cur.msg_bufx >= MSG_BSIZE) | |
130 | cur.msg_bufx = 0; | |
131 | ||
132 | /* | |
133 | * The message buffer is circular; start at the read pointer, and | |
134 | * go to the write pointer - 1. | |
135 | */ | |
136 | p = cur.msg_bufc + cur.msg_bufx; | |
137 | ep = cur.msg_bufc + cur.msg_bufx - 1; | |
138 | for (newl = skip = 0; p != ep; ++p) { | |
139 | if (p == cur.msg_bufc + MSG_BSIZE) | |
140 | p = cur.msg_bufc; | |
141 | ch = *p; | |
142 | /* Skip "\n<.*>" syslog sequences. */ | |
143 | if (skip) { | |
144 | if (ch == '>') | |
145 | newl = skip = 0; | |
146 | continue; | |
147 | } | |
148 | if (newl && ch == '<') { | |
149 | skip = 1; | |
150 | continue; | |
151 | } | |
152 | if (ch == '\0') | |
153 | continue; | |
154 | newl = ch == '\n'; | |
155 | (void)vis(buf, ch, 0, 0); | |
156 | if (buf[1] == 0) | |
157 | (void)putchar(buf[0]); | |
158 | else | |
159 | (void)printf("%s", buf); | |
160 | } | |
161 | if (!newl) | |
162 | (void)putchar('\n'); | |
163 | exit(0); | |
164 | } | |
165 | ||
166 | void | |
167 | usage() | |
168 | { | |
169 | (void)fprintf(stderr, "usage: dmesg [-M core] [-N system]\n"); | |
170 | exit(1); | |
171 | } |