summaryrefslogtreecommitdiffstats
path: root/sys/sys/msgbuf.h
diff options
context:
space:
mode:
authorken <ken@FreeBSD.org>2011-05-31 17:29:58 +0000
committerken <ken@FreeBSD.org>2011-05-31 17:29:58 +0000
commit0febb6df5eb6907033b87d0f65654df70b739241 (patch)
treef7ac793f0da7e570e8087f21ecc87bd6bd969a99 /sys/sys/msgbuf.h
parentd57b0d50d5199b2afa870a6e4ab099e0a8be716f (diff)
downloadFreeBSD-src-0febb6df5eb6907033b87d0f65654df70b739241.zip
FreeBSD-src-0febb6df5eb6907033b87d0f65654df70b739241.tar.gz
Fix apparent garbage in the message buffer.
While we have had a fix in place (options PRINTF_BUFR_SIZE=128) to fix scrambled console output, the message buffer and syslog were still getting log messages one character at a time. While all of the characters still made it into the log (courtesy of atomic operations), they were often interleaved when there were multiple threads writing to the buffer at the same time. This fixes message buffer accesses to use buffering logic as well, so that strings that are less than PRINTF_BUFR_SIZE will be put into the message buffer atomically. So now dmesg output should look the same as console output. subr_msgbuf.c: Convert most message buffer calls to use a new spin lock instead of atomic variables in some places. Add a new routine, msgbuf_addstr(), that adds a NUL-terminated string to a message buffer. This takes a priority argument, which allows us to eliminate some races (at least in the the string at a time case) that are present in the implementation of msglogchar(). (dangling and lastpri are static variables, and are subject to races when multiple callers are present.) msgbuf_addstr() also allows the caller to request that carriage returns be stripped out of the string. This matches the behavior of msglogchar(), but in testing so far it doesn't appear that any newlines are being stripped out. So the carriage return removal functionality may be a candidate for removal later on if further analysis shows that it isn't necessary. subr_prf.c: Add a new msglogstr() routine that calls msgbuf_logstr(). Rename putcons() to putbuf(). This now handles buffered output to the message log as well as the console. Also, remove the logic in putcons() (now putbuf()) that added a carriage return before a newline. The console path was the only path that needed it, and cnputc() (called by cnputs()) already adds a carriage return. So this duplication resulted in kernel-generated console output lines ending in '\r''\r''\n'. Refactor putchar() to handle the new buffering scheme. Add buffering to log(). Change log_console() to use msglogstr() instead of msglogchar(). Don't add extra newlines by default in log_console(). Hide that behavior behind a tunable/sysctl (kern.log_console_add_linefeed) for those who would like the old behavior. The old behavior led to the insertion of extra newlines for log output for programs that print out a string, and then a trailing newline on a separate write. (This is visible with dmesg -a.) msgbuf.h: Add a prototype for msgbuf_addstr(). Add three new fields to struct msgbuf, msg_needsnl, msg_lastpri and msg_lock. The first two are needed for log message functionality previously handled by msglogchar(). (Which is still active if buffering isn't enabled.) Include sys/lock.h and sys/mutex.h for the new mutex. Reviewed by: gibbs
Diffstat (limited to 'sys/sys/msgbuf.h')
-rw-r--r--sys/sys/msgbuf.h21
1 files changed, 14 insertions, 7 deletions
diff --git a/sys/sys/msgbuf.h b/sys/sys/msgbuf.h
index 8435c68..67f80a5 100644
--- a/sys/sys/msgbuf.h
+++ b/sys/sys/msgbuf.h
@@ -33,15 +33,21 @@
#ifndef _SYS_MSGBUF_H_
#define _SYS_MSGBUF_H_
+#include <sys/lock.h>
+#include <sys/mutex.h>
+
struct msgbuf {
- char *msg_ptr; /* pointer to buffer */
+ char *msg_ptr; /* pointer to buffer */
#define MSG_MAGIC 0x063062
- u_int msg_magic;
- u_int msg_size; /* size of buffer area */
- u_int msg_wseq; /* write sequence number */
- u_int msg_rseq; /* read sequence number */
- u_int msg_cksum; /* checksum of contents */
- u_int msg_seqmod; /* range for sequence numbers */
+ u_int msg_magic;
+ u_int msg_size; /* size of buffer area */
+ u_int msg_wseq; /* write sequence number */
+ u_int msg_rseq; /* read sequence number */
+ u_int msg_cksum; /* checksum of contents */
+ u_int msg_seqmod; /* range for sequence numbers */
+ int msg_lastpri; /* saved priority value */
+ int msg_needsnl; /* set when newline needed */
+ struct mtx msg_lock; /* mutex to protect the buffer */
};
/* Normalise a sequence number or a difference between sequence numbers. */
@@ -59,6 +65,7 @@ extern struct mtx msgbuf_lock;
void msgbufinit(void *ptr, int size);
void msgbuf_addchar(struct msgbuf *mbp, int c);
+void msgbuf_addstr(struct msgbuf *mbp, int pri, char *str, int filter_cr);
void msgbuf_clear(struct msgbuf *mbp);
void msgbuf_copy(struct msgbuf *src, struct msgbuf *dst);
int msgbuf_getbytes(struct msgbuf *mbp, char *buf, int buflen);
OpenPOWER on IntegriCloud