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 if (msgbuf_show_timestamp == 1) {
236 snprintf(buf, sizeof(buf), "[%jd] ",
237 (intmax_t)time_uptime);
238 } else {
239 struct timeval tv;
240
241 microuptime(&tv);
242 snprintf(buf, sizeof(buf), "[%jd.%06d] ",
243 (intmax_t)tv.tv_sec, (int)tv.tv_usec);
244 }
245 for (j = 0; buf[j] != '\0'; j++)
246 msgbuf_do_addchar(mbp, buf[j]);
247 needtime = 0;
248 }
249
250 /*
251 * Don't copy carriage returns if the caller requested
252 * filtering.
253 *
254 * XXX This matches the behavior of msglogchar(), but is it
255 * necessary? Testing has shown that we don't seem to get
256 * carriage returns here.
257 */
258 if ((filter_cr != 0) && (str[i] == '\r'))
259 continue;
260
261 /*
262 * Clear this flag if we see a newline. This affects whether
263 * we need to insert a new prefix or insert a newline later.
264 */
265 if (str[i] == '\n')
266 mbp->msg_flags &= ~MSGBUF_NEEDNL;
267 else
268 mbp->msg_flags |= MSGBUF_NEEDNL;
269
270 msgbuf_do_addchar(mbp, str[i]);
271 }
272 if (mbp->msg_wseq >= mbp->msg_size)
273 mbp->msg_flags |= MSGBUF_WRAP;
274
275 /*
276 * Set the last priority.
277 */
278 mbp->msg_lastpri = pri;
279
280 mtx_unlock_spin(&mbp->msg_lock);
281
282 }
283
284 /*
285 * Read and mark as read a character from a message buffer.
286 * Returns the character, or -1 if no characters are available.
287 */
288 int
msgbuf_getchar(struct msgbuf * mbp)289 msgbuf_getchar(struct msgbuf *mbp)
290 {
291 u_int len, wseq;
292 int c;
293
294 mtx_lock_spin(&mbp->msg_lock);
295
296 wseq = mbp->msg_wseq;
297 len = MSGBUF_SEQSUB(mbp, wseq, mbp->msg_rseq);
298 if (len == 0) {
299 mtx_unlock_spin(&mbp->msg_lock);
300 return (-1);
301 }
302 if (len > mbp->msg_size)
303 mbp->msg_rseq = MSGBUF_SEQSUB(mbp, wseq, mbp->msg_size);
304 c = (u_char)mbp->msg_ptr[MSGBUF_SEQ_TO_POS(mbp, mbp->msg_rseq)];
305 mbp->msg_rseq = MSGBUF_SEQADD(mbp, mbp->msg_rseq, 1);
306
307 mtx_unlock_spin(&mbp->msg_lock);
308
309 return (c);
310 }
311
312 /*
313 * Read and mark as read a number of characters from a message buffer.
314 * Returns the number of characters that were placed in `buf'.
315 */
316 int
msgbuf_getbytes(struct msgbuf * mbp,char * buf,int buflen)317 msgbuf_getbytes(struct msgbuf *mbp, char *buf, int buflen)
318 {
319 u_int len, pos, wseq;
320
321 mtx_lock_spin(&mbp->msg_lock);
322
323 wseq = mbp->msg_wseq;
324 len = MSGBUF_SEQSUB(mbp, wseq, mbp->msg_rseq);
325 if (len == 0) {
326 mtx_unlock_spin(&mbp->msg_lock);
327 return (0);
328 }
329 if (len > mbp->msg_size) {
330 mbp->msg_rseq = MSGBUF_SEQSUB(mbp, wseq, mbp->msg_size);
331 len = mbp->msg_size;
332 }
333 pos = MSGBUF_SEQ_TO_POS(mbp, mbp->msg_rseq);
334 len = min(len, mbp->msg_size - pos);
335 len = min(len, (u_int)buflen);
336
337 bcopy(&mbp->msg_ptr[pos], buf, len);
338 mbp->msg_rseq = MSGBUF_SEQADD(mbp, mbp->msg_rseq, len);
339
340 mtx_unlock_spin(&mbp->msg_lock);
341
342 return (len);
343 }
344
345 /*
346 * Peek at the full contents of a message buffer without marking any
347 * data as read. `seqp' should point to an unsigned integer that
348 * msgbuf_peekbytes() can use to retain state between calls so that
349 * the whole message buffer can be read in multiple short reads.
350 * To initialise this variable to the start of the message buffer,
351 * call msgbuf_peekbytes() with a NULL `buf' parameter.
352 *
353 * Returns the number of characters that were placed in `buf'.
354 */
355 int
msgbuf_peekbytes(struct msgbuf * mbp,char * buf,int buflen,u_int * seqp)356 msgbuf_peekbytes(struct msgbuf *mbp, char *buf, int buflen, u_int *seqp)
357 {
358 u_int len, pos, wseq;
359
360 mtx_lock_spin(&mbp->msg_lock);
361
362 if (buf == NULL) {
363 /* Just initialise *seqp. */
364 if (mbp->msg_flags & MSGBUF_WRAP)
365 *seqp = MSGBUF_SEQSUB(mbp, mbp->msg_wseq, mbp->msg_size);
366 else
367 *seqp = 0;
368 mtx_unlock_spin(&mbp->msg_lock);
369 return (0);
370 }
371
372 wseq = mbp->msg_wseq;
373 len = MSGBUF_SEQSUB(mbp, wseq, *seqp);
374 if (len == 0) {
375 mtx_unlock_spin(&mbp->msg_lock);
376 return (0);
377 }
378 if (len > mbp->msg_size) {
379 *seqp = MSGBUF_SEQSUB(mbp, wseq, mbp->msg_size);
380 len = mbp->msg_size;
381 }
382 pos = MSGBUF_SEQ_TO_POS(mbp, *seqp);
383 len = min(len, mbp->msg_size - pos);
384 len = min(len, (u_int)buflen);
385 bcopy(&mbp->msg_ptr[MSGBUF_SEQ_TO_POS(mbp, *seqp)], buf, len);
386 *seqp = MSGBUF_SEQADD(mbp, *seqp, len);
387
388 mtx_unlock_spin(&mbp->msg_lock);
389
390 return (len);
391 }
392
393 /*
394 * Compute the checksum for the complete message buffer contents.
395 */
396 static u_int
msgbuf_cksum(struct msgbuf * mbp)397 msgbuf_cksum(struct msgbuf *mbp)
398 {
399 u_int i, sum;
400
401 sum = 0;
402 for (i = 0; i < mbp->msg_size; i++)
403 sum += (u_char)mbp->msg_ptr[i];
404 return (sum);
405 }
406
407 /*
408 * Copy from one message buffer to another.
409 */
410 void
msgbuf_copy(struct msgbuf * src,struct msgbuf * dst)411 msgbuf_copy(struct msgbuf *src, struct msgbuf *dst)
412 {
413 int c;
414
415 while ((c = msgbuf_getchar(src)) >= 0)
416 msgbuf_addchar(dst, c);
417 }
418
419 /*
420 * Get a snapshot of the message buffer, without modifying its internal state
421 * (i.e. don't mark any new characters as read).
422 */
423 void
msgbuf_duplicate(struct msgbuf * src,struct msgbuf * dst,char * dst_msgptr)424 msgbuf_duplicate(struct msgbuf *src, struct msgbuf *dst, char *dst_msgptr)
425 {
426
427 mtx_lock_spin(&src->msg_lock);
428 bcopy(src, dst, sizeof(struct msgbuf));
429 dst->msg_ptr = dst_msgptr;
430 bcopy(src->msg_ptr, dst->msg_ptr, src->msg_size);
431 mtx_unlock_spin(&src->msg_lock);
432 }
433