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