File: | src/sbin/dmesg/dmesg.c |
Warning: | line 90, column 2 Value stored to 'argv' is never read |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* $OpenBSD: dmesg.c,v 1.31 2019/12/24 13:20:44 bluhm 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_MSGBUF0 0 |
52 | { "_msgbufp" }, |
53 | { NULL((void*)0) }, |
54 | }; |
55 | #endif |
56 | |
57 | void usage(void); |
58 | |
59 | #define KREAD(addr, var)kvm_read(kd, addr, &var, sizeof(var)) != sizeof(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 | struct msgbuf cur; |
68 | char *memf, *nlistf, *bufdata = NULL((void*)0); |
69 | char *allocated = NULL((void*)0); |
70 | int startupmsgs = 0; |
71 | char buf[5]; |
72 | |
73 | memf = nlistf = NULL((void*)0); |
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 | argc -= optind; |
90 | argv += optind; |
Value stored to 'argv' is never read | |
91 | |
92 | if (memf == NULL((void*)0) && nlistf == NULL((void*)0)) { |
93 | int mib[2], msgbufsize; |
94 | size_t len; |
95 | |
96 | mib[0] = CTL_KERN1; |
97 | mib[1] = startupmsgs ? KERN_CONSBUFSIZE82 : KERN_MSGBUFSIZE38; |
98 | len = sizeof(msgbufsize); |
99 | if (sysctl(mib, 2, &msgbufsize, &len, NULL((void*)0), 0)) |
100 | err(1, "sysctl: %s", startupmsgs ? "KERN_CONSBUFSIZE" : |
101 | "KERN_MSGBUFSIZE"); |
102 | |
103 | msgbufsize += offsetof(struct msgbuf, msg_bufc)__builtin_offsetof(struct msgbuf, msg_bufc); |
104 | allocated = bufdata = calloc(1, msgbufsize); |
105 | if (bufdata == NULL((void*)0)) |
106 | errx(1, "couldn't allocate space for buffer data"); |
107 | |
108 | mib[1] = startupmsgs ? KERN_CONSBUF83 : KERN_MSGBUF48; |
109 | len = msgbufsize; |
110 | if (sysctl(mib, 2, bufdata, &len, NULL((void*)0), 0)) |
111 | err(1, "sysctl: %s", |
112 | startupmsgs ? "KERN_CONSBUF" : "KERN_MSGBUF"); |
113 | |
114 | if (pledge("stdio", NULL((void*)0)) == -1) |
115 | err(1, "pledge"); |
116 | |
117 | memcpy(&cur, bufdata, sizeof(cur)); |
118 | bufdata = ((struct msgbuf *)bufdata)->msg_bufc; |
119 | } 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((void*)0), O_RDONLY0x0000, |
126 | "dmesg")) == NULL((void*)0)) |
127 | return (1); |
128 | |
129 | if (pledge("stdio", NULL((void*)0)) == -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_MSGBUF0].n_type == 0) |
135 | errx(1, "%s: msgbufp not found", |
136 | nlistf ? nlistf : "namelist"); |
137 | if (KREAD(nl[X_MSGBUF].n_value, bufp)kvm_read(kd, nl[0].n_value, &bufp, sizeof(bufp)) != sizeof (bufp)) |
138 | errx(1, "kvm_read: %s: (0x%lx)", kvm_geterr(kd), |
139 | nl[X_MSGBUF0].n_value); |
140 | if (KREAD((long)bufp, cur)kvm_read(kd, (long)bufp, &cur, sizeof(cur)) != sizeof(cur )) |
141 | errx(1, "kvm_read: %s (%0lx)", kvm_geterr(kd), |
142 | (unsigned long)bufp); |
143 | if (cur.msg_magic != MSG_MAGIC0x063061) |
144 | errx(1, "magic number incorrect"); |
145 | allocated = bufdata = malloc(cur.msg_bufs); |
146 | if (bufdata == NULL((void*)0)) |
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 | 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 | for (newl = skip = i = 0, p = bufdata + cur.msg_bufx; |
162 | i < cur.msg_bufs; i++, p++) { |
163 | if (p == bufdata + cur.msg_bufs) |
164 | p = bufdata; |
165 | ch = *p; |
166 | /* Skip "\n<.*>" syslog sequences. */ |
167 | if (skip) { |
168 | if (ch == '>') |
169 | newl = skip = 0; |
170 | continue; |
171 | } |
172 | if (newl && ch == '<') { |
173 | skip = 1; |
174 | continue; |
175 | } |
176 | if (ch == '\0') |
177 | continue; |
178 | newl = ch == '\n'; |
179 | vis(buf, ch, 0, 0); |
180 | if (buf[1] == 0) |
181 | putchar(buf[0])(!__isthreaded ? __sputc(buf[0], (&__sF[1])) : (putc)(buf [0], (&__sF[1]))); |
182 | else |
183 | printf("%s", buf); |
184 | } |
185 | if (!newl) |
186 | putchar('\n')(!__isthreaded ? __sputc('\n', (&__sF[1])) : (putc)('\n', (&__sF[1]))); |
187 | free(allocated); |
188 | return (0); |
189 | } |
190 | |
191 | void |
192 | usage(void) |
193 | { |
194 | extern char *__progname; |
195 | |
196 | fprintf(stderr(&__sF[2]), "usage: %s [-s] [-M core] [-N system]\n", __progname); |
197 | exit(1); |
198 | } |