Deleted Added
full compact
subr_msgbuf.c (231814) subr_msgbuf.c (233135)
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 231814 2012-02-16 05:11:35Z eadler $
25 * $FreeBSD: head/sys/kern/subr_msgbuf.c 233135 2012-03-19 00:36:32Z eadler $
26 */
27
28/*
29 * Generic message buffer support routines.
30 */
31
32#include <sys/param.h>
33#include <sys/systm.h>

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

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/*
26 */
27
28/*
29 * Generic message buffer support routines.
30 */
31
32#include <sys/param.h>
33#include <sys/systm.h>

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

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 *
52 * Timestamps in msgbuf are useful when trying to diagnose when core dumps
53 * or other actions occured.
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/*
60 * Initialize a message buffer of the specified size at the specified

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

138}
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 */
54 */
55static int msgbuf_show_timestamp = 0;
56SYSCTL_INT(_kern, OID_AUTO, msgbuf_show_timestamp, CTLFLAG_RW | CTLFLAG_TUN,
57 &msgbuf_show_timestamp, 0, "Show timestamp in msgbuf");
58TUNABLE_INT("kern.msgbuf_show_timestamp", &msgbuf_show_timestamp);
59
60/*
61 * Initialize a message buffer of the specified size at the specified

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

139}
140
141/*
142 * Add a character into the message buffer, and update the checksum and
143 * sequence number.
144 *
145 * The caller should hold the message buffer spinlock.
146 */
146static inline void
147__msgbuf_do_addchar(struct msgbuf * const mbp, u_int * const seq, const int c)
147
148static void
149msgbuf_do_addchar(struct msgbuf * const mbp, u_int * const seq, const int c)
148{
149 u_int pos;
150
151 /* Make sure we properly wrap the sequence number. */
152 pos = MSGBUF_SEQ_TO_POS(mbp, *seq);
150{
151 u_int pos;
152
153 /* Make sure we properly wrap the sequence number. */
154 pos = MSGBUF_SEQ_TO_POS(mbp, *seq);
153
154 mbp->msg_cksum += (u_int)c -
155 mbp->msg_cksum += (u_int)(u_char)c -
155 (u_int)(u_char)mbp->msg_ptr[pos];
156 (u_int)(u_char)mbp->msg_ptr[pos];
156
157 mbp->msg_ptr[pos] = c;
157 mbp->msg_ptr[pos] = c;
158
159 *seq = MSGBUF_SEQNORM(mbp, *seq + 1);
160}
161
158 *seq = MSGBUF_SEQNORM(mbp, *seq + 1);
159}
160
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
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

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

208 * carriage returns down this path. So do we still need it?
209 */
210void
211msgbuf_addstr(struct msgbuf *mbp, int pri, char *str, int filter_cr)
212{
213 u_int seq;
214 size_t len, prefix_len;
215 char prefix[MAXPRIBUF];
161/*
162 * Append a character to a message buffer.
163 */
164void
165msgbuf_addchar(struct msgbuf *mbp, int c)
166{
167 mtx_lock_spin(&mbp->msg_lock);
168

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

180 * carriage returns down this path. So do we still need it?
181 */
182void
183msgbuf_addstr(struct msgbuf *mbp, int pri, char *str, int filter_cr)
184{
185 u_int seq;
186 size_t len, prefix_len;
187 char prefix[MAXPRIBUF];
216 int nl, i;
188 char buf[32];
189 int nl, i, j, needtime;
217
218 len = strlen(str);
219 prefix_len = 0;
220 nl = 0;
221
222 /* If we have a zero-length string, no need to do anything. */
223 if (len == 0)
224 return;

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

245 * insert a newline before this string.
246 */
247 if (mbp->msg_lastpri != pri && (mbp->msg_flags & MSGBUF_NEEDNL) != 0) {
248
249 msgbuf_do_addchar(mbp, &seq, '\n');
250 mbp->msg_flags &= ~MSGBUF_NEEDNL;
251 }
252
190
191 len = strlen(str);
192 prefix_len = 0;
193 nl = 0;
194
195 /* If we have a zero-length string, no need to do anything. */
196 if (len == 0)
197 return;

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

218 * insert a newline before this string.
219 */
220 if (mbp->msg_lastpri != pri && (mbp->msg_flags & MSGBUF_NEEDNL) != 0) {
221
222 msgbuf_do_addchar(mbp, &seq, '\n');
223 mbp->msg_flags &= ~MSGBUF_NEEDNL;
224 }
225
226 needtime = 1;
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 */
259 if ((mbp->msg_flags & MSGBUF_NEEDNL) == 0 && prefix_len != 0) {
260 int j;
261
262 for (j = 0; j < prefix_len; j++)
263 msgbuf_do_addchar(mbp, &seq, prefix[j]);
264 }
265
227 for (i = 0; i < len; i++) {
228 /*
229 * If we just had a newline, and the priority is not -1
230 * (and therefore prefix_len != 0), then we need a priority
231 * prefix for this line.
232 */
233 if ((mbp->msg_flags & MSGBUF_NEEDNL) == 0 && prefix_len != 0) {
234 int j;
235
236 for (j = 0; j < prefix_len; j++)
237 msgbuf_do_addchar(mbp, &seq, prefix[j]);
238 }
239
240 if (msgbuf_show_timestamp && needtime == 1 &&
241 (mbp->msg_flags & MSGBUF_NEEDNL) == 0) {
242
243 snprintf(buf, sizeof(buf), "[%jd] ",
244 (intmax_t)time_uptime);
245 for (j = 0; buf[j] != '\0'; j++)
246 msgbuf_do_addchar(mbp, &seq, buf[j]);
247 needtime = 0;
248 }
249
266 /*
267 * Don't copy carriage returns if the caller requested
268 * filtering.
269 *
270 * XXX This matches the behavior of msglogchar(), but is it
271 * necessary? Testing has shown that we don't seem to get
272 * carriage returns here.
273 */

--- 161 unchanged lines hidden ---
250 /*
251 * Don't copy carriage returns if the caller requested
252 * filtering.
253 *
254 * XXX This matches the behavior of msglogchar(), but is it
255 * necessary? Testing has shown that we don't seem to get
256 * carriage returns here.
257 */

--- 161 unchanged lines hidden ---