mirror of
https://github.com/opnsense/src.git
synced 2026-02-13 15:57:05 -05:00
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
84 lines
3.4 KiB
C
84 lines
3.4 KiB
C
/*-
|
|
* Copyright (c) 1981, 1984, 1993
|
|
* The Regents of the University of California. All rights reserved.
|
|
*
|
|
* Redistribution and use in source and binary forms, with or without
|
|
* modification, are permitted provided that the following conditions
|
|
* are met:
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
* notice, this list of conditions and the following disclaimer.
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
* documentation and/or other materials provided with the distribution.
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
* may be used to endorse or promote products derived from this software
|
|
* without specific prior written permission.
|
|
*
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
* SUCH DAMAGE.
|
|
*
|
|
* @(#)msgbuf.h 8.1 (Berkeley) 6/2/93
|
|
* $FreeBSD$
|
|
*/
|
|
|
|
#ifndef _SYS_MSGBUF_H_
|
|
#define _SYS_MSGBUF_H_
|
|
|
|
#include <sys/lock.h>
|
|
#include <sys/mutex.h>
|
|
|
|
struct msgbuf {
|
|
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 */
|
|
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. */
|
|
#define MSGBUF_SEQNORM(mbp, seq) (((seq) + (mbp)->msg_seqmod) % \
|
|
(mbp)->msg_seqmod)
|
|
#define MSGBUF_SEQ_TO_POS(mbp, seq) ((seq) % (mbp)->msg_size)
|
|
/* Subtract sequence numbers. Note that only positive values result. */
|
|
#define MSGBUF_SEQSUB(mbp, seq1, seq2) (MSGBUF_SEQNORM((mbp), (seq1) - (seq2)))
|
|
|
|
#ifdef _KERNEL
|
|
extern int msgbufsize;
|
|
extern int msgbuftrigger;
|
|
extern struct msgbuf *msgbufp;
|
|
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);
|
|
int msgbuf_getchar(struct msgbuf *mbp);
|
|
int msgbuf_getcount(struct msgbuf *mbp);
|
|
void msgbuf_init(struct msgbuf *mbp, void *ptr, int size);
|
|
int msgbuf_peekbytes(struct msgbuf *mbp, char *buf, int buflen,
|
|
u_int *seqp);
|
|
void msgbuf_reinit(struct msgbuf *mbp, void *ptr, int size);
|
|
|
|
#ifndef MSGBUF_SIZE
|
|
#define MSGBUF_SIZE (32768 * 2)
|
|
#endif
|
|
#endif /* KERNEL */
|
|
|
|
#endif /* !_SYS_MSGBUF_H_ */
|