Deleted Added
full compact
subr_msgbuf.c (222550) subr_msgbuf.c (231814)
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 222550 2011-05-31 22:39:32Z ken $
25 * $FreeBSD: head/sys/kern/subr_msgbuf.c 231814 2012-02-16 05:11:35Z eadler $
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>
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>
35#include <sys/mutex.h>
36#include <sys/msgbuf.h>
36#include <sys/mutex.h>
37#include <sys/msgbuf.h>
38#include <sys/sysctl.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/*
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
49static u_int msgbuf_cksum(struct msgbuf *mbp);
50
51/*
52 *
53 */
54static int msgbuf_show_timestamp = 0;
55SYSCTL_INT(_kern, OID_AUTO, msgbuf_show_timestamp, CTLFLAG_RW | CTLFLAG_TUN,
56 &msgbuf_show_timestamp, 0, "Show timestamp in msgbuf");
57TUNABLE_INT("kern.msgbuf_show_timestamp", &msgbuf_show_timestamp);
58
59/*
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;
60 * Initialize a message buffer of the specified size at the specified
61 * location. This also zeros the buffer area.
62 */
63void
64msgbuf_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 msgbuf_clear(mbp);
71 mbp->msg_magic = MSG_MAGIC;
72 mbp->msg_lastpri = -1;
63 mbp->msg_needsnl = 0;
73 mbp->msg_flags = 0;
64 bzero(&mbp->msg_lock, sizeof(mbp->msg_lock));
65 mtx_init(&mbp->msg_lock, "msgbuf", NULL, MTX_SPIN);
66}
67
68/*
69 * Reinitialize a message buffer, retaining its previous contents if
70 * the size and checksum are correct. If the old contents cannot be
71 * recovered, the message buffer is cleared.

--- 18 unchanged lines hidden (view full) ---

90 mbp->msg_cksum, cksum);
91 printf("Old msgbuf not recovered\n");
92 }
93 msgbuf_clear(mbp);
94 }
95
96 mbp->msg_lastpri = -1;
97 /* Assume that the old message buffer didn't end in a newline. */
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.

--- 18 unchanged lines hidden (view full) ---

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. */
98 mbp->msg_needsnl = 1;
108 mbp->msg_flags |= MSGBUF_NEEDNL;
99 bzero(&mbp->msg_lock, sizeof(mbp->msg_lock));
100 mtx_init(&mbp->msg_lock, "msgbuf", NULL, MTX_SPIN);
101}
102
103/*
104 * Clear the message buffer.
105 */
106void

--- 22 unchanged lines hidden (view full) ---

129
130/*
131 * Add a character into the message buffer, and update the checksum and
132 * sequence number.
133 *
134 * The caller should hold the message buffer spinlock.
135 */
136static inline void
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 */
116void

--- 22 unchanged lines hidden (view full) ---

139
140/*
141 * Add a character into the message buffer, and update the checksum and
142 * sequence number.
143 *
144 * The caller should hold the message buffer spinlock.
145 */
146static inline void
137msgbuf_do_addchar(struct msgbuf *mbp, u_int *seq, int c)
147__msgbuf_do_addchar(struct msgbuf * const mbp, u_int * const seq, const int c)
138{
139 u_int pos;
140
141 /* Make sure we properly wrap the sequence number. */
142 pos = MSGBUF_SEQ_TO_POS(mbp, *seq);
143
144 mbp->msg_cksum += (u_int)c -
145 (u_int)(u_char)mbp->msg_ptr[pos];
146
147 mbp->msg_ptr[pos] = c;
148
149 *seq = MSGBUF_SEQNORM(mbp, *seq + 1);
150}
151
148{
149 u_int pos;
150
151 /* Make sure we properly wrap the sequence number. */
152 pos = MSGBUF_SEQ_TO_POS(mbp, *seq);
153
154 mbp->msg_cksum += (u_int)c -
155 (u_int)(u_char)mbp->msg_ptr[pos];
156
157 mbp->msg_ptr[pos] = c;
158
159 *seq = MSGBUF_SEQNORM(mbp, *seq + 1);
160}
161
162static inline void
163msgbuf_do_addchar(struct msgbuf * const mbp, u_int * const seq, const int c)
164{
165
166 if (msgbuf_show_timestamp &&
167 (mbp->msg_flags & MSGBUF_NEXT_NEW_LINE) != 0) {
168 char buf[32];
169 char const *bufp;
170 struct timespec ts;
171 int err;
172
173 getnanouptime(&ts);
174 err = snprintf(buf, sizeof (buf), "[%jd.%ld] ",
175 (intmax_t)ts.tv_sec, ts.tv_nsec / 1000);
176
177 for (bufp = buf; *bufp != '\0'; bufp++)
178 __msgbuf_do_addchar(mbp, seq, *bufp);
179
180 mbp->msg_flags &= ~MSGBUF_NEXT_NEW_LINE;
181 }
182
183 __msgbuf_do_addchar(mbp, seq, c);
184
185 if (c == '\n')
186 mbp->msg_flags |= MSGBUF_NEXT_NEW_LINE;
187}
188
152/*
153 * Append a character to a message buffer.
154 */
155void
156msgbuf_addchar(struct msgbuf *mbp, int c)
157{
158 mtx_lock_spin(&mbp->msg_lock);
159

--- 42 unchanged lines hidden (view full) ---

202
203 /*
204 * Whenever there is a change in priority, we have to insert a
205 * newline, and a priority prefix if the priority is not -1. Here
206 * we detect whether there was a priority change, and whether we
207 * did not end with a newline. If that is the case, we need to
208 * insert a newline before this string.
209 */
189/*
190 * Append a character to a message buffer.
191 */
192void
193msgbuf_addchar(struct msgbuf *mbp, int c)
194{
195 mtx_lock_spin(&mbp->msg_lock);
196

--- 42 unchanged lines hidden (view full) ---

239
240 /*
241 * Whenever there is a change in priority, we have to insert a
242 * newline, and a priority prefix if the priority is not -1. Here
243 * we detect whether there was a priority change, and whether we
244 * did not end with a newline. If that is the case, we need to
245 * insert a newline before this string.
246 */
210 if (mbp->msg_lastpri != pri && mbp->msg_needsnl != 0) {
247 if (mbp->msg_lastpri != pri && (mbp->msg_flags & MSGBUF_NEEDNL) != 0) {
211
212 msgbuf_do_addchar(mbp, &seq, '\n');
248
249 msgbuf_do_addchar(mbp, &seq, '\n');
213 mbp->msg_needsnl = 0;
250 mbp->msg_flags &= ~MSGBUF_NEEDNL;
214 }
215
216 for (i = 0; i < len; i++) {
217 /*
218 * If we just had a newline, and the priority is not -1
219 * (and therefore prefix_len != 0), then we need a priority
220 * prefix for this line.
221 */
251 }
252
253 for (i = 0; i < len; i++) {
254 /*
255 * If we just had a newline, and the priority is not -1
256 * (and therefore prefix_len != 0), then we need a priority
257 * prefix for this line.
258 */
222 if (mbp->msg_needsnl == 0 && prefix_len != 0) {
259 if ((mbp->msg_flags & MSGBUF_NEEDNL) == 0 && prefix_len != 0) {
223 int j;
224
225 for (j = 0; j < prefix_len; j++)
226 msgbuf_do_addchar(mbp, &seq, prefix[j]);
227 }
228
229 /*
230 * Don't copy carriage returns if the caller requested

--- 6 unchanged lines hidden (view full) ---

237 if ((filter_cr != 0) && (str[i] == '\r'))
238 continue;
239
240 /*
241 * Clear this flag if we see a newline. This affects whether
242 * we need to insert a new prefix or insert a newline later.
243 */
244 if (str[i] == '\n')
260 int j;
261
262 for (j = 0; j < prefix_len; j++)
263 msgbuf_do_addchar(mbp, &seq, prefix[j]);
264 }
265
266 /*
267 * Don't copy carriage returns if the caller requested

--- 6 unchanged lines hidden (view full) ---

274 if ((filter_cr != 0) && (str[i] == '\r'))
275 continue;
276
277 /*
278 * Clear this flag if we see a newline. This affects whether
279 * we need to insert a new prefix or insert a newline later.
280 */
281 if (str[i] == '\n')
245 mbp->msg_needsnl = 0;
282 mbp->msg_flags &= ~MSGBUF_NEEDNL;
246 else
283 else
247 mbp->msg_needsnl = 1;
284 mbp->msg_flags |= MSGBUF_NEEDNL;
248
249 msgbuf_do_addchar(mbp, &seq, str[i]);
250 }
251 /*
252 * Update the write sequence number for the actual number of
253 * characters we put in the message buffer. (Depends on whether
254 * carriage returns are filtered.)
255 */

--- 142 unchanged lines hidden ---
285
286 msgbuf_do_addchar(mbp, &seq, str[i]);
287 }
288 /*
289 * Update the write sequence number for the actual number of
290 * characters we put in the message buffer. (Depends on whether
291 * carriage returns are filtered.)
292 */

--- 142 unchanged lines hidden ---