subr_msgbuf.c (139804) | subr_msgbuf.c (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. --- 8 unchanged lines hidden (view full) --- 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 * | 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. --- 8 unchanged lines hidden (view full) --- 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 139804 2005-01-06 23:35:40Z imp $ | 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> | 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> |
|
34#include <sys/msgbuf.h> 35 | 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 |
|
36/* Read/write sequence numbers are modulo a multiple of the buffer size. */ 37#define SEQMOD(size) ((size) * 16) 38 39static u_int msgbuf_cksum(struct msgbuf *mbp); 40 41/* 42 * Initialize a message buffer of the specified size at the specified 43 * location. This also zeros the buffer area. 44 */ 45void 46msgbuf_init(struct msgbuf *mbp, void *ptr, int size) 47{ 48 49 mbp->msg_ptr = ptr; 50 mbp->msg_size = size; 51 mbp->msg_seqmod = SEQMOD(size); 52 msgbuf_clear(mbp); 53 mbp->msg_magic = MSG_MAGIC; | 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); |
|
54} 55 56/* 57 * Reinitialize a message buffer, retaining its previous contents if 58 * the size and checksum are correct. If the old contents cannot be 59 * recovered, the message buffer is cleared. 60 */ 61void --- 13 unchanged lines hidden (view full) --- 75 if (cksum != mbp->msg_cksum) { 76 if (bootverbose) { 77 printf("msgbuf cksum mismatch (read %x, calc %x)\n", 78 mbp->msg_cksum, cksum); 79 printf("Old msgbuf not recovered\n"); 80 } 81 msgbuf_clear(mbp); 82 } | 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 --- 13 unchanged lines hidden (view full) --- 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); |
|
83} 84 85/* 86 * Clear the message buffer. 87 */ 88void 89msgbuf_clear(struct msgbuf *mbp) 90{ --- 14 unchanged lines hidden (view full) --- 105 106 len = MSGBUF_SEQSUB(mbp, mbp->msg_wseq, mbp->msg_rseq); 107 if (len > mbp->msg_size) 108 len = mbp->msg_size; 109 return (len); 110} 111 112/* | 99} 100 101/* 102 * Clear the message buffer. 103 */ 104void 105msgbuf_clear(struct msgbuf *mbp) 106{ --- 14 unchanged lines hidden (view full) --- 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/* |
113 * Append a character to a message buffer. This function can be 114 * considered fully reentrant so long as the number of concurrent 115 * callers is less than the number of characters in the buffer. 116 * However, the message buffer is only guaranteed to be consistent 117 * for reading when there are no callers in this function. | 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. |
118 */ | 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 */ |
|
119void 120msgbuf_addchar(struct msgbuf *mbp, int c) 121{ | 153void 154msgbuf_addchar(struct msgbuf *mbp, int c) 155{ |
122 u_int new_seq, pos, seq; | 156 mtx_lock_spin(&mbp->msg_lock); |
123 | 157 |
124 do { 125 seq = mbp->msg_wseq; 126 new_seq = MSGBUF_SEQNORM(mbp, seq + 1); 127 } while (atomic_cmpset_rel_int(&mbp->msg_wseq, seq, new_seq) == 0); 128 pos = MSGBUF_SEQ_TO_POS(mbp, seq); 129 atomic_add_int(&mbp->msg_cksum, (u_int)(u_char)c - 130 (u_int)(u_char)mbp->msg_ptr[pos]); 131 mbp->msg_ptr[pos] = c; | 158 msgbuf_do_addchar(mbp, &mbp->msg_wseq, c); 159 160 mtx_unlock_spin(&mbp->msg_lock); |
132} 133 134/* | 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/* |
|
135 * Read and mark as read a character from a message buffer. 136 * Returns the character, or -1 if no characters are available. 137 */ 138int 139msgbuf_getchar(struct msgbuf *mbp) 140{ 141 u_int len, wseq; 142 int c; 143 | 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 |
|
144 wseq = mbp->msg_wseq; 145 len = MSGBUF_SEQSUB(mbp, wseq, mbp->msg_rseq); | 277 wseq = mbp->msg_wseq; 278 len = MSGBUF_SEQSUB(mbp, wseq, mbp->msg_rseq); |
146 if (len == 0) | 279 if (len == 0) { 280 mtx_unlock_spin(&mbp->msg_lock); |
147 return (-1); | 281 return (-1); |
282 } |
|
148 if (len > mbp->msg_size) 149 mbp->msg_rseq = MSGBUF_SEQNORM(mbp, wseq - mbp->msg_size); 150 c = (u_char)mbp->msg_ptr[MSGBUF_SEQ_TO_POS(mbp, mbp->msg_rseq)]; 151 mbp->msg_rseq = MSGBUF_SEQNORM(mbp, mbp->msg_rseq + 1); | 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 |
|
152 return (c); 153} 154 155/* 156 * Read and mark as read a number of characters from a message buffer. 157 * Returns the number of characters that were placed in `buf'. 158 */ 159int 160msgbuf_getbytes(struct msgbuf *mbp, char *buf, int buflen) 161{ 162 u_int len, pos, wseq; 163 | 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 |
|
164 wseq = mbp->msg_wseq; 165 len = MSGBUF_SEQSUB(mbp, wseq, mbp->msg_rseq); | 304 wseq = mbp->msg_wseq; 305 len = MSGBUF_SEQSUB(mbp, wseq, mbp->msg_rseq); |
166 if (len == 0) | 306 if (len == 0) { 307 mtx_unlock_spin(&mbp->msg_lock); |
167 return (0); | 308 return (0); |
309 } |
|
168 if (len > mbp->msg_size) { 169 mbp->msg_rseq = MSGBUF_SEQNORM(mbp, wseq - mbp->msg_size); 170 len = mbp->msg_size; 171 } 172 pos = MSGBUF_SEQ_TO_POS(mbp, mbp->msg_rseq); 173 len = min(len, mbp->msg_size - pos); 174 len = min(len, (u_int)buflen); 175 176 bcopy(&mbp->msg_ptr[pos], buf, len); 177 mbp->msg_rseq = MSGBUF_SEQNORM(mbp, mbp->msg_rseq + len); | 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 |
|
178 return (len); 179} 180 181/* 182 * Peek at the full contents of a message buffer without marking any 183 * data as read. `seqp' should point to an unsigned integer that 184 * msgbuf_peekbytes() can use to retain state between calls so that 185 * the whole message buffer can be read in multiple short reads. 186 * To initialise this variable to the start of the message buffer, 187 * call msgbuf_peekbytes() with a NULL `buf' parameter. 188 * 189 * Returns the number of characters that were placed in `buf'. 190 */ 191int 192msgbuf_peekbytes(struct msgbuf *mbp, char *buf, int buflen, u_int *seqp) 193{ 194 u_int len, pos, wseq; 195 | 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 |
|
196 if (buf == NULL) { 197 /* Just initialise *seqp. */ 198 *seqp = MSGBUF_SEQNORM(mbp, mbp->msg_wseq - mbp->msg_size); | 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); |
|
199 return (0); 200 } 201 202 wseq = mbp->msg_wseq; 203 len = MSGBUF_SEQSUB(mbp, wseq, *seqp); | 347 return (0); 348 } 349 350 wseq = mbp->msg_wseq; 351 len = MSGBUF_SEQSUB(mbp, wseq, *seqp); |
204 if (len == 0) | 352 if (len == 0) { 353 mtx_unlock_spin(&mbp->msg_lock); |
205 return (0); | 354 return (0); |
355 } |
|
206 if (len > mbp->msg_size) { 207 *seqp = MSGBUF_SEQNORM(mbp, wseq - mbp->msg_size); 208 len = mbp->msg_size; 209 } 210 pos = MSGBUF_SEQ_TO_POS(mbp, *seqp); 211 len = min(len, mbp->msg_size - pos); 212 len = min(len, (u_int)buflen); 213 bcopy(&mbp->msg_ptr[MSGBUF_SEQ_TO_POS(mbp, *seqp)], buf, len); 214 *seqp = MSGBUF_SEQNORM(mbp, *seqp + len); | 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 |
|
215 return (len); 216} 217 218/* 219 * Compute the checksum for the complete message buffer contents. 220 */ 221static u_int 222msgbuf_cksum(struct msgbuf *mbp) --- 20 unchanged lines hidden --- | 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) --- 20 unchanged lines hidden --- |