1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2003 Ian Dowse. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 /*
29 * Generic message buffer support routines.
30 */
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/lock.h>
35 #include <sys/kernel.h>
36 #include <sys/mutex.h>
37 #include <sys/msgbuf.h>
38 #include <sys/sysctl.h>
39
40 /*
41 * Maximum number conversion buffer length: uintmax_t in base 2, plus <>
42 * around the priority, and a terminating NUL.
43 */
44 #define MAXPRIBUF (sizeof(intmax_t) * NBBY + 3)
45
46 /* Read/write sequence numbers are modulo a multiple of the buffer size. */
47 #define SEQMOD(size) ((size) * 16)
48
49 static u_int msgbuf_cksum(struct msgbuf *mbp);
50
51 /*
52 * Timestamps in msgbuf are useful when trying to diagnose when core dumps
53 * or other actions occurred.
54 */
55 static int msgbuf_show_timestamp = 0;
56 SYSCTL_INT(_kern, OID_AUTO, msgbuf_show_timestamp, CTLFLAG_RWTUN,
57 &msgbuf_show_timestamp, 0, "Show timestamp in msgbuf");
58
59 /*
60 * Initialize a message buffer of the specified size at the specified
61 * location. This also zeros the buffer area.
62 */
63 void
msgbuf_init(struct msgbuf * mbp,void * ptr,int size)64 msgbuf_init(struct msgbuf *mbp, void *ptr, int size)
65 {
66
67 mbp->msg_ptr = ptr;
68 mbp->msg_size = size;
69 mbp->msg_seqmod = SEQMOD(size);
70 mbp->msg_lastpri = -1;
71 mbp->msg_flags = 0;
72 msgbuf_clear(mbp);
73 mbp->msg_magic = MSG_MAGIC;
74 bzero(&mbp->msg_lock, sizeof(mbp->msg_lock));
75 mtx_init(&mbp->msg_lock, "msgbuf", NULL, MTX_SPIN);
76 }
77
78 /*
79 * Reinitialize a message buffer, retaining its previous contents if
80 * the size and checksum are correct. If the old contents cannot be
81 * recovered, the message buffer is cleared.
82 */
83 void
msgbuf_reinit(struct msgbuf * mbp,void * ptr,int size)84 msgbuf_reinit(struct msgbuf *mbp, void *ptr, int size)
85 {
86 u_int cksum;
87
88 if (mbp->msg_magic != MSG_MAGIC || mbp->msg_size != size) {
89 msgbuf_init(mbp, ptr, size);
90 return;
91 }
92 mbp->msg_seqmod = SEQMOD(size);
93 mbp->msg_wseq = MSGBUF_SEQNORM(mbp, mbp->msg_wseq);
94 mbp->msg_rseq = MSGBUF_SEQNORM(mbp, mbp->msg_rseq);
95 mbp->msg_ptr = ptr;
96 cksum = msgbuf_cksum(mbp);
97 if (cksum != mbp->msg_cksum) {
98 if (bootverbose) {
99 printf("msgbuf cksum mismatch (read %x, calc %x)\n",
100 mbp->msg_cksum, cksum);
101 printf("Old msgbuf not recovered\n");
102 }
103 msgbuf_clear(mbp);
104 }
105
106 mbp->msg_lastpri = -1;
107 /* Assume that the old message buffer didn't end in a newline. */
108 mbp->msg_flags |= MSGBUF_NEEDNL;
109 bzero(&mbp->msg_lock, sizeof(mbp->msg_lock));
110 mtx_init(&mbp->msg_lock, "msgbuf", NULL, MTX_SPIN);
111 }
112
113 /*
114 * Clear the message buffer.
115 */
116 void
msgbuf_clear(struct msgbuf * mbp)117 msgbuf_clear(struct msgbuf *mbp)
118 {
119
120 bzero(mbp->msg_ptr, mbp->msg_size);
121 mbp->msg_wseq = 0;
122 mbp->msg_rseq = 0;
123 mbp->msg_cksum = 0;
124 mbp->msg_flags &= ~MSGBUF_WRAP;
125 }
126
127 /*
128 * Get a count of the number of unread characters in the message buffer.
129 */
130 int
msgbuf_getcount(struct msgbuf * mbp)131 msgbuf_getcount(struct msgbuf *mbp)
132 {
133 u_int len;
134
135 len = MSGBUF_SEQSUB(mbp, mbp->msg_wseq, mbp->msg_rseq);
136 if (len > mbp->msg_size)
137 len = mbp->msg_size;
138 return (len);
139 }
140
141 /*
142 * Add a character into the message buffer, and update the checksum and
143 * sequence number.
144 *
145 * The caller should hold the message buffer spinlock.
146 */
147 static void
msgbuf_do_addchar(struct msgbuf * const mbp,const int c)148 msgbuf_do_addchar(struct msgbuf * const mbp, const int c)
149 {
150 u_int pos;
151
152 /* Make sure we properly wrap the sequence number. */
153 pos = MSGBUF_SEQ_TO_POS(mbp, mbp->msg_wseq);
154 mbp->msg_cksum += (u_int)(u_char)c -
155 (u_int)(u_char)mbp->msg_ptr[pos];
156 mbp->msg_ptr[pos] = c;
157 mbp->msg_wseq = MSGBUF_SEQADD(mbp, mbp->msg_wseq, 1);
158 }
159
160 /*
161 * Append a character to a message buffer.
162 */
163 void
msgbuf_addchar(struct msgbuf * mbp,int c)164 msgbuf_addchar(struct msgbuf *mbp, int c)
165 {
166 mtx_lock_spin(&mbp->msg_lock);
167
168 msgbuf_do_addchar(mbp, c);
169 if (mbp->msg_wseq >= mbp->msg_size)
170 mbp->msg_flags |= MSGBUF_WRAP;
171
172 mtx_unlock_spin(&mbp->msg_lock);
173 }
174
175 /*
176 * Append a NUL-terminated string with a priority to a message buffer.
177 * Filter carriage returns if the caller requests it.
178 *
179 * XXX The carriage return filtering behavior is present in the
180 * msglogchar() API, however testing has shown that we don't seem to send
181 * carriage returns down this path. So do we still need it?
182 */
183 void
msgbuf_addstr(struct msgbuf * mbp,int pri,const char * str,int filter_cr)184 msgbuf_addstr(struct msgbuf *mbp, int pri, const char *str, int filter_cr)
185 {
186 size_t len, prefix_len;
187 char prefix[MAXPRIBUF];
188 char buf[32];
189 int i, j, needtime;
190
191 len = strlen(str);
192 prefix_len = 0;
193
194 /* If we have a zero-length string, no need to do anything. */
195 if (len == 0)
196 return;
197
198 mtx_lock_spin(&mbp->msg_lock);
199
200 /*
201 * If this is true, we may need to insert a new priority sequence,
202 * so prepare the prefix.
203 */
204 if (pri != -1)
205 prefix_len = sprintf(prefix, "<%d>", pri);
206
207 /*
208 * Whenever there is a change in priority, we have to insert a
209 * newline, and a priority prefix if the priority is not -1. Here
210 * we detect whether there was a priority change, and whether we
211 * did not end with a newline. If that is the case, we need to
212 * insert a newline before this string.
213 */
214 if (mbp->msg_lastpri != pri && (mbp->msg_flags & MSGBUF_NEEDNL) != 0) {
215 msgbuf_do_addchar(mbp, '\n');
216 mbp->msg_flags &= ~MSGBUF_NEEDNL;
217 }
218
219 needtime = 1;
220 for (i = 0; i < len; i++) {
221 /*
222 * If we just had a newline, and the priority is not -1
223 * (and therefore prefix_len != 0), then we need a priority
224 * prefix for this line.
225 */
226 if ((mbp->msg_flags & MSGBUF_NEEDNL) == 0 && prefix_len != 0) {
227 int j;
228
229 for (j = 0; j < prefix_len; j++)
230 msgbuf_do_addchar(mbp, prefix[j]);
231 }
232
233 if (msgbuf_show_timestamp && needtime == 1 &&
234 (mbp->msg_flags & MSGBUF_NEEDNL) == 0) {
235 snprintf(buf, sizeof(buf), "[%jd] ",
236 (intmax_t)time_uptime);
237 for (j = 0; buf[j] != '\0'; j++)
238 msgbuf_do_addchar(mbp, buf[j]);
239 needtime = 0;
240 }
241
242 /*
243 * Don't copy carriage returns if the caller requested
244 * filtering.
245 *
246 * XXX This matches the behavior of msglogchar(), but is it
247 * necessary? Testing has shown that we don't seem to get
248 * carriage returns here.
249 */
250 if ((filter_cr != 0) && (str[i] == '\r'))
251 continue;
252
253 /*
254 * Clear this flag if we see a newline. This affects whether
255 * we need to insert a new prefix or insert a newline later.
256 */
257 if (str[i] == '\n')
258 mbp->msg_flags &= ~MSGBUF_NEEDNL;
259 else
260 mbp->msg_flags |= MSGBUF_NEEDNL;
261
262 msgbuf_do_addchar(mbp, str[i]);
263 }
264 if (mbp->msg_wseq >= mbp->msg_size)
265 mbp->msg_flags |= MSGBUF_WRAP;
266
267 /*
268 * Set the last priority.
269 */
270 mbp->msg_lastpri = pri;
271
272 mtx_unlock_spin(&mbp->msg_lock);
273
274 }
275
276 /*
277 * Read and mark as read a character from a message buffer.
278 * Returns the character, or -1 if no characters are available.
279 */
280 int
msgbuf_getchar(struct msgbuf * mbp)281 msgbuf_getchar(struct msgbuf *mbp)
282 {
283 u_int len, wseq;
284 int c;
285
286 mtx_lock_spin(&mbp->msg_lock);
287
288 wseq = mbp->msg_wseq;
289 len = MSGBUF_SEQSUB(mbp, wseq, mbp->msg_rseq);
290 if (len == 0) {
291 mtx_unlock_spin(&mbp->msg_lock);
292 return (-1);
293 }
294 if (len > mbp->msg_size)
295 mbp->msg_rseq = MSGBUF_SEQSUB(mbp, wseq, mbp->msg_size);
296 c = (u_char)mbp->msg_ptr[MSGBUF_SEQ_TO_POS(mbp, mbp->msg_rseq)];
297 mbp->msg_rseq = MSGBUF_SEQADD(mbp, mbp->msg_rseq, 1);
298
299 mtx_unlock_spin(&mbp->msg_lock);
300
301 return (c);
302 }
303
304 /*
305 * Read and mark as read a number of characters from a message buffer.
306 * Returns the number of characters that were placed in `buf'.
307 */
308 int
msgbuf_getbytes(struct msgbuf * mbp,char * buf,int buflen)309 msgbuf_getbytes(struct msgbuf *mbp, char *buf, int buflen)
310 {
311 u_int len, pos, wseq;
312
313 mtx_lock_spin(&mbp->msg_lock);
314
315 wseq = mbp->msg_wseq;
316 len = MSGBUF_SEQSUB(mbp, wseq, mbp->msg_rseq);
317 if (len == 0) {
318 mtx_unlock_spin(&mbp->msg_lock);
319 return (0);
320 }
321 if (len > mbp->msg_size) {
322 mbp->msg_rseq = MSGBUF_SEQSUB(mbp, wseq, mbp->msg_size);
323 len = mbp->msg_size;
324 }
325 pos = MSGBUF_SEQ_TO_POS(mbp, mbp->msg_rseq);
326 len = min(len, mbp->msg_size - pos);
327 len = min(len, (u_int)buflen);
328
329 bcopy(&mbp->msg_ptr[pos], buf, len);
330 mbp->msg_rseq = MSGBUF_SEQADD(mbp, mbp->msg_rseq, len);
331
332 mtx_unlock_spin(&mbp->msg_lock);
333
334 return (len);
335 }
336
337 /*
338 * Peek at the full contents of a message buffer without marking any
339 * data as read. `seqp' should point to an unsigned integer that
340 * msgbuf_peekbytes() can use to retain state between calls so that
341 * the whole message buffer can be read in multiple short reads.
342 * To initialise this variable to the start of the message buffer,
343 * call msgbuf_peekbytes() with a NULL `buf' parameter.
344 *
345 * Returns the number of characters that were placed in `buf'.
346 */
347 int
msgbuf_peekbytes(struct msgbuf * mbp,char * buf,int buflen,u_int * seqp)348 msgbuf_peekbytes(struct msgbuf *mbp, char *buf, int buflen, u_int *seqp)
349 {
350 u_int len, pos, wseq;
351
352 mtx_lock_spin(&mbp->msg_lock);
353
354 if (buf == NULL) {
355 /* Just initialise *seqp. */
356 if (mbp->msg_flags & MSGBUF_WRAP)
357 *seqp = MSGBUF_SEQSUB(mbp, mbp->msg_wseq, mbp->msg_size);
358 else
359 *seqp = 0;
360 mtx_unlock_spin(&mbp->msg_lock);
361 return (0);
362 }
363
364 wseq = mbp->msg_wseq;
365 len = MSGBUF_SEQSUB(mbp, wseq, *seqp);
366 if (len == 0) {
367 mtx_unlock_spin(&mbp->msg_lock);
368 return (0);
369 }
370 if (len > mbp->msg_size) {
371 *seqp = MSGBUF_SEQSUB(mbp, wseq, mbp->msg_size);
372 len = mbp->msg_size;
373 }
374 pos = MSGBUF_SEQ_TO_POS(mbp, *seqp);
375 len = min(len, mbp->msg_size - pos);
376 len = min(len, (u_int)buflen);
377 bcopy(&mbp->msg_ptr[MSGBUF_SEQ_TO_POS(mbp, *seqp)], buf, len);
378 *seqp = MSGBUF_SEQADD(mbp, *seqp, len);
379
380 mtx_unlock_spin(&mbp->msg_lock);
381
382 return (len);
383 }
384
385 /*
386 * Compute the checksum for the complete message buffer contents.
387 */
388 static u_int
msgbuf_cksum(struct msgbuf * mbp)389 msgbuf_cksum(struct msgbuf *mbp)
390 {
391 u_int i, sum;
392
393 sum = 0;
394 for (i = 0; i < mbp->msg_size; i++)
395 sum += (u_char)mbp->msg_ptr[i];
396 return (sum);
397 }
398
399 /*
400 * Copy from one message buffer to another.
401 */
402 void
msgbuf_copy(struct msgbuf * src,struct msgbuf * dst)403 msgbuf_copy(struct msgbuf *src, struct msgbuf *dst)
404 {
405 int c;
406
407 while ((c = msgbuf_getchar(src)) >= 0)
408 msgbuf_addchar(dst, c);
409 }
410
411 /*
412 * Get a snapshot of the message buffer, without modifying its internal state
413 * (i.e. don't mark any new characters as read).
414 */
415 void
msgbuf_duplicate(struct msgbuf * src,struct msgbuf * dst,char * dst_msgptr)416 msgbuf_duplicate(struct msgbuf *src, struct msgbuf *dst, char *dst_msgptr)
417 {
418
419 mtx_lock_spin(&src->msg_lock);
420 bcopy(src, dst, sizeof(struct msgbuf));
421 dst->msg_ptr = dst_msgptr;
422 bcopy(src->msg_ptr, dst->msg_ptr, src->msg_size);
423 mtx_unlock_spin(&src->msg_lock);
424 }
425