xref: /freebsd/sys/kern/subr_msgbuf.c (revision 95ee2897)
19454b2d8SWarner Losh /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
38a36da99SPedro F. Giffuni  *
44784a469SIan Dowse  * Copyright (c) 2003 Ian Dowse.  All rights reserved.
54784a469SIan Dowse  *
64784a469SIan Dowse  * Redistribution and use in source and binary forms, with or without
74784a469SIan Dowse  * modification, are permitted provided that the following conditions
84784a469SIan Dowse  * are met:
94784a469SIan Dowse  * 1. Redistributions of source code must retain the above copyright
104784a469SIan Dowse  *    notice, this list of conditions and the following disclaimer.
114784a469SIan Dowse  * 2. Redistributions in binary form must reproduce the above copyright
124784a469SIan Dowse  *    notice, this list of conditions and the following disclaimer in the
134784a469SIan Dowse  *    documentation and/or other materials provided with the distribution.
144784a469SIan Dowse  *
154784a469SIan Dowse  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
164784a469SIan Dowse  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
174784a469SIan Dowse  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
184784a469SIan Dowse  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
194784a469SIan Dowse  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
204784a469SIan Dowse  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
214784a469SIan Dowse  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
224784a469SIan Dowse  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
234784a469SIan Dowse  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
244784a469SIan Dowse  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
254784a469SIan Dowse  * SUCH DAMAGE.
264784a469SIan Dowse  */
274784a469SIan Dowse 
284784a469SIan Dowse /*
294784a469SIan Dowse  * Generic message buffer support routines.
304784a469SIan Dowse  */
314784a469SIan Dowse 
324784a469SIan Dowse #include <sys/param.h>
334784a469SIan Dowse #include <sys/systm.h>
34d42a4eb5SKenneth D. Merry #include <sys/lock.h>
35f17a6f1bSEitan Adler #include <sys/kernel.h>
36d42a4eb5SKenneth D. Merry #include <sys/mutex.h>
374784a469SIan Dowse #include <sys/msgbuf.h>
38f17a6f1bSEitan Adler #include <sys/sysctl.h>
394784a469SIan Dowse 
40d42a4eb5SKenneth D. Merry /*
41d42a4eb5SKenneth D. Merry  * Maximum number conversion buffer length: uintmax_t in base 2, plus <>
42d42a4eb5SKenneth D. Merry  * around the priority, and a terminating NUL.
43d42a4eb5SKenneth D. Merry  */
44d42a4eb5SKenneth D. Merry #define	MAXPRIBUF	(sizeof(intmax_t) * NBBY + 3)
45d42a4eb5SKenneth D. Merry 
464784a469SIan Dowse /* Read/write sequence numbers are modulo a multiple of the buffer size. */
474784a469SIan Dowse #define SEQMOD(size) ((size) * 16)
484784a469SIan Dowse 
494784a469SIan Dowse static u_int msgbuf_cksum(struct msgbuf *mbp);
504784a469SIan Dowse 
514784a469SIan Dowse /*
5224c10828SEitan Adler  * Timestamps in msgbuf are useful when trying to diagnose when core dumps
53e3043798SPedro F. Giffuni  * or other actions occurred.
54f17a6f1bSEitan Adler  */
55f17a6f1bSEitan Adler static int msgbuf_show_timestamp = 0;
56af3b2549SHans Petter Selasky SYSCTL_INT(_kern, OID_AUTO, msgbuf_show_timestamp, CTLFLAG_RWTUN,
57f17a6f1bSEitan Adler     &msgbuf_show_timestamp, 0, "Show timestamp in msgbuf");
58f17a6f1bSEitan Adler 
59f17a6f1bSEitan Adler /*
604784a469SIan Dowse  * Initialize a message buffer of the specified size at the specified
614784a469SIan Dowse  * location. This also zeros the buffer area.
624784a469SIan Dowse  */
634784a469SIan Dowse void
msgbuf_init(struct msgbuf * mbp,void * ptr,int size)644784a469SIan Dowse msgbuf_init(struct msgbuf *mbp, void *ptr, int size)
654784a469SIan Dowse {
664784a469SIan Dowse 
674784a469SIan Dowse 	mbp->msg_ptr = ptr;
684784a469SIan Dowse 	mbp->msg_size = size;
694784a469SIan Dowse 	mbp->msg_seqmod = SEQMOD(size);
70d42a4eb5SKenneth D. Merry 	mbp->msg_lastpri = -1;
71f17a6f1bSEitan Adler 	mbp->msg_flags = 0;
7281dc0033SAlexander Motin 	msgbuf_clear(mbp);
7381dc0033SAlexander Motin 	mbp->msg_magic = MSG_MAGIC;
74534917efSKenneth D. Merry 	bzero(&mbp->msg_lock, sizeof(mbp->msg_lock));
75d42a4eb5SKenneth D. Merry 	mtx_init(&mbp->msg_lock, "msgbuf", NULL, MTX_SPIN);
764784a469SIan Dowse }
774784a469SIan Dowse 
784784a469SIan Dowse /*
794784a469SIan Dowse  * Reinitialize a message buffer, retaining its previous contents if
804784a469SIan Dowse  * the size and checksum are correct. If the old contents cannot be
814784a469SIan Dowse  * recovered, the message buffer is cleared.
824784a469SIan Dowse  */
834784a469SIan Dowse void
msgbuf_reinit(struct msgbuf * mbp,void * ptr,int size)844784a469SIan Dowse msgbuf_reinit(struct msgbuf *mbp, void *ptr, int size)
854784a469SIan Dowse {
864784a469SIan Dowse 	u_int cksum;
874784a469SIan Dowse 
884784a469SIan Dowse 	if (mbp->msg_magic != MSG_MAGIC || mbp->msg_size != size) {
894784a469SIan Dowse 		msgbuf_init(mbp, ptr, size);
904784a469SIan Dowse 		return;
914784a469SIan Dowse 	}
924784a469SIan Dowse 	mbp->msg_seqmod = SEQMOD(size);
934784a469SIan Dowse 	mbp->msg_wseq = MSGBUF_SEQNORM(mbp, mbp->msg_wseq);
944784a469SIan Dowse 	mbp->msg_rseq = MSGBUF_SEQNORM(mbp, mbp->msg_rseq);
954784a469SIan Dowse         mbp->msg_ptr = ptr;
964784a469SIan Dowse 	cksum = msgbuf_cksum(mbp);
974784a469SIan Dowse 	if (cksum != mbp->msg_cksum) {
98ce914a08SPoul-Henning Kamp 		if (bootverbose) {
994784a469SIan Dowse 			printf("msgbuf cksum mismatch (read %x, calc %x)\n",
1004784a469SIan Dowse 			    mbp->msg_cksum, cksum);
101ce914a08SPoul-Henning Kamp 			printf("Old msgbuf not recovered\n");
102ce914a08SPoul-Henning Kamp 		}
1034784a469SIan Dowse 		msgbuf_clear(mbp);
1044784a469SIan Dowse 	}
105d42a4eb5SKenneth D. Merry 
106d42a4eb5SKenneth D. Merry 	mbp->msg_lastpri = -1;
107d42a4eb5SKenneth D. Merry 	/* Assume that the old message buffer didn't end in a newline. */
108f17a6f1bSEitan Adler 	mbp->msg_flags |= MSGBUF_NEEDNL;
109534917efSKenneth D. Merry 	bzero(&mbp->msg_lock, sizeof(mbp->msg_lock));
110d42a4eb5SKenneth D. Merry 	mtx_init(&mbp->msg_lock, "msgbuf", NULL, MTX_SPIN);
1114784a469SIan Dowse }
1124784a469SIan Dowse 
1134784a469SIan Dowse /*
1144784a469SIan Dowse  * Clear the message buffer.
1154784a469SIan Dowse  */
1164784a469SIan Dowse void
msgbuf_clear(struct msgbuf * mbp)1174784a469SIan Dowse msgbuf_clear(struct msgbuf *mbp)
1184784a469SIan Dowse {
1194784a469SIan Dowse 
1204784a469SIan Dowse 	bzero(mbp->msg_ptr, mbp->msg_size);
1214784a469SIan Dowse 	mbp->msg_wseq = 0;
1224784a469SIan Dowse 	mbp->msg_rseq = 0;
1234784a469SIan Dowse 	mbp->msg_cksum = 0;
12481dc0033SAlexander Motin 	mbp->msg_flags &= ~MSGBUF_WRAP;
1254784a469SIan Dowse }
1264784a469SIan Dowse 
1274784a469SIan Dowse /*
1284784a469SIan Dowse  * Get a count of the number of unread characters in the message buffer.
1294784a469SIan Dowse  */
1304784a469SIan Dowse int
msgbuf_getcount(struct msgbuf * mbp)1314784a469SIan Dowse msgbuf_getcount(struct msgbuf *mbp)
1324784a469SIan Dowse {
1334784a469SIan Dowse 	u_int len;
1344784a469SIan Dowse 
1354784a469SIan Dowse 	len = MSGBUF_SEQSUB(mbp, mbp->msg_wseq, mbp->msg_rseq);
1364784a469SIan Dowse 	if (len > mbp->msg_size)
1374784a469SIan Dowse 		len = mbp->msg_size;
1384784a469SIan Dowse 	return (len);
1394784a469SIan Dowse }
1404784a469SIan Dowse 
1414784a469SIan Dowse /*
142d42a4eb5SKenneth D. Merry  * Add a character into the message buffer, and update the checksum and
143d42a4eb5SKenneth D. Merry  * sequence number.
144d42a4eb5SKenneth D. Merry  *
145d42a4eb5SKenneth D. Merry  * The caller should hold the message buffer spinlock.
146d42a4eb5SKenneth D. Merry  */
14724c10828SEitan Adler static void
msgbuf_do_addchar(struct msgbuf * const mbp,const int c)14881dc0033SAlexander Motin msgbuf_do_addchar(struct msgbuf * const mbp, const int c)
149d42a4eb5SKenneth D. Merry {
150d42a4eb5SKenneth D. Merry 	u_int pos;
151d42a4eb5SKenneth D. Merry 
152d42a4eb5SKenneth D. Merry 	/* Make sure we properly wrap the sequence number. */
15381dc0033SAlexander Motin 	pos = MSGBUF_SEQ_TO_POS(mbp, mbp->msg_wseq);
15424c10828SEitan Adler 	mbp->msg_cksum += (u_int)(u_char)c -
155d42a4eb5SKenneth D. Merry 	    (u_int)(u_char)mbp->msg_ptr[pos];
156d42a4eb5SKenneth D. Merry 	mbp->msg_ptr[pos] = c;
15781dc0033SAlexander Motin 	mbp->msg_wseq = MSGBUF_SEQADD(mbp, mbp->msg_wseq, 1);
158d42a4eb5SKenneth D. Merry }
159d42a4eb5SKenneth D. Merry 
160d42a4eb5SKenneth D. Merry /*
161d42a4eb5SKenneth D. Merry  * Append a character to a message buffer.
1624784a469SIan Dowse  */
1634784a469SIan Dowse void
msgbuf_addchar(struct msgbuf * mbp,int c)1644784a469SIan Dowse msgbuf_addchar(struct msgbuf *mbp, int c)
1654784a469SIan Dowse {
166d42a4eb5SKenneth D. Merry 	mtx_lock_spin(&mbp->msg_lock);
1674784a469SIan Dowse 
16881dc0033SAlexander Motin 	msgbuf_do_addchar(mbp, c);
16981dc0033SAlexander Motin 	if (mbp->msg_wseq >= mbp->msg_size)
17081dc0033SAlexander Motin 		mbp->msg_flags |= MSGBUF_WRAP;
171d42a4eb5SKenneth D. Merry 
172d42a4eb5SKenneth D. Merry 	mtx_unlock_spin(&mbp->msg_lock);
173d42a4eb5SKenneth D. Merry }
174d42a4eb5SKenneth D. Merry 
175d42a4eb5SKenneth D. Merry /*
176d42a4eb5SKenneth D. Merry  * Append a NUL-terminated string with a priority to a message buffer.
177d42a4eb5SKenneth D. Merry  * Filter carriage returns if the caller requests it.
178d42a4eb5SKenneth D. Merry  *
179d42a4eb5SKenneth D. Merry  * XXX The carriage return filtering behavior is present in the
180d42a4eb5SKenneth D. Merry  * msglogchar() API, however testing has shown that we don't seem to send
181d42a4eb5SKenneth D. Merry  * carriage returns down this path.  So do we still need it?
182d42a4eb5SKenneth D. Merry  */
183d42a4eb5SKenneth D. Merry void
msgbuf_addstr(struct msgbuf * mbp,int pri,const char * str,int filter_cr)18421aa6e83SKyle Evans msgbuf_addstr(struct msgbuf *mbp, int pri, const char *str, int filter_cr)
185d42a4eb5SKenneth D. Merry {
186d42a4eb5SKenneth D. Merry 	size_t len, prefix_len;
187d42a4eb5SKenneth D. Merry 	char prefix[MAXPRIBUF];
18824c10828SEitan Adler 	char buf[32];
189151ba793SAlexander Kabaev 	int i, j, needtime;
190d42a4eb5SKenneth D. Merry 
191d42a4eb5SKenneth D. Merry 	len = strlen(str);
192d42a4eb5SKenneth D. Merry 	prefix_len = 0;
193d42a4eb5SKenneth D. Merry 
194d42a4eb5SKenneth D. Merry 	/* If we have a zero-length string, no need to do anything. */
195d42a4eb5SKenneth D. Merry 	if (len == 0)
196d42a4eb5SKenneth D. Merry 		return;
197d42a4eb5SKenneth D. Merry 
198d42a4eb5SKenneth D. Merry 	mtx_lock_spin(&mbp->msg_lock);
199d42a4eb5SKenneth D. Merry 
200d42a4eb5SKenneth D. Merry 	/*
201d42a4eb5SKenneth D. Merry 	 * If this is true, we may need to insert a new priority sequence,
202d42a4eb5SKenneth D. Merry 	 * so prepare the prefix.
203d42a4eb5SKenneth D. Merry 	 */
204d42a4eb5SKenneth D. Merry 	if (pri != -1)
205d42a4eb5SKenneth D. Merry 		prefix_len = sprintf(prefix, "<%d>", pri);
206d42a4eb5SKenneth D. Merry 
207d42a4eb5SKenneth D. Merry 	/*
208d42a4eb5SKenneth D. Merry 	 * Whenever there is a change in priority, we have to insert a
209d42a4eb5SKenneth D. Merry 	 * newline, and a priority prefix if the priority is not -1.  Here
210d42a4eb5SKenneth D. Merry 	 * we detect whether there was a priority change, and whether we
211d42a4eb5SKenneth D. Merry 	 * did not end with a newline.  If that is the case, we need to
212d42a4eb5SKenneth D. Merry 	 * insert a newline before this string.
213d42a4eb5SKenneth D. Merry 	 */
214f17a6f1bSEitan Adler 	if (mbp->msg_lastpri != pri && (mbp->msg_flags & MSGBUF_NEEDNL) != 0) {
21581dc0033SAlexander Motin 		msgbuf_do_addchar(mbp, '\n');
216f17a6f1bSEitan Adler 		mbp->msg_flags &= ~MSGBUF_NEEDNL;
217d42a4eb5SKenneth D. Merry 	}
218d42a4eb5SKenneth D. Merry 
21924c10828SEitan Adler 	needtime = 1;
220d42a4eb5SKenneth D. Merry 	for (i = 0; i < len; i++) {
221d42a4eb5SKenneth D. Merry 		/*
222d42a4eb5SKenneth D. Merry 		 * If we just had a newline, and the priority is not -1
223d42a4eb5SKenneth D. Merry 		 * (and therefore prefix_len != 0), then we need a priority
224d42a4eb5SKenneth D. Merry 		 * prefix for this line.
225d42a4eb5SKenneth D. Merry 		 */
226f17a6f1bSEitan Adler 		if ((mbp->msg_flags & MSGBUF_NEEDNL) == 0 && prefix_len != 0) {
227d42a4eb5SKenneth D. Merry 			int j;
228d42a4eb5SKenneth D. Merry 
229d42a4eb5SKenneth D. Merry 			for (j = 0; j < prefix_len; j++)
23081dc0033SAlexander Motin 				msgbuf_do_addchar(mbp, prefix[j]);
231d42a4eb5SKenneth D. Merry 		}
232d42a4eb5SKenneth D. Merry 
23324c10828SEitan Adler 		if (msgbuf_show_timestamp && needtime == 1 &&
23424c10828SEitan Adler 		    (mbp->msg_flags & MSGBUF_NEEDNL) == 0) {
23545ae223aSWarner Losh 			if (msgbuf_show_timestamp == 1) {
23624c10828SEitan Adler 				snprintf(buf, sizeof(buf), "[%jd] ",
23724c10828SEitan Adler 				    (intmax_t)time_uptime);
23845ae223aSWarner Losh 			} else {
23945ae223aSWarner Losh 				struct timeval tv;
24045ae223aSWarner Losh 
24145ae223aSWarner Losh 				microuptime(&tv);
24245ae223aSWarner Losh 				snprintf(buf, sizeof(buf), "[%jd.%06d] ",
24345ae223aSWarner Losh 				    (intmax_t)tv.tv_sec, (int)tv.tv_usec);
24445ae223aSWarner Losh 			}
24524c10828SEitan Adler 			for (j = 0; buf[j] != '\0'; j++)
24681dc0033SAlexander Motin 				msgbuf_do_addchar(mbp, buf[j]);
24724c10828SEitan Adler 			needtime = 0;
24824c10828SEitan Adler 		}
24924c10828SEitan Adler 
250d42a4eb5SKenneth D. Merry 		/*
251d42a4eb5SKenneth D. Merry 		 * Don't copy carriage returns if the caller requested
252d42a4eb5SKenneth D. Merry 		 * filtering.
253d42a4eb5SKenneth D. Merry 		 *
254d42a4eb5SKenneth D. Merry 		 * XXX This matches the behavior of msglogchar(), but is it
255d42a4eb5SKenneth D. Merry 		 * necessary?  Testing has shown that we don't seem to get
256d42a4eb5SKenneth D. Merry 		 * carriage returns here.
257d42a4eb5SKenneth D. Merry 		 */
258d42a4eb5SKenneth D. Merry 		if ((filter_cr != 0) && (str[i] == '\r'))
259d42a4eb5SKenneth D. Merry 			continue;
260d42a4eb5SKenneth D. Merry 
261d42a4eb5SKenneth D. Merry 		/*
262d42a4eb5SKenneth D. Merry 		 * Clear this flag if we see a newline.  This affects whether
263d42a4eb5SKenneth D. Merry 		 * we need to insert a new prefix or insert a newline later.
264d42a4eb5SKenneth D. Merry 		 */
265d42a4eb5SKenneth D. Merry 		if (str[i] == '\n')
266f17a6f1bSEitan Adler 			mbp->msg_flags &= ~MSGBUF_NEEDNL;
267d42a4eb5SKenneth D. Merry 		else
268f17a6f1bSEitan Adler 			mbp->msg_flags |= MSGBUF_NEEDNL;
269d42a4eb5SKenneth D. Merry 
27081dc0033SAlexander Motin 		msgbuf_do_addchar(mbp, str[i]);
271d42a4eb5SKenneth D. Merry 	}
27281dc0033SAlexander Motin 	if (mbp->msg_wseq >= mbp->msg_size)
27381dc0033SAlexander Motin 		mbp->msg_flags |= MSGBUF_WRAP;
274d42a4eb5SKenneth D. Merry 
275d42a4eb5SKenneth D. Merry 	/*
276d42a4eb5SKenneth D. Merry 	 * Set the last priority.
277d42a4eb5SKenneth D. Merry 	 */
278d42a4eb5SKenneth D. Merry 	mbp->msg_lastpri = pri;
279d42a4eb5SKenneth D. Merry 
280d42a4eb5SKenneth D. Merry 	mtx_unlock_spin(&mbp->msg_lock);
281d42a4eb5SKenneth D. Merry 
2824784a469SIan Dowse }
2834784a469SIan Dowse 
2844784a469SIan Dowse /*
2854784a469SIan Dowse  * Read and mark as read a character from a message buffer.
2864784a469SIan Dowse  * Returns the character, or -1 if no characters are available.
2874784a469SIan Dowse  */
2884784a469SIan Dowse int
msgbuf_getchar(struct msgbuf * mbp)2894784a469SIan Dowse msgbuf_getchar(struct msgbuf *mbp)
2904784a469SIan Dowse {
2914784a469SIan Dowse 	u_int len, wseq;
2924784a469SIan Dowse 	int c;
2934784a469SIan Dowse 
294d42a4eb5SKenneth D. Merry 	mtx_lock_spin(&mbp->msg_lock);
295d42a4eb5SKenneth D. Merry 
2964784a469SIan Dowse 	wseq = mbp->msg_wseq;
2974784a469SIan Dowse 	len = MSGBUF_SEQSUB(mbp, wseq, mbp->msg_rseq);
298d42a4eb5SKenneth D. Merry 	if (len == 0) {
299d42a4eb5SKenneth D. Merry 		mtx_unlock_spin(&mbp->msg_lock);
3004784a469SIan Dowse 		return (-1);
301d42a4eb5SKenneth D. Merry 	}
3024784a469SIan Dowse 	if (len > mbp->msg_size)
30381dc0033SAlexander Motin 		mbp->msg_rseq = MSGBUF_SEQSUB(mbp, wseq, mbp->msg_size);
3044784a469SIan Dowse 	c = (u_char)mbp->msg_ptr[MSGBUF_SEQ_TO_POS(mbp, mbp->msg_rseq)];
30581dc0033SAlexander Motin 	mbp->msg_rseq = MSGBUF_SEQADD(mbp, mbp->msg_rseq, 1);
306d42a4eb5SKenneth D. Merry 
307d42a4eb5SKenneth D. Merry 	mtx_unlock_spin(&mbp->msg_lock);
308d42a4eb5SKenneth D. Merry 
3094784a469SIan Dowse 	return (c);
3104784a469SIan Dowse }
3114784a469SIan Dowse 
3124784a469SIan Dowse /*
3134784a469SIan Dowse  * Read and mark as read a number of characters from a message buffer.
3144784a469SIan Dowse  * Returns the number of characters that were placed in `buf'.
3154784a469SIan Dowse  */
3164784a469SIan Dowse int
msgbuf_getbytes(struct msgbuf * mbp,char * buf,int buflen)3174784a469SIan Dowse msgbuf_getbytes(struct msgbuf *mbp, char *buf, int buflen)
3184784a469SIan Dowse {
3194784a469SIan Dowse 	u_int len, pos, wseq;
3204784a469SIan Dowse 
321d42a4eb5SKenneth D. Merry 	mtx_lock_spin(&mbp->msg_lock);
322d42a4eb5SKenneth D. Merry 
3234784a469SIan Dowse 	wseq = mbp->msg_wseq;
3244784a469SIan Dowse 	len = MSGBUF_SEQSUB(mbp, wseq, mbp->msg_rseq);
325d42a4eb5SKenneth D. Merry 	if (len == 0) {
326d42a4eb5SKenneth D. Merry 		mtx_unlock_spin(&mbp->msg_lock);
3274784a469SIan Dowse 		return (0);
328d42a4eb5SKenneth D. Merry 	}
3294784a469SIan Dowse 	if (len > mbp->msg_size) {
33081dc0033SAlexander Motin 		mbp->msg_rseq = MSGBUF_SEQSUB(mbp, wseq, mbp->msg_size);
3314784a469SIan Dowse 		len = mbp->msg_size;
3324784a469SIan Dowse 	}
3334784a469SIan Dowse 	pos = MSGBUF_SEQ_TO_POS(mbp, mbp->msg_rseq);
3344784a469SIan Dowse 	len = min(len, mbp->msg_size - pos);
3354784a469SIan Dowse 	len = min(len, (u_int)buflen);
3364784a469SIan Dowse 
3374784a469SIan Dowse 	bcopy(&mbp->msg_ptr[pos], buf, len);
33881dc0033SAlexander Motin 	mbp->msg_rseq = MSGBUF_SEQADD(mbp, mbp->msg_rseq, len);
339d42a4eb5SKenneth D. Merry 
340d42a4eb5SKenneth D. Merry 	mtx_unlock_spin(&mbp->msg_lock);
341d42a4eb5SKenneth D. Merry 
3424784a469SIan Dowse 	return (len);
3434784a469SIan Dowse }
3444784a469SIan Dowse 
3454784a469SIan Dowse /*
3464784a469SIan Dowse  * Peek at the full contents of a message buffer without marking any
3474784a469SIan Dowse  * data as read. `seqp' should point to an unsigned integer that
3484784a469SIan Dowse  * msgbuf_peekbytes() can use to retain state between calls so that
3494784a469SIan Dowse  * the whole message buffer can be read in multiple short reads.
3504784a469SIan Dowse  * To initialise this variable to the start of the message buffer,
3514784a469SIan Dowse  * call msgbuf_peekbytes() with a NULL `buf' parameter.
3524784a469SIan Dowse  *
3534784a469SIan Dowse  * Returns the number of characters that were placed in `buf'.
3544784a469SIan Dowse  */
3554784a469SIan Dowse int
msgbuf_peekbytes(struct msgbuf * mbp,char * buf,int buflen,u_int * seqp)3564784a469SIan Dowse msgbuf_peekbytes(struct msgbuf *mbp, char *buf, int buflen, u_int *seqp)
3574784a469SIan Dowse {
3584784a469SIan Dowse 	u_int len, pos, wseq;
3594784a469SIan Dowse 
360d42a4eb5SKenneth D. Merry 	mtx_lock_spin(&mbp->msg_lock);
361d42a4eb5SKenneth D. Merry 
3624784a469SIan Dowse 	if (buf == NULL) {
3634784a469SIan Dowse 		/* Just initialise *seqp. */
36481dc0033SAlexander Motin 		if (mbp->msg_flags & MSGBUF_WRAP)
36581dc0033SAlexander Motin 			*seqp = MSGBUF_SEQSUB(mbp, mbp->msg_wseq, mbp->msg_size);
36681dc0033SAlexander Motin 		else
36781dc0033SAlexander Motin 			*seqp = 0;
368d42a4eb5SKenneth D. Merry 		mtx_unlock_spin(&mbp->msg_lock);
3694784a469SIan Dowse 		return (0);
3704784a469SIan Dowse 	}
3714784a469SIan Dowse 
3724784a469SIan Dowse 	wseq = mbp->msg_wseq;
3734784a469SIan Dowse 	len = MSGBUF_SEQSUB(mbp, wseq, *seqp);
374d42a4eb5SKenneth D. Merry 	if (len == 0) {
375d42a4eb5SKenneth D. Merry 		mtx_unlock_spin(&mbp->msg_lock);
3764784a469SIan Dowse 		return (0);
377d42a4eb5SKenneth D. Merry 	}
3784784a469SIan Dowse 	if (len > mbp->msg_size) {
37981dc0033SAlexander Motin 		*seqp = MSGBUF_SEQSUB(mbp, wseq, mbp->msg_size);
3804784a469SIan Dowse 		len = mbp->msg_size;
3814784a469SIan Dowse 	}
3824784a469SIan Dowse 	pos = MSGBUF_SEQ_TO_POS(mbp, *seqp);
3834784a469SIan Dowse 	len = min(len, mbp->msg_size - pos);
3844784a469SIan Dowse 	len = min(len, (u_int)buflen);
3854784a469SIan Dowse 	bcopy(&mbp->msg_ptr[MSGBUF_SEQ_TO_POS(mbp, *seqp)], buf, len);
38681dc0033SAlexander Motin 	*seqp = MSGBUF_SEQADD(mbp, *seqp, len);
387d42a4eb5SKenneth D. Merry 
388d42a4eb5SKenneth D. Merry 	mtx_unlock_spin(&mbp->msg_lock);
389d42a4eb5SKenneth D. Merry 
3904784a469SIan Dowse 	return (len);
3914784a469SIan Dowse }
3924784a469SIan Dowse 
3934784a469SIan Dowse /*
3944784a469SIan Dowse  * Compute the checksum for the complete message buffer contents.
3954784a469SIan Dowse  */
3964784a469SIan Dowse static u_int
msgbuf_cksum(struct msgbuf * mbp)3974784a469SIan Dowse msgbuf_cksum(struct msgbuf *mbp)
3984784a469SIan Dowse {
3994784a469SIan Dowse 	u_int i, sum;
4004784a469SIan Dowse 
4014784a469SIan Dowse 	sum = 0;
4024784a469SIan Dowse 	for (i = 0; i < mbp->msg_size; i++)
4034784a469SIan Dowse 		sum += (u_char)mbp->msg_ptr[i];
4044784a469SIan Dowse 	return (sum);
4054784a469SIan Dowse }
4064784a469SIan Dowse 
4074784a469SIan Dowse /*
4084784a469SIan Dowse  * Copy from one message buffer to another.
4094784a469SIan Dowse  */
4104784a469SIan Dowse void
msgbuf_copy(struct msgbuf * src,struct msgbuf * dst)4114784a469SIan Dowse msgbuf_copy(struct msgbuf *src, struct msgbuf *dst)
4124784a469SIan Dowse {
4134784a469SIan Dowse 	int c;
4144784a469SIan Dowse 
4154784a469SIan Dowse 	while ((c = msgbuf_getchar(src)) >= 0)
4164784a469SIan Dowse 		msgbuf_addchar(dst, c);
4174784a469SIan Dowse }
418588ab3c7SMitchell Horne 
419588ab3c7SMitchell Horne /*
420588ab3c7SMitchell Horne  * Get a snapshot of the message buffer, without modifying its internal state
421588ab3c7SMitchell Horne  * (i.e. don't mark any new characters as read).
422588ab3c7SMitchell Horne  */
423588ab3c7SMitchell Horne void
msgbuf_duplicate(struct msgbuf * src,struct msgbuf * dst,char * dst_msgptr)424588ab3c7SMitchell Horne msgbuf_duplicate(struct msgbuf *src, struct msgbuf *dst, char *dst_msgptr)
425588ab3c7SMitchell Horne {
426588ab3c7SMitchell Horne 
427588ab3c7SMitchell Horne 	mtx_lock_spin(&src->msg_lock);
428588ab3c7SMitchell Horne 	bcopy(src, dst, sizeof(struct msgbuf));
429588ab3c7SMitchell Horne 	dst->msg_ptr = dst_msgptr;
430588ab3c7SMitchell Horne 	bcopy(src->msg_ptr, dst->msg_ptr, src->msg_size);
431588ab3c7SMitchell Horne 	mtx_unlock_spin(&src->msg_lock);
432588ab3c7SMitchell Horne }
433