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